Skip to content

Commit

Permalink
Implemented "Are you sure?" dialog when closing a tool.
Browse files Browse the repository at this point in the history
  • Loading branch information
Frodo45127 committed Oct 9, 2024
1 parent b41a54c commit 51c57c9
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 14 deletions.
Binary file modified 3rdparty/builds/qt_rpfm_extensions.lib
Binary file not shown.
26 changes: 26 additions & 0 deletions 3rdparty/src/qt_rpfm_extensions/include/q_dialog_custom.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef QDIALOGCUSTOM_H
#define QDIALOGCUSTOM_H

#include <QDialog>
#include <QCloseEvent>
#include <QMoveEvent>
#include <QEvent>
#include <QMessageBox>
#include <QSettings>
#include <KBusyIndicatorWidget>

extern "C" QDialog* new_q_dialog_custom(QWidget *parent = nullptr, bool (*are_you_sure)(QDialog* dialog) = nullptr);

class QDialogCustom : public QDialog
{
Q_OBJECT
public:
explicit QDialogCustom(QWidget *parent = nullptr, bool (*are_you_sure)(QDialog* dialog) = nullptr);
void closeEvent(QCloseEvent *event) override;

private:
bool (*are_you_sure)(QDialog* dialog);

};

#endif // QDIALOGCUSTOM_H
6 changes: 4 additions & 2 deletions 3rdparty/src/qt_rpfm_extensions/qt_rpfm_extensions.pro
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ SOURCES += \
src/tree_item_delegate.cpp \
src/treeview_draggable.cpp \
src/unit_variant_item_delegate.cpp \
src/treeview_filter.cpp
src/treeview_filter.cpp \
src/q_dialog_custom.cpp

INCLUDEPATH += include

Expand Down Expand Up @@ -83,7 +84,8 @@ HEADERS += \
include/q_main_window_custom.h \
include/spoiler.h \
include/kcolor_combo.h \
include/unit_variant_item_delegate.h
include/unit_variant_item_delegate.h \
include/q_dialog_custom.h

release:DESTDIR = release
release:OBJECTS_DIR = release/.obj
Expand Down
25 changes: 25 additions & 0 deletions 3rdparty/src/qt_rpfm_extensions/src/q_dialog_custom.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "q_dialog_custom.h"
#include <QApplication>
#include <QDebug>
#include <QFileInfo>
#include <QIcon>
#include <QResource>
#include <QMimeData>

// Fuction to be able to create a custom QMainWindow.
extern "C" QDialog* new_q_dialog_custom(QWidget *parent, bool (*are_you_sure) (QDialog* dialog)) {
return dynamic_cast<QDialog*>(new QDialogCustom(parent, are_you_sure));
}

QDialogCustom::QDialogCustom(QWidget *parent, bool (*are_you_sure_fn) (QDialog* dialog)) : QDialog(parent) {
are_you_sure = are_you_sure_fn;
}

// Overload of the close event so we can put a dialog there.
void QDialogCustom::closeEvent(QCloseEvent *event) {
event->ignore();

if (are_you_sure(this)) {
event->accept();
}
}
1 change: 1 addition & 0 deletions locale/English_en.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,7 @@ game_selected_changed_on_opening = Game Selected changed to {"{"}{"}"}, as the P
rpfm_title = Rusted PackFile Manager
delete_mymod_0 = <p>Are you sure you want to delete this <i>'MyMod'</i> from your disk?</p><p>There is no way to recover it after that.</p>
delete_mymod_1 = <p>There are some changes yet to be saved.</p><p>Are you sure you want to exit?</p>
close_tool = <p>Are you sure you want to close the tool?</p><p>Changes will be lost.</p>
api_response_success_new_stable_update = <h4>New major stable update found: {"{"}{"}"}</h4> <p>Please, make sure to save your work in progress before hitting 'Update', or you may lose it.</p>
api_response_success_new_beta_update = <h4>New beta update found: {"{"}{"}"}</h4><p>Please, make sure to save your work in progress before hitting 'Update', or you may lose it.</p>
Expand Down
31 changes: 29 additions & 2 deletions rpfm_ui/src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Module containing the ffi functions used for custom widgets.

use qt_widgets::QAbstractSpinBox;
use qt_widgets::QAction;
use qt_widgets::QDialog;
use qt_widgets::QLabel;
use qt_widgets::QLayout;
use qt_widgets::QLineEdit;
Expand Down Expand Up @@ -235,6 +236,12 @@ pub fn main_window_drop_pack_signal(widget: QPtr<QWidget>) -> Signal<(*const ::q
}
}

// This function allow us to create a custom dialog.
extern "C" { fn new_q_dialog_custom(parent: *mut QWidget, are_you_sure_dialog: extern fn(*mut QDialog) -> bool) -> *mut QDialog; }
pub fn new_q_dialog_custom_safe(parent: Ptr<QWidget>, are_you_sure_dialog: extern fn(*mut QDialog) -> bool) -> QBox<QDialog> {
unsafe { QBox::from_raw(new_q_dialog_custom(parent.as_mut_raw_ptr(), are_you_sure_dialog)) }
}

