FactValueGrid.cc 12.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/****************************************************************************
 *
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

#include "FactValueGrid.h"
#include "InstrumentValueData.h"
#include "QGCApplication.h"
#include "QGCCorePlugin.h"

#include <QSettings>

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
const char* FactValueGrid::_columnsKey          = "columns";
const char* FactValueGrid::_rowsKey             = "rows";
const char* FactValueGrid::_rowCountKey         = "rowCount";
const char* FactValueGrid::_fontSizeKey         = "fontSize";
const char* FactValueGrid::_versionKey          = "version";
const char* FactValueGrid::_factGroupNameKey    = "factGroupName";
const char* FactValueGrid::_factNameKey         = "factName";
const char* FactValueGrid::_textKey             = "text";
const char* FactValueGrid::_showUnitsKey        = "showUnits";
const char* FactValueGrid::_iconKey             = "icon";
const char* FactValueGrid::_rangeTypeKey        = "rangeType";
const char* FactValueGrid::_rangeValuesKey      = "rangeValues";
const char* FactValueGrid::_rangeColorsKey      = "rangeColors";
const char* FactValueGrid::_rangeIconsKey       = "rangeIcons";
const char* FactValueGrid::_rangeOpacitiesKey   = "rangeOpacities";
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

const char* FactValueGrid::_deprecatedGroupKey =  "ValuesWidget";

QStringList FactValueGrid::_iconNames;

// Important: The indices of these strings must match the FactValueGrid::FontSize enum
const QStringList FactValueGrid::_fontSizeNames = {
    QT_TRANSLATE_NOOP("FactValueGrid", "Default"),
    QT_TRANSLATE_NOOP("FactValueGrid", "Small"),
    QT_TRANSLATE_NOOP("FactValueGrid", "Medium"),
    QT_TRANSLATE_NOOP("FactValueGrid", "Large"),
};

FactValueGrid::FactValueGrid(QQuickItem* parent)
    : QQuickItem(parent)
47
    , _columns  (new QmlObjectListModel(this))
48 49 50 51 52 53
{
    if (_iconNames.isEmpty()) {
        QDir iconDir(":/InstrumentValueIcons/");
        _iconNames = iconDir.entryList();
    }

54
    _init();
55 56 57 58 59
}

FactValueGrid::FactValueGrid(const QString& defaultSettingsGroup)
    : QQuickItem            (nullptr)
    , _defaultSettingsGroup (defaultSettingsGroup)
60
    , _columns              (new QmlObjectListModel(this))
61
{
62
    _init();
63 64
}

65
void FactValueGrid::_init(void)
66
{
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    Vehicle* offlineVehicle  = qgcApp()->toolbox()->multiVehicleManager()->offlineEditingVehicle();

    connect(offlineVehicle, &Vehicle::vehicleTypeChanged,       this, &FactValueGrid::_offlineVehicleTypeChanged);
    connect(this,           &FactValueGrid::fontSizeChanged,    this, &FactValueGrid::_saveSettings);

    _vehicleClass = QGCMAVLink::vehicleClass(offlineVehicle->vehicleType());
}

void FactValueGrid::_offlineVehicleTypeChanged(void)
{
    Vehicle*                    offlineVehicle  = qgcApp()->toolbox()->multiVehicleManager()->offlineEditingVehicle();
    QGCMAVLink::VehicleClass_t  newVehicleClass = QGCMAVLink::vehicleClass(offlineVehicle->vehicleType());

    if (newVehicleClass != _vehicleClass) {
        _vehicleClass = newVehicleClass;
        _loadSettings();
    }
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
}

void FactValueGrid::componentComplete(void)
{
    QQuickItem::componentComplete();

    // We should know settingsGroup/defaultSettingsGroup now so we can load settings
    _loadSettings();
}

void FactValueGrid::resetToDefaults(void)
{
    QSettings settings;
    settings.remove(_userSettingsGroup);
    _loadSettings();
}

QString FactValueGrid::_pascalCase(const QString& text)
{
    return text[0].toUpper() + text.right(text.length() - 1);
}

void FactValueGrid::setFontSize(FontSize fontSize)
{
    if (fontSize != _fontSize) {
        _fontSize = fontSize;
        emit fontSizeChanged(fontSize);
    }
}

void FactValueGrid::_saveValueData(QSettings& settings, InstrumentValueData* value)
{
    settings.setValue(_textKey,         value->text());
    settings.setValue(_showUnitsKey,    value->showUnits());
    settings.setValue(_iconKey,         value->icon());
    settings.setValue(_rangeTypeKey,    value->rangeType());

    if (value->rangeType() != InstrumentValueData::NoRangeInfo) {
        settings.setValue(_rangeValuesKey, value->rangeValues());
    }

    switch (value->rangeType()) {
    case InstrumentValueData::NoRangeInfo:
        break;
    case InstrumentValueData::ColorRange:
        settings.setValue(_rangeColorsKey,      value->rangeColors());
        break;
    case InstrumentValueData::OpacityRange:
        settings.setValue(_rangeOpacitiesKey,   value->rangeOpacities());
        break;
    case InstrumentValueData::IconSelectRange:
        settings.setValue(_rangeIconsKey,       value->rangeIcons());
        break;
    }

    if (value->fact()) {
        settings.setValue(_factGroupNameKey,    value->factGroupName());
        settings.setValue(_factNameKey,         value->factName());
    } else {
        settings.setValue(_factGroupNameKey,    "");
        settings.setValue(_factNameKey,         "");
    }
}

void FactValueGrid::_loadValueData(QSettings& settings, InstrumentValueData* value)
{
    QString factName = settings.value(_factNameKey).toString();
    if (!factName.isEmpty()) {
        value->setFact(settings.value(_factGroupNameKey).toString(), factName);
    }

    value->setText      (settings.value(_textKey).toString());
    value->setShowUnits (settings.value(_showUnitsKey, true).toBool());
    value->setIcon      (settings.value(_iconKey).toString());
    value->setRangeType (settings.value(_rangeTypeKey, InstrumentValueData::NoRangeInfo).value<InstrumentValueData::RangeType>());

    if (value->rangeType() != InstrumentValueData::NoRangeInfo) {
        value->setRangeValues(settings.value(_rangeValuesKey).value<QVariantList>());
    }
    switch (value->rangeType()) {
    case InstrumentValueData::NoRangeInfo:
        break;
    case InstrumentValueData::ColorRange:
        value->setRangeColors(settings.value(_rangeColorsKey).value<QVariantList>());
        break;
    case InstrumentValueData::OpacityRange:
        value->setRangeOpacities(settings.value(_rangeOpacitiesKey).value<QVariantList>());
        break;
    case InstrumentValueData::IconSelectRange:
        value->setRangeIcons(settings.value(_rangeIconsKey).value<QVariantList>());
        break;
    }
}

void FactValueGrid::_connectSaveSignals(InstrumentValueData* value)
{
    connect(value, &InstrumentValueData::factNameChanged,       this, &FactValueGrid::_saveSettings);
    connect(value, &InstrumentValueData::factGroupNameChanged,  this, &FactValueGrid::_saveSettings);
    connect(value, &InstrumentValueData::textChanged,           this, &FactValueGrid::_saveSettings);
    connect(value, &InstrumentValueData::showUnitsChanged,      this, &FactValueGrid::_saveSettings);
    connect(value, &InstrumentValueData::iconChanged,           this, &FactValueGrid::_saveSettings);
    connect(value, &InstrumentValueData::rangeTypeChanged,      this, &FactValueGrid::_saveSettings);
    connect(value, &InstrumentValueData::rangeValuesChanged,    this, &FactValueGrid::_saveSettings);
    connect(value, &InstrumentValueData::rangeColorsChanged,    this, &FactValueGrid::_saveSettings);
    connect(value, &InstrumentValueData::rangeOpacitiesChanged, this, &FactValueGrid::_saveSettings);
    connect(value, &InstrumentValueData::rangeIconsChanged,     this, &FactValueGrid::_saveSettings);
}

192
void FactValueGrid::appendRow(void)
193
{
194 195
    for (int colIndex=0; colIndex<_columns->count(); colIndex++) {
        QmlObjectListModel* list = _columns->value<QmlObjectListModel*>(colIndex);
196 197 198
        list->append(_createNewInstrumentValueWorker(list));

    }
199 200
    _rowCount++;
    emit rowCountChanged(_rowCount);
201 202 203
    _saveSettings();
}

204
void FactValueGrid::deleteLastRow(void)
205
{
206
    if (_rowCount <= 1) {
207 208
        return;
    }
209 210
    for (int colIndex=0; colIndex<_columns->count(); colIndex++) {
        QmlObjectListModel* list = _columns->value<QmlObjectListModel*>(colIndex);
211 212
        list->removeAt(list->count() - 1)->deleteLater();
    }
213 214
    _rowCount--;
    emit rowCountChanged(_rowCount);
215 216 217
    _saveSettings();
}

218
QmlObjectListModel* FactValueGrid::appendColumn(void)
219
{
220 221
    QmlObjectListModel* newList = new QmlObjectListModel(_columns);
    _columns->append(newList);
222 223

    // If this is the first row then we automatically add the first column as well
224 225
    int cRowsToAdd = qMax(_rowCount, 1);
    for (int i=0; i<cRowsToAdd; i++) {
226 227 228
        newList->append(_createNewInstrumentValueWorker(newList));
    }

229 230 231
    if (cRowsToAdd != _rowCount) {
        _rowCount = cRowsToAdd;
        emit rowCountChanged(_rowCount);
232 233 234 235 236 237 238
    }

    _saveSettings();

    return newList;
}

239
void FactValueGrid::deleteLastColumn(void)
240
{
241 242
    if (_columns->count() > 1) {
        _columns->removeAt(_columns->count() - 1)->deleteLater();
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
    }
}

InstrumentValueData* FactValueGrid::_createNewInstrumentValueWorker(QObject* parent)
{
    InstrumentValueData* value = new InstrumentValueData(this, parent);
    value->setFact(InstrumentValueData::vehicleFactGroupName, "AltitudeRelative");
    value->setText(value->fact()->shortDescription());
    _connectSaveSignals(value);
    return value;

}

void FactValueGrid::_saveSettings(void)
{
    if (_preventSaveSettings) {
        return;
    }

262 263
    QSettings   settings;
    QString     groupNameFormat("%1-%2");
264 265
    if (_userSettingsGroup.isEmpty()) {
        // This means we are setting up default settings
266
        settings.beginGroup(groupNameFormat.arg(_defaultSettingsGroup).arg(_vehicleClass));
267 268
    } else {
        // This means we are saving user modifications
269 270
        settings.remove(groupNameFormat.arg(_defaultSettingsGroup).arg(_vehicleClass));
        settings.beginGroup(groupNameFormat.arg(_userSettingsGroup).arg(_vehicleClass));
271 272 273 274
    }

    settings.remove(""); // Remove any previous settings

275 276
    settings.setValue(_versionKey,  1);
    settings.setValue(_fontSizeKey, _fontSize);
277
    settings.setValue(_rowCountKey, _rowCount);
278

279
    settings.beginWriteArray(_columnsKey);
280 281
    for (int colIndex=0; colIndex<_columns->count(); colIndex++) {
        QmlObjectListModel* columns = _columns->value<QmlObjectListModel*>(colIndex);
282

283 284
        settings.setArrayIndex(colIndex);
        settings.beginWriteArray(_rowsKey);
285 286 287 288 289 290 291 292 293 294 295 296 297 298

        for (int colIndex=0; colIndex<columns->count(); colIndex++) {
            InstrumentValueData* value = columns->value<InstrumentValueData*>(colIndex);
            settings.setArrayIndex(colIndex);
            _saveValueData(settings, value);
        }

        settings.endArray();
    }
    settings.endArray();
}

void FactValueGrid::_loadSettings(void)
{
299 300
    _preventSaveSettings = true;

301
    _columns->deleteLater();
302

303 304
    _columns    = new QmlObjectListModel(this);
    _rowCount   = 0;
305 306 307 308 309

    QSettings   settings;
    QString     groupNameFormat("%1-%2");

    if (!settings.childGroups().contains(groupNameFormat.arg(_userSettingsGroup).arg(_vehicleClass))) {
310 311 312 313
        qgcApp()->toolbox()->corePlugin()->factValueGridCreateDefaultSettings(_defaultSettingsGroup);
    }


314 315
    if (settings.childGroups().contains(groupNameFormat.arg(_defaultSettingsGroup).arg(_vehicleClass))) {
        settings.beginGroup(groupNameFormat.arg(_defaultSettingsGroup).arg(_vehicleClass));
316
    } else {
317
        settings.beginGroup(groupNameFormat.arg(_userSettingsGroup).arg(_vehicleClass));
318 319 320 321 322 323 324 325 326 327 328
    }

    int version = settings.value(_versionKey, 0).toInt();
    if (version != 1) {
        qgcApp()->showAppMessage(tr("Settings version %1 for %2 is not supported. Setup will be reset to defaults.").arg(version).arg(_userSettingsGroup), tr("Load Settings"));
        settings.remove("");
        qgcApp()->toolbox()->corePlugin()->factValueGridCreateDefaultSettings(_defaultSettingsGroup);
    }
    _fontSize = settings.value(_fontSizeKey, DefaultFontSize).value<FontSize>();

    // Initial setup of empty items
329 330 331
    int cRows       = settings.value(_rowCountKey).toInt();
    int cModelLists = settings.beginReadArray(_columnsKey);
    if (cModelLists && cRows) {
332
        appendColumn();
333
        for (int rowIndex=1; rowIndex<cRows; rowIndex++) {
334 335
            appendRow();
        }
336 337 338
        for (int colIndex=1; colIndex<cModelLists; colIndex++) {
            appendColumn();
        }
339 340 341
    }

    // Fill in the items from settings
342 343 344
    for (int colIndex=0; colIndex<cModelLists; colIndex++) {
        settings.setArrayIndex(colIndex);
        int cItems = settings.beginReadArray(_rowsKey);
345
        for (int itemIndex=0; itemIndex<cItems; itemIndex++) {
346
            QmlObjectListModel* list = _columns->value<QmlObjectListModel*>(colIndex);
347 348 349 350 351 352 353 354
            InstrumentValueData* value = list->value<InstrumentValueData*>(itemIndex);
            settings.setArrayIndex(itemIndex);
            _loadValueData(settings, value);
        }
        settings.endArray();
    }
    settings.endArray();

355
    emit columnsChanged(_columns);
356

357
    _preventSaveSettings = false;
358
}