Debian Codegraph @

src/Gui/BitmapFactory.h

raw
1 /***************************************************************************
2 * Copyright (c) 2004 Jürgen Riegel <[email protected]> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23
24 #ifndef GUI_BITMAPFACTORY_H
25 #define GUI_BITMAPFACTORY_H
26
27 #include <Base/Factory.h>
28 #include <QPixmap>
29 #include <QIcon>
30
31 // forward declaration
32 class SoSFImage;
33 class QImage;
34
35 namespace Gui
36 {
37
38 using ColorMap = std::map<unsigned long, unsigned long>;
39
40 /** The Bitmap Factory
41 * the main purpose is to collect all build in Bitmaps and
42 * hold all paths for the extern bitmaps (files) to serve
43 * as a single point of accessing bitmaps in FreeCAD
44 * \author Werner Mayer, Jürgen Riegel
45 */
46 class BitmapFactoryInstP;
47 class GuiExport BitmapFactoryInst: public Base::Factory
48 {
49 public:
50 enum Position
51 {
52 TopLeft, /**< Place to the top left corner */
53 TopRight, /**< Place to the top right corner */
54 BottomLeft, /**< Place to the bottom left corner */
55 BottomRight /**< Place to the bottom right corner */
56 };
57
58 static BitmapFactoryInst& instance();
59 static void destruct();
60
61 /// Adds a path where pixmaps can be found
62 void addPath(const QString& path);
63 /// Removes a path from the list of pixmap paths
64 void removePath(const QString& path);
65 /// Returns the list of search paths
66 QStringList getPaths() const;
67 /// Returns the absolute file names of icons found in the given search paths
68 QStringList findIconFiles() const;
69 /// Adds a build in XPM pixmap under a given name
70 void addPixmapToCache(const char* name, const QPixmap& icon);
71 /// Checks whether the pixmap is already registered.
72 bool findPixmapInCache(const char* name, QPixmap& icon) const;
73 /** Returns the QIcon corresponding to name in the current icon theme.
74 * If no such icon is found in the current theme fallback is returned instead.
75 */
76 QIcon iconFromTheme(const char* name, const QIcon& fallback = QIcon());
77 /** Returns the QIcon corresponding to name in the default (FreeCAD's) icon theme.
78 * If no such icon is found in the current theme fallback is returned instead.
79 */
80 QIcon iconFromDefaultTheme(const char* name, const QIcon& fallback = QIcon());
81 /// Retrieves a pixmap by name
82 QPixmap pixmap(const char* name) const;
83 /** Retrieves a pixmap by name and size created by an
84 * scalable vector graphics (SVG).
85 *
86 * @param colorMapping - a dictionary of substitute colors.
87 * Can be used to customize icon color scheme, e.g. crosshair color
88 */
89 QPixmap pixmapFromSvg(
90 const char* name,
91 const QSizeF& size,
92 const ColorMap& colorMapping = ColorMap()
93 ) const;
94 /** This method is provided for convenience and does the same
95 * as the method above except that it creates the pixmap from
96 * a byte array.
97 * @param colorMapping - see above.
98 */
99 QPixmap pixmapFromSvg(
100 const QByteArray& contents,
101 const QSizeF& size,
102 const ColorMap& colorMapping = ColorMap()
103 ) const;
104 /** Returns the names of all registered pixmaps.
105 * To get the appropriate pixmaps call pixmap() for each name.
106 */
107 QStringList pixmapNames() const;
108 /** Resizes the area of a pixmap
109 * If the new size is greater than the old one the pixmap
110 * will be placed in the center. The border area will be made
111 * depending on \a bgmode transparent or opaque.
112 */
113 QPixmap resize(int w, int h, const QPixmap& p, Qt::BGMode bgmode) const;
114 /** Creates an opaque or transparent area in a pixmap
115 * If the background mode is opaque then this method can
116 * be used for drawing a smaller pixmap into pixmap \a p.
117 * Note: To draw a smaller pixmap into another one the
118 * area in the resulting pixmap for the small pixmapmust
119 * be opaque in every pixel, otherwise the drawing may fail.
120 *
121 * If the background mode is transparent then this method can
122 * be used for resizing the pixmap \a p and make the new space
123 * transparent.
124 */
125 QPixmap fillRect(int x, int y, int w, int h, const QPixmap& p, Qt::BGMode) const;
126 /** Merges the two pixmaps \a p1 and \a p2 to one pixmap in
127 * vertical order if \a vertical is true, in horizontal order
128 * otherwise. The method resizes the resulting pixmap.
129 */
130 QPixmap merge(const QPixmap& p1, const QPixmap& p2, bool vertical) const;
131 /** Merges the two pixmaps \a p1 and \a p2 to one pixmap.
132 * The position of the smaller pixmap \a p2 is drawn into the given
133 * position \a pos of the bigger pixmap \a p1. This method does not
134 * resize the resulting pixmap.
135 */
136 QPixmap merge(const QPixmap& p1, const QPixmap& p2, Position pos = BitmapFactoryInst::BottomLeft) const;
137 /** Creates a disabled pixmap of the given pixmap \a p by changing the brightness
138 * of all opaque pixels to a higher value.
139 */
140 QPixmap disabled(const QPixmap& p) const;
141
142 /** Creates an empty pixmap, takes care of DPI and clearing out the image.
143 */
144 QPixmap empty(QSize size) const;
145
146 /** Converts a QImage into a SoSFImage to use it inside a SoImage node.
147 */
148 void convert(const QImage& img, SoSFImage& out) const;
149 /** Converts a SoSFImage into a QImage.
150 */
151 void convert(const SoSFImage& img, QImage& out) const;
152
153 /// Helper method to merge a pixmap into one corner of a QIcon
154 static QIcon mergePixmap(
155 const QIcon& base,
156 const QPixmap& px,
157 Gui::BitmapFactoryInst::Position position
158 );
159
160 static qreal getMaximumDPR();
161
162 private:
163 bool loadPixmap(const QString& path, QPixmap&) const;
164 void restoreCustomPaths();
165 void configureUseIconTheme();
166
167 static BitmapFactoryInst* _pcSingleton;
168 BitmapFactoryInst();
169 ~BitmapFactoryInst() override;
170
171 BitmapFactoryInstP* d;
172 };
173
174 /// Get the global instance
175 inline BitmapFactoryInst& BitmapFactory()
176 {
177 return BitmapFactoryInst::instance();
178 }
179
180 } // namespace Gui
181
182 #endif // GUI_BITMAPFACTORY_H