• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeui
 

tdeui

  • tdeui
ktabbar.cpp
1 /* This file is part of the KDE libraries
2  Copyright (C) 2003 Stephan Binner <binner@kde.org>
3  Copyright (C) 2003 Zack Rusin <zack@kde.org>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include <tqapplication.h>
22 #include <tqcursor.h>
23 #include <tqpainter.h>
24 #include <tqstyle.h>
25 #include <tqtimer.h>
26 #include <tqpushbutton.h>
27 #include <tqtooltip.h>
28 
29 #include <tdeglobalsettings.h>
30 #include <kiconloader.h>
31 #include <tdelocale.h>
32 
33 #include "ktabbar.h"
34 #include "ktabwidget.h"
35 
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 )
41 {
42  setAcceptDrops( true );
43  setMouseTracking( true );
44 
45  mEnableCloseButtonTimer = new TQTimer( this );
46  connect( mEnableCloseButtonTimer, TQ_SIGNAL( timeout() ), TQ_SLOT( enableCloseButton() ) );
47 
48  mActivateDragSwitchTabTimer = new TQTimer( this );
49  connect( mActivateDragSwitchTabTimer, TQ_SIGNAL( timeout() ), TQ_SLOT( activateDragSwitchTab() ) );
50 
51  connect(this, TQ_SIGNAL(layoutChanged()), TQ_SLOT(onLayoutChange()));
52 }
53 
54 KTabBar::~KTabBar()
55 {
56  //For the future
57  //delete d;
58 }
59 
60 void KTabBar::setTabEnabled( int id, bool enabled )
61 {
62  TQTab * t = tab( id );
63  if ( t ) {
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 );
71  else {
72  int index = indexOf( id );
73  index += ( index+1 == count() ) ? -1 : 1;
74  t = tabAt( index );
75  }
76 
77  if ( t->isEnabled() ) {
78  r = r.unite( t->rect() );
79  tablist->append( tablist->take( tablist->findRef( t ) ) );
80  emit selected( t->identifier() );
81  }
82  }
83  repaint( r );
84  }
85  }
86 }
87 
88 void KTabBar::mouseDoubleClickEvent( TQMouseEvent *e )
89 {
90  if( e->button() != TQt::LeftButton )
91  return;
92 
93  TQTab *tab = selectTab( e->pos() );
94  if( tab ) {
95  emit( mouseDoubleClick( indexOf( tab->identifier() ) ) );
96  return;
97  }
98  TQTabBar::mouseDoubleClickEvent( e );
99 }
100 
101 void KTabBar::mousePressEvent( TQMouseEvent *e )
102 {
103  if( e->button() == TQt::LeftButton ) {
104  mEnableCloseButtonTimer->stop();
105  mDragStart = e->pos();
106  }
107  else if( e->button() == TQt::RightButton ) {
108  TQTab *tab = selectTab( e->pos() );
109  if( tab ) {
110  emit( contextMenu( indexOf( tab->identifier() ), mapToGlobal( e->pos() ) ) );
111  return;
112  }
113  }
114  TQTabBar::mousePressEvent( e );
115 }
116 
117 void KTabBar::mouseMoveEvent( TQMouseEvent *e )
118 {
119  if ( e->state() == TQt::LeftButton ) {
120  TQTab *tab = selectTab( e->pos() );
121  if ( mDragSwitchTab && tab != mDragSwitchTab ) {
122  mActivateDragSwitchTabTimer->stop();
123  mDragSwitchTab = 0;
124  }
125 
126  int delay = TDEGlobalSettings::dndEventDelay();
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 )
130  {
131  if( tab ) {
132  emit( initiateDrag( indexOf( tab->identifier() ) ) );
133  return;
134  }
135  }
136  }
137  else if ( e->state() == TQt::MidButton ) {
138  if (mReorderStartTab==-1) {
139  int delay = TDEGlobalSettings::dndEventDelay();
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 )
143  {
144  TQTab *tab = selectTab( e->pos() );
145  if( tab && mTabReorderingEnabled ) {
146  mReorderStartTab = indexOf( tab->identifier() );
147  grabMouse( TQt::sizeAllCursor );
148  return;
149  }
150  }
151  }
152  else {
153  TQTab *tab = selectTab( e->pos() );
154  if( tab ) {
155  int reorderStopTab = indexOf( tab->identifier() );
156  if ( mReorderStartTab!=reorderStopTab && mReorderPreviousTab!=reorderStopTab ) {
157  emit( moveTab( mReorderStartTab, reorderStopTab ) );
158  mReorderPreviousTab=mReorderStartTab;
159  mReorderStartTab=reorderStopTab;
160  return;
161  }
162  }
163  }
164  }
165 
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);
171 
172  int xoff = 0, yoff = 0;
173  // The additional offsets were found by try and error, TODO: find the rational behind them
174  if ( t == tab( currentTab() ) ) {
175  xoff = style().pixelMetric( TQStyle::PM_TabBarTabShiftHorizontal, this ) + 3;
176  yoff = style().pixelMetric( TQStyle::PM_TabBarTabShiftVertical, this ) - 4;
177  }
178  else {
179  xoff = 7;
180  yoff = 0;
181  }
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 )
187  return;
188  mEnableCloseButtonTimer->stop();
189  mHoverCloseButton->deleteLater();
190  mHoverCloseButton = 0;
191  }
192 
193  mHoverCloseButton = new TQPushButton( this );
194  mHoverCloseButton->setIconSet( TDEGlobal::iconLoader()->loadIconSet("window-close", TDEIcon::Toolbar, TDEIcon::SizeSmall) );
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 );
202  }
203  mHoverCloseButtonTab = t;
204  connect( mHoverCloseButton, TQ_SIGNAL( clicked() ), TQ_SLOT( closeButtonClicked() ) );
205  return;
206  }
207  }
208  if ( mHoverCloseButton ) {
209  mEnableCloseButtonTimer->stop();
210  mHoverCloseButton->deleteLater();
211  mHoverCloseButton = 0;
212  }
213  }
214 
215  TQTabBar::mouseMoveEvent( e );
216 }
217 
218 void KTabBar::enableCloseButton()
219 {
220  mHoverCloseButton->setEnabled(true);
221 }
222 
223 void KTabBar::activateDragSwitchTab()
224 {
225  TQTab *tab = selectTab( mapFromGlobal( TQCursor::pos() ) );
226  if ( tab && mDragSwitchTab == tab )
227  setCurrentTab( mDragSwitchTab );
228  mDragSwitchTab = 0;
229 }
230 
231 void KTabBar::mouseReleaseEvent( TQMouseEvent *e )
232 {
233  if( e->button() == TQt::MidButton ) {
234  if ( mReorderStartTab==-1 ) {
235  TQTab *tab = selectTab( e->pos() );
236  if( tab ) {
237  emit( mouseMiddleClick( indexOf( tab->identifier() ) ) );
238  return;
239  }
240  }
241  else {
242  releaseMouse();
243  setCursor( TQt::arrowCursor );
244  mReorderStartTab=-1;
245  mReorderPreviousTab=-1;
246  }
247  }
248  TQTabBar::mouseReleaseEvent( e );
249 }
250 
251 void KTabBar::dragMoveEvent( TQDragMoveEvent *e )
252 {
253  TQTab *tab = selectTab( e->pos() );
254  if( tab ) {
255  bool accept = false;
256  // The receivers of the testCanDecode() signal has to adjust
257  // 'accept' accordingly.
258  emit testCanDecode( e, accept);
259  if ( accept && tab != TQTabBar::tab( currentTab() ) ) {
260  mDragSwitchTab = tab;
261  mActivateDragSwitchTabTimer->start( TQApplication::doubleClickInterval()*2, true );
262  }
263  e->accept( accept );
264  return;
265  }
266  e->accept( false );
267  TQTabBar::dragMoveEvent( e );
268 }
269 
270 void KTabBar::dropEvent( TQDropEvent *e )
271 {
272  TQTab *tab = selectTab( e->pos() );
273  if( tab ) {
274  mActivateDragSwitchTabTimer->stop();
275  mDragSwitchTab = 0;
276  emit( receivedDropEvent( indexOf( tab->identifier() ) , e ) );
277  return;
278  }
279  TQTabBar::dropEvent( e );
280 }
281 
282 #ifndef TQT_NO_WHEELEVENT
283 void KTabBar::wheelEvent( TQWheelEvent *e )
284 {
285  if ( e->orientation() == TQt::Horizontal )
286  return;
287 
288  emit( wheelDelta( e->delta() ) );
289 }
290 #endif
291 
292 void KTabBar::setTabColor( int id, const TQColor& color )
293 {
294  TQTab *t = tab( id );
295  if ( t ) {
296  mTabColors.insert( id, color );
297  repaint( t->rect(), false );
298  }
299 }
300 
301 void KTabBar::resetTabColor( int id )
302 {
303  TQTab *t = tab(id);
304  if (t) {
305  if (mTabColors.contains(id))
306  mTabColors.remove(id);
307  repaint(t->rect(), false);
308  }
309 }
310 
311 const TQColor &KTabBar::tabColor( int id ) const
312 {
313  if ( mTabColors.contains(id) && mTabColors[id].isValid() )
314  return mTabColors[id];
315 
316  return colorGroup().foreground();
317 }
318 
319 int KTabBar::insertTab( TQTab *t, int index )
320 {
321  int res = TQTabBar::insertTab( t, index );
322 
323  if ( mTabCloseActivatePrevious && count() > 2 ) {
324  TQPtrList<TQTab> *tablist = tabList();
325  tablist->insert( count()-2, tablist->take( tablist->findRef( t ) ) );
326  }
327 
328  return res;
329 }
330 
331 void KTabBar::removeTab( TQTab *t )
332 {
333  mTabColors.remove( t->identifier() );
334  TQTabBar::removeTab( t );
335 }
336 
337 void KTabBar::paintLabel( TQPainter *p, const TQRect& br,
338  TQTab *t, bool has_focus ) const
339 {
340  TQRect r = br;
341  bool selected = currentTab() == t->identifier();
342  if ( t->iconSet() ) {
343  // the tab has an iconset, draw it in the right mode
344  TQIconSet::Mode mode = ( t->isEnabled() && isEnabled() )
345  ? TQIconSet::Normal : TQIconSet::Disabled;
346  if ( mode == TQIconSet::Normal && has_focus )
347  mode = TQIconSet::Active;
348  TQPixmap pixmap = t->iconSet()->pixmap( TQIconSet::Small, mode );
349  int pixw = pixmap.width();
350  int pixh = pixmap.height();
351  r.setLeft( r.left() + pixw + 4 );
352  r.setRight( r.right() + 2 );
353 
354  int inactiveXShift = style().pixelMetric( TQStyle::PM_TabBarTabShiftHorizontal, this );
355  int inactiveYShift = style().pixelMetric( TQStyle::PM_TabBarTabShiftVertical, this );
356 
357  int right = t->text().isEmpty() ? br.right() - pixw : br.left() + 2;
358 
359  p->drawPixmap( right + (selected ? 0 : inactiveXShift),
360  br.center().y() - pixh / 2 + (selected ? 0 : inactiveYShift),
361  pixmap );
362  }
363 
364  TQStyle::SFlags flags = TQStyle::Style_Default;
365 
366  if ( isEnabled() && t->isEnabled() )
367  flags |= TQStyle::Style_Enabled;
368  if ( has_focus )
369  flags |= TQStyle::Style_HasFocus;
370 
371  TQColorGroup cg( colorGroup() );
372  if ( mTabColors.contains(t->identifier()) && mTabColors[t->identifier()].isValid() )
373  cg.setColor( TQColorGroup::Foreground, mTabColors[t->identifier()] );
374 
375  style().drawControl( TQStyle::CE_TabBarLabel, p, this, r,
376  t->isEnabled() ? cg : palette().disabled(),
377  flags, TQStyleOption(t) );
378 }
379 
380 bool KTabBar::isTabReorderingEnabled() const
381 {
382  return mTabReorderingEnabled;
383 }
384 
385 void KTabBar::setTabReorderingEnabled( bool on )
386 {
387  mTabReorderingEnabled = on;
388 }
389 
390 bool KTabBar::tabCloseActivatePrevious() const
391 {
392  return mTabCloseActivatePrevious;
393 }
394 
395 void KTabBar::setTabCloseActivatePrevious( bool on )
396 {
397  mTabCloseActivatePrevious = on;
398 }
399 
400 void KTabBar::closeButtonClicked()
401 {
402  emit closeRequest( indexOf( mHoverCloseButtonTab->identifier() ) );
403 }
404 
405 void KTabBar::setHoverCloseButton( bool button )
406 {
407  mHoverCloseButtonEnabled = button;
408  if ( !button )
409  onLayoutChange();
410 }
411 
412 bool KTabBar::hoverCloseButton() const
413 {
414  return mHoverCloseButtonEnabled;
415 }
416 
417 void KTabBar::setHoverCloseButtonDelayed( bool delayed )
418 {
419  mHoverCloseButtonDelayed = delayed;
420 }
421 
422 bool KTabBar::hoverCloseButtonDelayed() const
423 {
424  return mHoverCloseButtonDelayed;
425 }
426 
427 void KTabBar::onLayoutChange()
428 {
429  mEnableCloseButtonTimer->stop();
430  delete mHoverCloseButton;
431  mHoverCloseButton = 0;
432  mHoverCloseButtonTab = 0;
433  mActivateDragSwitchTabTimer->stop();
434  mDragSwitchTab = 0;
435 }
436 
437 #include "ktabbar.moc"
TDEGlobalSettings::dndEventDelay
static int dndEventDelay()
TDEGlobal::iconLoader
static TDEIconLoader * iconLoader()
TDEIcon::Toolbar
Toolbar
TDEIcon::SizeSmall
SizeSmall
TDEStdAccel::name
TQString name(StdAccel id)
tdelocale.h

tdeui

Skip menu "tdeui"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdeui

Skip menu "tdeui"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeui by doxygen 1.9.1
This website is maintained by Timothy Pearson.