1 /****************************************************************************************
2 * Copyright (c) 2007 Jeff Mitchell <kde-dev@emailgoeshere.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify it under *
5 * the terms of the GNU General Public License as published by the Free Software *
6 * Foundation; either version 2 of the License, or (at your option) any later *
7 * version. *
8 * *
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY *
10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
11 * PARTICULAR PURPOSE. See the GNU General Public License for more details. *
12 * *
13 * You should have received a copy of the GNU General Public License along with *
14 * this program. If not, see <http://www.gnu.org/licenses/>. *
15 ****************************************************************************************/
16
17 #include "StarManager.h"
18
19 #include "core/support/Amarok.h"
20 #include <config.h>
21 #include "core/support/Debug.h"
22 #include "MainWindow.h"
23
24 #include <KIconEffect>
25 #include <QStandardPaths> //KGlobal::dirs()
26
27 #include <QImage>
28 #include <QPixmap>
29
30
31 StarManager* StarManager::s_instance = nullptr;
32
instance()33 StarManager* StarManager::instance()
34 {
35 return s_instance ? s_instance : new StarManager( The::mainWindow() );
36 }
37
StarManager(QObject * parent)38 StarManager::StarManager( QObject* parent )
39 : QObject( parent )
40 {
41 DEBUG_BLOCK
42
43 s_instance = this;
44
45 /*if( AmarokConfig::customRatingsColors() )
46 AmarokConfig::setCustomRatingsColors( false );
47 m_colors[0] = AmarokConfig::starColorOne();
48 m_colors[1] = AmarokConfig::starColorTwo();
49 m_colors[2] = AmarokConfig::starColorThree();
50 m_colors[3] = AmarokConfig::starColorFour();
51 m_colors[4] = AmarokConfig::starColorFive();
52 m_halfStarColor = AmarokConfig::starColorHalf();*/
53 m_margin = 1;
54 m_height = 20;
55 reinitStars();
56 }
57
~StarManager()58 StarManager::~StarManager()
59 {
60 DEBUG_BLOCK
61 }
62
63 void
reinitStars(int height,int margin)64 StarManager::reinitStars( int height, int margin )
65 {
66 if( height != -1 )
67 m_height = height;
68 if( margin != -1 )
69 m_margin = margin;
70
71 int hval = m_height + m_margin * 2 - 4 + ( ( m_height % 2 ) ? 1 : 0 );
72 QImage star = QImage( QStandardPaths::locate( QStandardPaths::GenericDataLocation, "amarok/images/star.png" ) ).scaled( hval, hval, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
73 m_star = star.copy();
74 m_starPix = QPixmap::fromImage( star );
75 m_greyedStar = star.copy();
76 KIconEffect::toGray( m_greyedStar, 1.0 );
77 m_greyedStarPix = QPixmap::fromImage( m_greyedStar );
78 QImage half = QImage( QStandardPaths::locate( QStandardPaths::GenericDataLocation, "amarok/images/smallstar.png" ) ).scaled( hval, hval, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
79 m_halfStar = half.copy();
80 /*if( AmarokConfig::customRatingsColors() )
81 KIconEffect::colorize( m_halfStar, m_halfStarColor, 1.0 );*/
82 m_halfStarPix = QPixmap::fromImage( m_halfStar );
83
84 QImage tempstar;
85 QImage temphalfstar;
86 for( int i = 0; i < 5; i++ )
87 {
88 tempstar = star.copy();
89 temphalfstar = half.copy();
90 /*if( AmarokConfig::customRatingsColors() )
91 {
92 KIconEffect::colorize( tempstar, m_colors[i], 1.0 );
93 if( !AmarokConfig::fixedHalfStarColor() )
94 KIconEffect::colorize( temphalfstar, m_colors[i], 1.0 );
95 }*/
96 m_images[i] = tempstar.copy();
97 m_halfimages[i] = temphalfstar.copy();
98 m_pixmaps[i] = QPixmap::fromImage( tempstar );
99 m_halfpixmaps[i] = QPixmap::fromImage( temphalfstar );
100 tempstar = QImage();
101 temphalfstar = QImage();
102 }
103 //TODO:PORT
104 // if( Playlist::instance() ) Playlist::instance()->qscrollview()->viewport()->update();
105 /*PORT 2.0
106 if( CollectionView::instance() &&
107 CollectionView::instance()->viewMode() == CollectionView::modeFlatView )
108 CollectionView::instance()->triggerUpdate(); */
109 Q_EMIT ratingsColorsChanged();
110 }
111
112 QPixmap*
getStar(int num)113 StarManager::getStar( int num )
114 {
115 if( num < 1 || num > 5 )
116 return &m_starPix;
117 else
118 return &m_pixmaps[num - 1];
119 }
120
121 QImage&
getStarImage(int num)122 StarManager::getStarImage( int num )
123 {
124 if( num < 1 || num > 5 )
125 return m_star;
126 else
127 return m_images[num - 1];
128 }
129
130 QPixmap*
getHalfStar(int num)131 StarManager::getHalfStar( int num )
132 {
133 /*if( AmarokConfig::fixedHalfStarColor() || num == -1 )
134 return &m_halfStarPix;
135 else*/
136 if( num < 1 || num > 5 )
137 return &m_starPix;
138 else
139 return &m_halfpixmaps[num - 1];
140 }
141
142 QImage&
getHalfStarImage(int num)143 StarManager::getHalfStarImage( int num )
144 {
145 /*if( AmarokConfig::fixedHalfStarColor() || num == -1 )
146 return m_halfStar;
147 else*/
148 return m_halfimages[num - 1];
149 }
150
151 bool
setColor(int starNum,const QColor & color)152 StarManager::setColor( int starNum, const QColor &color )
153 {
154 if( starNum < 1 || starNum > 5 )
155 return false;
156 m_colors[starNum - 1] = color;
157 return true;
158 }
159
160 bool
setHalfColor(const QColor & color)161 StarManager::setHalfColor( const QColor &color )
162 {
163 m_halfStarColor = color;
164 return true;
165 }
166
167
168