diff --git a/src/GUI/CMakeLists.txt b/src/GUI/CMakeLists.txt index 038d1d7a6..0e4df7fc6 100644 --- a/src/GUI/CMakeLists.txt +++ b/src/GUI/CMakeLists.txt @@ -186,6 +186,8 @@ if (HAVE_QT) FGQQWindowManager.hxx PathUrlHelper.cxx PathUrlHelper.hxx + DialogStateController.cxx + DialogStateController.hxx ) set_property(TARGET fgqmlui PROPERTY AUTOMOC ON) diff --git a/src/GUI/DialogStateController.cxx b/src/GUI/DialogStateController.cxx new file mode 100644 index 000000000..168da1baf --- /dev/null +++ b/src/GUI/DialogStateController.cxx @@ -0,0 +1,65 @@ +// Copyright (C) 2020 James Turner +// +// 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 "config.h" + +#include "DialogStateController.hxx" + +#include + +void DialogStateController::add(QString key, QJSValue cb) +{ + bool wasEmpty = m_changes.isEmpty(); + if (!cb.isCallable()) { + qWarning() << "tried to insert non-callable into state key:" << key; + return; + } + + m_changes.insert(key, cb); + + if (wasEmpty) { + emit canApplyChanged(true); + } +} + +void DialogStateController::apply() +{ + auto it = m_changes.begin(); + for (; it != m_changes.end(); ++it) { + QJSValueList args; + args.append(QJSValue(it.key())); + it.value().call(args); + } +} + +void DialogStateController::cancel() +{ + m_changes.clear(); + emit canApplyChanged(false); +} + +void DialogStateController::restoreDefaults() +{ +} + +void DialogStateController::setCanRestoreDefaults(bool canRestoreDefaults) +{ + if (m_canRestoreDefaults == canRestoreDefaults) + return; + + m_canRestoreDefaults = canRestoreDefaults; + emit canRestoreDefaultsChanged(m_canRestoreDefaults); +} diff --git a/src/GUI/DialogStateController.hxx b/src/GUI/DialogStateController.hxx new file mode 100644 index 000000000..ddb746832 --- /dev/null +++ b/src/GUI/DialogStateController.hxx @@ -0,0 +1,66 @@ +// Copyright (C) 2020 James Turner +// +// 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 DialogStateController_h +#define DialogStateController_h + +#include +#include +#include + +class DialogStateController : public QObject +{ + Q_OBJECT + + Q_PROPERTY(bool canRestoreDefaults READ canRestoreDefaults WRITE setCanRestoreDefaults NOTIFY canRestoreDefaultsChanged) + Q_PROPERTY(bool canApply READ canApply NOTIFY canApplyChanged) +public: + Q_INVOKABLE void add(QString key, QJSValue cb); + + bool canApply() const + { + return m_canApply; + } + + bool canRestoreDefaults() const + { + return m_canRestoreDefaults; + } + +public slots: + + void apply(); + + void cancel(); + + void restoreDefaults(); + + void setCanRestoreDefaults(bool canRestoreDefaults); + +signals: + void canApplyChanged(bool canApply); + + void canRestoreDefaultsChanged(bool canRestoreDefaults); + +private: + // list of tracked changes + QMap m_changes; + + bool m_canApply = false; + bool m_canRestoreDefaults = false; +}; + +#endif /* DialogStateController_h */ diff --git a/src/GUI/QQuickDrawable.cxx b/src/GUI/QQuickDrawable.cxx index 0b26e90e5..06c7ea4a0 100755 --- a/src/GUI/QQuickDrawable.cxx +++ b/src/GUI/QQuickDrawable.cxx @@ -39,14 +39,13 @@ #include #include -//#include +#include #include #include #include -#include - #include
#include
+#include #include #if defined(HAVE_PUI) @@ -464,7 +463,7 @@ QQuickDrawable::QQuickDrawable() : d(new QQuickDrawablePrivate) // QML types qmlRegisterType("FlightGear", 1, 0, "Property"); - //qmlRegisterType("FlightGear", 1, 0, "DialogStateController"); + qmlRegisterType("FlightGear", 1, 0, "DialogStateController"); } } diff --git a/src/GUI/StackController.cxx b/src/GUI/StackController.cxx index 6f5e0f1ce..efda4bbef 100644 --- a/src/GUI/StackController.cxx +++ b/src/GUI/StackController.cxx @@ -31,9 +31,29 @@ QUrl StackController::currentPageSource() const return m_stack.front(); } -void StackController::push(QUrl page) +QString StackController::currentPageTitle() const +{ + if (m_stack.empty()) + return {}; + return m_titles.front(); +} + +QString StackController::previousPageTitle() const +{ + if (m_titles.size() < 2) + return {}; + return m_titles.at(1); +} + +bool StackController::canGoBack() const +{ + return (m_stack.size() > 1); +} + +void StackController::push(QUrl page, QString title) { m_stack.push_front(page); + m_titles.push_front(title); emit currentPageChanged(); } @@ -42,12 +62,16 @@ void StackController::pop() if (m_stack.empty()) return; m_stack.pop_front(); + m_titles.pop_front(); emit currentPageChanged(); } -void StackController::replace(QUrl url) +void StackController::replace(QUrl url, QString title) { m_stack.clear(); m_stack.push_back(url); + m_titles.clear(); + m_titles.push_back(title); + emit currentPageChanged(); } diff --git a/src/GUI/StackController.hxx b/src/GUI/StackController.hxx index c9ec6d3ba..99cfb24c1 100644 --- a/src/GUI/StackController.hxx +++ b/src/GUI/StackController.hxx @@ -32,20 +32,30 @@ class StackController : public QObject Q_PROPERTY(QUrl currentPageSource READ currentPageSource NOTIFY currentPageChanged) + Q_PROPERTY(bool canGoBack READ canGoBack NOTIFY currentPageChanged) + + Q_PROPERTY(QString currentPageTitle READ currentPageTitle NOTIFY currentPageChanged) + Q_PROPERTY(QString previousPageTitle READ previousPageTitle NOTIFY currentPageChanged) + public: StackController(); - Q_INVOKABLE void push(QUrl page); + Q_INVOKABLE void push(QUrl page, QString title); Q_INVOKABLE void pop(); - Q_INVOKABLE void replace(QUrl url); + Q_INVOKABLE void replace(QUrl url, QString title); QUrl currentPageSource() const; + bool canGoBack() const; + QString currentPageTitle() const; + QString previousPageTitle() const; + signals: void currentPageChanged(); private: QVector m_stack; + QVector m_titles; }; #endif // FG_GUI_STACK_CONTROLLER_HXX