• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Stellarium / stellarium / 4853788370

pending completion
4853788370

push

github

Alexander V. Wolf
Special patch for John Simple

3 of 3 new or added lines in 3 files covered. (100.0%)

14729 of 125046 relevant lines covered (11.78%)

20166.5 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/core/StelPropertyMgr.cpp
1
#include "StelPropertyMgr.hpp"
2
#include "StelApp.hpp"
3
#include <QtDebug>
4
#include <QApplication>
5

6
StelProperty::StelProperty(const QString &id, QObject *target, const QMetaProperty& prop)
×
7
        : id(id), target(target), prop(prop)
×
8
{
9
        setObjectName(id);
×
10
        if(prop.hasNotifySignal())
×
11
                connect(target,prop.notifySignal(),this,metaObject()->method(this->metaObject()->indexOfSlot("propertyChanged()")));
×
12
}
×
13

14
QVariant StelProperty::getValue() const
×
15
{
16
        return prop.read(target);
×
17
}
18

19
bool StelProperty::setValue(const QVariant &value) const
×
20
{
21
        //check if the property already has the specified value
22
        //preventing some unnecessary events
23
        if(getValue() == value)
×
24
                return true;
×
25
        return prop.write(target,value);
×
26
}
27

28
bool StelProperty::isReadOnly() const
×
29
{
30
        return !prop.isWritable();
×
31
}
32

33
bool StelProperty::isSynchronizable() const
×
34
{
35
        return prop.isWritable() && prop.isStored();
×
36
}
37

38
bool StelProperty::canNotify() const
×
39
{
40
        return prop.hasNotifySignal();
×
41
}
42

43
QMetaType::Type StelProperty::getType() const
×
44
{
45
        //Qt is quite funky when it comes to QVariant::Type vs QMetaType::Type, see
46
        //https://doc.qt.io/qt-5/qvariant.html#type
47
        //and https://stackoverflow.com/questions/31290606/qmetatypefloat-not-in-qvarianttype
48
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
49
        return static_cast<QMetaType::Type>(prop.typeId());
50
#else
51
        return static_cast<QMetaType::Type>(prop.type());
×
52
#endif
53
}
54

55
void StelProperty::propertyChanged()
×
56
{
57
        QVariant val = prop.read(target);
×
58
        Q_ASSERT(val.isValid());
×
59
        emit changed(val);
×
60
}
×
61

62
StelPropertyMgr::StelPropertyMgr()
×
63
{
64
}
×
65

66
StelPropertyMgr::~StelPropertyMgr()
×
67
{
68
}
×
69

70
StelProperty* StelPropertyMgr::registerProperty(const QString& id, QObject* target, const char* propertyName)
×
71
{
72
        int idx = target->metaObject()->indexOfProperty(propertyName);
×
73
        if(idx<0)
×
74
                qFatal("Property '%s' missing on object '%s' for StelProperty '%s'",propertyName, qPrintable(target->objectName()), qPrintable(id));
×
75

76
        return registerProperty(id,target,target->metaObject()->property(idx));
×
77
}
78

79
StelProperty* StelPropertyMgr::registerProperty(const QString &id, QObject *target,const QMetaProperty &prop)
×
80
{
81
        //check if the ID is already existing
82
        if(propMap.contains(id))
×
83
                qFatal("StelProperty with id '%s' already existing, please fix...",qPrintable(id));
×
84

85
        StelProperty* stelProp = new StelProperty(id,target,prop);
×
86
        stelProp->setParent(this);
×
87

88
        //react to the property changed event
89
        connect(stelProp,SIGNAL(changed(QVariant)),this,SLOT(onStelPropChanged(QVariant)));
×
90

91
        //check if the property is valid, crash otherwise
92
        //this may reveal if a qRegisterMetaType or similar is needed
93
        QVariant value = stelProp->getValue();
×
94
        if(!value.isValid())
×
95
                qFatal("StelProperty %s can not be read. Missing READ or need to register MetaType?",id.toUtf8().constData());
×
96

97
#if 0
98
        QString debugStr=(stelProp->isReadOnly()?"readonly":"readwrite");
99
        if(stelProp->canNotify())
100
                debugStr.append(" notify");
101
        qDebug()<<"StelProperty"<<id<<"registered, properties:"<<debugStr<<", value:"<<value;
102
#endif
103

104
        propMap.insert(id,stelProp);
×
105
        return stelProp;
×
106
}
×
107

108
QList<StelProperty*> StelPropertyMgr::getAllProperties() const
×
109
{
110
        return propMap.values();
×
111
}
112

113
StelProperty* StelPropertyMgr::getProperty(const QString &id, const bool noWarning) const
×
114
{
115
        StelProperty* prop = propMap.value(id);
×
116
        if((!prop) && (!noWarning))
×
117
                qWarning()<<"StelProperty"<<id<<"not found";
×
118
        return prop;
×
119
}
120

