cjanuswidget.cpp

00001 /*
00002  *  This file is part of the KDE libraries
00003  *  Copyright (c) 2001 Michael Goffioul <kdeprint@swing.be>
00004  *
00005  *  This library is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Library General Public
00007  *  License version 2 as published by the Free Software Foundation.
00008  *
00009  *  This library is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  *  Library General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU Library General Public License
00015  *  along with this library; see the file COPYING.LIB.  If not, write to
00016  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  *  Boston, MA 02110-1301, USA.
00018  **/
00019 
00020 #include "cjanuswidget.h"
00021 
00022 #include <qwidgetstack.h>
00023 #include <qlabel.h>
00024 #include <qpainter.h>
00025 #include <klistbox.h>
00026 #include <qlayout.h>
00027 #include <kseparator.h>
00028 
00029 class CJanusWidget::CPage
00030 {
00031 public:
00032     QWidget     *m_widget;
00033     QString     m_text;
00034     QString     m_header;
00035     QPixmap     m_pixmap;
00036     CListBoxItem    *m_item;
00037 };
00038 
00039 //***********************************************************************************
00040 
00041 class CJanusWidget::CListBoxItem : public QListBoxItem
00042 {
00043 public:
00044     CListBoxItem(QListBox *lb, QListBoxItem *after, const QPixmap& pix, const QString& text);
00045     int height(const QListBox*) const;
00046     int width(const QListBox*) const;
00047 
00048 protected:
00049     void paint(QPainter*);
00050 
00051 private:
00052     QPixmap m_pix;
00053 };
00054 
00055 CJanusWidget::CListBoxItem::CListBoxItem(QListBox *lb, QListBoxItem *after, const QPixmap& pix, const QString& text)
00056 : QListBoxItem(lb, after), m_pix(pix)
00057 {
00058     setText(text);
00059 }
00060 
00061 int CJanusWidget::CListBoxItem::height(const QListBox *lb) const
00062 {
00063     return (m_pix.height() + lb->fontMetrics().lineSpacing() + 12);
00064 }
00065 
00066 int CJanusWidget::CListBoxItem::width(const QListBox *lb) const
00067 {
00068     int w = QMAX(lb->fontMetrics().width(text()),m_pix.width());
00069     return (w + 10);
00070 }
00071 
00072 void CJanusWidget::CListBoxItem::paint(QPainter *p)
00073 {
00074     int w1 = (listBox()->contentsWidth()-m_pix.width())/2;
00075 
00076     p->drawPixmap(w1,5,m_pix);
00077     p->drawText(0,7+m_pix.height(),listBox()->contentsWidth(),p->fontMetrics().lineSpacing(),Qt::AlignHCenter,text());
00078 }
00079 
00080 //***********************************************************************************
00081 
00082 class CJanusWidget::CListBox : public KListBox
00083 {
00084 public:
00085     CListBox(QWidget *parent = 0, const char *name = 0);
00086     ~CListBox();
00087 
00088     void computeWidth();
00089 
00090 protected:
00091     virtual bool eventFilter(QObject*, QEvent*);
00092 };
00093 
00094 CJanusWidget::CListBox::CListBox(QWidget *parent, const char *name)
00095 : KListBox(parent,name)
00096 {
00097     verticalScrollBar()->installEventFilter(this);
00098 }
00099 
00100 CJanusWidget::CListBox::~CListBox()
00101 {
00102 }
00103 
00104 bool CJanusWidget::CListBox::eventFilter(QObject *o, QEvent *e)
00105 {
00106     if (e->type() == QEvent::Show || e->type() == QEvent::Hide)
00107         computeWidth();
00108     return KListBox::eventFilter(o,e);
00109 }
00110 
00111 void CJanusWidget::CListBox::computeWidth()
00112 {
00113     QListBoxItem    *item = firstItem();
00114     int w(40);
00115     while (item)
00116     {
00117         w = QMAX(w,item->width(this));
00118         item = item->next();
00119     }
00120     if (verticalScrollBar()->isVisible())
00121         w += verticalScrollBar()->sizeHint().width();
00122     w += (frameWidth()*2);
00123     setFixedWidth(w);
00124 }
00125 
00126 //***********************************************************************************
00127 
00128 CJanusWidget::CJanusWidget(QWidget *parent, const char *name)
00129 : QWidget(parent,name)
00130 {
00131     m_pages.setAutoDelete(true);
00132 
00133     m_stack = new QWidgetStack(this);
00134     m_header = new QLabel(this);
00135     QFont   f(m_header->font());
00136     f.setBold(true);
00137     m_header->setFont(f);
00138 
00139     KSeparator* sep = new KSeparator( KSeparator::HLine, this);
00140     sep->setFixedHeight(5);
00141 
00142     m_iconlist = new CListBox(this);
00143     f = m_iconlist->font();
00144     f.setBold(true);
00145     m_iconlist->setFont(f);
00146     connect(m_iconlist,SIGNAL(selectionChanged(QListBoxItem*)),SLOT(slotSelected(QListBoxItem*)));
00147 
00148     m_empty = new QWidget(this, "Empty");
00149     m_stack->addWidget(m_empty,0);
00150 
00151     QHBoxLayout *main_ = new QHBoxLayout(this, 0, 10);
00152     QVBoxLayout *sub_ = new QVBoxLayout(0, 0, 5);
00153     main_->addWidget(m_iconlist,0);
00154     main_->addLayout(sub_,1);
00155     sub_->addWidget(m_header,0);
00156     sub_->addWidget(sep,0);
00157     sub_->addWidget(m_stack,1);
00158 }
00159 
00160 CJanusWidget::~CJanusWidget()
00161 {
00162 }
00163 
00164 void CJanusWidget::addPage(QWidget *w, const QString& text, const QString& header, const QPixmap& pix)
00165 {
00166     CPage   *page = new CPage();
00167     m_pages.append(page);
00168     page->m_widget = w;
00169     page->m_text = text;
00170     page->m_header = header;
00171     page->m_pixmap = pix;
00172     page->m_item = new CListBoxItem(m_iconlist,findPrevItem(page),pix,text);
00173     m_iconlist->computeWidth();
00174     m_stack->addWidget(w,m_pages.count());
00175 
00176     if (m_iconlist->count() == 1)
00177         m_iconlist->setSelected(page->m_item,true);
00178 }
00179 
00180 void CJanusWidget::enablePage(QWidget *w)
00181 {
00182     CPage   *page = findPage(w);
00183     if (page && !page->m_item)
00184     {
00185         page->m_item = new CListBoxItem(m_iconlist,findPrevItem(page),page->m_pixmap,page->m_text);
00186         m_iconlist->computeWidth();
00187         if (m_iconlist->count() == 1)
00188             m_iconlist->setSelected(page->m_item,true);
00189     }
00190 }
00191 
00192 void CJanusWidget::disablePage(QWidget *w)
00193 {
00194     CPage   *page = findPage(w);
00195     if (page && page->m_item)
00196     {
00197         bool    needReselect(m_iconlist->isSelected(page->m_item));
00198         delete page->m_item;
00199         page->m_item = 0;
00200         m_iconlist->computeWidth();
00201         if (needReselect)
00202             if (m_iconlist->count() > 0)
00203                 m_iconlist->setSelected(m_iconlist->firstItem(),true);
00204             else
00205                 slotSelected(0);
00206     }
00207 }
00208 
00209 void CJanusWidget::slotSelected(QListBoxItem *item)
00210 {
00211     CPage   *page = findPage(item);
00212     if (page)
00213     {
00214         m_stack->raiseWidget(page->m_widget);
00215         m_header->setText(page->m_header);
00216     }
00217     else
00218     {
00219         m_header->setText("");
00220         m_stack->raiseWidget(m_empty);
00221     }
00222 }
00223 
00224 CJanusWidget::CPage* CJanusWidget::findPage(QWidget *w)
00225 {
00226     QPtrListIterator<CPage> it(m_pages);
00227     for (;it.current();++it)
00228         if (it.current()->m_widget == w)
00229             return it.current();
00230     return 0;
00231 }
00232 
00233 CJanusWidget::CPage* CJanusWidget::findPage(QListBoxItem *i)
00234 {
00235     QPtrListIterator<CPage> it(m_pages);
00236     for (;it.current();++it)
00237         if (it.current()->m_item == i)
00238             return it.current();
00239     return 0;
00240 }
00241 
00242 QListBoxItem* CJanusWidget::findPrevItem(CPage *p)
00243 {
00244     if (m_pages.findRef(p) == -1)
00245         m_pages.last();
00246     else
00247         m_pages.prev();
00248     for (;m_pages.current();m_pages.prev())
00249         if (m_pages.current()->m_item)
00250             return m_pages.current()->m_item;
00251     return 0;
00252 }
00253 
00254 void CJanusWidget::clearPages()
00255 {
00256     QPtrListIterator<CPage> it(m_pages);
00257     for (;it.current(); ++it)
00258     {
00259         delete it.current()->m_widget;
00260         delete it.current()->m_item;
00261     }
00262     m_pages.clear();
00263 }
00264 
00265 #include "cjanuswidget.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys