[style/stroke] 完成宽度设置

dev-LayerStyle
ArgonarioD 2023-03-15 14:23:52 +08:00
parent 27c1d4d18a
commit 1e3bef78f4
3 changed files with 39 additions and 22 deletions

View File

@ -1,33 +1,51 @@
#include "StrokeStyleWidget.h" #include "StrokeStyleWidget.h"
#include "ColorPicker.h" #include "ColorPicker.h"
#include <qtmaterialraisedbutton.h> #include <qtmaterialraisedbutton.h>
#include <qtmaterialtextfield.h>
#include <limits>
#define radialStroke(stroke) std::dynamic_pointer_cast<Renderer::StrokeRadialGradient>(stroke->materialStroke)
constexpr int COLUMN_WIDTH = 0; constexpr int COLUMN_WIDTH = 0;
constexpr int COLUMN_COLOR = 1; constexpr int COLUMN_COLOR = 1;
constexpr int COLUMN_METALLIC = 2; 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;
// TODO: 加上新增行按钮和宽度设置 // TODO: 加上新增行按钮
// FIXME: Material的控件有显示bug
StrokeStyleWidget::StrokeStyleWidget( StrokeStyleWidget::StrokeStyleWidget(
std::shared_ptr<Renderer::StrokeRadialGradient> stroke, std::shared_ptr<Renderer::MaterialStyleStroke> stroke,
QWidget* parent QWidget* parent
) : QWidget(parent), stroke(stroke) ) : QWidget(parent), stroke(stroke)
{ {
this->viewLayout = new QVBoxLayout(this); this->viewLayout = new QVBoxLayout(this);
this->setLayout(viewLayout); this->setLayout(viewLayout);
initTable(stroke); QtMaterialTextField* widthField = new QtMaterialTextField(this);
widthField->setLabel(QStringLiteral("本侧描边宽度"));
widthField->setText(QString::number(stroke->halfWidth));
QDoubleValidator* widthValidator = new QDoubleValidator(0.1, std::numeric_limits<float>::max(), 3, widthField);
widthValidator->setNotation(QDoubleValidator::StandardNotation);
widthField->setValidator(widthValidator);
connect(widthField, &QtMaterialTextField::textChanged, [this, widthField](const QString& changed) {
if (widthField->hasAcceptableInput())
{
this->stroke->halfWidth = changed.toFloat();
}
});
viewLayout->addWidget(widthField);
initTable(std::dynamic_pointer_cast<Renderer::StrokeRadialGradient>(stroke->materialStroke));
viewLayout->addWidget(strokeTable); viewLayout->addWidget(strokeTable);
this->adjustSize(); this->adjustSize();
} }
void StrokeStyleWidget::initTable(std::shared_ptr<Renderer::StrokeRadialGradient>& stroke) 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(stroke->materialMap.size()); strokeTable->setRowCount(materialStroke->materialMap.size());
QStringList headers; QStringList headers;
headers << QStringLiteral("ÀëÐľàÀëÕ¼±È") headers << QStringLiteral("ÀëÐľàÀëÕ¼±È")
<< QStringLiteral("ÑÕÉ«") << QStringLiteral("ÑÕÉ«")
@ -35,7 +53,7 @@ void StrokeStyleWidget::initTable(std::shared_ptr<Renderer::StrokeRadialGradient
<< QStringLiteral("´Ö²Ú¶È") << QStringLiteral("´Ö²Ú¶È")
<< QStringLiteral("ÆäËû²Ù×÷"); << QStringLiteral("ÆäËû²Ù×÷");
strokeTable->setHorizontalHeaderLabels(headers); strokeTable->setHorizontalHeaderLabels(headers);
for (int row = 0; auto & strokePair : stroke->materialMap) for (int row = 0; auto & strokePair : materialStroke->materialMap)
{ {
QTableWidgetItem* widthItem = new QTableWidgetItem; QTableWidgetItem* widthItem = new QTableWidgetItem;
widthItem->setData(Qt::EditRole, strokePair.first); widthItem->setData(Qt::EditRole, strokePair.first);
@ -64,8 +82,8 @@ void StrokeStyleWidget::initTable(std::shared_ptr<Renderer::StrokeRadialGradient
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]() {
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);
}); });
row++; row++;
@ -94,24 +112,25 @@ void StrokeStyleWidget::onCellChanged(int row, int column)
return; return;
} }
auto changedWidth = strokeTable->item(row, COLUMN_WIDTH)->text().toFloat(); auto changedWidth = strokeTable->item(row, COLUMN_WIDTH)->text().toFloat();
switch (row) switch (column)
{ {
case COLUMN_WIDTH: case COLUMN_WIDTH:
{ {
float oldWidth = this->currentItemValue.toFloat(); float oldWidth = this->currentItemValue.toFloat();
auto node = stroke->materialMap.extract(oldWidth); auto node = radialStroke(stroke)->materialMap.extract(oldWidth);
node.key() = changedWidth; node.key() = changedWidth;
stroke->materialMap.insert(std::move(node)); radialStroke(stroke)->materialMap.insert(std::move(node));
strokeTable->sortItems(COLUMN_WIDTH);
break; break;
} }
case COLUMN_METALLIC: case COLUMN_METALLIC:
{ {
stroke->materialMap[changedWidth].metallic = changedItemValue; radialStroke(stroke)->materialMap[changedWidth].metallic = changedItemValue;
break; break;
} }
case COLUMN_ROUGHNESS: case COLUMN_ROUGHNESS:
{ {
stroke->materialMap[changedWidth].roughness = changedItemValue; radialStroke(stroke)->materialMap[changedWidth].roughness = changedItemValue;
break; break;
} }
} }

