[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 "ColorPicker.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_COLOR = 1;
constexpr int COLUMN_METALLIC = 2;
constexpr int COLUMN_ROUGHNESS = 3;
constexpr int COLUMN_OPERATIONS = 4;
// TODO: 加上新增行按钮和宽度设置
// TODO: 加上新增行按钮
// FIXME: Material的控件有显示bug
StrokeStyleWidget::StrokeStyleWidget(
std::shared_ptr<Renderer::StrokeRadialGradient> stroke,
std::shared_ptr<Renderer::MaterialStyleStroke> stroke,
QWidget* parent
) : QWidget(parent), stroke(stroke)
{
this->viewLayout = new QVBoxLayout(this);
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);
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);
strokeTable->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContentsOnFirstShow);
strokeTable->setColumnCount(5);
strokeTable->setRowCount(stroke->materialMap.size());
strokeTable->setRowCount(materialStroke->materialMap.size());
QStringList headers;
headers << QStringLiteral("ÀëÐľàÀëÕ¼±È")
<< QStringLiteral("ÑÕÉ«")
@ -35,7 +53,7 @@ void StrokeStyleWidget::initTable(std::shared_ptr<Renderer::StrokeRadialGradient
<< QStringLiteral("´Ö²Ú¶È")
<< QStringLiteral("ÆäËû²Ù×÷");
strokeTable->setHorizontalHeaderLabels(headers);
for (int row = 0; auto & strokePair : stroke->materialMap)
for (int row = 0; auto & strokePair : materialStroke->materialMap)
{
QTableWidgetItem* widthItem = new QTableWidgetItem;
widthItem->setData(Qt::EditRole, strokePair.first);
@ -64,8 +82,8 @@ void StrokeStyleWidget::initTable(std::shared_ptr<Renderer::StrokeRadialGradient
removeButton->setFixedSize(20, 20);
strokeTable->setCellWidget(row, COLUMN_OPERATIONS, removeButton);
connect(removeButton, &QtMaterialRaisedButton::clicked, [this, row]() {
this->stroke->materialMap.erase(this->strokeTable->item(row, COLUMN_WIDTH)->text().toFloat());
this->strokeTable->removeRow(row);
radialStroke(this->stroke)->materialMap.erase(this->strokeTable->item(row, COLUMN_WIDTH)->text().toFloat());
this->strokeTable->removeRow(row);
});
row++;
@ -94,24 +112,25 @@ void StrokeStyleWidget::onCellChanged(int row, int column)
return;
}
auto changedWidth = strokeTable->item(row, COLUMN_WIDTH)->text().toFloat();
switch (row)
switch (column)
{
case COLUMN_WIDTH:
{
float oldWidth = this->currentItemValue.toFloat();
auto node = stroke->materialMap.extract(oldWidth);
auto node = radialStroke(stroke)->materialMap.extract(oldWidth);
node.key() = changedWidth;
stroke->materialMap.insert(std::move(node));
radialStroke(stroke)->materialMap.insert(std::move(node));
strokeTable->sortItems(COLUMN_WIDTH);
break;
}
case COLUMN_METALLIC:
{
stroke->materialMap[changedWidth].metallic = changedItemValue;
radialStroke(stroke)->materialMap[changedWidth].metallic = changedItemValue;
break;
}
case COLUMN_ROUGHNESS:
{
stroke->materialMap[changedWidth].roughness = changedItemValue;
radialStroke(stroke)->materialMap[changedWidth].roughness = changedItemValue;
break;
}
}

View File

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

View File

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