-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathOpenSaveProjectFileScenario.cpp
More file actions
164 lines (141 loc) · 7.05 KB
/
OpenSaveProjectFileScenario.cpp
File metadata and controls
164 lines (141 loc) · 7.05 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "OpenSaveProjectFileScenario.h"
#include "OpenSaveProjectFileScenario_p.h"
#include <QApplication>
#include <QFileDialog>
#include <QQmlComponent>
#include <QQuickWindow>
#include <QSettings>
#include <QStandardPaths>
#include <QTimer>
#include <QWindow>
#include <QtQuickTemplates2/private/qquickbutton_p.h>
#include <CoreApi/runtimeinterface.h>
#include <SVSCraftQuick/MessageBox.h>
#include <coreplugin/CoreInterface.h>
#include <coreplugin/DspxInspectorDialog.h>
namespace Core {
OpenSaveProjectFileScenario::OpenSaveProjectFileScenario(QObject *parent) : QObject(parent), d_ptr(new OpenSaveProjectFileScenarioPrivate) {
Q_D(OpenSaveProjectFileScenario);
d->q_ptr = this;
}
OpenSaveProjectFileScenario::~OpenSaveProjectFileScenario() = default;
QWindow *OpenSaveProjectFileScenario::window() const {
Q_D(const OpenSaveProjectFileScenario);
return d->window;
}
void OpenSaveProjectFileScenario::setWindow(QWindow *window) {
Q_D(OpenSaveProjectFileScenario);
if (d->window != window) {
d->window = window;
Q_EMIT windowChanged();
}
}
QString OpenSaveProjectFileScenario::dspxFileFilter(bool withAllFiles) {
auto dspxFileFilter = tr("DiffScope Project Exchange Format (*.dspx)");
auto allFileFilter = tr("All Files (*)");
return withAllFiles ? dspxFileFilter + ";;" + allFileFilter : dspxFileFilter;
}
QString OpenSaveProjectFileScenario::openProjectFile(const QString &defaultDir) const {
Q_D(const OpenSaveProjectFileScenario);
Q_UNUSED(d->window);
auto settings = RuntimeInterface::settings();
settings->beginGroup(staticMetaObject.className());
auto defaultOpenDir = !defaultDir.isEmpty() ? defaultDir : settings->value(QStringLiteral("defaultOpenDir"), QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)).toString();
settings->endGroup();
auto path = QFileDialog::getOpenFileName(
nullptr,
{},
defaultOpenDir,
dspxFileFilter(true)
);
if (path.isEmpty())
return {};
settings->beginGroup(staticMetaObject.className());
settings->setValue(QStringLiteral("defaultOpenDir"), QFileInfo(path).absolutePath());
settings->endGroup();
return path;
}
QString OpenSaveProjectFileScenario::saveProjectFile(const QString &defaultDir) const {
Q_D(const OpenSaveProjectFileScenario);
Q_UNUSED(d->window);
auto settings = RuntimeInterface::settings();
settings->beginGroup(staticMetaObject.className());
auto defaultSaveDir = !defaultDir.isEmpty() ? defaultDir : settings->value(QStringLiteral("defaultSaveDir"), QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)).toString();
settings->endGroup();
auto path = QFileDialog::getSaveFileName(
nullptr,
{},
defaultSaveDir,
dspxFileFilter(true)
);
if (path.isEmpty())
return {};
settings->beginGroup(staticMetaObject.className());
settings->setValue(QStringLiteral("defaultSaveDir"), QFileInfo(path).absolutePath());
settings->endGroup();
return path;
}
void OpenSaveProjectFileScenario::showOpenFailMessageBox(const QString &path, const QString &error) const {
Q_D(const OpenSaveProjectFileScenario);
SVS::MessageBox::critical(RuntimeInterface::qmlEngine(), d->window, tr("Failed to open file"), QStringLiteral("%1\n\n%2").arg(QDir::toNativeSeparators(path), error));
}
void OpenSaveProjectFileScenario::showSaveFailMessageBox(const QString &path, const QString &error) const {
Q_D(const OpenSaveProjectFileScenario);
SVS::MessageBox::critical(RuntimeInterface::qmlEngine(), d->window, tr("Failed to save file"), QStringLiteral("%1\n\n%2").arg(QDir::toNativeSeparators(path), error));
}
void OpenSaveProjectFileScenario::showDeserializationFailMessageBox(const QString &path) const {
Q_D(const OpenSaveProjectFileScenario);
QQmlComponent buttonComponent(RuntimeInterface::qmlEngine(), "SVSCraft.UIComponents", "Button");
auto button = qobject_cast<QQuickButton *>(buttonComponent.create());
Q_ASSERT(button);
button->setText(tr("Open DSPX Inspector"));
QQmlComponent component(RuntimeInterface::qmlEngine(), "SVSCraft.UIComponents", "MessageBoxDialog");
std::unique_ptr<QQuickWindow> mb(qobject_cast<QQuickWindow *>(component.createWithInitialProperties(
{{"text", tr("Failed to parse file content")},
{"informativeText", tr("%1\n\nYou can check for problems in the file with DSPX Inspector.").arg(QDir::toNativeSeparators(path))},
{"icon", SVS::SVSCraft::Critical},
{"transientParent", QVariant::fromValue(d->window)},
{"content", QVariant::fromValue(button)}}
)));
Q_ASSERT(mb);
connect(button, &QQuickButton::clicked, [this, path, &mb] {
mb->close();
QTimer::singleShot(0, [this, path] {
DspxInspectorDialog dialog;
dialog.setPath(path);
dialog.runCheck();
dialog.exec();
});
});
SVS::MessageBox::customExec(mb.get());
}
bool OpenSaveProjectFileScenario::confirmFileCreatedByAnotherApplication(const QString &name) const {
Q_D(const OpenSaveProjectFileScenario);
return SVS::MessageBox::question(
RuntimeInterface::qmlEngine(),
d->window,
tr("File created with another application"),
QStringLiteral("This project file was created with another application (%1). Some features may not be fully compatible or may behave differently.\n\nDo you want to continue opening it?")
.arg(name.isEmpty() ? tr("name unknown") : name)
) == SVS::SVSCraft::Yes;
}
bool OpenSaveProjectFileScenario::confirmFileCreatedByIncompatibleVersion(const QString &version) const {
Q_D(const OpenSaveProjectFileScenario);
return SVS::MessageBox::question(
RuntimeInterface::qmlEngine(),
d->window,
tr("File created with incompatible %1 version").arg(QApplication::applicationDisplayName()),
QStringLiteral("This project file was created with an newer version or test version of %1 (%2). Some features may not be fully compatible or may behave differently.\n\nDo you want to continue opening it?")
.arg(QApplication::applicationDisplayName(), version)
) == SVS::SVSCraft::Yes;
}
bool OpenSaveProjectFileScenario::confirmCustomCheckWarning(const QString &message) const {
Q_D(const OpenSaveProjectFileScenario);
return SVS::MessageBox::question(
RuntimeInterface::qmlEngine(),
d->window,
tr("Additional check failed"),
QStringLiteral("%1\n\nThe file can still be opened, but it may cause potential problems.\n\nDo you want to continue opening it?")
) == SVS::SVSCraft::Yes;
}
}