26 #include <tqapplication.h> 28 #include <tqstylesheet.h> 30 #include <tdeconfig.h> 31 #include <kiconloader.h> 32 #include <kstringhandler.h> 34 #include "ktabwidget.h" 37 class KTabWidgetPrivate {
39 bool m_automaticResizeTabs;
42 unsigned int m_CurrentMaxLength;
43 bool m_mouseWheelScroll;
46 TQStringList m_tabNames;
48 KTabWidgetPrivate() : m_automaticResizeTabs(false), m_mouseWheelScroll(true)
53 m_CurrentMaxLength = m_minLength;
57 KTabWidget::KTabWidget( TQWidget *parent,
const char *name, WFlags f )
58 : TQTabWidget( parent, name, f )
60 d =
new KTabWidgetPrivate;
61 setTabBar(
new KTabBar(
this,
"tabbar") );
62 setAcceptDrops(
true );
64 setHoverCloseButtonDelayed(
false);
66 connect(tabBar(), TQT_SIGNAL(contextMenu(
int,
const TQPoint & )), TQT_SLOT(contextMenu(
int,
const TQPoint & )));
67 connect(tabBar(), TQT_SIGNAL(mouseDoubleClick(
int )), TQT_SLOT(mouseDoubleClick(
int )));
68 connect(tabBar(), TQT_SIGNAL(mouseMiddleClick(
int )), TQT_SLOT(mouseMiddleClick(
int )));
69 connect(tabBar(), TQT_SIGNAL(initiateDrag(
int )), TQT_SLOT(initiateDrag(
int )));
70 connect(tabBar(), TQT_SIGNAL(testCanDecode(
const TQDragMoveEvent *,
bool & )), TQT_SIGNAL(testCanDecode(
const TQDragMoveEvent *,
bool & )));
71 connect(tabBar(), TQT_SIGNAL(receivedDropEvent(
int, TQDropEvent * )), TQT_SLOT(receivedDropEvent(
int, TQDropEvent * )));
72 connect(tabBar(), TQT_SIGNAL(moveTab(
int,
int )), TQT_SLOT(moveTab(
int,
int )));
73 connect(tabBar(), TQT_SIGNAL(closeRequest(
int )), TQT_SLOT(closeRequest(
int )));
74 #ifndef QT_NO_WHEELEVENT 75 connect(tabBar(), TQT_SIGNAL(wheelDelta(
int )), TQT_SLOT(wheelDelta(
int )));
86 TQTabWidget::insertTab( child, label, index );
91 TQTabWidget::insertTab( child, iconset, label, index );
96 TQTabWidget::insertTab( child, tab, index);
97 if ( d->m_automaticResizeTabs ) {
98 if ( index < 0 || index >= count() ) {
99 d->m_tabNames.append( tab->text() );
100 resizeTabs( d->m_tabNames.count()-1 );
103 d->m_tabNames.insert( d->m_tabNames.at( index ), tab->text() );
111 TQWidget *rightcorner = this->cornerWidget( TopRight );
112 TQWidget *leftcorner = this->cornerWidget( TopLeft );
115 if ( leftcorner ) leftcorner->hide();
116 if ( rightcorner ) rightcorner->hide();
120 if ( leftcorner ) leftcorner->show();
121 if ( rightcorner ) rightcorner->show();
127 return !( tabBar()->isVisible() );
132 d->m_mouseWheelScroll = mouseWheelScroll;
137 TQTab *t = tabBar()->tabAt( indexOf( w ) );
139 static_cast<KTabBar*
>(tabBar())->setTabColor( t->identifier(), color );
145 TQTab *t = tabBar()->tabAt( indexOf( w ) );
147 return static_cast<KTabBar*
>(tabBar())->tabColor( t->identifier() );
155 static_cast<KTabBar*
>(tabBar())->setTabReorderingEnabled( on );
160 return static_cast<KTabBar*
>(tabBar())->isTabReorderingEnabled();
165 static_cast<KTabBar*
>(tabBar())->setTabCloseActivatePrevious( previous );
170 return static_cast<KTabBar*
>(tabBar())->tabCloseActivatePrevious();
173 unsigned int KTabWidget::tabBarWidthForMaxChars( uint maxLength )
176 hframe = tabBar()->style().pixelMetric( TQStyle::PM_TabBarTabHSpace, tabBar() );
177 overlap = tabBar()->style().pixelMetric( TQStyle::PM_TabBarTabOverlap, tabBar() );
179 TQFontMetrics fm = tabBar()->fontMetrics();
181 for(
int i=0; i < count(); ++i ) {
182 TQString newTitle = d->m_tabNames[ i ];
185 TQTab* tab = tabBar()->tabAt( i );
186 int lw = fm.width( newTitle );
188 if ( tab->iconSet() )
189 iw = tab->iconSet()->pixmap( TQIconSet::Small, TQIconSet::Normal ).width() + 4;
190 x += ( tabBar()->style().tqsizeFromContents( TQStyle::CT_TabBarTab,
this,
191 TQSize( TQMAX( lw + hframe + iw, TQApplication::globalStrut().width() ), 0 ),
192 TQStyleOption( tab ) ) ).width();
199 TQTabWidget::changeTab( w, label );
200 if ( d->m_automaticResizeTabs ) {
201 int index = indexOf( w );
203 d->m_tabNames[ index ] = label;
211 TQTabWidget::changeTab( w, iconset, label );
212 if ( d->m_automaticResizeTabs ) {
213 int index = indexOf( w );
215 d->m_tabNames[ index ] = label;
223 if ( d->m_automaticResizeTabs ) {
224 if ( index >= 0 && index < count() )
225 return d->m_tabNames[ index ];
227 return TQString::null;
230 return TQTabWidget::label( index );
235 if ( d->m_automaticResizeTabs ) {
236 int index = indexOf( w );
238 return TQString::null;
240 return d->m_tabNames[ index ];
243 return TQTabWidget::tabLabel( w );
248 TQTabWidget::setTabLabel( w, l );
249 if ( d->m_automaticResizeTabs ) {
250 int index = indexOf( w );
252 d->m_tabNames[ index ] = l;
258 void KTabWidget::resizeTabs(
int changeTabIndex )
261 if ( d->m_automaticResizeTabs ) {
263 newMaxLength=d->m_maxLength;
266 int tabBarHeight = tabBar()->sizeHint().height();
267 if ( cornerWidget( TopLeft ) && cornerWidget( TopLeft )->isVisible() )
268 lcw = TQMAX( cornerWidget( TopLeft )->width(), tabBarHeight );
269 if ( cornerWidget( TopRight ) && cornerWidget( TopRight )->isVisible() )
270 rcw = TQMAX( cornerWidget( TopRight )->width(), tabBarHeight );
272 uint maxTabBarWidth = width() - lcw - rcw;
274 for ( ; newMaxLength > (uint)d->m_minLength; newMaxLength-- ) {
275 if ( tabBarWidthForMaxChars( newMaxLength ) < maxTabBarWidth )
283 if ( d->m_CurrentMaxLength != newMaxLength ) {
284 d->m_CurrentMaxLength = newMaxLength;
285 for(
int i = 0; i < count(); ++i )
288 else if ( changeTabIndex != -1 )
289 updateTab( changeTabIndex );
292 void KTabWidget::updateTab(
int index )
294 TQString title = d->m_automaticResizeTabs ? d->m_tabNames[ index ] : TQTabWidget::label( index );
295 removeTabToolTip( page( index ) );
296 if ( title.length() > d->m_CurrentMaxLength ) {
297 if ( TQStyleSheet::mightBeRichText( title ) )
298 setTabToolTip( page( index ), TQStyleSheet::escape(title) );
300 setTabToolTip( page( index ), title );
304 title.replace(
'&',
"&&" );
306 if ( TQTabWidget::label( index ) != title )
307 TQTabWidget::setTabLabel( page( index ), title );
310 void KTabWidget::dragMoveEvent( TQDragMoveEvent *e )
312 if ( isEmptyTabbarSpace( e->pos() ) ) {
316 emit testCanDecode( e, accept);
321 TQTabWidget::dragMoveEvent( e );
324 void KTabWidget::dropEvent( TQDropEvent *e )
326 if ( isEmptyTabbarSpace( e->pos() ) ) {
327 emit ( receivedDropEvent( e ) );
330 TQTabWidget::dropEvent( e );
333 #ifndef QT_NO_WHEELEVENT 334 void KTabWidget::wheelEvent( TQWheelEvent *e )
336 if ( e->orientation() == Qt::Horizontal )
339 if ( isEmptyTabbarSpace( e->pos() ) )
340 wheelDelta( e->delta() );
345 void KTabWidget::wheelDelta(
int delta)
347 if (count()<2 || !d->m_mouseWheelScroll)
350 int page = currentPageIndex();
352 page = (page + 1) % count();
359 setCurrentPage(page);
363 void KTabWidget::mouseDoubleClickEvent( TQMouseEvent *e )
365 if( e->button() != Qt::LeftButton )
368 if ( isEmptyTabbarSpace( e->pos() ) ) {
369 emit( mouseDoubleClick() );
372 TQTabWidget::mouseDoubleClickEvent( e );
375 void KTabWidget::mousePressEvent( TQMouseEvent *e )
377 if ( e->button() == Qt::RightButton ) {
378 if ( isEmptyTabbarSpace( e->pos() ) ) {
379 emit( contextMenu( mapToGlobal( e->pos() ) ) );
382 }
else if ( e->button() == Qt::MidButton ) {
383 if ( isEmptyTabbarSpace( e->pos() ) ) {
384 emit( mouseMiddleClick() );
388 TQTabWidget::mousePressEvent( e );
393 emit( receivedDropEvent( page( index ), e ) );
398 emit( initiateDrag( page( index ) ) );
403 emit( contextMenu( page( index ), p ) );
408 emit( mouseDoubleClick( page( index ) ) );
413 emit( mouseMiddleClick( page( index ) ) );
418 TQString tablabel = label( from );
419 TQWidget *w = page( from );
420 TQColor color = tabColor( w );
421 TQIconSet tabiconset = tabIconSet( w );
422 TQString tabtooltip = tabToolTip( w );
423 bool current = ( w == currentPage() );
424 bool enabled = isTabEnabled( w );
429 TQTab * t =
new TQTab();
430 t->setText(tablabel);
431 TQTabWidget::insertTab( w, t, to );
432 if ( d->m_automaticResizeTabs ) {
433 if ( to < 0 || to >= count() )
434 d->m_tabNames.append( TQString::null );
436 d->m_tabNames.insert( d->m_tabNames.at( to ), TQString::null );
440 changeTab( w, tabiconset, tablabel );
441 setTabToolTip( w, tabtooltip );
442 setTabColor( w, color );
445 setTabEnabled( w, enabled );
448 emit ( movedTab( from, to ) );
452 if ( d->m_automaticResizeTabs ) {
453 int index = indexOf( w );
455 d->m_tabNames.remove( d->m_tabNames.at( index ) );
457 TQTabWidget::removePage( w );
458 if ( d->m_automaticResizeTabs )
463 bool KTabWidget::isEmptyTabbarSpace(
const TQPoint &point )
const 465 TQSize size( tabBar()->sizeHint() );
466 if ( ( tabPosition()==Top && point.y()< size.height() ) || ( tabPosition()==Bottom && point.y()>(height()-size.height() ) ) ) {
467 TQWidget *rightcorner = cornerWidget( TopRight );
469 if ( point.x()>=width()-rightcorner->width() )
472 TQWidget *leftcorner = cornerWidget( TopLeft );
474 if ( point.x()<=leftcorner->width() )
477 TQTab *tab = tabBar()->selectTab( tabBar()->mapFromParent( point ) );
486 static_cast<KTabBar*
>(tabBar())->setHoverCloseButton( button );
491 return static_cast<KTabBar*
>(tabBar())->hoverCloseButton();
496 static_cast<KTabBar*
>(tabBar())->setHoverCloseButtonDelayed( delayed );
501 return static_cast<KTabBar*
>(tabBar())->hoverCloseButtonDelayed();
506 if ( d->m_automaticResizeTabs==enabled )
509 d->m_automaticResizeTabs = enabled;
511 d->m_tabNames.clear();
512 for(
int i = 0; i < count(); ++i )
513 d->m_tabNames.append( tabBar()->tabAt( i )->text() );
516 for(
int i = 0; i < count(); ++i )
517 tabBar()->tabAt( i )->setText( d->m_tabNames[ i ] );
523 return d->m_automaticResizeTabs;
528 emit( closeRequest( page( index ) ) );
531 void KTabWidget::resizeEvent( TQResizeEvent *e )
533 TQTabWidget::resizeEvent( e );
537 #include "ktabwidget.moc"
static TQString rsqueeze(const TQString &str, uint maxlen=40)
static TDEConfig * config()
int readNumEntry(const TQString &pKey, int nDefault=0) const