1
/****************************************************************************************
2
 * Copyright (c) 2008 Nikolaj Hald Nielsen <nhn@kde.org>                                *
3
 * Copyright (c) 2008 Jeff Mitchell <kde-dev@emailgoeshere.com>                         *
4
 * Copyright (c) 2009 Mark Kretschmann <kretschmann@kde.org>                            *
5
 *                                                                                      *
6
 * This program is free software; you can redistribute it and/or modify it under        *
7
 * the terms of the GNU General Public License as published by the Free Software        *
8
 * Foundation; either version 2 of the License, or (at your option) any later           *
9
 * version.                                                                             *
10
 *                                                                                      *
11
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
12
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
13
 * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
14
 *                                                                                      *
15
 * You should have received a copy of the GNU General Public License along with         *
16
 * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
17
 ****************************************************************************************/
18
 
19
#ifndef SVGHANDLER_H
20
#define SVGHANDLER_H
21
22
class QStyleOptionSlider;
23
24
#include "amarok_export.h"
25
#include "core/meta/Meta.h"
26
27
#include <KPixmapCache>
28
#include <QReadWriteLock>
29
#include <KSvgRenderer>
30
31
#include <QPixmap>
32
#include <QString>
33
34
class SvgHandler;
35
36
namespace The {
37
    AMAROK_EXPORT SvgHandler* svgHandler();
38
}
39
40
/**
41
A class to abstract out some common operations of users of tinted svgs
42
*/
43
class AMAROK_EXPORT SvgHandler : public QObject
44
{
45
    Q_OBJECT
46
47
    friend SvgHandler* The::svgHandler();
48
49
    public:
50
        ~SvgHandler();
51
52
        KSvgRenderer* getRenderer( const QString &name );
53
        KSvgRenderer* getRenderer();
54
        QPixmap renderSvg( const QString &name, const QString& keyname, int width, int height, const QString& element = QString(), bool skipCache = false );
55
56
        /**
57
        * Overloaded function that uses the current theme
58
        * @param keyname the name of the key to save in the cache
59
        * @param width Width of the resulting pixmap
60
        * @param height Height of the resulting pixmap
61
        * @param element The theme element to render ( if none the entire svg is rendered )
62
        * @param skipCache If true, the pixmap will always get rendered and never fetched from the cache.
63
        * @return The svg element/file rendered into a pixmap
64
        */
65
        QPixmap renderSvg( const QString& keyname, int width, int height, const QString& element = QString(), bool skipCache = false );
66
        
67
        /**
68
         * Yet another overloaded function. This one renders the svg element and adds half a divider element to the top and the bottom
69
         * so it looks sane when multiple elements with the same width are stacked.
70
         *
71
         * @param keyname the name of the key to save in the cache.
72
         * @param width Width of the resulting pixmap.
73
         * @param height Height of the resulting pixmap.
74
         * @param element The theme element to render ( if none the entire svg is rendered )
75
         * @return The svg element/file rendered into a pixmap.
76
         */
77
        QPixmap renderSvgWithDividers( const QString& keyname, int width, int height, const QString& element = QString() );
78
79
        /**
80
         * Take an album and extract the pixmap for sending to addBordersToPixmap.
81
         *
82
         * @param album The AlbumPtr
83
         * @param size Some size or another
84
         * @borderWidth The desired width of the border
85
         */
86
        QPixmap imageWithBorder( Meta::AlbumPtr album, int size = 1, int borderWidth = 5 );
87
88
        /**
89
         * Add nice borders to a pixmap. The function will create and return a new
90
         * Pixmal that is the size of the old one plus twice the border width in
91
         * each dimension.
92
         * 
93
         * @param orgPixmap The original pixmap.
94
         * @param borderWidth The pixel width of the borders to add to the pixmap. 
95
         * @param name A name for use as the basis of the cache key that for caching the completed image plus borders.
96
         * @param skipCache If true, the pixmap will always get rendered and never fetched from the cache.
97
         */
98
        QPixmap addBordersToPixmap( QPixmap orgPixmap, int borderWidth, const QString &name, bool skipCache =false );
99
100
        /**
101
         * Paint a custom slider using the specified painter. The slider consists
102
         * of a background part, a "knob" that moves along it to show the current
103
         * position, and 2 end markers to clearly mark the ends of the slider.
104
         * The background part before the knob, is painted in a different color than the
105
         * part after (and under) the knob.
106
         * @param p The painter to use.
107
         * @param x The x position to begin painting at.
108
         * @param y The y position to begin painting at.
109
         * @param width The width of the slider to paint.
110
         * @param height The height of the slider. The background part does not scale in height, it will always be a relatively thin line, but the knob and end markers do.
111
         * @param percentage The percentange of the slider that the knob is positioned at.
112
         * @param active Specifies whether the slider should be painted "active" using the current palettes active colors, to specify that it currently has mouse focus or hover.
113
         */
114
        void paintCustomSlider( QPainter *p, QStyleOptionSlider *slider, qreal percentage, bool paintMoodbar  = false );
115
116
        /**
117
         * Calculate the visual slider knob rect from its value, use it instead the QStyle functions
118
         * QStyle::sliderPositionFromValue() and QStyle::subControlRect();
119
         */
120
        QRect sliderKnobRect( const QRect &slider, qreal percent, bool inverse ) const;
121
122
        /**
123
         * Get the path of the currently used svg theme file.
124
         *
125
         * @return the path of the currently used theme file.
126
         */
127
        QString themeFile();
128
129
        /**
130
         * Change the currently used svg theme file. This function also
131
         * clears the pixmap cache as all svg elements have potentially changed
132
         * and should be re-rendered.
133
         *
134
         * @param themeFile The path of the new theme file to use.
135
         */
136
        void setThemeFile( const QString  & themeFile );
137
138
    public slots:
139
        void reTint();
140
141
    signals:
142
        void retinted();
143
144
    private slots:
145
        void discardCache();
146
147
    private:
148
        SvgHandler( QObject* parent = 0 );
149
150
        bool loadSvg( const QString& name );
151
152
        QPixmap sliderHandle( const QColor &color, bool pressed, int size );
153
        QColor calcLightColor(const QColor &color) const;
154
        QColor calcDarkColor(const QColor &color) const;
155
        bool lowThreshold(const QColor &color) const;
156
157
        KPixmapCache * m_cache;
158
        KPixmapCache * m_sliderHandleCache;
159
160
        QHash<QString,KSvgRenderer*> m_renderers;
161
        QReadWriteLock m_lock;
162
163
        QString m_themeFile;
164
        bool m_customTheme;
165
};
166
167
#endif