Wed, 25 May 2022 17:48:18 +0300
added a missing const
/* * 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 "modeleditor.h" #include "ui/objecteditor.h" Document::Document( 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}}, objectEditor{new ObjectEditor{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, &Document::splitterChanged); connect(this->canvas, &Canvas::newStatusText, this, &Document::newStatusText); connect(this->canvas, &Canvas::selectionChanged, [&](const QSet<ldraw::id_t>& newSelection) { QItemSelectionModel* selectionModel = this->ui.listView->selectionModel(); QItemSelection selection; for (const ldraw::id_t id : newSelection) { QModelIndex index = this->model->find(id); if (index != QModelIndex{}) { selection.select(index, index); } } QSignalBlocker blocker{this}; selectionModel->select(selection, QItemSelectionModel::ClearAndSelect); }); 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 resolve = [resolveIndex](const QItemSelection& selection) { return fn::map<QSet<ldraw::id_t>>(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->initializeTools(); } Document::~Document() { delete &this->ui; } QByteArray Document::saveSplitterState() const { return this->ui.viewportListSplitter->saveState(); } void Document::restoreSplitterState(const QByteArray& state) { this->ui.viewportListSplitter->restoreState(state); } std::unique_ptr<ModelEditor> Document::editModel() { std::unique_ptr<ModelEditor> editorPointer = std::make_unique<ModelEditor>(*this->model); connect(editorPointer.get(), &ModelEditor::objectModified, [&](int position){ this->model->emitDataChangedSignal(position); }); return editorPointer; } void Document::applyToVertices(VertexMap::ApplyFunction fn) const { this->vertexMap.apply(fn); } const char INDEX_PROPERTY[] = "_editing_mode_index"; void Document::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, &Document::editingModeTriggered); } } void Document::editingModeTriggered() { QAction* triggeredAction = qobject_cast<QAction*>(this->sender()); if (triggeredAction != nullptr) { const int index = triggeredAction->property(INDEX_PROPERTY).toInt(); this->canvas->mode = static_cast<EditingMode>(index); this->ui.toolWidgetStack->setCurrentIndex(index); for (QAction* action : this->toolActions) { action->setChecked(action == triggeredAction); } } } const Model &Document::getModel() const { return *this->model; } const QSet<ldraw::id_t> Document::selectedObjects() const { return this->canvas->selectedObjects(); }