1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-03-20 07:49:42 +01:00

* Add an alternative OverlayWidget CTOR for regular QWidgets.

This commit is contained in:
Christian Muehlhaeuser 2012-06-01 10:40:48 +02:00
parent 95dd12aaea
commit f35821b3b5
2 changed files with 54 additions and 27 deletions

View File

@ -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<PlayableProxyModel*>( m_parent->model() );
if ( !m_itemView )
return;
PlayableProxyModel* model = qobject_cast<PlayableProxyModel*>( 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() );

View File

@ -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;
};