new 4475edb243ed4627f4c5f2c470ca40b3def034d4
[tagua/yd.git] / src / loader / image.h
blobb84c1040650fe9aa03ce6d3878d1a17a4e924e78
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@gmail.com>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 */
11 #ifndef LOADER__IMAGE_H
12 #define LOADER__IMAGE_H
14 #include <boost/shared_ptr.hpp>
15 #include <QColor>
16 #include <QMatrix>
17 #include <QBrush>
18 #include <QFont>
19 #include <QImage>
21 class QPainter;
23 /**
24 * @namespace Loader
25 * @brief Namespace holding image (and more generally resource) loading functions
27 namespace Loader {
30 class Context;
33 /**
34 * @class Image <loader/image.h>
35 * @brief The image class to be used with lua
37 class Image {
38 private:
39 QImage m_image;
40 QMatrix m_draw_matrix;
41 double m_draw_opacity;
42 bool m_draw_over;
44 void init_painter(QPainter*);
46 public:
48 /**
49 * Creates an image. Image data will be undefined.
50 * @param width The width of the image.
51 * @param height The height of the image.
53 Image(int width,
54 int height);
57 /**
58 * Creates an image from a file. If the files is not found or could not be
59 * loaded, the resulting image will have width and height equal to 0
60 * @param ctx The current context (theme) reference (should be implicit in lua)
61 * @param file The file name.
63 Image(Context* ctx,
64 const QString& file,
65 bool use_cache = true);
68 /** \return the QImage */
69 QImage image() { return m_image; }
71 /** Returns the width of the image */
72 int width() {
73 return m_image.width();
77 /** Returns the height of the image */
78 int height() {
79 return m_image.height();
83 /**
84 * Sets the transformation matrix
86 void setMatrix(const QMatrix& m) {
87 m_draw_matrix = m;
91 /**
92 * Resets the transformation matrix
94 void resetMatrix() {
95 m_draw_matrix.reset();
99 /**
100 * Scales the transformation matrix
102 void scale(double x, double y) {
103 m_draw_matrix = m_draw_matrix * QMatrix().scale(x,y);
108 * Rotates the transformation matrix
110 void rotate(double angle) {
111 m_draw_matrix = m_draw_matrix * QMatrix().rotate(angle);
116 * Translates the transformation matrix
118 void translate(double x, double y) {
119 m_draw_matrix = m_draw_matrix * QMatrix().translate(x,y);
124 * Returns the transformation matrix
126 QMatrix matrix() {
127 return m_draw_matrix;
132 * Sets the opacity of the drawing operations
134 void setOpacity(double o) {
135 m_draw_opacity = o;
140 * Returns the opacity of the drawing operations
142 double opacity() {
143 return m_draw_opacity;
148 * Sets the composition mode
149 * @param over If true the painting operations will paint over,
150 * if false will replace the destination color.
152 void setPaintOver(bool over) {
153 m_draw_over = over;
158 * Clears the image.
159 * @param color The color to fill the image with (default transparent)
161 void clear(const QColor& color = Qt::transparent);
165 * Fills a rectangle.
166 * @param rect The rectangle
167 * @param brush The fill brush
169 void fillRect(const QRectF& rect,
170 const QBrush& brush);
174 * Draws a line.
175 * @param from The starting point
176 * @param to The end point
177 * @param col The pen color
178 * @param width The pen width
180 void drawLine(const QPointF& from,
181 const QPointF& to,
182 const QColor& col,
183 double width);
187 * Draws an image on the over the current one.
188 * @param dest The destination rectangle.
189 * @param src_img The source image.
190 * @param src The source rectangle.
192 void drawImage(const QRectF& dest,
193 const Image& src_img,
194 const QRectF& src = QRectF(0.0,0.0,0.0,0.0));
198 * Draws an image on the over the current one (overload).
199 * @param ctx The current context (theme) reference (should be implicit in lua)
200 * @param dest The destination rectangle.
201 * @param src_img The source image file.
202 * @param src The source rectangle, WITH RELATIVE COORDINATES 0.0 - 1.0 (of course).
203 * @return True if it was possible to load the image file.
205 bool drawImage(Context* ctx,
206 const QRectF& dest,
207 const QString& src_img,
208 const QRectF& src = QRectF(0.0,0.0,0.0,0.0),
209 bool use_cache = true);
213 * Draws an SVG file over the image.
214 * @param ctx The current context (theme) reference (should be implicit in lua)
215 * @param dest The destination rectangle.
216 * @param file The file to load.
217 * @return True if it was possible to load the SVG file.
218 * TODO: add css support (When Qt will support it)
220 bool drawSVG(Context* ctx,
221 const QRectF& dest,
222 const QString& file);
226 * Draws a font glyph over the image.
227 * @param ctx The current context (theme) reference (should be implicit in lua)
228 * @param dest The destination rectangle.
229 * @param font The font file to load.
230 * @param glyph The unicode glyph code.
231 * @param fg The foreground color.
232 * @param bg The background color.
233 * @param border The background expansion.
234 * @param draw_inner_bg If true the 'inner part' (detected with a flood fill
235 algorithm) will be filled with the background brush.
236 * @return True if it was possible to load the font file and find the glyph.
238 bool drawGlyph(Context* ctx,
239 const QRectF& dest,
240 const QString& font,
241 unsigned int glyph,
242 const QBrush& fg = Qt::black,
243 const QBrush& bg = Qt::white,
244 double border = 0.0,
245 bool draw_inner_bg = true);
249 * Blurs the image itself.
251 void expBlur(double radius);
254 * Returns a shadow image for the current image.
255 * @param radius The shadow radius.
256 * @param color The shadow color.
257 * @param grow How bigger the output image will be.
258 * @param offset Position of the shadow (relatively from being centered in the output).
260 Image createShadow(double radius,
261 const QColor& color,
262 const QPoint& grow = QPoint(),
263 const QPointF& offset = QPointF() );
267 * @class Glyph <loader/image.h>
268 * @brief A simple class that represents a glyph in a font to be used with lua
270 class Glyph {
271 private:
272 friend class Theme;
273 bool m_font_valid;
274 QFont m_font;
275 QString m_char;
276 int m_delta;
278 public:
279 Glyph(const QString& str, int = 0);
280 Glyph(Context* ctx, const QString&, const QChar& c, int = 0);
281 Glyph(const QChar& = QChar());
283 QFont font() const { return m_font; }
284 bool fontValid() const { return m_font_valid; }
285 QString str() const { return m_char; }
286 int delta() const { return m_delta; }
290 typedef boost::shared_ptr<class Font> FontPtr;
292 class Font {
293 private:
294 friend class Image;
295 friend class Glyph;
297 QString m_file;
298 int m_id;
299 QStringList m_families;
300 QFont m_font;
302 public:
303 Font(int id, int size = 128);
304 Font(const QFont& font);
305 ~Font();
307 QFont font() { return m_font; }
308 QStringList families() { return m_families; }
310 static FontPtr create(Context* ctx, const QString& file);
314 } //end namespace Loader
316 #endif //LOADER__IMAGE_H