-
Notifications
You must be signed in to change notification settings - Fork 0
/
newunitdialog.cpp
165 lines (140 loc) · 4.67 KB
/
newunitdialog.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
/*
Copyright © 2021 Frank Pereny
https://github.com/fjpereny/konverter
This program 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.
This program 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.
*/
#include "newunitdialog.h"
#include "ui_newunitdialog.h"
#include <QFile>
#include <QDir>
#include <QMessageBox>
#include <iostream>
NewUnitDialog::NewUnitDialog(QWidget *parent, QStringList *data_file_list, const QString *folder_separator) :
QMainWindow(parent),
ui(new Ui::NewUnitDialog),
fold_sep(new const QString)
{
ui->setupUi(this);
fold_sep = folder_separator;
for (int i=0; i<data_file_list->count(); i++)
{
QStringList file_name_extension = data_file_list->at(i).split(".");
if (file_name_extension[1] == "dat")
{
ui->existingListBox->addItem(file_name_extension[0]);
}
else if (file_name_extension[1] == "csv")
{
ui->existingListBox->addItem(file_name_extension[0] + " (Custom)");
}
}
}
NewUnitDialog::~NewUnitDialog()
{
delete ui;
}
void NewUnitDialog::on_cloneCheckBox_toggled(bool checked)
{
if (checked)
{
ui->existingListBox->setEnabled(true);
ui->masterLineEdit->setEnabled(false);
ui->masterLineEdit->clear();
}
else
{
ui->existingListBox->setEnabled(false);
ui->masterLineEdit->setEnabled(true);
}
}
void NewUnitDialog::on_buttonBox_rejected()
{
this->close();
}
void NewUnitDialog::on_buttonBox_accepted()
{
if (ui->nameLineEdit->text().isEmpty())
{
QMessageBox *mb = new QMessageBox(this);
mb->setText("File name cannot be empty.");
mb->show();
}
else if (ui->nameLineEdit->text().contains(" (Custom)"))
{
QMessageBox *mb = new QMessageBox(this);
mb->setText("Invalid filename:\nThe file name cannot contain \"(Custom)\"");
mb->show();
}
else if (reserved_char(ui->nameLineEdit->text()))
{
QMessageBox *mb = new QMessageBox(this);
mb->setText("Invalid filename:\nThe file name contains invalid characters.");
mb->show();
}
else
{
if (ui->cloneCheckBox->isChecked())
{
QString new_file_name = ui->nameLineEdit->text();
QString old_file = ui->existingListBox->selectedItems()[0]->text();
QString old_file_path;
if (old_file.contains(" (Custom)"))
{
QString old_file_name = old_file.split(" (Custom)")[0];
old_file_path = QDir::currentPath() + *fold_sep + "data" + *fold_sep +
old_file_name + ".csv";
}
else
{
old_file_path = QDir::currentPath() + *fold_sep + "data" + *fold_sep +
".default" + *fold_sep + old_file + ".dat";
}
QString new_file_path = QDir::currentPath() + *fold_sep + "data" + *fold_sep +
new_file_name + ".csv";
QFile::copy(old_file_path, new_file_path);
}
// Clone button NOT checked.
else
{
if (ui->masterLineEdit->text().isEmpty())
{
QMessageBox *mb = new QMessageBox(this);
mb->setText("Mater Unit name invald.");
mb->show();
}
else
{
QString new_file_name = ui->nameLineEdit->text() + ".csv";
QString file_path = QDir::currentPath() + *fold_sep + "data" + *fold_sep;
QFile file(file_path + new_file_name);
file.open(QFile::OpenModeFlag::ReadWrite);
QTextStream stream(&file);
stream << "Unit Type, " + new_file_name << "\n";
stream << "Master Unit, " + ui->masterLineEdit->text() + "\n";
stream << "Unit Name, Conversion Value, Notes";
stream.flush();
file.close();
}
}
((DataTableWindow*)parent())->read_file_names();
((DataTableWindow*)parent())->load_category_list();
this->close();
}
}
bool NewUnitDialog::reserved_char(QString input)
{
QString reserved = "<>:*\"/\\|?";
for (int i=0; i<reserved.count(); ++i)
{
if (input.contains(reserved.at(i)))
{
return true;
}
}
return false;
}