#ifndef ITEMGENERALSETTINGS_H #define ITEMGENERALSETTINGS_H #include #include #include "pictextitem.h" #include #include namespace Ui { class ItemGeneralSettings; } 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 TextItemModel& vm); /** * @brief 获取当前ViewModel * * 调用者可以检查是否有改动,并决定是否应用 */ TextItemModel 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 TextItemModel m_vm; // 初始加载时的VM状态,用于检测改动 TextItemModel m_vmSnapshot; QPainter m_painter; }; #endif // ITEMGENERALSETTINGS_H