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