swift
swiftdata.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2015 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
4 #include "swiftdata.h"
5 
6 #include <QAction>
7 #include <QPointer>
8 #include <QString>
9 #include <QStyle>
10 #include <QVersionNumber>
11 #include <QtGlobal>
12 
13 #include "ui_swiftdata.h"
14 
15 #include "config/buildconfig.h"
16 #include "core/data/globalsetup.h"
22 #include "gui/guiapplication.h"
23 #include "gui/stylesheetutility.h"
24 #include "misc/loghandler.h"
25 #include "misc/logmessage.h"
26 #include "misc/logpattern.h"
27 #include "misc/network/url.h"
31 #include "misc/statusmessage.h"
32 
33 using namespace swift::misc;
34 using namespace swift::misc::network;
35 using namespace swift::misc::simulation;
36 using namespace swift::core;
37 using namespace swift::core::data;
38 using namespace swift::core::db;
39 using namespace swift::gui;
40 using namespace swift::gui::components;
41 using namespace swift::config;
42 
43 CSwiftData::CSwiftData(QWidget *parent) : QMainWindow(parent), CIdentifiable(this), ui(new Ui::CSwiftData)
44 {
45 
46  Q_ASSERT_X(sGui, Q_FUNC_INFO, "Need sGui");
48  ui->setupUi(this);
49  this->init();
50 }
51 
52 void CSwiftData::initStyleSheet()
53 {
54  const QString s = sGui->getStyleSheetUtility().styles({ CStyleSheetUtility::fileNameFonts(),
55  CStyleSheetUtility::fileNameStandardWidget(),
56  CStyleSheetUtility::fileNameSwiftData() });
57 
58  this->setStyleSheet("");
59  this->setStyleSheet(s);
60 }
61 
63 
64 void CSwiftData::closeEvent(QCloseEvent *event)
65 {
66  if (sGui)
67  {
68  // save settings?
69  if (sGui->showCloseDialog(this, event) == QDialog::Rejected) { return; }
70  }
71  this->performGracefulShutdown();
72 }
73 
74 void CSwiftData::onStyleSheetsChanged() { this->initStyleSheet(); }
75 
76 void CSwiftData::init()
77 {
78  Q_ASSERT_X(sGui, Q_FUNC_INFO, "Need sGui");
80  this->initLogDisplay();
81 
82  m_mwaLogComponent = ui->comp_MainInfoArea->getLogComponent();
83  m_mwaStatusBar = &m_statusBar;
84  m_mwaOverlayFrame = ui->comp_MainInfoArea->getMappingComponent();
85 
86  this->initStyleSheet();
87  connect(sGui, &CGuiApplication::styleSheetsChanged, this, &CSwiftData::onStyleSheetsChanged, Qt::QueuedConnection);
88  this->initMenu();
89 
90  // update title
91  const CGlobalSetup s(sApp->getGlobalSetup());
92  if (!s.getDbHomePageUrl().isEmpty())
93  {
94  this->setWindowTitle(QStringLiteral("%1 %2").arg(this->windowTitle(), s.getDbHomePageUrl().toQString(true)));
95  }
96 
97  sGui->triggerNewVersionCheck(20 * 1000);
98  QPointer<CSwiftData> myself(this);
99  QTimer::singleShot(15 * 1000, this, [=] {
100  if (!myself || !sGui || sGui->isShuttingDown()) { return; }
101  this->checkMinimumVersion();
102  this->checkAutoPublishing();
103  });
104 }
105 
106 void CSwiftData::initLogDisplay()
107 {
108  m_statusBar.initStatusBar(ui->sb_SwiftData);
109  // m_statusBar.setSizeGripEnabled(false);
110 
111  connect(&m_logHistory, &CLogHistoryReplica::elementAdded, this,
112  [this](const CStatusMessage &message) { m_statusBar.displayStatusMessage(message); });
113  m_logHistory.setFilter(CLogPattern().withSeverityAtOrAbove(CStatusMessage::SeverityInfo));
114  m_logHistory.initialize(sApp->getDataLinkDBus());
115 
116  CLogHandler::instance()->install(true);
117  CLogHandler::instance()->enableConsoleOutput(false); // default disable
118 }
119 
120 void CSwiftData::initMenu()
121 {
122  // menu
123 
124  this->initDynamicMenus();
125  ui->menu_WindowMinimize->setIcon(this->style()->standardIcon(QStyle::SP_TitleBarMinButton));
126 
127  connect(ui->menu_WindowFont, &QAction::triggered, this, &CSwiftData::onMenuClicked);
128  connect(ui->menu_MappingMaxData, &QAction::triggered, this, &CSwiftData::onMenuClicked);
129  connect(ui->menu_MappingMaxMapping, &QAction::triggered, this, &CSwiftData::onMenuClicked);
130 
131  sGui->addMenuFile(*ui->menu_File);
132  sGui->addMenuInternals(*ui->menu_Internals);
133  sGui->addMenuWindow(*ui->menu_Window);
134  sGui->addMenuHelp(*ui->menu_Help);
135 }
136 
137 void CSwiftData::performGracefulShutdown()
138 {
139  if (m_updater) { m_updater->abandonAndWait(); }
140 }
141 
142 void CSwiftData::consolidationSettingChanged()
143 {
144  const int consolidationSecs = m_consolidationSettings.get();
145  if (consolidationSecs < 0)
146  {
147  if (m_updater)
148  {
149  ui->comp_MainInfoArea->getDataSettingsComponent()->setBackgroundUpdater(nullptr);
150  disconnect(m_updater);
151  m_updater->abandonAndWait();
152  m_updater = nullptr;
153  }
154  }
155  else
156  {
157  if (!m_updater)
158  {
159  m_updater = new CBackgroundDataUpdater(this);
160  connect(m_updater, &CBackgroundDataUpdater::consolidating, ui->comp_InfoBar,
161  &CInfoBarWebReadersStatusComponent::consolidationRunning, Qt::QueuedConnection);
162  m_updater->start(QThread::LowestPriority);
163  ui->comp_MainInfoArea->getDataSettingsComponent()->setBackgroundUpdater(m_updater);
164  }
165  m_updater->startUpdating(consolidationSecs);
166  }
167 }
168 
169 void CSwiftData::displayLog() { ui->comp_MainInfoArea->displayLog(); }
170 
171 void CSwiftData::checkMinimumVersion()
172 {
173  if (!sApp || sApp->isShuttingDown()) { return; }
175  {
176  CLogMessage(this).info(u"Checked mapping tool version, required '%1', this version '%2'")
177  << sApp->getGlobalSetup().getMappingMinimumVersionString() << CBuildConfig::getVersionString();
178  }
179  else
180  {
181  const CStatusMessage sm =
182  CStatusMessage(this, CStatusMessage::SeverityWarning,
183  u"Your are using swift version: '%1'. Creating mappings requires at least '%2'.")
184  << CBuildConfig::getVersionString() << sApp->getGlobalSetup().getMappingMinimumVersionString();
185  CLogMessage::preformatted(sm);
186  this->displayInOverlayWindow(sm);
187  }
188 }
189 
190 void CSwiftData::checkAutoPublishing()
191 {
192  if (!CAutoPublishData::existAutoPublishFiles()) { return; }
193  this->showAutoPublishing();
194 }
195 
196 void CSwiftData::showAutoPublishing()
197 {
198  if (!sApp || sApp->isShuttingDown()) { return; }
199  if (!m_autoPublishDialog) { m_autoPublishDialog = new CAutoPublishDialog(this); }
200  m_autoPublishDialog->readAndShow();
201 }
SWIFT_CORE_EXPORT swift::core::CApplication * sApp
Single instance of application object.
Definition: application.cpp:71
swift data entry control (aka mapping tool)
Definition: swiftdata.h:38
virtual void closeEvent(QCloseEvent *event)
Definition: swiftdata.cpp:64
virtual ~CSwiftData()
Destructor.
Definition: swiftdata.cpp:62
CSwiftData(QWidget *parent=nullptr)
Constructor.
Definition: swiftdata.cpp:43
swift::misc::shared_state::CDataLinkDBus * getDataLinkDBus()
Transport mechanism for sharing state between applications.
data::CGlobalSetup getGlobalSetup() const
Global setup.
bool isShuttingDown() const
Is application shutting down?
Global settings for readers, debug flags, etc.
Definition: globalsetup.h:31
bool isSwiftVersionMinimumMappingVersion() const
Meets the minimum mapping version.
Definition: globalsetup.cpp:85
const QString & getMappingMinimumVersionString() const
Creating mappings requires at least this version or higher.
Definition: globalsetup.h:124
Update and consolidation of DB data.
void registerMainApplicationWidget(QWidget *mainWidget)
Register main application window widget if this is known.
void triggerNewVersionCheck(int delayedMs)
Trigger new version check.
void addMenuHelp(QMenu &menu)
Help operations.
void addMenuFile(QMenu &menu)
File menu.
void addMenuInternals(QMenu &menu)
Internals menu.
const CStyleSheetUtility & getStyleSheetUtility() const
Style sheet handling.
QDialog::DialogCode showCloseDialog(QMainWindow *mainWindow, QCloseEvent *closeEvent)
Show close dialog.
void addMenuWindow(QMenu &menu)
Window operations.
void initMainApplicationWidget(QWidget *mainWidget)
Init the main application window based on information in this application.
void displayStatusMessage(const swift::misc::CStatusMessage &statusMessage)
Display status message.
void initStatusBar(QStatusBar *statusBar=nullptr)
Init.
QString styles(const QStringList &fileNames) const
Multiple styles concatenated.
CManagedStatusBar * m_mwaStatusBar
status bar if any
virtual bool displayInOverlayWindow(const swift::misc::CStatusMessage &message, std::chrono::milliseconds timeout=std::chrono::milliseconds(0))
Display in overlay window.
components::CLogComponent * m_mwaLogComponent
the log component if any
COverlayMessagesFrame * m_mwaOverlayFrame
overlay messages if any
CAutoPublishComponent as dialog.
int readAndShow()
Read files and show dialog.
T get() const
Get a copy of the current value.
Definition: valuecache.h:408
void start(QThread::Priority priority=QThread::InheritPriority)
Starts a thread and moves the worker into it.
Definition: worker.cpp:166
void startUpdating(int updateTimeSecs)
Start updating (start/stop timer)
Definition: worker.cpp:235
Base class with a member CIdentifier to be inherited by a class which has an identity in the environm...
Definition: identifiable.h:24
Class for emitting a log message.
Definition: logmessage.h:27
Value class for matching log messages based on their categories.
Definition: logpattern.h:49
Derived & info(const char16_t(&format)[N])
Set the severity to info, providing a format string.
Streamable status message, e.g.
void abandonAndWait() noexcept
Convenience to call abandon() followed by waitForFinished().
Definition: worker.cpp:147
void setFilter(const U &filter)
Set filter to choose list elements.
Definition: listobserver.h:77
virtual void initialize(IDataLink *dataLink)
Subscribe using the given transport mechanism.
Definition: listobserver.h:70
SWIFT_GUI_EXPORT swift::gui::CGuiApplication * sGui
Single instance of GUI application object.
Core data traits (aka cached values) and classes.
Classes interacting with the swift database (aka "datastore").
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
GUI related classes.
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