File indexing completed on 2024-05-05 04:49:29

0001 /****************************************************************************************
0002  * Copyright (c) 2007 Jeff Mitchell <kde-dev@emailgoeshere.com>                         *
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 #include "StarManager.h"
0018 
0019 #include "core/support/Amarok.h"
0020 #include <config.h>
0021 #include "core/support/Debug.h"
0022 #include "MainWindow.h"
0023 
0024 #include <KIconEffect>
0025 #include <QStandardPaths>   //KGlobal::dirs()
0026 
0027 #include <QImage>
0028 #include <QPixmap>
0029 
0030 
0031 StarManager* StarManager::s_instance = nullptr;
0032 
0033 StarManager* StarManager::instance()
0034 {
0035     return s_instance ? s_instance : new StarManager( The::mainWindow() );
0036 }
0037 
0038 StarManager::StarManager( QObject* parent )
0039     : QObject( parent )
0040 {
0041     DEBUG_BLOCK
0042 
0043     s_instance = this;
0044 
0045     /*if( AmarokConfig::customRatingsColors() )
0046         AmarokConfig::setCustomRatingsColors( false );
0047     m_colors[0] = AmarokConfig::starColorOne();
0048     m_colors[1] = AmarokConfig::starColorTwo();
0049     m_colors[2] = AmarokConfig::starColorThree();
0050     m_colors[3] = AmarokConfig::starColorFour();
0051     m_colors[4] = AmarokConfig::starColorFive();
0052     m_halfStarColor = AmarokConfig::starColorHalf();*/
0053     m_margin = 1;
0054     m_height = 20;
0055     reinitStars();
0056 }
0057 
0058 StarManager::~StarManager()
0059 {
0060     DEBUG_BLOCK
0061 }
0062 
0063 void
0064 StarManager::reinitStars( int height, int margin )
0065 {
0066     if( height != -1 )
0067         m_height = height;
0068     if( margin != -1 )
0069         m_margin = margin;
0070 
0071     int hval = m_height + m_margin * 2 - 4 + ( ( m_height % 2 ) ? 1 : 0 );
0072     QImage star = QImage( QStandardPaths::locate( QStandardPaths::GenericDataLocation, "amarok/images/star.png" ) ).scaled( hval, hval, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
0073     m_star = star.copy();
0074     m_starPix = QPixmap::fromImage( star );
0075     m_greyedStar = star.copy();
0076     KIconEffect::toGray( m_greyedStar, 1.0 );
0077     m_greyedStarPix = QPixmap::fromImage( m_greyedStar );
0078     QImage half = QImage( QStandardPaths::locate( QStandardPaths::GenericDataLocation, "amarok/images/smallstar.png" ) ).scaled( hval, hval, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
0079     m_halfStar = half.copy();
0080     /*if( AmarokConfig::customRatingsColors() )
0081         KIconEffect::colorize( m_halfStar, m_halfStarColor, 1.0 );*/
0082     m_halfStarPix = QPixmap::fromImage( m_halfStar );
0083 
0084     QImage tempstar;
0085     QImage temphalfstar;
0086     for( int i = 0; i < 5; i++ )
0087     {
0088         tempstar = star.copy();
0089         temphalfstar = half.copy();
0090         /*if( AmarokConfig::customRatingsColors() )
0091         {
0092             KIconEffect::colorize( tempstar, m_colors[i], 1.0 );
0093             if( !AmarokConfig::fixedHalfStarColor() )
0094                 KIconEffect::colorize( temphalfstar, m_colors[i], 1.0 );
0095         }*/
0096         m_images[i] = tempstar.copy();
0097         m_halfimages[i] = temphalfstar.copy();
0098         m_pixmaps[i] = QPixmap::fromImage( tempstar );
0099         m_halfpixmaps[i] = QPixmap::fromImage( temphalfstar );
0100         tempstar = QImage();
0101         temphalfstar = QImage();
0102     }
0103     //TODO:PORT
0104 //     if( Playlist::instance() ) Playlist::instance()->qscrollview()->viewport()->update();
0105 /*PORT 2.0
0106     if( CollectionView::instance() &&
0107             CollectionView::instance()->viewMode() == CollectionView::modeFlatView )
0108         CollectionView::instance()->triggerUpdate(); */
0109     Q_EMIT ratingsColorsChanged();
0110 }
0111 
0112 QPixmap*
0113 StarManager::getStar( int num )
0114 {
0115     if( num < 1 || num > 5 )
0116         return &m_starPix;
0117     else
0118         return &m_pixmaps[num - 1];
0119 }
0120 
0121 QImage&
0122 StarManager::getStarImage( int num )
0123 {
0124     if( num < 1 || num > 5 )
0125         return m_star;
0126     else
0127         return m_images[num - 1];
0128 }
0129 
0130 QPixmap*
0131 StarManager::getHalfStar( int num )
0132 {
0133     /*if( AmarokConfig::fixedHalfStarColor() || num == -1 )
0134         return &m_halfStarPix;
0135     else*/
0136     if( num < 1 || num > 5 )
0137         return &m_starPix;
0138     else
0139         return &m_halfpixmaps[num - 1];
0140 }
0141 
0142 QImage&
0143 StarManager::getHalfStarImage( int num  )
0144 {
0145     /*if( AmarokConfig::fixedHalfStarColor() || num == -1 )
0146         return m_halfStar;
0147     else*/
0148         return m_halfimages[num - 1];
0149 }
0150 
0151 bool
0152 StarManager::setColor( int starNum, const QColor &color )
0153 {
0154     if( starNum < 1 || starNum > 5 )
0155         return false;
0156     m_colors[starNum - 1] = color;
0157     return true;
0158 }
0159 
0160 bool
0161 StarManager::setHalfColor( const QColor &color )
0162 {
0163     m_halfStarColor = color;
0164     return true;
0165 }
0166 
0167