Experimental:添加了【图层提升/下放功能】

dev-wuyize
karlis 2023-03-22 13:55:06 +08:00
parent 56777c1f00
commit 0b63e8e737
2 changed files with 106 additions and 0 deletions

View File

@ -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<QAction*> optionList;
std::vector<LayerWrapper*> layers;
auto layer = this->selectedItem->data(0, Qt::UserRole).value<LayerWrapper*>();
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<LayerWrapper*, LayerWrapper*>({this->selectedItem->data(0, Qt::UserRole).value<LayerWrapper*>(), layer})));
connect(option, &QAction::triggered, this, [this, option]() {
auto pair = option->data().value<std::pair<LayerWrapper*, LayerWrapper*>>();
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<QAction*> optionList;
std::vector<FolderLayerWrapper*> layers;
auto layer = this->selectedItem->data(0, Qt::UserRole).value<LayerWrapper*>();
auto parent = layer->getParent();
for (auto child : parent->children)
{
if (child.get() == layer || typeid(*child) != typeid(FolderLayerWrapper))
continue;
layers.push_back(dynamic_cast<FolderLayerWrapper*>(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<LayerWrapper*, FolderLayerWrapper*>({ this->selectedItem->data(0, Qt::UserRole).value<LayerWrapper*>(), layer })));
connect(option, &QAction::triggered, this, [this, option]() {
auto pair = option->data().value<std::pair<LayerWrapper*, FolderLayerWrapper*>>();
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<LayerWrapper> 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();
}

View File

@ -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;