Add Qt dialog state controller
Allow standard control of drill-down dialogs (with back / forward navigation)
This commit is contained in:
parent
faa070307d
commit
3cd3ad465e
6 changed files with 174 additions and 8 deletions
|
@ -186,6 +186,8 @@ if (HAVE_QT)
|
|||
FGQQWindowManager.hxx
|
||||
PathUrlHelper.cxx
|
||||
PathUrlHelper.hxx
|
||||
DialogStateController.cxx
|
||||
DialogStateController.hxx
|
||||
)
|
||||
|
||||
set_property(TARGET fgqmlui PROPERTY AUTOMOC ON)
|
||||
|
|
65
src/GUI/DialogStateController.cxx
Normal file
65
src/GUI/DialogStateController.cxx
Normal file
|
@ -0,0 +1,65 @@
|
|||
// Copyright (C) 2020 James Turner <james@flightgear.org>
|
||||
//
|
||||
// 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 <QDebug>
|
||||
|
||||
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);
|
||||
}
|
66
src/GUI/DialogStateController.hxx
Normal file
66
src/GUI/DialogStateController.hxx
Normal file
|
@ -0,0 +1,66 @@
|
|||
// Copyright (C) 2020 James Turner <james@flightgear.org>
|
||||
//
|
||||
// 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 <QJSValue>
|
||||
#include <QMap>
|
||||
#include <QObject>
|
||||
|
||||
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<QString, QJSValue> m_changes;
|
||||
|
||||
bool m_canApply = false;
|
||||
bool m_canRestoreDefaults = false;
|
||||
};
|
||||
|
||||
#endif /* DialogStateController_h */
|
|
@ -39,14 +39,13 @@
|
|||
#include <osgViewer/GraphicsWindow>
|
||||
#include <osgViewer/Viewer>
|
||||
|
||||
//#include <GUI/DialogStateController.hxx>
|
||||
#include <GUI/DialogStateController.hxx>
|
||||
#include <GUI/FGQQWindowManager.hxx>
|
||||
#include <GUI/FGQmlInstance.hxx>
|
||||
#include <GUI/FGQmlPropertyNode.hxx>
|
||||
#include <Viewer/OSGQtAdaption.hxx>
|
||||
|
||||
#include <Main/fg_props.hxx>
|
||||
#include <Main/globals.hxx>
|
||||
#include <Viewer/OSGQtAdaption.hxx>
|
||||
#include <simgear/structure/commands.hxx>
|
||||
|
||||
#if defined(HAVE_PUI)
|
||||
|
@ -464,7 +463,7 @@ QQuickDrawable::QQuickDrawable() : d(new QQuickDrawablePrivate)
|
|||
|
||||
// QML types
|
||||
qmlRegisterType<FGQmlPropertyNode>("FlightGear", 1, 0, "Property");
|
||||
//qmlRegisterType<DialogStateController>("FlightGear", 1, 0, "DialogStateController");
|
||||
qmlRegisterType<DialogStateController>("FlightGear", 1, 0, "DialogStateController");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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<QUrl> m_stack;
|
||||
QVector<QString> m_titles;
|
||||
};
|
||||
|
||||
#endif // FG_GUI_STACK_CONTROLLER_HXX
|
||||
|
|
Loading…
Reference in a new issue