kdeui Library API Documentation

ktabwidget.cpp

00001 /* This file is part of the KDE libraries
00002     Copyright (C) 2003 Stephan Binner <binner@kde.org>
00003     Copyright (C) 2003 Zack Rusin <zack@kde.org>
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 as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public License
00016     along with this library; see the file COPYING.LIB.  If not, write to
00017     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018     Boston, MA 02111-1307, USA.
00019 */
00020 
00021 #include <qapplication.h>
00022 #include <qstyle.h>
00023 #include <qstylesheet.h>
00024 
00025 #include <kconfig.h>
00026 #include <kiconloader.h>
00027 #include <kstringhandler.h>
00028 
00029 #include "ktabwidget.h"
00030 #include "ktabbar.h"
00031 
00032 class KTabWidgetPrivate {
00033 public:
00034     bool m_automaticResizeTabs;
00035     int m_maxLength;
00036     int m_minLength;
00037     unsigned int m_CurrentMaxLength;
00038 
00039     //holds the full names of the tab, otherwise all we
00040     //know about is the shortened name
00041     QStringList m_tabNames;
00042 
00043     KTabWidgetPrivate() {
00044         m_automaticResizeTabs = false;
00045         KConfigGroupSaver groupsaver(KGlobal::config(), "General");
00046         m_maxLength = KGlobal::config()->readNumEntry("MaximumTabLength", 30);
00047         m_minLength = KGlobal::config()->readNumEntry("MinimumTabLength", 3);
00048         m_CurrentMaxLength = m_minLength;
00049     }
00050 };
00051 
00052 KTabWidget::KTabWidget( QWidget *parent, const char *name, WFlags f )
00053     : QTabWidget( parent, name, f )
00054 {
00055     d = new KTabWidgetPrivate;
00056     setTabBar( new KTabBar(this, "tabbar") );
00057     setAcceptDrops( true );
00058 
00059     connect(tabBar(), SIGNAL(contextMenu( int, const QPoint & )), SLOT(contextMenu( int, const QPoint & )));
00060     connect(tabBar(), SIGNAL(mouseDoubleClick( int )), SLOT(mouseDoubleClick( int )));
00061     connect(tabBar(), SIGNAL(mouseMiddleClick( int )), SLOT(mouseMiddleClick( int )));
00062     connect(tabBar(), SIGNAL(initiateDrag( int )), SLOT(initiateDrag( int )));
00063     connect(tabBar(), SIGNAL(testCanDecode(const QDragMoveEvent *, bool & )), SIGNAL(testCanDecode(const QDragMoveEvent *, bool & )));
00064     connect(tabBar(), SIGNAL(receivedDropEvent( int, QDropEvent * )), SLOT(receivedDropEvent( int, QDropEvent * )));
00065     connect(tabBar(), SIGNAL(moveTab( int, int )), SLOT(moveTab( int, int )));
00066     connect(tabBar(), SIGNAL(closeRequest( int )), SLOT(closeRequest( int )));
00067 #ifndef QT_NO_WHEELEVENT
00068     connect(tabBar(), SIGNAL(wheelDelta( int )), SLOT(wheelDelta( int )));
00069 #endif
00070 }
00071 
00072 KTabWidget::~KTabWidget()
00073 {
00074     delete d;
00075 }
00076 
00077 void KTabWidget::insertTab( QWidget *child, const QString &label, int index )
00078 {
00079     QTabWidget::insertTab( child, label, index );
00080 }
00081 
00082 void KTabWidget::insertTab( QWidget *child, const QIconSet& iconset, const QString &label, int index )
00083 {
00084     QTabWidget::insertTab( child, iconset, label, index );
00085 }
00086 
00087 void KTabWidget::insertTab( QWidget *child, QTab *tab, int index )
00088 {
00089     QTabWidget::insertTab( child, tab, index);
00090     if ( d->m_automaticResizeTabs ) {
00091         if ( index < 0 || index >= count() ) {
00092             d->m_tabNames.append( tab->text() );
00093             resizeTabs( d->m_tabNames.count()-1 );
00094         }
00095         else {
00096             d->m_tabNames.insert( d->m_tabNames.at( index ), tab->text() );
00097             resizeTabs( index );
00098         }
00099     }
00100 }
00101 
00102 void KTabWidget::setTabBarHidden( bool hide )
00103 {
00104     QWidget *rightcorner = this->cornerWidget( TopRight );
00105     QWidget *leftcorner = this->cornerWidget( TopLeft );
00106 
00107     if ( hide ) {
00108         if ( leftcorner ) leftcorner->hide();
00109         if ( rightcorner ) rightcorner->hide();
00110         tabBar()->hide();
00111     } else {
00112         tabBar()->show();
00113         if ( leftcorner ) leftcorner->show();
00114         if ( rightcorner ) rightcorner->show();
00115     }
00116 }
00117 
00118 bool KTabWidget::isTabBarHidden() const
00119 {
00120     return !( tabBar()->isVisible() );
00121 }
00122 
00123 void KTabWidget::setTabColor( QWidget *w, const QColor& color )
00124 {
00125     QTab *t = tabBar()->tabAt( indexOf( w ) );
00126     if (t) {
00127         static_cast<KTabBar*>(tabBar())->setTabColor( t->identifier(), color );
00128     }
00129 }
00130 
00131 QColor KTabWidget::tabColor( QWidget *w ) const
00132 {
00133     QTab *t = tabBar()->tabAt( indexOf( w ) );
00134     if (t) {
00135         return static_cast<KTabBar*>(tabBar())->tabColor( t->identifier() );
00136     } else {
00137         return QColor();
00138     }
00139 }
00140 
00141 void KTabWidget::setTabReorderingEnabled( bool on)
00142 {
00143     static_cast<KTabBar*>(tabBar())->setTabReorderingEnabled( on );
00144 }
00145 
00146 bool KTabWidget::isTabReorderingEnabled() const
00147 {
00148     return static_cast<KTabBar*>(tabBar())->isTabReorderingEnabled();
00149 }
00150 
00151 void KTabWidget::setTabCloseActivatePrevious( bool previous)
00152 {
00153     static_cast<KTabBar*>(tabBar())->setTabCloseActivatePrevious( previous );
00154 }
00155 
00156 bool KTabWidget::tabCloseActivatePrevious() const
00157 {
00158     return static_cast<KTabBar*>(tabBar())->tabCloseActivatePrevious();
00159 }
00160 
00161 unsigned int KTabWidget::tabBarWidthForMaxChars( uint maxLength )
00162 {
00163     int hframe, overlap;
00164     hframe  = tabBar()->style().pixelMetric( QStyle::PM_TabBarTabHSpace, tabBar() );
00165     overlap = tabBar()->style().pixelMetric( QStyle::PM_TabBarTabOverlap, tabBar() );
00166 
00167     QFontMetrics fm = tabBar()->fontMetrics();
00168     int x = 0;
00169     for( int i=0; i < count(); ++i ) {
00170         QString newTitle = d->m_tabNames[ i ];
00171         newTitle = KStringHandler::rsqueeze( newTitle, maxLength ).leftJustify( d->m_minLength, ' ' );
00172 
00173         QTab* tab = tabBar()->tabAt( i );
00174         int lw = fm.width( newTitle );
00175         int iw = 0;
00176         if ( tab->iconSet() )
00177           iw = tab->iconSet()->pixmap( QIconSet::Small, QIconSet::Normal ).width() + 4;
00178         x += ( tabBar()->style().sizeFromContents( QStyle::CT_TabBarTab, this,
00179                    QSize( QMAX( lw + hframe + iw, QApplication::globalStrut().width() ), 0 ),
00180                    QStyleOption( tab ) ) ).width();
00181     }
00182     return x;
00183 }
00184 
00185 void KTabWidget::changeTab( QWidget *w, const QString &label )
00186 {
00187     QTabWidget::changeTab( w, label );
00188     if ( d->m_automaticResizeTabs ) {
00189         int index = indexOf( w );
00190         if ( index != -1 ) {
00191             d->m_tabNames[ index ] = label;
00192             resizeTabs( index );
00193         }
00194     }
00195 }
00196 
00197 void KTabWidget::changeTab( QWidget *w, const QIconSet &iconset, const QString &label )
00198 {
00199     QTabWidget::changeTab( w, iconset, label );
00200     if ( d->m_automaticResizeTabs ) {
00201         int index = indexOf( w );
00202         if ( index != -1 ) {
00203             d->m_tabNames[ index ] = label;
00204             resizeTabs( index );
00205         }
00206     }
00207 }
00208 
00209 QString KTabWidget::label( int index ) const
00210 {
00211     if ( d->m_automaticResizeTabs ) {
00212         if ( index >= 0 && index < count() )
00213             return d->m_tabNames[ index ];
00214         else
00215             return QString::null;
00216     }
00217     else
00218         return QTabWidget::label( index );
00219 }
00220 
00221 QString KTabWidget::tabLabel( QWidget * w ) const
00222 {
00223     if ( d->m_automaticResizeTabs ) {
00224         int index = indexOf( w );
00225         if ( index == -1 )
00226             return QString::null;
00227         else
00228             return d->m_tabNames[ index ];
00229     }
00230     else
00231         return QTabWidget::tabLabel( w );
00232 }
00233 
00234 void KTabWidget::setTabLabel( QWidget *w, const QString &l )
00235 {
00236     QTabWidget::setTabLabel( w, l );
00237     if ( d->m_automaticResizeTabs ) {
00238         int index = indexOf( w );
00239         if ( index != -1 ) {
00240             d->m_tabNames[ index ] = l;
00241             resizeTabs( index );
00242         }
00243   }
00244 }
00245 
00246 void KTabWidget::resizeTabs( int changeTabIndex )
00247 {
00248     uint newMaxLength;
00249     if ( d->m_automaticResizeTabs ) {
00250         // Calculate new max length
00251         newMaxLength=d->m_maxLength;
00252         uint lcw=0, rcw=0;
00253 
00254         int tabBarHeight = tabBar()->sizeHint().height();
00255         if ( cornerWidget( TopLeft ) && cornerWidget( TopLeft )->isVisible() )
00256             lcw = QMAX( cornerWidget( TopLeft )->width(), tabBarHeight );
00257         if ( cornerWidget( TopRight ) && cornerWidget( TopRight )->isVisible() )
00258             rcw = QMAX( cornerWidget( TopRight )->width(), tabBarHeight );
00259 
00260         uint maxTabBarWidth = width() - lcw - rcw;
00261 
00262         for ( ; newMaxLength > (uint)d->m_minLength; newMaxLength-- ) {
00263             if ( tabBarWidthForMaxChars( newMaxLength ) < maxTabBarWidth )
00264                 break;
00265         }
00266     }
00267     else
00268         newMaxLength = 4711;
00269 
00270     // Update hinted or all tabs
00271     if ( d->m_CurrentMaxLength != newMaxLength ) {
00272         d->m_CurrentMaxLength = newMaxLength;
00273         for( int i = 0; i < count(); ++i )
00274             updateTab( i );
00275     }
00276     else if ( changeTabIndex != -1 )
00277         updateTab( changeTabIndex );
00278 }
00279 
00280 void KTabWidget::updateTab( int index )
00281 {
00282     QString title = d->m_automaticResizeTabs ? d->m_tabNames[ index ] : QTabWidget::label( index );
00283     removeTabToolTip( page( index ) );
00284     if ( title.length() > d->m_CurrentMaxLength )
00285         setTabToolTip( page( index ), QStyleSheet::escape(title) );
00286 
00287     title = KStringHandler::rsqueeze( title, d->m_CurrentMaxLength ).leftJustify( d->m_minLength, ' ' );
00288     title.replace( '&', "&&" );
00289 
00290     if ( QTabWidget::label( index ) != title )
00291         QTabWidget::setTabLabel( page( index ), title );
00292 }
00293 
00294 void KTabWidget::dragMoveEvent( QDragMoveEvent *e )
00295 {
00296     if ( isEmptyTabbarSpace( e->pos() ) ) {
00297         bool accept = false;
00298         // The receivers of the testCanDecode() signal has to adjust
00299         // 'accept' accordingly.
00300         emit testCanDecode( e, accept);
00301         e->accept( accept );
00302         return;
00303     }
00304     e->accept( false );
00305     QTabWidget::dragMoveEvent( e );
00306 }
00307 
00308 void KTabWidget::dropEvent( QDropEvent *e )
00309 {
00310     if ( isEmptyTabbarSpace( e->pos() ) ) {
00311         emit ( receivedDropEvent( e ) );
00312         return;
00313     }
00314     QTabWidget::dropEvent( e );
00315 }
00316 
00317 #ifndef QT_NO_WHEELEVENT
00318 void KTabWidget::wheelEvent( QWheelEvent *e )
00319 {
00320     if ( e->orientation() == Horizontal )
00321         return;
00322 
00323     if ( isEmptyTabbarSpace( e->pos() ) )
00324         wheelDelta( e->delta() );
00325     else
00326         e->ignore();
00327 }
00328 
00329 void KTabWidget::wheelDelta( int delta )
00330 {
00331     if ( count() < 2 )
00332         return;
00333 
00334     int page = currentPageIndex();
00335     if ( delta < 0 )
00336          page = (page + 1) % count();
00337     else {
00338         page--;
00339         if ( page < 0 )
00340             page = count() - 1;
00341     }
00342     setCurrentPage( page );
00343 }
00344 #endif
00345 
00346 void KTabWidget::mouseDoubleClickEvent( QMouseEvent *e )
00347 {
00348     if( e->button() != LeftButton )
00349         return;
00350 
00351     if ( isEmptyTabbarSpace( e->pos() ) ) {
00352         emit( mouseDoubleClick() );
00353         return;
00354     }
00355     QTabWidget::mouseDoubleClickEvent( e );
00356 }
00357 
00358 void KTabWidget::mousePressEvent( QMouseEvent *e )
00359 {
00360     if ( e->button() == RightButton ) {
00361         if ( isEmptyTabbarSpace( e->pos() ) ) {
00362             emit( contextMenu( mapToGlobal( e->pos() ) ) );
00363             return;
00364         }
00365     } else if ( e->button() == MidButton ) {
00366         if ( isEmptyTabbarSpace( e->pos() ) ) {
00367             emit( mouseMiddleClick() );
00368             return;
00369         }
00370     }
00371     QTabWidget::mousePressEvent( e );
00372 }
00373 
00374 void KTabWidget::receivedDropEvent( int index, QDropEvent *e )
00375 {
00376     emit( receivedDropEvent( page( index ), e ) );
00377 }
00378 
00379 void KTabWidget::initiateDrag( int index )
00380 {
00381     emit( initiateDrag( page( index ) ) );
00382 }
00383 
00384 void KTabWidget::contextMenu( int index, const QPoint &p )
00385 {
00386     emit( contextMenu( page( index ), p ) );
00387 }
00388 
00389 void KTabWidget::mouseDoubleClick( int index )
00390 {
00391     emit( mouseDoubleClick( page( index ) ) );
00392 }
00393 
00394 void KTabWidget::mouseMiddleClick( int index )
00395 {
00396     emit( mouseMiddleClick( page( index ) ) );
00397 }
00398 
00399 void KTabWidget::moveTab( int from, int to )
00400 {
00401     QString tablabel = label( from );
00402     QWidget *w = page( from );
00403     QColor color = tabColor( w );
00404     QIconSet tabiconset = tabIconSet( w );
00405     QString tabtooltip = tabToolTip( w );
00406     bool current = ( w == currentPage() );
00407     bool enabled = isTabEnabled( w );
00408     blockSignals(true);
00409     removePage( w );
00410 
00411     // Work-around kmdi brain damage which calls showPage() in insertTab()
00412     QTab * t = new QTab();
00413     t->setText(tablabel);
00414     QTabWidget::insertTab( w, t, to );
00415     if ( d->m_automaticResizeTabs ) {
00416         if ( to < 0 || to >= count() )
00417             d->m_tabNames.append( QString::null );
00418         else
00419             d->m_tabNames.insert( d->m_tabNames.at( to ), QString::null );
00420     }
00421 
00422     w = page( to );
00423     changeTab( w, tabiconset, tablabel );
00424     setTabToolTip( w, tabtooltip );
00425     setTabColor( w, color );
00426     if ( current )
00427         showPage( w );
00428     setTabEnabled( w, enabled );
00429     blockSignals(false);
00430 
00431     emit ( movedTab( from, to ) );
00432 }
00433 
00434 void KTabWidget::removePage( QWidget * w ) {
00435     if ( d->m_automaticResizeTabs ) {
00436         int index = indexOf( w );
00437         if ( index != -1 )
00438             d->m_tabNames.remove( d->m_tabNames.at( index ) );
00439     }
00440     QTabWidget::removePage( w );
00441     if ( d->m_automaticResizeTabs )
00442         resizeTabs();
00443 }
00444 
00445 
00446 bool KTabWidget::isEmptyTabbarSpace( const QPoint &p ) const
00447 {
00448     QPoint point( p );
00449     QSize size( tabBar()->sizeHint() );
00450     if ( ( tabPosition()==Top && point.y()< size.height() ) || ( tabPosition()==Bottom && point.y()>(height()-size.height() ) ) ) {
00451         QWidget *rightcorner = cornerWidget( TopRight );
00452         if ( rightcorner ) {
00453             if ( point.x()>=width()-rightcorner->width() )
00454                 return false;
00455         }
00456         QWidget *leftcorner = cornerWidget( TopLeft );
00457         if ( leftcorner ) {
00458             if ( point.x()<=leftcorner->width() )
00459                 return false;
00460             point.setX( point.x()-size.height() );
00461         }
00462         if ( tabPosition()==Bottom )
00463             point.setY( point.y()-( height()-size.height() ) );
00464         QTab *tab = tabBar()->selectTab( point);
00465         if( !tab )
00466             return true;
00467     }
00468     return false;
00469 }
00470 
00471 void KTabWidget::setHoverCloseButton( bool button )
00472 {
00473     static_cast<KTabBar*>(tabBar())->setHoverCloseButton( button );
00474 }
00475 
00476 bool KTabWidget::hoverCloseButton() const
00477 {
00478     return static_cast<KTabBar*>(tabBar())->hoverCloseButton();
00479 }
00480 
00481 void KTabWidget::setHoverCloseButtonDelayed( bool delayed )
00482 {
00483     static_cast<KTabBar*>(tabBar())->setHoverCloseButtonDelayed( delayed );
00484 }
00485 
00486 bool KTabWidget::hoverCloseButtonDelayed() const
00487 {
00488     return static_cast<KTabBar*>(tabBar())->hoverCloseButtonDelayed();
00489 }
00490 
00491 void KTabWidget::setAutomaticResizeTabs( bool enabled )
00492 {
00493     if ( d->m_automaticResizeTabs==enabled )
00494         return;
00495 
00496     d->m_automaticResizeTabs = enabled;
00497     if ( enabled ) {
00498         d->m_tabNames.clear();
00499         for( int i = 0; i < count(); ++i )
00500             d->m_tabNames.append( tabBar()->tabAt( i )->text() );
00501     }
00502     else
00503         for( int i = 0; i < count(); ++i )
00504             tabBar()->tabAt( i )->setText( d->m_tabNames[ i ] );
00505     resizeTabs();
00506 }
00507 
00508 bool KTabWidget::automaticResizeTabs() const
00509 {
00510     return d->m_automaticResizeTabs;
00511 }
00512 
00513 void KTabWidget::closeRequest( int index )
00514 {
00515     emit( closeRequest( page( index ) ) );
00516 }
00517 
00518 void KTabWidget::resizeEvent( QResizeEvent *e )
00519 {
00520     QTabWidget::resizeEvent( e );
00521     resizeTabs();
00522 }
00523 
00524 #include "ktabwidget.moc"
KDE Logo
This file is part of the documentation for kdeui Library Version 3.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Sep 15 10:27:03 2005 by doxygen 1.4.4 written by Dimitri van Heesch, © 1997-2003