FactValueGrid.cc 12.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/****************************************************************************
 *
 * (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>

const char* FactValueGrid::_rowsKey =             "rows";
const char* FactValueGrid::_fontSizeKey =         "fontSize";
const char* FactValueGrid::_versionKey =          "version";
const char* FactValueGrid::_factGroupNameKey =    "groupName";
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";

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)
45
    , _columns  (new QmlObjectListModel(this))
46 47 48 49 50 51
{
    if (_iconNames.isEmpty()) {
        QDir iconDir(":/InstrumentValueIcons/");
        _iconNames = iconDir.entryList();
    }

52
    _init();
53 54 55 56 57
}

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

63
void FactValueGrid::_init(void)
64
{
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    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();
    }
82 83 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
}

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);
}

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

    }
197 198
    _rowCount++;
    emit rowCountChanged(_rowCount);
199 200 201
    _saveSettings();
}

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

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

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

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

    _saveSettings();

    return newList;
}

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

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;
    }

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

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

273 274 275
    settings.setValue(_versionKey,  1);
    settings.setValue(_fontSizeKey, _fontSize);
    settings.setValue(_rowsKey,     _rowCount);
276 277

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

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

        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)
{
297 298
    _preventSaveSettings = true;

299
    _columns->deleteLater();
300

301 302
    _columns    = new QmlObjectListModel(this);
    _rowCount   = 0;
303 304 305 306 307

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

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


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

    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
327
    int cColumns = settings.value(_rowsKey).toInt();
328 329
    int cModelLists = settings.beginReadArray(_rowsKey);
    if (cModelLists && cColumns) {
330
        appendColumn();
331 332 333
        for (int itemIndex=1; itemIndex<cColumns; itemIndex++) {
            appendRow();
        }
334 335 336
        for (int colIndex=1; colIndex<cModelLists; colIndex++) {
            appendColumn();
        }
337 338 339
    }

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

353
    emit columnsChanged(_columns);
354

355
    _preventSaveSettings = false;
356
}