21 #include <tqapplication.h>
23 #include <tqpainter.h>
26 #include <tqpushbutton.h>
27 #include <tqtooltip.h>
29 #include <tdeglobalsettings.h>
30 #include <kiconloader.h>
34 #include "ktabwidget.h"
36 KTabBar::KTabBar( TQWidget *parent,
const char *name )
37 : TQTabBar( parent, name ), mReorderStartTab( -1 ), mReorderPreviousTab( -1 ),
38 mHoverCloseButtonTab( 0 ), mDragSwitchTab( 0 ), mHoverCloseButton( 0 ),
39 mHoverCloseButtonEnabled( false ), mHoverCloseButtonDelayed( true ),
40 mTabReorderingEnabled( false ), mTabCloseActivatePrevious( false )
42 setAcceptDrops(
true );
43 setMouseTracking(
true );
45 mEnableCloseButtonTimer =
new TQTimer(
this );
46 connect( mEnableCloseButtonTimer, TQT_SIGNAL( timeout() ), TQT_SLOT( enableCloseButton() ) );
48 mActivateDragSwitchTabTimer =
new TQTimer(
this );
49 connect( mActivateDragSwitchTabTimer, TQT_SIGNAL( timeout() ), TQT_SLOT( activateDragSwitchTab() ) );
51 connect(
this, TQT_SIGNAL(layoutChanged()), TQT_SLOT(onLayoutChange()));
60 void KTabBar::setTabEnabled(
int id,
bool enabled )
62 TQTab * t = tab(
id );
64 if ( t->isEnabled() != enabled ) {
65 t->setEnabled( enabled );
66 TQRect r( t->rect() );
67 if ( !enabled &&
id == currentTab() && count()>1 ) {
68 TQPtrList<TQTab> *tablist = tabList();
69 if ( mTabCloseActivatePrevious )
70 t = tablist->at( count()-2 );
72 int index = indexOf(
id );
73 index += ( index+1 == count() ) ? -1 : 1;
77 if ( t->isEnabled() ) {
78 r = r.unite( t->rect() );
79 tablist->append( tablist->take( tablist->findRef( t ) ) );
80 emit selected( t->identifier() );
88 void KTabBar::mouseDoubleClickEvent( TQMouseEvent *e )
90 if( e->button() != Qt::LeftButton )
93 TQTab *tab = selectTab( e->pos() );
95 emit( mouseDoubleClick( indexOf( tab->identifier() ) ) );
98 TQTabBar::mouseDoubleClickEvent( e );
101 void KTabBar::mousePressEvent( TQMouseEvent *e )
103 if( e->button() == Qt::LeftButton ) {
104 mEnableCloseButtonTimer->stop();
105 mDragStart = e->pos();
107 else if( e->button() == Qt::RightButton ) {
108 TQTab *tab = selectTab( e->pos() );
110 emit( contextMenu( indexOf( tab->identifier() ), mapToGlobal( e->pos() ) ) );
114 TQTabBar::mousePressEvent( e );
117 void KTabBar::mouseMoveEvent( TQMouseEvent *e )
119 if ( e->state() == Qt::LeftButton ) {
120 TQTab *tab = selectTab( e->pos() );
121 if ( mDragSwitchTab && tab != mDragSwitchTab ) {
122 mActivateDragSwitchTabTimer->stop();
127 TQPoint newPos = e->pos();
128 if( newPos.x() > mDragStart.x()+delay || newPos.x() < mDragStart.x()-delay ||
129 newPos.y() > mDragStart.y()+delay || newPos.y() < mDragStart.y()-delay )
132 emit( initiateDrag( indexOf( tab->identifier() ) ) );
137 else if ( e->state() == Qt::MidButton ) {
138 if (mReorderStartTab==-1) {
140 TQPoint newPos = e->pos();
141 if( newPos.x() > mDragStart.x()+delay || newPos.x() < mDragStart.x()-delay ||
142 newPos.y() > mDragStart.y()+delay || newPos.y() < mDragStart.y()-delay )
144 TQTab *tab = selectTab( e->pos() );
145 if( tab && mTabReorderingEnabled ) {
146 mReorderStartTab = indexOf( tab->identifier() );
147 grabMouse( tqsizeAllCursor );
153 TQTab *tab = selectTab( e->pos() );
155 int reorderStopTab = indexOf( tab->identifier() );
156 if ( mReorderStartTab!=reorderStopTab && mReorderPreviousTab!=reorderStopTab ) {
157 emit( moveTab( mReorderStartTab, reorderStopTab ) );
158 mReorderPreviousTab=mReorderStartTab;
159 mReorderStartTab=reorderStopTab;
166 if ( mHoverCloseButtonEnabled && mReorderStartTab==-1) {
167 TQTab *t = selectTab( e->pos() );
168 if( t && t->iconSet() && t->isEnabled() ) {
169 TQPixmap pixmap = t->iconSet()->pixmap( TQIconSet::Small, TQIconSet::Normal );
170 TQRect rect( 0, 0, pixmap.width() + 4, pixmap.height() +4);
172 int xoff = 0, yoff = 0;
174 if ( t == tab( currentTab() ) ) {
175 xoff = style().pixelMetric( TQStyle::PM_TabBarTabShiftHorizontal,
this ) + 3;
176 yoff = style().pixelMetric( TQStyle::PM_TabBarTabShiftVertical,
this ) - 4;
182 rect.moveLeft( t->rect().left() + 2 + xoff );
183 rect.moveTop( t->rect().center().y()-pixmap.height()/2 + yoff );
184 if ( rect.contains( e->pos() ) ) {
185 if ( mHoverCloseButton ) {
186 if ( mHoverCloseButtonTab == t )
188 mEnableCloseButtonTimer->stop();
189 mHoverCloseButton->deleteLater();
190 mHoverCloseButton = 0;
193 mHoverCloseButton =
new TQPushButton(
this );
195 mHoverCloseButton->setGeometry( rect );
196 TQToolTip::add(mHoverCloseButton,i18n(
"Close this tab"));
197 mHoverCloseButton->setFlat(
true);
198 mHoverCloseButton->show();
199 if ( mHoverCloseButtonDelayed ) {
200 mHoverCloseButton->setEnabled(
false);
201 mEnableCloseButtonTimer->start( TQApplication::doubleClickInterval(),
true );
203 mHoverCloseButtonTab = t;
204 connect( mHoverCloseButton, TQT_SIGNAL( clicked() ), TQT_SLOT( closeButtonClicked() ) );
208 if ( mHoverCloseButton ) {
209 mEnableCloseButtonTimer->stop();
210 mHoverCloseButton->deleteLater();
211 mHoverCloseButton = 0;
215 TQTabBar::mouseMoveEvent( e );
218 void KTabBar::enableCloseButton()
220 mHoverCloseButton->setEnabled(
true);
223 void KTabBar::activateDragSwitchTab()
225 TQTab *tab = selectTab( mapFromGlobal( TQCursor::pos() ) );
226 if ( tab && mDragSwitchTab == tab )
227 setCurrentTab( mDragSwitchTab );
231 void KTabBar::mouseReleaseEvent( TQMouseEvent *e )
233 if( e->button() == Qt::MidButton ) {
234 if ( mReorderStartTab==-1 ) {
235 TQTab *tab = selectTab( e->pos() );
237 emit( mouseMiddleClick( indexOf( tab->identifier() ) ) );
243 setCursor( tqarrowCursor );
245 mReorderPreviousTab=-1;
248 TQTabBar::mouseReleaseEvent( e );
251 void KTabBar::dragMoveEvent( TQDragMoveEvent *e )
253 TQTab *tab = selectTab( e->pos() );
258 emit testCanDecode( e, accept);
259 if ( accept && tab != TQTabBar::tab( currentTab() ) ) {
260 mDragSwitchTab = tab;
261 mActivateDragSwitchTabTimer->start( TQApplication::doubleClickInterval()*2,
true );
267 TQTabBar::dragMoveEvent( e );
270 void KTabBar::dropEvent( TQDropEvent *e )
272 TQTab *tab = selectTab( e->pos() );
274 mActivateDragSwitchTabTimer->stop();
276 emit( receivedDropEvent( indexOf( tab->identifier() ) , e ) );
279 TQTabBar::dropEvent( e );
282 #ifndef QT_NO_WHEELEVENT
283 void KTabBar::wheelEvent( TQWheelEvent *e )
288 emit( wheelDelta( e->delta() ) );
292 void KTabBar::setTabColor(
int id,
const TQColor& color )
294 TQTab *t = tab(
id );
296 mTabColors.insert(
id, color );
297 repaint( t->rect(), false );
301 const TQColor &KTabBar::tabColor(
int id )
const
303 if ( mTabColors.contains(
id ) )
304 return mTabColors[
id];
306 return colorGroup().foreground();
309 int KTabBar::insertTab( TQTab *t,
int index )
311 int res = TQTabBar::insertTab( t, index );
313 if ( mTabCloseActivatePrevious && count() > 2 ) {
314 TQPtrList<TQTab> *tablist = tabList();
315 tablist->insert( count()-2, tablist->take( tablist->findRef( t ) ) );
321 void KTabBar::removeTab( TQTab *t )
323 mTabColors.remove( t->identifier() );
324 TQTabBar::removeTab( t );
327 void KTabBar::paintLabel( TQPainter *p,
const TQRect& br,
328 TQTab *t,
bool has_focus )
const
331 bool selected = currentTab() == t->identifier();
332 if ( t->iconSet() ) {
334 TQIconSet::Mode mode = ( t->isEnabled() && isEnabled() )
335 ? TQIconSet::Normal : TQIconSet::Disabled;
336 if ( mode == TQIconSet::Normal && has_focus )
337 mode = TQIconSet::Active;
338 TQPixmap pixmap = t->iconSet()->pixmap( TQIconSet::Small, mode );
339 int pixw = pixmap.width();
340 int pixh = pixmap.height();
341 r.setLeft( r.left() + pixw + 4 );
342 r.setRight( r.right() + 2 );
344 int inactiveXShift = style().pixelMetric( TQStyle::PM_TabBarTabShiftHorizontal,
this );
345 int inactiveYShift = style().pixelMetric( TQStyle::PM_TabBarTabShiftVertical,
this );
347 int right = t->text().isEmpty() ? br.right() - pixw : br.left() + 2;
349 p->drawPixmap( right + (selected ? 0 : inactiveXShift),
350 br.center().y() - pixh / 2 + (selected ? 0 : inactiveYShift),
354 TQStyle::SFlags flags = TQStyle::Style_Default;
356 if ( isEnabled() && t->isEnabled() )
357 flags |= TQStyle::Style_Enabled;
359 flags |= TQStyle::Style_HasFocus;
361 TQColorGroup cg( colorGroup() );
362 if ( mTabColors.contains( t->identifier() ) )
363 cg.setColor( TQColorGroup::Foreground, mTabColors[t->identifier()] );
365 style().drawControl( TQStyle::CE_TabBarLabel, p,
this, r,
366 t->isEnabled() ? cg : palette().disabled(),
367 flags, TQStyleOption(t) );
370 bool KTabBar::isTabReorderingEnabled()
const
372 return mTabReorderingEnabled;
375 void KTabBar::setTabReorderingEnabled(
bool on )
377 mTabReorderingEnabled = on;
380 bool KTabBar::tabCloseActivatePrevious()
const
382 return mTabCloseActivatePrevious;
385 void KTabBar::setTabCloseActivatePrevious(
bool on )
387 mTabCloseActivatePrevious = on;
390 void KTabBar::closeButtonClicked()
392 emit closeRequest( indexOf( mHoverCloseButtonTab->identifier() ) );
395 void KTabBar::setHoverCloseButton(
bool button )
397 mHoverCloseButtonEnabled = button;
402 bool KTabBar::hoverCloseButton()
const
404 return mHoverCloseButtonEnabled;
407 void KTabBar::setHoverCloseButtonDelayed(
bool delayed )
409 mHoverCloseButtonDelayed = delayed;
412 bool KTabBar::hoverCloseButtonDelayed()
const
414 return mHoverCloseButtonDelayed;
417 void KTabBar::onLayoutChange()
419 mEnableCloseButtonTimer->stop();
420 delete mHoverCloseButton;
421 mHoverCloseButton = 0;
422 mHoverCloseButtonTab = 0;
423 mActivateDragSwitchTabTimer->stop();
427 #include "ktabbar.moc"
static TDEIconLoader * iconLoader()
static int dndEventDelay()