diff -r 6988973515d2 -r ca23936b455b src/document.cpp --- a/src/document.cpp Wed May 25 20:36:34 2022 +0300 +++ b/src/document.cpp Mon Jun 06 22:01:22 2022 +0300 @@ -21,13 +21,9 @@ #include "document.h" #include "ui_document.h" #include "model.h" -#include "modeleditor.h" #include "ui/objecteditor.h" -#include "linetypes/edge.h" -#include "linetypes/triangle.h" -#include "linetypes/quadrilateral.h" -Document::Document( +EditorTabWidget::EditorTabWidget( Model* model, DocumentManager* documents, const ldraw::ColorTable& colorTable, @@ -39,8 +35,7 @@ documents{documents}, vertexMap{model}, ui{*new Ui_Document}, - toolsBar{new QToolBar{this}}, - objectEditor{new ObjectEditor{this}} + toolsBar{new QToolBar{this}} { this->ui.setupUi(this); const int listWidth = static_cast(this->width() / 3); @@ -52,17 +47,17 @@ this->ui.viewportFrame->layout()->addWidget(this->canvas); this->toolsBar->setOrientation(Qt::Vertical); this->setMouseTracking(true); - connect(this->ui.viewportListSplitter, &QSplitter::splitterMoved, this, &Document::splitterChanged); - connect(this->canvas, &Canvas::mouseClick, this, &Document::canvasMouseClick); - connect(this->canvas, &Canvas::mouseMove, this, &Document::canvasMouseMove); - connect(this->canvas, &Canvas::newStatusText, this, &Document::newStatusText); + connect(this->ui.viewportListSplitter, &QSplitter::splitterMoved, this, &EditorTabWidget::splitterChanged); + connect(this->canvas, &Canvas::mouseClick, this, &EditorTabWidget::canvasMouseClick); + connect(this->canvas, &Canvas::mouseMove, this, &EditorTabWidget::canvasMouseMove); + connect(this->canvas, &Canvas::newStatusText, this, &EditorTabWidget::newStatusText); connect(this->ui.listView->selectionModel(), &QItemSelectionModel::selectionChanged, [&](const QItemSelection& selected, const QItemSelection& deselected) { - auto resolveIndex = [this](const QModelIndex& index){ return (*this->model)[index.row()]->id; }; + auto resolveIndex = [this](const QModelIndex& index){ return this->model->idAt(index.row()); }; auto resolve = [resolveIndex](const QItemSelection& selection) { - return fn::map>(selection.indexes(), resolveIndex); + return fn::map>(selection.indexes(), resolveIndex); }; this->canvas->handleSelectionChange(resolve(selected), resolve(deselected)); }); @@ -75,38 +70,29 @@ this->initializeTools(); } -Document::~Document() +EditorTabWidget::~EditorTabWidget() { delete &this->ui; } -QByteArray Document::saveSplitterState() const +QByteArray EditorTabWidget::saveSplitterState() const { return this->ui.viewportListSplitter->saveState(); } -void Document::restoreSplitterState(const QByteArray& state) +void EditorTabWidget::restoreSplitterState(const QByteArray& state) { this->ui.viewportListSplitter->restoreState(state); } -std::unique_ptr Document::editModel() -{ - std::unique_ptr editorPointer = std::make_unique(*this->model); - connect(editorPointer.get(), &ModelEditor::objectModified, [&](int position){ - this->model->emitDataChangedSignal(position); - }); - return editorPointer; -} - -void Document::applyToVertices(VertexMap::ApplyFunction fn) const +void EditorTabWidget::applyToVertices(VertexMap::ApplyFunction fn) const { this->vertexMap.apply(fn); } const char INDEX_PROPERTY[] = "_editing_mode_index"; -void Document::initializeTools() +void EditorTabWidget::initializeTools() { const struct { @@ -118,7 +104,7 @@ .name = tr("Select"), .tooltip = tr("Select elements from the model."), .icon = {":/icons/navigate-outline.png"}, - .widget = this->objectEditor, + //.widget = this->objectEditor, }, { .name = tr("Draw"), @@ -142,12 +128,12 @@ } this->ui.toolWidgetStack->addWidget(widget); this->toolActions.push_back(action); - connect(action, &QAction::triggered, this, &Document::editingModeTriggered); + connect(action, &QAction::triggered, this, &EditorTabWidget::editingModeTriggered); } this->ui.listView->selectAll(); } -void Document::editingModeTriggered() +void EditorTabWidget::editingModeTriggered() { QAction* triggeredAction = qobject_cast(this->sender()); if (triggeredAction != nullptr) @@ -186,16 +172,16 @@ return any(points, std::bind(geom::isclose, std::placeholders::_1, pos)); } -void Document::canvasMouseClick(QMouseEvent *event) +void EditorTabWidget::canvasMouseClick(QMouseEvent *event) { switch(this->drawState.mode) { case SelectMode: if (event->button() == Qt::LeftButton) { - const ldraw::id_t highlighted = this->canvas->getHighlightedObject(); - QSet selected; - if (highlighted != ldraw::NULL_ID) { + const ModelId highlighted = this->canvas->getHighlightedObject(); + QSet selected; + if (highlighted != ModelId{0}) { selected.insert(highlighted); } this->select(selected); @@ -229,7 +215,7 @@ } } -void Document::canvasMouseMove(QMouseEvent *event) +void EditorTabWidget::canvasMouseMove(QMouseEvent *event) { switch(this->drawState.mode) { @@ -247,52 +233,59 @@ } } -void Document::select(const QSet &selected) +void EditorTabWidget::select(const QSet &selected) { QItemSelectionModel* selectionModel = this->ui.listView->selectionModel(); QItemSelection itemSelection; - for (const ldraw::id_t id : selected) + for (const ModelId id : selected) { - QModelIndex index = this->model->find(id); - if (index != QModelIndex{}) + const std::optional row = this->model->find(id); + if (row.has_value()) { - itemSelection.select(index, index); + const QModelIndex qindex = this->model->index(*row); + itemSelection.select(qindex, qindex); } } selectionModel->select(itemSelection, QItemSelectionModel::ClearAndSelect); } -const Model &Document::getModel() const -{ - return *this->model; -} - -const QSet Document::selectedObjects() const +const QSet EditorTabWidget::selectedObjects() const { return this->canvas->selectedObjects(); } -void Document::closeShape() +void EditorTabWidget::closeShape() { if (this->drawState.polygon.size() >= 2 and this->drawState.polygon.size() <= 4) { - std::unique_ptr modelEditor = this->editModel(); switch (this->drawState.polygon.size()) { case 2: - modelEditor->append( - vectorToArray<2>(this->drawState.polygon), - ldraw::EDGE_COLOR); + this->model->append(Colored{ + LineSegment{ + .p1 = this->drawState.polygon[0], + .p2 = this->drawState.polygon[1], + }, + ldraw::EDGE_COLOR}); break; case 3: - modelEditor->append( - vectorToArray<3>(this->drawState.polygon), - ldraw::MAIN_COLOR); + this->model->append(Colored{ + Triangle{ + .p1 = this->drawState.polygon[0], + .p2 = this->drawState.polygon[1], + .p3 = this->drawState.polygon[2], + }, + ldraw::MAIN_COLOR}); break; case 4: - modelEditor->append( - vectorToArray<4>(this->drawState.polygon), - ldraw::MAIN_COLOR); + this->model->append(Colored{ + Quadrilateral{ + .p1 = this->drawState.polygon[0], + .p2 = this->drawState.polygon[1], + .p3 = this->drawState.polygon[2], + .p4 = this->drawState.polygon[3], + }, + ldraw::MAIN_COLOR}); break; } }