File indexing completed on 2024-04-21 04:47:53
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-2013 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 #include "amarok_export.h" 0023 #include "core/meta/forward_declarations.h" 0024 0025 #include <QHash> 0026 #include <QPixmap> 0027 #include <QReadWriteLock> 0028 #include <QString> 0029 0030 #include <KImageCache> 0031 0032 class QStyleOptionSlider; 0033 class QSvgRenderer; 0034 class SvgHandler; 0035 0036 namespace The { 0037 AMAROK_EXPORT SvgHandler* svgHandler(); 0038 } 0039 0040 /** 0041 A class to abstract out some common operations of users of tinted svgs 0042 */ 0043 class AMAROK_EXPORT SvgHandler : public QObject 0044 { 0045 Q_OBJECT 0046 0047 friend SvgHandler* The::svgHandler(); 0048 0049 public: 0050 ~SvgHandler() override; 0051 0052 QSvgRenderer* getRenderer( const QString &name ); 0053 QSvgRenderer* getRenderer(); 0054 QPixmap renderSvg( const QString &name, const QString& keyname, int width, int height, const QString& element = QString(), bool skipCache = false, const qreal opacity = 1.0 ); 0055 0056 /** 0057 * Overloaded function that uses the current theme 0058 * @param keyname the name of the key to save in the cache 0059 * @param width Width of the resulting pixmap 0060 * @param height Height of the resulting pixmap 0061 * @param element The theme element to render ( if none the entire svg is rendered ) 0062 * @param skipCache If true, the pixmap will always get rendered and never fetched from the cache. 0063 * @param opacity The opacity used for rendering. Range 0.0 to 1.0. 0064 * @return The svg element/file rendered into a pixmap 0065 */ 0066 QPixmap renderSvg( const QString& keyname, int width, int height, const QString& element = QString(), bool skipCache = false, const qreal opacity = 1.0 ); 0067 0068 /** 0069 * Another overloaded function that loads a svg file from an url. This function is usable from QML. 0070 * @param url the given URL 0071 * @param keyname the name of the key to save in the cache 0072 * @param width Width of the resulting pixmap 0073 * @param height Height of the resulting pixmap 0074 * @param element The theme element to render ( if none the entire svg is rendered ) 0075 * @param skipCache If true, the pixmap will always get rendered and never fetched from the cache. 0076 * @param opacity The opacity used for rendering. Range 0.0 to 1.0. 0077 * @return The svg element/file rendered into a pixmap 0078 */ 0079 Q_INVOKABLE QPixmap renderSvg( const QUrl& url, const QString& keyname, int width, int height, const QString& element = QString(), bool skipCache = false, const qreal opacity = 1.0 ); 0080 0081 /** 0082 * Yet another overloaded function. This one renders the svg element and adds half a divider element to the top and the bottom 0083 * so it looks sane when multiple elements with the same width are stacked. 0084 * 0085 * @param keyname the name of the key to save in the cache. 0086 * @param width Width of the resulting pixmap. 0087 * @param height Height of the resulting pixmap. 0088 * @param element The theme element to render ( if none the entire svg is rendered ) 0089 * @return The svg element/file rendered into a pixmap. 0090 */ 0091 QPixmap renderSvgWithDividers( const QString& keyname, int width, int height, const QString& element = QString() ); 0092 0093 /** 0094 * Take an album and extract the pixmap for sending to addBordersToPixmap. 0095 * 0096 * @param album The AlbumPtr 0097 * @param size The size of the resulting image (border included) 0098 * @param borderWidth The desired width of the border 0099 */ 0100 QPixmap imageWithBorder( Meta::AlbumPtr album, int size = 1, int borderWidth = 5 ); 0101 0102 /** 0103 * Add nice borders to a pixmap. The function will create and return a new 0104 * Pixmap that is the size of the old one plus twice the border width in 0105 * each dimension. 0106 * 0107 * @param orgPixmap The original pixmap. 0108 * @param borderWidth The pixel width of the borders to add to the pixmap. 0109 * @param name A name for use as the basis of the cache key that for caching the completed image plus borders. 0110 * @param skipCache If true, the pixmap will always get rendered and never fetched from the cache. 0111 */ 0112 QPixmap addBordersToPixmap( const QPixmap &orgPixmap, int borderWidth, const QString &name, bool skipCache =false ); 0113 0114 /** 0115 * Paint a custom slider using the specified painter. The slider consists 0116 * of a background part, a "knob" that moves along it to show the current 0117 * position, and 2 end markers to clearly mark the ends of the slider. 0118 * The background part before the knob, is painted in a different color than the 0119 * part after (and under) the knob. 0120 * @param p The painter to use. 0121 * @param slider The slider. 0122 * @param percentage The percentage of the slider that the knob is positioned at. 0123 * @param paintMoodbar Whether to paint a moodbar. 0124 */ 0125 void paintCustomSlider( QPainter *p, QStyleOptionSlider *slider, qreal percentage, bool paintMoodbar = false ); 0126 0127 /** 0128 * Calculate the visual slider knob rect from its value, use it instead the QStyle functions 0129 * QStyle::sliderPositionFromValue() and QStyle::subControlRect(); 0130 */ 0131 QRect sliderKnobRect( const QRect &slider, qreal percent, bool inverse ) const; 0132 0133 /** 0134 * Get the path of the currently used svg theme file. 0135 * 0136 * @return the path of the currently used theme file. 0137 */ 0138 QString themeFile(); 0139 0140 /** 0141 * Change the currently used svg theme file. This function also 0142 * clears the pixmap cache as all svg elements have potentially changed 0143 * and should be re-rendered. 0144 * 0145 * @param themeFile The path of the new theme file to use. 0146 */ 0147 void setThemeFile( const QString & themeFile ); 0148 0149 public Q_SLOTS: 0150 void reTint(); 0151 0152 Q_SIGNALS: 0153 void retinted(); 0154 0155 private Q_SLOTS: 0156 void discardCache(); 0157 0158 private: 0159 SvgHandler( QObject* parent = nullptr ); 0160 0161 bool loadSvg( const QString& name, bool forceCustomTheme = false ); 0162 0163 QPixmap sliderHandle( const QColor &color, bool pressed, int size ); 0164 QColor calcLightColor(const QColor &color) const; 0165 QColor calcDarkColor(const QColor &color) const; 0166 bool lowThreshold(const QColor &color) const; 0167 0168 KImageCache * m_cache; 0169 0170 QHash<QString,QSvgRenderer*> m_renderers; 0171 QReadWriteLock m_lock; 0172 0173 QString m_themeFile; 0174 bool m_customTheme; 0175 }; 0176 0177 #endif