ATLAS Offline Software
Loading...
Searching...
No Matches
VP1TabWidget.cxx
Go to the documentation of this file.
1
2// //
3// File adopted from KDE4 libraries by //
4// T. Kittelmann <Thomas.Kittelmann@cern.ch>, March 2007. //
5// //
6// Main thing is to remove dependence on KDE for length of title text //
7// settings, delay on drag settings and title eliding. Also, //
8// hoverbuttons were removed (since these had not been properly //
9// implemented in KDE4 at the time the code was copied). //
10// //
11// Notice about Copyrights and GPL license from the orignal file is //
12// left untouched below. //
13// //
15
16
17/* This file is part of the KDE libraries
18 Copyright (C) 2003, 2021 Stephan Binner <binner@kde.org>
19 Copyright (C) 2003 Zack Rusin <zack@kde.org>
20
21 This library is free software; you can redistribute it and/or
22 modify it under the terms of the GNU Library General Public
23 License as published by the Free Software Foundation; either
24 version 2 of the License, or (at your option) any later version.
25
26 This library is distributed in the hope that it will be useful,
27 but WITHOUT ANY WARRANTY; without even the implied warranty of
28 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29 Library General Public License for more details.
30
31 You should have received a copy of the GNU Library General Public License
32 along with this library; see the file COPYING.LIB. If not, write to
33 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
34 Boston, MA 02110-1301, USA.
35*/
36
37#include "VP1Base/VP1TabBar.h"
39
40#include <QApplication>
41#include <QDragMoveEvent>
42#include <QStyle>
43#include <QTextDocument>
44#include <QtGuiVersion>
45
46
47
49{
50 public:
52 : m_parent( parent ),
54 {
55 m_maxLength=30;m_minLength=3;//TK-fixme
57 }
58
64
65 //holds the full names of the tab, otherwise all we
66 //know about is the shortened name
67 QStringList m_tabNames;
68
69 bool isEmptyTabbarSpace( const QPoint & ) const;
70 void resizeTabs( int changedTabIndex = -1 );
71 void updateTab( int index );
72};
73
74//Added by TK:
76 return static_cast<VP1TabBar*>(tabBar());
77}
78
79bool VP1TabWidget::Private::isEmptyTabbarSpace( const QPoint &point ) const
80{
81 QSize size( m_parent->tabBar()->sizeHint() );
82 if ( ( m_parent->tabPosition() == QTabWidget::North && point.y() < size.height() ) ||
83 ( m_parent->tabPosition() == QTabWidget::South && point.y() > (m_parent->height() - size.height() ) ) ) {
84
85 QWidget *rightcorner = m_parent->cornerWidget( Qt::TopRightCorner );
86 if ( rightcorner ) {
87 if ( point.x() >= m_parent->width()-rightcorner->width() )
88 return false;
89 }
90
91 QWidget *leftcorner = m_parent->cornerWidget( Qt::TopLeftCorner );
92 if ( leftcorner ) {
93 if ( point.x() <= leftcorner->width() )
94 return false;
95 }
96
97 for ( int i = 0; i < m_parent->count(); ++i )
98 if ( m_parent->tabBar()->tabRect( i ).contains( m_parent->tabBar()->mapFromParent( point ) ) )
99 return false;
100
101 return true;
102 }
103
104 return false;
105}
106
107void VP1TabWidget::Private::resizeTabs( int changeTabIndex )
108{
109 int newMaxLength;
110 if ( m_automaticResizeTabs ) {
111 // Calculate new max length
112 newMaxLength = m_maxLength;
113 int lcw = 0, rcw = 0;
114
115 int tabBarHeight = m_parent->tabBar()->sizeHint().height();
116 if ( m_parent->cornerWidget( Qt::TopLeftCorner ) && m_parent->cornerWidget( Qt::TopLeftCorner )->isVisible() )
117 lcw = qMax( m_parent->cornerWidget( Qt::TopLeftCorner )->width(), tabBarHeight );
118
119 if ( m_parent->cornerWidget( Qt::TopRightCorner ) && m_parent->cornerWidget( Qt::TopRightCorner )->isVisible() )
120 rcw = qMax( m_parent->cornerWidget( Qt::TopRightCorner )->width(), tabBarHeight );
121
122 int maxTabBarWidth = m_parent->width() - lcw - rcw;
123
124 for ( ; newMaxLength > m_minLength; newMaxLength-- ) {
125 if ( m_parent->tabBarWidthForMaxChars( newMaxLength ) < maxTabBarWidth )
126 break;
127 }
128 } else
129 newMaxLength = 4711;
130
131 // Update hinted or all tabs
132 if ( m_currentMaxLength != newMaxLength ) {
133 m_currentMaxLength = newMaxLength;
134 for ( int i = 0; i < m_parent->count(); ++i )
135 updateTab( i );
136 } else if ( changeTabIndex != -1 )
137 updateTab( changeTabIndex );
138}
139
141{
142 QString title = m_automaticResizeTabs ? m_tabNames[ index ] : m_parent->QTabWidget::tabText( index );
143 m_parent->setTabToolTip( index, QString() );
144
145 if ( title.length() > m_currentMaxLength ) {
146 if ( Qt::mightBeRichText( title ) )
147 // m_parent->setTabToolTip( index, Qt::escape( title ) ); // old Qt code
148 m_parent->setTabToolTip( index, QString(title).toHtmlEscaped() );
149 else
150 m_parent->setTabToolTip( index, title );
151 }
152
153 //TK-fixme. Just use elide here?
154 //TK-fixme title = KStringHandler::rsqueeze( title, m_currentMaxLength ).leftJustified( m_minLength, ' ' );
155 title.replace( '&', "&&" );
156
157 if ( m_parent->QTabWidget::tabText( index ) != title )
158 m_parent->QTabWidget::setTabText( index, title );
159}
160
161// VP1TabWidget::VP1TabWidget( QWidget *parent, Qt::WFlags flags ) // old Qt code
162VP1TabWidget::VP1TabWidget( QWidget *parent, Qt::WindowFlags flags )
163 : QTabWidget( parent ),
164 m_d( new Private( this ) )
165{
166 setWindowFlags( flags );
167 setTabBar( new VP1TabBar( this ) );
168 setObjectName( "tabbar" );
169 setAcceptDrops( true );
170
171 connect(tabBar(), SIGNAL(contextMenu( int, const QPoint & )), SLOT(contextMenu( int, const QPoint & )));
172 connect(tabBar(), SIGNAL(mouseDoubleClick( int )), SLOT(mouseDoubleClick( int )));
173 connect(tabBar(), SIGNAL(mouseMiddleClick( int )), SLOT(mouseMiddleClick( int )));
174 connect(tabBar(), SIGNAL(initiateDrag( int )), SLOT(initiateDrag( int )));
175 connect(tabBar(), SIGNAL(testCanDecode(const QDragMoveEvent *, bool & )), SIGNAL(testCanDecode(const QDragMoveEvent *, bool & )));
176 connect(tabBar(), SIGNAL(receivedDropEvent( int, QDropEvent * )), SLOT(receivedDropEvent( int, QDropEvent * )));
178 connect(tabBar(), SIGNAL(closeRequest( int )), SLOT(closeRequest( int )));
179#ifndef QT_NO_WHEELEVENT
181#endif
182}
183
185{
186 delete m_d;
187}
188
189/*void VP1TabWidget::insertTab( QWidget *child, const QString &label, int index )
190{
191 QTabWidget::insertTab( child, label, index );
192}
193
194void VP1TabWidget::insertTab( QWidget *child, const QIcon& iconset, const QString &label, int index )
195{
196 QTabWidget::insertTab( child, iconset, label, index );
197}
198
199void VP1TabWidget::insertTab( QWidget *child, QTab *tab, int index )
200{
201 QTabWidget::insertTab( child, tab, index);
202 if ( m_d->m_automaticResizeTabs ) {
203 if ( index < 0 || index >= count() ) {
204 m_d->m_tabNames.append( tab->text() );
205 m_d->resizeTabs( m_d->m_tabNames.count()-1 );
206 }
207 else {
208 m_d->m_tabNames.insert( m_d->m_tabNames.at( index ), tab->text() );
209 m_d->resizeTabs( index );
210 }
211 }
212}*/
213
215{
216 QWidget *rightcorner = cornerWidget( Qt::TopRightCorner );
217 QWidget *leftcorner = cornerWidget( Qt::TopLeftCorner );
218
219 if ( hide ) {
220 if ( leftcorner ) leftcorner->hide();
221 if ( rightcorner ) rightcorner->hide();
222 tabBar()->hide();
223 } else {
224 tabBar()->show();
225 if ( leftcorner ) leftcorner->show();
226 if ( rightcorner ) rightcorner->show();
227 }
228}
229
231{
232 return !( tabBar()->isVisible() );
233}
234
235void VP1TabWidget::setTabTextColor( int index, const QColor& color )
236{
237 tabBar()->setTabTextColor( index, color );
238}
239
241{
242 return tabBar()->tabTextColor( index );
243}
244
246{
247 static_cast<VP1TabBar*>(tabBar())->setTabReorderingEnabled( on );
248}
249
251{
252 return static_cast<VP1TabBar*>(tabBar())->isTabReorderingEnabled();
253}
254
256{
257 static_cast<VP1TabBar*>(tabBar())->setTabCloseActivatePrevious( previous );
258}
259
261{
262 return static_cast<VP1TabBar*>(tabBar())->tabCloseActivatePrevious();
263}
264
266{
267 int hframe /*, overlap*/;
268 hframe = tabBar()->style()->pixelMetric( QStyle::PM_TabBarTabHSpace, 0L, tabBar() );
269 //overlap = tabBar()->style()->pixelMetric( QStyle::PM_TabBarTabOverlap, 0L, tabBar() );
270
271 QFontMetrics fm = tabBar()->fontMetrics();
272 int x = 0;
273 for ( int i = 0; i < count(); ++i ) {
274 QString newTitle = m_d->m_tabNames[ i ];
275 //TK-fixme. Just use elide here?
276 //TK-fixme newTitle = KStringHandler::rsqueeze( newTitle, maxLength ).leftJustified( m_d->m_minLength, ' ' );
277
278 int lw = fm.horizontalAdvance( newTitle );
279 int iw = 0;
280 if ( !tabBar()->tabIcon( i ).isNull() ){
281 iw = tabBar()->tabIcon( i ).pixmap( style()->pixelMetric( QStyle::PM_SmallIconSize ), QIcon::Normal ).width() + 4;
282 }
283 x += ( tabBar()->style()->sizeFromContents( QStyle::CT_TabBarTab, 0L,
284 QSize( qMax( lw + hframe + iw, QApplication::globalStrut().width() ), 0 ),
285 this ) ).width();
286 }
287
288 return x;
289}
290
291QString VP1TabWidget::tabText( int index ) const
292{
293 if ( m_d->m_automaticResizeTabs ) {
294 if ( index >= 0 && index < count() )
295 return m_d->m_tabNames[ index ];
296 else
297 return QString();
298 }
299 else
300 return QTabWidget::tabText( index );
301}
302
303void VP1TabWidget::setTabText( int index, const QString &text )
304{
305 QTabWidget::setTabText( index, text );
306 if ( m_d->m_automaticResizeTabs ) {
307 if ( index != -1 ) {
308 m_d->m_tabNames[ index ] = text;
309 m_d->resizeTabs( index );
310 }
311 }
312}
313
314
315void VP1TabWidget::dragEnterEvent( QDragEnterEvent *event )
316{
317 event->setAccepted( true );
318 QTabWidget::dragEnterEvent( event );
319}
320
321void VP1TabWidget::dragMoveEvent( QDragMoveEvent *event )
322{
323 if ( m_d->isEmptyTabbarSpace( event->pos() ) ) {
324 bool accept = false;
325 // The receivers of the testCanDecode() signal has to adjust
326 // 'accept' accordingly.
327 emit testCanDecode( event, accept);
328
329 event->setAccepted( accept );
330 return;
331 }
332
333 event->setAccepted( false );
334 QTabWidget::dragMoveEvent( event );
335}
336
337void VP1TabWidget::dropEvent( QDropEvent *event )
338{
339 if ( m_d->isEmptyTabbarSpace( event->pos() ) ) {
340 emit ( receivedDropEvent( event ) );
341 return;
342 }
343
344 QTabWidget::dropEvent( event );
345}
346
347#ifndef QT_NO_WHEELEVENT
348void VP1TabWidget::wheelEvent( QWheelEvent *event )
349{
350#if QTGUI_VERSION >= 0x050E00
351 if ( m_d->isEmptyTabbarSpace( event->position().toPoint() ) )
352#else
353 if ( m_d->isEmptyTabbarSpace( event->pos() ) )
354#endif
355 wheelDelta( event->angleDelta().y() );
356 else
357 event->ignore();
358}
359
361{
362 if ( count() < 2 )
363 return;
364
365 int page = currentIndex();
366 if ( delta < 0 )
367 page = (page + 1) % count();
368 else {
369 page--;
370 if ( page < 0 )
371 page = count() - 1;
372 }
373 setCurrentIndex( page );
374}
375#endif
376
377void VP1TabWidget::mouseDoubleClickEvent( QMouseEvent *event )
378{
379 if ( event->button() != Qt::LeftButton )
380 return;
381
382 if ( m_d->isEmptyTabbarSpace( event->pos() ) ) {
383 emit( mouseDoubleClick() );
384 return;
385 }
386
387 QTabWidget::mouseDoubleClickEvent( event );
388}
389
390void VP1TabWidget::mousePressEvent( QMouseEvent *event )
391{
392 if ( event->button() == Qt::RightButton ) {
393 if ( m_d->isEmptyTabbarSpace( event->pos() ) ) {
394 emit( contextMenu( mapToGlobal( event->pos() ) ) );
395 return;
396 }
397 } else if ( event->button() == Qt::MiddleButton ) {
398 if ( m_d->isEmptyTabbarSpace( event->pos() ) ) {
399 emit( mouseMiddleClick() );
400 return;
401 }
402 }
403
404 QTabWidget::mousePressEvent( event );
405}
406
407void VP1TabWidget::receivedDropEvent( int index, QDropEvent *event )
408{
409 emit( receivedDropEvent( widget( index ), event ) );
410}
411
413{
414 emit( initiateDrag( widget( index ) ) );
415}
416
417void VP1TabWidget::contextMenu( int index, const QPoint &point )
418{
419 emit( contextMenu( widget( index ), point ) );
420}
421
423{
424 emit( mouseDoubleClick( widget( index ) ) );
425}
426
428{
429 emit( mouseMiddleClick( widget( index ) ) );
430}
431
432void VP1TabWidget::moveTab( int from, int to )
433{
434 QString tablabel = tabText( from );
435 QWidget *w = widget( from );
436 QColor color = tabTextColor( from );
437 QIcon tabiconset = tabIcon( from );
438 QString tabtooltip = tabToolTip( from );
439 bool current = ( from == currentIndex() );
440 bool enabled = isTabEnabled( from );
441
442 bool blocked = blockSignals( true );
443 removeTab( from );
444
445 // Work-around kmdi brain damage which calls showPage() in insertTab()
446 insertTab( to, w, tablabel );
447 if ( m_d->m_automaticResizeTabs ) {
448 if ( to < 0 || to >= count() )
449 m_d->m_tabNames.append( QString() );
450 else
451 m_d->m_tabNames.insert( to, QString() );
452 }
453
454 setTabIcon( to, tabiconset );
455 setTabText( to, tablabel );
456 setTabToolTip( to, tabtooltip );
457 setTabTextColor( to, color );
458 if ( current )
459 setCurrentIndex( to );
460 setTabEnabled( to, enabled );
461 blockSignals( blocked );
462
463 emit ( movedTab( from, to ) );
464}
465
466void VP1TabWidget::removePage( QWidget *widget )
467{
468 QTabWidget::removeTab( indexOf( widget ) );
469 if ( m_d->m_automaticResizeTabs )
470 m_d->resizeTabs();
471}
472
474{
475 QTabWidget::removeTab( index );
476 if ( m_d->m_automaticResizeTabs )
477 m_d->resizeTabs();
478}
479
481{
482 if ( m_d->m_automaticResizeTabs == enabled )
483 return;
484
485 m_d->m_automaticResizeTabs = enabled;
486 if ( enabled ) {
487 m_d->m_tabNames.clear();
488 for ( int i = 0; i < count(); ++i )
489 m_d->m_tabNames.append( tabBar()->tabText( i ) );
490 } else
491 for ( int i = 0; i < count(); ++i )
492 tabBar()->setTabText( i, m_d->m_tabNames[ i ] );
493
494 m_d->resizeTabs();
495}
496
498{
499 return m_d->m_automaticResizeTabs;
500}
501
503{
504 emit( closeRequest( widget( index ) ) );
505}
506
507void VP1TabWidget::resizeEvent( QResizeEvent *event )
508{
509 QTabWidget::resizeEvent( event );
510 m_d->resizeTabs();
511}
512
514{
515 m_d->m_tabNames.insert( idx, tabBar()->tabText( idx ) );
516}
517
519{
520 m_d->m_tabNames.removeAt( idx );
521}
const double width
#define x
void moveTab(int, int)
void wheelDelta(int)
Private(VP1TabWidget *parent)
bool isEmptyTabbarSpace(const QPoint &) const
void updateTab(int index)
VP1TabWidget * m_parent
void resizeTabs(int changedTabIndex=-1)
void receivedDropEvent(QDropEvent *)
void setTabReorderingEnabled(bool enable)
virtual void tabRemoved(int)
void setTabTextColor(int index, const QColor &color)
void mouseDoubleClick()
virtual QT_MOC_COMPAT void removePage(QWidget *w)
void testCanDecode(const QDragMoveEvent *e, bool &accept)
bool isTabBarHidden() const
void closeRequest(QWidget *)
void mouseMiddleClick()
virtual void tabInserted(int)
virtual ~VP1TabWidget()
virtual void wheelEvent(QWheelEvent *)
int tabBarWidthForMaxChars(int)
void initiateDrag(QWidget *)
virtual void removeTab(int index)
bool automaticResizeTabs
VP1TabWidget(QWidget *parent=0, Qt::WindowFlags flags=Qt::WindowFlags())
Private *const m_d
VP1TabBar * getVP1TabBar()
bool tabCloseActivatePrevious
void movedTab(int, int)
void contextMenu(const QPoint &)
void setTabBarHidden(bool hide)
virtual void dragEnterEvent(QDragEnterEvent *)
bool isTabReorderingEnabled() const
void setTabText(int, const QString &)
virtual void mouseDoubleClickEvent(QMouseEvent *)
virtual void wheelDelta(int)
void setAutomaticResizeTabs(bool enable)
QColor tabTextColor(int index) const
virtual void resizeEvent(QResizeEvent *)
virtual void mousePressEvent(QMouseEvent *)
virtual void dropEvent(QDropEvent *)
void setTabCloseActivatePrevious(bool previous)
virtual void moveTab(int, int)
QString tabText(int) const
virtual void dragMoveEvent(QDragMoveEvent *)
int count(std::string s, const std::string &regx)
count how many occurances of a regx are in a string
Definition hcg.cxx:146
Definition index.py:1