1 #ifndef COLORTHEMELISTVIEW_H 2 #define COLORTHEMELISTVIEW_H 3 4 #include <QTimer> 5 #include <QListView> 6 #include <QJsonObject> 7 #include <QAbstractListModel> 8 #include <QStyledItemDelegate> 9 10 struct ColorOption { 11 QString optionName; 12 QColor color; 13 bool changed; 14 }; 15 Q_DECLARE_METATYPE(ColorOption); 16 17 class ColorSettingsModel; 18 19 class ColorThemeListView : public QListView 20 { 21 Q_OBJECT 22 public: 23 ColorThemeListView(QWidget *parent = nullptr); ~ColorThemeListView()24 virtual ~ColorThemeListView() override {} 25 26 ColorSettingsModel* colorSettingsModel() const; 27 28 protected slots: 29 void currentChanged(const QModelIndex ¤t, 30 const QModelIndex &previous) override; 31 32 void dataChanged(const QModelIndex & topLeft, const QModelIndex & bottomRight, 33 const QVector<int> &roles = QVector<int>()) override; 34 35 void mouseReleaseEvent(QMouseEvent *e) override; 36 37 void mouseMoveEvent(QMouseEvent *e) override; 38 39 40 private slots: 41 void blinkTimeout(); 42 43 signals: 44 void itemChanged(const QColor &option); 45 46 void dataChanged(const ColorOption &data); 47 48 void blink(); 49 50 private: 51 QTimer blinkTimer; 52 QColor backgroundColor; 53 }; 54 55 //============================================== 56 57 class ColorSettingsModel : public QAbstractListModel 58 { 59 Q_OBJECT 60 public: 61 ColorSettingsModel(QObject *parent = nullptr); ~ColorSettingsModel()62 virtual ~ColorSettingsModel() override {} 63 64 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; 65 66 bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; 67 68 int rowCount(const QModelIndex &parent = QModelIndex()) const override 69 { 70 Q_UNUSED(parent) 71 return theme.size(); 72 } 73 74 void updateTheme(); 75 76 QJsonDocument getTheme() const; 77 78 private: 79 QList<ColorOption> theme; 80 }; 81 82 class ColorOptionDelegate : public QStyledItemDelegate 83 { 84 Q_OBJECT 85 public: 86 ColorOptionDelegate(QObject *parent = nullptr); ~ColorOptionDelegate()87 ~ColorOptionDelegate() override {} 88 89 void paint(QPainter *painter, 90 const QStyleOptionViewItem &option, 91 const QModelIndex &index) const override; 92 93 QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override; 94 95 QRect getResetButtonRect() const; 96 97 private: 98 const int margin = 12; 99 QPixmap resetButtonPixmap; 100 QRect resetButtonRect; 101 102 QPixmap getPixmapFromSvg(const QString& fileName, const QColor& after) const; 103 }; 104 105 #endif // COLORTHEMELISTVIEW_H 106