Stellarium 0.15.2
StelPropertyMgr.hpp
1 /*
2  * Copyright (C) 2015-2016 Florian Schaukowitsch
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
17  */
18 
19 #ifndef _STELPROPERTYMGR_HPP_
20 #define _STELPROPERTYMGR_HPP_
21 
22 #include <QObject>
23 #include <QSet>
24 #include <QMetaProperty>
25 
26 class StelProperty;
27 
33 class StelPropertyProxy : public QObject
34 {
35  Q_OBJECT
36 public:
37  StelPropertyProxy(StelProperty* prop,QObject* parent);
38 
39 protected slots:
42  virtual void onPropertyChanged(const QVariant& value) = 0;
43 
44 protected:
47 };
48 
51 {
52  Q_OBJECT
53 public:
54  StelPropertyIntProxy(StelProperty* prop,QObject* parent);
55 protected slots:
56  void onPropertyChanged(const QVariant &value) Q_DECL_OVERRIDE;
57 signals:
59  void propertyChanged(int value);
60 };
61 
64 {
65  Q_OBJECT
66 public:
67  StelPropertyBoolProxy(StelProperty* prop,QObject* parent);
68 protected slots:
69  void onPropertyChanged(const QVariant &value) Q_DECL_OVERRIDE;
70 signals:
72  void propertyChanged(bool value);
73 };
74 
77 {
78  Q_OBJECT
79 public:
80  StelPropertyDoubleProxy(StelProperty* prop,QObject* parent);
81 protected slots:
82  void onPropertyChanged(const QVariant &value) Q_DECL_OVERRIDE;
83 signals:
85  void propertyChanged(double value);
86 };
87 
88 
206 class StelProperty : public QObject
207 {
208  friend class StelPropertyMgr;
209  Q_OBJECT
210 
211 public slots:
213  QString getId() const { return id; }
214 
216  QVariant getValue() const;
217 
224  bool setValue(const QVariant& value) const;
225 
227  bool isReadOnly() const;
228 
230  bool canNotify() const;
231 
233  QMetaType::Type getType() const;
234 
236  QMetaProperty getMetaProp() const { return prop; }
237 
239  QObject* getTarget() const { return target; }
240 signals:
242  void changed(const QVariant& newValue);
243 protected slots:
245  void propertyChanged();
246 protected:
247  StelProperty(const QString& id,QObject* target, const QMetaProperty& prop);
248 
249  QString id;
250  QObject* target;
251  QMetaProperty prop;
252 };
253 
257 class StelPropertyMgr : public QObject
258 {
259  Q_OBJECT
260 public:
261  typedef QMap<QString,StelProperty*> StelPropertyMap;
262 
264  StelPropertyMgr();
265  ~StelPropertyMgr();
266 
273  StelProperty* registerProperty(const QString& id, QObject* target, const char* propertyName);
274 
279  void registerObject(QObject *obj);
280 
282  QStringList getPropertyList() const;
284  QList<StelProperty*> getAllProperties() const;
286  const StelPropertyMap& getPropertyMap() const { return propMap; }
287 
289  StelProperty* getProperty(const QString& id) const;
290 
295  QVariant getStelPropertyValue(const QString& id) const;
301  bool setStelPropertyValue(const QString& id, const QVariant &value) const;
303  QMetaProperty getMetaProperty(const QString& id) const;
304 private slots:
305  void onStelPropChanged(const QVariant& val);
306 signals:
310  void stelPropChanged(const QString& id, const QVariant& value);
311 private:
312  StelProperty* registerProperty(const QString &id, QObject *target, const QMetaProperty& prop);
313 
314  QMap<QString,QObject*> registeredObjects;
315  StelPropertyMap propMap;
316 };
317 
318 #endif
A StelPropertyProxy for bool-based properties.
Wrapper around a Q_PROPERTY (see the Qt property system for more information) of a specific object...
QMetaProperty getMetaProp() const
Returns the actual Q_PROPERTY wrapped by this instance.
StelProperty * prop
The connected property, set by the constructor.
const StelPropertyMap & getPropertyMap() const
Returns a map from property IDs to StelProperty objects.
A StelPropertyProxy for double-based properties.
Manages the registration of specific object properties with the StelProperty system.
Abstract base class for a StelProperty proxy implementation, which allow reacting to the StelProperty...
QObject * getTarget() const
Returns the object to which this property belongs.
A StelPropertyProxy for int-based properties.
QString getId() const
Returns the unique ID which is used to identify this property.
virtual void onPropertyChanged(const QVariant &value)=0
This is connected by the constructor to the StelProperty::changed event of the connected property...