ktabctl.cpp
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
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 
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);   
00109     return false;     
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());    
00129     tabs->resize(min);         
00130 
00131 
00132     QSize max(QCOORD_MAX,QCOORD_MAX);
00133     
00134 
00135     for (i = 0; i < pages.size(); i++) {
00136 
00137         
00138 
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     
00152     
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 
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     
00170     setMinimumSize(min.width()+4, min.height()+tabs->height()+4);
00171 
00172     
00173 
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     
00196     QSize hint(tabs->sizeHint());
00197 
00198     
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             
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         
00218         if (pageHint.width() > hint.width())
00219             hint.setWidth(pageHint.width());
00220 
00221         
00222         hint.setHeight(hint.height() + pageHint.height());
00223 
00224         
00225 
00226 
00227 
00228         return (hint + QSize(4,4));
00229     }
00230 
00231     
00232 
00233 
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);      
00256     p.setPen(colorGroup().midlight());
00257     p.drawLine(x0, y0, x1 - 1, y0);      
00258     p.setPen(colorGroup().light());
00259     p.drawLine(x0, y0 + 1, x0, y1);      
00260     p.setPen(black);
00261     p.drawLine(x1, y1, x0, y1);          
00262     p.drawLine(x1, y1 - 1, x1, y0);
00263     p.setPen(colorGroup().dark());
00264     p.drawLine(x0 + 1, y1 - 1, x1 - 1, y1 - 1);  
00265     p.drawLine(x1 - 1, y1 - 2, x1 - 1, y0 + 1);
00266     p.end();
00267 }
00268 
00269 
00270 
00271 
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 
00288 
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 
00313 
00314 
00315 
00316 
00317 
00318 
00319 void KTabCtl::addTab(QWidget *w, const QString& name)
00320 {
00321     QTab *t = new QTab();
00322     t->setText( name );
00323     t->setEnabled( true );
00324     int id = tabs->addTab(t);   
00325     if (id == (int)pages.size()) {
00326     pages.resize(id + 1);
00327         pages[id] = w;          
00328     }
00329     
00330     setSizes();
00331 }
00332 
00333 void KTabCtl::virtual_hook( int, void* )
00334 {  }
00335 
00336 #include "ktabctl.moc"
 
This file is part of the documentation for kdeui Library Version 3.2.0.