From 7ad9e32c809c2b8c03e857fc0a9a349fa257eb31 Mon Sep 17 00:00:00 2001 From: Cyrille Bollu Date: Mon, 22 Feb 2016 10:23:01 +0100 Subject: [PATCH] Added tooltip support for the TabWidget class. Atm, tooltips are simply tabs' name. --- include/TabWidget.h | 3 ++ src/gui/widgets/TabWidget.cpp | 69 ++++++++++++++++++++++++++++------- 2 files changed, 59 insertions(+), 13 deletions(-) diff --git a/include/TabWidget.h b/include/TabWidget.h index c8a9365fc..7ee30f879 100644 --- a/include/TabWidget.h +++ b/include/TabWidget.h @@ -40,6 +40,8 @@ public: void setActiveTab( int _idx ); + int findTabAtPos( const QPoint *pos ); + inline int activeTab() const { return( m_activeTab ); @@ -47,6 +49,7 @@ public: protected: + virtual bool event( QEvent * event ); virtual void mousePressEvent( QMouseEvent * _me ); virtual void paintEvent( QPaintEvent * _pe ); virtual void resizeEvent( QResizeEvent * _re ); diff --git a/src/gui/widgets/TabWidget.cpp b/src/gui/widgets/TabWidget.cpp index cb41f771e..9824860fa 100644 --- a/src/gui/widgets/TabWidget.cpp +++ b/src/gui/widgets/TabWidget.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include "gui_templates.h" @@ -134,30 +135,72 @@ void TabWidget::setActiveTab( int _idx ) } - - -void TabWidget::mousePressEvent( QMouseEvent * _me ) +// Return the index of the tab at position "pos" +int TabWidget::findTabAtPos( const QPoint *pos ) { int height = ( m_usePixmap ? GRAPHICAL_TAB_HEIGHT -1 : TEXT_TAB_HEIGHT -1 ); - if( _me->y() > 1 && _me->y() < height ) + if( pos->y() > 1 && pos->y() < height ) { - int cx = ( ( m_caption == "" ) ? 4 : 14 ) + - fontMetrics().width( m_caption ); - for( widgetStack::iterator it = m_widgets.begin(); - it != m_widgets.end(); ++it ) + int cx = ( ( m_caption == "" ) ? 4 : 14 ) + fontMetrics().width( m_caption ); + + for( widgetStack::iterator it = m_widgets.begin(); it != m_widgets.end(); ++it ) { - if( _me->x() >= cx && - _me->x() <= cx + ( *it ).nwidth ) + if( pos->x() >= cx && pos->x() <= cx + ( *it ).nwidth ) { - setActiveTab( it.key() ); - update(); - return; + return( it.key() ); } cx += ( *it ).nwidth; } } + + // Haven't found any tab at position "pos" + return( -1 ); +} + + +// Overload the QWidget::event handler to display tooltips (from https://doc.qt.io/qt-4.8/qt-widgets-tooltips-example.html) +bool TabWidget::event(QEvent *event) +{ + + if ( event->type() == QEvent::ToolTip ) + { + QHelpEvent *helpEvent = static_cast(event); + + int idx = findTabAtPos( & helpEvent->pos() ); + + if ( idx != -1 ) + { + // Display tab's tooltip + QToolTip::showText( helpEvent->globalPos(), m_widgets[idx].name ); + } else + { + // The tooltip event doesn't relate to any tab, let's ignore it + QToolTip::hideText(); + event->ignore(); + } + + return true; + } + + // not a Tooltip event, let's propagate it to the other event handlers + return QWidget::event(event); +} + + +// Activate tab when clicked +void TabWidget::mousePressEvent( QMouseEvent * _me ) +{ + + int idx = findTabAtPos( & _me->pos() ); + + if ( idx != -1 ) + { + setActiveTab( idx ); + update(); + return; + } }