aboutsummaryrefslogtreecommitdiffstats
path: root/tools/materialeditor/SaveChangesDialog.qml
blob: 6e4527ada65cc301297712b93a1a0aae975cb330 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

import QtQuick
import QtQuick.Controls
import QtQuick3D.MaterialEditor

Dialog {
    id: root
    title: qsTr("Unsaved changes")
    modal: true

    required property MaterialAdapter materialAdapter
    required property var saveAsDialog

    function doIfChangesSavedOrDiscarded(actionFunction) {
        if (!materialAdapter.unsavedChanges) {
            actionFunction()
            return
        }

        // There are unsaved changes, so we need to prompt.

        function disconnectSaveChangesSignals() {
            root.accepted.disconnect(saveChanges)
            root.discarded.disconnect(discardChanges)
            root.rejected.disconnect(cancel)
        }

        function saveChanges() {
            if (materialAdapter.materialSaveFile.toString().length > 0) {
                // Existing project; can save without a dialog.
                if (materialAdapter.save()) {
                    // Saved successfully, so now we can perform the action.
                    performAction()
                } else {
                    // Failed to save; cancel.
                    cancel()
                }
            } else {
                // New project; need to save as.
                function disconnectSaveAsSignals() {
                    materialAdapter.errorOccurred.disconnect(saveAsFailed)
                    materialAdapter.postMaterialSaved.disconnect(saveAsSucceeded)
                    saveAsDialog.rejected.disconnect(saveAsDialogRejected)
                }

                function saveAsSucceeded() {
                    disconnectSaveAsSignals()
                    performAction()
                }

                function saveAsFailed() {
                    disconnectSaveAsSignals()
                    disconnectSaveChangesSignals()
                }

                function saveAsDialogRejected() {
                    disconnectSaveAsSignals()
                    cancel()
                }

                materialAdapter.errorOccurred.connect(saveAsFailed)
                materialAdapter.postMaterialSaved.connect(saveAsSucceeded)
                saveAsDialog.rejected.connect(saveAsDialogRejected)

                saveAsDialog.open()
            }
        }

        function discardChanges() {
            performAction()
            root.close()
        }

        function performAction() {
            disconnectSaveChangesSignals()
            actionFunction()
        }

        function cancel() {
            disconnectSaveChangesSignals()
        }

        root.accepted.connect(saveChanges)
        root.discarded.connect(discardChanges)
        root.rejected.connect(cancel)
        root.open()
    }

    Label {
        text: qsTr("Save changes to the material before closing?")
    }

    // Using a DialogButtonBox allows us to assign objectNames to the buttons,
    // which makes it possible to test them.
    footer: DialogButtonBox {
        Button {
            objectName: "cancelDialogButton"
            text: qsTr("Cancel")
            DialogButtonBox.buttonRole: DialogButtonBox.RejectRole
        }
        Button {
            objectName: "saveChangesDialogButton"
            text: qsTr("Save")
            DialogButtonBox.buttonRole: DialogButtonBox.AcceptRole
        }
        Button {
            objectName: "discardChangesDialogButton"
            text: qsTr("Don't save")
            DialogButtonBox.buttonRole: DialogButtonBox.DestructiveRole
        }
    }
}