QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
FactValueGrid.cc
Go to the documentation of this file.
2#include "FactValueGrid.h"
4#include "AppMessages.h"
5#include "QGCCorePlugin.h"
7#include "Vehicle.h"
8
9#include <QtCore/QCoreApplication>
10#include <QtCore/QSettings>
11#include <QtCore/QDir>
12
14{
15 static FactValueGridLayouts layouts;
16 return &layouts;
17}
18
19QStringList FactValueGrid::_iconNames;
20
21QList<FactValueGrid*> FactValueGrid::_vehicleCardInstanceList;
22
24 : QQuickItem(parent)
25 , _columns (new QmlObjectListModel(this))
26 , _fontSizeNames({
27 QCoreApplication::translate("FactValueGrid", "Default"),
28 QCoreApplication::translate("FactValueGrid", "Small"),
29 QCoreApplication::translate("FactValueGrid", "Medium"),
30 QCoreApplication::translate("FactValueGrid", "Large"),
31 })
32{
33 if (_iconNames.isEmpty()) {
34 QDir iconDir(":/InstrumentValueIcons/");
35 _iconNames = iconDir.entryList();
36 }
37}
38
40{
41 QQuickItem::componentComplete();
42
43 connect(this, &FactValueGrid::fontSizeChanged, this, &FactValueGrid::_saveSettings);
44
46 _vehicleCardInstanceList.append(this);
47 _initForNewVehicle(_specificVehicleForCard);
48 } else {
49 // We are not tracking a specific vehicle so we need to track the active vehicle or offline editing vehicle if not active vehicle
50 auto multiVehicleManager = MultiVehicleManager::instance();
51 connect(multiVehicleManager, &MultiVehicleManager::activeVehicleChanged, this, &FactValueGrid::_activeVehicleChanged);
52 _activeVehicle = multiVehicleManager->activeVehicle();
53 if (!_activeVehicle) {
54 _activeVehicle = multiVehicleManager->offlineEditingVehicle();
55 }
56 _initForNewVehicle(_activeVehicle);
57 }
58}
59
60QList<FactValueGrid::LayoutFact> FactValueGrid::layoutFacts(const QString& settingsGroup)
61{
62 MultiVehicleManager* const multiVehicleManager = MultiVehicleManager::instance();
63 Vehicle* vehicle = multiVehicleManager->activeVehicle();
64 if (!vehicle) {
65 vehicle = multiVehicleManager->offlineEditingVehicle();
66 }
67 if (!vehicle) {
68 return {};
69 }
70
71 // A layout loads into a grid, which the core plugin fills with the defaults: one that is never shown.
72 FactValueGrid grid;
74 grid._activeVehicle = vehicle;
75 grid._resetFromSettings();
76
77 QList<LayoutFact> facts;
78 for (int colIndex = 0; colIndex < grid._columns->count(); colIndex++) {
79 const QmlObjectListModel* const column = grid._columns->value<const QmlObjectListModel*>(colIndex);
80 for (int rowIndex = 0; rowIndex < column->count(); rowIndex++) {
81 const InstrumentValueData* const value = column->value<InstrumentValueData*>(rowIndex);
82 if (value->fact()) {
83 facts.append({value->factGroupName(), value->factName()});
84 }
85 }
86 }
87 return facts;
88}
89
90void FactValueGrid::_initForNewVehicle(Vehicle* vehicle)
91{
92 if (!vehicle) {
93 qCritical() << "FactValueGrid::_initForNewVehicle: vehicle is NULL";
94 return;
95 }
96
97 connect(vehicle, &Vehicle::vehicleTypeChanged, this, &FactValueGrid::_resetFromSettings);
98 _resetFromSettings();
99}
100
101void FactValueGrid::_deinitVehicle(Vehicle* vehicle)
102{
103 disconnect(vehicle, &Vehicle::vehicleTypeChanged, this, &FactValueGrid::_resetFromSettings);
104}
105
106void FactValueGrid::_activeVehicleChanged(Vehicle* activeVehicle)
107{
108 if (!_activeVehicle) {
109 qCritical() << "FactValueGrid::_activeVehicleChanged: _activeVehicle is NULL";
110 }
111
112 if (_activeVehicle) {
113 _deinitVehicle(_activeVehicle);
114 _activeVehicle = nullptr;
115 }
116
117 if (!activeVehicle) {
119 }
120 _activeVehicle = activeVehicle;
121 _initForNewVehicle(activeVehicle);
122}
123
125{
126 _vehicleCardInstanceList.removeAll(this);
127}
128
133
135{
136 QSettings settings;
137 settings.remove(_settingsGroup);
138 _resetFromSettings();
139}
140
141QString FactValueGrid::_pascalCase(const QString& text)
142{
143 return text[0].toUpper() + text.right(text.length() - 1);
144}
145
153
154void FactValueGrid::_saveValueData(QSettings& settings, InstrumentValueData* value)
155{
156 settings.setValue(_textKey, value->text());
157 settings.setValue(_showUnitsKey, value->showUnits());
158 settings.setValue(_iconKey, value->icon());
159 settings.setValue(_rangeTypeKey, value->rangeType());
160
162 settings.setValue(_rangeValuesKey, value->rangeValues());
163 }
164
165 switch (value->rangeType()) {
167 break;
169 settings.setValue(_rangeColorsKey, value->rangeColors());
170 break;
172 settings.setValue(_rangeOpacitiesKey, value->rangeOpacities());
173 break;
175 settings.setValue(_rangeIconsKey, value->rangeIcons());
176 break;
177 }
178
179 settings.setValue(_factGroupNameKey, value->factGroupName());
180 settings.setValue(_factNameKey, value->factName());
181}
182
183void FactValueGrid::_loadValueData(QSettings& settings, InstrumentValueData* value)
184{
185 QString factName = settings.value(_factNameKey).toString();
186 if (!factName.isEmpty()) {
187 value->setFact(settings.value(_factGroupNameKey).toString(), factName);
188 }
189
190 value->setText (settings.value(_textKey).toString());
191 value->setShowUnits (settings.value(_showUnitsKey, true).toBool());
192 value->setIcon (settings.value(_iconKey).toString());
193 value->setRangeType (settings.value(_rangeTypeKey, InstrumentValueData::NoRangeInfo).value<InstrumentValueData::RangeType>());
194
196 value->setRangeValues(settings.value(_rangeValuesKey).value<QVariantList>());
197 }
198 switch (value->rangeType()) {
200 break;
202 value->setRangeColors(settings.value(_rangeColorsKey).value<QVariantList>());
203 break;
205 value->setRangeOpacities(settings.value(_rangeOpacitiesKey).value<QVariantList>());
206 break;
208 value->setRangeIcons(settings.value(_rangeIconsKey).value<QVariantList>());
209 break;
210 }
211}
212
213void FactValueGrid::_connectSaveSignals(InstrumentValueData* value)
214{
215 connect(value, &InstrumentValueData::factNameChanged, this, &FactValueGrid::_saveSettings);
216 connect(value, &InstrumentValueData::factGroupNameChanged, this, &FactValueGrid::_saveSettings);
217 connect(value, &InstrumentValueData::textChanged, this, &FactValueGrid::_saveSettings);
218 connect(value, &InstrumentValueData::showUnitsChanged, this, &FactValueGrid::_saveSettings);
219 connect(value, &InstrumentValueData::iconChanged, this, &FactValueGrid::_saveSettings);
220 connect(value, &InstrumentValueData::rangeTypeChanged, this, &FactValueGrid::_saveSettings);
221 connect(value, &InstrumentValueData::rangeValuesChanged, this, &FactValueGrid::_saveSettings);
222 connect(value, &InstrumentValueData::rangeColorsChanged, this, &FactValueGrid::_saveSettings);
223 connect(value, &InstrumentValueData::rangeOpacitiesChanged, this, &FactValueGrid::_saveSettings);
224 connect(value, &InstrumentValueData::rangeIconsChanged, this, &FactValueGrid::_saveSettings);
225}
226
228{
229 for (int colIndex=0; colIndex<_columns->count(); colIndex++) {
230 QmlObjectListModel* list = _columns->value<QmlObjectListModel*>(colIndex);
231 list->append(_createNewInstrumentValueWorker(list));
232
233 }
234 _rowCount++;
236 _saveSettings();
237}
238
240{
241 if (_rowCount <= 1) {
242 return;
243 }
244 for (int colIndex=0; colIndex<_columns->count(); colIndex++) {
245 QmlObjectListModel* list = _columns->value<QmlObjectListModel*>(colIndex);
246 list->removeAt(list->count() - 1)->deleteLater();
247 }
248 _rowCount--;
250 _saveSettings();
251}
252
254{
256 _columns->append(newList);
257
258 // If this is the first row then we automatically add the first column as well
259 int cRowsToAdd = qMax(_rowCount, 1);
260 for (int i=0; i<cRowsToAdd; i++) {
261 newList->append(_createNewInstrumentValueWorker(newList));
262 }
263
264 if (cRowsToAdd != _rowCount) {
265 _rowCount = cRowsToAdd;
267 }
268
269 _saveSettings();
270
271 return newList;
272}
273
275{
276 if (_columns->count() > 1) {
277 _columns->removeAt(_columns->count() - 1)->deleteLater();
278 _saveSettings();
279 }
280}
281
282InstrumentValueData* FactValueGrid::_createNewInstrumentValueWorker(QObject* parent)
283{
284 InstrumentValueData* value = new InstrumentValueData(this, parent);
285 value->setFact(InstrumentValueData::vehicleFactGroupName, "AltitudeRelative");
286 value->setText(value->fact()->shortDescription());
287 _connectSaveSignals(value);
288 return value;
289
290}
291
292void FactValueGrid::_saveSettings(void)
293{
295 return;
296 }
297
298 QSettings settings;
299 QString groupNameFormat("%1-%2");
300
301 settings.beginGroup(_settingsKey());
302 settings.remove(""); // Remove any previous settings
303
304 settings.setValue(_versionKey, 1);
305 settings.setValue(_fontSizeKey, _fontSize);
306 settings.setValue(_rowCountKey, _rowCount);
307
308 settings.beginWriteArray(_columnsKey);
309 for (int colIndex=0; colIndex<_columns->count(); colIndex++) {
310 QmlObjectListModel* columns = _columns->value<QmlObjectListModel*>(colIndex);
311
312 settings.setArrayIndex(colIndex);
313 settings.beginWriteArray(_rowsKey);
314
315 for (int rowIndex=0; rowIndex<columns->count(); rowIndex++) {
316 InstrumentValueData* value = columns->value<InstrumentValueData*>(rowIndex);
317 settings.setArrayIndex(rowIndex);
318 _saveValueData(settings, value);
319 }
320
321 settings.endArray();
322 }
323 settings.endArray();
324
326
327 // If this settings change was set from a Vehicle card, this makes so the changes are
328 // immediately applied to the other Vehicle cards.
330 for (FactValueGrid* obj : _vehicleCardInstanceList) {
331 if (obj != this) {
332 obj->_resetFromSettings();
333 }
334 }
335 }
336}
337
338QString FactValueGrid::_settingsKey(void)
339{
340 return QStringLiteral("%1-%2").arg(_settingsGroup).arg(vehicleClass());
341}
342
343void FactValueGrid::_resetFromSettings(void)
344{
346
347 _columns->deleteLater();
348
349 _columns = new QmlObjectListModel(this);
350 _rowCount = 0;
351
352 QSettings settings;
353 QString groupNameFormat("%1-%2");
354
355 if (settings.childGroups().contains(_settingsKey())) {
356 // Load from settings
357 settings.beginGroup(_settingsKey());
358
359 int version = settings.value(_versionKey, 0).toInt();
360 if (version != 1) {
361 QGC::showAppMessage(tr("Settings version %1 for %2 is not supported. Setup will be reset to defaults.").arg(version).arg(_settingsGroup), tr("Load Settings"));
362 settings.remove("");
364 }
365 _fontSize = settings.value(_fontSizeKey, DefaultFontSize).value<FontSize>();
366
367 // Initial setup of empty items
368 int cRows = settings.value(_rowCountKey).toInt();
369 int cModelLists = settings.beginReadArray(_columnsKey);
370 if (cModelLists && cRows) {
371 appendColumn();
372 for (int rowIndex=1; rowIndex<cRows; rowIndex++) {
373 appendRow();
374 }
375 for (int colIndex=1; colIndex<cModelLists; colIndex++) {
376 appendColumn();
377 }
378 }
379
380 // Fill in the items from settings
381 for (int colIndex=0; colIndex<cModelLists; colIndex++) {
382 settings.setArrayIndex(colIndex);
383 int cItems = settings.beginReadArray(_rowsKey);
384 for (int itemIndex=0; itemIndex<cItems; itemIndex++) {
385 QmlObjectListModel* list = _columns->value<QmlObjectListModel*>(colIndex);
386 InstrumentValueData* value = list->value<InstrumentValueData*>(itemIndex);
387 settings.setArrayIndex(itemIndex);
388 _loadValueData(settings, value);
389 }
390 settings.endArray();
391 }
392 settings.endArray();
393 } else {
394 // Default settings are added directly to this FactValueGrid
396 }
397
399
400 _preventSaveSettings = false;
401}
static FactValueGridLayouts * instance()
Q_INVOKABLE void deleteLastColumn(void)
void columnsChanged(QmlObjectListModel *model)
Vehicle * _activeVehicle
Q_INVOKABLE void resetToDefaults(void)
< null means track active vehicle, set to specific vehicle to track a single vehicle and share settin...
Vehicle * _specificVehicleForCard
static QList< LayoutFact > layoutFacts(const QString &settingsGroup)
void rowCountChanged(int rowCount)
QmlObjectListModel * columns(void) const
Vehicle * currentVehicle(void) const
FontSize _fontSize
void componentComplete(void) final
QmlObjectListModel * _columns
QString settingsGroup(void) const
void setFontSize(FontSize fontSize)
Q_INVOKABLE QmlObjectListModel * appendColumn(void)
FactValueGrid(QQuickItem *parent=nullptr)
Q_INVOKABLE void deleteLastRow(void)
QGCMAVLinkTypes::VehicleClass_t vehicleClass(void) const
Q_INVOKABLE void appendRow(void)
QString _settingsGroup
bool _preventSaveSettings
void fontSizeChanged(FontSize fontSize)
FontSize fontSize(void) const
QString shortDescription() const
Definition Fact.cc:525
static constexpr const char * vehicleFactGroupName
void setRangeOpacities(const QVariantList &rangeOpacities)
void rangeOpacitiesChanged(const QVariantList &rangeOpacities)
QString text(void) const
void showUnitsChanged(bool showUnits)
void factGroupNameChanged(const QString &factGroup)
QVariantList rangeOpacities(void) const
void rangeTypeChanged(RangeType rangeType)
Fact * fact(void) const
void setText(const QString &text)
QVariantList rangeColors(void) const
void setRangeType(RangeType rangeType)
QVariantList rangeValues(void) const
void setIcon(const QString &icon)
RangeType rangeType(void) const
void textChanged(QString text)
bool showUnits(void) const
void rangeIconsChanged(const QVariantList &rangeIcons)
void rangeValuesChanged(const QVariantList &rangeValues)
void rangeColorsChanged(const QVariantList &rangeColors)
QString factGroupName(void) const
void setRangeColors(const QVariantList &rangeColors)
void factNameChanged(const QString &factName)
void setRangeIcons(const QVariantList &rangeIcons)
void setShowUnits(bool showUnits)
void setRangeValues(const QVariantList &rangeValues)
QString icon(void) const
QVariantList rangeIcons(void) const
void iconChanged(const QString &icon)
QString factName(void) const
Q_INVOKABLE void setFact(const QString &factGroupName, const QString &factName)
static MultiVehicleManager * instance()
Vehicle * activeVehicle() const
Vehicle * offlineEditingVehicle() const
void activeVehicleChanged(Vehicle *activeVehicle)
virtual void factValueGridCreateDefaultSettings(FactValueGrid *factValueGrid)
static QGCCorePlugin * instance()
void append(QObject *object)
Caller maintains responsibility for object ownership and deletion.
T value(int index) const
QObject * removeAt(int index)
int count() const override final
void vehicleTypeChanged()
void showAppMessage(const QString &message, const QString &title)
Modal application message. Queued if the UI isn't ready yet.
Definition AppMessages.cc:9