forked from foldynl/QLog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImportDialog.cpp
348 lines (279 loc) · 11.9 KB
/
ImportDialog.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include <QFileDialog>
#include <QMessageBox>
#include "ImportDialog.h"
#include "ui_ImportDialog.h"
#include "logformat/LogFormat.h"
#include "core/debug.h"
#include "data/StationProfile.h"
#include "data/RigProfile.h"
MODULE_IDENTIFICATION("qlog.ui.importdialog");
ImportDialog::ImportDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::ImportDialog)
{
FCT_IDENTIFICATION;
ui->setupUi(this);
ui->allCheckBox->setChecked(true);
ui->startDateEdit->setDisplayFormat(locale.formatDateShortWithYYYY());
ui->startDateEdit->setDate(QDate::currentDate());
ui->endDateEdit->setDate(QDate::currentDate());
ui->endDateEdit->setDisplayFormat(locale.formatDateShortWithYYYY());
ui->progressBar->setValue(0);
ui->progressBar->setMinimum(0);
ui->progressBar->setMaximum(100);
//ui->progressBar->setDisabled(true);
QStringList rigs = RigProfilesManager::instance()->profileNameList();
QStringListModel* rigModel = new QStringListModel(rigs, this);
ui->rigSelect->setModel(rigModel);
if (!ui->rigSelect->currentText().isEmpty())
{
ui->rigCheckBox->setChecked(true);
ui->rigSelect->setCurrentText(RigProfilesManager::instance()->getCurProfile1().profileName);
}
QStringList profiles = StationProfilesManager::instance()->profileNameList();
QStringListModel* profileModel = new QStringListModel(profiles, this);
ui->profileSelect->setModel(profileModel);
if (!ui->profileSelect->currentText().isEmpty())
{
ui->profileSelect->setCurrentText(StationProfilesManager::instance()->getCurProfile1().profileName);
ui->profileCheckBox->setChecked(true);
}
ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("&Import"));
}
void ImportDialog::browse()
{
FCT_IDENTIFICATION;
QSettings settings;
const QString &lastPath = ( ui->fileEdit->text().isEmpty() ) ? settings.value("import/last_path", QDir::homePath()).toString()
: ui->fileEdit->text();
QString filename = QFileDialog::getOpenFileName(this, tr("Select File"),
lastPath,
ui->typeSelect->currentText().toUpper() + "(*." + ui->typeSelect->currentText().toLower() + ")",
nullptr,
#if defined(Q_OS_LINUX) && !(defined(QLOG_FLATPAK) && defined(Q_PROCESSOR_ARM_64))
// Do not use the Native Dialog under Linux because the dialog is case-sensitive.
// QT variant looks different but it is case-insensitive.
// More information:
// https://stackoverflow.com/questions/34858220/qt-how-to-set-a-case-insensitive-filter-on-qfiledialog
// https://bugreports.qt.io/browse/QTBUG-51712
// But Raspberry pi crashes when DontUseNativeDialog is used therefore use the native for it.
QFileDialog::DontUseNativeDialog
#else
QFileDialog::Options()
#endif
);
if ( !filename.isEmpty() )
{
settings.setValue("import/last_path", QFileInfo(filename).path());
ui->fileEdit->setText(filename);
}
}
void ImportDialog::toggleAll() {
FCT_IDENTIFICATION;
ui->startDateEdit->setEnabled(!ui->allCheckBox->isChecked());
ui->endDateEdit->setEnabled(!ui->allCheckBox->isChecked());
}
void ImportDialog::toggleMyProfile()
{
FCT_IDENTIFICATION;
ui->profileSelect->setEnabled(ui->profileCheckBox->isChecked());
}
void ImportDialog::toggleMyRig()
{
FCT_IDENTIFICATION;
ui->rigSelect->setEnabled(ui->rigCheckBox->isChecked());
}
void ImportDialog::commentChanged(const QString &comment)
{
QString toolTip = tr("The value is used when an input record does not contain the ADIF value") + "<br/>"
+ "<b>" + tr("Comment") + ":</b> " + comment + "<br/>";
ui->commentEdit->setToolTip(toolTip);
ui->commentCheckBox->setToolTip(toolTip);
}
void ImportDialog::toggleComment()
{
FCT_IDENTIFICATION;
ui->commentEdit->setEnabled(ui->commentCheckBox->isChecked());
commentChanged(ui->commentEdit->text());
}
void ImportDialog::computeProgress(qint64 position)
{
FCT_IDENTIFICATION;
int progress = (int)(position * 100 / size);
ui->progressBar->setValue(progress);
QCoreApplication::processEvents();
}
void ImportDialog::stationProfileTextChanged(const QString &newProfileName)
{
FCT_IDENTIFICATION;
selectedStationProfile = StationProfilesManager::instance()->getProfile(newProfileName);
QString toolTip = tr("The values below will be used when an input record does not contain the ADIF values") + "<br/>"
+ selectedStationProfile.toHTMLString();
ui->profileSelect->setToolTip(toolTip);
ui->profileCheckBox->setToolTip(toolTip);
}
void ImportDialog::rigProfileTextChanged(const QString &newProfileName)
{
FCT_IDENTIFICATION;
QString toolTip = tr("The values below will be used when an input record does not contain the ADIF values") + "<br/>"
+ RigProfilesManager::instance()->getProfile(newProfileName).toHTMLString();
ui->rigSelect->setToolTip(toolTip);
ui->rigCheckBox->setToolTip(toolTip);
}
LogFormat::duplicateQSOBehaviour ImportDialog::showDuplicateDialog(QSqlRecord *imported, QSqlRecord *original)
{
FCT_IDENTIFICATION;
qCDebug(function_parameters) << *imported << " " << *original;
LogFormat::duplicateQSOBehaviour ret = LogFormat::ACCEPT_ONE;
QMessageBox::StandardButton reply;
QString inLogQSO = tr("<p><b>In-Log QSO:</b></p><p>")
+ original->value("start_time").toString() + " "
+ original->value("callsign").toString() + "</p>";
QString importedQSO = tr("<p><b>Importing:</b></p><p>")
+ imported->value("start_time").toString() + " "
+ imported->value("callsign").toString() + "<p> ";
reply = QMessageBox::question(nullptr,
tr("Duplicate QSO"),
tr("<p>Do you want to import duplicate QSO?</p>%1 %2").arg(inLogQSO,importedQSO),
QMessageBox::Yes|QMessageBox::No|QMessageBox::YesAll|QMessageBox::NoAll);
switch ( reply )
{
case QMessageBox::Yes:
ret = LogFormat::ACCEPT_ONE;
break;
case QMessageBox::YesAll:
ret = LogFormat::ACCEPT_ALL;
break;
case QMessageBox::No:
ret = LogFormat::SKIP_ONE;
break;
case QMessageBox::NoAll:
ret = LogFormat::SKIP_ALL;
break;
default:
ret = LogFormat::ASK_NEXT;
}
qCDebug(runtime) << "ret: " << ret;
return ret;
}
void ImportDialog::saveImportDetails(const QString &importDetail, const QString &filename,
const int count, const unsigned long warnings, const unsigned long errors)
{
FCT_IDENTIFICATION;
QSettings settings;
const QString &lastPath = settings.value("import/last_path_importdetails", QDir::homePath()).toString();
QString filePath = QFileDialog::getSaveFileName(this, tr("Save to File"),
lastPath,
"TXT (*.txt)");
if ( !filePath.isEmpty() )
{
QFile file(filePath);
if ( file.open(QIODevice::WriteOnly | QIODevice::Text) )
{
const QDateTime &currTime = QDateTime::currentDateTimeUtc();
QTextStream out(&file);
out << tr("QLog Import Summary") << "\n"
<< "\n"
<< tr("Import date") << ": " << currTime.toString(locale.formatDateShortWithYYYY()) << " " << locale.toString(currTime, locale.formatTimeLongWithoutTZ()) << " UTC\n"
<< tr("Imported file") << ": " << filename
<< "\n\n"
<< tr("Imported: %n contact(s)", "", count) << "\n"
<< tr("Warning(s): %n", "", warnings) << "\n"
<< tr("Error(s): %n", "", errors) << "\n"
<< "\n"
<< tr("Details") << ":\n"
<< importDetail;
file.close();
settings.setValue("import/last_path_importdetails", QFileInfo(filePath).path());
}
}
}
void ImportDialog::runImport()
{
FCT_IDENTIFICATION;
if ( ui->fileEdit->text().isEmpty() )
{
QMessageBox::warning(nullptr, QMessageBox::tr("QLog Warning"),
QMessageBox::tr("Filename is empty"));
return;
}
QFile file(ui->fileEdit->text());
file.open(QFile::ReadOnly | QFile::Text);
QTextStream in(&file);
size = file.size();
QMap<QString, QString> defaults;
if (ui->rigCheckBox->isChecked())
{
defaults["my_rig_intl"] = ui->rigSelect->currentText();
}
if (ui->commentCheckBox->isChecked())
{
defaults["comment_intl"] = ui->commentEdit->text();
}
LogFormat* format = LogFormat::open(ui->typeSelect->currentText(), in);
if (!format) {
qCritical() << "unknown log format";
return;
}
format->setDefaults(defaults);
format->setUpdateDxcc(ui->updateDxccCheckBox->isChecked());
if (!ui->allCheckBox->isChecked()) {
format->setFilterDateRange(ui->startDateEdit->date(), ui->endDateEdit->date());
}
format->setDuplicateQSOCallback(showDuplicateDialog);
connect(format, &LogFormat::importPosition, this, &ImportDialog::computeProgress);
ui->buttonBox->setEnabled(false);
ui->fileEdit->setEnabled(false);
ui->typeSelect->setEnabled(false);
ui->browseButton->setEnabled(false);
ui->startDateEdit->setEnabled(false);
ui->endDateEdit->setEnabled(false);
ui->allCheckBox->setEnabled(false);
ui->profileCheckBox->setEnabled(false);
ui->profileSelect->setEnabled(false);
ui->rigCheckBox->setEnabled(false);
ui->rigSelect->setEnabled(false);
ui->commentCheckBox->setEnabled(false);
ui->commentEdit->setEnabled(false);
ui->updateDxccCheckBox->setEnabled(false);
QString s;
QTextStream out(&s);
unsigned long errors = 0L;
unsigned long warnings = 0L;
int count = format->runImport(out,
( ui->profileCheckBox->isChecked() && selectedStationProfile != StationProfile() ) ?&selectedStationProfile: nullptr,
&warnings,
&errors);
QString report = QObject::tr("<b>Imported</b>: %n contact(s)", "", count) + "<br/>" +
QObject::tr("<b>Warning(s)</b>: %n", "", warnings) + "<br/>" +
QObject::tr("<b>Error(s)</b>: %n", "", errors);
QMessageBox msgBox;
QAbstractButton* pButtonYes = nullptr;
msgBox.setWindowTitle(tr("Import Result"));
msgBox.setText(report);
msgBox.setDetailedText(s);
msgBox.setIcon(QMessageBox::Information);
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
if ( !s.isEmpty() )
{
pButtonYes = msgBox.addButton(tr("Save Details..."), QMessageBox::ActionRole);
}
QSpacerItem* horizontalSpacer = new QSpacerItem(500, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
QGridLayout* layout = (QGridLayout*)msgBox.layout();
layout->addItem(horizontalSpacer, layout->rowCount(), 0, 1, layout->columnCount());
msgBox.exec();
if ( pButtonYes
&& msgBox.clickedButton() == pButtonYes )
{
saveImportDetails(s, ui->fileEdit->text(),
count, warnings, errors);
}
qCDebug(runtime).noquote() << s;
accept();
}
ImportDialog::~ImportDialog()
{
FCT_IDENTIFICATION;
delete ui;
}