diff options
Diffstat (limited to 'src/widgets/qtabbar.cpp')
-rw-r--r-- | src/widgets/qtabbar.cpp | 237 |
1 files changed, 143 insertions, 94 deletions
diff --git a/src/widgets/qtabbar.cpp b/src/widgets/qtabbar.cpp index 383285d..ab4d33b 100644 --- a/src/widgets/qtabbar.cpp +++ b/src/widgets/qtabbar.cpp @@ -335,7 +335,7 @@ private: */ QTabBar::QTabBar( QWidget * parent, const char *name ) - : QWidget( parent, name, WNoAutoErase | WNoMousePropagation ), l(NULL) + : QWidget( parent, name, WNoAutoErase | WNoMousePropagation ), l(NULL), hoverTab( 0 ) { d = new QTabPrivate; d->pressed = 0; @@ -581,45 +581,49 @@ QSize QTabBar::minimumSizeHint() const void QTabBar::paint( QPainter * p, QTab * t, bool selected ) const { - QStyle::SFlags flags = QStyle::Style_Default; - - if (isEnabled() && t->isEnabled()) - flags |= QStyle::Style_Enabled; - if (topLevelWidget() == qApp->activeWindow()) - flags |= QStyle::Style_Active; - if ( selected ) - flags |= QStyle::Style_Selected; - else if(t == d->pressed) - flags |= QStyle::Style_Sunken; - //selection flags - if(t->rect().contains(mapFromGlobal(QCursor::pos()))) - flags |= QStyle::Style_MouseOver; - style().drawControl( QStyle::CE_TabBarTab, p, this, t->rect(), - colorGroup(), flags, QStyleOption(t) ); - - QRect r( t->r ); - p->setFont( font() ); - - int iw = 0; - int ih = 0; - if ( t->iconset != 0 ) { - iw = t->iconset->pixmap( QIconSet::Small, QIconSet::Normal ).width() + 4; - ih = t->iconset->pixmap( QIconSet::Small, QIconSet::Normal ).height(); - } - QFontMetrics fm = p->fontMetrics(); - int fw = fm.width( t->label ); - fw -= t->label.contains('&') * fm.width('&'); - fw += t->label.contains("&&") * fm.width('&'); - int w = iw + fw + 4; - int h = QMAX(fm.height() + 4, ih ); - int offset = 3; + QStyle::SFlags flags = QStyle::Style_Default; + + if (isEnabled() && t->isEnabled()) { + flags |= QStyle::Style_Enabled; + } + if (topLevelWidget() == qApp->activeWindow()) { + flags |= QStyle::Style_Active; + } + if ( selected ) { + flags |= QStyle::Style_Selected; + } + else if (t == d->pressed) { + flags |= QStyle::Style_Sunken; + } + + //selection flags + if (t->rect().contains(mapFromGlobal(QCursor::pos()))) { + flags |= QStyle::Style_MouseOver; + } + style().drawControl( QStyle::CE_TabBarTab, p, this, t->rect(), colorGroup(), flags, QStyleOption(t, hoverTab) ); + + QRect r( t->r ); + p->setFont( font() ); + + int iw = 0; + int ih = 0; + if ( t->iconset != 0 ) { + iw = t->iconset->pixmap( QIconSet::Small, QIconSet::Normal ).width() + 4; + ih = t->iconset->pixmap( QIconSet::Small, QIconSet::Normal ).height(); + } + QFontMetrics fm = p->fontMetrics(); + int fw = fm.width( t->label ); + fw -= t->label.contains('&') * fm.width('&'); + fw += t->label.contains("&&") * fm.width('&'); + int w = iw + fw + 4; + int h = QMAX(fm.height() + 4, ih ); + int offset = 3; #ifdef Q_WS_MAC - if (::qt_cast<QMacStyle *>(&style())) - offset = 0; + if (::qt_cast<QMacStyle *>(&style())) { + offset = 0; + } #endif - paintLabel( p, QRect( r.left() + (r.width()-w)/2 - offset, - r.top() + (r.height()-h)/2, - w, h ), t, t->id == keyboardFocusTab() ); + paintLabel( p, QRect( r.left() + (r.width()-w)/2 - offset, r.top() + (r.height()-h)/2, w, h ), t, t->id == keyboardFocusTab() ); } /*! @@ -630,43 +634,48 @@ void QTabBar::paint( QPainter * p, QTab * t, bool selected ) const void QTabBar::paintLabel( QPainter* p, const QRect& br, QTab* t, bool has_focus ) const { - QRect r = br; - bool selected = currentTab() == t->id; - if ( t->iconset) { - // the tab has an iconset, draw it in the right mode - QIconSet::Mode mode = (t->enabled && isEnabled()) - ? QIconSet::Normal : QIconSet::Disabled; - if ( mode == QIconSet::Normal && has_focus ) - mode = QIconSet::Active; - QPixmap pixmap = t->iconset->pixmap( QIconSet::Small, mode ); - int pixw = pixmap.width(); - int pixh = pixmap.height(); - r.setLeft( r.left() + pixw + 4 ); - r.setRight( r.right() + 2 ); - - int xoff = 0, yoff = 0; - if(!selected) { - xoff = style().pixelMetric(QStyle::PM_TabBarTabShiftHorizontal, this); - yoff = style().pixelMetric(QStyle::PM_TabBarTabShiftVertical, this); + QRect r = br; + bool selected = currentTab() == t->id; + if ( t->iconset) { + // the tab has an iconset, draw it in the right mode + QIconSet::Mode mode = (t->enabled && isEnabled()) + ? QIconSet::Normal : QIconSet::Disabled; + if ( mode == QIconSet::Normal && has_focus ) + mode = QIconSet::Active; + QPixmap pixmap = t->iconset->pixmap( QIconSet::Small, mode ); + int pixw = pixmap.width(); + int pixh = pixmap.height(); + r.setLeft( r.left() + pixw + 4 ); + r.setRight( r.right() + 2 ); + + int xoff = 0, yoff = 0; + if(!selected) { + xoff = style().pixelMetric(QStyle::PM_TabBarTabShiftHorizontal, this); + yoff = style().pixelMetric(QStyle::PM_TabBarTabShiftVertical, this); + } + p->drawPixmap( br.left() + 2 + xoff, br.center().y()-pixh/2 + yoff, pixmap ); } - p->drawPixmap( br.left() + 2 + xoff, br.center().y()-pixh/2 + yoff, pixmap ); - } - QStyle::SFlags flags = QStyle::Style_Default; - - if (isEnabled() && t->isEnabled()) - flags |= QStyle::Style_Enabled; - if (has_focus) - flags |= QStyle::Style_HasFocus; - if ( selected ) - flags |= QStyle::Style_Selected; - else if(t == d->pressed) - flags |= QStyle::Style_Sunken; - if(t->rect().contains(mapFromGlobal(QCursor::pos()))) - flags |= QStyle::Style_MouseOver; - style().drawControl( QStyle::CE_TabBarLabel, p, this, r, - t->isEnabled() ? colorGroup(): palette().disabled(), - flags, QStyleOption(t) ); + QStyle::SFlags flags = QStyle::Style_Default; + + if (isEnabled() && t->isEnabled()) { + flags |= QStyle::Style_Enabled; + } + if (has_focus) { + flags |= QStyle::Style_HasFocus; + } + if ( selected ) { + flags |= QStyle::Style_Selected; + } + else if(t == d->pressed) { + flags |= QStyle::Style_Sunken; + } + if(t->rect().contains(mapFromGlobal(QCursor::pos()))) { + flags |= QStyle::Style_MouseOver; + } + style().drawControl( QStyle::CE_TabBarLabel, p, this, r, + t->isEnabled() ? colorGroup(): palette().disabled(), + flags, QStyleOption(t, hoverTab) ); } @@ -689,8 +698,9 @@ void QTabBar::paintEvent( QPaintEvent * e ) t = l->first(); do { QTab * n = l->next(); - if ( t && t->r.intersects( e->rect() ) ) + if ( t && t->r.intersects( e->rect() ) ) { paint( buffer.painter(), t, n == 0 ); + } t = n; } while ( t != 0 ); @@ -776,19 +786,38 @@ void QTabBar::mousePressEvent( QMouseEvent * e ) void QTabBar::mouseMoveEvent ( QMouseEvent *e ) { - if ( e->state() != LeftButton ) { - e->ignore(); - return; - } - if(style().styleHint( QStyle::SH_TabBar_SelectMouseType, this ) == QEvent::MouseButtonRelease) { QTab *t = selectTab( e->pos() ); - if(t != d->pressed) { - if(d->pressed) - repaint(d->pressed->rect(), FALSE); - if((d->pressed = t)) - repaint(t->rect(), FALSE); + + // Repaint hover indicator(s) + // Also, avoid unnecessary repaints which otherwise would occour on every MouseMove event causing high cpu load + bool forceRepaint = true; + if (hoverTab == t) { + forceRepaint = false; + } + hoverTab = t; + if (forceRepaint) { + repaint(false); + } + + if ( e->state() != LeftButton ) { + e->ignore(); + return; + } + + if(style().styleHint( QStyle::SH_TabBar_SelectMouseType, this ) == QEvent::MouseButtonRelease) { + if(t != d->pressed) { + if (d->pressed) { + if (!forceRepaint) { + repaint(d->pressed->rect(), FALSE); + } + } + if ((d->pressed = t)) { + if (!forceRepaint) { + repaint(t->rect(), FALSE); + } + } + } } - } } /*! @@ -797,16 +826,28 @@ void QTabBar::mouseMoveEvent ( QMouseEvent *e ) void QTabBar::mouseReleaseEvent( QMouseEvent *e ) { - if ( e->button() != LeftButton ) - e->ignore(); - if(d->pressed) { - QTab *t = selectTab( e->pos() ) == d->pressed ? d->pressed : 0; - d->pressed = 0; - if(t && t->enabled && e->type() == style().styleHint( QStyle::SH_TabBar_SelectMouseType, this )) - setCurrentTab( t ); - } + if (e->button() != LeftButton) { + e->ignore(); + } + + if (d->pressed) { + QTab *t = selectTab( e->pos() ) == d->pressed ? d->pressed : 0; + d->pressed = 0; + if(t && t->enabled && e->type() == style().styleHint( QStyle::SH_TabBar_SelectMouseType, this )) { + setCurrentTab( t ); + } + } } +void QTabBar::enterEvent( QEvent * ) +{ + hoverTab = 0; +} + +void QTabBar::leaveEvent( QEvent * ) +{ + hoverTab = 0; +} /*! \reimp @@ -1119,7 +1160,7 @@ void QTabBar::layoutTabs() h += vframe; t->r = QRect(QPoint(x, 0), style().sizeFromContents(QStyle::CT_TabBarTab, this, QSize( QMAX( lw + hframe + iw, QApplication::globalStrut().width() ), h ), - QStyleOption(t) )); + QStyleOption(t, hoverTab) )); x += t->r.width() - overlap; r = r.unite( t->r ); if ( reverse ) @@ -1375,4 +1416,12 @@ void QTabBar::fontChange( const QFont & oldFont ) QWidget::fontChange( oldFont ); } +/*! + Returns the tab currently under the mouse pointer, or NULL if no tab is under the cursor +*/ +QTab *QTabBar::mouseHoverTab() const +{ + return hoverTab; +} + #endif |