21 #include <tqapplication.h>
23 #include <tqstylesheet.h>
26 #include <kiconloader.h>
27 #include <kstringhandler.h>
29 #include "ktabwidget.h"
32 class KTabWidgetPrivate {
34 bool m_automaticResizeTabs;
37 unsigned int m_CurrentMaxLength;
41 TQStringList m_tabNames;
44 m_automaticResizeTabs =
false;
48 m_CurrentMaxLength = m_minLength;
52 KTabWidget::KTabWidget( TQWidget *parent,
const char *name, WFlags f )
53 : TQTabWidget( parent, name, f )
55 d =
new KTabWidgetPrivate;
56 setTabBar(
new KTabBar(
this,
"tabbar") );
57 setAcceptDrops(
true );
59 setHoverCloseButtonDelayed(
false);
61 connect(tabBar(), TQT_SIGNAL(contextMenu(
int,
const TQPoint & )), TQT_SLOT(contextMenu(
int,
const TQPoint & )));
62 connect(tabBar(), TQT_SIGNAL(mouseDoubleClick(
int )), TQT_SLOT(mouseDoubleClick(
int )));
63 connect(tabBar(), TQT_SIGNAL(mouseMiddleClick(
int )), TQT_SLOT(mouseMiddleClick(
int )));
64 connect(tabBar(), TQT_SIGNAL(initiateDrag(
int )), TQT_SLOT(initiateDrag(
int )));
65 connect(tabBar(), TQT_SIGNAL(testCanDecode(
const TQDragMoveEvent *,
bool & )), TQT_SIGNAL(testCanDecode(
const TQDragMoveEvent *,
bool & )));
66 connect(tabBar(), TQT_SIGNAL(receivedDropEvent(
int, TQDropEvent * )), TQT_SLOT(receivedDropEvent(
int, TQDropEvent * )));
67 connect(tabBar(), TQT_SIGNAL(moveTab(
int,
int )), TQT_SLOT(moveTab(
int,
int )));
68 connect(tabBar(), TQT_SIGNAL(closeRequest(
int )), TQT_SLOT(closeRequest(
int )));
69 #ifndef QT_NO_WHEELEVENT
70 connect(tabBar(), TQT_SIGNAL(wheelDelta(
int )), TQT_SLOT(wheelDelta(
int )));
81 TQTabWidget::insertTab( child, label, index );
86 TQTabWidget::insertTab( child, iconset, label, index );
91 TQTabWidget::insertTab( child, tab, index);
92 if ( d->m_automaticResizeTabs ) {
93 if ( index < 0 || index >= count() ) {
94 d->m_tabNames.append( tab->text() );
95 resizeTabs( d->m_tabNames.count()-1 );
98 d->m_tabNames.insert( d->m_tabNames.at( index ), tab->text() );
106 TQWidget *rightcorner = this->cornerWidget( TopRight );
107 TQWidget *leftcorner = this->cornerWidget( TopLeft );
110 if ( leftcorner ) leftcorner->hide();
111 if ( rightcorner ) rightcorner->hide();
115 if ( leftcorner ) leftcorner->show();
116 if ( rightcorner ) rightcorner->show();
122 return !( tabBar()->isVisible() );
127 TQTab *t = tabBar()->tabAt( indexOf( w ) );
135 TQTab *t = tabBar()->tabAt( indexOf( w ) );
163 unsigned int KTabWidget::tabBarWidthForMaxChars( uint maxLength )
166 hframe = tabBar()->style().pixelMetric( TQStyle::PM_TabBarTabHSpace, tabBar() );
167 overlap = tabBar()->style().pixelMetric( TQStyle::PM_TabBarTabOverlap, tabBar() );
169 TQFontMetrics fm = tabBar()->fontMetrics();
171 for(
int i=0; i < count(); ++i ) {
172 TQString newTitle = d->m_tabNames[ i ];
175 TQTab* tab = tabBar()->tabAt( i );
176 int lw = fm.width( newTitle );
178 if ( tab->iconSet() )
179 iw = tab->iconSet()->pixmap( TQIconSet::Small, TQIconSet::Normal ).width() + 4;
180 x += ( tabBar()->style().tqsizeFromContents( TQStyle::CT_TabBarTab,
this,
181 TQSize( QMAX( lw + hframe + iw, TQApplication::globalStrut().width() ), 0 ),
182 TQStyleOption( tab ) ) ).width();
189 TQTabWidget::changeTab( w, label );
190 if ( d->m_automaticResizeTabs ) {
191 int index = indexOf( w );
193 d->m_tabNames[ index ] =
label;
201 TQTabWidget::changeTab( w, iconset, label );
202 if ( d->m_automaticResizeTabs ) {
203 int index = indexOf( w );
205 d->m_tabNames[ index ] =
label;
213 if ( d->m_automaticResizeTabs ) {
214 if ( index >= 0 && index < count() )
215 return d->m_tabNames[ index ];
217 return TQString::null;
220 return TQTabWidget::label( index );
225 if ( d->m_automaticResizeTabs ) {
226 int index = indexOf( w );
228 return TQString::null;
230 return d->m_tabNames[ index ];
233 return TQTabWidget::tabLabel( w );
238 TQTabWidget::setTabLabel( w, l );
239 if ( d->m_automaticResizeTabs ) {
240 int index = indexOf( w );
242 d->m_tabNames[ index ] = l;
248 void KTabWidget::resizeTabs(
int changeTabIndex )
251 if ( d->m_automaticResizeTabs ) {
253 newMaxLength=d->m_maxLength;
256 int tabBarHeight = tabBar()->sizeHint().height();
257 if ( cornerWidget( TopLeft ) && cornerWidget( TopLeft )->isVisible() )
258 lcw = QMAX( cornerWidget( TopLeft )->width(), tabBarHeight );
259 if ( cornerWidget( TopRight ) && cornerWidget( TopRight )->isVisible() )
260 rcw = QMAX( cornerWidget( TopRight )->width(), tabBarHeight );
262 uint maxTabBarWidth = width() - lcw - rcw;
264 for ( ; newMaxLength > (uint)d->m_minLength; newMaxLength-- ) {
265 if ( tabBarWidthForMaxChars( newMaxLength ) < maxTabBarWidth )
273 if ( d->m_CurrentMaxLength != newMaxLength ) {
274 d->m_CurrentMaxLength = newMaxLength;
275 for(
int i = 0; i < count(); ++i )
278 else if ( changeTabIndex != -1 )
279 updateTab( changeTabIndex );
282 void KTabWidget::updateTab(
int index )
284 TQString title = d->m_automaticResizeTabs ? d->m_tabNames[ index ] : TQTabWidget::label( index );
285 removeTabToolTip( page( index ) );
286 if ( title.length() > d->m_CurrentMaxLength ) {
287 if ( TQStyleSheet::mightBeRichText( title ) )
288 setTabToolTip( page( index ), TQStyleSheet::escape(title) );
290 setTabToolTip( page( index ), title );
294 title.replace(
'&',
"&&" );
296 if ( TQTabWidget::label( index ) != title )
297 TQTabWidget::setTabLabel( page( index ), title );
300 void KTabWidget::dragMoveEvent( TQDragMoveEvent *e )
302 if ( isEmptyTabbarSpace( e->pos() ) ) {
311 TQTabWidget::dragMoveEvent( e );
314 void KTabWidget::dropEvent( TQDropEvent *e )
316 if ( isEmptyTabbarSpace( e->pos() ) ) {
320 TQTabWidget::dropEvent( e );
323 #ifndef QT_NO_WHEELEVENT
324 void KTabWidget::wheelEvent( TQWheelEvent *e )
326 if ( e->orientation() == Qt::Horizontal )
329 if ( isEmptyTabbarSpace( e->pos() ) )
330 wheelDelta( e->delta() );
335 void KTabWidget::wheelDelta(
int delta )
340 int page = currentPageIndex();
342 page = (page + 1) % count();
348 setCurrentPage( page );
352 void KTabWidget::mouseDoubleClickEvent( TQMouseEvent *e )
354 if( e->button() != Qt::LeftButton )
357 if ( isEmptyTabbarSpace( e->pos() ) ) {
361 TQTabWidget::mouseDoubleClickEvent( e );
364 void KTabWidget::mousePressEvent( TQMouseEvent *e )
366 if ( e->button() == Qt::RightButton ) {
367 if ( isEmptyTabbarSpace( e->pos() ) ) {
371 }
else if ( e->button() == Qt::MidButton ) {
372 if ( isEmptyTabbarSpace( e->pos() ) ) {
377 TQTabWidget::mousePressEvent( e );
407 TQString tablabel =
label( from );
408 TQWidget *w = page( from );
410 TQIconSet tabiconset = tabIconSet( w );
411 TQString tabtooltip = tabToolTip( w );
412 bool current = ( w == currentPage() );
413 bool enabled = isTabEnabled( w );
418 TQTab * t =
new TQTab();
419 t->setText(tablabel);
420 TQTabWidget::insertTab( w, t, to );
421 if ( d->m_automaticResizeTabs ) {
422 if ( to < 0 || to >= count() )
423 d->m_tabNames.append( TQString::null );
425 d->m_tabNames.insert( d->m_tabNames.at( to ), TQString::null );
430 setTabToolTip( w, tabtooltip );
434 setTabEnabled( w, enabled );
441 if ( d->m_automaticResizeTabs ) {
442 int index = indexOf( w );
444 d->m_tabNames.remove( d->m_tabNames.at( index ) );
446 TQTabWidget::removePage( w );
447 if ( d->m_automaticResizeTabs )
452 bool KTabWidget::isEmptyTabbarSpace(
const TQPoint &point )
const
454 TQSize size( tabBar()->sizeHint() );
455 if ( ( tabPosition()==Top && point.y()< size.height() ) || ( tabPosition()==Bottom && point.y()>(height()-size.height() ) ) ) {
456 TQWidget *rightcorner = cornerWidget( TopRight );
458 if ( point.x()>=width()-rightcorner->width() )
461 TQWidget *leftcorner = cornerWidget( TopLeft );
463 if ( point.x()<=leftcorner->width() )
466 TQTab *tab = tabBar()->selectTab( tabBar()->mapFromParent( point ) );
495 if ( d->m_automaticResizeTabs==enabled )
498 d->m_automaticResizeTabs = enabled;
500 d->m_tabNames.clear();
501 for(
int i = 0; i < count(); ++i )
502 d->m_tabNames.append( tabBar()->tabAt( i )->text() );
505 for(
int i = 0; i < count(); ++i )
506 tabBar()->tabAt( i )->setText( d->m_tabNames[ i ] );
512 return d->m_automaticResizeTabs;
520 void KTabWidget::resizeEvent( TQResizeEvent *e )
522 TQTabWidget::resizeEvent( e );
526 #include "ktabwidget.moc"
static TQString rsqueeze(const TQString &str, uint maxlen=40)
int readNumEntry(const TQString &pKey, int nDefault=0) const
static KConfig * config()