[editor] 修改了style相关的一些东西 #30

* 将LayerStyleContainer的覆盖运算符从<<改为了 |
 * 调整了LayerContainerListWidget的UI
 * fix: 移除描边时可能导致空指针异常
dev-wuyize
ArgonarioD 2023-03-23 15:02:44 +08:00
parent 483afb9fb4
commit 214fa0f82f
8 changed files with 50 additions and 30 deletions

View File

@ -71,7 +71,7 @@
</widget> </widget>
</item> </item>
<item row="5" column="0" colspan="2"> <item row="5" column="0" colspan="2">
<widget class="LayerContainerListWidget" name="styleList"/> <widget class="LayerContainerListWidget" name="styleList" native="true"/>
</item> </item>
</layout> </layout>
</widget> </widget>
@ -88,7 +88,7 @@
</customwidget> </customwidget>
<customwidget> <customwidget>
<class>LayerContainerListWidget</class> <class>LayerContainerListWidget</class>
<extends>QListWidget</extends> <extends>QWidget</extends>
<header location="global">EditorWidgetComponent/LayerContainerListWidget.h</header> <header location="global">EditorWidgetComponent/LayerContainerListWidget.h</header>
</customwidget> </customwidget>
</customwidgets> </customwidgets>

View File

@ -1,17 +1,28 @@
#include "LayerContainerListWidget.h" #include "LayerContainerListWidget.h"
#include "LayerStyleDialog.h" #include "LayerStyleDialog.h"
#include "../ColorHelper.hpp"
#include <QLabel> #include <QLabel>
#include <ranges> #include <ranges>
inline void initMaterialButton(QtMaterialRaisedButton* button)
{
button->setFixedHeight(25);
button->setBackgroundColor(ColorHelper::instance().getPrimary1());
}
LayerContainerListWidget::LayerContainerListWidget(QWidget* parent, const PLayerStyleContainer& styleContainer) LayerContainerListWidget::LayerContainerListWidget(QWidget* parent, const PLayerStyleContainer& styleContainer)
: QListWidget(parent), styleContainer(styleContainer) : QWidget(parent), headerWidget(new QWidget(this)), styleList(new QListWidget(this)), styleContainer(styleContainer)
{ {
connect(this, &LayerContainerListWidget::addLayerStyle, connect(this, &LayerContainerListWidget::addLayerStyle,
this, &LayerContainerListWidget::onAddLayerStyle); this, &LayerContainerListWidget::onAddLayerStyle);
connect(this, &LayerContainerListWidget::removeLayerStyle, connect(this, &LayerContainerListWidget::removeLayerStyle,
this, &LayerContainerListWidget::onRemoveLayerStyle); this, &LayerContainerListWidget::onRemoveLayerStyle);
auto* widgetLayout = new QVBoxLayout(this);
initHeader(); initHeader();
widgetLayout->addWidget(this->headerWidget);
widgetLayout->addWidget(this->styleList);
setStyleContainer(styleContainer, true); setStyleContainer(styleContainer, true);
} }
@ -35,12 +46,7 @@ void LayerContainerListWidget::setStyleContainer(const PLayerStyleContainer& sty
return; return;
} }
const int count = this->count(); styleList->clear();
for (int i = 1; i < count; i++)
{
const auto* item = this->takeItem(1);
delete item;
}
if (!styleContainer) if (!styleContainer)
{ {
@ -49,11 +55,11 @@ void LayerContainerListWidget::setStyleContainer(const PLayerStyleContainer& sty
this->styleContainer = styleContainer; this->styleContainer = styleContainer;
for (auto& style : *styleContainer | std::views::values) for (const auto& style : *styleContainer | std::views::values)
{ {
auto* item = new QListWidgetItem(this); auto* item = new QListWidgetItem(styleList);
item->setSizeHint(QSize(50, 40)); item->setSizeHint(QSize(50, 40));
this->setItemWidget(item, buildStyleListWidget(style)); styleList->setItemWidget(item, buildStyleListWidget(style));
} }
resetAddButton(); resetAddButton();
} }
@ -79,10 +85,10 @@ void LayerContainerListWidget::onAddLayerStyle(const std::shared_ptr<LayerStyle>
{ {
styleContainer->computeNewHash(); styleContainer->computeNewHash();
resetAddButton(); resetAddButton();
auto* newItem = new QListWidgetItem(this); auto* newItem = new QListWidgetItem(styleList);
styleItemMap[style->getDisplayName()] = newItem; styleItemMap[style->getDisplayName()] = newItem;
newItem->setSizeHint(QSize(50, 40)); newItem->setSizeHint(QSize(50, 40));
this->setItemWidget(newItem, buildStyleListWidget(style)); styleList->setItemWidget(newItem, buildStyleListWidget(style));
} }
} }
@ -93,22 +99,20 @@ void LayerContainerListWidget::onRemoveLayerStyle(const QString& styleName)
styleContainer->computeNewHash(); styleContainer->computeNewHash();
auto* removedItem = styleItemMap.extract(styleName).mapped(); auto* removedItem = styleItemMap.extract(styleName).mapped();
resetAddButton(); resetAddButton();
delete this->takeItem(this->row(removedItem)); delete styleList->takeItem(styleList->row(removedItem));
} }
} }
void LayerContainerListWidget::initHeader() void LayerContainerListWidget::initHeader()
{ {
this->headerWidget = new QWidget(this);
auto* headerLayout = new QHBoxLayout; auto* headerLayout = new QHBoxLayout;
auto* headerLabel = new QLabel(headerWidget); auto* headerLabel = new QLabel(headerWidget);
headerLabel->setText(QStringLiteral("ÑùʽÁбí")); headerLabel->setText(QStringLiteral("ÑùʽÁбí"));
headerLabel->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding)); headerLabel->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum));
//auto* addStyleButton = new QtMaterialRaisedButton("+", headerWidget); this->addButton = new QtMaterialRaisedButton("+", headerWidget);
this->addButton = new QPushButton("+", headerWidget); initMaterialButton(addButton);
addButton->setFixedSize(QSize(20, 20));
connect(addButton, &QPushButton::clicked, [this] { connect(addButton, &QPushButton::clicked, [this] {
auto* dialog = new LayerStyleDialog(this->styleContainer, nullptr, this); auto* dialog = new LayerStyleDialog(this->styleContainer, nullptr, this);
dialog->exec(); dialog->exec();
@ -118,15 +122,24 @@ void LayerContainerListWidget::initHeader()
} }
}); });
this->copyButton = new QtMaterialRaisedButton(QStringLiteral("复制所有样式"), headerWidget);
initMaterialButton(copyButton);
// TODO: 实现复制
this->pasteButton = new QtMaterialRaisedButton(QStringLiteral("从剪贴板粘贴"), headerWidget);
initMaterialButton(pasteButton);
headerLayout->addWidget(headerLabel); headerLayout->addWidget(headerLabel);
headerLayout->addWidget(addButton); headerLayout->addWidget(addButton);
headerLayout->addWidget(copyButton);
headerLayout->addWidget(pasteButton);
headerLayout->setContentsMargins(5, 0, 5, 0); headerLayout->setContentsMargins(5, 0, 5, 0);
headerWidget->setLayout(headerLayout); headerWidget->setLayout(headerLayout);
auto* headerItem = new QListWidgetItem(this); /*auto* headerItem = new QListWidgetItem(this);
headerItem->setFlags(Qt::NoItemFlags); headerItem->setFlags(Qt::NoItemFlags);
this->addItem(headerItem); this->addItem(headerItem);
this->setItemWidget(headerItem, headerWidget); this->setItemWidget(headerItem, headerWidget);*/
} }

