Stellarium 0.15.0
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 "StelPropertyMgr.hpp"
24 #include <QKeySequence>
25 #include <QList>
26 
42 class StelAction : public QObject
43 {
44  Q_OBJECT
45 public:
49  Q_PROPERTY(bool checked READ isChecked WRITE setChecked NOTIFY toggled)
55  Q_PROPERTY(bool checkable READ isCheckable)
56 
58  bool isCheckable() const {return boolProperty;}
60  bool isChecked() const {return boolProperty ? boolProperty->getValue().toBool() : false; }
61  bool isGlobal() const {return global;}
63  void setShortcut(const QString& key);
65  void setAltShortcut(const QString& key);
66  QKeySequence::SequenceMatch matches(const QKeySequence& seq) const;
67 
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:
81  void toggled(bool);
83  void triggered();
85  void changed();
86 public slots:
89  void setChecked(bool);
92  void trigger();
96  void toggle();
97 private slots:
98  void propertyChanged(bool);
99 private:
100  friend class StelActionMgr;
101 
103  StelAction(const QString& actionId,
104  const QString& groupId,
105  const QString& text,
106  const QString& primaryKey="",
107  const QString& altKey="",
108  bool global=false);
113  void connectToObject(QObject* target, const char* slot);
114 
115  QString group;
116  QString text;
117  bool global;
118  QKeySequence keySequence;
119  QKeySequence altKeySequence;
120  const QKeySequence defaultKeySequence;
121  const QKeySequence defaultAltKeySequence;
122  QObject* target;
123  //If the StelAction is connected to a boolean property with a NOTIFY signal, a StelProperty is used for the connection
124  StelProperty* boolProperty;
125  QMetaMethod slot;
126 
127  // Currently, there is no proper way to handle shortcuts with non latin
128  // keyboards layouts. So for the moment, if we don't use QuickView, we
129  // create a QAction added to the main view that will trigger the
130  // StelAction when the shortcut is typed.
131 #ifndef USE_QUICKVIEW
132 private slots:
133  void onChanged();
134 private:
135  class QAction* qAction;
136 #endif
137 };
138 
140 class StelActionMgr : public QObject
141 {
142  Q_OBJECT
143 public:
144  StelActionMgr();
145  ~StelActionMgr();
160  StelAction* addAction(const QString& id, const QString& groupId, const QString& text,
161  QObject* target, const char* slot,
162  const QString& shortcut="", const QString& altShortcut="",
163  bool global=false);
164  StelAction* findAction(const QString& id);
165  bool pushKey(int key, bool global=false);
166 
168  QStringList getGroupList() const;
170  QList<StelAction*> getActionList(const QString& group) const;
172  QList<StelAction*> getActionList() const;
173 
175  void saveShortcuts();
178 
179 signals:
183  void actionToggled(const QString& id, bool value);
184 
185 public slots:
189  void setAllActionsEnabled(bool value) {actionsEnabled = value;}
190 
191 private slots:
192  void onStelActionToggled(bool val);
193 
194 private:
195  bool actionsEnabled;
196  QList<int> keySequence;
197 };
198 
199 #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.
bool isChecked() const
Wrapper around a Q_PROPERTY (see the Qt property system for more information) of a specific object...
bool checked
When the StelAction is checkable, this may be used to get/set the current value.
void setShortcut(const QString &key)
Defines the key-combination used to call this action.
void setChecked(bool)
QStringList getGroupList() const
Returns a list of all current StelAction groups.
bool checkable
If this is true, this StelAction can be toggled.
bool isCheckable() const
void triggered()
Emitted after an argumentless slot has been called.
QVariant getValue() const
Returns the current value of this property as a QVariant.
Wrapper around an argumentless QObject slot or a bool Q_PROPERTY with WRITE method, allowing the slot to be called/property to be toggled using this action object.
QString getId() const
The ID of this action. Must be unique.
void actionToggled(const QString &id, bool value)
Emitted when any action registered with this StelActionMgr is toggled.
void setAltShortcut(const QString &key)
Defines an alternative key-combination.
void toggled(bool)
Emitted when the boolean state of this StelAction changes.
void changed()
Emitted when additional data associated with this action changed (i.e. shortcuts, text...
void toggle()
If the action is checkable, this toggles the value of the connected boolean property.
void restoreDefaultShortcuts()
Restore the default shortcuts combinations.
Manager for StelAction instances. Allows registration of new actions, and finding an existing one by ...
void trigger()
If the action is checkable, toggle() is called.
void saveShortcuts()
Save current shortcuts to file.
void setAllActionsEnabled(bool value)
Enable/disable all actions of application.
QList< StelAction * > getActionList() const
Returns all registered StelActions.