129 lines
3.7 KiB
C++
129 lines
3.7 KiB
C++
#ifndef ITEMGENERALSETTINGS_H
|
|
#define ITEMGENERALSETTINGS_H
|
|
|
|
#include <QWidget>
|
|
#include <QButtonGroup>
|
|
#include "pictextitem.h"
|
|
#include <QPainter>
|
|
#include <QImage>
|
|
|
|
namespace Ui {
|
|
class ItemGeneralSettings;
|
|
}
|
|
|
|
/**
|
|
* @brief 图元通用属性的ViewModel
|
|
*
|
|
* 目的:解耦UI编辑器与PicTextItem,支持灵活的数据绑定和批量操作。
|
|
* UI修改属性时先更新VM,再决定何时应用到item(实时或延迟)。
|
|
*/
|
|
struct ItemGeneralSettingsVM {
|
|
// 图元类型,用于判断是否显示字间距
|
|
SourceType sourceType = SourceType::UNKNOWN;
|
|
|
|
ItemDirectionType direction = NORMAL;
|
|
int boldMultiple = 1;
|
|
int letterSpace = 0;
|
|
bool colorReversal = false;
|
|
|
|
std::function<QImage()> imageGenerator; // 图片生成函数
|
|
|
|
// 便利方法:从PicTextItem快照创建VM
|
|
static ItemGeneralSettingsVM fromItem(PicTextItem *item) {
|
|
ItemGeneralSettingsVM vm;
|
|
if (item) {
|
|
vm.sourceType = item->sourceType();
|
|
vm.direction = item->direction();
|
|
vm.boldMultiple = item->boldMultiple();
|
|
vm.letterSpace = item->letterSpace();
|
|
vm.colorReversal = item->colorReversal();
|
|
}
|
|
return vm;
|
|
}
|
|
|
|
// 检查是否有改动
|
|
bool hasDifference(const ItemGeneralSettingsVM &other) const {
|
|
return sourceType != other.sourceType ||
|
|
direction != other.direction ||
|
|
boldMultiple != other.boldMultiple ||
|
|
letterSpace != other.letterSpace ||
|
|
colorReversal != other.colorReversal;
|
|
}
|
|
};
|
|
|
|
class ItemGeneralSettings : public QWidget
|
|
{
|
|
Q_OBJECT
|
|
|
|
public:
|
|
explicit ItemGeneralSettings(QWidget *parent = nullptr);
|
|
~ItemGeneralSettings();
|
|
|
|
/**
|
|
* @brief 加载PicTextItem的属性到ViewModel,初始化UI
|
|
*
|
|
* 原因:UI应该显示item的状态,但不应该直接持有指针长期修改。
|
|
* edit()后,用户对UI的编辑会更新m_vm,而不是直接改item。
|
|
*/
|
|
void edit(PicTextItem* item);
|
|
|
|
/**
|
|
* @brief 设置当前ViewModel
|
|
*
|
|
* @param vm
|
|
*/
|
|
void setViewModel(const ItemGeneralSettingsVM& vm);
|
|
|
|
/**
|
|
* @brief 获取当前ViewModel
|
|
*
|
|
* 调用者可以检查是否有改动,并决定是否应用
|
|
*/
|
|
ItemGeneralSettingsVM viewModel() const { return m_vm; }
|
|
|
|
/**
|
|
* @brief 将ViewModel的属性应用到指定的PicTextItem
|
|
*
|
|
* 解耦的核心:UI不再直接修改item,而是通过VM进行间接修改。
|
|
* 这支持撤销/重做、批量编辑等高级功能。
|
|
*/
|
|
void applyToItem(PicTextItem* item);
|
|
|
|
/**
|
|
* @brief 当前编辑的item源引用(可能为空)
|
|
*
|
|
* 主要用于获取preview()时作为示例源,不用于直接修改
|
|
*/
|
|
PicTextItem* currentItem() { return m_currItem; }
|
|
|
|
void initUI();
|
|
|
|
private slots:
|
|
void ondirectionChanged(int dir);
|
|
void onBoldSpinBoxValueChanged(int multiple);
|
|
void onLetterSpaceSpinBoxChanged(int space);
|
|
void onColorReversalCheckBoxChecked(bool b);
|
|
|
|
private:
|
|
/**
|
|
* @brief 刷新预览,基于当前VM和item的source渲染
|
|
*/
|
|
void updatePreview();
|
|
|
|
Ui::ItemGeneralSettings *ui;
|
|
QButtonGroup m_directionButtonGroup;
|
|
|
|
// 原始item的快照,用于preview()和后续应用
|
|
PicTextItem* m_currItem = nullptr;
|
|
|
|
// UI编辑的状态,独立于m_currItem
|
|
ItemGeneralSettingsVM m_vm;
|
|
|
|
// 初始加载时的VM状态,用于检测改动
|
|
ItemGeneralSettingsVM m_vmSnapshot;
|
|
|
|
QPainter m_painter;
|
|
};
|
|
|
|
#endif // ITEMGENERALSETTINGS_H
|