diff --git a/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.cpp b/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.cpp index 4a2292e..b30d441 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,105 @@ 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(); + while (layer != nullptr) + { + if (layer->getParent() == nullptr) + break; + layer = layer->getParent(); + if (layer->getParent() == nullptr) + break; + layers.push_back(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->getParent(); + 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;