Mon, 06 Jun 2022 22:01:22 +0300
Giant refactor
24 | 1 | /* |
2 | * LDForge: LDraw parts authoring CAD | |
3 | * Copyright (C) 2013 - 2020 Teemu Piippo | |
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 | ||
47 | 19 | #include <QMouseEvent> |
125
f127982d3412
Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents:
124
diff
changeset
|
20 | #include <QMessageBox> |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
21 | #include "document.h" |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
22 | #include "ui_document.h" |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
23 | #include "model.h" |
191
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
24 | #include "ui/objecteditor.h" |
125
f127982d3412
Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents:
124
diff
changeset
|
25 | |
200 | 26 | EditorTabWidget::EditorTabWidget( |
35
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
27 | Model* model, |
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
28 | DocumentManager* documents, |
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
29 | const ldraw::ColorTable& colorTable, |
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
30 | QWidget* parent) : |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
31 | QWidget{parent}, |
178 | 32 | colorTable{colorTable}, |
191
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
33 | canvas{new Canvas{model, this, documents, colorTable, this}}, |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
34 | model{model}, |
21 | 35 | documents{documents}, |
118 | 36 | vertexMap{model}, |
191
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
37 | ui{*new Ui_Document}, |
200 | 38 | toolsBar{new QToolBar{this}} |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
39 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
40 | this->ui.setupUi(this); |
187 | 41 | const int listWidth = static_cast<int>(this->width() / 3); |
42 | this->ui.viewportListSplitter->setSizes({listWidth * 2, listWidth}); | |
125
f127982d3412
Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents:
124
diff
changeset
|
43 | this->ui.toolsBarContainer->setLayout(new QVBoxLayout{this->ui.toolsBarContainer}); |
f127982d3412
Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents:
124
diff
changeset
|
44 | this->ui.toolsBarContainer->layout()->addWidget(this->toolsBar); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
45 | this->ui.listView->setModel(model); |
81
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
46 | this->ui.viewportFrame->setLayout(new QVBoxLayout{this->ui.listView}); |
191
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
47 | this->ui.viewportFrame->layout()->addWidget(this->canvas); |
125
f127982d3412
Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents:
124
diff
changeset
|
48 | this->toolsBar->setOrientation(Qt::Vertical); |
47 | 49 | this->setMouseTracking(true); |
200 | 50 | connect(this->ui.viewportListSplitter, &QSplitter::splitterMoved, this, &EditorTabWidget::splitterChanged); |
51 | connect(this->canvas, &Canvas::mouseClick, this, &EditorTabWidget::canvasMouseClick); | |
52 | connect(this->canvas, &Canvas::mouseMove, this, &EditorTabWidget::canvasMouseMove); | |
53 | connect(this->canvas, &Canvas::newStatusText, this, &EditorTabWidget::newStatusText); | |
51 | 54 | connect(this->ui.listView->selectionModel(), &QItemSelectionModel::selectionChanged, |
55 | [&](const QItemSelection& selected, const QItemSelection& deselected) | |
56 | { | |
200 | 57 | auto resolveIndex = [this](const QModelIndex& index){ return this->model->idAt(index.row()); }; |
63
f7dd937667a5
omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
58 | auto resolve = [resolveIndex](const QItemSelection& selection) |
51 | 59 | { |
200 | 60 | return fn::map<QSet<ModelId>>(selection.indexes(), resolveIndex); |
63
f7dd937667a5
omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
61 | }; |
191
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
62 | this->canvas->handleSelectionChange(resolve(selected), resolve(deselected)); |
51 | 63 | }); |
191
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
64 | connect(this->model, &Model::dataChanged, this->canvas, qOverload<>(&Canvas::update)); |
118 | 65 | connect(&this->vertexMap, &VertexMap::verticesChanged, [&]() |
66 | { | |
191
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
67 | this->canvas->rebuildVertices(this); |
125
f127982d3412
Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents:
124
diff
changeset
|
68 | }); |
198
eb9d900dc79a
fix up things and remove unnecessary code
Teemu Piippo <teemu@hecknology.net>
parents:
197
diff
changeset
|
69 | this->canvas->drawState = &this->drawState; |
125
f127982d3412
Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents:
124
diff
changeset
|
70 | this->initializeTools(); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
71 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
72 | |
200 | 73 | EditorTabWidget::~EditorTabWidget() |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
74 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
75 | delete &this->ui; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
76 | } |
9
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
77 | |
200 | 78 | QByteArray EditorTabWidget::saveSplitterState() const |
9
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
79 | { |
187 | 80 | return this->ui.viewportListSplitter->saveState(); |
9
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
81 | } |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
82 | |
200 | 83 | void EditorTabWidget::restoreSplitterState(const QByteArray& state) |
9
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
84 | { |
187 | 85 | this->ui.viewportListSplitter->restoreState(state); |
9
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
86 | } |
36
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
87 | |
200 | 88 | void EditorTabWidget::applyToVertices(VertexMap::ApplyFunction fn) const |
118 | 89 | { |
90 | this->vertexMap.apply(fn); | |
91 | } | |
92 | ||
191
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
93 | const char INDEX_PROPERTY[] = "_editing_mode_index"; |
125
f127982d3412
Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents:
124
diff
changeset
|
94 | |
200 | 95 | void EditorTabWidget::initializeTools() |
125
f127982d3412
Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents:
124
diff
changeset
|
96 | { |
191
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
97 | const struct |
125
f127982d3412
Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents:
124
diff
changeset
|
98 | { |
191
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
99 | QString name, tooltip; |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
100 | QPixmap icon; |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
101 | QWidget* widget; |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
102 | } editingModesInfo[] = { |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
103 | { |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
104 | .name = tr("Select"), |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
105 | .tooltip = tr("Select elements from the model."), |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
106 | .icon = {":/icons/navigate-outline.png"}, |
200 | 107 | //.widget = this->objectEditor, |
191
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
108 | }, |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
109 | { |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
110 | .name = tr("Draw"), |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
111 | .tooltip = tr("Draw new elements into the model."), |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
112 | .icon = {":/icons/pencil-outline.png"}, |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
113 | .widget = nullptr, |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
114 | }, |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
115 | }; |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
116 | for (int i = 0; i < countof(editingModesInfo); ++i) { |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
117 | const auto& editingModeInfo = editingModesInfo[i]; |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
118 | QAction* action = new QAction{editingModeInfo.name, this}; |
143
7b62c52835a1
Fix memory corruption involving document tools.
Teemu Piippo <teemu@hecknology.net>
parents:
141
diff
changeset
|
119 | action->setCheckable(true); |
191
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
120 | action->setChecked(i == 0); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
121 | action->setToolTip(editingModeInfo.tooltip); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
122 | action->setIcon(QPixmap{editingModeInfo.icon}); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
123 | action->setProperty(INDEX_PROPERTY, i); |
143
7b62c52835a1
Fix memory corruption involving document tools.
Teemu Piippo <teemu@hecknology.net>
parents:
141
diff
changeset
|
124 | this->toolsBar->addAction(action); |
191
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
125 | QWidget* widget = editingModeInfo.widget; |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
126 | if (widget == nullptr) { |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
127 | widget = new QWidget{this}; |
125
f127982d3412
Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents:
124
diff
changeset
|
128 | } |
191
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
129 | this->ui.toolWidgetStack->addWidget(widget); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
130 | this->toolActions.push_back(action); |
200 | 131 | connect(action, &QAction::triggered, this, &EditorTabWidget::editingModeTriggered); |
125
f127982d3412
Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents:
124
diff
changeset
|
132 | } |
197
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
133 | this->ui.listView->selectAll(); |
125
f127982d3412
Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents:
124
diff
changeset
|
134 | } |
f127982d3412
Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents:
124
diff
changeset
|
135 | |
200 | 136 | void EditorTabWidget::editingModeTriggered() |
125
f127982d3412
Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents:
124
diff
changeset
|
137 | { |
191
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
138 | QAction* triggeredAction = qobject_cast<QAction*>(this->sender()); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
139 | if (triggeredAction != nullptr) |
125
f127982d3412
Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents:
124
diff
changeset
|
140 | { |
191
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
141 | const int index = triggeredAction->property(INDEX_PROPERTY).toInt(); |
198
eb9d900dc79a
fix up things and remove unnecessary code
Teemu Piippo <teemu@hecknology.net>
parents:
197
diff
changeset
|
142 | this->drawState.mode = static_cast<EditingMode>(index); |
191
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
143 | this->ui.toolWidgetStack->setCurrentIndex(index); |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
144 | for (QAction* action : this->toolActions) { |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
145 | action->setChecked(action == triggeredAction); |
126
a7c720aff97c
moved ObjectEditor under SelectTool
Teemu Piippo <teemu@hecknology.net>
parents:
125
diff
changeset
|
146 | } |
125
f127982d3412
Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents:
124
diff
changeset
|
147 | } |
f127982d3412
Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents:
124
diff
changeset
|
148 | } |
f127982d3412
Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents:
124
diff
changeset
|
149 | |
197
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
150 | void updatePreviewPolygon(DrawState* drawState) |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
151 | { |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
152 | drawState->previewPolygon = drawState->polygon; |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
153 | drawState->previewPolygon.resize(drawState->polygon.size() + 1); |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
154 | drawState->previewPolygon.back() = drawState->previewPoint; |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
155 | if (drawState->previewPolygon.size() > 2) |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
156 | { |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
157 | drawState->isconcave = not geom::isConvex(drawState->previewPolygon); |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
158 | } |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
159 | } |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
160 | |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
161 | void removeLastPoint(DrawState* drawState) |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
162 | { |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
163 | if (drawState->polygon.size() > 0) |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
164 | { |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
165 | drawState->polygon.erase(drawState->polygon.end() - 1); |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
166 | updatePreviewPolygon(drawState); |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
167 | } |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
168 | } |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
169 | |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
170 | bool isCloseToExistingPoints(const std::vector<glm::vec3>& points, const glm::vec3 &pos) |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
171 | { |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
172 | return any(points, std::bind(geom::isclose, std::placeholders::_1, pos)); |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
173 | } |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
174 | |
200 | 175 | void EditorTabWidget::canvasMouseClick(QMouseEvent *event) |
197
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
176 | { |
198
eb9d900dc79a
fix up things and remove unnecessary code
Teemu Piippo <teemu@hecknology.net>
parents:
197
diff
changeset
|
177 | switch(this->drawState.mode) |
197
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
178 | { |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
179 | case SelectMode: |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
180 | if (event->button() == Qt::LeftButton) |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
181 | { |
200 | 182 | const ModelId highlighted = this->canvas->getHighlightedObject(); |
183 | QSet<ModelId> selected; | |
184 | if (highlighted != ModelId{0}) { | |
197
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
185 | selected.insert(highlighted); |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
186 | } |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
187 | this->select(selected); |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
188 | event->accept(); |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
189 | } |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
190 | break; |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
191 | case DrawMode: |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
192 | if (event->button() == Qt::LeftButton and this->canvas->worldPosition.has_value()) |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
193 | { |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
194 | const glm::vec3& pos = this->canvas->worldPosition.value(); |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
195 | if (isCloseToExistingPoints(this->drawState.polygon, pos)) |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
196 | { |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
197 | this->closeShape(); |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
198 | } |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
199 | else |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
200 | { |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
201 | this->drawState.polygon.push_back(pos); |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
202 | updatePreviewPolygon(&this->drawState); |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
203 | } |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
204 | event->accept(); |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
205 | } |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
206 | else if (true |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
207 | and event->button() == Qt::RightButton |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
208 | and this->drawState.polygon.size() > 0 |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
209 | ) { |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
210 | this->drawState.polygon.erase(this->drawState.polygon.end() - 1); |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
211 | updatePreviewPolygon(&this->drawState); |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
212 | event->accept(); |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
213 | } |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
214 | break; |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
215 | } |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
216 | } |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
217 | |
200 | 218 | void EditorTabWidget::canvasMouseMove(QMouseEvent *event) |
197
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
219 | { |
198
eb9d900dc79a
fix up things and remove unnecessary code
Teemu Piippo <teemu@hecknology.net>
parents:
197
diff
changeset
|
220 | switch(this->drawState.mode) |
197
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
221 | { |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
222 | case SelectMode: |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
223 | break; |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
224 | case DrawMode: |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
225 | if (this->canvas->worldPosition.has_value()) |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
226 | { |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
227 | this->drawState.previewPoint = this->canvas->worldPosition.value(); |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
228 | updatePreviewPolygon(&this->drawState); |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
229 | this->update(); |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
230 | } |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
231 | event->accept(); |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
232 | break; |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
233 | } |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
234 | } |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
235 | |
200 | 236 | void EditorTabWidget::select(const QSet<ModelId> &selected) |
197
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
237 | { |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
238 | QItemSelectionModel* selectionModel = this->ui.listView->selectionModel(); |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
239 | QItemSelection itemSelection; |
200 | 240 | for (const ModelId id : selected) |
197
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
241 | { |
200 | 242 | const std::optional<int> row = this->model->find(id); |
243 | if (row.has_value()) | |
197
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
244 | { |
200 | 245 | const QModelIndex qindex = this->model->index(*row); |
246 | itemSelection.select(qindex, qindex); | |
197
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
247 | } |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
248 | } |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
249 | selectionModel->select(itemSelection, QItemSelectionModel::ClearAndSelect); |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
250 | } |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
251 | |
200 | 252 | const QSet<ModelId> EditorTabWidget::selectedObjects() const |
169
6da096930534
Added the delete action
Teemu Piippo <teemu@hecknology.net>
parents:
163
diff
changeset
|
253 | { |
191
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
254 | return this->canvas->selectedObjects(); |
169
6da096930534
Added the delete action
Teemu Piippo <teemu@hecknology.net>
parents:
163
diff
changeset
|
255 | } |
197
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
256 | |
200 | 257 | void EditorTabWidget::closeShape() |
197
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
258 | { |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
259 | if (this->drawState.polygon.size() >= 2 and this->drawState.polygon.size() <= 4) |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
260 | { |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
261 | switch (this->drawState.polygon.size()) |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
262 | { |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
263 | case 2: |
200 | 264 | this->model->append(Colored<LineSegment>{ |
265 | LineSegment{ | |
266 | .p1 = this->drawState.polygon[0], | |
267 | .p2 = this->drawState.polygon[1], | |
268 | }, | |
269 | ldraw::EDGE_COLOR}); | |
197
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
270 | break; |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
271 | case 3: |
200 | 272 | this->model->append(Colored<Triangle>{ |
273 | Triangle{ | |
274 | .p1 = this->drawState.polygon[0], | |
275 | .p2 = this->drawState.polygon[1], | |
276 | .p3 = this->drawState.polygon[2], | |
277 | }, | |
278 | ldraw::MAIN_COLOR}); | |
197
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
279 | break; |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
280 | case 4: |
200 | 281 | this->model->append(Colored<Quadrilateral>{ |
282 | Quadrilateral{ | |
283 | .p1 = this->drawState.polygon[0], | |
284 | .p2 = this->drawState.polygon[1], | |
285 | .p3 = this->drawState.polygon[2], | |
286 | .p4 = this->drawState.polygon[3], | |
287 | }, | |
288 | ldraw::MAIN_COLOR}); | |
197
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
289 | break; |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
290 | } |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
291 | } |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
292 | this->drawState.polygon.clear(); |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
293 | updatePreviewPolygon(&this->drawState); |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
294 | } |