-
Notifications
You must be signed in to change notification settings - Fork 105
/
autosave_dialog.cpp
198 lines (170 loc) · 4.97 KB
/
autosave_dialog.cpp
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
* Copyright 2014 Kai Pastor
*
* This file is part of OpenOrienteering.
*
* OpenOrienteering is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenOrienteering is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenOrienteering. If not, see <http://www.gnu.org/licenses/>.
*/
#include "autosave_dialog.h"
#include <QCloseEvent>
#include <QDateTime>
#include <QDialogButtonBox>
#include <QFileInfo>
#include <QLabel>
#include <QListWidget>
#include <QVBoxLayout>
#include "main_window.h"
namespace OpenOrienteering {
AutosaveDialog::AutosaveDialog(const QString& path, const QString& autosave_path, const QString& actual_path, MainWindow* parent, Qt::WindowFlags f)
: QDialog(parent, f)
, main_window(parent)
, original_path(path)
, autosave_path(autosave_path)
, resolved(false)
{
const QString text_template = QString::fromLatin1("<b>%1</b><br/>%2<br/>%3");
QFileInfo autosaved_file_info(autosave_path);
autosaved_text.setHtml(text_template.
arg(tr("Autosaved file"),
autosaved_file_info.lastModified().toLocalTime().toString(),
tr("%n bytes", 0, autosaved_file_info.size())));
QFileInfo user_saved_file_info(path);
user_saved_text.setHtml(text_template.
arg(tr("File saved by the user"),
user_saved_file_info.lastModified().toLocalTime().toString(),
tr("%n bytes", 0, user_saved_file_info.size())) );
layout = new QVBoxLayout();
setLayout(layout);
setWindowTitle(tr("File recovery"));
QString intro_text = tr("File %1 was not properly closed. At the moment, there are two versions:");
QLabel* label = new QLabel(intro_text.arg(QString::fromLatin1("<b>%1</b>").arg(user_saved_file_info.fileName())));
label->setWordWrap(true);
layout->addWidget(label);
list_widget = new QListWidget();
list_widget->setItemDelegate(new TextDocItemDelegate(this, this));
list_widget->setSelectionMode(QAbstractItemView::SingleSelection);
QListWidgetItem* item = new QListWidgetItem(list_widget, QListWidgetItem::UserType);
item->setData(Qt::UserRole, QVariant(1));
item = new QListWidgetItem(list_widget, QListWidgetItem::UserType);
item->setData(Qt::UserRole, QVariant(2));
layout->addWidget(list_widget);
label = new QLabel(tr("Save the active file to remove the conflicting version."));
label->setWordWrap(true);
layout->addWidget(label);
setSelectedPath(actual_path);
connect(list_widget, &QListWidget::currentRowChanged, this, &AutosaveDialog::currentRowChanged, Qt::QueuedConnection);
#if defined(Q_OS_ANDROID)
setWindowState((windowState() & ~(Qt::WindowMinimized | Qt::WindowFullScreen))
| Qt::WindowMaximized);
#endif
}
AutosaveDialog::~AutosaveDialog()
{
// Nothing
}
// slot
int AutosaveDialog::exec()
{
QDialogButtonBox button_box(QDialogButtonBox::Open | QDialogButtonBox::Cancel);
layout->addWidget(&button_box);
connect(&button_box, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(&button_box, &QDialogButtonBox::rejected, this, &QDialog::reject);
const int result = QDialog::exec();
resolved = true;
return result;
}
// slot
void AutosaveDialog::autosaveConflictResolved()
{
resolved = true;
if (!isModal())
close();
}
void AutosaveDialog::closeEvent(QCloseEvent *event)
{
if (main_window && !resolved)
event->setAccepted(main_window->closeFile());
else
event->setAccepted(resolved);
}
QString AutosaveDialog::selectedPath() const
{
const int row = list_widget->currentRow();
switch (row)
{
case 0:
return autosave_path;
break;
case 1:
return original_path;
break;
case -1:
break; // Nothing selected?
default:
qWarning("Undefined index");
}
return QString();
}
// slot
void AutosaveDialog::setSelectedPath(const QString& path)
{
if (path == original_path)
list_widget->setCurrentRow(1);
else if (path == autosave_path)
list_widget->setCurrentRow(0);
else
list_widget->setCurrentRow(-1);
}
void AutosaveDialog::currentRowChanged(int row)
{
switch (row)
{
case 0:
emit pathSelected(autosave_path);
break;
case 1:
emit pathSelected(original_path);
break;
case -1:
return; // Nothing selected?
default:
qWarning("Undefined index");
}
}
const QTextDocument* AutosaveDialog::textDoc(const QModelIndex& index) const
{
const QTextDocument* ret = nullptr;
bool ok = true;
int i = index.data(Qt::UserRole).toInt(&ok);
if (ok)
{
switch (i)
{
case 1:
ret = &autosaved_text;
break;
case 2:
ret = &user_saved_text;
break;
default:
qWarning("Undefined index");
}
}
else
{
qWarning("Invalid data for UserRole");
}
return ret;
}
} // namespace OpenOrienteering