Stellarium 0.13.2
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  StelAction(const QString& actionId,
47  const QString& groupId,
48  const QString& text,
49  const QString& primaryKey="",
50  const QString& altKey="",
51  bool global=false);
56  void connectToObject(QObject* obj, const char* slot);
59  void setCheckable(bool value) {checkable = value; emit changed();}
60  bool isCheckable() const {return checkable;}
61  bool isChecked() const {return checked;}
62  bool isGlobal() const {return global;}
63  void setShortcut(const QString& key);
64  void setAltShortcut(const QString& key);
65  QKeySequence::SequenceMatch matches(const QKeySequence& seq) const;
66 
67  QString getId() const {return objectName();}
68  QString getGroup() const {return group;}
69  const QKeySequence getShortcut() const {return keySequence;}
70  const QKeySequence getAltShortcut() const {return altKeySequence;}
71  QString getText() const;
72  void setText(const QString& value) {text = value; emit changed();}
73 signals:
74  void toggled(bool);
75  void triggered();
76  void changed();
77 public slots:
78  void setChecked(bool);
79  void trigger();
80  void toggle();
81 private slots:
82  void propertyChanged(bool);
83 private:
84  bool checkable;
85  bool checked;
86  QString group;
87  QString text;
88  bool global;
89  QKeySequence keySequence;
90  QKeySequence altKeySequence;
91  const QKeySequence defaultKeySequence;
92  const QKeySequence defaultAltKeySequence;
93  QObject* target;
94  const char* property;
95 
96  // Currently, there is no proper way to handle shortcuts with non latin
97  // keyboards layouts. So for the moment, if we don't use QuickView, we
98  // create a QAction added to the main view that will trigger the
99  // StelAction when the shortcut is typed.
100 #ifndef USE_QUICKVIEW
101 private slots:
102  void onChanged();
103 private:
104  class QAction* qAction;
105 #endif
106 };
107 
108 class StelActionMgr : public QObject
109 {
110  Q_OBJECT
111 public:
112  StelActionMgr();
113  ~StelActionMgr();
126  StelAction* addAction(const QString& id, const QString& groupId, const QString& text,
127  QObject* target, const char* slot,
128  const QString& shortcut="", const QString& altShortcut="",
129  bool global=false);
130  StelAction* findAction(const QString& id);
131  bool pushKey(int key, bool global=false);
132 
133  QStringList getGroupList() const;
134  QList<StelAction*> getActionList(const QString& group) const;
135 
137  void saveShortcuts();
140 
141 public slots:
145  void setAllActionsEnabled(bool value) {actionsEnabled = value;}
146 private:
147  bool actionsEnabled;
148  QList<int> keySequence;
149 };
150 
151 #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.