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 "config.h"
24 
25 #include <QObject>
26 #include <QKeySequence>
27 #include <QList>
28 
29 class StelAction : public QObject
30 {
31  Q_OBJECT
32 public:
33  friend class StelActionMgr;
34  Q_PROPERTY(bool checked READ isChecked WRITE setChecked NOTIFY toggled)
35 
36 
37  StelAction(QObject *parent)
38  : QObject(parent)
39  , checkable(false)
40  , checked(false)
41  , global(false)
42  , target(NULL)
43  , property(NULL)
44  #ifndef USE_QUICKVIEW
45  , qAction(NULL) {}
46  #endif
47 
48  StelAction(const QString& actionId,
49  const QString& groupId,
50  const QString& text,
51  const QString& primaryKey="",
52  const QString& altKey="",
53  bool global=false);
58  void connectToObject(QObject* obj, const char* slot);
61  void setCheckable(bool value) {checkable = value; emit changed();}
62  bool isCheckable() const {return checkable;}
63  bool isChecked() const {return checked;}
64  bool isGlobal() const {return global;}
65  void setShortcut(const QString& key);
66  void setAltShortcut(const QString& key);
67  QKeySequence::SequenceMatch matches(const QKeySequence& seq) const;
68 
69  QString getId() const {return objectName();}
70  QString getGroup() const {return group;}
71  const QKeySequence getShortcut() const {return keySequence;}
72  const QKeySequence getAltShortcut() const {return altKeySequence;}
73  QString getText() const;
74  void setText(const QString& value) {text = value; emit changed();}
75 signals:
76  void toggled(bool);
77  void triggered();
78  void changed();
79 public slots:
80  void setChecked(bool);
81  void trigger();
82  void toggle();
83 private slots:
84  void propertyChanged(bool);
85 private:
86  bool checkable;
87  bool checked;
88  QString group;
89  QString text;
90  bool global;
91  QKeySequence keySequence;
92  QKeySequence altKeySequence;
93  const QKeySequence defaultKeySequence;
94  const QKeySequence defaultAltKeySequence;
95  QObject* target;
96  const char* property;
97 
98  // Currently, there is no proper way to handle shortcuts with non latin
99  // keyboards layouts. So for the moment, if we don't use QuickView, we
100  // create a QAction added to the main view that will trigger the
101  // StelAction when the shortcut is typed.
102 #ifndef USE_QUICKVIEW
103 private slots:
104  void onChanged();
105 private:
106  class QAction* qAction;
107 #endif
108 };
109 
110 class StelActionMgr : public QObject
111 {
112  Q_OBJECT
113 public:
114  StelActionMgr();
115  ~StelActionMgr();
128  StelAction* addAction(const QString& id, const QString& groupId, const QString& text,
129  QObject* target, const char* slot,
130  const QString& shortcut="", const QString& altShortcut="",
131  bool global=false);
132  StelAction* findAction(const QString& id);
133  bool pushKey(int key, bool global=false);
134 
135  QStringList getGroupList() const;
136  QList<StelAction*> getActionList(const QString& group) const;
137 
139  void saveShortcuts();
142 
143 public slots:
147  void setAllActionsEnabled(bool value) {actionsEnabled = value;}
148 private:
149  bool actionsEnabled;
150  QList<int> keySequence;
151 };
152 
153 #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.