diff --git a/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/LayerCreateWidget.cpp b/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/LayerCreateWidget.cpp index ed6743a..45d8dfe 100644 --- a/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/LayerCreateWidget.cpp +++ b/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/LayerCreateWidget.cpp @@ -1,15 +1,46 @@ #include "LayerCreateWidget.h" #include -LayerCreateWidget::LayerCreateWidget(ElementManager* elementManager, QWidget* parent) : +LayerCreateWidget::LayerCreateWidget(ElementManager* elementManager, FolderLayerWrapper* folderLayer, QWidget* parent) : QDialog(parent) { + this->elementManager = elementManager; ui.setupUi(this); connect(ui.buttonBox, SIGNAL(accepted()), this, SLOT(accept())); connect(ui.buttonBox, SIGNAL(rejected()), this, SLOT(reject())); connect(ui.comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndexChanged(int))); elementPool = new ElementPoolWidget(ui.elementPool); - elementPool->setElementList(elementManager->elements); + elements = elementManager->elements; + elementsCheck(folderLayer); + elementPool->setElementList(elements); +} + +void LayerCreateWidget::elementsCheck(FolderLayerWrapper* parent) +{ + std::set upSet, downSet; + parent->collectUpReachable(upSet); + for (auto it = elements.begin(); it != elements.end();) + { + bool valid = true; + auto ele = dynamic_cast(*it); + if (ele != nullptr) + { + downSet.clear(); + ele->collectReachable(downSet); + for (auto& layer : downSet) + { + if (upSet.find(layer) != upSet.end()) + { + valid = false; + break; + } + } + } + if (valid) + ++it; + else + it = elements.erase(it); + } } LayerCreateWidget::~LayerCreateWidget() @@ -21,8 +52,20 @@ void LayerCreateWidget::accept() QJsonObject jsonObj; jsonObj.insert("name", ui.name->text()); if (ui.comboBox->currentIndex() == 0) { + auto currentEle = elements[elementPool->currentIndex]; + int index = -1; + for (int i = 0; i < elementManager->elements.size(); i++) + { + if (elementManager->elements[i] == currentEle) + { + index = i; + break; + } + } + if (index == -1) + return; jsonObj.insert("is-folder", false); - jsonObj.insert("element", elementPool->currentIndex); + jsonObj.insert("element", index); } else { jsonObj.insert("is-folder", true); diff --git a/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/LayerCreateWidget.h b/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/LayerCreateWidget.h index c9b7c89..38456b2 100644 --- a/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/LayerCreateWidget.h +++ b/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/LayerCreateWidget.h @@ -11,12 +11,15 @@ class LayerCreateWidget : Q_OBJECT private: + ElementManager* elementManager; + std::vector elements; Ui::LayerCreateWidget ui; ElementPoolWidget* elementPool; + void elementsCheck(FolderLayerWrapper*); public: - LayerCreateWidget(ElementManager* elementManager,QWidget* parent = nullptr); + LayerCreateWidget(ElementManager* elementManager, FolderLayerWrapper* folderLayer, QWidget* parent = nullptr); ~LayerCreateWidget(); void accept() override; diff --git a/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp b/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp index 8896f9e..5c0acb0 100644 --- a/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp +++ b/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp @@ -148,4 +148,12 @@ QPixmap GroupElement::getPreview(QSize size) rect.setBottomRight(rect.bottomRight() + QPoint(5, 5)); result = result.copy(rect); return result.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation); +} + +void GroupElement::collectReachable(std::set& set) const +{ + if (sourceLayer != nullptr) + { + sourceLayer->collectDownReachable(set); + } } \ No newline at end of file diff --git a/ArchitectureColoredPainting/src/Editor/GraphicElement.h b/ArchitectureColoredPainting/src/Editor/GraphicElement.h index 12e2cfa..d0500b2 100644 --- a/ArchitectureColoredPainting/src/Editor/GraphicElement.h +++ b/ArchitectureColoredPainting/src/Editor/GraphicElement.h @@ -66,6 +66,7 @@ public: void setSourceLayer(FolderLayerWrapper* sourceLayer); void paint(QPainter* painter, QTransform transform, const LayerStyleContainer& styles) override; QPixmap getPreview(QSize size) override; + void collectReachable(std::set& set) const; }; //******************************** BitmapPath ********************************// diff --git a/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp b/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp index 9281248..e30b068 100644 --- a/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp +++ b/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp @@ -285,4 +285,36 @@ void LeafLayerWrapper::paint(QPainter* painter, QTransform transform, bool ignor { wrappedElement->paint(painter, transform, styles); } +} + +void LayerWrapper::collectUpReachable(std::set& reachable) +{ + auto cPos = this; + while (cPos != nullptr) + { + reachable.insert(cPos); + cPos = cPos->parent; + } +} + +void LayerWrapper::collectDownReachable(std::set& reachable) +{ + reachable.insert(this); +} + +void LeafLayerWrapper::collectDownReachable(std::set& reachable) +{ + LayerWrapper::collectDownReachable(reachable); + auto ele = dynamic_cast(wrappedElement); + if (ele != nullptr) + { + ele->collectReachable(reachable); + } +} + +void FolderLayerWrapper::collectDownReachable(std::set& reachable) +{ + LayerWrapper::collectDownReachable(reachable); + for (auto& child : children) + child->collectDownReachable(reachable); } \ No newline at end of file diff --git a/ArchitectureColoredPainting/src/Editor/LayerWrapper.h b/ArchitectureColoredPainting/src/Editor/LayerWrapper.h index dd87c4f..92d82d1 100644 --- a/ArchitectureColoredPainting/src/Editor/LayerWrapper.h +++ b/ArchitectureColoredPainting/src/Editor/LayerWrapper.h @@ -68,7 +68,8 @@ class LayerWrapper virtual void delSelf(); virtual QJsonObject toJson() const; ~LayerWrapper() = default; - + virtual void collectUpReachable(std::set& reachable); + virtual void collectDownReachable(std::set& reachable); }; class FolderLayerWrapper : public LayerWrapper @@ -92,6 +93,7 @@ class FolderLayerWrapper : public LayerWrapper QJsonObject toJson() const override; int getReferencedBy()const; void paint(QPainter* painter, QTransform transform = QTransform(), bool ignoreSelected = false) override; + void collectDownReachable(std::set& reachable) override; }; class LeafLayerWrapper : public LayerWrapper @@ -107,6 +109,7 @@ class LeafLayerWrapper : public LayerWrapper LeafLayerWrapper(QJsonObject json, ElementManager *elementManager, FolderLayerWrapper*parent); QJsonObject toJson() const override; void paint(QPainter* painter, QTransform transform = QTransform(), bool ignoreSelected = false) override; + void collectDownReachable(std::set& reachable) override; }; Q_DECLARE_METATYPE(LayerWrapper *) diff --git a/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.cpp b/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.cpp index 13f8e4e..8b85c1a 100644 --- a/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.cpp +++ b/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.cpp @@ -50,7 +50,7 @@ void LayerTreeWidget::popMenu(const QPoint &pos) if (layer != nullptr) { if (typeid(*layer) == typeid(FolderLayerWrapper)) { menu.addAction(QString::fromLocal8Bit("创建子节点"), this, [this, layer]() { - auto dialog = new LayerCreateWidget(elementManager, this); + auto dialog = new LayerCreateWidget(elementManager, dynamic_cast(layer), this); connect(dialog, &LayerCreateWidget::LayerInfoReturned, this, [this, layer](QJsonObject jsonObj) { auto folderLayer = dynamic_cast(layer); LayerWrapper* newLayer; @@ -76,6 +76,7 @@ void LayerTreeWidget::popMenu(const QPoint &pos) layer->getParent()->removeChild(layer); this->refresh(); emit requireRefreshPreview(); + emit requireRefreshElementWidget(); }); menu.addAction(QString::fromLocal8Bit("重命名"), this, &LayerTreeWidget::onRenameEvent); if(typeid(*layer) == typeid(FolderLayerWrapper)) @@ -85,6 +86,7 @@ void LayerTreeWidget::popMenu(const QPoint &pos) layer->getParent()->removeChild(layer); this->refresh(); emit requireRefreshPreview(); + emit requireRefreshElementWidget(); }); } if (typeid(*layer) == typeid(FolderLayerWrapper) && ((FolderLayerWrapper*)layer)->getReferencedBy() == -1) {