Skip to content

Commit

Permalink
Merge branch 'ign-gui3' into chapulina/3/world_from_menu
Browse files Browse the repository at this point in the history
  • Loading branch information
chapulina authored Jan 15, 2022
2 parents 5e93077 + 297d565 commit 85b539c
Show file tree
Hide file tree
Showing 21 changed files with 1,239 additions and 8 deletions.
29 changes: 29 additions & 0 deletions examples/config/dialog_on_exit_shutdown.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0"?>

<!-- Window -->
<window>
<width>1000</width>
<height>845</height>
<default_exit_action>SHUTDOWN_SERVER</default_exit_action>
<dialog_on_exit>true</dialog_on_exit>
<dialog_on_exit_options>
<prompt_text>really?</prompt_text>
<show_shutdown_button>true</show_shutdown_button>
<show_close_gui_button>true</show_close_gui_button>
<shutdown_button_text>Quit Server and GUI</shutdown_button_text>
<!-- Always change this text when displaying both close GUI and shutdown button, otherwise there
would be a dialog with "OK", "Cancel" and "shutdown", which is not very good UX. -->
<close_gui_button_text>Quit GUI only</close_gui_button_text>
</dialog_on_exit_options>
</window>

<plugin filename="ShutdownButton">
<ignition-gui>
<title>Shutdown</title>
<property type="int" key="x">500</property>
<property type="int" key="y">950</property>
<property type="int" key="height">180</property>
<property type="int" key="width">240</property>
</ignition-gui>
</plugin>

