将LayerStyleDialog改为了修改拷贝对象

MaterialStyle的拷贝有问题,导致编译报错
TaoZhang-Branch
ArgonarioD 2023-03-10 17:00:52 +08:00
parent 293c1f3096
commit b981056674
5 changed files with 37 additions and 15 deletions

View File

@ -18,10 +18,10 @@ LayerStyleDialog::LayerStyleDialog(
if (existedStyle) if (existedStyle)
{ {
this->layerStyle = existedStyle; this->modifyingStyle = std::make_unique<LayerStyle>(existedStyle->clonePtr());
this->styleContainer = nullptr; this->styleContainer = nullptr;
this->styleWidget = layerStyle->getInputWidget(); this->styleWidget = modifyingStyle->getInputWidget();
this->styleWidget->setParent(this); this->styleWidget->setParent(this);
dialogLayout->addWidget(styleWidget); dialogLayout->addWidget(styleWidget);
// do something // do something
@ -41,9 +41,9 @@ LayerStyleDialog::LayerStyleDialog(
if (!excludeStyleNames.contains(pair.first)) if (!excludeStyleNames.contains(pair.first))
{ {
typeSelector->addItem(pair.first); typeSelector->addItem(pair.first);
if (!this->layerStyle) if (!this->modifyingStyle)
{ {
this->layerStyle = pair.second(); this->modifyingStyle = std::move(pair.second());
} }
} }
} }
@ -54,7 +54,7 @@ LayerStyleDialog::LayerStyleDialog(
this->styleContainer = new QGridLayout(this); this->styleContainer = new QGridLayout(this);
dialogLayout->addLayout(styleContainer); dialogLayout->addLayout(styleContainer);
this->styleWidget = this->layerStyle->getInputWidget(); this->styleWidget = this->modifyingStyle->getInputWidget();
this->styleWidget->setParent(this); this->styleWidget->setParent(this);
this->styleContainer->addWidget(styleWidget); this->styleContainer->addWidget(styleWidget);
connect(typeSelector, QOverload<int>::of(&QComboBox::currentIndexChanged), connect(typeSelector, QOverload<int>::of(&QComboBox::currentIndexChanged),
@ -67,6 +67,12 @@ LayerStyleDialog::LayerStyleDialog(
dialogLayout->addWidget(buttonBox); dialogLayout->addWidget(buttonBox);
} }
void LayerStyleDialog::accept()
{
this->layerStyle = std::move(this->modifyingStyle);
QDialog::accept();
}
void LayerStyleDialog::onStyleTypeSelectorChanged(int index) void LayerStyleDialog::onStyleTypeSelectorChanged(int index)
{ {
if (this->styleWidget) if (this->styleWidget)
@ -75,8 +81,8 @@ void LayerStyleDialog::onStyleTypeSelectorChanged(int index)
this->styleWidget->setParent(nullptr); this->styleWidget->setParent(nullptr);
delete styleWidget; delete styleWidget;
} }
this->layerStyle = std::move(LayerStyle::types[index].second()); this->modifyingStyle = std::move(LayerStyle::types[index].second());
this->styleWidget = this->layerStyle->getInputWidget(); this->styleWidget = this->modifyingStyle->getInputWidget();
this->styleWidget->setParent(this); this->styleWidget->setParent(this);
this->styleContainer->addWidget(styleWidget, 0, 0, 1, 1); this->styleContainer->addWidget(styleWidget, 0, 0, 1, 1);
} }

View File

@ -9,6 +9,7 @@ class LayerStyleDialog : public QDialog
private: private:
QWidget* styleWidget; QWidget* styleWidget;
QGridLayout* styleContainer; QGridLayout* styleContainer;
std::unique_ptr<LayerStyle> modifyingStyle;
public: public:
LayerStyleDialog( LayerStyleDialog(
QWidget* parent = nullptr, QWidget* parent = nullptr,
@ -17,5 +18,6 @@ public:
std::shared_ptr<LayerStyle> layerStyle; std::shared_ptr<LayerStyle> layerStyle;
private slots: private slots:
void onStyleTypeSelectorChanged(int index); void onStyleTypeSelectorChanged(int index);
void accept() override;
}; };

View File

@ -5,14 +5,14 @@
#include <QLabel> #include <QLabel>
#include <QLineEdit> #include <QLineEdit>
const std::vector<std::pair<QString, std::function<std::shared_ptr<LayerStyle>()>>> LayerStyle::types = { const std::vector<std::pair<QString, std::function<std::unique_ptr<LayerStyle>()>>> LayerStyle::types = {
{ {
QStringLiteral("Ãè±ß"), QStringLiteral("Ãè±ß"),
[]() { return std::make_shared<StrokeElementLayerStyle>(); } []() { return std::make_unique<StrokeElementLayerStyle>(); }
}, },
{ {
QStringLiteral("Ìî³ä"), QStringLiteral("Ìî³ä"),
[]() { return std::make_shared<FillElementLayerStyle>(); } []() { return std::make_unique<FillElementLayerStyle>(); }
} }
}; };
@ -45,7 +45,7 @@ QWidget* StrokeElementLayerStyle::getListDisplayWidget() const
return w; return w;
} }
StrokeElementLayerStyle::StrokeElementLayerStyle(StrokeElementLayerStyle& other) StrokeElementLayerStyle::StrokeElementLayerStyle(const StrokeElementLayerStyle& other)
{ {
materialStyles = std::vector<std::shared_ptr<Renderer::MaterialStyleStroke>>(other.materialStyles.size()); materialStyles = std::vector<std::shared_ptr<Renderer::MaterialStyleStroke>>(other.materialStyles.size());
for (size_t i = 0; i < other.materialStyles.size(); i++) for (size_t i = 0; i < other.materialStyles.size(); i++)
@ -54,6 +54,11 @@ StrokeElementLayerStyle::StrokeElementLayerStyle(StrokeElementLayerStyle& other)
} }
} }
std::shared_ptr<LayerStyle> StrokeElementLayerStyle::clonePtr() const
{
return std::make_shared<LayerStyle>(StrokeElementLayerStyle(*this));
}
std::vector<Renderer::BaseStyle> FillElementLayerStyle::toBaseStyles() const std::vector<Renderer::BaseStyle> FillElementLayerStyle::toBaseStyles() const
{ {
return std::vector<Renderer::BaseStyle>(); return std::vector<Renderer::BaseStyle>();
@ -83,7 +88,7 @@ QWidget* FillElementLayerStyle::getListDisplayWidget() const
return w; return w;
} }
FillElementLayerStyle::FillElementLayerStyle(FillElementLayerStyle& other) FillElementLayerStyle::FillElementLayerStyle(const FillElementLayerStyle& other)
{ {
materialStyles = std::vector<std::shared_ptr<Renderer::MaterialStyleFill>>(other.materialStyles.size()); materialStyles = std::vector<std::shared_ptr<Renderer::MaterialStyleFill>>(other.materialStyles.size());
for (size_t i = 0; i < other.materialStyles.size(); i++) for (size_t i = 0; i < other.materialStyles.size(); i++)
@ -91,3 +96,8 @@ FillElementLayerStyle::FillElementLayerStyle(FillElementLayerStyle& other)
materialStyles[i] = std::make_shared<Renderer::MaterialStyleFill>(*other.materialStyles[i]); materialStyles[i] = std::make_shared<Renderer::MaterialStyleFill>(*other.materialStyles[i]);
} }
} }
std::shared_ptr<LayerStyle> FillElementLayerStyle::clonePtr() const
{
return std::make_shared<LayerStyle>(FillElementLayerStyle(*this));
}

