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/************************************************************************/
37{
38 struct canvas *store = new canvas;
39
40 store->map_pixmap = QPixmap(width, height);
41 return store;
42}
43
44/************************************************************************/
48void qtg_canvas_free(struct canvas *store)
49{
50 delete store;
51}
52
53/************************************************************************/
56void qtg_canvas_set_zoom(struct canvas *store, float zoom)
57{
58 // Qt-client has no zoom support
59}
60
61/************************************************************************/
65{
66 return FALSE;
67}
68
69/************************************************************************/
73{
74}
75
76/************************************************************************/
79void 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
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/************************************************************************/
101void pixmap_copy(QPixmap *dest, QPixmap *src, int src_x, int src_y,
102 int dest_x, int dest_y, int width, int height)
103{
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/************************************************************************/
120void image_copy(QImage *dest, QImage *src, int src_x, int src_y,
121 int dest_x, int dest_y, int width, int height)
122{
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/************************************************************************/
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/************************************************************************/
164
165/************************************************************************/
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
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/************************************************************************/
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/************************************************************************/
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/************************************************************************/
243
244/************************************************************************/
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/************************************************************************/
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/************************************************************************/
332 enum client_font font, const char *text)
333{
334 QFont *afont;
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/************************************************************************/
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);
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/************************************************************************/
380{
381 QFont *qf;
382 int ssize;
383
384 switch (font) {
385 case FONT_CITY_NAME:
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:
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;
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/************************************************************************/
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}
QRect zealous_crop_rect(QImage &p)
Definition canvas.cpp:419
void qtg_get_text_size(int *width, int *height, enum client_font font, const char *text)
Definition canvas.cpp:331
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
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
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
bool qtg_has_zoom_support()
Definition canvas.cpp:64
void qtg_canvas_put_sprite_full(struct canvas *pcanvas, int canvas_x, int canvas_y, struct sprite *sprite)
Definition canvas.cpp:154
void qtg_canvas_free(struct canvas *store)
Definition canvas.cpp:48
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
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
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
struct canvas * qtg_canvas_create(int width, int height)
Definition canvas.cpp:36
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
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
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
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
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
void qtg_canvas_set_zoom(struct canvas *store, float zoom)
Definition canvas.cpp:56
void qtg_canvas_mapview_init(struct canvas *store)
Definition canvas.cpp:72
static QFont * get_font(enum client_font font)
Definition canvas.cpp:379
struct canvas int int struct sprite int int int int height
Definition canvas_g.h:44
struct canvas int int int int struct sprite *sprite struct canvas struct color * pcolor
Definition canvas_g.h:56
struct canvas int int canvas_y
Definition canvas_g.h:43
struct canvas int canvas_x
Definition canvas_g.h:43
canvas_put_sprite
Definition canvas_g.h:42
struct canvas int int int canvas_w
Definition canvas_g.h:49
struct canvas int int int int canvas_h
Definition canvas_g.h:49
struct canvas * pcanvas
Definition canvas_g.h:42
struct canvas int int struct sprite int int offset_y
Definition canvas_g.h:44
struct canvas int int struct sprite int offset_x
Definition canvas_g.h:44
struct canvas int int struct sprite int int int width
Definition canvas_g.h:44
line_type
Definition canvas_g.h:25
@ LINE_GOTO
Definition canvas_g.h:26
@ LINE_TILE_FRAME
Definition canvas_g.h:26
@ LINE_BORDER
Definition canvas_g.h:26
@ LINE_NORMAL
Definition canvas_g.h:26
static fc_font * instance()
Definition fonts.cpp:41
QFont * get_font(QString name)
Definition fonts.cpp:63
char * incite_cost
Definition comments.c:75
#define fc_malloc(sz)
Definition mem.h:34
static mpgui * gui
Definition mpgui_qt.cpp:52
const char *const city_productions
Definition fonts.h:33
const char *const city_names
Definition fonts.h:32
const char *const reqtree_text
Definition fonts.h:34
struct sprite int int y
Definition sprite_g.h:31
struct sprite int x
Definition sprite_g.h:31
struct sprite int int int int struct sprite int int float bool smooth get_sprite_dimensions
Definition sprite_g.h:36
float zoom
Definition canvas.h:25
QPixmap map_pixmap
Definition canvas.h:25
Definition colors.h:21
QPixmap * pm
Definition sprite.h:25
#define FALSE
Definition support.h:47