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

kdeui

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

kdeui

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

kdeui

Skip menu "kdeui"
  • arts
  • dcop
  • dnssd
  • interfaces
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for kdeui by doxygen 1.8.13
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |