ValuesWidgetController.cc 17.3 KB
Newer Older
1 2
/****************************************************************************
 *
Gus Grubba's avatar
Gus Grubba committed
3
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 5 6 7 8 9
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

10 11

#include "ValuesWidgetController.h"
12 13
#include "QGCApplication.h"
#include "QGCCorePlugin.h"
14 15 16

#include <QSettings>

17 18 19
const char* ValuesWidgetController::_groupKey =         "ValuesWidget2";
const char* ValuesWidgetController::_rowsKey =          "rows";
const char* ValuesWidgetController::_columnsKey =       "columns";
20

21 22 23 24 25 26 27 28 29
const char* ValuesWidgetController::_deprecatedGroupKey =         "ValuesWidget";
const char* ValuesWidgetController::_deprecatedLargeValuesKey =   "large";
const char* ValuesWidgetController::_deprecatedSmallValuesKey =   "small";

const char*  InstrumentValue::_factGroupNameKey =       "groupName";
const char*  InstrumentValue::_factNameKey =             "factName";
const char*  InstrumentValue::_labelKey =               "label";
const char*  InstrumentValue::_fontSizeKey =            "fontSize";
const char*  InstrumentValue::_showUnitsKey =           "showUnits";
30 31
const char*  InstrumentValue::_iconKey =                "icon";
const char*  InstrumentValue::_iconPositionKey =        "iconPosition";
32
const char*  InstrumentValue::_vehicleFactGroupName =   "Vehicle";
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

QStringList InstrumentValue::_iconNames;

// Important: The indices of these strings must match the InstrumentValue::IconPosition enumconst QStringList InstrumentValue::_iconPositionNames = {
const QStringList InstrumentValue::_iconPositionNames = {
    QT_TRANSLATE_NOOP("InstrumentValue", "Above"),
    QT_TRANSLATE_NOOP("InstrumentValue", "Left"),
};

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

ValuesWidgetController::ValuesWidgetController(bool forDefaultSettingsCreation)
    : _valuesModel(new QmlObjectListModel(this))
52 53 54 55 56
{
    QSettings settings;

    settings.beginGroup(_groupKey);

57 58 59 60 61 62 63 64
    _multiVehicleMgr = qgcApp()->toolbox()->multiVehicleManager();
    connect(_multiVehicleMgr, &MultiVehicleManager::activeVehicleChanged, this, &ValuesWidgetController::_activeVehicleChanged);

    if (!forDefaultSettingsCreation) {
        _loadSettings();
    }
}

65
void ValuesWidgetController::_connectSignalsToController(InstrumentValue* value, ValuesWidgetController* controller)
66
{
67 68 69 70 71 72 73 74
    connect(value, &InstrumentValue::factNameChanged,        controller, &ValuesWidgetController::_saveSettings);
    connect(value, &InstrumentValue::factGroupNameChanged,   controller, &ValuesWidgetController::_saveSettings);
    connect(value, &InstrumentValue::labelChanged,           controller, &ValuesWidgetController::_saveSettings);
    connect(value, &InstrumentValue::fontSizeChanged,        controller, &ValuesWidgetController::_saveSettings);
    connect(value, &InstrumentValue::showUnitsChanged,       controller, &ValuesWidgetController::_saveSettings);
    connect(value, &InstrumentValue::iconChanged,            controller, &ValuesWidgetController::_saveSettings);
    connect(value, &InstrumentValue::iconPositionChanged,    controller, &ValuesWidgetController::_saveSettings);
}
75

76 77 78 79
InstrumentValue* ValuesWidgetController::_createNewInstrumentValueWorker(Vehicle* activeVehicle, InstrumentValue::FontSize fontSize, QmlObjectListModel* rowModel)
{
    InstrumentValue* newValue = new InstrumentValue(activeVehicle, fontSize, rowModel);
    _connectSignalsToController(newValue, this);
80 81
    return newValue;
}
82

83 84 85
InstrumentValue* ValuesWidgetController::appendColumn(int rowIndex)
{
    InstrumentValue* newValue = nullptr;
86

87 88
    if (rowIndex >= 0 && rowIndex < _valuesModel->count()) {
        QmlObjectListModel* row = _valuesModel->value<QmlObjectListModel*>(rowIndex);
89
        InstrumentValue::FontSize fontSize = InstrumentValue::DefaultFontSize;
90 91 92 93 94
        if (row->count()) {
            fontSize = row->value<InstrumentValue*>(0)->fontSize();
        }
        newValue  = _createNewInstrumentValueWorker(_currentActiveVehicle(), fontSize, row);
        row->append(newValue);
Don Gagne's avatar
Don Gagne committed
95
    }
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    _saveSettings();

    return newValue;
}

void ValuesWidgetController::deleteLastColumn(int rowIndex)
{
    if (rowIndex >= 0 && rowIndex < _valuesModel->count()) {
        QmlObjectListModel* row = _valuesModel->value<QmlObjectListModel*>(rowIndex);

        if (rowIndex != 0 || row->count() > 1) {
            row->removeAt(row->count() - 1);
        }
        if (row->count() == 0) {
            // Last column was deleted, delete the row as well
            _valuesModel->removeAt(rowIndex);
        }
Don Gagne's avatar
Don Gagne committed
113
    }
114
    _saveSettings();
115 116
}

117
QmlObjectListModel* ValuesWidgetController::appendRow(bool addBlanksColumn)
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 192 193 194 195 196 197 198 199 200
    QmlObjectListModel* newRow = new QmlObjectListModel(_valuesModel);
    _valuesModel->append(newRow);
    int rowIndex = _valuesModel->count() - 1;
    if (addBlanksColumn) {
        appendColumn(rowIndex);
    }
    _saveSettings();
    return newRow;
}

QmlObjectListModel* ValuesWidgetController::insertRow(int atIndex, bool addBlanksColumn)
{
    QmlObjectListModel* newRow = nullptr;

    if (atIndex >= 0 && atIndex < _valuesModel->count() + 1) {
        QmlObjectListModel* newRow = new QmlObjectListModel(_valuesModel);
        _valuesModel->insert(atIndex, newRow);
        if (addBlanksColumn) {
            appendColumn(atIndex);
        }
        _saveSettings();
    }
    return newRow;
}

void ValuesWidgetController::deleteRow(int rowIndex)
{
    if (rowIndex >= 0 && rowIndex < _valuesModel->count()) {
        if (_valuesModel->count() > 1) {
            _valuesModel->removeAt(rowIndex);
        }
        _saveSettings();
    }
}

Vehicle* ValuesWidgetController::_currentActiveVehicle(void)
{
    Vehicle* activeVehicle = _multiVehicleMgr->activeVehicle();
    if (!activeVehicle) {
        activeVehicle = _multiVehicleMgr->offlineEditingVehicle();
    }
    return activeVehicle;
}

void ValuesWidgetController::_activeVehicleChanged(Vehicle* activeVehicle)
{
    if (!activeVehicle) {
        activeVehicle = _currentActiveVehicle();
    }

    for (int rowIndex=0; rowIndex<_valuesModel->count(); rowIndex++) {
        QmlObjectListModel* rowModel = _valuesModel->value<QmlObjectListModel*>(rowIndex);
        for (int colIndex=0; colIndex<rowModel->count(); colIndex++) {
            rowModel->value<InstrumentValue*>(colIndex)->activeVehicleChanged(activeVehicle);
        }
    }
}

bool ValuesWidgetController::_validRowIndex(int rowIndex)
{
    return rowIndex >= 0 && rowIndex < _valuesModel->count();
}


int ValuesWidgetController::fontSizeForRow(int rowIndex)
{
    return _validRowIndex(rowIndex) ? _rgFontSizeByRow[rowIndex].toInt() : _rgFontSizeByRow[0].toInt();
}

void ValuesWidgetController::setFontSizeForRow(int rowIndex, int fontSize)
{
    if (_validRowIndex(rowIndex)) {
        _rgFontSizeByRow[rowIndex] = fontSize;
    }
}

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

201 202 203
    QSettings settings;

    settings.beginGroup(_groupKey);
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    settings.remove(QStringLiteral(""));
    settings.beginWriteArray(_rowsKey);

    for (int rowIndex=0; rowIndex<_valuesModel->count(); rowIndex++) {
        QmlObjectListModel* colValuesModel = _valuesModel->value<QmlObjectListModel*>(rowIndex);

        settings.setArrayIndex(rowIndex);
        settings.beginWriteArray(_columnsKey);

        for (int colIndex=0; colIndex<colValuesModel->count(); colIndex++) {
            settings.setArrayIndex(colIndex);
            colValuesModel->value<InstrumentValue*>(colIndex)->saveToSettings(settings);
        }

        settings.endArray();
    }

    settings.endArray();
}


void ValuesWidgetController::_loadSettings(void)
{
    QSettings settings;

    _valuesModel->deleteLater();
    _valuesModel = new QmlObjectListModel(this);
    emit valuesModelChanged(_valuesModel);

    if (settings.childGroups().contains(_deprecatedGroupKey)) {
        settings.beginGroup(_deprecatedGroupKey);

        QStringList largeValues = settings.value(_deprecatedLargeValuesKey).toStringList();
        QStringList smallValues = settings.value(_deprecatedSmallValuesKey).toStringList();
238
        QStringList altitudeProperties = { "AltitudeRelative" , "AltitudeAMSL" };
239 240 241 242 243

        int rowIndex = -1;
        int valueCount = 0;
        QmlObjectListModel* rowModel = nullptr;
        for (const QString& largeValue: largeValues) {
244 245 246
            QStringList parts =     largeValue.split(".");
            QString factGroupName = _pascalCase(parts[0]);
            QString factName =      _pascalCase(parts[1]);
247 248 249 250 251

            rowModel = appendRow(false /* addBlankColumn */);
            rowIndex++;

            InstrumentValue* colValue = appendColumn(rowIndex);
