|
| 1 | +/* |
| 2 | + * Copyright 2023 Simon Vetter |
| 3 | + * |
| 4 | + * This file is part of PeakAscentLogger. |
| 5 | + * |
| 6 | + * PeakAscentLogger is free software: you can redistribute it and/or modify it under the terms |
| 7 | + * of the GNU General Public License as published by the Free Software Foundation, |
| 8 | + * either version 3 of the License, or (at your option) any later version. |
| 9 | + * |
| 10 | + * PeakAscentLogger is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 11 | + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | + * See the GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License along with PeakAscentLogger. |
| 15 | + * If not, see <https://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +/** |
| 19 | + * @file settings_table.cpp |
| 20 | + * |
| 21 | + * This file defines the SettingsTable class. |
| 22 | + */ |
| 23 | + |
| 24 | +#include "settings_table.h" |
| 25 | + |
| 26 | +#include "src/settings/project_settings.h" |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +/** |
| 31 | + * Creates a new SettingsTable. |
| 32 | + */ |
| 33 | +SettingsTable::SettingsTable() : |
| 34 | + Table("ProjectSettings", "Project settings", false), |
| 35 | + // name uiName type nullable primaryKey foreignKey table |
| 36 | + primaryKeyColumn (new Column("projectSettingID", QString(), ID, false, true, nullptr, this)), |
| 37 | + settingKeyColumn (new Column("settingKey", QString(), String, false, false, nullptr, this)), |
| 38 | + settingValueColumn (new Column("settingValue", QString(), String, false, false, nullptr, this)) |
| 39 | +{ |
| 40 | + addColumn(primaryKeyColumn); |
| 41 | + addColumn(settingKeyColumn); |
| 42 | + addColumn(settingValueColumn); |
| 43 | +} |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +/** |
| 48 | + * Indicates whether the given setting is present in the project settings table. |
| 49 | + * |
| 50 | + * @param setting The setting to check. |
| 51 | + * @param parent The parent window. Can be nullptr, in which case no cleanup is performed for duplicate settings. |
| 52 | + * @return True if the setting is present, false otherwise. |
| 53 | + */ |
| 54 | +bool SettingsTable::settingIsPresent(const GenericProjectSetting* setting, QWidget* parent) |
| 55 | +{ |
| 56 | + return findSettingID(setting, parent).isValid(); |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Returns the current value of the given setting in the project settings table. |
| 61 | + * |
| 62 | + * If the parent window is given, the setting is added to the project settings table using the |
| 63 | + * default value if it is not present. |
| 64 | + * |
| 65 | + * @param setting The setting to look up. |
| 66 | + * @param parent The parent window. Can be nullptr, in which case no cleanup is performed for duplicate settings. |
| 67 | + * @return The current value of the setting. |
| 68 | + */ |
| 69 | +QVariant SettingsTable::getSetting(const GenericProjectSetting* setting, QWidget* parent) |
| 70 | +{ |
| 71 | + ItemID id = findSettingID(setting); |
| 72 | + if (id.isInvalid()) { |
| 73 | + if (!parent) { |
| 74 | + return setting->defaultValue; |
| 75 | + } |
| 76 | + |
| 77 | + QList<ColumnDataPair> columnDataPairs = QList<ColumnDataPair>(); |
| 78 | + columnDataPairs.append({settingKeyColumn, setting->key}); |
| 79 | + columnDataPairs.append({settingValueColumn, setting->defaultValue}); |
| 80 | + BufferRowIndex newBufferIndex = addRow(parent, columnDataPairs); |
| 81 | + id = primaryKeyColumn->getValueAt(newBufferIndex); |
| 82 | + } |
| 83 | + |
| 84 | + return settingValueColumn->getValueFor(FORCE_VALID(id)); |
| 85 | +} |
| 86 | + |
| 87 | + |
| 88 | +/** |
| 89 | + * Adds or updates the the setting in the project settings table with the given value. |
| 90 | + * |
| 91 | + * @param parent The parent window. Cannot be nullptr. |
| 92 | + * @param setting The setting to add or update. |
| 93 | + * @param value The new value for the setting. |
| 94 | + */ |
| 95 | +void SettingsTable::setSetting(QWidget* parent, const GenericProjectSetting* setting, QVariant value) |
| 96 | +{ |
| 97 | + ItemID id = findSettingID(setting, parent); |
| 98 | + if (id.isValid()) { |
| 99 | + // Update setting |
| 100 | + updateCellInNormalTable(parent, FORCE_VALID(id), settingValueColumn, value); |
| 101 | + } else { |
| 102 | + // Add setting |
| 103 | + QList<ColumnDataPair> columnDataPairs = QList<ColumnDataPair>(); |
| 104 | + columnDataPairs.append({settingKeyColumn, setting->key}); |
| 105 | + columnDataPairs.append({settingValueColumn, setting->defaultValue}); |
| 106 | + BufferRowIndex newBufferIndex = addRow(parent, columnDataPairs); |
| 107 | + id = primaryKeyColumn->getValueAt(newBufferIndex); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * Removes the setting from the project settings table entirely. |
| 113 | + * |
| 114 | + * @param parent The parent window. Cannot be nullptr. |
| 115 | + * @param setting The setting to remove. |
| 116 | + */ |
| 117 | +void SettingsTable::removeSetting(QWidget* parent, const GenericProjectSetting* setting) |
| 118 | +{ |
| 119 | + ItemID id = findSettingID(setting, parent); |
| 120 | + if (id.isInvalid()) return; |
| 121 | + removeMatchingRows(parent, settingKeyColumn, FORCE_VALID(id)); |
| 122 | +} |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | +/** |
| 127 | + * Finds the projectSettingID (primary key) of the given setting in the database. |
| 128 | + * |
| 129 | + * @param setting The setting to look up. |
| 130 | + * @param parent The parent window. Can be nullptr, in which case no cleanup is performed for duplicate settings. |
| 131 | + * @return The projectSettingID of the setting, or an invalid ItemID if the setting is not present. |
| 132 | + */ |
| 133 | +ItemID SettingsTable::findSettingID(const GenericProjectSetting* setting, QWidget* parent) |
| 134 | +{ |
| 135 | + QList<BufferRowIndex> bufferRowIndices = getMatchingBufferRowIndices(settingKeyColumn, setting->key); |
| 136 | + |
| 137 | + if (bufferRowIndices.size() == 1) { |
| 138 | + return VALID_ITEM_ID(); |
| 139 | + } |
| 140 | + |
| 141 | + BufferRowIndex settingIndex = bufferRowIndices.last(); |
| 142 | + if (bufferRowIndices.size() > 1) { |
| 143 | + QString error = "WARNING: Found " + QString::number(bufferRowIndices.size()) + " entries for project setting " + setting->key + "."; |
| 144 | + if (parent) { |
| 145 | + error += " Cleaning up."; |
| 146 | + } |
| 147 | + qDebug().noquote() << error; |
| 148 | + if (parent) { |
| 149 | + for (const BufferRowIndex& rowIndex : bufferRowIndices) { |
| 150 | + if (rowIndex == settingIndex) continue; // Leave the last one in place |
| 151 | + ValidItemID id = VALID_ITEM_ID(primaryKeyColumn->getValueAt(rowIndex)); |
| 152 | + removeMatchingRows(parent, settingKeyColumn, id); |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + return VALID_ITEM_ID(primaryKeyColumn->getValueAt(settingIndex)); |
| 158 | +} |
0 commit comments