145 changes: 145 additions & 0 deletions include/ignition/gui/MainWindow.hh
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,22 @@ namespace ignition
{
namespace gui
{
Q_NAMESPACE
class MainWindowPrivate;
struct WindowConfig;

/// \brief The action executed when GUI is closed without prompt.
enum class ExitAction
{
/// \brief Close GUI and leave server running
CLOSE_GUI,
/// \brief Close GUI and shutdown server
SHUTDOWN_SERVER,
};
/// \cond DO_NOT_DOCUMENT
Q_ENUM_NS(ExitAction)
/// \endcond

/// \brief The main window class creates a QQuickWindow and acts as an
/// interface which provides properties and functions which can be called
/// from Main.qml
Expand Down Expand Up @@ -177,6 +190,14 @@ namespace ignition
NOTIFY ShowPluginMenuChanged
)

/// \brief Flag to enable confirmation dialog on exit
Q_PROPERTY(
ExitAction defaultExitAction
READ DefaultExitAction
WRITE SetDefaultExitAction
NOTIFY DefaultExitActionChanged
)

/// \brief Flag to enable confirmation dialog on exit
Q_PROPERTY(
bool showDialogOnExit
Expand All @@ -185,6 +206,46 @@ namespace ignition
NOTIFY ShowDialogOnExitChanged
)

/// \brief Text of the prompt in confirmation dialog on exit
Q_PROPERTY(
QString dialogOnExitText
READ DialogOnExitText
WRITE SetDialogOnExitText
NOTIFY DialogOnExitTextChanged
)

/// \brief Flag to show "shutdown" button in confirmation dialog on exit
Q_PROPERTY(
bool exitDialogShowShutdown
READ ExitDialogShowShutdown
WRITE SetExitDialogShowShutdown
NOTIFY ExitDialogShowShutdownChanged
)

/// \brief Flag to show "close GUI" button in confirmation dialog on exit
Q_PROPERTY(
bool exitDialogShowCloseGui
READ ExitDialogShowCloseGui
WRITE SetExitDialogShowCloseGui
NOTIFY ExitDialogShowCloseGuiChanged
)

/// \brief Text of the "shutdown" button in confirmation dialog on exit
Q_PROPERTY(
QString exitDialogShutdownText
READ ExitDialogShutdownText
WRITE SetExitDialogShutdownText
NOTIFY ExitDialogShutdownTextChanged
)

/// \brief Text of the "Close GUI" button in confirmation dialog on exit
Q_PROPERTY(
QString exitDialogCloseGuiText
READ ExitDialogCloseGuiText
WRITE SetExitDialogCloseGuiText
NOTIFY ExitDialogCloseGuiTextChanged
)

/// \brief Constructor
public: MainWindow();

Expand Down Expand Up @@ -352,6 +413,15 @@ namespace ignition
/// \param[in] _showPluginMenu True to show.
public: Q_INVOKABLE void SetShowPluginMenu(const bool _showPluginMenu);

/// \brief Get the action performed when GUI closes without prompt.
/// \return The action.
public: Q_INVOKABLE ExitAction DefaultExitAction() const;

/// \brief Set the action performed when GUI closes without prompt.
/// \param[in] _defaultExitAction The action.
public: Q_INVOKABLE void SetDefaultExitAction(
enum ExitAction _defaultExitAction);

/// \brief Get the flag to show the plugin menu.
/// \return True to show.
public: Q_INVOKABLE bool ShowDialogOnExit() const;
Expand All @@ -360,6 +430,60 @@ namespace ignition
/// \param[in] _showDialogOnExit True to show.
public: Q_INVOKABLE void SetShowDialogOnExit(bool _showDialogOnExit);

/// \brief Get the text of prompt in exit dialog.
/// \return Prompt text.
public: Q_INVOKABLE QString DialogOnExitText() const;

/// \brief Set the text of the prompt in exit dialog.
/// \param[in] _dialogOnExitText Prompt text.
public: Q_INVOKABLE void SetDialogOnExitText(
const QString &_dialogOnExitText);

/// \brief Get the flag to show "shutdown" button in exit dialog.
/// \return True to show.
public: Q_INVOKABLE bool ExitDialogShowShutdown() const;

/// \brief Set the flag to show "shutdown" button in exit dialog.
/// \param[in] _exitDialogShowShutdown True to show.
public: Q_INVOKABLE void SetExitDialogShowShutdown(
bool _exitDialogShowShutdown);

/// \brief Get the flag to show "Close GUI" button in exit dialog.
/// \return True to show.
public: Q_INVOKABLE bool ExitDialogShowCloseGui() const;

/// \brief Set the flag to show "Close GUI" button in exit dialog.
/// \param[in] _exitDialogShowCloseGui True to show.
public: Q_INVOKABLE void SetExitDialogShowCloseGui(
bool _exitDialogShowCloseGui);

/// \brief Get the text of the "shutdown" button in exit dialog.
/// \return Button text.
public: Q_INVOKABLE QString ExitDialogShutdownText() const;

/// \brief Set the text of the "shutdown" button in exit dialog.
/// \param[in] _exitDialogShutdownText Button text.
public: Q_INVOKABLE void SetExitDialogShutdownText(
const QString &_exitDialogShutdownText);

/// \brief Get the text of the "Close GUI" button in exit dialog.
/// \return Button text.
public: Q_INVOKABLE QString ExitDialogCloseGuiText() const;

/// \brief Set the text of the "Close GUI" button in exit dialog.
/// \param[in] _exitDialogCloseGuiText Button text.
public: Q_INVOKABLE void SetExitDialogCloseGuiText(
const QString &_exitDialogCloseGuiText);

/// \brief Get the topic of the server control service.
/// \return The service topic.
public: Q_INVOKABLE std::string ServerControlService() const;

/// \brief Set the topic of the server control service.
/// \param[in] _service The service topic.
public: Q_INVOKABLE void SetServerControlService(
const std::string &_service);

/// \brief Callback when load configuration is selected
public slots: void OnLoadConfig(const QString &_path);

Expand All @@ -369,6 +493,9 @@ namespace ignition
/// \brief Callback when "save configuration as" is selected
public slots: void OnSaveConfigAs(const QString &_path);

/// \brief Callback when "shutdown simulation" is called
public slots: void OnStopServer();

/// \brief Notifies when the number of plugins has changed.
signals: void PluginCountChanged();

Expand Down Expand Up @@ -414,9 +541,27 @@ namespace ignition
/// \brief Notifies when the show menu flag has changed.
signals: void ShowPluginMenuChanged();

/// \brief Notifies when the defaultExitAction has changed.
signals: void DefaultExitActionChanged();

/// \brief Notifies when the showDialogOnExit flag has changed.
signals: void ShowDialogOnExitChanged();

/// \brief Notifies when dialogOnExitText has changed.
signals: void DialogOnExitTextChanged();

/// \brief Notifies when the exitDialogShowShutdown flag has changed.
signals: void ExitDialogShowShutdownChanged();

/// \brief Notifies when the exitDialogShowCloseGui flag has changed.
signals: void ExitDialogShowCloseGuiChanged();

/// \brief Notifies when exitDialogShutdownText has changed.
signals: void ExitDialogShutdownTextChanged();

/// \brief Notifies when exitDialogCloseGuiText has changed.
signals: void ExitDialogCloseGuiTextChanged();

/// \brief Notifies when the window config has changed.
signals: void configChanged();

Expand Down
67 changes: 60 additions & 7 deletions include/ignition/gui/qml/Main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import QtQuick.Controls 2.2
import QtQuick.Controls.Material 2.1
import QtQuick.Dialogs 1.0
import QtQuick.Layouts 1.3
import ExitAction 1.0
import "qrc:/qml"

ApplicationWindow
Expand All @@ -45,7 +46,19 @@ ApplicationWindow
property string pluginToolBarTextColorLight: MainWindow.pluginToolBarTextColorLight
property string pluginToolBarColorDark: MainWindow.pluginToolBarColorDark
property string pluginToolBarTextColorDark: MainWindow.pluginToolBarTextColorDark
// Expose config properties to C++
property int defaultExitAction: MainWindow.defaultExitAction
property bool showDialogOnExit: MainWindow.showDialogOnExit
property string dialogOnExitText: MainWindow.dialogOnExitText
property bool exitDialogShowShutdown: MainWindow.exitDialogShowShutdown
property bool exitDialogShowCloseGui: MainWindow.exitDialogShowCloseGui
property string exitDialogShutdownText: MainWindow.exitDialogShutdownText
property string exitDialogCloseGuiText: MainWindow.exitDialogCloseGuiText
/**
* Flag to indicate if the close event was triggered by the close dialog.
*/
property bool closingFromDialog: false

/**
* Tool bar background color
*/
Expand Down Expand Up @@ -75,7 +88,13 @@ ApplicationWindow
onClosing: {
close.accepted = !showDialogOnExit
if(showDialogOnExit){
confirmationDialogOnExit.open()
if (closingFromDialog) {
close.accepted = true;
} else {
confirmationDialogOnExit.open()
}
} else if (defaultExitAction == ExitAction.SHUTDOWN_SERVER) {
MainWindow.OnStopServer()
}
}

Expand Down Expand Up @@ -325,24 +344,58 @@ ApplicationWindow
}
}

