Skip to content

Commit

Permalink
Confirmation dialog when closing main window (#225)
Browse files Browse the repository at this point in the history
* Adds confirmation dialog when closing window
* Updates docs and extends test coverage.
* Adds dialog_on_exit atribute to example .config

Signed-off-by: Franco Cipollone <[email protected]>
  • Loading branch information
francocipollone authored Jun 3, 2021
1 parent bf50949 commit dd80dc9
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 2 deletions.
1 change: 1 addition & 0 deletions examples/config/layout.config
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<position_y>230</position_y>
<width>550</width>
<height>551</height>
<dialog_on_exit>true</dialog_on_exit>
</window>
<plugin filename="Publisher">
<title>1</title>
Expand Down
21 changes: 20 additions & 1 deletion include/ignition/gui/MainWindow.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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();

Expand Down
31 changes: 30 additions & 1 deletion include/ignition/gui/qml/Main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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
Expand Down Expand Up @@ -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()
}
}
}
8 changes: 8 additions & 0 deletions src/Application.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
16 changes: 16 additions & 0 deletions src/MainWindow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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};
};
}
}
Expand Down Expand Up @@ -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();
}
4 changes: 4 additions & 0 deletions test/config/test.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<?xml version="1.0"?>

<window>
<dialog_on_exit>false</dialog_on_exit>
</window>

<plugin filename="TestPlugin">
</plugin>
1 change: 1 addition & 0 deletions tutorials/04_layout.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ by adding a `<window>` element to the config file. The child elements are:
the menu. If `from_paths` is true, all plugins will be shown
anyway, so adding `<show>` has no effect. For the plugin to
be shown, it must be on the path.
* `<dialog_on_exit>`: If true, a confirmation dialog will show up when closing the window.

## Example layout

Expand Down

0 comments on commit dd80dc9

Please sign in to comment.