1 /***************************************************************************
2  *   Copyright (C) 2012~2012 by CSSlayer                                   *
3  *   wengxt@gmail.com                                                      *
4  *                                                                         *
5  *  This program is free software: you can redistribute it and/or modify   *
6  *  it under the terms of the GNU General Public License as published by   *
7  *  the Free Software Foundation, either version 3 of the License, or      *
8  *  (at your option) any later version.                                    *
9  *                                                                         *
10  *  This program is distributed in the hope that it will be useful,        *
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
13  *  GNU General Public License for more details.                           *
14  *                                                                         *
15  *  You should have received a copy of the GNU General Public License      *
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  *
17  *                                                                         *
18  ***************************************************************************/
19 
20 #ifndef FCITX_TOOLS_GUI_MODEL_H_
21 #define FCITX_TOOLS_GUI_MODEL_H_
22 
23 #include <QAbstractTableModel>
24 #include <QFutureWatcher>
25 #include <QSet>
26 #include <QTextStream>
27 
28 class QFile;
29 namespace fcitx {
30 
31 typedef QList<QPair<QString, QString>> QStringPairList;
32 
33 class QuickPhraseModel : public QAbstractTableModel {
34     Q_OBJECT
35 public:
36     explicit QuickPhraseModel(QObject *parent = 0);
37     virtual ~QuickPhraseModel();
38 
39     virtual Qt::ItemFlags flags(const QModelIndex &index) const;
40     virtual bool setData(const QModelIndex &index, const QVariant &value,
41                          int role = Qt::EditRole);
42     virtual QVariant headerData(int section, Qt::Orientation orientation,
43                                 int role = Qt::DisplayRole) const;
44     virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
45     virtual int columnCount(const QModelIndex &parent = QModelIndex()) const;
46     virtual QVariant data(const QModelIndex &index,
47                           int role = Qt::DisplayRole) const;
48     void load(const QString &file, bool append);
49     void loadData(QTextStream &stream);
50     void addItem(const QString &macro, const QString &word);
51     void deleteItem(int row);
52     void deleteAllItem();
53     QFutureWatcher<bool> *save(const QString &file);
54     void saveData(QTextStream &dev);
55     bool needSave();
56 
57 Q_SIGNALS:
58     void needSaveChanged(bool m_needSave);
59 
60 private Q_SLOTS:
61     void loadFinished();
62     void saveFinished();
63 
64 private:
65     QStringPairList parse(const QString &file);
66     bool saveData(const QString &file, const fcitx::QStringPairList &list);
67     void setNeedSave(bool needSave);
68     bool m_needSave;
69     QStringPairList m_list;
70     QFutureWatcher<QStringPairList> *m_futureWatcher;
71 };
72 } // namespace fcitx
73 
74 #endif // FCITX_TOOLS_GUI_MODEL_H_
75