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

View File

@ -1,17 +1,28 @@
#include "LayerContainerListWidget.h"
#include "LayerStyleDialog.h"
#include "../ColorHelper.hpp"
#include <QLabel>
#include <ranges>
inline void initMaterialButton(QtMaterialRaisedButton* button)
{
button->setFixedHeight(25);
button->setBackgroundColor(ColorHelper::instance().getPrimary1());
}
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,
this, &LayerContainerListWidget::onAddLayerStyle);
connect(this, &LayerContainerListWidget::removeLayerStyle,
this, &LayerContainerListWidget::onRemoveLayerStyle);
auto* widgetLayout = new QVBoxLayout(this);
initHeader();
widgetLayout->addWidget(this->headerWidget);
widgetLayout->addWidget(this->styleList);
setStyleContainer(styleContainer, true);
}
@ -35,12 +46,7 @@ void LayerContainerListWidget::setStyleContainer(const PLayerStyleContainer& sty
return;
}
const int count = this->count();
for (int i = 1; i < count; i++)
{
const auto* item = this->takeItem(1);
delete item;
}
styleList->clear();
if (!styleContainer)
{
@ -49,11 +55,11 @@ void LayerContainerListWidget::setStyleContainer(const PLayerStyleContainer& sty
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));
this->setItemWidget(item, buildStyleListWidget(style));
styleList->setItemWidget(item, buildStyleListWidget(style));
}
resetAddButton();
}
@ -79,10 +85,10 @@ void LayerContainerListWidget::onAddLayerStyle(const std::shared_ptr<LayerStyle>
{
styleContainer->computeNewHash();
resetAddButton();
auto* newItem = new QListWidgetItem(this);
auto* newItem = new QListWidgetItem(styleList);
styleItemMap[style->getDisplayName()] = newItem;
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();
auto* removedItem = styleItemMap.extract(styleName).mapped();
resetAddButton();
delete this->takeItem(this->row(removedItem));
delete styleList->takeItem(styleList->row(removedItem));
}
}
void LayerContainerListWidget::initHeader()
{
this->headerWidget = new QWidget(this);
auto* headerLayout = new QHBoxLayout;
auto* headerLabel = new QLabel(headerWidget);
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 QPushButton("+", headerWidget);
addButton->setFixedSize(QSize(20, 20));
this->addButton = new QtMaterialRaisedButton("+", headerWidget);
initMaterialButton(addButton);
connect(addButton, &QPushButton::clicked, [this] {
auto* dialog = new LayerStyleDialog(this->styleContainer, nullptr, this);
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(addButton);
headerLayout->addWidget(copyButton);
headerLayout->addWidget(pasteButton);
headerLayout->setContentsMargins(5, 0, 5, 0);
headerWidget->setLayout(headerLayout);
auto* headerItem = new QListWidgetItem(this);
/*auto* headerItem = new QListWidgetItem(this);
headerItem->setFlags(Qt::NoItemFlags);
this->addItem(headerItem);
this->setItemWidget(headerItem, headerWidget);
this->setItemWidget(headerItem, headerWidget);*/
}

View File

@ -4,13 +4,16 @@
#include <qtmaterialraisedbutton.h>
#include "LayerStyle.h"
class LayerContainerListWidget : public QListWidget
class LayerContainerListWidget : public QWidget
{
Q_OBJECT
using PLayerStyleContainer = LayerStyleContainer*;
private:
QWidget* headerWidget;
QPushButton* addButton;
QtMaterialRaisedButton* addButton;
QtMaterialRaisedButton* copyButton;
QtMaterialRaisedButton* pasteButton;
QListWidget* styleList;
PLayerStyleContainer styleContainer;
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->setFixedSize(20, 20);
strokeTable->setCellWidget(row, COLUMN_OPERATIONS, removeButton);
connect(removeButton, &QtMaterialRaisedButton::clicked, [this, row] {
radialStroke(this->stroke)->materialMap.erase(this->strokeTable->item(row, COLUMN_WIDTH)->text().toFloat());
this->strokeTable->removeRow(row);
connect(removeButton, &QtMaterialRaisedButton::clicked, [this, widthItem] {
radialStroke(this->stroke)->materialMap.erase(widthItem->data(Qt::EditRole).toFloat());
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);
}
LayerStyleContainer LayerStyleContainer::operator<<(const LayerStyleContainer& other) const
LayerStyleContainer LayerStyleContainer::operator|(const LayerStyleContainer& other) const
{
LayerStyleContainer result = other;
for (const auto& style : std::ranges::subrange(this->cbegin(), this->cend())

View File

@ -144,7 +144,10 @@ public:
[[nodiscard]] size_t getHash() 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 "./EditorWidgetComponent/LayerStyleDialog.h"
#include "../ColorHelper.hpp"
#include <QLineEdit>
#include <QTextBlock>
#include <QComboBox>

View File

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