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

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
  •     tdecore
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  • tdeioslave
  •   http
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeui by doxygen 1.8.8
This website is maintained by Timothy Pearson.