View File

@ -12,11 +12,11 @@ private:
QVBoxLayout* viewLayout; QVBoxLayout* viewLayout;
QTableWidget* strokeTable; QTableWidget* strokeTable;
void initTable(std::shared_ptr<Renderer::StrokeRadialGradient>& stroke); void initTable(std::shared_ptr<Renderer::StrokeRadialGradient> materialStroke);
public: public:
StrokeStyleWidget(std::shared_ptr<Renderer::StrokeRadialGradient> stroke, QWidget* parent = nullptr); StrokeStyleWidget(std::shared_ptr<Renderer::MaterialStyleStroke> stroke, QWidget* parent = nullptr);
std::shared_ptr<Renderer::StrokeRadialGradient> stroke; std::shared_ptr<Renderer::MaterialStyleStroke> stroke;
protected slots: protected slots:
void onCurrentItemChanged(QTableWidgetItem* current, QTableWidgetItem* previous); void onCurrentItemChanged(QTableWidgetItem* current, QTableWidgetItem* previous);

View File

@ -1,6 +1,7 @@
#include "LayerStyle.h" #include "LayerStyle.h"
#include "./EditorWidgetComponent/StrokeStyleWidget.h" #include "./EditorWidgetComponent/StrokeStyleWidget.h"
#include <qtmaterialcheckbox.h> #include <qtmaterialcheckbox.h>
#include <qtmaterialtextfield.h>
#include <QHBoxLayout> #include <QHBoxLayout>
#include <QDialogButtonBox> #include <QDialogButtonBox>
#include <QPushButton> #include <QPushButton>
@ -51,6 +52,7 @@ QWidget* StrokeElementLayerStyle::getInputWidget()
{ {
auto materialMap = std::map<float, Renderer::Material>(); auto materialMap = std::map<float, Renderer::Material>();
materialMap[0.3] = Renderer::Material{ QColor(0,255,255), 0.f, .8f }; materialMap[0.3] = Renderer::Material{ QColor(0,255,255), 0.f, .8f };
materialMap[0.6] = Renderer::Material{ QColor(50,165,100), 0.f, .8f };
materialMap[1.0] = Renderer::Material{ QColor(80,25,255), 0.f, .8f }; materialMap[1.0] = Renderer::Material{ QColor(80,25,255), 0.f, .8f };
this->strokePair.first = std::shared_ptr<Renderer::MaterialStyleStroke>(new Renderer::MaterialStyleStroke( this->strokePair.first = std::shared_ptr<Renderer::MaterialStyleStroke>(new Renderer::MaterialStyleStroke(
15, 15,
@ -79,9 +81,7 @@ QWidget* StrokeElementLayerStyle::getInputWidget()
QVBoxLayout* layout = new QVBoxLayout(w); QVBoxLayout* layout = new QVBoxLayout(w);
layout->setMargin(0); layout->setMargin(0);
StrokeStyleWidget* leftStrokeView = new StrokeStyleWidget( StrokeStyleWidget* leftStrokeView = new StrokeStyleWidget(this->strokePair.first, w);
std::dynamic_pointer_cast<Renderer::StrokeRadialGradient>(this->strokePair.first->materialStroke), w
);
layout->addWidget(leftStrokeView); layout->addWidget(leftStrokeView);
QtMaterialCheckBox* checkEachSideIndependent = new QtMaterialCheckBox(w); QtMaterialCheckBox* checkEachSideIndependent = new QtMaterialCheckBox(w);
@ -89,9 +89,7 @@ QWidget* StrokeElementLayerStyle::getInputWidget()
checkEachSideIndependent->setChecked(enableEachSideIndependent); checkEachSideIndependent->setChecked(enableEachSideIndependent);
layout->addWidget(checkEachSideIndependent); layout->addWidget(checkEachSideIndependent);
StrokeStyleWidget* rightStrokeView = new StrokeStyleWidget( StrokeStyleWidget* rightStrokeView = new StrokeStyleWidget(this->strokePair.second, w);
std::dynamic_pointer_cast<Renderer::StrokeRadialGradient>(this->strokePair.second->materialStroke), w
);
layout->addWidget(rightStrokeView); layout->addWidget(rightStrokeView);
rightStrokeView->setDisabled(!this->enableEachSideIndependent); rightStrokeView->setDisabled(!this->enableEachSideIndependent);