From 2a5acb8d8815ebc78b88c66bd5afb1f453ba3bca Mon Sep 17 00:00:00 2001 From: Reinder Feenstra Date: Wed, 9 Nov 2022 23:03:56 +0100 Subject: [PATCH] added clock to statusbar (can be disabled in settings) --- client/CMakeLists.txt | 2 + client/src/mainwindow.cpp | 7 +- client/src/mainwindow.hpp | 7 ++ client/src/mainwindow/mainwindowstatusbar.cpp | 76 +++++++++++++++++++ client/src/mainwindow/mainwindowstatusbar.hpp | 45 +++++++++++ client/src/network/object.cpp | 7 ++ client/src/network/object.hpp | 8 ++ client/src/settings/settingsdialog.cpp | 4 +- client/src/settings/statusbarsettings.hpp | 48 ++++++++++++ .../src/settings/statusbarsettingswidget.cpp | 34 +++++++++ .../src/settings/statusbarsettingswidget.hpp | 36 +++++++++ 11 files changed, 271 insertions(+), 3 deletions(-) create mode 100644 client/src/mainwindow/mainwindowstatusbar.cpp create mode 100644 client/src/mainwindow/mainwindowstatusbar.hpp create mode 100644 client/src/settings/statusbarsettings.hpp create mode 100644 client/src/settings/statusbarsettingswidget.cpp create mode 100644 client/src/settings/statusbarsettingswidget.hpp diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index c7a3acd4..e5b476d8 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -40,6 +40,8 @@ file(GLOB SOURCES "src/board/*.cpp" "src/dialog/*.hpp" "src/dialog/*.cpp" + "src/mainwindow/*.hpp" + "src/mainwindow/*.cpp" "src/misc/*.hpp" "src/misc/*.cpp" "src/network/*.hpp" diff --git a/client/src/mainwindow.cpp b/client/src/mainwindow.cpp index c8741388..c2660197 100644 --- a/client/src/mainwindow.cpp +++ b/client/src/mainwindow.cpp @@ -22,7 +22,6 @@ #include "mainwindow.hpp" #include -#include #include #include #include @@ -38,6 +37,7 @@ #include #include #include "mdiarea.hpp" +#include "mainwindow/mainwindowstatusbar.hpp" #include "dialog/connectdialog.hpp" #include "settings/settingsdialog.hpp" #include "dialog/worldlistdialog.hpp" @@ -99,6 +99,7 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), m_splitter{new QSplitter(Qt::Vertical, this)}, m_mdiArea{new MdiArea(m_splitter)}, + m_statusBar{new MainWindowStatusBar(*this)}, m_serverLog{nullptr}, m_toolbar{new QToolBar(this)} { @@ -514,7 +515,7 @@ MainWindow::MainWindow(QWidget* parent) : m_splitter->setCollapsible(0, false); setCentralWidget(m_splitter); - setStatusBar(new QStatusBar()); + setStatusBar(m_statusBar); QSettings settings; if(settings.contains(SETTING_GEOMETRY)) @@ -585,6 +586,7 @@ void MainWindow::worldChanged() m_clockAction->setEnabled(false); m_clockAction->setChecked(false); m_clock.reset(); + emit worldClockChanged(); if(m_connection) { @@ -602,6 +604,7 @@ void MainWindow::worldChanged() if(auto* freeze = object->getProperty("freeze")) { m_clock = object; + emit worldClockChanged(); m_clockAction->setEnabled(true); m_clockAction->setChecked(!freeze->toBool()); diff --git a/client/src/mainwindow.hpp b/client/src/mainwindow.hpp index c95c7331..8bf8c30f 100644 --- a/client/src/mainwindow.hpp +++ b/client/src/mainwindow.hpp @@ -29,6 +29,7 @@ #include "subwindow/subwindowtype.hpp" class MdiArea; +class MainWindowStatusBar; class QSplitter; class QActionGroup; class QToolButton; @@ -52,6 +53,7 @@ class MainWindow : public QMainWindow ObjectPtr m_clock; QSplitter* m_splitter; MdiArea* m_mdiArea; + MainWindowStatusBar* m_statusBar; ServerLogWidget* m_serverLog; QMap m_subWindows; // Main menu: @@ -119,10 +121,15 @@ class MainWindow : public QMainWindow const std::shared_ptr& connection() { return m_connection; } + const ObjectPtr& worldClock() const { return m_clock; } + public slots: void connectToServer(const QString& url = QString()); void showObject(const ObjectPtr& object, SubWindowType flags = SubWindowType::Object); void showObject(const QString& id, const QString& title = "", SubWindowType flags = SubWindowType::Object); + + signals: + void worldClockChanged(); }; #endif diff --git a/client/src/mainwindow/mainwindowstatusbar.cpp b/client/src/mainwindow/mainwindowstatusbar.cpp new file mode 100644 index 00000000..ada938d7 --- /dev/null +++ b/client/src/mainwindow/mainwindowstatusbar.cpp @@ -0,0 +1,76 @@ +/** + * client/src/mainwindow/mainwindowstatusbar.cpp + * + * This file is part of the traintastic source code. + * + * Copyright (C) 2022 Reinder Feenstra + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "mainwindowstatusbar.hpp" +#include +#include "../mainwindow.hpp" +#include "../network/object.hpp" +#include "../network/abstractproperty.hpp" +#include "../settings/statusbarsettings.hpp" + +MainWindowStatusBar::MainWindowStatusBar(MainWindow& mainWindow) + : QStatusBar(&mainWindow) + , m_mainWindow{mainWindow} + , m_clockLabel{new QLabel(this)} +{ + // clock: + m_clockLabel->setMinimumWidth(m_clockLabel->fontMetrics().averageCharWidth() * 5); + m_clockLabel->setAlignment(Qt::AlignRight); + addPermanentWidget(m_clockLabel); + connect(&m_mainWindow, &MainWindow::worldClockChanged, + [this]() + { + if(const auto& clock = m_mainWindow.worldClock()) + if(const auto* minute = clock->getProperty("minute")) + connect(minute, &AbstractProperty::valueChanged, this, &MainWindowStatusBar::updateClock); + }); + + // settings: + connect(&StatusBarSettings::instance(), &StatusBarSettings::changed, this, &MainWindowStatusBar::settingsChanged); + settingsChanged(); +} + +void MainWindowStatusBar::settingsChanged() +{ + clockChanged(); +} + +void MainWindowStatusBar::clockChanged() +{ + const bool visible = StatusBarSettings::instance().showClock.value(); + + if(m_clockLabel->isVisible() == visible) + return; + + if(visible) + updateClock(); + + m_clockLabel->setVisible(visible); +} + +void MainWindowStatusBar::updateClock() +{ + if(const auto& clock = m_mainWindow.worldClock()) + m_clockLabel->setText(QString("%1:%2").arg(clock->getPropertyValueInt("hour", 0)).arg(clock->getPropertyValueInt("minute", 0), 2, 10, QChar('0'))); + else + m_clockLabel->setText("--:--"); +} diff --git a/client/src/mainwindow/mainwindowstatusbar.hpp b/client/src/mainwindow/mainwindowstatusbar.hpp new file mode 100644 index 00000000..7639d407 --- /dev/null +++ b/client/src/mainwindow/mainwindowstatusbar.hpp @@ -0,0 +1,45 @@ +/** + * client/src/mainwindow/mainwindowstatusbar.hpp + * + * This file is part of the traintastic source code. + * + * Copyright (C) 2022 Reinder Feenstra + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef TRAINTASTIC_CLIENT_MAINWINDOW_MAINWINDOWSTATUSBAR_HPP +#define TRAINTASTIC_CLIENT_MAINWINDOW_MAINWINDOWSTATUSBAR_HPP + +#include + +class MainWindow; +class QLabel; + +class MainWindowStatusBar : public QStatusBar +{ + private: + MainWindow& m_mainWindow; + QLabel* m_clockLabel; + + void settingsChanged(); + void clockChanged(); + void updateClock(); + + public: + MainWindowStatusBar(MainWindow& mainWindow); +}; + +#endif diff --git a/client/src/network/object.cpp b/client/src/network/object.cpp index 2bb01094..2a008670 100644 --- a/client/src/network/object.cpp +++ b/client/src/network/object.cpp @@ -62,6 +62,13 @@ bool Object::getPropertyValueBool(const QString& name, bool defaultValue) const return defaultValue; } +int Object::getPropertyValueInt(const QString& name, int defaultValue) const +{ + if(const auto* property = getProperty(name); property && property->type() == ValueType::Integer) + return property->toInt(); + return defaultValue; +} + const AbstractVectorProperty* Object::getVectorProperty(const QString& name) const { return dynamic_cast(m_interfaceItems.find(name)); diff --git a/client/src/network/object.hpp b/client/src/network/object.hpp index 6d20307a..86087188 100644 --- a/client/src/network/object.hpp +++ b/client/src/network/object.hpp @@ -75,6 +75,14 @@ class Object : public QObject */ bool getPropertyValueBool(const QString& name, bool defaultValue) const; + /** + * \brief Get integer property value + * \param[in] name Property name + * \param[in] defaultValue Value to return if property doesn't exist or isn't an integer + * \return Property value or \c defaultValue if property doesn't exist or isn't an integer + */ + int getPropertyValueInt(const QString& name, int defaultValue) const; + inline bool hasVectorProperty(const QString& name) const { return getVectorProperty(name); } const AbstractVectorProperty* getVectorProperty(const QString& name) const; AbstractVectorProperty* getVectorProperty(const QString& name); diff --git a/client/src/settings/settingsdialog.cpp b/client/src/settings/settingsdialog.cpp index 797260d8..7edd8b3d 100644 --- a/client/src/settings/settingsdialog.cpp +++ b/client/src/settings/settingsdialog.cpp @@ -3,7 +3,7 @@ * * This file is part of the traintastic source code. * - * Copyright (C) 2019-2021 Reinder Feenstra + * Copyright (C) 2019-2022 Reinder Feenstra * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -27,6 +27,7 @@ #include #include #include "generalsettingswidget.hpp" +#include "statusbarsettingswidget.hpp" #include "boardsettingswidget.hpp" #include "developersettingswidget.hpp" @@ -38,6 +39,7 @@ SettingsDialog::SettingsDialog(QWidget* parent) : setWindowTitle(Locale::tr("qtapp.mainmenu:settings")); add(new GeneralSettingsWidget(this)); + add(new StatusBarSettingsWidget(this)); add(new BoardSettingsWidget(this)); add(new DeveloperSettingsWidget(this)); diff --git a/client/src/settings/statusbarsettings.hpp b/client/src/settings/statusbarsettings.hpp new file mode 100644 index 00000000..07cfd4b1 --- /dev/null +++ b/client/src/settings/statusbarsettings.hpp @@ -0,0 +1,48 @@ +/** + * client/src/settings/statusbarsettings.hpp + * + * This file is part of the traintastic source code. + * + * Copyright (C) 2022 Reinder Feenstra + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef TRAINTASTIC_CLIENT_SETTINGS_STATUSBARSETTINGS_HPP +#define TRAINTASTIC_CLIENT_SETTINGS_STATUSBARSETTINGS_HPP + +#include "settingsbase.hpp" +#include "setting.hpp" + +class StatusBarSettings : public SettingsBase +{ + private: + StatusBarSettings() + : SettingsBase("statusbar") + , showClock{*this, "show_clock", true} + { + } + + public: + static StatusBarSettings& instance() + { + static StatusBarSettings settings; + return settings; + } + + Setting showClock; +}; + +#endif diff --git a/client/src/settings/statusbarsettingswidget.cpp b/client/src/settings/statusbarsettingswidget.cpp new file mode 100644 index 00000000..bc7378d4 --- /dev/null +++ b/client/src/settings/statusbarsettingswidget.cpp @@ -0,0 +1,34 @@ +/** + * client/src/settings/statusbarsettingswidget.cpp + * + * This file is part of the traintastic source code. + * + * Copyright (C) 2022 Reinder Feenstra + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "statusbarsettingswidget.hpp" +#include "statusbarsettings.hpp" + +StatusBarSettingsWidget::StatusBarSettingsWidget(QWidget* parent) + : SettingsBaseWidget("qtapp.settings.statusbar", parent) +{ + StatusBarSettings& s = StatusBarSettings::instance(); + + addSetting(s.showClock); + + done(); +} diff --git a/client/src/settings/statusbarsettingswidget.hpp b/client/src/settings/statusbarsettingswidget.hpp new file mode 100644 index 00000000..4fe98498 --- /dev/null +++ b/client/src/settings/statusbarsettingswidget.hpp @@ -0,0 +1,36 @@ +/** + * client/src/settings/statusbarsettingswidget.hpp + * + * This file is part of the traintastic source code. + * + * Copyright (C) 2022 Reinder Feenstra + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef TRAINTASTIC_CLIENT_SETTINGS_STATUSBARSETTINGSWIDGET_HPP +#define TRAINTASTIC_CLIENT_SETTINGS_STATUSBARSETTINGSWIDGET_HPP + +#include "settingsbasewidget.hpp" + +class StatusBarSettingsWidget : public SettingsBaseWidget +{ + public: + StatusBarSettingsWidget(QWidget* parent = nullptr); + + QString title() const final { return QStringLiteral("qtapp.settings:statusbar"); } +}; + +#endif