cmake: avoid de-duplication of user's CXXFLAGS
[quassel.git] / src / qtui / settingspages / shortcutsmodel.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2022 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
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 2 of the License, or     *
8  *   (at your option) version 3.                                           *
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, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #ifndef SHORTCUTSMODEL_H
22 #define SHORTCUTSMODEL_H
23
24 #include <QAbstractItemModel>
25 #include <QKeySequence>
26
27 class Action;
28 class ActionCollection;
29
30 //! Model that exposes the actions from one or more ActionCollections
31 /** This model takes one or more ActionCollections and exposes their actions as model items.
32  *  Note that the ShortcutsModel will not react to changes in the ActionCollection (e.g. adding,
33  *  removing actions), because it is supposed to be used after all actions being defined.
34  */
35 class ShortcutsModel : public QAbstractItemModel
36 {
37     Q_OBJECT
38 public:
39     enum Role
40     {
41         ActionRole = Qt::UserRole,
42         DefaultShortcutRole,
43         ActiveShortcutRole,
44         IsConfigurableRole
45     };
46
47     ShortcutsModel(const QHash<QString, ActionCollection*>& actionCollections, QObject* parent = nullptr);
48     ~ShortcutsModel() override;
49
50     QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const override;
51     QModelIndex parent(const QModelIndex& child) const override;
52     int columnCount(const QModelIndex& parent = QModelIndex()) const override;
53     int rowCount(const QModelIndex& parent = QModelIndex()) const override;
54     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
55     QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
56     bool setData(const QModelIndex& index, const QVariant& value, int role = ActiveShortcutRole) override;
57
58 public slots:
59     //! Load shortcuts from the ActionCollections
60     /** Note that this will not rebuild the internal structure of the model, as we assume the
61      *  ActionCollections to be static during the lifetime of the settingspage. This will merely
62      *  re-read the shortcuts currently set in Quassel.
63      */
64     void load();
65
66     //! Load default shortcuts from the ActionCollections
67     /** Note that this will not rebuild the internal structure of the model, as we assume the
68      *  ActionCollections to be static during the lifetime of the settingspage. This will update
69      *  the model's state from the ActionCollections' defaults.
70      */
71     void defaults();
72
73     //! Commit the model changes to the ActionCollections
74     void commit();
75
76     inline bool hasChanged() const { return _changedCount; }
77
78 signals:
79     //! Reflects the difference between model contents and the ActionCollections we loaded this from
80     void changed(bool changed);
81
82 private:
83     struct Item
84     {
85         inline Item()
86         {
87             parentItem = nullptr;
88             collection = nullptr;
89             action = nullptr;
90         }
91         inline ~Item() { qDeleteAll(actionItems); }
92         int row;
93         Item* parentItem;
94         ActionCollection* collection;
95         Action* action;
96         QKeySequence shortcut;
97         QList<Item*> actionItems;
98     };
99
100     QList<Item*> _categoryItems;
101     int _changedCount;
102 };
103
104 #endif  // SHORTCUTSMODEL_H