From b2f37cc4eb4f6a45dd9828404474d3c6f567514e Mon Sep 17 00:00:00 2001 From: karlis <2995621482@qq.com> Date: Wed, 22 Mar 2023 13:55:06 +0800 Subject: [PATCH] =?UTF-8?q?Experimental=EF=BC=9A=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E4=BA=86=E3=80=90=E5=9B=BE=E5=B1=82=E6=8F=90=E5=8D=87/?= =?UTF-8?q?=E4=B8=8B=E6=94=BE=E5=8A=9F=E8=83=BD=E3=80=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/Editor/RightBar/LayerTreeWidget.cpp | 102 ++++++++++++++++++ .../src/Editor/RightBar/LayerTreeWidget.h | 3 + 2 files changed, 105 insertions(+) diff --git a/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.cpp b/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.cpp index 4a2292e..f7bddc9 100644 --- a/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.cpp +++ b/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.cpp @@ -110,6 +110,8 @@ void LayerTreeWidget::popMenu(const QPoint &pos) }); } } + menu.addAction(getPromoteUpAction()); + menu.addAction(getPromoteDownAction()); } menu.exec(mapToGlobal(pos)); } @@ -203,4 +205,104 @@ void LayerTreeWidget::pushDownLayer() parent->qTreeWidgetItem->insertChild(index + 1, layer->qTreeWidgetItem); this->setCurrentItem(layer->qTreeWidgetItem); emit triggerCentralRefresh(); +} + +QAction* LayerTreeWidget::getPromoteUpAction() +{ + QAction* action = new QAction(QString::fromLocal8Bit("提升 (experimental)"), this); + QMenu* optionMenu = new QMenu(); + QList optionList; + std::vector layers; + auto layer = this->selectedItem->data(0, Qt::UserRole).value(); + layer = layer->getParent(); + while (layer != nullptr) + { + if (layer->getParent() == nullptr) + break; + layer = layer->getParent(); + layers.push_back(dynamic_cast(layer)); + } + reverse(layers.begin(), layers.end()); + if (layers.size() == 0) + { + action->setEnabled(false); + return action; + } + for (auto layer : layers) + { + if (layer == nullptr) + continue; + QAction* option = new QAction(QString::fromLocal8Bit("提升至 ") + layer->property.name + QString::fromLocal8Bit(" 下"), this); + option->setData(QVariant::fromValue(std::pair({this->selectedItem->data(0, Qt::UserRole).value(), layer}))); + connect(option, &QAction::triggered, this, [this, option]() { + auto pair = option->data().value>(); + auto layer = pair.first; + auto parent = pair.second; + this->moveLayer(layer, parent); + }); + optionList.append(option); + } + optionMenu->addActions(optionList); + action->setMenu(optionMenu); + return action; +} + +QAction* LayerTreeWidget::getPromoteDownAction() +{ + QAction* action = new QAction(QString::fromLocal8Bit("下放 (experimental)"), this); + QMenu* optionMenu = new QMenu(); + QList optionList; + std::vector layers; + auto layer = this->selectedItem->data(0, Qt::UserRole).value(); + auto parent = layer->getParent(); + for (auto child : parent->children) + { + if (child.get() == layer || typeid(*child) != typeid(FolderLayerWrapper)) + continue; + layers.push_back(dynamic_cast(child.get())); + } + reverse(layers.begin(), layers.end()); + if (layers.size() == 0) + { + action->setEnabled(false); + return action; + } + for (auto layer : layers) + { + if (layer == nullptr) + continue; + QAction* option = new QAction(QString::fromLocal8Bit("下放至 ") + layer->property.name + QString::fromLocal8Bit(" 中"), this); + option->setData(QVariant::fromValue(std::pair({ this->selectedItem->data(0, Qt::UserRole).value(), layer }))); + connect(option, &QAction::triggered, this, [this, option]() { + auto pair = option->data().value>(); + auto layer = pair.first; + auto parent = pair.second; + this->moveLayer(layer, parent); + }); + optionList.append(option); + } + optionMenu->addActions(optionList); + action->setMenu(optionMenu); + return action; +} + +void LayerTreeWidget::moveLayer(LayerWrapper* layer, FolderLayerWrapper* parent) +{ + auto oldParent = layer->getParent(); + std::shared_ptr layerPtr = nullptr; + for (int i = 0; i < oldParent->children.size(); i++) + { + if (oldParent->children[i].get() == layer) + { + layerPtr = oldParent->children[i]; + oldParent->children.erase(oldParent->children.begin() + i); + break; + } + } + oldParent->qTreeWidgetItem->removeChild(layer->qTreeWidgetItem); + parent->qTreeWidgetItem->addChild(layer->qTreeWidgetItem); + parent->addChild(layerPtr); + layer->setParent(parent); + oldParent->removeChild(layer); + emit triggerCentralRefresh(); } \ No newline at end of file diff --git a/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.h b/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.h index 4dce9be..e14cefd 100644 --- a/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.h +++ b/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.h @@ -11,6 +11,9 @@ class LayerTreeWidget : public QTreeWidget LayerWrapper *copiedItem; void pushUpLayer(); void pushDownLayer(); + QAction* getPromoteUpAction(); + QAction* getPromoteDownAction(); + void moveLayer(LayerWrapper* source, FolderLayerWrapper* parent); public: ElementManager* elementManager;