252
            colValue->setFact(factGroupName, factName);
253 254
            colValue->setLabel(colValue->fact()->shortDescription());
            colValue->setShowUnits(true);
255
            colValue->setFontSize(altitudeProperties.contains(factName) ? InstrumentValue::LargeFontSize : InstrumentValue::DefaultFontSize);
256
        }
257

258 259 260
        valueCount = 0;
        rowModel = nullptr;
        for (const QString& smallValue: smallValues) {
261 262 263
            QStringList parts =     smallValue.split(".");
            QString factGroupName = _pascalCase(parts[0]);
            QString factName =      _pascalCase(parts[1]);
264 265 266 267 268 269 270

            if (!(valueCount++ & 1)) {
                rowModel = appendRow(false /* addBlankColumn */);
                rowIndex++;
            }

            InstrumentValue* colValue = appendColumn(rowIndex);
271
            colValue->setFact(factGroupName, factName);
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
            colValue->setLabel(colValue->fact()->shortDescription());
            colValue->setShowUnits(true);
            colValue->setFontSize(InstrumentValue::SmallFontSize);
        }

        settings.endGroup();
        settings.remove(_deprecatedGroupKey);
    } else {
        _preventSaveSettings = true;

        settings.beginGroup(_groupKey);
        int cRows = settings.beginReadArray(_rowsKey);

        for (int rowIndex=0; rowIndex<cRows; rowIndex++) {
            appendRow(false /* addBlankColumns */);

            settings.setArrayIndex(rowIndex);
            int cCols = settings.beginReadArray(_columnsKey);

            for (int colIndex=0; colIndex<cCols; colIndex++) {
                settings.setArrayIndex(colIndex);
                appendColumn(rowIndex)->readFromSettings(settings);
            }

            settings.endArray();
        }

        settings.endArray();

        _preventSaveSettings = false;
    }

    // Use defaults if nothing there
    if (_valuesModel->count() == 0) {
        _valuesModel->deleteLater();
        _valuesModel = qgcApp()->toolbox()->corePlugin()->valuesWidgetDefaultSettings(this);
        emit valuesModelChanged(_valuesModel);
    }
