swift
settingsmodelcomponent.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2016 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #include <QValidator>
7 
8 #include "ui_settingsmodelcomponent.h"
9 
11 #include "gui/guiapplication.h"
12 #include "misc/logmessage.h"
13 
14 using namespace swift::misc;
15 using namespace swift::misc::simulation::settings;
16 using namespace swift::gui::settings;
17 using namespace swift::core::db;
18 
19 namespace swift::gui::components
20 {
21  CSettingsModelComponent::CSettingsModelComponent(QWidget *parent)
22  : QFrame(parent), ui(new Ui::CSettingsModelComponent)
23  {
24  ui->setupUi(this);
25  ui->le_ConsolidateSecs->setValidator(
26  new QIntValidator(0, TBackgroundConsolidation::maxSecs(), ui->le_ConsolidateSecs));
27 
28  this->cacheChanged();
29  const QString lbl("Consolidate (%1-%2s):");
30  ui->lbl_Consolidate->setText(
31  lbl.arg(TBackgroundConsolidation::minSecs()).arg(TBackgroundConsolidation::maxSecs()));
32 
33  connect(ui->le_ConsolidateSecs, &QLineEdit::returnPressed, this,
34  &CSettingsModelComponent::consolidationEntered);
35  connect(ui->cb_AllowExcludeModels, &QCheckBox::toggled, this,
36  &CSettingsModelComponent::allowExcludedModelsChanged);
37 
38  // start updater if not yet done
39  QPointer<CSettingsModelComponent> myself(this);
40  QTimer::singleShot(2500, this, [=] {
41  if (!myself) { return; }
42  this->consolidationEntered();
43  });
44  }
45 
47 
49  {
50  const QString v = ui->le_ConsolidateSecs->text().trimmed();
51  if (v.isEmpty()) { return -1; }
52  bool ok = false;
53  const int secs = v.toInt(&ok);
54  return ok ? secs : -1;
55  }
56 
57  void CSettingsModelComponent::setBackgroundUpdater(const CBackgroundDataUpdater *updater) { m_updater = updater; }
58 
59  void CSettingsModelComponent::consolidationEntered()
60  {
61  int v = this->getBackgroundUpdaterIntervallSecs();
62  if (v < TBackgroundConsolidation::minSecs()) { v = -1; }
63 
64  const CStatusMessage m = m_consolidationSetting.setAndSave(v);
65  CLogMessage::preformatted(m);
66  this->cacheChanged();
67  }
68 
69  void CSettingsModelComponent::allowExcludedModelsChanged(bool allow)
70  {
71  CModelSettings ms = m_modelSettings.get();
72  if (ms.getAllowExcludedModels() == allow) { return; }
73  ms.setAllowExcludedModels(allow);
74  const CStatusMessage msg = m_modelSettings.setAndSave(ms);
75  CLogMessage::preformatted(msg);
76  }
77 
78  void CSettingsModelComponent::cacheChanged()
79  {
80  const int v = m_consolidationSetting.get();
81  const bool on = v > 0;
82  const QString s = on ? QString::number(v) : "";
83  ui->le_ConsolidateSecs->setText(s);
84 
85  const bool updater = on && sApp && !sApp->isShuttingDown() && m_updater && m_updater->isEnabled();
86  ui->comp_Led->setOn(updater);
87 
88  // avoid unnecessary roundtrips
89  const CModelSettings ms = m_modelSettings.get();
90  if (ui->cb_AllowExcludeModels->isChecked() != ms.getAllowExcludedModels())
91  {
92  ui->cb_AllowExcludeModels->setChecked(ms.getAllowExcludedModels());
93  }
94  }
95 } // namespace swift::gui::components
SWIFT_CORE_EXPORT swift::core::CApplication * sApp
Single instance of application object.
Definition: application.cpp:71
bool isShuttingDown() const
Is application shutting down?
Update and consolidation of DB data.
Settings UI for model matching/mapping.
void setBackgroundUpdater(const swift::core::db::CBackgroundDataUpdater *updater)
Updater (the updater this setting is for)
int getBackgroundUpdaterIntervallSecs() const
Interval, -1 for disabled.
CStatusMessage setAndSave(const T &value, qint64 timestamp=0)
Write and save in the same step. Must be called from the thread in which the owner lives.
Definition: valuecache.h:417
T get() const
Get a copy of the current value.
Definition: valuecache.h:408
bool isEnabled() const
Enabled (running)?
Definition: worker.h:300
Streamable status message, e.g.
void setAllowExcludedModels(bool allow)
Allow excluded models?
Definition: modelsettings.h:35
bool getAllowExcludedModels() const
Allow excluded models?
Definition: modelsettings.h:32
Classes interacting with the swift database (aka "datastore").
High level reusable GUI components.
Definition: aboutdialog.cpp:13
Free functions in swift::misc.
auto singleShot(int msec, QObject *target, F &&task)
Starts a single-shot timer which will call a task in the thread of the given object when it times out...
Definition: threadutils.h:30