1 /**********************************************************************************************
2 Copyright (C) 2014 Oliver Eichler <oliver.eichler@gmx.de>
3 Copyright (C) 2015 Christian Eichler <code@christian-eichler.de>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 **********************************************************************************************/
19
20 #ifndef CPAINTER_H
21 #define CPAINTER_H
22
23 #include <QPainter>
24 #include <QPolygonF>
25 #include <QRectF>
26
27 #include "CMainWindow.h"
USE_ANTI_ALIASING(QPainter & p,bool useAntiAliasing)28 inline void USE_ANTI_ALIASING(QPainter& p, bool useAntiAliasing)
29 {
30 p.setRenderHints(QPainter::TextAntialiasing | QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::HighQualityAntialiasing, useAntiAliasing);
31 }
32
33 #define RECT_RADIUS 3
34 #define PAINT_ROUNDED_RECT(p, r) p.drawRoundedRect(r, RECT_RADIUS, RECT_RADIUS)
35
36
37 class CDraw
38 {
39 public:
40
41 static QPen penBorderBlue;
42 static QPen penBorderGray;
43 static QPen penBorderBlack;
44 static QBrush brushBackWhite;
45 static QBrush brushBackYellow;
46
47 /**
48 @brief Draw arrows along a line
49
50 An arrow is drawn if all the following requirements are met:
51 * the position the new arrow would have been drawn is within viewport
52 OR
53 `viewport.height() == 0`
54 * the two points have a distance of at least `minPointDist`
55 * the (potential) position of the new arrow has at least a distance of `minArrowDist` from the previous arrow
56
57 @param line The line to draw the arrows along
58 @param viewport Restrict drawing of arrows to this viewport (no limitation is applied if `viewport.height() == 0`)
59 @param minPointDist The minimum distance of two points (in px)
60 @param minArrowDist The minimum distance of two consecutive arrows (in px)
61 */
62 static void arrows(const QPolygonF& line, const QRectF& viewport, QPainter& p, int minPointDist, int minArrowDist, qreal scale);
63
64 static void text(const QString& str, QPainter& p, const QPoint& center, const QColor& color, const QFont& font = CMainWindow::self().getMapFont());
65 static void text(const QString& str, QPainter& p, const QRect& r, const QColor& color);
66
67 /**
68 @brief Draw a cartoon bubble
69
70 `pointerBasePos` denotes the position of the pointer's base, where 0 is `at the very left of the content`, and 1 is `at the very right`.
71 Be careful with small values (near 0) or large values (near 1) for pointerBasePos, this might lead to incorrect drawing,
72 especially if pointerBaseWidth is large.
73 If is larger than 1, a value in pixels is assumed.
74
75 @param p An active QPainter
76 @param contentRect The area the actual content will be in
77 @param pointerPos The position of the pointer's head
78 @param pointerBaseWidth The width of the pointer
79 @param pointerBasePos The (relative) location of the pointer (in percent / pixels)
80 */
81 static QPoint bubble(QPainter& p, const QRect& contentRect, const QPoint& pointerPos, int pointerBaseWidth = 20, float pointerBasePos = .5f);
82
83 static void drawCrossHairDot(QPainter& p, const QPointF& pt);
84
85 static void drawRectangle(QPainter& p, const QRectF& rect, const Qt::GlobalColor& pen, const Qt::GlobalColor& brush);
86 static void drawRectangle(QPainter& p, const QRectF& rect, const QPen& pen, const QBrush& brush);
87 private:
88 /**
89 @brief Creates a new arrow using the brush specified
90 @return A QImage containing the arrow
91 */
92 static QImage createBasicArrow(const QBrush& brush, qreal scale);
93 };
94
95 #endif // CPAINTER_H
96
97