diff --git a/examples/config/layout.config b/examples/config/layout.config index 3f6ac5aab..6d3446a5f 100644 --- a/examples/config/layout.config +++ b/examples/config/layout.config @@ -6,6 +6,7 @@ 230 550 551 + true 1 diff --git a/include/ignition/gui/MainWindow.hh b/include/ignition/gui/MainWindow.hh index 6953ba246..ea956e21e 100644 --- a/include/ignition/gui/MainWindow.hh +++ b/include/ignition/gui/MainWindow.hh @@ -44,7 +44,7 @@ namespace ignition /// \brief The main window class creates a QQuickWindow and acts as an /// interface which provides properties and functions which can be called - /// from MainWindow.qml + /// from Main.qml class IGNITION_GUI_VISIBLE MainWindow : public QObject { Q_OBJECT @@ -177,6 +177,14 @@ namespace ignition NOTIFY ShowPluginMenuChanged ) + /// \brief Flag to enable confirmation dialog on exit + Q_PROPERTY( + bool showDialogOnExit + READ ShowDialogOnExit + WRITE SetShowDialogOnExit + NOTIFY ShowDialogOnExitChanged + ) + /// \brief Constructor public: MainWindow(); @@ -344,6 +352,14 @@ namespace ignition /// \param[in] _showPluginMenu True to show. public: Q_INVOKABLE void SetShowPluginMenu(const bool _showPluginMenu); + /// \brief Get the flag to show the plugin menu. + /// \return True to show. + public: Q_INVOKABLE bool ShowDialogOnExit() const; + + /// \brief Set the flag to show the confirmation dialog when exiting. + /// \param[in] _showDialogOnExit True to show. + public: Q_INVOKABLE void SetShowDialogOnExit(bool _showDialogOnExit); + /// \brief Callback when load configuration is selected public slots: void OnLoadConfig(const QString &_path); @@ -398,6 +414,9 @@ namespace ignition /// \brief Notifies when the show menu flag has changed. signals: void ShowPluginMenuChanged(); + /// \brief Notifies when the showDialogOnExit flag has changed. + signals: void ShowDialogOnExitChanged(); + /// \brief Notifies when the window config has changed. signals: void configChanged(); diff --git a/include/ignition/gui/qml/Main.qml b/include/ignition/gui/qml/Main.qml index c914f0c0d..ec75de916 100644 --- a/include/ignition/gui/qml/Main.qml +++ b/include/ignition/gui/qml/Main.qml @@ -45,7 +45,7 @@ ApplicationWindow property string pluginToolBarTextColorLight: MainWindow.pluginToolBarTextColorLight property string pluginToolBarColorDark: MainWindow.pluginToolBarColorDark property string pluginToolBarTextColorDark: MainWindow.pluginToolBarTextColorDark - + property bool showDialogOnExit: MainWindow.showDialogOnExit /** * Tool bar background color */ @@ -71,6 +71,14 @@ ApplicationWindow titleLabel.text = window.title } + // Handler for window closing + onClosing: { + close.accepted = !showDialogOnExit + if(showDialogOnExit){ + confirmationDialogOnExit.open() + } + } + // C++ signals to QML slots Connections { target: MainWindow @@ -315,4 +323,25 @@ ApplicationWindow } } } + + /** + * Confirmation dialog on close button + */ + Dialog { + id: confirmationDialogOnExit + title: "Do you really want to exit?" + + modal: true + focus: true + parent: ApplicationWindow.overlay + width: 300 + x: (parent.width - width) / 2 + y: (parent.height - height) / 2 + closePolicy: Popup.CloseOnEscape + standardButtons: Dialog.Ok | Dialog.Cancel + + onAccepted: { + Qt.quit() + } + } } diff --git a/src/Application.cc b/src/Application.cc index 3e11543ef..ea7f2aab2 100644 --- a/src/Application.cc +++ b/src/Application.cc @@ -273,6 +273,14 @@ bool Application::LoadConfig(const std::string &_config) return false; } this->dataPtr->windowConfig.MergeFromXML(std::string(printer.CStr())); + + // Closing behavior. + if (auto dialogOnExitElem = winElem->FirstChildElement("dialog_on_exit")) + { + bool showDialogOnExit{false}; + dialogOnExitElem->QueryBoolText(&showDialogOnExit); + this->dataPtr->mainWin->SetShowDialogOnExit(showDialogOnExit); + } } this->ApplyConfig(); diff --git a/src/MainWindow.cc b/src/MainWindow.cc index a0a40d0ca..d7d62c597 100644 --- a/src/MainWindow.cc +++ b/src/MainWindow.cc @@ -47,6 +47,9 @@ namespace ignition /// \brief Minimum number of paint events to consider the window to be /// fully initialized. public: const unsigned int paintCountMin{20}; + + /// \brief Show the confirmation dialog on exit + public: bool showDialogOnExit{false}; }; } } @@ -843,3 +846,16 @@ void MainWindow::SetShowPluginMenu(const bool _showPluginMenu) this->dataPtr->windowConfig.showPluginMenu = _showPluginMenu; this->ShowPluginMenuChanged(); } + +///////////////////////////////////////////////// +bool MainWindow::ShowDialogOnExit() const +{ + return this->dataPtr->showDialogOnExit; +} + +///////////////////////////////////////////////// +void MainWindow::SetShowDialogOnExit(bool _showDialogOnExit) +{ + this->dataPtr->showDialogOnExit = _showDialogOnExit; + this->ShowDialogOnExitChanged(); +} diff --git a/test/config/test.config b/test/config/test.config index 706a1ad50..dd3b76e65 100644 --- a/test/config/test.config +++ b/test/config/test.config @@ -1,4 +1,8 @@ + + false + + diff --git a/tutorials/04_layout.md b/tutorials/04_layout.md index 6d04697e1..6ad02c358 100644 --- a/tutorials/04_layout.md +++ b/tutorials/04_layout.md @@ -28,6 +28,7 @@ by adding a `` element to the config file. The child elements are: the menu. If `from_paths` is true, all plugins will be shown anyway, so adding `` has no effect. For the plugin to be shown, it must be on the path. +* ``: If true, a confirmation dialog will show up when closing the window. ## Example layout