Freeciv-3.1: /fast/freeciv/nightly/S3_1/client/gui-qt/canvas.cpp Source File

Freeciv-3.1

Loading...
Searching...
No Matches
canvas.cpp
Go to the documentation of this file.
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)
6 any later version.
7
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***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18// Qt
19#include <QFontMetrics>
20#include <QPainter>
21#include <QPainterPath>
22
23// qt-client
24#include "canvas.h"
25#include "colors.h"
26#include "fc_client.h"
27#include "fonts.h"
28#include "qtg_cxxside.h"
29#include "sprite.h"
30
31static QFont *get_font(enum client_font font);
32/************************************************************************/
35 struct canvas *qtg_canvas_create(int width, int height)
36{
37 struct canvas *store = new canvas;
38
39 store->map_pixmap = QPixmap(width, height);
40 return store;
41}
42
43/************************************************************************/
47 void qtg_canvas_free(struct canvas *store)
48{
49 delete store;
50}
51
52/************************************************************************/
55 void qtg_canvas_set_zoom(struct canvas *store, float zoom)
56{
57 /* Qt-client has no zoom support */
58}
59
60/************************************************************************/
63 bool qtg_has_zoom_support()
64{
65 return FALSE;
66}
67
68/************************************************************************/
71 void qtg_canvas_mapview_init(struct canvas *store)
72{
73}
74
75/************************************************************************/
78 void qtg_canvas_copy(struct canvas *dest, struct canvas *src,
79 int src_x, int src_y, int dest_x, int dest_y, int width,
80 int height)
81{
82
83 QRectF source_rect(src_x, src_y, width, height);
84 QRectF dest_rect(dest_x, dest_y, width, height);
85 QPainter p;
86
87 if (!width || !height) {
88 return;
89 }
90
91 p.begin(&dest->map_pixmap);
92 p.drawPixmap(dest_rect, src->map_pixmap, source_rect);
93 p.end();
94
95}
96
97/************************************************************************/
100 void pixmap_copy(QPixmap *dest, QPixmap *src, int src_x, int src_y,
101 int dest_x, int dest_y, int width, int height)
102{
103 QRectF source_rect(src_x, src_y, width, height);
104 QRectF dest_rect(dest_x, dest_y, width, height);
105 QPainter p;
106
107 if (!width || !height) {
108 return;
109 }
110
111 p.begin(dest);
112 p.drawPixmap(dest_rect, *src, source_rect);
113 p.end();
114}
115
116/************************************************************************/
119 void image_copy(QImage *dest, QImage *src, int src_x, int src_y,
120 int dest_x, int dest_y, int width, int height)
121{
122 QRectF source_rect(src_x, src_y, width, height);
123 QRectF dest_rect(dest_x, dest_y, width, height);
124 QPainter p;
125
126 if (!width || !height) {
127 return;
128 }
129
130 p.begin(dest);
131 p.drawImage(dest_rect, *src, source_rect);
132 p.end();
133}
134
135/************************************************************************/
138 void qtg_canvas_put_sprite(struct canvas *pcanvas,
139 int canvas_x, int canvas_y,
140 struct sprite *sprite,
141 int offset_x, int offset_y, int width, int height)
142{
143 QPainter p;
144
145 p.begin(&pcanvas->map_pixmap);
146 p.drawPixmap(canvas_x, canvas_y, *sprite->pm, offset_x, offset_y, width, height);
147 p.end();
148}
149
150/************************************************************************/
153 void qtg_canvas_put_sprite_full(struct canvas *pcanvas,
154 int canvas_x, int canvas_y,
155 struct sprite *sprite)
156{
157 int width, height;
158
159 get_sprite_dimensions(sprite, &width, &height);
160 canvas_put_sprite(pcanvas, canvas_x, canvas_y, sprite,
161 0, 0, width, height);
162}
163
164/************************************************************************/
168 void qtg_canvas_put_sprite_fogged(struct canvas *pcanvas,
169 int canvas_x, int canvas_y,
170 struct sprite *psprite,
171 bool fog, int fog_x, int fog_y)
172{
173 QPainter p;
174
175 p.begin(&pcanvas->map_pixmap);
176 p.setCompositionMode(QPainter::CompositionMode_Difference);
177 p.setOpacity(0.5);
178 p.drawPixmap(canvas_x, canvas_y, *psprite->pm);
179 p.end();
180}
181
182/************************************************************************/
185 void qtg_canvas_put_rectangle(struct canvas *pcanvas,
186 struct color *pcolor,
187 int canvas_x, int canvas_y,
188 int width, int height)
189{
190
191 QBrush brush(pcolor->qcolor);
192 QPen pen(pcolor->qcolor);
193 QPainter p;
194
195 p.begin(&pcanvas->map_pixmap);
196 p.setPen(pen);
197 p.setBrush(brush);
198 if (width == 1 && height == 1) {
199 p.drawPoint(canvas_x, canvas_y);
200 } else if (width == 1) {
201 p.drawLine(canvas_x, canvas_y, canvas_x, canvas_y + height -1);
202 } else if (height == 1) {
203 p.drawLine(canvas_x, canvas_y, canvas_x + width - 1, canvas_y);
204 } else {
205 p.drawRect(canvas_x, canvas_y, width, height);
206 }
207
208 p.end();
209}
210
211/************************************************************************/
214 void qtg_canvas_fill_sprite_area(struct canvas *pcanvas,
215 struct sprite *psprite, struct color *pcolor,
216 int canvas_x, int canvas_y)
217{
218 int width, height;
219
220 get_sprite_dimensions(psprite, &width, &height);
221 qtg_canvas_put_rectangle(pcanvas, pcolor, canvas_x, canvas_y, width, height);
222}
223
224/************************************************************************/
227 void qtg_canvas_put_line(struct canvas *pcanvas, struct color *pcolor,
228 enum line_type ltype, int start_x, int start_y,
229 int dx, int dy)
230{
231 QPen pen;
232 QPainter p;
233
234 pen.setColor(pcolor->qcolor);
235
236 switch (ltype) {
237 case LINE_NORMAL:
238 pen.setWidth(1);
239 break;
240 case LINE_BORDER:
241 pen.setStyle(Qt::DashLine);
242 pen.setDashOffset(4);
243 pen.setWidth(1);
244 break;
245 case LINE_TILE_FRAME:
246 pen.setWidth(2);
247 break;
248 case LINE_GOTO:
249 pen.setWidth(2);
250 break;
251 default:
252 pen.setWidth(1);
253 break;
254 }
255
256 p.begin(&pcanvas->map_pixmap);
257 p.setPen(pen);
258 p.setRenderHint(QPainter::Antialiasing);
259 p.drawLine(start_x, start_y, start_x + dx, start_y + dy);
260 p.end();
261}
262
263/************************************************************************/
266 void qtg_canvas_put_curved_line(struct canvas *pcanvas, struct color *pcolor,
267 enum line_type ltype, int start_x, int start_y,
268 int dx, int dy)
269{
270 QPen pen;
271 pen.setColor(pcolor->qcolor);
272 QPainter p;
273 QPainterPath path;
274
275 switch (ltype) {
276 case LINE_NORMAL:
277 pen.setWidth(1);
278 break;
279 case LINE_BORDER:
280 pen.setStyle(Qt::DashLine);
281 pen.setDashOffset(4);
282 pen.setWidth(2);
283 break;
284 case LINE_TILE_FRAME:
285 pen.setWidth(2);
286 break;
287 case LINE_GOTO:
288 pen.setWidth(2);
289 break;
290 default:
291 pen.setWidth(1);
292 break;
293 }
294
295 p.begin(&pcanvas->map_pixmap);
296 p.setRenderHints(QPainter::Antialiasing);
297 p.setPen(pen);
298
299 path.moveTo(start_x, start_y);
300 path.cubicTo(start_x + dx / 2, start_y, start_x, start_y + dy / 2,
301 start_x + dx, start_y + dy);
302 p.drawPath(path);
303 p.end();
304}
305
306/************************************************************************/
311 void qtg_get_text_size(int *width, int *height,
312 enum client_font font, const char *text)
313{
314 QFont *afont;
315 QFontMetrics *fm;
316
317 afont = get_font(font);
318 fm = new QFontMetrics(*afont);
319 if (width) {
320 *width = fm->horizontalAdvance(QString::fromUtf8(text));
321 }
322
323 if (height) {
324 *height = fm->height();
325 }
326 delete fm;
327}
328
329/************************************************************************/
334 void qtg_canvas_put_text(struct canvas *pcanvas, int canvas_x, int canvas_y,
335 enum client_font font, struct color *pcolor,
336 const char *text)
337{
338 QPainter p;
339 QPen pen;
340 QFont *afont;
341 QColor color(pcolor->qcolor);
342 QFontMetrics *fm;
343
344 afont = get_font(font);
345 pen.setColor(color);
346 fm = new QFontMetrics(*afont);
347
348 p.begin(&pcanvas->map_pixmap);
349 p.setPen(pen);
350 p.setFont(*afont);
351 p.drawText(canvas_x, canvas_y + fm->ascent(), QString::fromUtf8(text));
352 p.end();
353 delete fm;
354}
355
356/************************************************************************/
359QFont *get_font(client_font font)
360{
361 QFont *qf;
362 int ssize;
363
364 switch (font) {
365 case FONT_CITY_NAME:
366 qf = fc_font::instance()->get_font(fonts::city_names);
367 if (gui()->map_scale != 1.0f && gui()->map_font_scale) {
368 ssize = ceil(gui()->map_scale * fc_font::instance()->city_fontsize);
369 if (qf->pointSize() != ssize) {
370 qf->setPointSize(ssize);
371 }
372 }
373 break;
374 case FONT_CITY_PROD:
375 qf = fc_font::instance()->get_font(fonts::city_productions);
376 if (gui()->map_scale != 1.0f && gui()->map_font_scale) {
377 ssize = ceil(gui()->map_scale * fc_font::instance()->prod_fontsize);
378 if (qf->pointSize() != ssize) {
379 qf->setPointSize(ssize);
380 }
381 }
382 break;
383 case FONT_REQTREE_TEXT:
384 qf = fc_font::instance()->get_font(fonts::reqtree_text);
385 break;
386 case FONT_COUNT:
387 qf = NULL;
388 break;
389 default:
390 qf = NULL;
391 break;
392 }
393 return qf;
394}
395
396/************************************************************************/
399QRect zealous_crop_rect(QImage &p)
400{
401 int r, t, b, l;
402 int oh, ow;
403 QRgb *row;
404
405 ow = p.width();
406 l = ow;
407 r = 0;
408 oh = p.height();
409 t = oh;
410 b = 0;
411
412 row = (QRgb *)fc_malloc(sizeof(QRgb) * ow);
413
414 for (int y = 0; y < oh; y++) {
415 bool row_filled = false;
416 int x;
417
418 /* Copy to a location with guaranteed QRgb suitable alignment.
419 * That fixes clang compiler warning. */
420 memcpy(row, p.scanLine(y), ow * sizeof(QRgb));
421
422 for (x = 0; x < ow; ++x) {
423 if (qAlpha(row[x])) {
424 row_filled = true;
425 r = qMax(r, x);
426 if (l > x) {
427 l = x;
428 x = r;
429 }
430 }
431 }
432 if (row_filled) {
433 t = qMin(t, y);
434 b = y;
435 }
436 }
437
438 free(row);
439
440 return QRect(l, t, qMax(0, r - l + 1), qMax(0, b - t + 1));
441}
zealous_crop_rect
QRect zealous_crop_rect(QImage &p)
Definition canvas.cpp:399
qtg_get_text_size
void qtg_get_text_size(int *width, int *height, enum client_font font, const char *text)
Definition canvas.cpp:311
image_copy
void image_copy(QImage *dest, QImage *src, int src_x, int src_y, int dest_x, int dest_y, int width, int height)
Definition canvas.cpp:119
qtg_canvas_fill_sprite_area
void qtg_canvas_fill_sprite_area(struct canvas *pcanvas, struct sprite *psprite, struct color *pcolor, int canvas_x, int canvas_y)
Definition canvas.cpp:214
qtg_canvas_put_text
void qtg_canvas_put_text(struct canvas *pcanvas, int canvas_x, int canvas_y, enum client_font font, struct color *pcolor, const char *text)
Definition canvas.cpp:334
qtg_has_zoom_support
bool qtg_has_zoom_support()
Definition canvas.cpp:63
qtg_canvas_put_sprite_full
void qtg_canvas_put_sprite_full(struct canvas *pcanvas, int canvas_x, int canvas_y, struct sprite *sprite)
Definition canvas.cpp:153
qtg_canvas_free
void qtg_canvas_free(struct canvas *store)
Definition canvas.cpp:47
qtg_canvas_put_curved_line
void qtg_canvas_put_curved_line(struct canvas *pcanvas, struct color *pcolor, enum line_type ltype, int start_x, int start_y, int dx, int dy)
Definition canvas.cpp:266
qtg_canvas_put_line
void qtg_canvas_put_line(struct canvas *pcanvas, struct color *pcolor, enum line_type ltype, int start_x, int start_y, int dx, int dy)
Definition canvas.cpp:227
qtg_canvas_create
struct canvas * qtg_canvas_create(int width, int height)
Definition canvas.cpp:35
qtg_canvas_put_sprite
void qtg_canvas_put_sprite(struct canvas *pcanvas, int canvas_x, int canvas_y, struct sprite *sprite, int offset_x, int offset_y, int width, int height)
Definition canvas.cpp:138
qtg_canvas_copy
void qtg_canvas_copy(struct canvas *dest, struct canvas *src, int src_x, int src_y, int dest_x, int dest_y, int width, int height)
Definition canvas.cpp:78
qtg_canvas_put_rectangle
void qtg_canvas_put_rectangle(struct canvas *pcanvas, struct color *pcolor, int canvas_x, int canvas_y, int width, int height)
Definition canvas.cpp:185
qtg_canvas_put_sprite_fogged
void qtg_canvas_put_sprite_fogged(struct canvas *pcanvas, int canvas_x, int canvas_y, struct sprite *psprite, bool fog, int fog_x, int fog_y)
Definition canvas.cpp:168
pixmap_copy
void pixmap_copy(QPixmap *dest, QPixmap *src, int src_x, int src_y, int dest_x, int dest_y, int width, int height)
Definition canvas.cpp:100
qtg_canvas_set_zoom
void qtg_canvas_set_zoom(struct canvas *store, float zoom)
Definition canvas.cpp:55
qtg_canvas_mapview_init
void qtg_canvas_mapview_init(struct canvas *store)
Definition canvas.cpp:71
get_font
static QFont * get_font(enum client_font font)
Definition canvas.cpp:359
height
struct canvas int int struct sprite int int int int height
Definition canvas_g.h:44
pcolor
struct canvas int int struct sprite bool int int fog_y struct canvas struct sprite struct color * pcolor
Definition canvas_g.h:57
fog_x
struct canvas int int struct sprite bool int fog_x
Definition canvas_g.h:51
FONT_REQTREE_TEXT
FONT_REQTREE_TEXT
Definition canvas_g.h:72
canvas_y
struct canvas int int canvas_y
Definition canvas_g.h:43
canvas_x
struct canvas int canvas_x
Definition canvas_g.h:43
FONT_CITY_NAME
FONT_CITY_NAME
Definition canvas_g.h:70
canvas_put_sprite
canvas_put_sprite
Definition canvas_g.h:42
FONT_CITY_PROD
FONT_CITY_PROD
Definition canvas_g.h:71
client_font
struct canvas int int struct sprite bool int int fog_y struct canvas struct sprite struct color int int canvas_y struct canvas struct color enum line_type ltype int start_x int start_y int dx int dy enum client_font
Definition canvas_g.h:69
width
struct canvas int int struct sprite int int int width
Definition canvas_g.h:44
pcanvas
struct canvas * pcanvas
Definition canvas_g.h:42
psprite
struct canvas int int struct sprite * psprite
Definition canvas_g.h:50
offset_y
struct canvas int int struct sprite int int offset_y
Definition canvas_g.h:44
offset_x
struct canvas int int struct sprite int offset_x
Definition canvas_g.h:44
line_type
line_type
Definition canvas_g.h:25
LINE_GOTO
@ LINE_GOTO
Definition canvas_g.h:26
LINE_TILE_FRAME
@ LINE_TILE_FRAME
Definition canvas_g.h:26
LINE_BORDER
@ LINE_BORDER
Definition canvas_g.h:26
LINE_NORMAL
@ LINE_NORMAL
Definition canvas_g.h:26
fog
struct canvas int int struct sprite bool fog
Definition canvas_g.h:51
fc_font::instance
static fc_font * instance()
Definition fonts.cpp:41
fc_font::get_font
QFont * get_font(QString name)
Definition fonts.cpp:63
fc_client.h
fonts.h
get_sprite_dimensions
void get_sprite_dimensions(struct sprite *sprite, int *width, int *height)
Definition sprite.c:107
canvas.h
colors.h
sprite.h
fc_malloc
#define fc_malloc(sz)
Definition mem.h:34
gui
static mpgui * gui
Definition mpgui_qt.cpp:52
fonts::city_productions
const char *const city_productions
Definition fonts.h:33
fonts::city_names
const char *const city_names
Definition fonts.h:32
fonts::reqtree_text
const char *const reqtree_text
Definition fonts.h:34
qtg_cxxside.h
canvas
Definition canvas.h:21
canvas::zoom
float zoom
Definition canvas.h:24
canvas::map_pixmap
QPixmap map_pixmap
Definition canvas.h:25
color
Definition colors.h:20
sprite
Definition sprite.h:22
sprite::pm
QPixmap * pm
Definition sprite.h:25
FALSE
#define FALSE
Definition support.h:47