File indexing completed on 2025-01-05 04:25:25

0001 /****************************************************************************************
0002  * Copyright (c) 2011 Emmanuel Wagner <manu.wagner@sfr.fr>                              *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #define DEBUG_PREFIX "CoverGridApplet"
0018 
0019 #include "CoverGridApplet.h"
0020 
0021 #include "AlbumItem.h"
0022 #include "core/support/Amarok.h"
0023 #include "EngineController.h"
0024 #include "core/support/Debug.h"
0025 #include "context/ContextView.h"
0026 #include "core/collections/Collection.h"
0027 #include "core/meta/Meta.h"
0028 #include "core-impl/collections/support/CollectionManager.h"
0029 #include "playlist/PlaylistModelStack.h"
0030 #include "playlist/PlaylistController.h"
0031 
0032 #include <QAction>
0033 #include <KColorScheme>
0034 #include <KConfigDialog>
0035 #include <KGlobalSettings>
0036 #include <Plasma/ScrollWidget>
0037 #include "covermanager/CoverCache.h"
0038 #include <KStandardDirs>
0039 
0040 #include <QGraphicsLinearLayout>
0041 #include <QGraphicsProxyWidget>
0042 #include <QGraphicsSimpleTextItem>
0043 #include <QGraphicsWidget>
0044 #include <QLabel>
0045 #include <QPixmap>
0046 #include <QGraphicsPixmapItem>
0047 #include <QGraphicsGridLayout>
0048 
0049 CoverGridApplet::CoverGridApplet( QObject* parent, const QVariantList& args )
0050     : Context::Applet( parent, args )
0051 {
0052     DEBUG_BLOCK
0053 
0054     setHasConfigurationInterface( true );
0055 }
0056 
0057 void
0058 CoverGridApplet::init()
0059 {
0060     // Call the base implementation.
0061     Context::Applet::init();
0062     setBackgroundHints( Plasma::Applet::NoBackground );
0063 
0064     KConfigGroup config = Amarok::config( "CoverGrid Applet" );
0065     m_coversize = config.readEntry( "CoverSize", 70 );
0066 
0067     QGraphicsLinearLayout* lay = new QGraphicsLinearLayout( Qt::Vertical );
0068     setLayout( lay );
0069     m_scroll = new Plasma::ScrollWidget( this );
0070     m_scroll->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
0071     m_scroll->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOn );
0072     m_scroll->show();
0073 
0074     m_proxywidget = new QGraphicsProxyWidget( this ) ;
0075     m_layout = new QGraphicsGridLayout( m_proxywidget );
0076     m_layout->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
0077 
0078     setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
0079     // create a scroll Area
0080     m_scroll->setWidget( m_proxywidget );
0081     lay->addItem( m_scroll );
0082 
0083     Collections::Collection *coll = CollectionManager::instance()->primaryCollection();
0084     Collections::QueryMaker *qm = coll->queryMaker();
0085     qm->setAutoDelete( true );
0086     qm->setQueryType( Collections::QueryMaker::Album );
0087     qm->orderBy( Meta::valArtist );
0088 
0089     connect( qm, SIGNAL(newResultReady(Meta::AlbumList)),
0090              this, SLOT(slotAlbumQueryResult(Meta::AlbumList)) );
0091     qm->run();
0092 }
0093 
0094 CoverGridApplet::~CoverGridApplet()
0095 {
0096     delete m_proxywidget;
0097 }
0098 
0099 void CoverGridApplet::slotAlbumQueryResult( Meta::AlbumList albums ) //SLOT
0100 {
0101     DEBUG_BLOCK
0102 
0103     m_album_list = albums;
0104     prepareLayout();
0105 }
0106 
0107 void CoverGridApplet::createConfigurationInterface( KConfigDialog *parent )
0108 {
0109     KConfigGroup configuration = config();
0110     QWidget * const settings = new QWidget;
0111     ui_Settings.setupUi( settings );
0112     if( m_coversize )
0113         ui_Settings.coversizeSpin->setValue( m_coversize );
0114     parent->addPage( settings, i18n( "Covergrid Settings" ), "preferences-system" );
0115     connect( parent, SIGNAL(accepted()), this, SLOT(saveSettings()) );
0116 }
0117 bool
0118 CoverGridApplet::hasHeightForWidth() const
0119 {
0120     return false;
0121 }
0122 void CoverGridApplet::saveSettings()
0123 {
0124     m_coversize = ui_Settings.coversizeSpin->value();
0125     KConfigGroup config = Amarok::config( "CoverGrid Applet" );
0126     config.writeEntry( "CoverSize", m_coversize );
0127     prepareLayout();
0128 }
0129 void CoverGridApplet::prepareLayout()
0130 {
0131     int nb_prev = m_layout->count();
0132     for( int i = nb_prev - 1; i >= 0; i-- ) m_layout->removeAt( i );
0133     m_layout->invalidate();
0134 
0135     const int horizontal_size = boundingRect().width();
0136     int x_pos = 0;
0137     int y_pos = 0;
0138     int nb_albums =  m_album_list.size();
0139     int nbcolumns = horizontal_size / m_coversize;
0140     for( int index = 0; index < nb_albums; index++ )
0141     {
0142         Meta::AlbumPtr album = m_album_list[index];
0143         QPixmap pixmap;
0144         if( album->hasImage() )
0145         {
0146             pixmap = The::coverCache()->getCover( album, m_coversize );
0147         }
0148         else
0149         {
0150             pixmap = QPixmap( KStandardDirs::locate( "data", "amarok/images/nocover.png" ) );
0151             QImage image = pixmap.toImage();
0152             image = image.scaled( QSize( m_coversize, m_coversize ), Qt::KeepAspectRatio, Qt::SmoothTransformation );
0153             pixmap = QPixmap::fromImage( image );
0154         }
0155 
0156         QGraphicsProxyWidget* proxywidget = new QGraphicsProxyWidget( this ) ;
0157 
0158         AlbumItem *pixmap_widget = new AlbumItem( pixmap, album );
0159         proxywidget->setWidget( pixmap_widget );
0160         m_layout->addItem( proxywidget, y_pos, x_pos, 0 );
0161         x_pos ++;
0162 
0163         if( x_pos > nbcolumns )
0164         {
0165             x_pos = 0; y_pos++;
0166         }
0167     }
0168     m_layout->activate();
0169 }
0170