310 311
}

312
void ValuesWidgetController::resetToDefaults(void)
313 314 315 316
{
    QSettings settings;

    settings.beginGroup(_groupKey);
317 318 319 320 321 322 323 324 325 326
    settings.remove("");

    _loadSettings();
}

void ValuesWidgetController::setPreventSaveSettings(bool preventSaveSettings)
{
    _preventSaveSettings = preventSaveSettings;
}

327 328 329 330 331 332 333 334 335 336 337 338 339
void ValuesWidgetController::setValuesModelParentController(ValuesWidgetController* newParentController)
{
    _valuesModel->setParent(newParentController);

    // Signalling must be reconnected to new controller as well
    for (int rowIndex=0; rowIndex<_valuesModel->count(); rowIndex++) {
        QmlObjectListModel* rowModel = _valuesModel->value<QmlObjectListModel*>(rowIndex);
        for (int colIndex=0; colIndex<rowModel->count(); colIndex++) {
            _connectSignalsToController(rowModel->value<InstrumentValue*>(colIndex), newParentController);
        }
    }
}

340 341 342 343 344
QString ValuesWidgetController::_pascalCase(const QString& text)
{
    return text[0].toUpper() + text.right(text.length() - 1);
}

345
InstrumentValue::InstrumentValue(Vehicle* activeVehicle, FontSize fontSize, QmlObjectListModel* rowModel)
346 347 348 349 350
    : QObject       (rowModel)
    , _activeVehicle(activeVehicle)
    , _rowModel     (rowModel)
    , _fontSize     (fontSize)
{
351 352 353 354
    if (_iconNames.isEmpty()) {
        QDir iconDir(":/InstrumentValueIcons/");
        _iconNames = iconDir.entryList();
    }
355 356

    activeVehicleChanged(_activeVehicle);
357 358 359 360 361 362
}

