From 4b2776d4297df8e4ce49dfa77c26505becde97bf Mon Sep 17 00:00:00 2001 From: ArgonarioD Date: Mon, 20 Mar 2023 19:03:15 +0800 Subject: [PATCH 1/2] =?UTF-8?q?[editor/style]=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E4=BA=86Stroke=E7=9A=84=E6=B7=BB=E5=8A=A0=E4=BA=A4=E4=BA=92=20?= =?UTF-8?q?|=20#17?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../StrokeStyleWidget.cpp | 62 ++++++++++++------- .../EditorWidgetComponent/StrokeStyleWidget.h | 3 + 2 files changed, 44 insertions(+), 21 deletions(-) diff --git a/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/StrokeStyleWidget.cpp b/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/StrokeStyleWidget.cpp index e9999bb..3984076 100644 --- a/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/StrokeStyleWidget.cpp +++ b/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/StrokeStyleWidget.cpp @@ -3,6 +3,7 @@ #include "../ColorHelper.hpp" #include #include +#include constexpr int COLUMN_WIDTH = 0; constexpr int COLUMN_COLOR = 1; @@ -10,11 +11,22 @@ constexpr int COLUMN_METALLIC = 2; constexpr int COLUMN_ROUGHNESS = 3; constexpr int COLUMN_OPERATIONS = 4; +inline Renderer::Material newMaterial() +{ + return {ColorHelper::instance().getPrimary1()}; +} + StrokeStyleWidget::StrokeStyleWidget( std::shared_ptr stroke, QWidget* parent ) : QWidget(parent), stroke(stroke) { + auto& materialMap = radialStroke(stroke)->materialMap; + if (materialMap.empty()) + { + materialMap[1.f] = newMaterial(); + } + auto* viewLayout = new QVBoxLayout(this); this->setLayout(viewLayout); @@ -30,8 +42,12 @@ StrokeStyleWidget::StrokeStyleWidget( viewLayout->addWidget(strokeProperties); viewLayout->addWidget(widthField); - initTable(std::dynamic_pointer_cast(stroke->materialStroke)); + initTable(radialStroke(stroke)); viewLayout->addWidget(strokeTable); + + initAddButton(); + viewLayout->addWidget(addButton); + this->adjustSize(); } @@ -83,13 +99,12 @@ void StrokeStyleWidget::initStrokeSettings() }); } -// TODO: 新增时参数校验 void StrokeStyleWidget::initTable(std::shared_ptr materialStroke) { this->strokeTable = new QTableWidget(this); strokeTable->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContentsOnFirstShow); strokeTable->setColumnCount(5); - strokeTable->setRowCount(materialStroke->materialMap.size() + 1); + strokeTable->setRowCount(materialStroke->materialMap.size()); QStringList headers; headers << QStringLiteral("离心距离占比") << QStringLiteral("颜色") @@ -97,44 +112,45 @@ void StrokeStyleWidget::initTable(std::shared_ptrsetHorizontalHeaderLabels(headers); + strokeTable->setMinimumHeight(strokeTable->rowHeight(0) * 5); + strokeTable->setMinimumWidth(strokeTable->sizeHint().width()); int row = 0; // 内容 - for (auto & strokePair : materialStroke->materialMap) + for (auto& [width, material] : std::views::reverse(materialStroke->materialMap)) { - setTableRow(row, strokePair.first, strokePair.second); + setTableRow(row, width, material); row++; } - // 新增按钮 - auto* addButton = new QtMaterialRaisedButton("+", strokeTable); + connect(strokeTable, &QTableWidget::currentItemChanged, this, &StrokeStyleWidget::onCurrentItemChanged); + connect(strokeTable, &QTableWidget::cellChanged, this, &StrokeStyleWidget::onCellChanged); +} + +void StrokeStyleWidget::initAddButton() +{ + this->addButton = new QtMaterialRaisedButton("+", strokeTable); + addButton->setFixedHeight(this->strokeTable->rowHeight(0)); addButton->setBackgroundColor(ColorHelper::instance().getPrimary1()); - strokeTable->setSpan(row, 0, 1, 5); - strokeTable->setCellWidget(row, 0, addButton); - strokeTable->setMinimumHeight(strokeTable->rowHeight(row) * 5); - strokeTable->setMinimumWidth(strokeTable->sizeHint().width()); - addButton->setFixedHeight(strokeTable->rowHeight(row)); - connect(addButton, &QtMaterialRaisedButton::clicked, [this]() { + connect(addButton, &QtMaterialRaisedButton::clicked, [this] { handlingRowInsert = true; auto materialMap = &radialStroke(this->stroke)->materialMap; float newWidth; if (materialMap->empty()) { - newWidth = 0.1; + newWidth = 1.f; } else { - const auto lastPair = materialMap->rbegin(); - newWidth = lastPair->first + 0.01; + const auto firstPair = materialMap->begin(); + newWidth = firstPair->first / 2; } const Renderer::Material newMaterial(ColorHelper::instance().getPrimary1()); (*materialMap)[newWidth] = newMaterial; - int newRow = this->strokeTable->rowCount() - 1; + int newRow = this->strokeTable->rowCount(); this->strokeTable->insertRow(newRow); setTableRow(newRow, newWidth, (*materialMap)[newWidth]); this->strokeTable->update(); handlingRowInsert = false; }); - connect(strokeTable, &QTableWidget::currentItemChanged, this, &StrokeStyleWidget::onCurrentItemChanged); - connect(strokeTable, &QTableWidget::cellChanged, this, &StrokeStyleWidget::onCellChanged); } void StrokeStyleWidget::setTableRow(int row, float width, Renderer::Material& material) @@ -166,7 +182,7 @@ void StrokeStyleWidget::setTableRow(int row, float width, Renderer::Material& ma removeButton->setBackgroundColor(ColorHelper::instance().getPrimary1()); removeButton->setFixedSize(20, 20); strokeTable->setCellWidget(row, COLUMN_OPERATIONS, removeButton); - connect(removeButton, &QtMaterialRaisedButton::clicked, [this, row]() { + connect(removeButton, &QtMaterialRaisedButton::clicked, [this, row] { radialStroke(this->stroke)->materialMap.erase(this->strokeTable->item(row, COLUMN_WIDTH)->text().toFloat()); this->strokeTable->removeRow(row); }); @@ -199,9 +215,13 @@ void StrokeStyleWidget::onCellChanged(int row, int column) { float oldWidth = this->currentItemValue.toFloat(); auto node = radialStroke(stroke)->materialMap.extract(oldWidth); + if (node.empty()) + { + break; + } node.key() = changedWidth; radialStroke(stroke)->materialMap.insert(std::move(node)); - strokeTable->sortItems(COLUMN_WIDTH); + strokeTable->sortItems(COLUMN_WIDTH, Qt::DescendingOrder); break; } case COLUMN_METALLIC: diff --git a/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/StrokeStyleWidget.h b/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/StrokeStyleWidget.h index 67af3ee..d0bd250 100644 --- a/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/StrokeStyleWidget.h +++ b/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/StrokeStyleWidget.h @@ -6,6 +6,7 @@ #include #include #include +#include class StrokeStyleWidget : public QWidget { Q_OBJECT @@ -16,10 +17,12 @@ private: QComboBox* endTypeBox; QtMaterialTextField* widthField; QTableWidget* strokeTable; + QtMaterialRaisedButton* addButton; bool handlingRowInsert = false; void initStrokeSettings(); void initTable(std::shared_ptr materialStroke); + void initAddButton(); void setTableRow(int row, float width, Renderer::Material& material); public: From bf71d17b5d31abe029fa35e38ad21e55b70936ed Mon Sep 17 00:00:00 2001 From: ArgonarioD Date: Mon, 20 Mar 2023 19:58:02 +0800 Subject: [PATCH 2/2] =?UTF-8?q?[editor/style]=20=E9=80=82=E9=85=8D?= =?UTF-8?q?=E4=BA=86=E6=96=B0=E7=9A=84StrokeEndType=20|=20#18=20=20*=20[st?= =?UTF-8?q?roke]=20=E9=80=82=E9=85=8D=E4=BA=86=E6=96=B0=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E7=9A=84StrokeEndType::kClosed=20=20*=20[stroke]=20=E5=AF=B9?= =?UTF-8?q?=E6=8E=A5=E4=BA=86Renderer=E6=96=B9=E9=9D=A2=E6=8F=90=E4=BE=9B?= =?UTF-8?q?=E7=9A=84=E6=96=B0=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../StrokeStyleWidget.cpp | 75 ++++++++++--------- .../EditorWidgetComponent/StrokeStyleWidget.h | 4 +- .../src/Editor/LayerStyle.cpp | 8 +- .../src/Editor/LayerStyle.h | 24 +++--- 4 files changed, 60 insertions(+), 51 deletions(-) diff --git a/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/StrokeStyleWidget.cpp b/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/StrokeStyleWidget.cpp index 3984076..7422763 100644 --- a/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/StrokeStyleWidget.cpp +++ b/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/StrokeStyleWidget.cpp @@ -3,7 +3,9 @@ #include "../ColorHelper.hpp" #include #include +#include #include +#include constexpr int COLUMN_WIDTH = 0; constexpr int COLUMN_COLOR = 1; @@ -16,8 +18,13 @@ inline Renderer::Material newMaterial() return {ColorHelper::instance().getPrimary1()}; } +inline bool isClosedStroke(const std::shared_ptr& stroke) +{ + return stroke->endType == Renderer::StrokeEndType::kClosed; +} + StrokeStyleWidget::StrokeStyleWidget( - std::shared_ptr stroke, + const std::shared_ptr& stroke, QWidget* parent ) : QWidget(parent), stroke(stroke) { @@ -37,7 +44,10 @@ StrokeStyleWidget::StrokeStyleWidget( strokeProperties->setLayout(strokePropertiesLayout); strokePropertiesLayout->addWidget(enableGradual); - strokePropertiesLayout->addWidget(endTypeBox); + if (!isClosedStroke(stroke)) + { + strokePropertiesLayout->addWidget(endTypeBox); + } viewLayout->addWidget(strokeProperties); viewLayout->addWidget(widthField); @@ -60,30 +70,23 @@ void StrokeStyleWidget::initStrokeSettings() radialStroke(this->stroke)->gradual = checked; }); -#define endTypeBoxLabel(start, end) QStringLiteral(start##" -> "##end) - this->endTypeBox = new QComboBox(this); - endTypeBox->addItem(endTypeBoxLabel("圆头", "圆头")); // kRound - endTypeBox->addItem(endTypeBoxLabel("平头", "圆头")); // kFlatRound - endTypeBox->addItem(endTypeBoxLabel("圆头", "平头")); // kRoundFlat - endTypeBox->addItem(endTypeBoxLabel("平头", "平头")); // kFlat - endTypeBox->setCurrentIndex(static_cast(this->stroke->endType)); - connect(endTypeBox, QOverload::of(&QComboBox::currentIndexChanged), [this](int index) { - switch (index) + if (!isClosedStroke(stroke)) + { + this->endTypeBox = new QComboBox(this); + for (const auto& displayName : MaterialStyleStroke::strokeEndTypeNames | std::views::keys) { - case 0: - this->stroke->endType = Renderer::StrokeEndType::kRound; - break; - case 1: - this->stroke->endType = Renderer::StrokeEndType::kFlatRound; - break; - case 2: - this->stroke->endType = Renderer::StrokeEndType::kRoundFlat; - break; - case 3: - this->stroke->endType = Renderer::StrokeEndType::kFlat; - break; + endTypeBox->addItem(displayName); } - }); + endTypeBox->setCurrentIndex(static_cast(this->stroke->endType)); + connect(endTypeBox, QOverload::of(&QComboBox::currentIndexChanged), [this](int index) { + const auto& [displayName, endType] = MaterialStyleStroke::strokeEndTypeNames[index]; + this->stroke->endType = endType; + }); + } + else + { + this->endTypeBox = nullptr; + } this->widthField = new QtMaterialTextField(this); widthField->setLabel(QStringLiteral("本侧描边宽度")); @@ -99,7 +102,7 @@ void StrokeStyleWidget::initStrokeSettings() }); } -void StrokeStyleWidget::initTable(std::shared_ptr materialStroke) +void StrokeStyleWidget::initTable(const std::shared_ptr& materialStroke) { this->strokeTable = new QTableWidget(this); strokeTable->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContentsOnFirstShow); @@ -132,22 +135,22 @@ void StrokeStyleWidget::initAddButton() addButton->setBackgroundColor(ColorHelper::instance().getPrimary1()); connect(addButton, &QtMaterialRaisedButton::clicked, [this] { handlingRowInsert = true; - auto materialMap = &radialStroke(this->stroke)->materialMap; + auto& materialMap = radialStroke(this->stroke)->materialMap; float newWidth; - if (materialMap->empty()) + if (materialMap.empty()) { newWidth = 1.f; } else { - const auto firstPair = materialMap->begin(); + const auto firstPair = materialMap.begin(); newWidth = firstPair->first / 2; } const Renderer::Material newMaterial(ColorHelper::instance().getPrimary1()); - (*materialMap)[newWidth] = newMaterial; - int newRow = this->strokeTable->rowCount(); + materialMap[newWidth] = newMaterial; + const int newRow = this->strokeTable->rowCount(); this->strokeTable->insertRow(newRow); - setTableRow(newRow, newWidth, (*materialMap)[newWidth]); + setTableRow(newRow, newWidth, materialMap[newWidth]); this->strokeTable->update(); handlingRowInsert = false; }); @@ -166,7 +169,7 @@ void StrokeStyleWidget::setTableRow(int row, float width, Renderer::Material& ma auto* colorPicker = new ColorPicker(*colorPtr, strokeTable); strokeTable->setCellWidget(row, COLUMN_COLOR, colorPicker); connect(colorPicker, &ColorPicker::colorChanged, [this, colorPtr](QColor color) { - *colorPtr = color; + *colorPtr = std::move(color); this->strokeTable->update(); }); @@ -191,7 +194,7 @@ void StrokeStyleWidget::setTableRow(int row, float width, Renderer::Material& ma void StrokeStyleWidget::onCurrentItemChanged(QTableWidgetItem* current, QTableWidgetItem* previous) { if (!current) return; - int column = current->column(); + const int column = current->column(); if (column != COLUMN_COLOR && column != COLUMN_OPERATIONS) { this->currentItemValue = current->data(Qt::EditRole); @@ -201,14 +204,14 @@ void StrokeStyleWidget::onCurrentItemChanged(QTableWidgetItem* current, QTableWi void StrokeStyleWidget::onCellChanged(int row, int column) { if (handlingRowInsert) return; - auto changedItem = strokeTable->item(row, column); - auto changedItemValue = changedItem->text().toFloat(); + const auto changedItem = strokeTable->item(row, column); + const auto changedItemValue = changedItem->text().toFloat(); if (changedItemValue < 0 || 1 < changedItemValue) { changedItem->setData(Qt::EditRole, this->currentItemValue.toFloat()); return; } - auto changedWidth = strokeTable->item(row, COLUMN_WIDTH)->data(Qt::EditRole).toFloat(); + const auto changedWidth = strokeTable->item(row, COLUMN_WIDTH)->data(Qt::EditRole).toFloat(); switch (column) { case COLUMN_WIDTH: diff --git a/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/StrokeStyleWidget.h b/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/StrokeStyleWidget.h index d0bd250..c4d1bfc 100644 --- a/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/StrokeStyleWidget.h +++ b/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/StrokeStyleWidget.h @@ -21,12 +21,12 @@ private: bool handlingRowInsert = false; void initStrokeSettings(); - void initTable(std::shared_ptr materialStroke); + void initTable(const std::shared_ptr& materialStroke); void initAddButton(); void setTableRow(int row, float width, Renderer::Material& material); public: - StrokeStyleWidget(std::shared_ptr stroke, QWidget* parent = nullptr); + StrokeStyleWidget(const std::shared_ptr& stroke, QWidget* parent = nullptr); std::shared_ptr stroke; protected slots: diff --git a/ArchitectureColoredPainting/src/Editor/LayerStyle.cpp b/ArchitectureColoredPainting/src/Editor/LayerStyle.cpp index 292d0ca..d8bbae3 100644 --- a/ArchitectureColoredPainting/src/Editor/LayerStyle.cpp +++ b/ArchitectureColoredPainting/src/Editor/LayerStyle.cpp @@ -78,7 +78,7 @@ QWidget* StrokeElementLayerStyle::getListDisplayWidget() const void LayerStyleContainer::computeNewHash() { hash = 0; - for (auto& f : styles + for (auto& f : styles | std::views::values | std::views::transform(&LayerStyle::toBaseStyles) | std::views::join @@ -248,18 +248,18 @@ std::unique_ptr StrokeElementLayerStyle::fromJson(const return ptr; } -StrokeElementLayerStyle::StrokeElementLayerStyle() +StrokeElementLayerStyle::StrokeElementLayerStyle(bool isClosed) { const auto materialMap = std::map(); this->strokePair.first = std::make_shared( 7, - Renderer::StrokeType::kLeftSide, Renderer::StrokeEndType::kFlat, + Renderer::StrokeType::kLeftSide, isClosed ? Renderer::StrokeEndType::kClosed : Renderer::StrokeEndType::kFlat, std::make_shared(materialMap, false) ); this->strokePair.second = std::make_shared( 7, - Renderer::StrokeType::kRightSide, Renderer::StrokeEndType::kFlat, + Renderer::StrokeType::kRightSide, isClosed ? Renderer::StrokeEndType::kClosed : Renderer::StrokeEndType::kFlat, std::make_shared(materialMap, false) ); diff --git a/ArchitectureColoredPainting/src/Editor/LayerStyle.h b/ArchitectureColoredPainting/src/Editor/LayerStyle.h index 9274732..3eba1ad 100644 --- a/ArchitectureColoredPainting/src/Editor/LayerStyle.h +++ b/ArchitectureColoredPainting/src/Editor/LayerStyle.h @@ -46,7 +46,7 @@ public: STYLE_NAME("描边", "stroke") static std::unique_ptr fromJson(const QJsonObject& json); - StrokeElementLayerStyle(); + StrokeElementLayerStyle(bool isClosed); StrokeElementLayerStyle(const PMaterialStyleStroke& left, const PMaterialStyleStroke& right = nullptr); StrokeElementLayerStyle(const StrokeElementLayerStyle& other); ~StrokeElementLayerStyle() override = default; @@ -88,17 +88,23 @@ public: class LayerStyleContainer : public Renderer::ElementStyle { using DisplayNameWithSupplier = std::map()>>; -private: - inline const static DisplayNameWithSupplier commonStyles = { { +private: + inline const static DisplayNameWithSupplier commonStyles = { }; + inline const static DisplayNameWithSupplier closedOnlyStyles = { + { + FillElementLayerStyle::displayName(), + [] { return std::make_unique(); } + }, + { + StrokeElementLayerStyle::displayName(), + [] { return std::make_unique(true); } + } + }; + inline const static DisplayNameWithSupplier unclosedOnlyStyles = { { StrokeElementLayerStyle::displayName(), - [] { return std::make_unique(); } + [] { return std::make_unique(false); } } }; - inline const static DisplayNameWithSupplier closedOnlyStyles = { { - FillElementLayerStyle::displayName(), - [] { return std::make_unique(); } - } }; - inline const static DisplayNameWithSupplier unclosedOnlyStyles = { }; DisplayNameWithSupplier unusedStyles; DisplayNameWithSupplier usedStyles;