From 4b2776d4297df8e4ce49dfa77c26505becde97bf Mon Sep 17 00:00:00 2001 From: ArgonarioD Date: Mon, 20 Mar 2023 19:03:15 +0800 Subject: [PATCH] =?UTF-8?q?[editor/style]=20=E4=BC=98=E5=8C=96=E4=BA=86Str?= =?UTF-8?q?oke=E7=9A=84=E6=B7=BB=E5=8A=A0=E4=BA=A4=E4=BA=92=20|=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: