kdeui Library API Documentation

ktabctl.cpp

00001 /* This file is part of the KDE libraries
00002     Copyright (C) 1997 Alexander Sanda (alex@darkstar.ping.at)
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License as published by the Free Software Foundation; either
00007     version 2 of the License, or (at your option) any later version.
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., 59 Temple Place - Suite 330,
00017     Boston, MA 02111-1307, USA.
00018 */
00019 
00027 #include <qtabbar.h>
00028 #include <qpushbutton.h>
00029 #include <qpainter.h>
00030 #include <qpixmap.h>
00031 
00032 #include "ktabctl.h"
00033 
00034 KTabCtl::KTabCtl(QWidget *parent, const char *name)
00035     : QWidget(parent, name)
00036 {
00037     tabs = new QTabBar(this, "_tabbar");
00038     connect(tabs, SIGNAL(selected(int)), this, SLOT(showTab(int)));
00039     tabs->move(2, 1);
00040 
00041     blBorder = TRUE;
00042 
00043 }
00044 
00045 KTabCtl::~KTabCtl()
00046 {
00047         delete tabs;
00048 }
00049 
00050 void KTabCtl::resizeEvent(QResizeEvent *)
00051 {
00052     int i;
00053     QRect r = getChildRect();
00054 
00055     if (tabs) {
00056         for (i=0; i<(int)pages.size(); i++) {
00057             pages[i]->setGeometry(r);
00058         }
00059         if( ( tabs->shape() == QTabBar::RoundedBelow ) ||
00060             ( tabs->shape() == QTabBar::TriangularBelow ) ) {
00061             tabs->move( 0, height()-tabs->height()-4 );
00062         }
00063     }
00064 }
00065 
00066 void KTabCtl::setFont(const QFont & font)
00067 {
00068     QFont f(font);
00069     f.setWeight(QFont::Light);
00070     QWidget::setFont(f);
00071 
00072     setSizes();
00073 }
00074 
00075 void KTabCtl::setTabFont(const QFont & font)
00076 {
00077     QFont f(font);
00078 //    f.setWeight(QFont::Light);
00079     tabs->setFont(f);
00080 
00081     setSizes();
00082 }
00083 
00084 void KTabCtl::show()
00085 {
00086     unsigned int i;
00087 
00088     if(isVisible())
00089         return;
00090 
00091     setSizes();
00092 
00093     for(i = 0; i < pages.size(); i++)
00094         pages[i]->hide();
00095 
00096     QResizeEvent r(size(), size());
00097     resizeEvent(&r);
00098 
00099     QWidget::show();
00100 }
00101 
00102 bool KTabCtl::isTabEnabled(const QString& name)
00103 {
00104     unsigned int i;
00105 
00106     for(i = 0; i < pages.size(); i++)
00107         if (QString::fromLatin1(pages[i]->name()) == name)
00108             return tabs->isTabEnabled(i);   /* return the enabled status */
00109     return false;     /* tab does not exist */
00110 }
00111 
00112 void KTabCtl::setTabEnabled(const QString& name, bool state)
00113 {
00114     unsigned i;
00115 
00116     if (name.isEmpty())
00117         return;
00118 
00119     for (i = 0; i < pages.size(); i++)
00120         if (QString::fromLatin1(pages[i]->name()) == name)
00121             tabs->setTabEnabled(i, state);
00122 }
00123 
00124 void KTabCtl::setSizes()
00125 {
00126     unsigned i;
00127 
00128     QSize min(tabs->sizeHint());    /* the minimum required size for the tabbar */
00129     tabs->resize(min);         /* make sure that the tabbar does not require more space than actually needed. */
00130 
00131 
00132     QSize max(QCOORD_MAX,QCOORD_MAX);
00133     //int th = min.height();          /* the height of the tabbar itself (without pages and stuff) */
00134 
00135     for (i = 0; i < pages.size(); i++) {
00136 
00137         /*
00138          * check the actual minimum and maximum sizes
00139          */
00140 
00141         if (pages[i]->maximumSize().height() < max.height())
00142             max.setHeight(pages[i]->maximumSize().height());
00143         if (pages[i]->maximumSize().width() < max.width())
00144             max.setWidth( pages[i]->maximumSize().width());
00145         if ( pages[i]->minimumSize().height() > min.height())
00146             min.setHeight( pages[i]->minimumSize().height());
00147         if ( pages[i]->minimumSize().width() > min.width())
00148             min.setWidth( pages[i]->minimumSize().width());
00149     }
00150 
00151     // BL: min and max are sizes of children, not tabcontrol
00152     // min.setHeight(min.height() + th);
00153 
00154     if (max.width() < min.width())
00155         max.setWidth(min.width());
00156     if (max.height() < min.height())
00157         max.setHeight(min.height());
00158 
00159     /*
00160      * now, apply the calculated size values to all of the pages
00161      */
00162 
00163     for( i=0; i<(uint)pages.size(); i++ ) {
00164         pages[i]->setMinimumSize(min);
00165         pages[i]->setMaximumSize(max);
00166     }
00167 
00168 
00169     // BL: set minimum size of tabcontrol
00170     setMinimumSize(min.width()+4, min.height()+tabs->height()+4);
00171 
00172     /*
00173      * generate a resizeEvent, if we're visible
00174      */
00175 
00176     if(isVisible()) {
00177         QResizeEvent r(size(), size());
00178         resizeEvent(&r);
00179     }
00180 }
00181 
00182 void KTabCtl::setBorder( bool state )
00183 {
00184     blBorder = state;
00185 }
00186 
00187 void KTabCtl::setShape( QTabBar::Shape shape )
00188 {
00189     tabs->setShape( shape );
00190 }
00191 
00192 QSize
00193 KTabCtl::sizeHint() const
00194 {
00195         /* desired size of the tabbar */
00196         QSize hint(tabs->sizeHint());
00197 
00198         /* overall desired size of all pages */
00199         QSize pageHint;
00200         for (unsigned int i = 0; i < pages.size(); i++)
00201         {
00202                 QSize sizeI(pages[i]->sizeHint());
00203 
00204                 if (sizeI.isValid())
00205                 {
00206                         /* only pages with valid size are used */
00207                         if (sizeI.width() > pageHint.width())
00208                                 pageHint.setWidth(sizeI.width());
00209 
00210                         if (sizeI.height() > pageHint.height())
00211                                 pageHint.setHeight(sizeI.height());
00212                 }
00213         }
00214 
00215         if (pageHint.isValid())
00216         {
00217                 /* use maximum of width of tabbar and pages */
00218                 if (pageHint.width() > hint.width())
00219                         hint.setWidth(pageHint.width());
00220 
00221                 /* heights must just be added */
00222                 hint.setHeight(hint.height() + pageHint.height());
00223 
00224                 /* 1999-09-18: Espen Sand
00225                    I cannot get the size to be correct unless the total
00226                    border size is included: ie 2*2 pixels.
00227                 */
00228                 return (hint + QSize(4,4));
00229         }
00230 
00231         /*
00232          * If not at least a one page has a valid sizeHint we have to return
00233          * an invalid size as well.
00234          */
00235         return (pageHint);
00236 }
00237 
00238 void KTabCtl::paintEvent(QPaintEvent *)
00239 {
00240     if (!tabs)
00241         return;
00242 
00243     if( !blBorder )
00244         return;
00245 
00246     QPainter p;
00247     p.begin(this);
00248 
00249     int y0 = getChildRect().top() - 1;
00250     int y1 = getChildRect().bottom() + 2;
00251     int x1 = getChildRect().right() + 2;
00252     int x0 = getChildRect().left() - 1;
00253 
00254     p.setPen(colorGroup().light());
00255     p.drawLine(x0, y0 - 1, x1 - 1, y0 - 1);      /* 1st top line */
00256     p.setPen(colorGroup().midlight());
00257     p.drawLine(x0, y0, x1 - 1, y0);      /* 2nd top line */
00258     p.setPen(colorGroup().light());
00259     p.drawLine(x0, y0 + 1, x0, y1);      /* left line */
00260     p.setPen(black);
00261     p.drawLine(x1, y1, x0, y1);          /* bottom line */
00262     p.drawLine(x1, y1 - 1, x1, y0);
00263     p.setPen(colorGroup().dark());
00264     p.drawLine(x0 + 1, y1 - 1, x1 - 1, y1 - 1);  /* bottom */
00265     p.drawLine(x1 - 1, y1 - 2, x1 - 1, y0 + 1);
00266     p.end();
00267 }
00268 
00269 /*
00270  * return the client rect. This is the maximum size for any child
00271  * widget (page).
00272  */
00273 
00274 QRect KTabCtl::getChildRect() const
00275 {
00276     if( ( tabs->shape() == QTabBar::RoundedBelow ) ||
00277         ( tabs->shape() == QTabBar::TriangularBelow ) ) {
00278         return QRect(2, 1, width() - 4,
00279                      height() - tabs->height() - 4);
00280     } else {
00281         return QRect(2, tabs->height() + 1, width() - 4,
00282                      height() - tabs->height() - 4);
00283     }
00284 }
00285 
00286 /*
00287  * show a single page, depending on the selected tab
00288  * emit tabSelected(new_pagenumber) BEFORE the page is shown
00289  */
00290 
00291 void KTabCtl::showTab(int i)
00292 {
00293     unsigned int j;
00294     for (j = 0; j < pages.size(); j++) {
00295       if (j != (unsigned)i) {
00296         pages[j]->hide();
00297       }
00298     }
00299 
00300     if((unsigned)i < pages.size()) {
00301         emit(tabSelected(i));
00302                 if( pages.size() >= 2 ) {
00303                         pages[i]->raise();
00304                 }
00305                 tabs->setCurrentTab(i);
00306         pages[i]->setGeometry(getChildRect());
00307         pages[i]->show();
00308     }
00309 }
00310 
00311 /*
00312  * add a tab to the control. This tab will manage the given Widget w.
00313  * in most cases, w will be a QWidget and will only act as parent for the
00314  * actual widgets on this page
00315  * NOTE: w is not required to be of class QWidget, but expect strange results with
00316  * other types of widgets
00317  */
00318 
00319 void KTabCtl::addTab(QWidget *w, const QString& name)
00320 {
00321     QTab *t = new QTab();
00322 #if QT_VERSION < 300
00323     t->label = name;
00324     t->enabled = TRUE;
00325 #else
00326     t->setText( name );
00327     t->setEnabled( true );
00328 #endif
00329     int id = tabs->addTab(t);   /* add the tab itself to the tabbar */
00330     if (id == (int)pages.size()) {
00331         pages.resize(id + 1);
00332         pages[id] = w;          /* remember the widget to manage by this tab */
00333     }
00334     // BL: compute sizes
00335     setSizes();
00336 }
00337 
00338 void KTabCtl::virtual_hook( int, void* )
00339 { /*BASE::virtual_hook( id, data );*/ }
00340 
00341 #include "ktabctl.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 12:57:45 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001