Freeciv-3.2: /fast/freeciv/nightly/S3_2/client/gui-qt/canvas.cpp Source File

Freeciv-3.2

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