summaryrefslogtreecommitdiff
path: root/Applications/Spreadsheet
diff options
context:
space:
mode:
authorXavier Cooney <xavier.cooney03@gmail.com>2020-12-22 16:15:15 +1100
committerAndreas Kling <kling@serenityos.org>2020-12-23 15:41:49 +0100
commit5f58fe16435ff27a0b2a396dab42a75a893d3e2c (patch)
tree0783c897e1d6b1fa4723caaa334eeec4f327265a /Applications/Spreadsheet
parent23febb9d8e035948c4443da5bb4075764b07aa6e (diff)
downloadserenity-5f58fe16435ff27a0b2a396dab42a75a893d3e2c.zip
Spreadsheet: Prompt user before closing with unsaved changes
Diffstat (limited to 'Applications/Spreadsheet')
-rw-r--r--Applications/Spreadsheet/Spreadsheet.cpp4
-rw-r--r--Applications/Spreadsheet/SpreadsheetWidget.cpp28
-rw-r--r--Applications/Spreadsheet/SpreadsheetWidget.h1
-rw-r--r--Applications/Spreadsheet/Workbook.cpp1
-rw-r--r--Applications/Spreadsheet/Workbook.h3
-rw-r--r--Applications/Spreadsheet/main.cpp8
6 files changed, 44 insertions, 1 deletions
diff --git a/Applications/Spreadsheet/Spreadsheet.cpp b/Applications/Spreadsheet/Spreadsheet.cpp
index 4a9e57c2f2..f0f28b3e80 100644
--- a/Applications/Spreadsheet/Spreadsheet.cpp
+++ b/Applications/Spreadsheet/Spreadsheet.cpp
@@ -163,8 +163,10 @@ void Sheet::update()
// Grab a copy as updates might insert cells into the table.
for (auto& it : m_cells) {
- if (it.value->dirty())
+ if (it.value->dirty()) {
cells_copy.append(it.value);
+ m_workbook.set_dirty(true);
+ }
}
for (auto& cell : cells_copy)
diff --git a/Applications/Spreadsheet/SpreadsheetWidget.cpp b/Applications/Spreadsheet/SpreadsheetWidget.cpp
index 5734b888f0..6e94ed4093 100644
--- a/Applications/Spreadsheet/SpreadsheetWidget.cpp
+++ b/Applications/Spreadsheet/SpreadsheetWidget.cpp
@@ -31,6 +31,7 @@
#include <LibCore/File.h>
#include <LibGUI/BoxLayout.h>
#include <LibGUI/Button.h>
+#include <LibGUI/FilePicker.h>
#include <LibGUI/Label.h>
#include <LibGUI/Menu.h>
#include <LibGUI/MessageBox.h>
@@ -216,6 +217,33 @@ void SpreadsheetWidget::load(const StringView& filename)
setup_tabs(m_workbook->sheets());
}
+bool SpreadsheetWidget::request_close()
+{
+ if (!m_workbook->dirty())
+ return true;
+
+ auto result = GUI::MessageBox::show(window(), "The spreadsheet has been modified. Would you like to save?", "Unsaved changes", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::YesNoCancel);
+
+ if (result == GUI::MessageBox::ExecYes) {
+ if (current_filename().is_empty()) {
+ String name = "workbook";
+ Optional<String> save_path = GUI::FilePicker::get_save_filepath(window(), name, "sheets");
+ if (!save_path.has_value())
+ return false;
+
+ save(save_path.value());
+ } else {
+ save(current_filename());
+ }
+ return true;
+ }
+
+ if (result == GUI::MessageBox::ExecNo)
+ return true;
+
+ return false;
+}
+
void SpreadsheetWidget::add_sheet()
{
StringBuilder name;
diff --git a/Applications/Spreadsheet/SpreadsheetWidget.h b/Applications/Spreadsheet/SpreadsheetWidget.h
index 10eb908da0..c13531fc2a 100644
--- a/Applications/Spreadsheet/SpreadsheetWidget.h
+++ b/Applications/Spreadsheet/SpreadsheetWidget.h
@@ -41,6 +41,7 @@ public:
void save(const StringView& filename);
void load(const StringView& filename);
+ bool request_close();
void add_sheet();
void add_sheet(NonnullRefPtr<Sheet>&&);
diff --git a/Applications/Spreadsheet/Workbook.cpp b/Applications/Spreadsheet/Workbook.cpp
index ca0f932f76..db5bf16cb5 100644
--- a/Applications/Spreadsheet/Workbook.cpp
+++ b/Applications/Spreadsheet/Workbook.cpp
@@ -183,6 +183,7 @@ Result<bool, String> Workbook::save(const StringView& filename)
}
set_filename(filename);
+ set_dirty(false);
return true;
}
diff --git a/Applications/Spreadsheet/Workbook.h b/Applications/Spreadsheet/Workbook.h
index c03fbfa5ab..63c016d501 100644
--- a/Applications/Spreadsheet/Workbook.h
+++ b/Applications/Spreadsheet/Workbook.h
@@ -42,6 +42,8 @@ public:
const String& current_filename() const { return m_current_filename; }
bool set_filename(const String& filename);
+ bool dirty() { return m_dirty; }
+ void set_dirty(bool dirty) { m_dirty = dirty; }
bool has_sheets() const { return !m_sheets.is_empty(); }
@@ -70,6 +72,7 @@ private:
WorkbookObject* m_workbook_object { nullptr };
String m_current_filename;
+ bool m_dirty { false };
};
}
diff --git a/Applications/Spreadsheet/main.cpp b/Applications/Spreadsheet/main.cpp
index c9ab4321bb..0bfde24459 100644
--- a/Applications/Spreadsheet/main.cpp
+++ b/Applications/Spreadsheet/main.cpp
@@ -114,9 +114,17 @@ int main(int argc, char* argv[])
app_menu.add_separator();
app_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) {
+ if (!spreadsheet_widget.request_close())
+ return;
app->quit(0);
}));
+ window->on_close_request = [&]() -> GUI::Window::CloseRequestDecision {
+ if (spreadsheet_widget.request_close())
+ return GUI::Window::CloseRequestDecision::Close;
+ return GUI::Window::CloseRequestDecision::StayOpen;
+ };
+
auto& file_menu = menubar->add_menu("File");
file_menu.add_action(GUI::CommonActions::make_open_action([&](auto&) {
Optional<String> load_path = GUI::FilePicker::get_open_filepath(window);