Stellarium 0.14.3
StelActionMgr.hpp
1 /*
2  * Stellarium
3  * Copyright (C) 2013 Guillaume Chereau
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (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, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18  */
19 
20 #ifndef _STELACTIONMGR_HPP_
21 #define _STELACTIONMGR_HPP_
22 
23 #include <QObject>
24 #include <QKeySequence>
25 #include <QList>
26 
27 class StelAction : public QObject
28 {
29  Q_OBJECT
30 public:
31  friend class StelActionMgr;
32  Q_PROPERTY(bool checked READ isChecked WRITE setChecked NOTIFY toggled)
33 
34 
35  StelAction(QObject *parent)
36  : QObject(parent)
37  , checkable(false)
38  , checked(false)
39  , global(false)
40  , target(NULL)
41  , property(NULL)
42  #ifndef USE_QUICKVIEW
43  , qAction(NULL)
44  #endif
45  {}
46 
47  StelAction(const QString& actionId,
48  const QString& groupId,
49  const QString& text,
50  const QString& primaryKey="",
51  const QString& altKey="",
52  bool global=false);
57  void connectToObject(QObject* obj, const char* slot);
60  void setCheckable(bool value) {checkable = value; emit changed();}
61  bool isCheckable() const {return checkable;}
62  bool isChecked() const {return checked;}
63  bool isGlobal() const {return global;}
64  void setShortcut(const QString& key);
65  void setAltShortcut(const QString& key);
66  QKeySequence::SequenceMatch matches(const QKeySequence& seq) const;
67 
68  QString getId() const {return objectName();}
69  QString getGroup() const {return group;}
70  const QKeySequence getShortcut() const {return keySequence;}
71  const QKeySequence getAltShortcut() const {return altKeySequence;}
72  QString getText() const;
73  void setText(const QString& value) {text = value; emit changed();}
74 signals:
75  void toggled(bool);
76  void triggered();
77  void changed();
78 public slots:
79  void setChecked(bool);
80  void trigger();
81  void toggle();
82 private slots:
83  void propertyChanged(bool);
84 private:
85  bool checkable;
86  bool checked;
87  QString group;
88  QString text;
89  bool global;
90  QKeySequence keySequence;
91  QKeySequence altKeySequence;
92  const QKeySequence defaultKeySequence;
93  const QKeySequence defaultAltKeySequence;
94  QObject* target;
95  const char* property;
96 
97  // Currently, there is no proper way to handle shortcuts with non latin
98  // keyboards layouts. So for the moment, if we don't use QuickView, we
99  // create a QAction added to the main view that will trigger the
100  // StelAction when the shortcut is typed.
101 #ifndef USE_QUICKVIEW
102 private slots:
103  void onChanged();
104 private:
105  class QAction* qAction;
106 #endif
107 };
108 
109 class StelActionMgr : public QObject
110 {
111  Q_OBJECT
112 public:
113  StelActionMgr();
114  ~StelActionMgr();
127  StelAction* addAction(const QString& id, const QString& groupId, const QString& text,
128  QObject* target, const char* slot,
129  const QString& shortcut="", const QString& altShortcut="",
130  bool global=false);
131  StelAction* findAction(const QString& id);
132  bool pushKey(int key, bool global=false);
133 
134  QStringList getGroupList() const;
135  QList<StelAction*> getActionList(const QString& group) const;
136 
138  void saveShortcuts();
141 
142 public slots:
146  void setAllActionsEnabled(bool value) {actionsEnabled = value;}
147 private:
148  bool actionsEnabled;
149  QList<int> keySequence;
150 };
151 
152 #endif // _STELACTIONMGR_HPP_
StelAction * addAction(const QString &id, const QString &groupId, const QString &text, QObject *target, const char *slot, const QString &shortcut="", const QString &altShortcut="", bool global=false)
Create and add a new StelAction, connected to an object property or slot.
StelAction(QObject *parent)
Don't use this constructor, this is just there to ease the migration from QAction.
void connectToObject(QObject *obj, const char *slot)
Connect the action to an object property or slot.
void setCheckable(bool value)
Don't use setCheckable, connectToObject can automatically determine if the action is checkable or not...
void restoreDefaultShortcuts()
Restore the default shortcuts combinations.
void saveShortcuts()
Save current shortcuts to file.
void setAllActionsEnabled(bool value)
Enable/disable all actions of application.