src/document.cpp

Mon, 06 Jun 2022 22:01:22 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Mon, 06 Jun 2022 22:01:22 +0300
changeset 200
ca23936b455b
parent 198
eb9d900dc79a
child 201
5d201ee4a9c3
permissions
-rw-r--r--

Giant refactor

24
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
1 /*
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
2 * LDForge: LDraw parts authoring CAD
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
3 * Copyright (C) 2013 - 2020 Teemu Piippo
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
4 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
5 * This program is free software: you can redistribute it and/or modify
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
6 * it under the terms of the GNU General Public License as published by
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
8 * (at your option) any later version.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
9 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
10 * This program is distributed in the hope that it will be useful,
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
13 * GNU General Public License for more details.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
14 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
15 * You should have received a copy of the GNU General Public License
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
17 */
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
18
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 39
diff changeset
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
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
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
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
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
Teemu Piippo <teemu@hecknology.net>
parents: 9
diff changeset
35 documents{documents},
118
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
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
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
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
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
41 const int listWidth = static_cast<int>(this->width() / 3);
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
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
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 39
diff changeset
49 this->setMouseTracking(true);
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
50 connect(this->ui.viewportListSplitter, &QSplitter::splitterMoved, this, &EditorTabWidget::splitterChanged);
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
51 connect(this->canvas, &Canvas::mouseClick, this, &EditorTabWidget::canvasMouseClick);
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
52 connect(this->canvas, &Canvas::mouseMove, this, &EditorTabWidget::canvasMouseMove);
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
53 connect(this->canvas, &Canvas::newStatusText, this, &EditorTabWidget::newStatusText);
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
54 connect(this->ui.listView->selectionModel(), &QItemSelectionModel::selectionChanged,
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
55 [&](const QItemSelection& selected, const QItemSelection& deselected)
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
56 {
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
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
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
59 {
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
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
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
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
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
65 connect(&this->vertexMap, &VertexMap::verticesChanged, [&]()
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
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
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
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
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
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
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
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
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
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
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
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
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
88 void EditorTabWidget::applyToVertices(VertexMap::ApplyFunction fn) const
118
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
89 {
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
90 this->vertexMap.apply(fn);
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
91 }
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
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
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
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
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
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
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
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
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
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
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
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
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
182 const ModelId highlighted = this->canvas->getHighlightedObject();
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
183 QSet<ModelId> selected;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
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
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
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
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
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
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
240 for (const ModelId id : selected)
197
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
241 {
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
242 const std::optional<int> row = this->model->find(id);
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
243 if (row.has_value())
197
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
244 {
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
245 const QModelIndex qindex = this->model->index(*row);
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
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
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
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
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
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
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
264 this->model->append(Colored<LineSegment>{
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
265 LineSegment{
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
266 .p1 = this->drawState.polygon[0],
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
267 .p2 = this->drawState.polygon[1],
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
268 },
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
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
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
272 this->model->append(Colored<Triangle>{
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
273 Triangle{
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
274 .p1 = this->drawState.polygon[0],
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
275 .p2 = this->drawState.polygon[1],
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
276 .p3 = this->drawState.polygon[2],
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
277 },
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
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
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
281 this->model->append(Colored<Quadrilateral>{
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
282 Quadrilateral{
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
283 .p1 = this->drawState.polygon[0],
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
284 .p2 = this->drawState.polygon[1],
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
285 .p3 = this->drawState.polygon[2],
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
286 .p4 = this->drawState.polygon[3],
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
287 },
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 198
diff changeset
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 }

mercurial