ValuesWidgetController.cc 13.4 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 30 31 32
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";

ValuesWidgetController::ValuesWidgetController(bool forDefaultSettingsCreation)
    : _valuesModel(new QmlObjectListModel(this))
33 34 35 36 37
{
    QSettings settings;

    settings.beginGroup(_groupKey);

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
    _multiVehicleMgr = qgcApp()->toolbox()->multiVehicleManager();
    connect(_multiVehicleMgr, &MultiVehicleManager::activeVehicleChanged, this, &ValuesWidgetController::_activeVehicleChanged);

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

InstrumentValue* ValuesWidgetController::_createNewInstrumentValueWorker(Vehicle* activeVehicle, int fontSize, QmlObjectListModel* rowModel)
{
    InstrumentValue* newValue = new InstrumentValue(activeVehicle, fontSize, rowModel);

    connect(newValue, &InstrumentValue::factChanged,            this, &ValuesWidgetController::_saveSettings);
    connect(newValue, &InstrumentValue::factGroupNameChanged,   this, &ValuesWidgetController::_saveSettings);
    connect(newValue, &InstrumentValue::labelChanged,           this, &ValuesWidgetController::_saveSettings);
    connect(newValue, &InstrumentValue::fontSizeChanged,        this, &ValuesWidgetController::_saveSettings);
    connect(newValue, &InstrumentValue::showUnitsChanged,       this, &ValuesWidgetController::_saveSettings);

    return newValue;
}
58

Don Gagne's avatar
Don Gagne committed
59

60 61 62
InstrumentValue* ValuesWidgetController::appendColumn(int rowIndex)
{
    InstrumentValue* newValue = nullptr;
63

64 65 66 67 68 69 70 71
    if (rowIndex >= 0 && rowIndex < _valuesModel->count()) {
        QmlObjectListModel* row = _valuesModel->value<QmlObjectListModel*>(rowIndex);
        int fontSize = InstrumentValue::DefaultFontSize;
        if (row->count()) {
            fontSize = row->value<InstrumentValue*>(0)->fontSize();
        }
        newValue  = _createNewInstrumentValueWorker(_currentActiveVehicle(), fontSize, row);
        row->append(newValue);
Don Gagne's avatar
Don Gagne committed
72
    }
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    _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
90
    }
91
    _saveSettings();
92 93
}

94
QmlObjectListModel* ValuesWidgetController::appendRow(bool addBlanksColumn)
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
    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;
    }

178 179 180
    QSettings settings;

    settings.beginGroup(_groupKey);
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 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
    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();
        QStringList altitudeProperties = { "altitudeRelative" , "altitudeAMSL" };

        int rowIndex = -1;
        int valueCount = 0;
        QmlObjectListModel* rowModel = nullptr;
        for (const QString& largeValue: largeValues) {
            QStringList parts = largeValue.split(".");

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

            InstrumentValue* colValue = appendColumn(rowIndex);
            colValue->setFact(parts[0], parts[1], QString());
            colValue->setLabel(colValue->fact()->shortDescription());
            colValue->setShowUnits(true);
            colValue->setFontSize(altitudeProperties.contains(parts[1]) ? InstrumentValue::LargeFontSize : InstrumentValue::DefaultFontSize);
        }
232

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
        valueCount = 0;
        rowModel = nullptr;
        for (const QString& smallValue: smallValues) {
            QStringList parts = smallValue.split(".");

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

            InstrumentValue* colValue = appendColumn(rowIndex);
            colValue->setFact(parts[0], parts[1], QString());
            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);
    }
283 284
}

285
void ValuesWidgetController::resetToDefaults(void)
286 287 288 289
{
    QSettings settings;

    settings.beginGroup(_groupKey);
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
    settings.remove("");

    _loadSettings();
}

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

InstrumentValue::InstrumentValue(Vehicle* activeVehicle, int fontSize, QmlObjectListModel* rowModel)
    : QObject       (rowModel)
    , _activeVehicle(activeVehicle)
    , _rowModel     (rowModel)
    , _fontSize     (fontSize)
{

}

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

    if (_fact) {
        _fact = nullptr;

        FactGroup* factGroup = nullptr;
        QString factName;
        if (_factGroupName == QStringLiteral("Vehicle")) {
            factGroup = _activeVehicle;
        } else {
            factGroup = _activeVehicle->getFactGroup(_factGroupName);
        }

        if (factGroup) {
            _fact = factGroup->getFact(factName);
        }
        emit factChanged(_fact);
    }
}

void InstrumentValue::setFact(QString factGroupName, QString factName, QString label)
{
    if (_fact) {
        _fact = nullptr;
    }

    FactGroup* factGroup = nullptr;
    if (factGroupName == QStringLiteral("Vehicle")) {
        factGroup = _activeVehicle;
    } else {
        factGroup = _activeVehicle->getFactGroup(factGroupName);
    }

    if (factGroup) {
        _fact = factGroup->getFact(factName);
    }

    if (_fact) {
        _factGroupName = factGroupName;
        _label = label;
    } else {
        _factGroupName.clear();
        _label.clear();
    }

    emit labelChanged(_label);
    emit factChanged(_fact);
    emit factGroupNameChanged(_factGroupName);
}

void InstrumentValue::_setFontSize(int fontSize)
{
    if (fontSize != _fontSize) {
        _fontSize = fontSize;
        emit fontSizeChanged(fontSize);
    }
}

void InstrumentValue::setFontSize(int fontSize)
{
    _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);
        settings.setValue(_factNameKey,         _fact->name());
    } else {
        settings.setValue(_factGroupNameKey,    "");
        settings.setValue(_factNameKey,         "");
    }
    settings.setValue(_labelKey,        _label);
    settings.setValue(_fontSizeKey,     _fontSize);
    settings.setValue(_showUnitsKey,    _showUnits);
}

void InstrumentValue::readFromSettings(const QSettings& settings)
{
    _factGroupName =    settings.value(_factGroupNameKey).toString();
    _label =            settings.value(_labelKey).toString();
    _fontSize =         settings.value(_fontSizeKey).toInt();
    _showUnits =        settings.value(_showUnitsKey).toBool();

    QString factName = settings.value(_factNameKey).toString();
    if (!factName.isEmpty()) {
        setFact(_factGroupName, factName, _label);
    }

    emit factChanged            (_fact);
    emit factGroupNameChanged   (_factGroupName);
    emit labelChanged           (_label);
    emit fontSizeChanged        (_fontSize);
    emit showUnitsChanged       (_showUnits);
}

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();
    _showUnits = true;
437

438 439 440 441
    emit factChanged            (_fact);
    emit factGroupNameChanged   (_factGroupName);
    emit labelChanged           (_label);
    emit showUnitsChanged       (_showUnits);
442
}