src/ui/canvas.cpp

Sat, 08 Feb 2020 00:08:57 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 08 Feb 2020 00:08:57 +0200
changeset 52
eee644f88e93
parent 51
1a9eac27698d
child 55
cb81ecb5fb23
permissions
-rw-r--r--

avoid having the null id in the selection

47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 #include <QMouseEvent>
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
2 #include "canvas.h"
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
3
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4 Canvas::Canvas(
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5 Model* model,
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 DocumentManager* documents,
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7 const ldraw::ColorTable& colorTable,
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8 QWidget* parent) :
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 PartRenderer{model, documents, colorTable, parent}
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 {
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11 this->setMouseTracking(true);
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 }
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
14 void Canvas::handleSelectionChange(const QSet<ldraw::Id>& selectedIds, const QSet<ldraw::Id>& deselectedIds)
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
15 {
52
eee644f88e93 avoid having the null id in the selection
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
16 Q_ASSERT(not selectedIds.contains(ldraw::NULL_ID));
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
17 this->selection.subtract(deselectedIds);
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
18 this->selection.unite(selectedIds);
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
19 this->compiler->setSelectedObjects(this->selection);
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
20 this->update();
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
21 }
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
22
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23 void Canvas::mouseMoveEvent(QMouseEvent* event)
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 {
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 const ldraw::Id id = this->pick(event->pos());
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26 this->newStatusText("Selected: %1"_q.arg(id.value));
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
27 this->highlighted = id;
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
28 this->totalMouseMove += (event->pos() - this->lastMousePosition).manhattanLength();
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
29 this->lastMousePosition = event->pos();
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 PartRenderer::mouseMoveEvent(event);
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31 }
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
32
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
33 void Canvas::mousePressEvent(QMouseEvent* event)
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
34 {
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
35 this->totalMouseMove = 0;
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
36 this->lastMousePosition = event->pos();
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
37 PartRenderer::mousePressEvent(event);
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
38 }
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
39
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
40 void Canvas::mouseReleaseEvent(QMouseEvent* event)
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
41 {
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
42 if (this->totalMouseMove < (2.0 / sqrt(2)) * 5.0)
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
43 {
52
eee644f88e93 avoid having the null id in the selection
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
44 if (this->highlighted == ldraw::NULL_ID)
eee644f88e93 avoid having the null id in the selection
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
45 {
eee644f88e93 avoid having the null id in the selection
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
46 this->selection = {};
eee644f88e93 avoid having the null id in the selection
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
47 }
eee644f88e93 avoid having the null id in the selection
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
48 else
eee644f88e93 avoid having the null id in the selection
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
49 {
eee644f88e93 avoid having the null id in the selection
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
50 this->selection = {this->highlighted};
eee644f88e93 avoid having the null id in the selection
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
51 }
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
52 this->compiler->setSelectedObjects(this->selection);
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
53 emit selectionChanged(this->selection);
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
54 this->update();
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
55 }
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
56 PartRenderer::mouseReleaseEvent(event);
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
57 }

mercurial