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

tdeui

  • tdeui
ktabwidget.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  Additional changes:
22  - 2013/10/14 Michele Calgaro:
23  add "scroll tabs on mouse wheel event" functionality
24 */
25 
26 #include <tqapplication.h>
27 #include <tqstyle.h>
28 #include <tqstylesheet.h>
29 
30 #include <tdeconfig.h>
31 #include <kiconloader.h>
32 #include <kstringhandler.h>
33 
34 #include "ktabwidget.h"
35 #include "ktabbar.h"
36 
37 class KTabWidgetPrivate {
38 public:
39  bool m_automaticResizeTabs;
40  int m_maxLength;
41  int m_minLength;
42  unsigned int m_CurrentMaxLength;
43  bool m_mouseWheelScroll;
44 
45  // Holds the full names of the tab, otherwise all we know about is the shortened name
46  TQStringList m_tabNames;
47 
48  KTabWidgetPrivate() : m_automaticResizeTabs(false), m_mouseWheelScroll(true)
49  {
50  TDEConfigGroupSaver groupsaver(TDEGlobal::config(), "General");
51  m_maxLength = TDEGlobal::config()->readNumEntry("MaximumTabLength", 30);
52  m_minLength = TDEGlobal::config()->readNumEntry("MinimumTabLength", 3);
53  m_CurrentMaxLength = m_minLength;
54  }
55 };
56 
57 KTabWidget::KTabWidget( TQWidget *parent, const char *name, WFlags f )
58  : TQTabWidget( parent, name, f )
59 {
60  d = new KTabWidgetPrivate;
61  setTabBar( new KTabBar(this, "tabbar") );
62  setAcceptDrops( true );
63 
64  setHoverCloseButtonDelayed(false);
65 
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 )));
76 #endif
77 }
78 
79 KTabWidget::~KTabWidget()
80 {
81  delete d;
82 }
83 
84 void KTabWidget::insertTab( TQWidget *child, const TQString &label, int index )
85 {
86  TQTabWidget::insertTab( child, label, index );
87 }
88 
89 void KTabWidget::insertTab( TQWidget *child, const TQIconSet& iconset, const TQString &label, int index )
90 {
91  TQTabWidget::insertTab( child, iconset, label, index );
92 }
93 
94 void KTabWidget::insertTab( TQWidget *child, TQTab *tab, int index )
95 {
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 );
101  }
102  else {
103  d->m_tabNames.insert( d->m_tabNames.at( index ), tab->text() );
104  resizeTabs( index );
105  }
106  }
107 }
108 
109 void KTabWidget::setTabBarHidden( bool hide )
110 {
111  TQWidget *rightcorner = this->cornerWidget( TopRight );
112  TQWidget *leftcorner = this->cornerWidget( TopLeft );
113 
114  if ( hide ) {
115  if ( leftcorner ) leftcorner->hide();
116  if ( rightcorner ) rightcorner->hide();
117  tabBar()->hide();
118  } else {
119  tabBar()->show();
120  if ( leftcorner ) leftcorner->show();
121  if ( rightcorner ) rightcorner->show();
122  }
123 }
124 
125 bool KTabWidget::isTabBarHidden() const
126 {
127  return !( tabBar()->isVisible() );
128 }
129 
130 void KTabWidget::setMouseWheelScroll(bool mouseWheelScroll)
131 {
132  d->m_mouseWheelScroll = mouseWheelScroll;
133 }
134 
135 void KTabWidget::setTabColor( TQWidget *w, const TQColor& color )
136 {
137  TQTab *t = tabBar()->tabAt( indexOf( w ) );
138  if (t) {
139  static_cast<KTabBar*>(tabBar())->setTabColor( t->identifier(), color );
140  }
141 }
142 
143 TQColor KTabWidget::tabColor( TQWidget *w ) const
144 {
145  TQTab *t = tabBar()->tabAt( indexOf( w ) );
146  if (t) {
147  return static_cast<KTabBar*>(tabBar())->tabColor( t->identifier() );
148  } else {
149  return TQColor();
150  }
151 }
152 
153 void KTabWidget::setTabReorderingEnabled( bool on)
154 {
155  static_cast<KTabBar*>(tabBar())->setTabReorderingEnabled( on );
156 }
157 
158 bool KTabWidget::isTabReorderingEnabled() const
159 {
160  return static_cast<KTabBar*>(tabBar())->isTabReorderingEnabled();
161 }
162 
163 void KTabWidget::setTabCloseActivatePrevious( bool previous)
164 {
165  static_cast<KTabBar*>(tabBar())->setTabCloseActivatePrevious( previous );
166 }
167 
168 bool KTabWidget::tabCloseActivatePrevious() const
169 {
170  return static_cast<KTabBar*>(tabBar())->tabCloseActivatePrevious();
171 }
172 
173 unsigned int KTabWidget::tabBarWidthForMaxChars( uint maxLength )
174 {
175  int hframe, overlap;
176  hframe = tabBar()->style().pixelMetric( TQStyle::PM_TabBarTabHSpace, tabBar() );
177  overlap = tabBar()->style().pixelMetric( TQStyle::PM_TabBarTabOverlap, tabBar() );
178 
179  TQFontMetrics fm = tabBar()->fontMetrics();
180  int x = 0;
181  for( int i=0; i < count(); ++i ) {
182  TQString newTitle = d->m_tabNames[ i ];
183  newTitle = KStringHandler::rsqueeze( newTitle, maxLength ).leftJustify( d->m_minLength, ' ' );
184 
185  TQTab* tab = tabBar()->tabAt( i );
186  int lw = fm.width( newTitle );
187  int iw = 0;
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();
193  }
194  return x;
195 }
196 
197 void KTabWidget::changeTab( TQWidget *w, const TQString &label )
198 {
199  TQTabWidget::changeTab( w, label );
200  if ( d->m_automaticResizeTabs ) {
201  int index = indexOf( w );
202  if ( index != -1 ) {
203  d->m_tabNames[ index ] = label;
204  resizeTabs( index );
205  }
206  }
207 }
208 
209 void KTabWidget::changeTab( TQWidget *w, const TQIconSet &iconset, const TQString &label )
210 {
211  TQTabWidget::changeTab( w, iconset, label );
212  if ( d->m_automaticResizeTabs ) {
213  int index = indexOf( w );
214  if ( index != -1 ) {
215  d->m_tabNames[ index ] = label;
216  resizeTabs( index );
217  }
218  }
219 }
220 
221 TQString KTabWidget::label( int index ) const
222 {
223  if ( d->m_automaticResizeTabs ) {
224  if ( index >= 0 && index < count() )
225  return d->m_tabNames[ index ];
226  else
227  return TQString::null;
228  }
229  else
230  return TQTabWidget::label( index );
231 }
232 
233 TQString KTabWidget::tabLabel( TQWidget * w ) const
234 {
235  if ( d->m_automaticResizeTabs ) {
236  int index = indexOf( w );
237  if ( index == -1 )
238  return TQString::null;
239  else
240  return d->m_tabNames[ index ];
241  }
242  else
243  return TQTabWidget::tabLabel( w );
244 }
245 
246 void KTabWidget::setTabLabel( TQWidget *w, const TQString &l )
247 {
248  TQTabWidget::setTabLabel( w, l );
249  if ( d->m_automaticResizeTabs ) {
250  int index = indexOf( w );
251  if ( index != -1 ) {
252  d->m_tabNames[ index ] = l;
253  resizeTabs( index );
254  }
255  }
256 }
257 
258 void KTabWidget::resizeTabs( int changeTabIndex )
259 {
260  uint newMaxLength;
261  if ( d->m_automaticResizeTabs ) {
262  // Calculate new max length
263  newMaxLength=d->m_maxLength;
264  uint lcw=0, rcw=0;
265 
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 );
271 
272  uint maxTabBarWidth = width() - lcw - rcw;
273 
274  for ( ; newMaxLength > (uint)d->m_minLength; newMaxLength-- ) {
275  if ( tabBarWidthForMaxChars( newMaxLength ) < maxTabBarWidth )
276  break;
277  }
278  }
279  else
280  newMaxLength = 4711;
281 
282  // Update hinted or all tabs
283  if ( d->m_CurrentMaxLength != newMaxLength ) {
284  d->m_CurrentMaxLength = newMaxLength;
285  for( int i = 0; i < count(); ++i )
286  updateTab( i );
287  }
288  else if ( changeTabIndex != -1 )
289  updateTab( changeTabIndex );
290 }
291 
292 void KTabWidget::updateTab( int index )
293 {
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) );
299  else
300  setTabToolTip( page( index ), title );
301  }
302 
303  title = KStringHandler::rsqueeze( title, d->m_CurrentMaxLength ).leftJustify( d->m_minLength, ' ' );
304  title.replace( '&', "&&" );
305 
306  if ( TQTabWidget::label( index ) != title )
307  TQTabWidget::setTabLabel( page( index ), title );
308 }
309 
310 void KTabWidget::dragMoveEvent( TQDragMoveEvent *e )
311 {
312  if ( isEmptyTabbarSpace( e->pos() ) ) {
313  bool accept = false;
314  // The receivers of the testCanDecode() signal has to adjust
315  // 'accept' accordingly.
316  emit testCanDecode( e, accept);
317  e->accept( accept );
318  return;
319  }
320  e->accept( false );
321  TQTabWidget::dragMoveEvent( e );
322 }
323 
324 void KTabWidget::dropEvent( TQDropEvent *e )
325 {
326  if ( isEmptyTabbarSpace( e->pos() ) ) {
327  emit ( receivedDropEvent( e ) );
328  return;
329  }
330  TQTabWidget::dropEvent( e );
331 }
332 
333 #ifndef QT_NO_WHEELEVENT
334 void KTabWidget::wheelEvent( TQWheelEvent *e )
335 {
336  if ( e->orientation() == Qt::Horizontal )
337  return;
338 
339  if ( isEmptyTabbarSpace( e->pos() ) )
340  wheelDelta( e->delta() );
341  else
342  e->ignore();
343 }
344 
345 void KTabWidget::wheelDelta(int delta)
346 {
347  if (count()<2 || !d->m_mouseWheelScroll)
348  return;
349 
350  int page = currentPageIndex();
351  if (delta<0)
352  page = (page + 1) % count();
353  else
354  {
355  page--;
356  if (page<0)
357  page = count() - 1;
358  }
359  setCurrentPage(page);
360 }
361 #endif
362 
363 void KTabWidget::mouseDoubleClickEvent( TQMouseEvent *e )
364 {
365  if( e->button() != Qt::LeftButton )
366  return;
367 
368  if ( isEmptyTabbarSpace( e->pos() ) ) {
369  emit( mouseDoubleClick() );
370  return;
371  }
372  TQTabWidget::mouseDoubleClickEvent( e );
373 }
374 
375 void KTabWidget::mousePressEvent( TQMouseEvent *e )
376 {
377  if ( e->button() == Qt::RightButton ) {
378  if ( isEmptyTabbarSpace( e->pos() ) ) {
379  emit( contextMenu( mapToGlobal( e->pos() ) ) );
380  return;
381  }
382  } else if ( e->button() == Qt::MidButton ) {
383  if ( isEmptyTabbarSpace( e->pos() ) ) {
384  emit( mouseMiddleClick() );
385  return;
386  }
387  }
388  TQTabWidget::mousePressEvent( e );
389 }
390 
391 void KTabWidget::receivedDropEvent( int index, TQDropEvent *e )
392 {
393  emit( receivedDropEvent( page( index ), e ) );
394 }
395 
396 void KTabWidget::initiateDrag( int index )
397 {
398  emit( initiateDrag( page( index ) ) );
399 }
400 
401 void KTabWidget::contextMenu( int index, const TQPoint &p )
402 {
403  emit( contextMenu( page( index ), p ) );
404 }
405 
406 void KTabWidget::mouseDoubleClick( int index )
407 {
408  emit( mouseDoubleClick( page( index ) ) );
409 }
410 
411 void KTabWidget::mouseMiddleClick( int index )
412 {
413  emit( mouseMiddleClick( page( index ) ) );
414 }
415 
416 void KTabWidget::moveTab( int from, int to )
417 {
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 );
425  blockSignals(true);
426  removePage( w );
427 
428  // Work-around tdemdi brain damage which calls showPage() in insertTab()
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 );
435  else
436  d->m_tabNames.insert( d->m_tabNames.at( to ), TQString::null );
437  }
438 
439  w = page( to );
440  changeTab( w, tabiconset, tablabel );
441  setTabToolTip( w, tabtooltip );
442  setTabColor( w, color );
443  if ( current )
444  showPage( w );
445  setTabEnabled( w, enabled );
446  blockSignals(false);
447 
448  emit ( movedTab( from, to ) );
449 }
450 
451 void KTabWidget::removePage( TQWidget * w ) {
452  if ( d->m_automaticResizeTabs ) {
453  int index = indexOf( w );
454  if ( index != -1 )
455  d->m_tabNames.remove( d->m_tabNames.at( index ) );
456  }
457  TQTabWidget::removePage( w );
458  if ( d->m_automaticResizeTabs )
459  resizeTabs();
460 }
461 
462 
463 bool KTabWidget::isEmptyTabbarSpace( const TQPoint &point ) const
464 {
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 );
468  if ( rightcorner ) {
469  if ( point.x()>=width()-rightcorner->width() )
470  return false;
471  }
472  TQWidget *leftcorner = cornerWidget( TopLeft );
473  if ( leftcorner ) {
474  if ( point.x()<=leftcorner->width() )
475  return false;
476  }
477  TQTab *tab = tabBar()->selectTab( tabBar()->mapFromParent( point ) );
478  if( !tab )
479  return true;
480  }
481  return false;
482 }
483 
484 void KTabWidget::setHoverCloseButton( bool button )
485 {
486  static_cast<KTabBar*>(tabBar())->setHoverCloseButton( button );
487 }
488 
489 bool KTabWidget::hoverCloseButton() const
490 {
491  return static_cast<KTabBar*>(tabBar())->hoverCloseButton();
492 }
493 
494 void KTabWidget::setHoverCloseButtonDelayed( bool delayed )
495 {
496  static_cast<KTabBar*>(tabBar())->setHoverCloseButtonDelayed( delayed );
497 }
498 
499 bool KTabWidget::hoverCloseButtonDelayed() const
500 {
501  return static_cast<KTabBar*>(tabBar())->hoverCloseButtonDelayed();
502 }
503 
504 void KTabWidget::setAutomaticResizeTabs( bool enabled )
505 {
506  if ( d->m_automaticResizeTabs==enabled )
507  return;
508 
509  d->m_automaticResizeTabs = enabled;
510  if ( enabled ) {
511  d->m_tabNames.clear();
512  for( int i = 0; i < count(); ++i )
513  d->m_tabNames.append( tabBar()->tabAt( i )->text() );
514  }
515  else
516  for( int i = 0; i < count(); ++i )
517  tabBar()->tabAt( i )->setText( d->m_tabNames[ i ] );
518  resizeTabs();
519 }
520 
521 bool KTabWidget::automaticResizeTabs() const
522 {
523  return d->m_automaticResizeTabs;
524 }
525 
526 void KTabWidget::closeRequest( int index )
527 {
528  emit( closeRequest( page( index ) ) );
529 }
530 
531 void KTabWidget::resizeEvent( TQResizeEvent *e )
532 {
533  TQTabWidget::resizeEvent( e );
534  resizeTabs();
535 }
536 
537 #include "ktabwidget.moc"
KTabWidget::mouseDoubleClick
void mouseDoubleClick()
KStringHandler::rsqueeze
static TQString rsqueeze(const TQString &str, uint maxlen=40)
KTabWidget::insertTab
virtual void insertTab(TQWidget *, const TQString &, int index=-1)
Definition: ktabwidget.cpp:84
KTabWidget::tabLabel
TQString tabLabel(TQWidget *) const
Definition: ktabwidget.cpp:233
KTabWidget::setTabLabel
void setTabLabel(TQWidget *, const TQString &)
Definition: ktabwidget.cpp:246
KTabWidget::closeRequest
void closeRequest(TQWidget *)
TDEConfigGroupSaver
KTabWidget::hoverCloseButtonDelayed
bool hoverCloseButtonDelayed() const
Definition: ktabwidget.cpp:499
KTabWidget::initiateDrag
void initiateDrag(TQWidget *)
KTabWidget::contextMenu
void contextMenu(const TQPoint &)
KTabWidget::isTabReorderingEnabled
bool isTabReorderingEnabled() const
Definition: ktabwidget.cpp:158
KTabWidget::isTabBarHidden
bool isTabBarHidden() const
Definition: ktabwidget.cpp:125
KTabWidget::setTabCloseActivatePrevious
void setTabCloseActivatePrevious(bool previous)
Definition: ktabwidget.cpp:163
KTabWidget::mouseMiddleClick
void mouseMiddleClick()
KTabWidget::tabColor
TQColor tabColor(TQWidget *) const
Definition: ktabwidget.cpp:143
KTabWidget::hoverCloseButton
bool hoverCloseButton() const
Definition: ktabwidget.cpp:489
KTabWidget::~KTabWidget
virtual ~KTabWidget()
Destructor.
Definition: ktabwidget.cpp:79
KTabWidget::setTabReorderingEnabled
void setTabReorderingEnabled(bool enable)
Definition: ktabwidget.cpp:153
KTabWidget::setTabBarHidden
void setTabBarHidden(bool hide)
Definition: ktabwidget.cpp:109
KTabWidget::setMouseWheelScroll
void setMouseWheelScroll(bool mouseWheelScroll)
Definition: ktabwidget.cpp:130
KTabBar
Definition: ktabbar.h:35
KTabWidget::label
TQString label(int) const
Definition: ktabwidget.cpp:221
KTabWidget::setHoverCloseButton
void setHoverCloseButton(bool enable)
Definition: ktabwidget.cpp:484
KTabWidget::tabCloseActivatePrevious
bool tabCloseActivatePrevious() const
Definition: ktabwidget.cpp:168
KTabWidget::moveTab
virtual void moveTab(int, int)
Definition: ktabwidget.cpp:416
KTabWidget::setHoverCloseButtonDelayed
void setHoverCloseButtonDelayed(bool delayed)
Definition: ktabwidget.cpp:494
KTabWidget::changeTab
void changeTab(TQWidget *, const TQString &)
Definition: ktabwidget.cpp:197
KTabWidget::setAutomaticResizeTabs
void setAutomaticResizeTabs(bool enable)
Definition: ktabwidget.cpp:504
TDEGlobal::config
static TDEConfig * config()
KTabWidget::removePage
virtual void removePage(TQWidget *w)
Definition: ktabwidget.cpp:451
KTabWidget::receivedDropEvent
void receivedDropEvent(TQDropEvent *)
KTabWidget::setTabColor
void setTabColor(TQWidget *, const TQColor &color)
Definition: ktabwidget.cpp:135
KTabWidget::automaticResizeTabs
bool automaticResizeTabs() const
Definition: ktabwidget.cpp:521
TDEConfigBase::readNumEntry
int readNumEntry(const TQString &pKey, int nDefault=0) const

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.8.11
This website is maintained by Timothy Pearson.