ValuesWidgetController.cc 11.2 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
#include "ValuesWidgetController.h"
11 12
#include "QGCApplication.h"
#include "QGCCorePlugin.h"
13 14 15

#include <QSettings>

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

20 21 22 23 24 25
const char* ValuesWidgetController::_deprecatedGroupKey =         "ValuesWidget";
const char* ValuesWidgetController::_deprecatedLargeValuesKey =   "large";
const char* ValuesWidgetController::_deprecatedSmallValuesKey =   "small";

ValuesWidgetController::ValuesWidgetController(bool forDefaultSettingsCreation)
    : _valuesModel(new QmlObjectListModel(this))
26 27 28 29 30
{
    QSettings settings;

    settings.beginGroup(_groupKey);

31 32 33 34 35 36 37 38
    _multiVehicleMgr = qgcApp()->toolbox()->multiVehicleManager();
    connect(_multiVehicleMgr, &MultiVehicleManager::activeVehicleChanged, this, &ValuesWidgetController::_activeVehicleChanged);

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

39
void ValuesWidgetController::_connectSignalsToController(InstrumentValue* value, ValuesWidgetController* controller)
40
{
41 42
    connect(value, &InstrumentValue::factNameChanged,       controller, &ValuesWidgetController::_saveSettings);
    connect(value, &InstrumentValue::factGroupNameChanged,  controller, &ValuesWidgetController::_saveSettings);
43
    connect(value, &InstrumentValue::textChanged,           controller, &ValuesWidgetController::_saveSettings);
44 45 46
    connect(value, &InstrumentValue::fontSizeChanged,       controller, &ValuesWidgetController::_saveSettings);
    connect(value, &InstrumentValue::showUnitsChanged,      controller, &ValuesWidgetController::_saveSettings);
    connect(value, &InstrumentValue::iconChanged,           controller, &ValuesWidgetController::_saveSettings);
47
    connect(value, &InstrumentValue::labelPositionChanged,   controller, &ValuesWidgetController::_saveSettings);
48 49 50 51 52
    connect(value, &InstrumentValue::rangeTypeChanged,      controller, &ValuesWidgetController::_saveSettings);
    connect(value, &InstrumentValue::rangeValuesChanged,    controller, &ValuesWidgetController::_saveSettings);
    connect(value, &InstrumentValue::rangeColorsChanged,    controller, &ValuesWidgetController::_saveSettings);
    connect(value, &InstrumentValue::rangeOpacitiesChanged, controller, &ValuesWidgetController::_saveSettings);
    connect(value, &InstrumentValue::rangeIconsChanged,     controller, &ValuesWidgetController::_saveSettings);
53
}
54

55 56 57 58
InstrumentValue* ValuesWidgetController::_createNewInstrumentValueWorker(Vehicle* activeVehicle, InstrumentValue::FontSize fontSize, QmlObjectListModel* rowModel)
{
    InstrumentValue* newValue = new InstrumentValue(activeVehicle, fontSize, rowModel);
    _connectSignalsToController(newValue, this);
59 60
    return newValue;
}
61

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

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

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

180 181 182
    QSettings settings;

    settings.beginGroup(_groupKey);
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
    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();
217
        QStringList altitudeProperties = { "AltitudeRelative" , "AltitudeAMSL" };
218 219 220 221 222

        int rowIndex = -1;
        int valueCount = 0;
        QmlObjectListModel* rowModel = nullptr;
        for (const QString& largeValue: largeValues) {
223 224 225
            QStringList parts =     largeValue.split(".");
            QString factGroupName = _pascalCase(parts[0]);
            QString factName =      _pascalCase(parts[1]);
226 227 228 229 230

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

            InstrumentValue* colValue = appendColumn(rowIndex);
231
            colValue->setFact(factGroupName, factName);
232
            colValue->setText(colValue->fact()->shortDescription());
233
            colValue->setShowUnits(true);
234
            colValue->setFontSize(altitudeProperties.contains(factName) ? InstrumentValue::LargeFontSize : InstrumentValue::DefaultFontSize);
235
        }
236

237 238 239
        valueCount = 0;
        rowModel = nullptr;
        for (const QString& smallValue: smallValues) {
240 241 242
            QStringList parts =     smallValue.split(".");
            QString factGroupName = _pascalCase(parts[0]);
            QString factName =      _pascalCase(parts[1]);
243 244 245 246 247 248 249

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

            InstrumentValue* colValue = appendColumn(rowIndex);
250
            colValue->setFact(factGroupName, factName);
251
            colValue->setText(colValue->fact()->shortDescription());
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 283 284 285 286 287 288
            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);
    }
289 290
}

291
void ValuesWidgetController::resetToDefaults(void)
292 293 294 295
{
    QSettings settings;

    settings.beginGroup(_groupKey);
296 297 298 299 300 301 302 303 304 305
    settings.remove("");

    _loadSettings();
}

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

306 307 308 309 310 311 312 313 314 315 316 317 318
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);
        }
    }
}

319 320 321 322
QString ValuesWidgetController::_pascalCase(const QString& text)
{
    return text[0].toUpper() + text.right(text.length() - 1);
}