Debian Codegraph @

gui/customphraseeditor/customphrasemodel.h

raw
1 /*
2 * SPDX-FileCopyrightText: 2023~2023 CSSlayer <[email protected]>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 *
6 */
7 #ifndef _CUSTOMPHRASEEDITOR_MODEL_H_
8 #define _CUSTOMPHRASEEDITOR_MODEL_H_
9
10 #include "../../im/pinyin/customphrase.h"
11 #include <QAbstractTableModel>
12 #include <QFutureWatcher>
13 #include <QSet>
14 #include <QTextStream>
15 #include <qfuturewatcher.h>
16 #include <vector>
17
18 namespace fcitx {
19
20 struct CustomPhraseItem {
21 QString key;
22 QString value;
23 int order;
24 bool enabled;
25 };
26
27 class CustomPhraseModel : public QAbstractTableModel {
28 Q_OBJECT
29 public:
30 enum { Column_Enable = 0, Column_Key, Column_Phrase, Column_Order };
31 explicit CustomPhraseModel(QObject *parent = 0);
32 virtual ~CustomPhraseModel();
33
34 Qt::ItemFlags flags(const QModelIndex &index) const override;
35 bool setData(const QModelIndex &index, const QVariant &value,
36 int role = Qt::EditRole) override;
37 QVariant headerData(int section, Qt::Orientation orientation,
38 int role = Qt::DisplayRole) const override;
39 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
40 int columnCount(const QModelIndex &parent = QModelIndex()) const override;
41 QVariant data(const QModelIndex &index,
42 int role = Qt::DisplayRole) const override;
43 void addItem(const QString &key, const QString &value, int order,
44 bool enabled);
45 void deleteItem(int row);
46 void deleteAllItem();
47 QFutureWatcher<bool> *save();
48 bool needSave();
49 void load();
50
51 Q_SIGNALS:
52 void needSaveChanged(bool needSave);
53
54 private Q_SLOTS:
55 void loadFinished();
56 void saveFinished();
57 void setNeedSave(bool needSave);
58
59 private:
60 static QList<CustomPhraseItem> parse(const QString &file);
61 static bool saveData(const QString &file,
62 const QList<CustomPhraseItem> &list);
63 QList<CustomPhraseItem> list_;
64 bool needSave_ = false;
65 QFutureWatcher<QList<CustomPhraseItem>> *futureWatcher_ = nullptr;
66 };
67
68 std::string customPhraseHelpMessage();
69
70 static inline constexpr char customPhraseFileName[] = "pinyin/customphrase";
71
72 } // namespace fcitx
73
74 #endif // _CUSTOMPHRASEEDITOR_MODEL_H_