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

0001 /****************************************************************************************
0002  * Copyright (c) 2007 Leo Franchi <lfranchi@gmail.com>                                  *
0003  * Copyright (c) 2007 Jeff Mitchell <kde-dev@emailgoeshere.com>                         *
0004  *                                                                                      *
0005  * This program is free software; you can redistribute it and/or modify it under        *
0006  * the terms of the GNU General Public License as published by the Free Software        *
0007  * Foundation; either version 2 of the License, or (at your option) any later           *
0008  * version.                                                                             *
0009  *                                                                                      *
0010  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0012  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0013  *                                                                                      *
0014  * You should have received a copy of the GNU General Public License along with         *
0015  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0016  ****************************************************************************************/
0017 
0018 #include "PlaylistInfo.h"
0019 
0020 #include "core/support/Amarok.h"
0021 #include "core/support/Debug.h"
0022 #include "context/Svg.h"
0023 
0024 #include <QPainter>
0025 #include <QBrush>
0026 #include <QSpinBox>
0027 #include <QLabel>
0028 #include <KConfigGroup>
0029 #include <QDialogButtonBox>
0030 #include <QPushButton>
0031 #include <QVBoxLayout>
0032 
0033 PlaylistInfo::PlaylistInfo( QObject* parent, const QStringList& args )
0034     : Plasma::Applet( parent, args )
0035     , m_config( 0 )
0036     , m_configLayout( 0 )
0037     , m_width( 0 )
0038     , m_aspectRatio( 0.0 )
0039     , m_size( QSizeF() )
0040     , m_rating( -1 )
0041     , m_trackLength( 0 )
0042 {
0043     DEBUG_BLOCK
0044 
0045     setHasConfigurationInterface( true );
0046 
0047     dataEngine( "amarok-current" )->connectSource( "current", this );
0048 
0049     m_theme = new Context::Svg( this );
0050     m_theme->setImagePath( "widgets/amarok-playlistinfo" );
0051     m_theme->setContentType( Context::Svg::SingleImage );
0052     m_width = globalConfig().readEntry( "width", 500 );
0053 
0054     m_totalTracks = new QGraphicsSimpleTextItem( this );
0055     m_totalTime = new QGraphicsSimpleTextItem( this );
0056     m_totalSize = new QGraphicsSimpleTextItem( this );
0057 
0058     m_totalTracks->setBrush( QBrush( Qt::white ) );
0059     m_totalTime->setBrush( QBrush( Qt::white ) );
0060     m_totalSize->setBrush( QBrush( Qt::white ) );
0061 
0062     // get natural aspect ratio, so we can keep it on resize
0063     m_theme->resize();
0064     m_aspectRatio = (qreal)m_theme->size().height() / (qreal)m_theme->size().width();
0065     resize( m_width, m_aspectRatio );
0066 
0067     constraintsEvent();
0068 }
0069 
0070 PlaylistInfo::~PlaylistInfo()
0071 {
0072     DEBUG_BLOCK
0073 }
0074 
0075 void PlaylistInfo::constraintsEvent()
0076 {
0077     prepareGeometryChange();
0078     // here we put all of the text items into the correct locations
0079     m_title->setPos( m_theme->elementRect( "track" ).topLeft() );
0080     m_artist->setPos( m_theme->elementRect( "artist" ).topLeft() );
0081     m_album->setPos( m_theme->elementRect( "album" ).topLeft() );
0082     m_score->setPos( m_theme->elementRect( "score" ).topLeft() );
0083     m_numPlayed->setPos( m_theme->elementRect( "numplayed" ).topLeft() );
0084     m_playedLast->setPos( m_theme->elementRect( "playedlast" ).topLeft() );
0085     m_albumCover->setPos( m_theme->elementRect( "albumart" ).topLeft() );
0086 
0087     QPixmap cover = m_albumCover->pixmap();
0088     cover = cover.scaledToWidth( m_theme->elementRect( "albumart" ).size().width(), Qt::SmoothTransformation );
0089     m_albumCover->setPixmap( cover );
0090 //     debug() << "changing pixmap size from " << m_albumCover->pixmap().width() << " to " << cover.width();
0091 
0092     dataEngine( "amarok-current" )->setProperty( "coverWidth", m_theme->elementRect( "albumart" ).size().width() );
0093 }
0094 
0095 void PlaylistInfo::dataUpdated( const QString& name, const Plasma::DataEngine::Data& data )
0096 {
0097     DEBUG_BLOCK
0098     Q_UNUSED( name );
0099 
0100     if( data.size() == 0 ) return;
0101 
0102     QVariantList currentInfo = data[ "current" ].toList();
0103     kDebug() << "got data from engine: " << currentInfo;
0104     m_title->setText( currentInfo[ 1 ].toString() );
0105     m_artist->setText( currentInfo[ 0 ].toString() );
0106     m_album->setText( currentInfo[ 2 ].toString() );
0107 //     m_rating = currentInfo[ 3 ].toInt();
0108     // TODO i can't add ratings... so hardcoding to test
0109     m_rating = 6;
0110     m_score->setText( currentInfo[ 4 ].toString() );
0111     m_trackLength = currentInfo[ 5 ].toInt();
0112 //     m_playedLast->setText( Amarok::verboseTimeSince( currentInfo[ 6 ].toInt() ) );
0113 
0114     // scale pixmap on demand
0115     QPixmap cover = m_albumCover->pixmap();
0116     cover = cover.scaledToWidth( m_theme->elementRect( "albumart" ).size().width(), Qt::SmoothTransformation );
0117     m_albumCover->setPixmap( cover );
0118 //     debug() << "changing pixmap size from " << m_albumCover->pixmap().width() << " to " << cover.width();
0119 
0120     m_numPlayed->setText( currentInfo[ 7 ].toString() );
0121     m_albumCover->setPixmap( data[ "albumart" ].value<QPixmap>() );
0122 
0123 }
0124 
0125 void PlaylistInfo::paintInterface( QPainter *p, const QStyleOptionGraphicsItem *option, const QRect &contentsRect )
0126 {
0127     Q_UNUSED( option );
0128 
0129     p->save();
0130     m_theme->paint( p, contentsRect, "background" );
0131     p->restore();
0132 
0133     p->save();
0134     int stars = m_rating / 2;
0135     for( int i = 1; i <= stars; i++ )
0136     {
0137         p->translate( m_theme->elementSize( "star" ).width(), 0 );
0138         m_theme->paint( p, m_theme->elementRect( "star" ), "star" );
0139     } //
0140     if( m_rating % 2 != 0 )
0141     { // paint a half star too
0142         m_theme->paint( p, m_theme->elementRect( "half-star" ), "half-star" );
0143     }
0144     p->restore();
0145 
0146     // TODO get, and then paint, album pixmap
0147 
0148 }
0149 
0150 void PlaylistInfo::showConfigurationInterface()
0151 {
0152     if (m_config == 0)
0153     {
0154         m_config = new QDialog();
0155         m_config->setWindowTitle( i18n( "Configure Playlist Info Applet" ) );
0156 
0157         QWidget* widget = new QWidget( m_config );
0158         QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel|QDialogButtonBox::Apply);
0159         QVBoxLayout *mainLayout = new QVBoxLayout;
0160         m_config->setLayout(mainLayout);
0161         
0162         QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
0163         okButton->setDefault(true);
0164         okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0165         m_config->connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
0166         m_config->connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
0167         connect( m_config, SIGNAL(clicked()), this, SLOT(configAccepted()) );
0168         connect( m_config, SIGNAL(clicked()), this, SLOT(configAccepted()) );
0169 
0170         mainLayout->addWidget(buttonBox);
0171 
0172         m_configLayout = new QHBoxLayout( widget );
0173         m_spinWidth = new QSpinBox();
0174         m_spinWidth->setRange( 200, 700 );
0175         m_spinWidth->setValue( m_width );
0176 
0177         QLabel *labelWidth = new QLabel(i18n("Width"));
0178         m_configLayout->addWidget( labelWidth );
0179         m_configLayout->addWidget( m_spinWidth );
0180 
0181     }
0182 
0183     m_config->show();
0184 }
0185 
0186 void PlaylistInfo::configAccepted() // SLOT
0187 {
0188     KConfigGroup cg = globalConfig();
0189 
0190     m_width = m_spinWidth->value();
0191     resize( m_width, m_aspectRatio );
0192 
0193     cg.writeEntry( "width", m_width );
0194 
0195     cg.sync();
0196     constraintsEvent();
0197 }
0198 
0199 void PlaylistInfo::resize( qreal newWidth, qreal aspectRatio )
0200 {
0201     qreal height = aspectRatio * newWidth;
0202     m_size.setWidth( newWidth );
0203     m_size.setHeight( height );
0204 
0205     m_theme->resize( m_size );
0206     kDebug() << "set new size: " << m_size;
0207     constraintsEvent();
0208 }
0209 
0210