Timer {
id: timer
}

/**
* Confirmation dialog on close button
*/
Dialog {
id: confirmationDialogOnExit
title: "Do you really want to exit?"
title: (dialogOnExitText ? dialogOnExitText : "Do you really want to exit?")
objectName: "confirmationDialogOnExit"

modal: true
focus: true
parent: ApplicationWindow.overlay
width: 300
width: 500
x: (parent.width - width) / 2
y: (parent.height - height) / 2
closePolicy: Popup.CloseOnEscape
standardButtons: Dialog.Ok | Dialog.Cancel

onAccepted: {
Qt.quit()
standardButtons:
(exitDialogShowCloseGui ? Dialog.Ok : Dialog.NoButton) |
(exitDialogShowShutdown ? Dialog.Discard : Dialog.NoButton) |
Dialog.Cancel

// The button texts need to be changed later than in onCompleted as standardButtons change later
onAboutToShow: function () {
if (exitDialogShowCloseGui && exitDialogCloseGuiText)
footer.standardButton(Dialog.Ok).text = exitDialogCloseGuiText
if (exitDialogShowShutdown)
footer.standardButton(Dialog.Discard).text =
(exitDialogShutdownText ? exitDialogShutdownText : "Shutdown server and GUI")
}

footer:
DialogButtonBox {
onClicked: function (btn) {
if (btn == this.standardButton(Dialog.Ok)) {
closingFromDialog = true;
window.close();
}
else if (btn == this.standardButton(Dialog.Discard)) {
MainWindow.OnStopServer()
// if GUI and server run in the same process, give server opportunity to kill the GUI
timer.interval = 100;
timer.repeat = false;
timer.triggered.connect(function() {
closingFromDialog = true;
window.close();
});
timer.start();
}
}
}
}
}
Loading

0 comments on commit 85b539c

Please sign in to comment.