Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confirmation dialog when closing main window #225

Merged
merged 5 commits into from
Jun 3, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
29 changes: 29 additions & 0 deletions include/ignition/gui/qml/Main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ ApplicationWindow
titleLabel.text = window.title
}

// Handler for window closing
onClosing: {
close.accepted = !MainWindow.showDialogOnExit
if(MainWindow.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 @@ -275,6 +275,14 @@ bool Application::LoadConfig(const std::string &_config)
this->dataPtr->windowConfig.MergeFromXML(std::string(printer.CStr()));
}

// Closing behavior.
if (auto dialogOnExitElem = doc.FirstChildElement("dialog_on_exit"))
{
bool showDialogOnExit{false};
dialogOnExitElem->QueryBoolText(&showDialogOnExit);
this->dataPtr->mainWin->SetShowDialogOnExit(showDialogOnExit);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the previous behavior where <dialog_on_exit> was in <window> was better. You can move this section to the above if (auto winElem = doc.FirstChildElement("window")) and then update this if to if (auto dialogOnExitElem = winElem->FirstChildElement("dialog_on_exit"))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, from a user perspective it is better to have it within the <window> node.
Done!


this->ApplyConfig();

return true;
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();
}