Logo Search packages:      
Sourcecode: mailody version File versions  Download package

sidebar.cpp

Go to the documentation of this file.
/* This file is part of the KDE project

   Copyright 2005-2006 by Joern Ahrens <joern.ahrens@kdemail.net>
   Copyright 2006 by Gilles Caulier <caulier.gilles@kdemail.net>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

/** @file sidebar.cpp */

// QT includes.

#include <qsplitter.h>
#include <qwidgetstack.h>
#include <qdatastream.h>

// KDE includes.

#include <kapplication.h>
#include <kconfig.h>
#include <kdeversion.h>
#include <kmultitabbar.h>
#include <kiconloader.h>

// Local includes.

#include "sidebar.h"

namespace SideBar {

class SidebarPriv
{
public:

    SidebarPriv()
    {
        stack     = 0;
        splitter  = 0;
        tabs      = 0;
        activeTab = -1;
        minimized = false;
    }

    bool          minimizedDefault;
    bool          minimized;
    bool          isMinimized;

    int           tabs;
    int           activeTab;
    int           minSize;
    int           maxSize;

    QWidgetStack *stack;
    QSplitter    *splitter;
    QSize         bigSize;

    Sidebar::Side side;
};

00072 Sidebar::Sidebar(QWidget *parent, const char *name, Side side, bool minimizedDefault)
       : KMultiTabBar(KMultiTabBar::Vertical, parent, name)
{
    d = new SidebarPriv;
    d->minimizedDefault = minimizedDefault;
    d->side = side;
}

Sidebar::~Sidebar()
{
    saveViewState();
    delete d;
}

00086 void Sidebar::setSplitter(QSplitter *sp)
{
#if KDE_IS_VERSION(3,3,0)
    setStyle(KMultiTabBar::KDEV3ICON);
#else
    setStyle(KMultiTabBar::KDEV3);
#endif
    showActiveTabTexts(true);
    d->stack = new QWidgetStack(sp);

    if(d->side == Left)
        setPosition(KMultiTabBar::Left);
    else
        setPosition(KMultiTabBar::Right);
}

00102 void Sidebar::loadViewState()
{
    int tab;
    int minimized;

    KConfig *config = kapp->config();
    config->setGroup(QString("%1").arg(name()));

    tab = config->readNumEntry("ActiveTab", 0);
    minimized = config->readBoolEntry("Minimized", d->minimizedDefault);

    if(minimized)
    {
        d->activeTab = tab;
        setTab(d->activeTab, true);
        d->stack->raiseWidget(d->activeTab);

        emit signalChangedTab(d->stack->visibleWidget());
    }
    else
        d->activeTab = -1;

    clicked(tab);
}

00127 void Sidebar::saveViewState()
{
    KConfig *config = kapp->config();
    config->setGroup(QString("%1").arg(name()));

    config->writeEntry("ActiveTab", d->activeTab);
    config->writeEntry("Minimized", d->minimized);
    config->sync();
}

00137 void Sidebar::backup()
{
    d->isMinimized = d->minimized;

    if (!d->isMinimized)
        shrink();

    KMultiTabBar::hide();
}

00147 void Sidebar::restore()
{
    if (!d->isMinimized)
        expand();

    KMultiTabBar::show();
}

00155 void Sidebar::appendTab(QWidget *w, const QPixmap &pic, const QString &title)
{
    w->reparent(d->stack, QPoint(0, 0));
    KMultiTabBar::appendTab(pic, d->tabs, title);
    d->stack->addWidget(w, d->tabs);

    connect(tab(d->tabs), SIGNAL(clicked(int)),
            this, SLOT(clicked(int)));

    d->tabs++;
}

00167 void Sidebar::deleteTab(QWidget *w)
{
    int tab = d->stack->id(w);
    if(tab < 0)
        return;

    if(tab == d->activeTab)
        d->activeTab = -1;

    removeTab(tab);
    //TODO show another widget
}

00180 void Sidebar::clicked(int tab)
{
    if(tab >= d->tabs || tab < 0)
        return;

    if(tab == d->activeTab)
    {
        d->stack->isHidden() ? expand() : shrink();
    }
    else
    {
        if(d->activeTab >= 0)
            setTab(d->activeTab, false);

        d->activeTab = tab;
        setTab(d->activeTab, true);
        d->stack->raiseWidget(d->activeTab);

        if(d->minimized)
            expand();

        emit signalChangedTab(d->stack->visibleWidget());
    }
}

00205 void Sidebar::setActiveTab(QWidget *w)
{
    int tab = d->stack->id(w);
    if(tab < 0)
        return;

    if(d->activeTab >= 0)
        setTab(d->activeTab, false);

    d->activeTab = tab;
    setTab(d->activeTab, true);
    d->stack->raiseWidget(d->activeTab);

    if(d->minimized)
        expand();

    emit signalChangedTab(d->stack->visibleWidget());
}

00224 QWidget* Sidebar::getActiveTab() const
{
    return d->stack->visibleWidget();
}

00229 void Sidebar::shrink()
{
    d->minimized = true;
    d->bigSize = size();
    d->minSize = minimumWidth();
    d->maxSize = maximumWidth();

    d->stack->hide();

    KMultiTabBarTab* tab = tabs()->first();
    if (tab)
    {
        setFixedWidth(tab->width());
    }
    else
    {
        setFixedWidth(width());
    }
}

00249 void Sidebar::expand()
{
    d->minimized = false;
    d->stack->show();
    resize(d->bigSize);
    setMinimumWidth(d->minSize);
    setMaximumWidth(d->maxSize);
}

}

#include "sidebar.moc"

Generated by  Doxygen 1.6.0   Back to index