121
void StelPropertyMgr::onStelPropChanged(const QVariant &val)
×
122
{
123
        StelProperty* prop = qobject_cast<StelProperty*>(sender());
×
124
#ifndef NDEBUG
125
        if (qApp->property("verbose") == true)
×
126
                qDebug()<<"StelProperty"<<prop->getId()<<"changed, value"<<val;
×
127
#endif
128
        emit stelPropertyChanged(prop, val);
×
129
}
×
130

131
QStringList StelPropertyMgr::getPropertyList() const
×
132
{
133
        return propMap.keys();
×
134
}
135

136
void StelPropertyMgr::registerObject(QObject *obj)
×
137
{
138
        const QString name = obj->objectName();
×
139
        //make sure an object name is set, and does not contain a dot
140
        if(name.isEmpty() || name.contains('.'))
×
141
                qFatal("[StelPropertyMgr] Object name '%s' is invalid, must be non-empty and without a dot", qPrintable(name));
×
142

143
        if(!registeredObjects.contains(name))
×
144
        {
145
                //just a sanity check that we dont have the same object registered under a different name
146
                Q_ASSERT(!registeredObjects.values().contains(obj));
×
147

148
#ifndef NDEBUG
149
                qDebug()<<"[StelPropertyMgr] Registering object"<<name;
×
150
#endif
151

152
                //iterate and store the static properties, dynamic ones are skipped
153
                const QMetaObject* metaObj = obj->metaObject();
×
154
                for(int i = metaObj->propertyOffset(); i < metaObj->propertyCount(); ++i)
×
155
                {
156
                        QMetaProperty metaProp = metaObj->property(i);
×
157
                        QString propName = name +'.'+metaProp.name();
×
158
                        registerProperty(propName,obj,metaProp);
×
159
                }
×
160

161
                registeredObjects.insert(name,obj);
×
162
        }
163
}
×
164

165
QVariant StelPropertyMgr::getStelPropertyValue(const QString &id, const bool noWarning) const
×
166
{
167
        StelProperty* prop = getProperty(id, noWarning);
×
168
        if(prop)
×
169
        {
170
                return prop->getValue();
×
171
        }
172
        //return an invalid qvariant
173
        return QVariant();
×
174
}
175

176
bool StelPropertyMgr::setStelPropertyValue(const QString &id, const QVariant &value) const
×
177
{
178
        StelProperty* prop = getProperty(id);
×
179
        if(prop)
×
180
        {
181
                return prop->setValue(value);
×
182
        }
183
        return false;
×
184
}
185

186
QMetaProperty StelPropertyMgr::getMetaProperty(const QString &id) const
×
187
{
188
        StelProperty* prop = getProperty(id);
×
189
        if(prop)
×
190
        {
191
                return prop->getMetaProp();
×
192
        }
193
        //return an invalid metaprop
194
        return QMetaProperty();
×
195
}
196

197
StelPropertyProxy::StelPropertyProxy(StelProperty *prop, QObject *parent)
×
198
        : QObject(parent), prop(prop)
×
199
{
200
        connect(prop, &StelProperty::changed, this, &StelPropertyProxy::onPropertyChanged);
×
201
}
×
202

203
StelPropertyIntProxy::StelPropertyIntProxy(StelProperty *pr, QObject *parent)
×
204
        : StelPropertyProxy(pr,parent)
×
205
{
206
}
×
207

208
void StelPropertyIntProxy::onPropertyChanged(const QVariant &value)
×
209
{
210
        emit propertyChanged(value.toInt());
×
211
}
×
212

213
StelPropertyBoolProxy::StelPropertyBoolProxy(StelProperty *pr, QObject *parent)
×
214
        : StelPropertyProxy(pr,parent)
×
215
{
216
}
×
217

218
void StelPropertyBoolProxy::onPropertyChanged(const QVariant &value)
×
219
{
220
        emit propertyChanged(value.toBool());
×
221
}
×
222

223
StelPropertyDoubleProxy::StelPropertyDoubleProxy(StelProperty *pr, QObject *parent)
×
224
        : StelPropertyProxy(pr,parent)
×
225
{
226
}
×
227

228
void StelPropertyDoubleProxy::onPropertyChanged(const QVariant &value)
×
229
{
230
        emit propertyChanged(value.toDouble());
×
231
}
×
232

233
StelPropertyStringProxy::StelPropertyStringProxy(StelProperty *pr, QObject *parent)
×
234
        : StelPropertyProxy(pr,parent)
×
235
{
236
}
×
237

238
void StelPropertyStringProxy::onPropertyChanged(const QVariant &value)
×
239
{
240
        emit propertyChanged(value.toString());
×
241
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc