[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 <qtmaterialraisedbutton.h>
#include <limits>
#include <ranges>
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<MaterialStyleStroke> 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<Renderer::StrokeRadialGradient>(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<Renderer::StrokeRadialGradient> 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_ptr<Renderer::StrokeRadialGradient
<< QStringLiteral("´Ö²Ú¶È")
<< QStringLiteral("ÆäËû²Ù×÷");
strokeTable->setHorizontalHeaderLabels(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:

View File

@ -6,6 +6,7 @@
#include <QComboBox>
#include <qtmaterialtextfield.h>
#include <qtmaterialcheckbox.h>
#include <qtmaterialraisedbutton.h>
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<Renderer::StrokeRadialGradient> materialStroke);
void initAddButton();
void setTableRow(int row, float width, Renderer::Material& material);
public: