From f35821b3b59cc7a018d9d16b1344c9d931a46775 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Fri, 1 Jun 2012 10:40:48 +0200 Subject: [PATCH] * Add an alternative OverlayWidget CTOR for regular QWidgets. --- src/libtomahawk/widgets/OverlayWidget.cpp | 74 +++++++++++++++-------- src/libtomahawk/widgets/OverlayWidget.h | 7 ++- 2 files changed, 54 insertions(+), 27 deletions(-) diff --git a/src/libtomahawk/widgets/OverlayWidget.cpp b/src/libtomahawk/widgets/OverlayWidget.cpp index 4917c84cf..aa6b846ba 100644 --- a/src/libtomahawk/widgets/OverlayWidget.cpp +++ b/src/libtomahawk/widgets/OverlayWidget.cpp @@ -31,32 +31,30 @@ #define OPACITY 0.70 +OverlayWidget::OverlayWidget( QWidget* parent ) + : QWidget( parent ) // this is on purpose! + , m_parent( parent ) + , m_itemView( 0 ) +{ + init(); +} + + OverlayWidget::OverlayWidget( QAbstractItemView* parent ) : QWidget( parent ) // this is on purpose! - , m_opacity( 0.00 ) , m_parent( parent ) + , m_itemView( parent ) { - setAttribute( Qt::WA_TranslucentBackground, true ); + init(); - setOpacity( m_opacity ); - - m_timer.setSingleShot( true ); - connect( &m_timer, SIGNAL( timeout() ), this, SLOT( hide() ) ); - - if ( m_parent->model() ) + if ( m_itemView->model() ) { - connect( m_parent->model(), SIGNAL( rowsInserted( QModelIndex, int, int ) ), SLOT( onViewChanged() ), Qt::UniqueConnection ); - connect( m_parent->model(), SIGNAL( rowsRemoved( QModelIndex, int, int ) ), SLOT( onViewChanged() ), Qt::UniqueConnection ); - connect( m_parent->model(), SIGNAL( loadingStarted() ), SLOT( onViewChanged() ), Qt::UniqueConnection ); - connect( m_parent->model(), SIGNAL( loadingFinished() ), SLOT( onViewChanged() ), Qt::UniqueConnection ); + connect( m_itemView->model(), SIGNAL( rowsInserted( QModelIndex, int, int ) ), SLOT( onViewChanged() ), Qt::UniqueConnection ); + connect( m_itemView->model(), SIGNAL( rowsRemoved( QModelIndex, int, int ) ), SLOT( onViewChanged() ), Qt::UniqueConnection ); + connect( m_itemView->model(), SIGNAL( loadingStarted() ), SLOT( onViewChanged() ), Qt::UniqueConnection ); + connect( m_itemView->model(), SIGNAL( loadingFinished() ), SLOT( onViewChanged() ), Qt::UniqueConnection ); } - connect( m_parent, SIGNAL( modelChanged() ), SLOT( onViewModelChanged() ) ); - -#ifdef Q_WS_MAC - QFont f( font() ); - f.setPointSize( f.pointSize() - 2 ); - setFont( f ); -#endif + connect( m_itemView, SIGNAL( modelChanged() ), SLOT( onViewModelChanged() ) ); } @@ -65,6 +63,24 @@ OverlayWidget::~OverlayWidget() } +void +OverlayWidget::init() +{ + setAttribute( Qt::WA_TranslucentBackground, true ); + m_opacity = 0.00; + setOpacity( m_opacity ); + + m_timer.setSingleShot( true ); + connect( &m_timer, SIGNAL( timeout() ), this, SLOT( hide() ) ); + +#ifdef Q_WS_MAC + QFont f( font() ); + f.setPointSize( f.pointSize() - 2 ); + setFont( f ); +#endif +} + + void OverlayWidget::setOpacity( qreal opacity ) { @@ -133,7 +149,10 @@ OverlayWidget::shown() const void OverlayWidget::onViewChanged() { - PlayableProxyModel* model = qobject_cast( m_parent->model() ); + if ( !m_itemView ) + return; + + PlayableProxyModel* model = qobject_cast( m_itemView->model() ); if ( m_text.isEmpty() || ( model && ( model->rowCount( QModelIndex() ) || model->isLoading() ) ) ) { @@ -149,12 +168,15 @@ OverlayWidget::onViewChanged() void OverlayWidget::onViewModelChanged() { - if ( m_parent->model() ) + if ( !m_itemView ) + return; + + if ( m_itemView->model() ) { - connect( m_parent->model(), SIGNAL( rowsInserted( QModelIndex, int, int ) ), SLOT( onViewChanged() ), Qt::UniqueConnection ); - connect( m_parent->model(), SIGNAL( rowsRemoved( QModelIndex, int, int ) ), SLOT( onViewChanged() ), Qt::UniqueConnection ); - connect( m_parent->model(), SIGNAL( loadingStarted() ), SLOT( onViewChanged() ), Qt::UniqueConnection ); - connect( m_parent->model(), SIGNAL( loadingFinished() ), SLOT( onViewChanged() ), Qt::UniqueConnection ); + connect( m_itemView->model(), SIGNAL( rowsInserted( QModelIndex, int, int ) ), SLOT( onViewChanged() ), Qt::UniqueConnection ); + connect( m_itemView->model(), SIGNAL( rowsRemoved( QModelIndex, int, int ) ), SLOT( onViewChanged() ), Qt::UniqueConnection ); + connect( m_itemView->model(), SIGNAL( loadingStarted() ), SLOT( onViewChanged() ), Qt::UniqueConnection ); + connect( m_itemView->model(), SIGNAL( loadingFinished() ), SLOT( onViewChanged() ), Qt::UniqueConnection ); onViewChanged(); } @@ -167,7 +189,7 @@ OverlayWidget::paintEvent( QPaintEvent* event ) Q_UNUSED( event ); { - QSize maxiSize = QSize( (double)m_parent->viewport()->width() * 0.70, (double)m_parent->viewport()->height() * 0.70 ); + QSize maxiSize = QSize( (double)m_parent->width() * 0.70, (double)m_parent->height() * 0.70 ); QSize prefSize = QSize( 380, 128 ); int width = qMin( maxiSize.width(), prefSize.width() ); int height = qMin( maxiSize.height(), prefSize.height() ); diff --git a/src/libtomahawk/widgets/OverlayWidget.h b/src/libtomahawk/widgets/OverlayWidget.h index 352049799..c618cd118 100644 --- a/src/libtomahawk/widgets/OverlayWidget.h +++ b/src/libtomahawk/widgets/OverlayWidget.h @@ -31,6 +31,7 @@ Q_OBJECT Q_PROPERTY( qreal opacity READ opacity WRITE setOpacity ) public: + OverlayWidget( QWidget* parent ); OverlayWidget( QAbstractItemView* parent ); ~OverlayWidget(); @@ -55,10 +56,14 @@ private slots: void onViewModelChanged(); private: + void init(); + QString m_text; qreal m_opacity; - QAbstractItemView* m_parent; + QWidget* m_parent; + QAbstractItemView* m_itemView; + QTimer m_timer; };