void InstrumentValue::activeVehicleChanged(Vehicle* activeVehicle)
{
    _activeVehicle = activeVehicle;

363 364 365 366 367 368 369 370
    _factGroupNames.clear();
    _factGroupNames = _activeVehicle->factGroupNames();
    for (QString& name: _factGroupNames) {
        name[0] = name[0].toUpper();
    }
    _factGroupNames.prepend(_vehicleFactGroupName);
    emit factGroupNamesChanged(_factGroupNames);

371 372 373 374
    if (_fact) {
        _fact = nullptr;

        FactGroup* factGroup = nullptr;
375
        if (_factGroupName == _vehicleFactGroupName) {
376 377 378 379 380 381
            factGroup = _activeVehicle;
        } else {
            factGroup = _activeVehicle->getFactGroup(_factGroupName);
        }

        if (factGroup) {
382
            _fact = factGroup->getFact(_factName);
383 384 385 386 387
        }
        emit factChanged(_fact);
    }
}

388
void InstrumentValue::setFact(const QString& factGroupName, const QString& factName)
389 390 391 392 393 394
{
    if (_fact) {
        _fact = nullptr;
    }

    FactGroup* factGroup = nullptr;
395
    if (factGroupName == _vehicleFactGroupName) {
396 397 398 399 400
        factGroup = _activeVehicle;
    } else {
        factGroup = _activeVehicle->getFactGroup(factGroupName);
    }

401 402 403 404 405 406 407
    _factValueNames.clear();
    _factValueNames = factGroup->factNames();
    for (QString& name: _factValueNames) {
        name[0] = name[0].toUpper();
    }

    QString nonEmptyFactName;
408
    if (factGroup) {
409 410 411 412 413 414
        if (factName.isEmpty()) {
            nonEmptyFactName = _factValueNames[0];
        } else {
            nonEmptyFactName = factName;
        }
        _fact = factGroup->getFact(nonEmptyFactName);
415 416 417 418
    }

    if (_fact) {
        _factGroupName = factGroupName;
419
        _factName =      nonEmptyFactName;
420
    } else {
421
        _factName.clear();
422 423 424
        _factGroupName.clear();
    }

425 426 427 428
    emit factChanged            (_fact);
    emit factNameChanged        (_factName);
    emit factGroupNameChanged   (_factGroupName);
    emit factValueNamesChanged  (_factValueNames);
429 430
}

