From 71c46e5db9fb6f1efdd40d6969107732bcb6b3e3 Mon Sep 17 00:00:00 2001
From: Christian Muehlhaeuser <muesli@gmail.com>
Date: Tue, 4 Dec 2012 04:26:52 +0100
Subject: [PATCH] * Added PlayableCover. A QLabel that you can set artists,
 albums or queries on and which shows a play button when hovered.

---
 src/libtomahawk/CMakeLists.txt            |   1 +
 src/libtomahawk/widgets/PlayableCover.cpp | 108 ++++++++++++++++++++++
 src/libtomahawk/widgets/PlayableCover.h   |  66 +++++++++++++
 3 files changed, 175 insertions(+)
 create mode 100644 src/libtomahawk/widgets/PlayableCover.cpp
 create mode 100644 src/libtomahawk/widgets/PlayableCover.h

diff --git a/src/libtomahawk/CMakeLists.txt b/src/libtomahawk/CMakeLists.txt
index 35d33e356..e2c6898f7 100644
--- a/src/libtomahawk/CMakeLists.txt
+++ b/src/libtomahawk/CMakeLists.txt
@@ -149,6 +149,7 @@ set( libGuiSources
     widgets/ComboBox.cpp
     widgets/ToggleButton.cpp
     widgets/FadingPixmap.cpp
+    widgets/PlayableCover.cpp
     widgets/SocialPlaylistWidget.cpp
     widgets/SourceTreePopupDialog.cpp
     widgets/infowidgets/SourceInfoWidget.cpp
diff --git a/src/libtomahawk/widgets/PlayableCover.cpp b/src/libtomahawk/widgets/PlayableCover.cpp
new file mode 100644
index 000000000..ea042384d
--- /dev/null
+++ b/src/libtomahawk/widgets/PlayableCover.cpp
@@ -0,0 +1,108 @@
+/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
+ *
+ *   Copyright 2011 - 2012, Christian Muehlhaeuser <muesli@tomahawk-player.org>
+ *
+ *   Tomahawk is free software: you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation, either version 3 of the License, or
+ *   (at your option) any later version.
+ *
+ *   Tomahawk is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "PlayableCover.h"
+
+#include "audio/AudioEngine.h"
+#include "widgets/ImageButton.h"
+#include "utils/TomahawkUtils.h"
+#include "utils/Logger.h"
+
+#include <QPainter>
+
+
+PlayableCover::PlayableCover( QWidget* parent )
+    : QLabel( parent )
+{
+    setMouseTracking( true );
+
+    m_button = new ImageButton( this );
+    m_button->setPixmap( RESPATH "images/play-rest.png" );
+    m_button->setPixmap( RESPATH "images/play-pressed.png", QIcon::Off, QIcon::Active );
+    m_button->setFixedSize( 48, 48 );
+    m_button->setContentsMargins( 0, 0, 0, 0 );
+    m_button->setFocusPolicy( Qt::NoFocus );
+    m_button->installEventFilter( this );
+    m_button->hide();
+
+    connect( m_button, SIGNAL( clicked( bool ) ), SLOT( onClicked() ) );
+}
+
+
+PlayableCover::~PlayableCover()
+{
+}
+
+
+void
+PlayableCover::enterEvent( QEvent* event )
+{
+    QLabel::enterEvent( event );
+
+    m_button->show();
+}
+
+
+void
+PlayableCover::leaveEvent( QEvent* event )
+{
+    QLabel::leaveEvent( event );
+
+    m_button->hide();
+}
+
+
+void
+PlayableCover::resizeEvent( QResizeEvent* event )
+{
+    QLabel::resizeEvent( event );
+    m_button->move( contentsRect().center() - QPoint( 23, 23 ) );
+}
+
+
+void
+PlayableCover::onClicked()
+{
+    if ( m_artist )
+        AudioEngine::instance()->playItem( m_artist );
+    else if ( m_album )
+        AudioEngine::instance()->playItem( m_album );
+    else if ( m_query )
+        AudioEngine::instance()->playItem( Tomahawk::playlistinterface_ptr(), m_query );
+}
+
+
+void
+PlayableCover::setArtist( const Tomahawk::artist_ptr& artist )
+{
+    m_artist = artist;
+}
+
+
+void
+PlayableCover::setAlbum( const Tomahawk::album_ptr& album )
+{
+    m_album = album;
+}
+
+
+void
+PlayableCover::setQuery( const Tomahawk::query_ptr& query )
+{
+    m_query = query;
+}
diff --git a/src/libtomahawk/widgets/PlayableCover.h b/src/libtomahawk/widgets/PlayableCover.h
new file mode 100644
index 000000000..5bc505e75
--- /dev/null
+++ b/src/libtomahawk/widgets/PlayableCover.h
@@ -0,0 +1,66 @@
+/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
+ *
+ *   Copyright 2011 - 2012, Christian Muehlhaeuser <muesli@tomahawk-player.org>
+ *
+ *   Tomahawk is free software: you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation, either version 3 of the License, or
+ *   (at your option) any later version.
+ *
+ *   Tomahawk is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef PLAYABLEPIXMAP_H
+#define PLAYABLEPIXMAP_H
+
+#include <QLabel>
+#include <QPaintEvent>
+#include <QResizeEvent>
+
+#include "Artist.h"
+#include "DllMacro.h"
+
+class ImageButton;
+
+/**
+ * \class PlayableCover
+ * \brief QLabel which shows a play/pause button on hovering.
+ */
+class DLLEXPORT PlayableCover : public QLabel
+{
+Q_OBJECT
+
+public:
+    PlayableCover( QWidget* parent = 0 );
+    virtual ~PlayableCover();
+
+public slots:
+    virtual void setArtist( const Tomahawk::artist_ptr& artist );
+    virtual void setAlbum( const Tomahawk::album_ptr& album );
+    virtual void setQuery( const Tomahawk::query_ptr& query );
+
+protected:
+    virtual void resizeEvent( QResizeEvent* event );
+
+    void leaveEvent( QEvent* event );
+    void enterEvent( QEvent* event );
+
+private slots:
+    void onClicked();
+
+private:
+    QPixmap m_pixmap;
+
+    ImageButton* m_button;
+    Tomahawk::artist_ptr m_artist;
+    Tomahawk::album_ptr m_album;
+    Tomahawk::query_ptr m_query;
+};
+
+#endif