FOSSredder 0.5.0
Loading...
Searching...
No Matches
SettingsViewModel.cpp
Go to the documentation of this file.
1
5
7
10#include "ui/shell/AppActions.h"
11#include "ui/shell/Defaults.h"
12#include "ui/shell/Navigation.h"
13#include "ui/shell/Settings.h"
14
15namespace ui {
16
17namespace {
18
19constexpr int kGeneralCategory = 0;
20constexpr int kImportCategory = 1;
21constexpr int kExportCategory = 2;
22constexpr int kMiscellaneousCategory = 3;
23
24QVariantMap categoryRow(int value, const QString& text, bool selected) {
25 return {{QStringLiteral("category"), value},
26 {QStringLiteral("text"), text},
27 {QStringLiteral("selected"), selected}};
28}
29
30QString languageCode(const QVariant& value) {
31 return value.toMap().value(QStringLiteral("code")).toString();
32}
33
34bool languageAvailable(const QVariant& value) {
35 const QVariantMap map = value.toMap();
36 return !map.contains(QStringLiteral("available")) ||
37 map.value(QStringLiteral("available")).toBool();
38}
39
40QVariantMap themeModeOption(const QString& code, const QString& label) {
41 return {{QStringLiteral("code"), code}, {QStringLiteral("label"), label}};
42}
43
44QString themeModeCode(const QVariant& value) {
45 return value.toMap().value(QStringLiteral("code")).toString();
46}
47
48int autosaveIntervalMinutes(const QVariant& value) {
49 return value.toMap().value(QStringLiteral("minutes")).toInt();
50}
51
52QVariantMap autosaveIntervalOption(int minutes, const QString& label) {
53 return {{QStringLiteral("minutes"), minutes},
54 {QStringLiteral("label"), label}};
55}
56
57} // namespace
58
59SettingsViewModel::SettingsViewModel(QObject* parent) : QObject(parent) {}
60
62 if (navigation_ == value) {
63 return;
64 }
65 bindNavigation(value);
66 emitChanged();
67}
68
70 if (settings_ == value) {
71 return;
72 }
73 bindSettings(value);
74 emitChanged();
75}
76
78 if (actions_ == value) {
79 return;
80 }
81 bindActions(value);
82 emitChanged();
83}
84
86 if (languageService_ == value) {
87 return;
88 }
89 if (languageService_) {
90 disconnect(languageService_, nullptr, this, nullptr);
91 }
92 languageService_ = value;
93 if (languageService_) {
94 connect(languageService_, &LanguageService::currentLanguageChanged, this,
95 &SettingsViewModel::emitChanged);
96 connect(languageService_, &LanguageService::availableLanguagesChanged, this,
97 &SettingsViewModel::emitChanged);
98 }
99 emitChanged();
100}
101
102void SettingsViewModel::bindNavigation(Navigation* value) {
103 if (navigation_) {
104 disconnect(navigation_, nullptr, this, nullptr);
105 }
106 navigation_ = value;
107 if (navigation_) {
108 connect(navigation_, &Navigation::settingsCategoryChanged, this,
109 &SettingsViewModel::emitChanged);
110 }
111}
112
113void SettingsViewModel::bindSettings(Settings* value) {
114 if (settings_) {
115 disconnect(settings_, nullptr, this, nullptr);
116 }
117 settings_ = value;
118 if (settings_) {
119 connect(settings_, &Settings::stateChanged, this,
120 &SettingsViewModel::emitChanged);
121 connect(settings_, &Settings::languageChanged, this,
122 &SettingsViewModel::emitChanged);
123 connect(settings_, &Settings::themeModeChanged, this,
124 &SettingsViewModel::emitChanged);
125 }
126}
127
128void SettingsViewModel::bindActions(Actions* value) {
129 if (actions_) {
130 disconnect(actions_, nullptr, this, nullptr);
131 }
132 actions_ = value;
133 if (!actions_) {
134 return;
135 }
136 connect(actions_, &Actions::importFileSelected, this,
137 [this](const QString& path) {
138 if (!path.isEmpty()) {
140 }
141 });
142 connect(actions_, &Actions::importFilesSelected, this,
143 [this](const QStringList& paths) {
144 if (!paths.isEmpty() && !paths.first().isEmpty()) {
145 setImportDefaultPath(paths.first());
146 }
147 });
148 connect(actions_, &Actions::exportDirectorySelected, this,
149 [this](const QString& path) {
150 if (!path.isEmpty()) {
152 }
153 });
154}
155
156void SettingsViewModel::emitChanged() {
157 emit changed();
158}
159
161 return navigation_ ? navigation_->settingsCategoryValue() : firstCategory();
162}
163
165 const int selected = currentCategory();
166 return {
167 categoryRow(kGeneralCategory, tr("General"),
168 selected == kGeneralCategory),
169 categoryRow(kImportCategory, tr("Import"), selected == kImportCategory),
170 categoryRow(kExportCategory, tr("Export"), selected == kExportCategory),
171 categoryRow(kMiscellaneousCategory, tr("Miscellaneous"),
172 selected == kMiscellaneousCategory)};
173}
174
176 return lastCategory() > firstCategory();
177}
178
180 if (settings_) {
181 settings_->load();
182 }
183 if (navigation_) {
184 navigation_->setSettingsCategoryValue(currentCategory());
185 }
186 emitChanged();
187}
188
190 const int first = firstCategory();
191 const int last = lastCategory();
192 const int total = last - first + 1;
193 if (!navigation_ || total <= 0) {
194 return;
195 }
196 const int offset = currentCategory() - first;
197 const int next = first + ((offset + delta + total) % total);
198 selectCategory(next);
199}
200
202 if (navigation_) {
203 navigation_->setSettingsCategoryValue(category);
204 }
205}
206
208 selectCategory(kGeneralCategory);
209 if (settings_) {
210 settings_->resetToDefaults();
211 }
212 emitChanged();
213}
214
215int SettingsViewModel::firstCategory() const noexcept {
216 return kGeneralCategory;
217}
218
219int SettingsViewModel::lastCategory() const noexcept {
220 return kMiscellaneousCategory;
221}
222
224 return languageService_ ? languageService_->availableLanguages()
225 : QVariantList();
226}
227
229 const QString selectedLanguage = language();
230 const QVariantList options = languageOptions();
231 for (int i = 0; i < options.size(); ++i) {
232 if (languageCode(options.at(i)) == selectedLanguage) {
233 return i;
234 }
235 }
236 return -1;
237}
238
240 return {themeModeOption(QStringLiteral("light"), tr("Light")),
241 themeModeOption(QStringLiteral("dark"), tr("Dark"))};
242}
243
245 const QString selectedThemeMode = themeMode();
246 const QVariantList options = themeModeOptions();
247 for (int i = 0; i < options.size(); ++i) {
248 if (themeModeCode(options.at(i)) == selectedThemeMode) {
249 return i;
250 }
251 }
252 return 0;
253}
254
256 if (settings_) {
257 return settings_->language();
258 }
259 return languageService_ ? languageService_->currentLanguage() : QString();
260}
261
262void SettingsViewModel::setLanguage(const QString& value) {
263 if (settings_) {
264 settings_->setLanguage(value);
265 }
266 if (languageService_) {
267 languageService_->applyLanguage(value);
268 }
269}
270
272 return settings_ ? settings_->themeMode() : QStringLiteral("light");
273}
274
275void SettingsViewModel::setThemeMode(const QString& value) {
276 if (settings_) {
277 settings_->setThemeMode(value);
278 }
279}
280
282 const QVariantList options = languageOptions();
283 if (index < 0 || index >= options.size() ||
284 !languageAvailable(options.at(index))) {
285 emitChanged();
286 return;
287 }
288 setLanguage(languageCode(options.at(index)));
289}
290
292 const QVariantList options = themeModeOptions();
293 if (index < 0 || index >= options.size()) {
294 emitChanged();
295 return;
296 }
297 setThemeMode(themeModeCode(options.at(index)));
298}
299
301 return !settings_ || settings_->autosaveOnClose();
302}
303
305 if (settings_) {
306 settings_->setAutosaveOnClose(value);
307 }
308}
309
311 return {autosaveIntervalOption(config::autosave::kIntervalOff, tr("Off")),
312 autosaveIntervalOption(config::autosave::kInterval1Minute,
313 tr("Every 1 minute")),
314 autosaveIntervalOption(config::autosave::kInterval5Minutes,
315 tr("Every 5 minutes")),
316 autosaveIntervalOption(config::autosave::kInterval10Minutes,
317 tr("Every 10 minutes")),
318 autosaveIntervalOption(config::autosave::kInterval15Minutes,
319 tr("Every 15 minutes")),
320 autosaveIntervalOption(config::autosave::kInterval30Minutes,
321 tr("Every 30 minutes"))};
322}
323
325 const int selectedMinutes = settings_ ? settings_->autosaveIntervalMinutes()
327 const QVariantList options = autosaveIntervalOptions();
328 for (int i = 0; i < options.size(); ++i) {
329 if (autosaveIntervalMinutes(options.at(i)) == selectedMinutes) {
330 return i;
331 }
332 }
333 return 0;
334}
335
337 const QVariantList options = autosaveIntervalOptions();
338 if (!settings_ || index < 0 || index >= options.size()) {
339 emitChanged();
340 return;
341 }
342 settings_->setAutosaveIntervalMinutes(
343 autosaveIntervalMinutes(options.at(index)));
344}
345
347 return settings_ ? settings_->importDefaultPath() : QString();
348}
349
350void SettingsViewModel::setImportDefaultPath(const QString& value) {
351 if (settings_) {
352 settings_->setImportDefaultPath(value);
353 }
354}
355
357 return settings_ ? settings_->importPoppler() : QString();
358}
359
360void SettingsViewModel::setImportPoppler(const QString& value) {
361 if (settings_) {
362 settings_->setImportPoppler(value);
363 }
364}
365
367 return settings_ ? settings_->importOpenCv() : QString();
368}
369
370void SettingsViewModel::setImportOpenCv(const QString& value) {
371 if (settings_) {
372 settings_->setImportOpenCv(value);
373 }
374}
375
377 return settings_ ? settings_->importTesseract() : QString();
378}
379
380void SettingsViewModel::setImportTesseract(const QString& value) {
381 if (settings_) {
382 settings_->setImportTesseract(value);
383 }
384}
385
387 return settings_ ? settings_->importParser() : QString();
388}
389
390void SettingsViewModel::setImportParser(const QString& value) {
391 if (settings_) {
392 settings_->setImportParser(value);
393 }
394}
395
397 return settings_ ? settings_->importMatcher() : QString();
398}
399
400void SettingsViewModel::setImportMatcher(const QString& value) {
401 if (settings_) {
402 settings_->setImportMatcher(value);
403 }
404}
405
407 return settings_ ? settings_->exportDefaultDirectory() : QString();
408}
409
411 if (settings_) {
412 settings_->setExportDefaultDirectory(value);
413 }
414}
415
417 return settings_ ? settings_->exportArchiveFormat() : 0;
418}
419
421 if (settings_) {
422 settings_->setExportArchiveFormat(value);
423 }
424}
425
427 return !settings_ || settings_->exportIncludeFormulas();
428}
429
431 if (settings_) {
432 settings_->setExportIncludeFormulas(value);
433 }
434}
435
437 return !settings_ || settings_->toolbarShowBooking();
438}
439
441 if (settings_) {
442 settings_->setToolbarShowBooking(value);
443 }
444}
445
447 return !settings_ || settings_->toolbarShowImport();
448}
449
451 if (settings_) {
452 settings_->setToolbarShowImport(value);
453 }
454}
455
457 return !settings_ || settings_->toolbarShowActors();
458}
459
461 if (settings_) {
462 settings_->setToolbarShowActors(value);
463 }
464}
465
467 return !settings_ || settings_->toolbarShowExport();
468}
469
471 if (settings_) {
472 settings_->setToolbarShowExport(value);
473 }
474}
475
477 return !settings_ || settings_->toolbarShowProperties();
478}
479
481 if (settings_) {
482 settings_->setToolbarShowProperties(value);
483 }
484}
485
487 return !settings_ || settings_->toolbarShowAnalysis();
488}
489
491 if (settings_) {
492 settings_->setToolbarShowAnalysis(value);
493 }
494}
495
497 return !settings_ || settings_->toolbarShowContracts();
498}
499
501 if (settings_) {
502 settings_->setToolbarShowContracts(value);
503 }
504}
505
507 return !settings_ || settings_->toolbarShowAnnual();
508}
509
511 if (settings_) {
512 settings_->setToolbarShowAnnual(value);
513 }
514}
515
517 return !settings_ || settings_->toolbarShowSettings();
518}
519
521 if (settings_) {
522 settings_->setToolbarShowSettings(value);
523 }
524}
525
527 observability::traceViewModel("SettingsViewModel::saveSettings",
528 "Settings save submitted");
529 if (settings_) {
530 settings_->save();
531 }
532 if (languageService_ && settings_) {
533 languageService_->applyLanguage(settings_->language());
534 }
535 emitChanged();
536}
537
539 if (actions_) {
540 actions_->browseImportPdf();
541 }
542}
543
545 if (actions_) {
546 actions_->browseExportDirectory();
547 }
548}
549
550} // namespace ui
Declares QAction wrappers exposed to the UI layer and QML.
Defines UI-specific runtime constants that are not shared with core.
Declares the UI service responsible for runtime language switching.
Declarations for the UI Navigation component.
Declares the centralized UI settings view model exposed to QML.
Declares the Settings page view model.
Declarations for the UI Trace component.
Owns the main window actions and exposes them to QML.
Definition AppActions.h:18
void importFileSelected(const QString &path)
Emitted after a single import file was selected by the user.
void exportDirectorySelected(const QString &path)
Emitted after an export directory was chosen by the user.
void importFilesSelected(const QStringList &paths)
Emitted after multiple import files were selected by the user.
Manages the active UI language and the available translation options.
void availableLanguagesChanged()
Emitted when the available language list changes.
void currentLanguageChanged()
Emitted when the active language changes.
Stores the active top-level shell section and settings sub-section.
Definition Navigation.h:15
void settingsCategoryChanged()
Emitted when the active settings category changes.
Stores and exposes globally persisted UI settings for settings-related views.
Definition Settings.h:19
void stateChanged()
Emitted when any settings value changes.
void languageChanged()
void themeModeChanged()
Q_INVOKABLE void selectAutosaveIntervalAt(int index)
Selects an autosave interval option by index.
void setToolbarShowActors(bool value)
Updates whether the actor toolbar entry is visible.
void setToolbarShowAnnual(bool value)
Updates whether the annual toolbar entry is visible.
void setAutosaveOnClose(bool value)
Updates whether autosave on close is enabled.
void setLanguageService(LanguageService *value)
Sets the language service used to build language options.
void setImportDefaultPath(const QString &value)
Updates the default import path.
void setToolbarShowProperties(bool value)
Updates whether the property toolbar entry is visible.
void setNavigation(Navigation *value)
Sets the shell navigation object used by the settings page.
Q_INVOKABLE void saveSettings()
Persists the current settings values.
void setExportDefaultDirectory(const QString &value)
Updates the default export directory.
void setToolbarShowSettings(bool value)
Updates whether the settings toolbar entry is visible.
void setLanguage(const QString &value)
Updates the configured language code.
void setToolbarShowImport(bool value)
Updates whether the import toolbar entry is visible.
void setThemeMode(const QString &value)
Updates the configured theme mode.
SettingsViewModel(QObject *parent=nullptr)
Creates the settings view model.
void changed()
Emitted when settings view model state changed.
void setImportParser(const QString &value)
Updates the configured import parser key.
Q_INVOKABLE void selectCategory(int category)
Selects a settings category.
void setToolbarShowExport(bool value)
Updates whether the export toolbar entry is visible.
void setExportIncludeFormulas(bool value)
Updates whether exported spreadsheets include formulas.
void setToolbarShowContracts(bool value)
Updates whether the contract toolbar entry is visible.
void setImportTesseract(const QString &value)
Updates the configured Tesseract path.
QVariantList autosaveIntervalOptions
Q_INVOKABLE void selectLanguageAt(int index)
Selects a language option by index.
void setImportMatcher(const QString &value)
Updates the configured import matcher key.
void setToolbarShowAnalysis(bool value)
Updates whether the analysis toolbar entry is visible.
Q_INVOKABLE void browseExportDirectory()
Opens a browser for the export default directory.
Q_INVOKABLE void selectThemeModeAt(int index)
Selects a theme mode option by index.
Q_INVOKABLE void browseImportPath()
Opens a browser for the import default path.
void setImportOpenCv(const QString &value)
Updates the configured OpenCV path.
Q_INVOKABLE void resetSettings()
Resets settings values to defaults.
void setImportPoppler(const QString &value)
Updates the configured Poppler path.
void setExportArchiveFormat(int value)
Updates the configured export archive format index.
void setToolbarShowBooking(bool value)
Updates whether the booking toolbar entry is visible.
void setSettings(Settings *value)
Sets the settings state object used for persisted settings values.
Q_INVOKABLE void activate()
Activates the settings page state.
Q_INVOKABLE void navigateCategory(int delta)
Navigates settings categories by a relative delta.
void setActions(Actions *value)
Sets shell actions used for file browsing.
constexpr int kInterval5Minutes
Definition Defaults.h:129
constexpr int kIntervalOff
Allowed periodic autosave intervals in minutes; 0 means disabled.
Definition Defaults.h:127
constexpr int kInterval15Minutes
Definition Defaults.h:131
constexpr int kInterval1Minute
Definition Defaults.h:128
constexpr int kInterval30Minutes
Definition Defaults.h:132
constexpr int kInterval10Minutes
Definition Defaults.h:130
void traceViewModel(const char *origin, std::string message, core::errors::ErrorContext context={})
Emits a view model trace event.
Definition Trace.h:97