1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
|
/* This file is part of the KDE libraries
Copyright (C) 2003 Stephan Binner <binner@kde.org>
Copyright (C) 2003 Zack Rusin <zack@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include <qapplication.h>
#include <qstyle.h>
#include <qstylesheet.h>
#include <kconfig.h>
#include <kiconloader.h>
#include <kstringhandler.h>
#include "ktabwidget.h"
#include "ktabbar.h"
class KTabWidgetPrivate {
public:
bool m_automaticResizeTabs;
int m_maxLength;
int m_minLength;
unsigned int m_CurrentMaxLength;
//holds the full names of the tab, otherwise all we
//know about is the shortened name
QStringList m_tabNames;
KTabWidgetPrivate() {
m_automaticResizeTabs = false;
KConfigGroupSaver groupsaver(KGlobal::config(), "General");
m_maxLength = KGlobal::config()->readNumEntry("MaximumTabLength", 30);
m_minLength = KGlobal::config()->readNumEntry("MinimumTabLength", 3);
m_CurrentMaxLength = m_minLength;
}
};
KTabWidget::KTabWidget( QWidget *parent, const char *name, WFlags f )
: QTabWidget( parent, name, f )
{
d = new KTabWidgetPrivate;
setTabBar( new KTabBar(this, "tabbar") );
setAcceptDrops( true );
setHoverCloseButtonDelayed(false);
connect(tabBar(), SIGNAL(contextMenu( int, const QPoint & )), SLOT(contextMenu( int, const QPoint & )));
connect(tabBar(), SIGNAL(mouseDoubleClick( int )), SLOT(mouseDoubleClick( int )));
connect(tabBar(), SIGNAL(mouseMiddleClick( int )), SLOT(mouseMiddleClick( int )));
connect(tabBar(), SIGNAL(initiateDrag( int )), SLOT(initiateDrag( int )));
connect(tabBar(), SIGNAL(testCanDecode(const QDragMoveEvent *, bool & )), SIGNAL(testCanDecode(const QDragMoveEvent *, bool & )));
connect(tabBar(), SIGNAL(receivedDropEvent( int, QDropEvent * )), SLOT(receivedDropEvent( int, QDropEvent * )));
connect(tabBar(), SIGNAL(moveTab( int, int )), SLOT(moveTab( int, int )));
connect(tabBar(), SIGNAL(closeRequest( int )), SLOT(closeRequest( int )));
#ifndef QT_NO_WHEELEVENT
connect(tabBar(), SIGNAL(wheelDelta( int )), SLOT(wheelDelta( int )));
#endif
}
KTabWidget::~KTabWidget()
{
delete d;
}
void KTabWidget::insertTab( QWidget *child, const QString &label, int index )
{
QTabWidget::insertTab( child, label, index );
}
void KTabWidget::insertTab( QWidget *child, const QIconSet& iconset, const QString &label, int index )
{
QTabWidget::insertTab( child, iconset, label, index );
}
void KTabWidget::insertTab( QWidget *child, QTab *tab, int index )
{
QTabWidget::insertTab( child, tab, index);
if ( d->m_automaticResizeTabs ) {
if ( index < 0 || index >= count() ) {
d->m_tabNames.append( tab->text() );
resizeTabs( d->m_tabNames.count()-1 );
}
else {
d->m_tabNames.insert( d->m_tabNames.at( index ), tab->text() );
resizeTabs( index );
}
}
}
void KTabWidget::setTabBarHidden( bool hide )
{
QWidget *rightcorner = this->cornerWidget( TopRight );
QWidget *leftcorner = this->cornerWidget( TopLeft );
if ( hide ) {
if ( leftcorner ) leftcorner->hide();
if ( rightcorner ) rightcorner->hide();
tabBar()->hide();
} else {
tabBar()->show();
if ( leftcorner ) leftcorner->show();
if ( rightcorner ) rightcorner->show();
}
}
bool KTabWidget::isTabBarHidden() const
{
return !( tabBar()->isVisible() );
}
void KTabWidget::setTabColor( QWidget *w, const QColor& color )
{
QTab *t = tabBar()->tabAt( indexOf( w ) );
if (t) {
static_cast<KTabBar*>(tabBar())->setTabColor( t->identifier(), color );
}
}
QColor KTabWidget::tabColor( QWidget *w ) const
{
QTab *t = tabBar()->tabAt( indexOf( w ) );
if (t) {
return static_cast<KTabBar*>(tabBar())->tabColor( t->identifier() );
} else {
return QColor();
}
}
void KTabWidget::setTabReorderingEnabled( bool on)
{
static_cast<KTabBar*>(tabBar())->setTabReorderingEnabled( on );
}
bool KTabWidget::isTabReorderingEnabled() const
{
return static_cast<KTabBar*>(tabBar())->isTabReorderingEnabled();
}
void KTabWidget::setTabCloseActivatePrevious( bool previous)
{
static_cast<KTabBar*>(tabBar())->setTabCloseActivatePrevious( previous );
}
bool KTabWidget::tabCloseActivatePrevious() const
{
return static_cast<KTabBar*>(tabBar())->tabCloseActivatePrevious();
}
unsigned int KTabWidget::tabBarWidthForMaxChars( uint maxLength )
{
int hframe, overlap;
hframe = tabBar()->style().pixelMetric( QStyle::PM_TabBarTabHSpace, tabBar() );
overlap = tabBar()->style().pixelMetric( QStyle::PM_TabBarTabOverlap, tabBar() );
QFontMetrics fm = tabBar()->fontMetrics();
int x = 0;
for( int i=0; i < count(); ++i ) {
QString newTitle = d->m_tabNames[ i ];
newTitle = KStringHandler::rsqueeze( newTitle, maxLength ).leftJustify( d->m_minLength, ' ' );
QTab* tab = tabBar()->tabAt( i );
int lw = fm.width( newTitle );
int iw = 0;
if ( tab->iconSet() )
iw = tab->iconSet()->pixmap( QIconSet::Small, QIconSet::Normal ).width() + 4;
x += ( tabBar()->style().sizeFromContents( QStyle::CT_TabBarTab, this,
QSize( QMAX( lw + hframe + iw, QApplication::globalStrut().width() ), 0 ),
QStyleOption( tab ) ) ).width();
}
return x;
}
void KTabWidget::changeTab( QWidget *w, const QString &label )
{
QTabWidget::changeTab( w, label );
if ( d->m_automaticResizeTabs ) {
int index = indexOf( w );
if ( index != -1 ) {
d->m_tabNames[ index ] = label;
resizeTabs( index );
}
}
}
void KTabWidget::changeTab( QWidget *w, const QIconSet &iconset, const QString &label )
{
QTabWidget::changeTab( w, iconset, label );
if ( d->m_automaticResizeTabs ) {
int index = indexOf( w );
if ( index != -1 ) {
d->m_tabNames[ index ] = label;
resizeTabs( index );
}
}
}
QString KTabWidget::label( int index ) const
{
if ( d->m_automaticResizeTabs ) {
if ( index >= 0 && index < count() )
return d->m_tabNames[ index ];
else
return QString::null;
}
else
return QTabWidget::label( index );
}
QString KTabWidget::tabLabel( QWidget * w ) const
{
if ( d->m_automaticResizeTabs ) {
int index = indexOf( w );
if ( index == -1 )
return QString::null;
else
return d->m_tabNames[ index ];
}
else
return QTabWidget::tabLabel( w );
}
void KTabWidget::setTabLabel( QWidget *w, const QString &l )
{
QTabWidget::setTabLabel( w, l );
if ( d->m_automaticResizeTabs ) {
int index = indexOf( w );
if ( index != -1 ) {
d->m_tabNames[ index ] = l;
resizeTabs( index );
}
}
}
void KTabWidget::resizeTabs( int changeTabIndex )
{
uint newMaxLength;
if ( d->m_automaticResizeTabs ) {
// Calculate new max length
newMaxLength=d->m_maxLength;
uint lcw=0, rcw=0;
int tabBarHeight = tabBar()->sizeHint().height();
if ( cornerWidget( TopLeft ) && cornerWidget( TopLeft )->isVisible() )
lcw = QMAX( cornerWidget( TopLeft )->width(), tabBarHeight );
if ( cornerWidget( TopRight ) && cornerWidget( TopRight )->isVisible() )
rcw = QMAX( cornerWidget( TopRight )->width(), tabBarHeight );
uint maxTabBarWidth = width() - lcw - rcw;
for ( ; newMaxLength > (uint)d->m_minLength; newMaxLength-- ) {
if ( tabBarWidthForMaxChars( newMaxLength ) < maxTabBarWidth )
break;
}
}
else
newMaxLength = 4711;
// Update hinted or all tabs
if ( d->m_CurrentMaxLength != newMaxLength ) {
d->m_CurrentMaxLength = newMaxLength;
for( int i = 0; i < count(); ++i )
updateTab( i );
}
else if ( changeTabIndex != -1 )
updateTab( changeTabIndex );
}
void KTabWidget::updateTab( int index )
{
QString title = d->m_automaticResizeTabs ? d->m_tabNames[ index ] : QTabWidget::label( index );
removeTabToolTip( page( index ) );
if ( title.length() > d->m_CurrentMaxLength ) {
if ( QStyleSheet::mightBeRichText( title ) )
setTabToolTip( page( index ), QStyleSheet::escape(title) );
else
setTabToolTip( page( index ), title );
}
title = KStringHandler::rsqueeze( title, d->m_CurrentMaxLength ).leftJustify( d->m_minLength, ' ' );
title.replace( '&', "&&" );
if ( QTabWidget::label( index ) != title )
QTabWidget::setTabLabel( page( index ), title );
}
void KTabWidget::dragMoveEvent( QDragMoveEvent *e )
{
if ( isEmptyTabbarSpace( e->pos() ) ) {
bool accept = false;
// The receivers of the testCanDecode() signal has to adjust
// 'accept' accordingly.
emit testCanDecode( e, accept);
e->accept( accept );
return;
}
e->accept( false );
QTabWidget::dragMoveEvent( e );
}
void KTabWidget::dropEvent( QDropEvent *e )
{
if ( isEmptyTabbarSpace( e->pos() ) ) {
emit ( receivedDropEvent( e ) );
return;
}
QTabWidget::dropEvent( e );
}
#ifndef QT_NO_WHEELEVENT
void KTabWidget::wheelEvent( QWheelEvent *e )
{
if ( e->orientation() == Horizontal )
return;
if ( isEmptyTabbarSpace( e->pos() ) )
wheelDelta( e->delta() );
else
e->ignore();
}
void KTabWidget::wheelDelta( int delta )
{
if ( count() < 2 )
return;
int page = currentPageIndex();
if ( delta < 0 )
page = (page + 1) % count();
else {
page--;
if ( page < 0 )
page = count() - 1;
}
setCurrentPage( page );
}
#endif
void KTabWidget::mouseDoubleClickEvent( QMouseEvent *e )
{
if( e->button() != LeftButton )
return;
if ( isEmptyTabbarSpace( e->pos() ) ) {
emit( mouseDoubleClick() );
return;
}
QTabWidget::mouseDoubleClickEvent( e );
}
void KTabWidget::mousePressEvent( QMouseEvent *e )
{
if ( e->button() == RightButton ) {
if ( isEmptyTabbarSpace( e->pos() ) ) {
emit( contextMenu( mapToGlobal( e->pos() ) ) );
return;
}
} else if ( e->button() == MidButton ) {
if ( isEmptyTabbarSpace( e->pos() ) ) {
emit( mouseMiddleClick() );
return;
}
}
QTabWidget::mousePressEvent( e );
}
void KTabWidget::receivedDropEvent( int index, QDropEvent *e )
{
emit( receivedDropEvent( page( index ), e ) );
}
void KTabWidget::initiateDrag( int index )
{
emit( initiateDrag( page( index ) ) );
}
void KTabWidget::contextMenu( int index, const QPoint &p )
{
emit( contextMenu( page( index ), p ) );
}
void KTabWidget::mouseDoubleClick( int index )
{
emit( mouseDoubleClick( page( index ) ) );
}
void KTabWidget::mouseMiddleClick( int index )
{
emit( mouseMiddleClick( page( index ) ) );
}
void KTabWidget::moveTab( int from, int to )
{
QString tablabel = label( from );
QWidget *w = page( from );
QColor color = tabColor( w );
QIconSet tabiconset = tabIconSet( w );
QString tabtooltip = tabToolTip( w );
bool current = ( w == currentPage() );
bool enabled = isTabEnabled( w );
blockSignals(true);
removePage( w );
// Work-around kmdi brain damage which calls showPage() in insertTab()
QTab * t = new QTab();
t->setText(tablabel);
QTabWidget::insertTab( w, t, to );
if ( d->m_automaticResizeTabs ) {
if ( to < 0 || to >= count() )
d->m_tabNames.append( QString::null );
else
d->m_tabNames.insert( d->m_tabNames.at( to ), QString::null );
}
w = page( to );
changeTab( w, tabiconset, tablabel );
setTabToolTip( w, tabtooltip );
setTabColor( w, color );
if ( current )
showPage( w );
setTabEnabled( w, enabled );
blockSignals(false);
emit ( movedTab( from, to ) );
}
void KTabWidget::removePage( QWidget * w ) {
if ( d->m_automaticResizeTabs ) {
int index = indexOf( w );
if ( index != -1 )
d->m_tabNames.remove( d->m_tabNames.at( index ) );
}
QTabWidget::removePage( w );
if ( d->m_automaticResizeTabs )
resizeTabs();
}
bool KTabWidget::isEmptyTabbarSpace( const QPoint &point ) const
{
QSize size( tabBar()->sizeHint() );
if ( ( tabPosition()==Top && point.y()< size.height() ) || ( tabPosition()==Bottom && point.y()>(height()-size.height() ) ) ) {
QWidget *rightcorner = cornerWidget( TopRight );
if ( rightcorner ) {
if ( point.x()>=width()-rightcorner->width() )
return false;
}
QWidget *leftcorner = cornerWidget( TopLeft );
if ( leftcorner ) {
if ( point.x()<=leftcorner->width() )
return false;
}
QTab *tab = tabBar()->selectTab( tabBar()->mapFromParent( point ) );
if( !tab )
return true;
}
return false;
}
void KTabWidget::setHoverCloseButton( bool button )
{
static_cast<KTabBar*>(tabBar())->setHoverCloseButton( button );
}
bool KTabWidget::hoverCloseButton() const
{
return static_cast<KTabBar*>(tabBar())->hoverCloseButton();
}
void KTabWidget::setHoverCloseButtonDelayed( bool delayed )
{
static_cast<KTabBar*>(tabBar())->setHoverCloseButtonDelayed( delayed );
}
bool KTabWidget::hoverCloseButtonDelayed() const
{
return static_cast<KTabBar*>(tabBar())->hoverCloseButtonDelayed();
}
void KTabWidget::setAutomaticResizeTabs( bool enabled )
{
if ( d->m_automaticResizeTabs==enabled )
return;
d->m_automaticResizeTabs = enabled;
if ( enabled ) {
d->m_tabNames.clear();
for( int i = 0; i < count(); ++i )
d->m_tabNames.append( tabBar()->tabAt( i )->text() );
}
else
for( int i = 0; i < count(); ++i )
tabBar()->tabAt( i )->setText( d->m_tabNames[ i ] );
resizeTabs();
}
bool KTabWidget::automaticResizeTabs() const
{
return d->m_automaticResizeTabs;
}
void KTabWidget::closeRequest( int index )
{
emit( closeRequest( page( index ) ) );
}
void KTabWidget::resizeEvent( QResizeEvent *e )
{
QTabWidget::resizeEvent( e );
resizeTabs();
}
#include "ktabwidget.moc"
|