Removed lagacy code for qt < 4.2
[tagua/yd.git] / src / loader / image.h
blob598b1efa973661cac69e583eff40733e3a9ea492
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@sns.it>
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 <QColor>
15 #include <QMatrix>
16 #include <QBrush>
17 #include <QFont>
18 #include <QImage>
20 class QPainter;
22 /**
23 * @namespace Loader
24 * @brief Namespace holding image (and more generally resource) loading functions
26 namespace Loader {
29 class Context;
32 /**
33 * @class Image <loader/image.h>
34 * @brief The image class to be used with lua
36 class Image {
37 public:
38 QImage m_image;
39 QMatrix m_draw_matrix;
40 double m_draw_opacity;
41 bool m_draw_over;
43 void init_painter(QPainter*);
45 public:
47 /**
48 * Creates an image. Image data will be undefined.
49 * @param width The width of the image.
50 * @param height The height of the image.
52 Image(int width,
53 int height);
56 /**
57 * Creates an image from a file. If the files is not found or could not be
58 * loaded, the resulting image will have width and height equal to 0
59 * @param ctx The current context (theme) reference (should be implicit in lua)
60 * @param file The file name.
62 Image(Context* ctx,
63 const QString& file);
66 /** Returns the width of the image */
67 int width() {
68 return m_image.width();
72 /** Returns the height of the image */
73 int height() {
74 return m_image.height();
78 /**
79 * Sets the transformation matrix
81 void setMatrix(const QMatrix& m) {
82 m_draw_matrix = m;
86 /**
87 * Resets the transformation matrix
89 void resetMatrix() {
90 m_draw_matrix.reset();
94 /**
95 * Scales the transformation matrix
97 void scale(double x, double y) {
98 m_draw_matrix = m_draw_matrix * QMatrix().scale(x,y);
103 * Rotates the transformation matrix
105 void rotate(double angle) {
106 m_draw_matrix = m_draw_matrix * QMatrix().rotate(angle);
111 * Translates the transformation matrix
113 void translate(double x, double y) {
114 m_draw_matrix = m_draw_matrix * QMatrix().translate(x,y);
119 * Returns the transformation matrix
121 QMatrix matrix() {
122 return m_draw_matrix;
127 * Sets the opacity of the drawing operations
129 void setOpacity(double o) {
130 m_draw_opacity = o;
135 * Returns the opacity of the drawing operations
137 double opacity() {
138 return m_draw_opacity;
143 * Sets the composition mode
144 * @param over If true the painting operations will paint over,
145 * if false will replace the destination color.
147 void setPaintOver(bool over) {
148 m_draw_over = over;
153 * Clears the image.
154 * @param color The color to fill the image with (default transparent)
156 void clear(const QColor& color = Qt::transparent);
160 * Fills a rectangle.
161 * @param rect The rectangle
162 * @param brush The fill brush
164 void fillRect(const QRectF& rect,
165 const QBrush& brush);
169 * Draws a line.
170 * @param from The starting point
171 * @param to The end point
172 * @param col The pen color
173 * @param width The pen width
175 void drawLine(const QPointF& from,
176 const QPointF& to,
177 const QColor& col,
178 double width);
182 * Draws an image on the over the current one.
183 * @param dest The destination rectangle.
184 * @param src_img The source image.
185 * @param src The source rectangle.
187 void drawImage(const QRectF& dest,
188 const Image& src_img,
189 const QRectF& src = QRectF(0.0,0.0,0.0,0.0));
193 * Draws an image on the over the current one (overload).
194 * @param ctx The current context (theme) reference (should be implicit in lua)
195 * @param dest The destination rectangle.
196 * @param src_img The source image file.
197 * @param src The source rectangle, WITH RELATIVE COORDINATES 0.0 - 1.0 (of course).
198 * @return True if it was possible to load the image file.
200 bool drawImage(Context* ctx,
201 const QRectF& dest,
202 const QString& src_img,
203 const QRectF& src = QRectF(0.0,0.0,0.0,0.0));
207 * Draws an SVG file over the image.
208 * @param ctx The current context (theme) reference (should be implicit in lua)
209 * @param dest The destination rectangle.
210 * @param file The file to load.
211 * @return True if it was possible to load the SVG file.
212 * TODO: add css support (When Qt will support it)
214 bool drawSVG(Context* ctx,
215 const QRectF& dest,
216 const QString& file);
220 * Draws a font glyph over the image.
221 * @param ctx The current context (theme) reference (should be implicit in lua)
222 * @param dest The destination rectangle.
223 * @param font The font file to load.
224 * @param glyph The unicode glyph code.
225 * @param fg The foreground color.
226 * @param bg The background color.
227 * @param border The background expansion.
228 * @param draw_inner_bg If true the 'inner part' (detected with a flood fill
229 algorithm) will be filled with the background brush.
230 * @return True if it was possible to load the font file and find the glyph.
232 bool drawGlyph(Context* ctx,
233 const QRectF& dest,
234 const QString& font,
235 unsigned int glyph,
236 const QBrush& fg = Qt::black,
237 const QBrush& bg = Qt::white,
238 double border = 0.0,
239 bool draw_inner_bg = true);
242 * Returns a shadow image for the current image.
243 * @param radius The shadow radius.
244 * @param color The shadow color.
245 * @param grow How bigger the output image will be.
246 * @param offset Position of the shadow (relatively from being centered in the output).
248 Image createShadow(double radius,
249 const QColor& color,
250 const QPoint& grow = QPoint(),
251 const QPointF& offset = QPointF() );
255 * @class Glyph <loader/image.h>
256 * @brief A simple class that represents a glyph in a font to be used with lua
258 class Glyph {
259 public:
260 bool m_font_valid;
261 QFont m_font;
262 QChar m_char;
263 int m_delta;
264 Glyph(Context* ctx, const QString&, QChar, int = 0);
265 Glyph(QChar = QChar());
268 } //end namespace Loader
270 #endif //LOADER__IMAGE_H