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

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