File indexing completed on 2023-05-30 11:30:52

0001 /**
0002  * Copyright (c) 2008 Nikolaj Hald Nielsen <nhn@kde.org>
0003  * Copyright (c) 2008 Jeff Mitchell <kde-dev@emailgoeshere.com>
0004  * Copyright (c) 2009 Mark Kretschmann <kretschmann@kde.org>
0005  *
0006  * This program is free software; you can redistribute it and/or modify it under
0007  * the terms of the GNU General Public License as published by the Free Software
0008  * Foundation; either version 2 of the License, or (at your option) any later
0009  * version.
0010  *
0011  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0012  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0013  * PARTICULAR PURPOSE. See the GNU General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU General Public License along with
0016  * this program.  If not, see <http://www.gnu.org/licenses/>.
0017  */
0018 
0019 #ifndef SVGHANDLER_H
0020 #define SVGHANDLER_H
0021 
0022 class QStyleOptionSlider;
0023 
0024 #include <QMap>
0025 #include <QReadWriteLock>
0026 
0027 #include <QPixmap>
0028 #include <QString>
0029 
0030 class SvgHandler;
0031 class QSvgRenderer;
0032 
0033 namespace The {
0034     SvgHandler* svgHandler();
0035 }
0036 
0037 /**
0038 A class to abstract out some common operations of users of tinted svgs
0039 */
0040 class SvgHandler : public QObject
0041 {
0042     Q_OBJECT
0043 
0044     friend SvgHandler* The::svgHandler();
0045 
0046     public:
0047         ~SvgHandler();
0048 
0049         QSvgRenderer* getRenderer();
0050 
0051         /**
0052         * Overloaded function that uses the current theme
0053         * @param keyname the name of the key to save in the cache
0054         * @param width Width of the resulting pixmap
0055         * @param height Height of the resulting pixmap
0056         * @param element The theme element to render ( if none the entire svg is rendered )
0057         * @return The svg element/file rendered into a pixmap
0058         */
0059         QPixmap renderSvg( const QString& keyname, int width, int height, const QString& element = QString() );
0060 
0061         /**
0062          * Paint a custom slider using the specified painter. The slider consists
0063          * of a background part, a "knob" that moves along it to show the current
0064          * position, and 2 end markers to clearly mark the ends of the slider.
0065          * The background part before the knob, is painted in a different color than the
0066          * part after (and under) the knob.
0067          * @param p The painter to use.
0068          * @param x The x position to begin painting at.
0069          * @param y The y position to begin painting at.
0070          * @param width The width of the slider to paint.
0071          * @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.
0072          * @param percentage The percentange of the slider that the knob is positioned at.
0073          * @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.
0074          */
0075         void paintCustomSlider( QPainter *p, QStyleOptionSlider *slider, qreal percentage );
0076 
0077         /**
0078          * Calculate the visual slider knob rect from its value, use it instead the QStyle functions
0079          * QStyle::sliderPositionFromValue() and QStyle::subControlRect();
0080          */
0081         QRectF sliderKnobRect( const QRectF &slider, qreal percent, bool inverse ) const;
0082 
0083         /**
0084          * Get the path of the currently used svg theme file.
0085          *
0086          * @return the path of the currently used theme file.
0087          */
0088         QString themeFile();
0089         
0090         void setDevicePixelRatioF(qreal dpr);
0091 
0092     public slots:
0093         void reTint();
0094 
0095     signals:
0096         void retinted();
0097 
0098     private:
0099         SvgHandler( QObject* parent = 0 );
0100 
0101         bool loadSvg( const QString& name );
0102 
0103         QPixmap sliderHandle( const QColor &color, bool pressed, int size );
0104 
0105         QMap<QString, QPixmap> m_cache;
0106 
0107         QSvgRenderer *m_renderer;
0108         QReadWriteLock m_lock;
0109 
0110         QString m_themeFile;
0111         
0112         qreal dpr = 1;
0113 };
0114 
0115 #endif