qwwnavigationbar.cpp

00001 //
00002 // C++ Implementation: %{MODULE}
00003 //
00004 // Description:
00005 //
00006 //
00007 // Author: %{AUTHOR} <%{EMAIL}>, (C) %{YEAR}
00008 //
00009 // Copyright: See COPYING file that comes with this distribution
00010 //
00011 //
00012 #include "qwwnavigationbar.h"
00013 #include <QPushButton>
00014 #include <QButtonGroup>
00015 #include <QStackedWidget>
00016 #include <QLayout>
00017 #include <QSplitter>
00018 #include <QToolButton>
00019 #include <QVariant>
00020 #include <QMessageBox>
00021 #include <QActionEvent>
00022 #include "wwglobal_p.h"
00023 
00024 
00032 class QwwNavigationBarPrivate : public QwwPrivate {
00033 public:
00034   QwwNavigationBarPrivate(QwwNavigationBar *q) : QwwPrivate(q){
00035     stack = 0;
00036     buttonLayout = 0;
00037     bottomFrame = 0;
00038     topButton = 0;
00039     splitter = 0;
00040   }
00041   QList<QPushButton *> buttons;
00042   QStackedWidget *stack;
00043   QVBoxLayout *buttonLayout;
00044   QFrame *bottomFrame;
00045   QPushButton *topButton;
00046   QSplitter *splitter;
00047   void setTitle(QWidget *w, const QString &s);
00048   void setIcon(QWidget *w, const QIcon &ic);
00049 };
00050 
00051 QwwNavigationBar::QwwNavigationBar(QWidget *parent)
00052         : QWidget(parent), QwwPrivatable(new QwwNavigationBarPrivate(this)) {
00053     Q_D(QwwNavigationBar);
00054     d->stack = new QStackedWidget(this);
00055     while (d->stack->count()>0)
00056     d->stack->removeWidget(d->stack->widget(0));
00057     QWidget *bw = new QWidget;
00058     bw->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred);
00059     d->buttonLayout = new QVBoxLayout(bw);
00060     d->buttonLayout->setSpacing(0);
00061     d->buttonLayout->setMargin(0);
00062     d->splitter = new QSplitter(Qt::Vertical);
00063 //     splitter->setObjectName("__qt__passive_splitter");
00064     d->splitter->addWidget(d->stack);
00065     d->splitter->addWidget(bw);
00066     QVBoxLayout *l = new QVBoxLayout(this);
00067     d->topButton = new QPushButton;
00068     l->addWidget(d->topButton);
00069     d->topButton->setFlat(true);
00070     l->addWidget(d->splitter);
00071     l->setSpacing(0);
00072     l->setMargin(2);
00073     d->bottomFrame = new QFrame;
00074     d->bottomFrame->setFrameShape(QFrame::NoFrame);
00075     d->bottomFrame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
00076     l->addWidget(d->bottomFrame);
00077     QHBoxLayout *hl = new QHBoxLayout(d->bottomFrame);
00078     hl->setSpacing(2);
00079     hl->setMargin(1);
00080     hl->addStretch(1);
00081     connect(d->stack, SIGNAL(currentChanged(int)), this, SIGNAL(currentIndexChanged(int)));
00082 }
00083 
00084 
00085 QwwNavigationBar::~QwwNavigationBar() {}
00086 
00087 
00094 void QwwNavigationBar::addWidget(QWidget * child, const QIcon &icon, const QString &label) {
00095     Q_D(QwwNavigationBar);
00096     insertWidget(d->stack->count(), child, icon, label);
00097 }
00098 
00105 void QwwNavigationBar::addWidget(QWidget * child, const QString & label) {
00106     addWidget(child, QIcon(), label);
00107 }
00108 
00109 
00110 
00118 void QwwNavigationBar::insertWidget(int index, QWidget * child, const QIcon &icon, const QString &label) {
00119     Q_D(QwwNavigationBar);
00120     QPushButton *b = new QPushButton;
00121     connect(b, SIGNAL(clicked()), SLOT(_q_buttonClicked()));
00122     d->stack->insertWidget(index, child);
00123     d->buttonLayout->insertWidget(index, b);
00124     d->buttons.insert(index, b);
00125 
00126     b->setObjectName("__qt__passive_button");
00127     b->setIcon(child->windowIcon());
00128 
00129     b->updateGeometry();
00130     if(label.isNull()){
00131         b->setText(child->windowTitle());
00132     } else {
00133         d->setTitle(child, label);
00134     }
00135     int h = d->buttonLayout->parentWidget()->sizeHint().height();
00136     d->buttonLayout->parentWidget()->setMaximumSize(16777215, h);
00137     if (d->stack->count()==1) {
00138         setCurrentIndex(0);
00139     }
00140 }
00141 
00149 void QwwNavigationBar::insertWidget(int index, QWidget * child, const QString & label) {
00150     insertWidget(index, child, QIcon(), label);
00151 }
00152 
00153 void QwwNavigationBar::_q_buttonClicked() {
00154     Q_D(QwwNavigationBar);
00155     QPushButton *b = static_cast<QPushButton*>(sender());
00156     int ind = d->buttons.indexOf(b);
00157     d->topButton->setText(b->text());
00158     d->topButton->setIcon(b->icon());
00159     d->stack->setCurrentIndex(ind);
00160 }
00161 
00166 void QwwNavigationBar::removeWidget(int index) {
00167     Q_D(QwwNavigationBar);
00168     delete d->buttons.at(index);
00169     d->buttons.removeAt(index);
00170     d->stack->removeWidget(d->stack->widget(index));
00171     int ind = d->stack->currentIndex();
00172     if (ind>-1) {
00173         QPushButton *b = const_cast<QPushButton*>(button(ind));
00174         d->topButton->setText(b->text());
00175         d->topButton->setIcon(b->icon());
00176     } else {
00177         d->topButton->setText(QString::null);
00178         d->topButton->setIcon(QIcon());
00179     }
00180 }
00181 
00187 int QwwNavigationBar::indexOf(QWidget *w) const {
00188     Q_D(const QwwNavigationBar);
00189     return d->stack->indexOf(w);
00190 }
00191 
00196 int QwwNavigationBar::currentIndex() const {
00197     Q_D(const QwwNavigationBar);
00198     return d->stack->currentIndex();
00199 }
00200 
00205 void QwwNavigationBar::setCurrentIndex(int index) {
00206     Q_D(QwwNavigationBar);
00207     if (index>=d->stack->count() || index<0) return;
00208     d->stack->setCurrentIndex(index);
00209     QWidget *w = widget(index);
00210     if(!w) return;
00211     QPushButton *b = const_cast<QPushButton*>(button(index));
00212     b->setText(w->windowTitle());
00213     d->topButton->setText(w->windowTitle());
00214     d->topButton->setIcon(w->windowIcon());
00215 }
00216 
00222 const QPushButton * QwwNavigationBar::button(int index) const {
00223     Q_D(const QwwNavigationBar);
00224     if (index>=d->buttons.count() || index<0) return 0;
00225     return d->buttons.at(index);
00226 }
00227 
00233 QIcon QwwNavigationBar::widgetIcon(int index) const {
00234     const QPushButton *b = button(index);
00235     if (!b) return QIcon();
00236     return b->icon();
00237 }
00238 
00244 void QwwNavigationBar::setWidgetIcon(int index, const QIcon &icon) {
00245     Q_D(QwwNavigationBar);
00246     QPushButton *b = const_cast<QPushButton*>(button(index));
00247     if (!b)
00248         return;
00249     b->setIcon(icon);
00250     QWidget *w = widget(index);
00251     if(!w) return;
00252     d->setIcon(w, icon);
00253     if (index == currentIndex()) {
00254         d->topButton->setIcon(icon);
00255     }
00256 }
00257 
00263 QString QwwNavigationBar::widgetLabel(int index) const {
00264     QWidget *w = widget(index);
00265     if(!w) return QString();
00266     return w->windowTitle();
00267 }
00268 
00274 void QwwNavigationBar::setWidgetLabel(int index, const QString &label) {
00275     Q_D(QwwNavigationBar);
00276     QPushButton *b = const_cast<QPushButton*>(button(index));
00277     if (!b)
00278         return;
00279     b->setText(label);
00280     QWidget *w = widget(index);
00281     //if(!w) return;
00282     w->setProperty("windowTitle", QVariant(label));
00283     if (index == currentIndex()) {
00284         d->topButton->setText(label);
00285         emit widgetLabelChanged(label);
00286     }
00287 }
00288 
00293 QIcon QwwNavigationBar::currentWidgetIcon() const {
00294     const QPushButton *b = button(currentIndex());
00295     if (!b) return QIcon();
00296     return b->icon();
00297 }
00298 
00303 void QwwNavigationBar::setCurrentWidgetLabel(const QString &label) {
00304     Q_D(QwwNavigationBar);
00305      if(label==currentWidgetLabel()) return;
00306     d->stack->currentWidget()->setWindowTitle(label);
00307     d->topButton->setText(label);
00308     QPushButton *b = const_cast<QPushButton*>(button(currentIndex()));
00309     b->setText(label);
00310     emit widgetLabelChanged(label);
00311 }
00312 
00317 void QwwNavigationBar::setCurrentWidgetIcon(const QIcon &icon) {
00318     Q_D(QwwNavigationBar);
00319 //    if(icon==currentWidgetIcon()) return;
00320     d->stack->currentWidget()->setWindowIcon(icon);
00321     d->topButton->setIcon(icon);
00322     QPushButton *b = const_cast<QPushButton*>(button(currentIndex()));
00323     b->setIcon(icon);
00324     emit widgetIconChanged(icon);
00325 }
00326 
00331 QString QwwNavigationBar::currentWidgetLabel() const {
00332     return widgetLabel(currentIndex());
00333 }
00334 
00339 int QwwNavigationBar::widgetCount() const {
00340     Q_D(const QwwNavigationBar);
00341     return d->stack->count();
00342 }
00343 
00349 QWidget * QwwNavigationBar::widget(int index) const {
00350     Q_D(const QwwNavigationBar);
00351     return d->stack->widget(index);
00352 }
00353 
00354 void QwwNavigationBarPrivate::setTitle(QWidget * w, const QString & s) {
00355     w->setWindowTitle(s);
00356 }
00357 
00358 void QwwNavigationBarPrivate::setIcon(QWidget * w, const QIcon & ic) {
00359     w->setWindowIcon(ic);
00360 }
00361 
00362 void QwwNavigationBar::actionEvent(QActionEvent * ev)
00363 {
00364     Q_D(QwwNavigationBar);
00365     switch(ev->type()){
00366     case QEvent::ActionAdded: {
00367 
00368 
00369         QHBoxLayout *hl = static_cast<QHBoxLayout*>(d->bottomFrame->layout());
00370         if(ev->before()==0){
00371             QToolButton *tb = new QToolButton;
00372             tb->setDefaultAction(ev->action());
00373             tb->setAutoRaise(true);
00374             hl->addWidget(tb);
00375         } else
00376         for(int i=0;i<hl->count();i++){
00377             QLayoutItem *lit = hl->itemAt(i);
00378             if(!lit) break;
00379             QWidget *w = lit->widget();
00380             if(!w) continue;
00381             QToolButton *t = dynamic_cast<QToolButton*>(w);
00382             if(!t) continue;
00383             if(ev->before()==t->defaultAction()){
00384                 QToolButton *tb = new QToolButton;
00385                 tb->setDefaultAction(ev->action());
00386                 tb->setAutoRaise(true);
00387                 hl->insertWidget(i, tb);
00388                 return;
00389             }
00390         }
00391     }
00392     break;
00393     case QEvent::ActionRemoved: {
00394         QHBoxLayout *hl = static_cast<QHBoxLayout*>(d->bottomFrame->layout());
00395         for(int i=0;i<hl->count();i++){
00396             QLayoutItem *lit = hl->itemAt(i);
00397             if(!lit) break;
00398             QWidget *w = lit->widget();
00399             if(!w) continue;
00400             QToolButton *t = dynamic_cast<QToolButton*>(w);
00401             if(!t) continue;
00402             if(t->defaultAction()==ev->action()){
00403                 delete t;
00404                 return;
00405             }
00406         }
00407     }
00408     break;
00409     default:break;
00410     }
00411 
00412 }
00413 

Generated on Sat Apr 21 21:54:36 2007 for wwWidgets by  doxygen 1.5.1