Mon, 06 Jun 2022 22:01:22 +0300
Giant refactor
/* * LDForge: LDraw parts authoring CAD * Copyright (C) 2013 - 2020 Teemu Piippo * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include <QMouseEvent> #include <QMessageBox> #include "document.h" #include "ui_document.h" #include "model.h" #include "ui/objecteditor.h" EditorTabWidget::EditorTabWidget( Model* model, DocumentManager* documents, const ldraw::ColorTable& colorTable, QWidget* parent) : QWidget{parent}, colorTable{colorTable}, canvas{new Canvas{model, this, documents, colorTable, this}}, model{model}, documents{documents}, vertexMap{model}, ui{*new Ui_Document}, toolsBar{new QToolBar{this}} { this->ui.setupUi(this); const int listWidth = static_cast<int>(this->width() / 3); this->ui.viewportListSplitter->setSizes({listWidth * 2, listWidth}); this->ui.toolsBarContainer->setLayout(new QVBoxLayout{this->ui.toolsBarContainer}); this->ui.toolsBarContainer->layout()->addWidget(this->toolsBar); this->ui.listView->setModel(model); this->ui.viewportFrame->setLayout(new QVBoxLayout{this->ui.listView}); this->ui.viewportFrame->layout()->addWidget(this->canvas); this->toolsBar->setOrientation(Qt::Vertical); this->setMouseTracking(true); 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->idAt(index.row()); }; auto resolve = [resolveIndex](const QItemSelection& selection) { return fn::map<QSet<ModelId>>(selection.indexes(), resolveIndex); }; this->canvas->handleSelectionChange(resolve(selected), resolve(deselected)); }); connect(this->model, &Model::dataChanged, this->canvas, qOverload<>(&Canvas::update)); connect(&this->vertexMap, &VertexMap::verticesChanged, [&]() { this->canvas->rebuildVertices(this); }); this->canvas->drawState = &this->drawState; this->initializeTools(); } EditorTabWidget::~EditorTabWidget() { delete &this->ui; } QByteArray EditorTabWidget::saveSplitterState() const { return this->ui.viewportListSplitter->saveState(); } void EditorTabWidget::restoreSplitterState(const QByteArray& state) { this->ui.viewportListSplitter->restoreState(state); } void EditorTabWidget::applyToVertices(VertexMap::ApplyFunction fn) const { this->vertexMap.apply(fn); } const char INDEX_PROPERTY[] = "_editing_mode_index"; void EditorTabWidget::initializeTools() { const struct { QString name, tooltip; QPixmap icon; QWidget* widget; } editingModesInfo[] = { { .name = tr("Select"), .tooltip = tr("Select elements from the model."), .icon = {":/icons/navigate-outline.png"}, //.widget = this->objectEditor, }, { .name = tr("Draw"), .tooltip = tr("Draw new elements into the model."), .icon = {":/icons/pencil-outline.png"}, .widget = nullptr, }, }; for (int i = 0; i < countof(editingModesInfo); ++i) { const auto& editingModeInfo = editingModesInfo[i]; QAction* action = new QAction{editingModeInfo.name, this}; action->setCheckable(true); action->setChecked(i == 0); action->setToolTip(editingModeInfo.tooltip); action->setIcon(QPixmap{editingModeInfo.icon}); action->setProperty(INDEX_PROPERTY, i); this->toolsBar->addAction(action); QWidget* widget = editingModeInfo.widget; if (widget == nullptr) { widget = new QWidget{this}; } this->ui.toolWidgetStack->addWidget(widget); this->toolActions.push_back(action); connect(action, &QAction::triggered, this, &EditorTabWidget::editingModeTriggered); } this->ui.listView->selectAll(); } void EditorTabWidget::editingModeTriggered() { QAction* triggeredAction = qobject_cast<QAction*>(this->sender()); if (triggeredAction != nullptr) { const int index = triggeredAction->property(INDEX_PROPERTY).toInt(); this->drawState.mode = static_cast<EditingMode>(index); this->ui.toolWidgetStack->setCurrentIndex(index); for (QAction* action : this->toolActions) { action->setChecked(action == triggeredAction); } } } void updatePreviewPolygon(DrawState* drawState) { drawState->previewPolygon = drawState->polygon; drawState->previewPolygon.resize(drawState->polygon.size() + 1); drawState->previewPolygon.back() = drawState->previewPoint; if (drawState->previewPolygon.size() > 2) { drawState->isconcave = not geom::isConvex(drawState->previewPolygon); } } void removeLastPoint(DrawState* drawState) { if (drawState->polygon.size() > 0) { drawState->polygon.erase(drawState->polygon.end() - 1); updatePreviewPolygon(drawState); } } bool isCloseToExistingPoints(const std::vector<glm::vec3>& points, const glm::vec3 &pos) { return any(points, std::bind(geom::isclose, std::placeholders::_1, pos)); } void EditorTabWidget::canvasMouseClick(QMouseEvent *event) { switch(this->drawState.mode) { case SelectMode: if (event->button() == Qt::LeftButton) { const ModelId highlighted = this->canvas->getHighlightedObject(); QSet<ModelId> selected; if (highlighted != ModelId{0}) { selected.insert(highlighted); } this->select(selected); event->accept(); } break; case DrawMode: if (event->button() == Qt::LeftButton and this->canvas->worldPosition.has_value()) { const glm::vec3& pos = this->canvas->worldPosition.value(); if (isCloseToExistingPoints(this->drawState.polygon, pos)) { this->closeShape(); } else { this->drawState.polygon.push_back(pos); updatePreviewPolygon(&this->drawState); } event->accept(); } else if (true and event->button() == Qt::RightButton and this->drawState.polygon.size() > 0 ) { this->drawState.polygon.erase(this->drawState.polygon.end() - 1); updatePreviewPolygon(&this->drawState); event->accept(); } break; } } void EditorTabWidget::canvasMouseMove(QMouseEvent *event) { switch(this->drawState.mode) { case SelectMode: break; case DrawMode: if (this->canvas->worldPosition.has_value()) { this->drawState.previewPoint = this->canvas->worldPosition.value(); updatePreviewPolygon(&this->drawState); this->update(); } event->accept(); break; } } void EditorTabWidget::select(const QSet<ModelId> &selected) { QItemSelectionModel* selectionModel = this->ui.listView->selectionModel(); QItemSelection itemSelection; for (const ModelId id : selected) { const std::optional<int> row = this->model->find(id); if (row.has_value()) { const QModelIndex qindex = this->model->index(*row); itemSelection.select(qindex, qindex); } } selectionModel->select(itemSelection, QItemSelectionModel::ClearAndSelect); } const QSet<ModelId> EditorTabWidget::selectedObjects() const { return this->canvas->selectedObjects(); } void EditorTabWidget::closeShape() { if (this->drawState.polygon.size() >= 2 and this->drawState.polygon.size() <= 4) { switch (this->drawState.polygon.size()) { case 2: this->model->append(Colored<LineSegment>{ LineSegment{ .p1 = this->drawState.polygon[0], .p2 = this->drawState.polygon[1], }, ldraw::EDGE_COLOR}); break; case 3: this->model->append(Colored<Triangle>{ Triangle{ .p1 = this->drawState.polygon[0], .p2 = this->drawState.polygon[1], .p3 = this->drawState.polygon[2], }, ldraw::MAIN_COLOR}); break; case 4: this->model->append(Colored<Quadrilateral>{ 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; } } this->drawState.polygon.clear(); updatePreviewPolygon(&this->drawState); }