431
void InstrumentValue::_setFontSize(FontSize fontSize)
432 433 434 435 436 437 438
{
    if (fontSize != _fontSize) {
        _fontSize = fontSize;
        emit fontSizeChanged(fontSize);
    }
}

439
void InstrumentValue::setFontSize(FontSize fontSize)
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
{
    _setFontSize(fontSize);

    // All other items in row must change to match
    for (int i=0; i<_rowModel->count(); i++) {
        InstrumentValue* instrumentValue = _rowModel->value<InstrumentValue*>(i);
        if (instrumentValue != this) {
            instrumentValue->_setFontSize(fontSize);
        }
    }
}

void InstrumentValue::saveToSettings(QSettings& settings) const
{
    if (_fact) {
        settings.setValue(_factGroupNameKey,    _factGroupName);
456
        settings.setValue(_factNameKey,         _factName);
457 458 459 460 461 462 463
    } else {
        settings.setValue(_factGroupNameKey,    "");
        settings.setValue(_factNameKey,         "");
    }
    settings.setValue(_labelKey,        _label);
    settings.setValue(_fontSizeKey,     _fontSize);
    settings.setValue(_showUnitsKey,    _showUnits);
464 465
    settings.setValue(_iconKey,         _icon);
    settings.setValue(_iconPositionKey, _iconPosition);
466 467 468 469 470 471
}

void InstrumentValue::readFromSettings(const QSettings& settings)
{
    _factGroupName =    settings.value(_factGroupNameKey).toString();
    _label =            settings.value(_labelKey).toString();
472 473 474 475
    _fontSize =         settings.value(_fontSizeKey, DefaultFontSize).value<FontSize>();
    _showUnits =        settings.value(_showUnitsKey, true).toBool();
    _icon =             settings.value(_iconKey).toString();
    _iconPosition =     settings.value(_iconPositionKey, IconLeft).value<IconPosition>();
476 477 478

    QString factName = settings.value(_factNameKey).toString();
    if (!factName.isEmpty()) {
479
        setFact(_factGroupName, factName);
480 481 482 483 484 485 486
    }

    emit factChanged            (_fact);
    emit factGroupNameChanged   (_factGroupName);
    emit labelChanged           (_label);
    emit fontSizeChanged        (_fontSize);
    emit showUnitsChanged       (_showUnits);
487 488
    emit iconChanged            (_icon);
    emit iconPositionChanged    (_iconPosition);
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
}

void InstrumentValue::setLabel(const QString& label)
{
    if (label != _label) {
        _label = label;
        emit labelChanged(label);
    }
}

void InstrumentValue::setShowUnits(bool showUnits)
{
    if (showUnits != _showUnits) {
        _showUnits = showUnits;
        emit showUnitsChanged(showUnits);
    }
}

void InstrumentValue::clearFact(void)
{
    _fact = nullptr;
    _factGroupName.clear();
    _label.clear();
DonLakeFlyer's avatar
DonLakeFlyer committed
512
    _icon.clear();
513
    _showUnits = true;
514

515 516 517
    emit factChanged            (_fact);
    emit factGroupNameChanged   (_factGroupName);
    emit labelChanged           (_label);
DonLakeFlyer's avatar
DonLakeFlyer committed
518
    emit iconChanged            (_icon);
519
    emit showUnitsChanged       (_showUnits);
520
}
521 522 523 524

void InstrumentValue::setIcon(const QString& icon)
{
    if (icon != _icon) {
DonLakeFlyer's avatar
DonLakeFlyer committed
525
        _icon = icon;
526 527 528 529 530 531 532 533 534 535 536
        emit iconChanged(_icon);
    }
}

void InstrumentValue::setIconPosition(IconPosition iconPosition)
{
    if (iconPosition != _iconPosition) {
        _iconPosition = iconPosition;
        emit iconPositionChanged(iconPosition);
    }
}