|
1 #include <QPainter> |
|
2 #include <QPaintEvent> |
|
3 #include "patterneditor.h" |
|
4 #include "patternviewer.h" |
|
5 |
|
6 /* |
|
7 * Construct a pattern viewer for an existing pattern editor. |
|
8 */ |
|
9 PatternViewer::PatternViewer(PatternEditor* parent) : |
|
10 QWidget {parent}, |
|
11 editor {parent} |
|
12 { |
|
13 this->transformation.scale(4, 4); |
|
14 } |
|
15 |
|
16 /* |
|
17 * Unfortunately, Qt doesn't provide an easy way to translate a floating point rectangle |
|
18 * into a floating point polygon. So here's a manual implementation. |
|
19 */ |
|
20 static QPolygonF transformRect(const QRectF& rect, const QTransform& transform) |
|
21 { |
|
22 QPolygonF transformed; |
|
23 for (const QPointF& point : { |
|
24 rect.topLeft(), |
|
25 rect.topRight(), |
|
26 rect.bottomRight(), |
|
27 rect.bottomLeft() |
|
28 }) { |
|
29 transformed.append(transform.map(point)); |
|
30 } |
|
31 return transformed; |
|
32 } |
|
33 |
|
34 /* |
|
35 * Renders the pattern. |
|
36 */ |
|
37 void PatternViewer::paintEvent(QPaintEvent* event) |
|
38 { |
|
39 const Pattern& pattern = this->editor->pattern; |
|
40 static const QPixmap viewerBackground {":/data/pattern-background.png"}; |
|
41 static const QPixmap canvasBackground {":/data/transparent-background.png"}; |
|
42 QPainter painter {this}; |
|
43 painter.drawTiledPixmap(this->rect(), viewerBackground); |
|
44 painter.setBrush(canvasBackground); |
|
45 QRectF canvasRect = QRectF {{0.0f, 0.0f}, pattern.canvasSize}; |
|
46 painter.drawPolygon(::transformRect(canvasRect, this->transformation)); |
|
47 painter.setTransform(this->transformation); |
|
48 event->accept(); |
|
49 } |