[editor/style] 优化了Stroke的添加交互 | #17

dev-LayerStyle
ArgonarioD 2023-03-20 19:03:15 +08:00
parent 831541ff36
commit 4b2776d429
2 changed files with 44 additions and 21 deletions

View File

@ -3,6 +3,7 @@
#include "../ColorHelper.hpp" #include "../ColorHelper.hpp"
#include <qtmaterialraisedbutton.h> #include <qtmaterialraisedbutton.h>
#include <limits> #include <limits>
#include <ranges>
constexpr int COLUMN_WIDTH = 0; constexpr int COLUMN_WIDTH = 0;
constexpr int COLUMN_COLOR = 1; constexpr int COLUMN_COLOR = 1;
@ -10,11 +11,22 @@ constexpr int COLUMN_METALLIC = 2;
constexpr int COLUMN_ROUGHNESS = 3; constexpr int COLUMN_ROUGHNESS = 3;
constexpr int COLUMN_OPERATIONS = 4; constexpr int COLUMN_OPERATIONS = 4;
inline Renderer::Material newMaterial()
{
return {ColorHelper::instance().getPrimary1()};
}
StrokeStyleWidget::StrokeStyleWidget( StrokeStyleWidget::StrokeStyleWidget(
std::shared_ptr<MaterialStyleStroke> stroke, std::shared_ptr<MaterialStyleStroke> stroke,
QWidget* parent QWidget* parent
) : QWidget(parent), stroke(stroke) ) : QWidget(parent), stroke(stroke)
{ {
auto& materialMap = radialStroke(stroke)->materialMap;
if (materialMap.empty())
{
materialMap[1.f] = newMaterial();
}
auto* viewLayout = new QVBoxLayout(this); auto* viewLayout = new QVBoxLayout(this);
this->setLayout(viewLayout); this->setLayout(viewLayout);
@ -30,8 +42,12 @@ StrokeStyleWidget::StrokeStyleWidget(
viewLayout->addWidget(strokeProperties); viewLayout->addWidget(strokeProperties);
viewLayout->addWidget(widthField); viewLayout->addWidget(widthField);
initTable(std::dynamic_pointer_cast<Renderer::StrokeRadialGradient>(stroke->materialStroke)); initTable(radialStroke(stroke));
viewLayout->addWidget(strokeTable); viewLayout->addWidget(strokeTable);
initAddButton();
viewLayout->addWidget(addButton);
this->adjustSize(); this->adjustSize();
} }
@ -83,13 +99,12 @@ void StrokeStyleWidget::initStrokeSettings()
}); });
} }
// TODO: 新增时参数校验
void StrokeStyleWidget::initTable(std::shared_ptr<Renderer::StrokeRadialGradient> materialStroke) void StrokeStyleWidget::initTable(std::shared_ptr<Renderer::StrokeRadialGradient> materialStroke)
{ {
this->strokeTable = new QTableWidget(this); this->strokeTable = new QTableWidget(this);
strokeTable->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContentsOnFirstShow); strokeTable->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContentsOnFirstShow);
strokeTable->setColumnCount(5); strokeTable->setColumnCount(5);
strokeTable->setRowCount(materialStroke->materialMap.size() + 1); strokeTable->setRowCount(materialStroke->materialMap.size());
QStringList headers; QStringList headers;
headers << QStringLiteral("ÀëÐľàÀëÕ¼±È") headers << QStringLiteral("ÀëÐľàÀëÕ¼±È")
<< QStringLiteral("ÑÕÉ«") << QStringLiteral("ÑÕÉ«")
@ -97,44 +112,45 @@ void StrokeStyleWidget::initTable(std::shared_ptr<Renderer::StrokeRadialGradient
<< QStringLiteral("´Ö²Ú¶È") << QStringLiteral("´Ö²Ú¶È")
<< QStringLiteral("ÆäËû²Ù×÷"); << QStringLiteral("ÆäËû²Ù×÷");
strokeTable->setHorizontalHeaderLabels(headers); strokeTable->setHorizontalHeaderLabels(headers);
strokeTable->setMinimumHeight(strokeTable->rowHeight(0) * 5);
strokeTable->setMinimumWidth(strokeTable->sizeHint().width());
int row = 0; 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++; row++;
} }
// 新增按钮 connect(strokeTable, &QTableWidget::currentItemChanged, this, &StrokeStyleWidget::onCurrentItemChanged);
auto* addButton = new QtMaterialRaisedButton("+", strokeTable); 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()); addButton->setBackgroundColor(ColorHelper::instance().getPrimary1());
strokeTable->setSpan(row, 0, 1, 5); connect(addButton, &QtMaterialRaisedButton::clicked, [this] {
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]() {
handlingRowInsert = true; handlingRowInsert = true;
auto materialMap = &radialStroke(this->stroke)->materialMap; auto materialMap = &radialStroke(this->stroke)->materialMap;
float newWidth; float newWidth;
if (materialMap->empty()) if (materialMap->empty())
{ {
newWidth = 0.1; newWidth = 1.f;
} }
else else
{ {
const auto lastPair = materialMap->rbegin(); const auto firstPair = materialMap->begin();
newWidth = lastPair->first + 0.01; newWidth = firstPair->first / 2;
} }
const Renderer::Material newMaterial(ColorHelper::instance().getPrimary1()); const Renderer::Material newMaterial(ColorHelper::instance().getPrimary1());
(*materialMap)[newWidth] = newMaterial; (*materialMap)[newWidth] = newMaterial;
int newRow = this->strokeTable->rowCount() - 1; int newRow = this->strokeTable->rowCount();
this->strokeTable->insertRow(newRow); this->strokeTable->insertRow(newRow);
setTableRow(newRow, newWidth, (*materialMap)[newWidth]); setTableRow(newRow, newWidth, (*materialMap)[newWidth]);
this->strokeTable->update(); this->strokeTable->update();
handlingRowInsert = false; 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) 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->setBackgroundColor(ColorHelper::instance().getPrimary1());
removeButton->setFixedSize(20, 20); removeButton->setFixedSize(20, 20);
strokeTable->setCellWidget(row, COLUMN_OPERATIONS, removeButton); 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()); radialStroke(this->stroke)->materialMap.erase(this->strokeTable->item(row, COLUMN_WIDTH)->text().toFloat());
this->strokeTable->removeRow(row); this->strokeTable->removeRow(row);
}); });
@ -199,9 +215,13 @@ void StrokeStyleWidget::onCellChanged(int row, int column)
{ {
float oldWidth = this->currentItemValue.toFloat(); float oldWidth = this->currentItemValue.toFloat();
auto node = radialStroke(stroke)->materialMap.extract(oldWidth); auto node = radialStroke(stroke)->materialMap.extract(oldWidth);
if (node.empty())
{
break;
}
node.key() = changedWidth; node.key() = changedWidth;
radialStroke(stroke)->materialMap.insert(std::move(node)); radialStroke(stroke)->materialMap.insert(std::move(node));
strokeTable->sortItems(COLUMN_WIDTH); strokeTable->sortItems(COLUMN_WIDTH, Qt::DescendingOrder);
break; break;
} }
case COLUMN_METALLIC: case COLUMN_METALLIC:

View File

@ -6,6 +6,7 @@
#include <QComboBox> #include <QComboBox>
#include <qtmaterialtextfield.h> #include <qtmaterialtextfield.h>
#include <qtmaterialcheckbox.h> #include <qtmaterialcheckbox.h>
#include <qtmaterialraisedbutton.h>
class StrokeStyleWidget : public QWidget class StrokeStyleWidget : public QWidget
{ {
Q_OBJECT Q_OBJECT
@ -16,10 +17,12 @@ private:
QComboBox* endTypeBox; QComboBox* endTypeBox;
QtMaterialTextField* widthField; QtMaterialTextField* widthField;
QTableWidget* strokeTable; QTableWidget* strokeTable;
QtMaterialRaisedButton* addButton;
bool handlingRowInsert = false; bool handlingRowInsert = false;
void initStrokeSettings(); void initStrokeSettings();
void initTable(std::shared_ptr<Renderer::StrokeRadialGradient> materialStroke); void initTable(std::shared_ptr<Renderer::StrokeRadialGradient> materialStroke);
void initAddButton();
void setTableRow(int row, float width, Renderer::Material& material); void setTableRow(int row, float width, Renderer::Material& material);
public: public: