swift
modelmatcherlogenable.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2018 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #include <QPointer>
7 #include <QTimer>
8 
9 #include "ui_modelmatcherlogenable.h"
10 
13 #include "gui/guiapplication.h"
14 
15 using namespace swift::core;
16 using namespace swift::core::context;
17 using namespace swift::misc::network;
18 using namespace swift::misc::simulation;
19 
20 namespace swift::gui::components
21 {
22  CModelMatcherLogEnable::CModelMatcherLogEnable(QWidget *parent) : QFrame(parent), ui(new Ui::CModelMatcherLogEnable)
23  {
24  ui->setupUi(this);
25  connect(ui->cb_LogReverseLookup, &QCheckBox::toggled, this, &CModelMatcherLogEnable::enabledCheckboxChanged,
26  Qt::QueuedConnection);
27  connect(ui->cb_LogMatchingMessages, &QCheckBox::toggled, this, &CModelMatcherLogEnable::enabledCheckboxChanged,
28  Qt::QueuedConnection);
29  connect(ui->cb_LogDetailed, &QCheckBox::toggled, this, &CModelMatcherLogEnable::enabledCheckboxChanged,
30  Qt::QueuedConnection);
31 
32  if (this->hasContexts())
33  {
34  connect(sGui->getIContextSimulator(), &IContextSimulator::changedLogOrDebugSettings, this,
35  &CModelMatcherLogEnable::valuesChanged, Qt::QueuedConnection);
36  connect(sGui->getIContextNetwork(), &IContextNetwork::changedLogOrDebugSettings, this,
37  &CModelMatcherLogEnable::valuesChanged, Qt::QueuedConnection);
38  connect(sGui->getIContextNetwork(), &IContextNetwork::connectionStatusChanged, this,
39  &CModelMatcherLogEnable::connectionStatusChanged, Qt::QueuedConnection);
40  }
41 
42  QPointer<CModelMatcherLogEnable> myself(this);
43  QTimer::singleShot(5000, this, [=] {
44  if (!myself) { return; }
45  this->initGui();
46  });
47  }
48 
50 
51  bool CModelMatcherLogEnable::isReverseLookupLogEnabled() const { return ui->cb_LogReverseLookup->isChecked(); }
52 
53  bool CModelMatcherLogEnable::isMatchingLogEnabled() const { return ui->cb_LogMatchingMessages->isChecked(); }
54 
55  void CModelMatcherLogEnable::enabledCheckboxChanged(bool enabled)
56  {
57  if (!this->hasContexts()) { return; }
58  const QObject *sender = QObject::sender();
59 
60  const bool detailed = (sender == ui->cb_LogDetailed) ? enabled : ui->cb_LogDetailed->isChecked();
61  const bool reverse = (sender == ui->cb_LogReverseLookup) ? enabled : ui->cb_LogReverseLookup->isChecked();
62  const bool matching =
63  (sender == ui->cb_LogMatchingMessages) ? enabled : ui->cb_LogMatchingMessages->isChecked();
64  const bool simplified = !detailed;
65 
66  if (sender == ui->cb_LogReverseLookup || sender == ui->cb_LogDetailed)
67  {
68  ReverseLookupLogging revLog = RevLogDisabled;
69  if (reverse && simplified) { revLog = RevLogEnabledSimplified; }
70  else if (reverse) { revLog = RevLogEnabled; }
71 
73  }
74 
75  if (sender == ui->cb_LogMatchingMessages || sender == ui->cb_LogDetailed)
76  {
77  MatchingLog matchingLog = MatchingLogNothing;
78  if (matching && simplified) { matchingLog = MatchingLogSimplified; }
79  else if (matching) { matchingLog = MatchingLogAll; }
80 
82  }
83  }
84 
85  void CModelMatcherLogEnable::initGui()
86  {
87  if (this->hasContexts())
88  {
89  // avoid signal roundtrips
90  const ReverseLookupLogging revLog = sGui->getIContextNetwork()->isReverseLookupMessagesEnabled();
91  const bool revLogEnabled = revLog.testFlag(RevLogEnabled);
92  if (revLogEnabled != ui->cb_LogReverseLookup->isChecked())
93  {
94  ui->cb_LogReverseLookup->setChecked(revLogEnabled);
95  }
96 
97  const MatchingLog matchingLog = sGui->getIContextSimulator()->isMatchingMessagesEnabled();
98  const bool matchingLogEnabled = matchingLog > 0;
99  if (matchingLogEnabled != ui->cb_LogMatchingMessages->isChecked())
100  {
101  ui->cb_LogMatchingMessages->setChecked(matchingLogEnabled);
102  }
103 
104  const bool simplified = revLog.testFlag(RevLogSimplifiedInfo) || matchingLog == MatchingLogSimplified;
105  const bool detailed = !simplified;
106  if (detailed != ui->cb_LogDetailed->isChecked()) { ui->cb_LogDetailed->setChecked(detailed); }
107  }
108  }
109 
110  bool CModelMatcherLogEnable::hasContexts() const
111  {
112  if (!sGui || sGui->isShuttingDown()) { return false; }
114  }
115 
116  void CModelMatcherLogEnable::valuesChanged() { this->initGui(); }
117 
118  void CModelMatcherLogEnable::connectionStatusChanged(const CConnectionStatus &from, const CConnectionStatus &to)
119  {
120  Q_UNUSED(from);
121  if (to.isConnected() || to.isDisconnected()) { this->initGui(); }
122  }
123 } // namespace swift::gui::components
const context::IContextNetwork * getIContextNetwork() const
Direct access to contexts if a CCoreFacade has been initialized.
bool isShuttingDown() const
Is application shutting down?
const context::IContextSimulator * getIContextSimulator() const
Direct access to contexts if a CCoreFacade has been initialized.
virtual swift::misc::simulation::ReverseLookupLogging isReverseLookupMessagesEnabled() const =0
Enabled reverse lookup logging?
virtual void enableReverseLookupMessages(swift::misc::simulation::ReverseLookupLogging enable)=0
Enable reverse lookup logging.
virtual swift::misc::simulation::MatchingLog isMatchingMessagesEnabled() const =0
Enabled mapping logging?
virtual void enableMatchingMessages(swift::misc::simulation::MatchingLog enabled)=0
Enable mapping logging.
bool isMatchingLogEnabled() const
Matching log.enabled.
bool isReverseLookupLogEnabled() const
Reverse lookup enabled?
Value object encapsulating information about a connection status.
bool isConnected() const
Query status.
bool isDisconnected() const
Query status.
SWIFT_GUI_EXPORT swift::gui::CGuiApplication * sGui
Single instance of GUI application object.
Backend services of the swift project, like dealing with the network or the simulators.
Definition: actionbind.cpp:7
High level reusable GUI components.
Definition: aboutdialog.cpp:13
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