//---------------------------------------------------------------------------//
// i64 Spinbox stuff.
//---------------------------------------------------------------------------//
Expand Down Expand Up @@ -689,8 +696,11 @@ pub extern fn anim_paths_by_skeleton_callback(skeleton_name: *mut QString, out:
/// This function allow us to create a dialog when trying to close the main window.
pub extern fn are_you_sure(main_window: *mut QMainWindow, is_delete_my_mod: bool) -> bool {
let title = qtr("rpfm_title");
let message = if is_delete_my_mod { qtr("delete_mymod_0") }
else if UI_STATE.get_is_modified() { qtr("delete_mymod_1") }
let message = if is_delete_my_mod {
qtr("delete_mymod_0")
} else if UI_STATE.get_is_modified() {
qtr("delete_mymod_1")
}

// In any other situation... just return true and forget about the dialog.
else { return true };
Expand All @@ -707,6 +717,23 @@ pub extern fn are_you_sure(main_window: *mut QMainWindow, is_delete_my_mod: bool
).exec() == 3 }
}

/// This function allow us to create a dialog when trying to close another dialog.
pub extern fn are_you_sure_dialog(dialog: *mut QDialog) -> bool {
let title = qtr("rpfm_title");
let message = qtr("close_tool");

// Create the dialog and run it (Yes => 3, No => 4).
unsafe { QMessageBox::from_2_q_string_icon3_int_q_widget(
&title,
&message,
q_message_box::Icon::Warning,
65536, // No
16384, // Yes
1, // By default, select yes.
dialog,
).exec() == 3 }
}

pub extern fn generate_tooltip_message(view: *mut QTableView, global_pos_x: i32, global_pos_y: i32) {
unsafe {
let view = view.as_ref().unwrap();
Expand Down
15 changes: 10 additions & 5 deletions rpfm_ui/src/tools/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ pub mod unit_editor;
#[getset(get = "pub", get_mut = "pub")]
pub struct Tool {

/// Main widget of the tool, built from a Template. Usually, the dialog.
/// The dialog of the tool.
dialog: QBox<QDialog>,

/// Main widget of the tool, built from a Template.
main_widget: QBox<QWidget>,

/// Paths which the tool requires data from.
Expand Down Expand Up @@ -180,8 +183,9 @@ impl Tool {
_ => panic!("{THREADS_COMMUNICATION_ERROR}{response:?}"),
}

// Load the UI Template.
let main_widget = crate::utils::load_template(parent, template_path)?;
// Load the UI Template. All templates must be simple widgets.
let dialog = new_q_dialog_custom_safe(parent.cast_into(), are_you_sure_dialog);
let main_widget = crate::utils::load_template(&dialog, template_path)?;

// Get the common widgets for all tools.
let message_widget: QPtr<QWidget> = Self::find_widget_no_tool(&main_widget.static_upcast(), "message_widget")?;
Expand All @@ -195,6 +199,7 @@ impl Tool {

// Then, build the tool.
Ok(Self{
dialog,
main_widget,
used_paths,
packed_files: Rc::new(RefCell::new(HashMap::new())),
Expand All @@ -204,8 +209,8 @@ impl Tool {
}

/// This function returns the main widget casted as a QDialog, which should be the type of the widget defined in the UI Template.
pub unsafe fn get_ref_dialog(&self) -> qt_core::QPtr<QDialog> {
self.main_widget.static_downcast::<QDialog>()
pub unsafe fn get_ref_dialog(&self) -> &QBox<QDialog> {
&self.dialog
}

/// This function sets the title of the Tool's window.
Expand Down
2 changes: 1 addition & 1 deletion rpfm_ui/src/tools/unit_editor/variant_unit_editor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ impl SubToolVariantUnitEditor {
view.load_data(data)?;

// If we hit ok, save the data back to the parent tool.
if view.tool.main_widget().static_downcast::<QDialog>().exec() == 1 {
if view.tool.dialog().exec() == 1 {
Ok(Some(view.save_data()?))
}

Expand Down
2 changes: 1 addition & 1 deletion rpfm_ui/ui_templates/tool_faction_color_editor.ui
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<widget class="QWidget" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
Expand Down
2 changes: 1 addition & 1 deletion rpfm_ui/ui_templates/tool_translator_editor.ui
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<widget class="QWidget" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
Expand Down
2 changes: 1 addition & 1 deletion rpfm_ui/ui_templates/tool_unit_editor.ui
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<widget class="QWidget" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
Expand Down
2 changes: 1 addition & 1 deletion rpfm_ui/ui_templates/tool_unit_editor_variant_editor.ui
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>variant_editor_dialog</class>
<widget class="QDialog" name="variant_editor_dialog">
<widget class="QWidget" name="variant_editor_dialog">
<property name="geometry">
<rect>
<x>0</x>
Expand Down

0 comments on commit 51c57c9

Please sign in to comment.