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