[stroke] 对接了Renderer方面提供的新接口

dev-LayerStyle
ArgonarioD 2023-03-20 19:31:16 +08:00
parent 1ef08dc49e
commit 21aeb0832b
2 changed files with 24 additions and 36 deletions

View File

@ -3,7 +3,9 @@
#include "../ColorHelper.hpp" #include "../ColorHelper.hpp"
#include <qtmaterialraisedbutton.h> #include <qtmaterialraisedbutton.h>
#include <limits> #include <limits>
#include <array>
#include <ranges> #include <ranges>
#include <utility>
constexpr int COLUMN_WIDTH = 0; constexpr int COLUMN_WIDTH = 0;
constexpr int COLUMN_COLOR = 1; constexpr int COLUMN_COLOR = 1;
@ -17,7 +19,7 @@ inline Renderer::Material newMaterial()
} }
StrokeStyleWidget::StrokeStyleWidget( StrokeStyleWidget::StrokeStyleWidget(
std::shared_ptr<MaterialStyleStroke> stroke, const std::shared_ptr<MaterialStyleStroke>& stroke,
QWidget* parent QWidget* parent
) : QWidget(parent), stroke(stroke) ) : QWidget(parent), stroke(stroke)
{ {
@ -59,30 +61,16 @@ void StrokeStyleWidget::initStrokeSettings()
connect(enableGradual, &QtMaterialCheckBox::toggled, [this](bool checked) { connect(enableGradual, &QtMaterialCheckBox::toggled, [this](bool checked) {
radialStroke(this->stroke)->gradual = checked; radialStroke(this->stroke)->gradual = checked;
}); });
#define endTypeBoxLabel(start, end) QStringLiteral(start##" -> "##end)
this->endTypeBox = new QComboBox(this); this->endTypeBox = new QComboBox(this);
endTypeBox->addItem(endTypeBoxLabel("Բͷ", "Բͷ")); // kRound for (const auto& displayName : MaterialStyleStroke::strokeEndTypeNames | std::views::keys)
endTypeBox->addItem(endTypeBoxLabel("ƽͷ", "Բͷ")); // kFlatRound {
endTypeBox->addItem(endTypeBoxLabel("Բͷ", "ƽͷ")); // kRoundFlat endTypeBox->addItem(displayName);
endTypeBox->addItem(endTypeBoxLabel("ƽͷ", "ƽͷ")); // kFlat }
endTypeBox->setCurrentIndex(static_cast<int>(this->stroke->endType)); endTypeBox->setCurrentIndex(static_cast<int>(this->stroke->endType));
connect(endTypeBox, QOverload<int>::of(&QComboBox::currentIndexChanged), [this](int index) { connect(endTypeBox, QOverload<int>::of(&QComboBox::currentIndexChanged), [this](int index) {
switch (index) const auto& [displayName, endType] = MaterialStyleStroke::strokeEndTypeNames[index];
{ this->stroke->endType = endType;
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;
}
}); });
this->widthField = new QtMaterialTextField(this); this->widthField = new QtMaterialTextField(this);
@ -99,7 +87,7 @@ void StrokeStyleWidget::initStrokeSettings()
}); });
} }
void StrokeStyleWidget::initTable(std::shared_ptr<Renderer::StrokeRadialGradient> materialStroke) void StrokeStyleWidget::initTable(const std::shared_ptr<Renderer::StrokeRadialGradient>& materialStroke)
{ {
this->strokeTable = new QTableWidget(this); this->strokeTable = new QTableWidget(this);
strokeTable->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContentsOnFirstShow); strokeTable->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContentsOnFirstShow);
@ -132,22 +120,22 @@ void StrokeStyleWidget::initAddButton()
addButton->setBackgroundColor(ColorHelper::instance().getPrimary1()); addButton->setBackgroundColor(ColorHelper::instance().getPrimary1());
connect(addButton, &QtMaterialRaisedButton::clicked, [this] { 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 = 1.f; newWidth = 1.f;
} }
else else
{ {
const auto firstPair = materialMap->begin(); const auto firstPair = materialMap.begin();
newWidth = firstPair->first / 2; 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(); const 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;
}); });
@ -166,7 +154,7 @@ void StrokeStyleWidget::setTableRow(int row, float width, Renderer::Material& ma
auto* colorPicker = new ColorPicker(*colorPtr, strokeTable); auto* colorPicker = new ColorPicker(*colorPtr, strokeTable);
strokeTable->setCellWidget(row, COLUMN_COLOR, colorPicker); strokeTable->setCellWidget(row, COLUMN_COLOR, colorPicker);
connect(colorPicker, &ColorPicker::colorChanged, [this, colorPtr](QColor color) { connect(colorPicker, &ColorPicker::colorChanged, [this, colorPtr](QColor color) {
*colorPtr = color; *colorPtr = std::move(color);
this->strokeTable->update(); this->strokeTable->update();
}); });
@ -191,7 +179,7 @@ void StrokeStyleWidget::setTableRow(int row, float width, Renderer::Material& ma
void StrokeStyleWidget::onCurrentItemChanged(QTableWidgetItem* current, QTableWidgetItem* previous) void StrokeStyleWidget::onCurrentItemChanged(QTableWidgetItem* current, QTableWidgetItem* previous)
{ {
if (!current) return; if (!current) return;
int column = current->column(); const int column = current->column();
if (column != COLUMN_COLOR && column != COLUMN_OPERATIONS) if (column != COLUMN_COLOR && column != COLUMN_OPERATIONS)
{ {
this->currentItemValue = current->data(Qt::EditRole); this->currentItemValue = current->data(Qt::EditRole);
@ -201,14 +189,14 @@ void StrokeStyleWidget::onCurrentItemChanged(QTableWidgetItem* current, QTableWi
void StrokeStyleWidget::onCellChanged(int row, int column) void StrokeStyleWidget::onCellChanged(int row, int column)
{ {
if (handlingRowInsert) return; if (handlingRowInsert) return;
auto changedItem = strokeTable->item(row, column); const auto changedItem = strokeTable->item(row, column);
auto changedItemValue = changedItem->text().toFloat(); const auto changedItemValue = changedItem->text().toFloat();
if (changedItemValue < 0 || 1 < changedItemValue) if (changedItemValue < 0 || 1 < changedItemValue)
{ {
changedItem->setData(Qt::EditRole, this->currentItemValue.toFloat()); changedItem->setData(Qt::EditRole, this->currentItemValue.toFloat());
return; 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) switch (column)
{ {
case COLUMN_WIDTH: case COLUMN_WIDTH:

View File

@ -21,12 +21,12 @@ private:
bool handlingRowInsert = false; bool handlingRowInsert = false;
void initStrokeSettings(); void initStrokeSettings();
void initTable(std::shared_ptr<Renderer::StrokeRadialGradient> materialStroke); void initTable(const std::shared_ptr<Renderer::StrokeRadialGradient>& materialStroke);
void initAddButton(); void initAddButton();
void setTableRow(int row, float width, Renderer::Material& material); void setTableRow(int row, float width, Renderer::Material& material);
public: public:
StrokeStyleWidget(std::shared_ptr<MaterialStyleStroke> stroke, QWidget* parent = nullptr); StrokeStyleWidget(const std::shared_ptr<MaterialStyleStroke>& stroke, QWidget* parent = nullptr);
std::shared_ptr<MaterialStyleStroke> stroke; std::shared_ptr<MaterialStyleStroke> stroke;
protected slots: protected slots: