1 /**********************************************************************
2 Freeciv - Copyright (C) 1996-2005 - Freeciv Development Team
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 ***********************************************************************/
15 #include <fc_config.h>
19 #include <QFontMetrics>
25 #include "fc_client.h"
27 #include "qtg_cxxside.h"
30 static QFont
*get_font(enum client_font font
);
31 /****************************************************************************
32 Create a canvas of the given size.
33 ****************************************************************************/
34 struct canvas
*qtg_canvas_create(int width
, int height
)
36 struct canvas
*store
= new canvas
;
38 store
->map_pixmap
= QPixmap(width
, height
);
42 /****************************************************************************
43 Free any resources associated with this canvas and the canvas struct
45 ****************************************************************************/
46 void qtg_canvas_free(struct canvas
*store
)
51 /****************************************************************************
52 Set canvas zoom for future drawing operations.
53 ****************************************************************************/
54 void qtg_canvas_set_zoom(struct canvas
*store
, float zoom
)
56 /* Qt-client has no zoom support */
59 /****************************************************************************
60 This gui has zoom support.
61 ****************************************************************************/
62 bool qtg_has_zoom_support()
67 /****************************************************************************
68 Copies an area from the source canvas to the destination canvas.
69 ****************************************************************************/
70 void qtg_canvas_copy(struct canvas
*dest
, struct canvas
*src
,
71 int src_x
, int src_y
, int dest_x
, int dest_y
, int width
,
75 QRectF
source_rect(src_x
, src_y
, width
, height
);
76 QRectF
dest_rect(dest_x
, dest_y
, width
, height
);
79 if (!width
|| !height
) {
83 p
.begin(&dest
->map_pixmap
);
84 p
.drawPixmap(dest_rect
, src
->map_pixmap
, source_rect
);
89 /****************************************************************************
90 Copies an area from the source pixmap to the destination pixmap.
91 ****************************************************************************/
92 void pixmap_copy(QPixmap
*dest
, QPixmap
*src
, int src_x
, int src_y
,
93 int dest_x
, int dest_y
, int width
, int height
)
95 QRectF
source_rect(src_x
, src_y
, width
, height
);
96 QRectF
dest_rect(dest_x
, dest_y
, width
, height
);
99 if (!width
|| !height
) {
104 p
.drawPixmap(dest_rect
, *src
, source_rect
);
108 /****************************************************************************
109 Copies an area from the source image to the destination image.
110 ****************************************************************************/
111 void image_copy(QImage
*dest
, QImage
*src
, int src_x
, int src_y
,
112 int dest_x
, int dest_y
, int width
, int height
)
114 QRectF
source_rect(src_x
, src_y
, width
, height
);
115 QRectF
dest_rect(dest_x
, dest_y
, width
, height
);
118 if (!width
|| !height
) {
123 p
.drawImage(dest_rect
, *src
, source_rect
);
127 /****************************************************************************
128 Draw some or all of a sprite onto the canvas.
129 ****************************************************************************/
130 void qtg_canvas_put_sprite(struct canvas
*pcanvas
,
131 int canvas_x
, int canvas_y
,
132 struct sprite
*sprite
,
133 int offset_x
, int offset_y
, int width
, int height
)
137 p
.begin(&pcanvas
->map_pixmap
);
138 p
.drawPixmap(canvas_x
, canvas_y
, *sprite
->pm
, offset_x
, offset_y
, width
, height
);
142 /****************************************************************************
143 Draw a full sprite onto the canvas.
144 ****************************************************************************/
145 void qtg_canvas_put_sprite_full(struct canvas
*pcanvas
,
146 int canvas_x
, int canvas_y
,
147 struct sprite
*sprite
)
151 get_sprite_dimensions(sprite
, &width
, &height
);
152 canvas_put_sprite(pcanvas
, canvas_x
, canvas_y
, sprite
,
153 0, 0, width
, height
);
156 /****************************************************************************
157 Draw a full sprite onto the canvas. If "fog" is specified draw it with
159 ****************************************************************************/
160 void qtg_canvas_put_sprite_fogged(struct canvas
*pcanvas
,
161 int canvas_x
, int canvas_y
,
162 struct sprite
*psprite
,
163 bool fog
, int fog_x
, int fog_y
)
167 p
.begin(&pcanvas
->map_pixmap
);
168 p
.setCompositionMode(QPainter::CompositionMode_Difference
);
170 p
.drawPixmap(canvas_x
, canvas_y
, *psprite
->pm
);
174 /****************************************************************************
175 Draw a filled-in colored rectangle onto canvas.
176 ****************************************************************************/
177 void qtg_canvas_put_rectangle(struct canvas
*pcanvas
,
178 struct color
*pcolor
,
179 int canvas_x
, int canvas_y
,
180 int width
, int height
)
183 QBrush
brush(pcolor
->qcolor
);
184 QPen
pen(pcolor
->qcolor
);
187 p
.begin(&pcanvas
->map_pixmap
);
190 if (width
== 1 && height
== 1) {
191 p
.drawPoint(canvas_x
, canvas_y
);
192 } else if (width
== 1) {
193 p
.drawLine(canvas_x
, canvas_y
, canvas_x
, canvas_y
+ height
-1);
194 } else if (height
== 1) {
195 p
.drawLine(canvas_x
, canvas_y
, canvas_x
+ width
- 1, canvas_y
);
197 p
.drawRect(canvas_x
, canvas_y
, width
, height
);
203 /****************************************************************************
204 Fill the area covered by the sprite with the given color.
205 ****************************************************************************/
206 void qtg_canvas_fill_sprite_area(struct canvas
*pcanvas
,
207 struct sprite
*psprite
, struct color
*pcolor
,
208 int canvas_x
, int canvas_y
)
212 get_sprite_dimensions(psprite
, &width
, &height
);
213 qtg_canvas_put_rectangle(pcanvas
, pcolor
, canvas_x
, canvas_y
, width
, height
);
216 /****************************************************************************
217 Draw a 1-pixel-width colored line onto the canvas.
218 ****************************************************************************/
219 void qtg_canvas_put_line(struct canvas
*pcanvas
, struct color
*pcolor
,
220 enum line_type ltype
, int start_x
, int start_y
,
226 pen
.setColor(pcolor
->qcolor
);
233 pen
.setStyle(Qt::DashLine
);
234 pen
.setDashOffset(4);
237 case LINE_TILE_FRAME
:
248 p
.begin(&pcanvas
->map_pixmap
);
250 p
.setRenderHint(QPainter::Antialiasing
);
251 p
.drawLine(start_x
, start_y
, start_x
+ dx
, start_y
+ dy
);
255 /****************************************************************************
256 Draw a 1-pixel-width colored curved line onto the canvas.
257 ****************************************************************************/
258 void qtg_canvas_put_curved_line(struct canvas
*pcanvas
, struct color
*pcolor
,
259 enum line_type ltype
, int start_x
, int start_y
,
263 pen
.setColor(pcolor
->qcolor
);
272 pen
.setStyle(Qt::DashLine
);
273 pen
.setDashOffset(4);
276 case LINE_TILE_FRAME
:
287 p
.begin(&pcanvas
->map_pixmap
);
288 p
.setRenderHints(QPainter::Antialiasing
);
291 path
.moveTo(start_x
, start_y
);
292 path
.cubicTo(start_x
+ dx
/ 2, start_y
, start_x
, start_y
+ dy
/ 2,
293 start_x
+ dx
, start_y
+ dy
);
298 /****************************************************************************
299 Return the size of the given text in the given font. This size should
300 include the ascent and descent of the text. Either of width or height
301 may be NULL in which case those values simply shouldn't be filled out.
302 ****************************************************************************/
303 void qtg_get_text_size(int *width
, int *height
,
304 enum client_font font
, const char *text
)
309 afont
= get_font(font
);
310 fm
= new QFontMetrics(*afont
);
312 *width
= fm
->width(QString::fromUtf8(text
));
316 *height
= fm
->height();
321 /****************************************************************************
322 Draw the text onto the canvas in the given color and font. The canvas
323 position does not account for the ascent of the text; this function must
324 take care of this manually. The text will not be NULL but may be empty.
325 ****************************************************************************/
326 void qtg_canvas_put_text(struct canvas
*pcanvas
, int canvas_x
, int canvas_y
,
327 enum client_font font
, struct color
*pcolor
,
333 QColor
color(pcolor
->qcolor
);
336 afont
= get_font(font
);
338 fm
= new QFontMetrics(*afont
);
340 p
.begin(&pcanvas
->map_pixmap
);
343 p
.drawText(canvas_x
, canvas_y
+ fm
->ascent(), QString::fromUtf8(text
));
348 /****************************************************************************
350 ****************************************************************************/
351 QFont
*get_font(client_font font
)
358 qf
= fc_font::instance()->get_font(fonts::city_names
);
359 if (gui()->map_scale
!= 1.0f
) {
360 ssize
= ceil(gui()->map_scale
* fc_font::instance()->city_fontsize
);
361 if (qf
->pointSize() != ssize
) {
362 qf
->setPointSize(ssize
);
367 qf
= fc_font::instance()->get_font(fonts::city_productions
);
368 if (gui()->map_scale
!= 1.0f
) {
369 ssize
= ceil(gui()->map_scale
* fc_font::instance()->prod_fontsize
);
370 if (qf
->pointSize() != ssize
) {
371 qf
->setPointSize(ssize
);
375 case FONT_REQTREE_TEXT
:
376 qf
= fc_font::instance()->get_font(fonts::reqtree_text
);
388 /****************************************************************************
389 Return rectangle containing pure image (crops transparency)
390 ****************************************************************************/
391 QRect
zealous_crop_rect(QImage
&p
)
402 for (int y
= 0; y
< oh
; y
++) {
404 bool row_filled
= false;
407 /* Copy to a location with guaranteed QRgb suitable alignment.
408 * That fixes clang compiler warning. */
409 memcpy(row
, p
.scanLine(y
), ow
* sizeof(QRgb
));
411 for (x
= 0; x
< ow
; ++x
) {
412 if (qAlpha(row
[x
])) {
426 return QRect(l
, t
, r
- l
, b
- t
);