1 /*
2 * Copyright (C) 2014-2018 Christopho, Solarus - http://www.solarus-games.org
3 *
4 * Solarus Quest Editor is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * Solarus Quest Editor is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17 #include "widgets/gui_tools.h"
18 #include <solarus/gui/gui_tools.h>
19 #include <QMessageBox>
20 #include <QPainter>
21
22 namespace SolarusEditor {
23
24 namespace GuiTools {
25
26 /**
27 * @brief Shows a modal dialog box with an information message.
28 * @param message The message to show.
29 */
information_dialog(const QString & message)30 void information_dialog(const QString& message) {
31
32 SolarusGui::GuiTools::information_dialog(message);
33 }
34
35 /**
36 * @brief Shows a modal dialog box with a warning message.
37 * @param message The message to show.
38 */
warning_dialog(const QString & message)39 void warning_dialog(const QString& message) {
40
41 SolarusGui::GuiTools::warning_dialog(message);
42 }
43
44 /**
45 * @brief Shows a modal dialog box with an error message.
46 * @param message The message to show.
47 */
error_dialog(const QString & message)48 void error_dialog(const QString& message) {
49
50 SolarusGui::GuiTools::error_dialog(message);
51 }
52
53 /**
54 * @brief Draws a rectangle border.
55 *
56 * Unlike QPainter::drawRect(), this function draws the outline entirely
57 * inside the rectangle and does not involve half-pixels.
58 *
59 * @param painter The painter.
60 * @param where Rectangle to draw the outline of.
61 * @param color Color to use.
62 * @param thickness Thickness of the brush.
63 */
draw_rectangle_border(QPainter & painter,const QRect & where,const QColor & color,int thickness)64 void draw_rectangle_border(QPainter& painter,
65 const QRect& where,
66 const QColor& color,
67 int thickness) {
68 const int x = where.x();
69 const int y = where.y();
70 const int w = where.width();
71 const int h = where.height();
72 const int t = thickness;
73 QBrush brush(color);
74 painter.fillRect(QRect( x, y, w, t), brush);
75 painter.fillRect(QRect( x, y + h - t, w, t), brush);
76 painter.fillRect(QRect( x, y, t, h), brush);
77 painter.fillRect(QRect(x + w - t, y, t, h), brush);
78
79 }
80
81 /**
82 * @brief Draws a rectangle border as two black lines and a color between them.
83 *
84 * The black lines will always keep a thickness of one pixel
85 * no matter the transformation set on the painter.
86 *
87 * @param painter The painter.
88 * @param where Rectangle to draw the outline of.
89 * @param color Color to use.
90 * @param thickness Thickness of the brush.
91 */
draw_rectangle_border_double(QPainter & painter,const QRect & where,const QColor & color_between)92 void draw_rectangle_border_double(QPainter& painter,
93 const QRect& where,
94 const QColor& color_between) {
95
96 draw_rectangle_border(painter, where, color_between, 2);
97
98 painter.setPen(QPen(Qt::black, 0, Qt::SolidLine));
99 QRect dst = where;
100 painter.drawRect(dst);
101 dst.adjust(2, 2, -2, -2);
102 painter.drawRect(dst);
103 }
104
105 /**
106 * @brief Draws a grid with dashed lines.
107 *
108 * The lines of the grid will always keep a thickness of one pixel
109 * no matter the transformation set on the painter.
110 *
111 * @param painter The painter to draw.
112 * @param where Rectangle where drawing the grid should be limited to.
113 * @param size Grid size.
114 * @param color Grid color.
115 * @param style Grid style.
116 */
draw_grid(QPainter & painter,const QRect & where,const QSize & size,const QColor & color,GridStyle style)117 void draw_grid(QPainter& painter, const QRect& where,
118 const QSize &size, const QColor& color, GridStyle style) {
119
120 if (style == GridStyle::INTERSECT_POINT) {
121 draw_grid_point(painter, where, size, color);
122 return;
123 }
124
125 QVarLengthArray<QLineF, 100> lines;
126
127 if (style == GridStyle::INTERSECT_CROSS) {
128
129 for (int x = where.left(); x < where.right(); x += size.width()) {
130 for (int y = where.top(); y < where.bottom(); y += size.height()) {
131 lines.append(QLineF(x - 2, y, x + 2, y));
132 lines.append(QLineF(x, y - 2, x, y + 2));
133 }
134 }
135
136 } else {
137
138 for (int x = where.left(); x < where.right(); x += size.width()) {
139 lines.append(QLineF(x, where.top(), x, where.bottom()));
140 }
141 for (int y = where.top(); y < where.bottom(); y += size.height()) {
142 lines.append(QLineF(where.left(), y, where.right(), y));
143 }
144 }
145
146 QPen pen(color, 0);
147 if (style == GridStyle::DASHED) {
148 pen.setStyle(Qt::DashLine);
149 }
150
151 painter.setPen(pen);
152 painter.drawLines(lines.data(), lines.size());
153
154 }
155
156 /**
157 * @brief Draws a grid with points.
158 * @param painter The painter to draw.
159 * @param where Rectangle where drawing the grid should be limited to.
160 * @param size Grid size.
161 * @param color Grid color.
162 */
draw_grid_point(QPainter & painter,const QRect & where,const QSize & size,const QColor & color)163 void draw_grid_point(
164 QPainter& painter, const QRect& where,
165 const QSize &size, const QColor& color) {
166
167 QVarLengthArray<QPointF, 100> points;
168
169 for (int x = where.left(); x < where.right(); x += size.width()) {
170 for (int y = where.top(); y < where.bottom(); y += size.height()) {
171 points.append(QPointF(x, y));
172 }
173 }
174
175 painter.setPen(QPen(color, 1));
176 painter.drawPoints(points.data(), points.size());
177 }
178
179 }
180
181 }
182