View File

@ -4,13 +4,16 @@
#include <qtmaterialraisedbutton.h> #include <qtmaterialraisedbutton.h>
#include "LayerStyle.h" #include "LayerStyle.h"
class LayerContainerListWidget : public QListWidget class LayerContainerListWidget : public QWidget
{ {
Q_OBJECT Q_OBJECT
using PLayerStyleContainer = LayerStyleContainer*; using PLayerStyleContainer = LayerStyleContainer*;
private: private:
QWidget* headerWidget; QWidget* headerWidget;
QPushButton* addButton; QtMaterialRaisedButton* addButton;
QtMaterialRaisedButton* copyButton;
QtMaterialRaisedButton* pasteButton;
QListWidget* styleList;
PLayerStyleContainer styleContainer; PLayerStyleContainer styleContainer;
std::map<QString, QListWidgetItem*> styleItemMap; std::map<QString, QListWidgetItem*> styleItemMap;

View File

@ -180,9 +180,9 @@ 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, widthItem] {
radialStroke(this->stroke)->materialMap.erase(this->strokeTable->item(row, COLUMN_WIDTH)->text().toFloat()); radialStroke(this->stroke)->materialMap.erase(widthItem->data(Qt::EditRole).toFloat());
this->strokeTable->removeRow(row); this->strokeTable->removeRow(widthItem->row());
}); });
} }

View File

@ -275,7 +275,7 @@ bool LayerStyleContainer::operator==(const LayerStyleContainer& other) const
return std::ranges::equal(styles | std::views::values, other.styles | std::views::values); return std::ranges::equal(styles | std::views::values, other.styles | std::views::values);
} }
LayerStyleContainer LayerStyleContainer::operator<<(const LayerStyleContainer& other) const LayerStyleContainer LayerStyleContainer::operator|(const LayerStyleContainer& other) const
{ {
LayerStyleContainer result = other; LayerStyleContainer result = other;
for (const auto& style : std::ranges::subrange(this->cbegin(), this->cend()) for (const auto& style : std::ranges::subrange(this->cbegin(), this->cend())

View File

@ -144,7 +144,10 @@ public:
[[nodiscard]] size_t getHash() const; [[nodiscard]] size_t getHash() const;
[[nodiscard]] bool operator==(const LayerStyleContainer& other) const; [[nodiscard]] bool operator==(const LayerStyleContainer& other) const;
[[nodiscard]] LayerStyleContainer operator<<(const LayerStyleContainer& other) const; /**
* LayerStyleContainer
*/
[[nodiscard]] LayerStyleContainer operator|(const LayerStyleContainer& other) const;
/** /**
* *

View File

@ -1,5 +1,6 @@
#include "InfoDisplayWidget.h" #include "InfoDisplayWidget.h"
#include "./EditorWidgetComponent/LayerStyleDialog.h" #include "./EditorWidgetComponent/LayerStyleDialog.h"
#include "../ColorHelper.hpp"
#include <QLineEdit> #include <QLineEdit>
#include <QTextBlock> #include <QTextBlock>
#include <QComboBox> #include <QComboBox>

View File

@ -41,7 +41,7 @@ namespace UnitTest
TEST_METHOD(ContainerCoverTest) TEST_METHOD(ContainerCoverTest)
{ {
const auto newContainer = containerParent << containerChild; const auto& newContainer = containerParent | containerChild;
Assert::IsTrue(newContainer.full()); Assert::IsTrue(newContainer.full());
Assert::AreEqual(newContainer.getHash(), containerChild.getHash()); Assert::AreEqual(newContainer.getHash(), containerChild.getHash());
} }