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