View File

@ -19,11 +19,12 @@
class LayerStyle class LayerStyle
{ {
public: public:
const static std::vector<std::pair<QString, std::function<std::shared_ptr<LayerStyle>()>>> types; const static std::vector<std::pair<QString, std::function<std::unique_ptr<LayerStyle>()>>> types;
virtual QString getStyleName() const = 0; virtual QString getStyleName() const = 0;
virtual QWidget* getInputWidget() const = 0; virtual QWidget* getInputWidget() const = 0;
virtual QWidget* getListDisplayWidget() const = 0; virtual QWidget* getListDisplayWidget() const = 0;
virtual ~LayerStyle() {}; virtual ~LayerStyle() {};
virtual std::shared_ptr<LayerStyle> clonePtr() const = 0;
}; };
class StrokeElementLayerStyle : public Renderer::ElementStyle, public LayerStyle class StrokeElementLayerStyle : public Renderer::ElementStyle, public LayerStyle
@ -34,9 +35,10 @@ public:
QWidget* getInputWidget() const override; QWidget* getInputWidget() const override;
QWidget* getListDisplayWidget() const override; QWidget* getListDisplayWidget() const override;
StrokeElementLayerStyle() = default; StrokeElementLayerStyle() = default;
StrokeElementLayerStyle(StrokeElementLayerStyle& other); StrokeElementLayerStyle(const StrokeElementLayerStyle& other);
~StrokeElementLayerStyle() = default; ~StrokeElementLayerStyle() = default;
std::vector<std::shared_ptr<Renderer::MaterialStyleStroke>> materialStyles; std::vector<std::shared_ptr<Renderer::MaterialStyleStroke>> materialStyles;
std::shared_ptr<LayerStyle> clonePtr() const override;
}; };
class FillElementLayerStyle : public Renderer::ElementStyle, public LayerStyle class FillElementLayerStyle : public Renderer::ElementStyle, public LayerStyle
@ -47,7 +49,8 @@ public:
QWidget* getInputWidget() const override; QWidget* getInputWidget() const override;
QWidget* getListDisplayWidget() const override; QWidget* getListDisplayWidget() const override;
FillElementLayerStyle() = default; FillElementLayerStyle() = default;
FillElementLayerStyle(FillElementLayerStyle& other); FillElementLayerStyle(const FillElementLayerStyle& other);
~FillElementLayerStyle() = default; ~FillElementLayerStyle() = default;
std::vector<std::shared_ptr<Renderer::MaterialStyleFill>> materialStyles; std::vector<std::shared_ptr<Renderer::MaterialStyleFill>> materialStyles;
std::shared_ptr<LayerStyle> clonePtr() const override;
}; };

View File

@ -198,6 +198,7 @@ void InfoDisplayWidget::generateLayerForm()
if (dialog->layerStyle) if (dialog->layerStyle)
{ {
(*styleIterator) = dialog->layerStyle;
emit requireRefreshPreview(); emit requireRefreshPreview();
emit requireSelfRefresh(); emit requireSelfRefresh();
} }