-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
284 lines (228 loc) · 10.3 KB
/
mainwindow.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
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) //creating mainwindow
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowIcon(QIcon(":/Pictures/Data/Pictures/ProgramIcon.png")); // setting icon
setWindowTitle("RoadCycle"); // setting title
mkdir("Data"); //create directory for files
fill_table_from_txt(); // filling table
ui->RoadData->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); // make table look nice)
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::fill_table_from_txt()
{
string road_name, road_type, road_pavement, road_divider, temp_line;
double road_length; // temp variables to accept data
int road_lines;
int amount_of_lines{0}, current_row{0};
ifstream fileIn ("Data/DataBase.txt");
ifstream fileIn_to_count ("Data/DataBase.txt");
while (getline(fileIn_to_count, temp_line)){
amount_of_lines++;
}
ui->RoadData->setRowCount(amount_of_lines);
if(fileIn.is_open()){
while(fileIn >> road_name >> road_type >> road_length >> road_lines >> road_pavement >> road_divider){
ui->RoadData->setItem(current_row,0,new QTableWidgetItem(QString::fromStdString(road_name))); // row, column, item
ui->RoadData->setItem(current_row,1,new QTableWidgetItem(QString::fromStdString(road_type)));
auto road_length_converted = new QTableWidgetItem; // conversion special for sorting data is not string!
road_length_converted->setData(Qt::EditRole,road_length);
ui->RoadData->setItem(current_row,2,road_length_converted);
auto road_lines_converted = new QTableWidgetItem;
road_lines_converted->setData(Qt::EditRole,road_lines);
ui->RoadData->setItem(current_row,3,road_lines_converted);
ui->RoadData->setItem(current_row,4,new QTableWidgetItem(QString::fromStdString(road_pavement)));
ui->RoadData->setItem(current_row,5,new QTableWidgetItem(QString::fromStdString(road_divider)));
current_row++;
}
fileIn.close();
}
fileIn_to_count.close();
}
void MainWindow::on_actionResetTable_triggered() //reseting data from vector
{
QApplication::beep();
QMessageBox::StandardButton reply;
reply = QMessageBox::warning(this,"Відновити дані","Ви дійсно бажаєте відновити дані до початкових?", QMessageBox::No | QMessageBox::Yes);
if(reply == QMessageBox::Yes){
ofstream fileOut;
fileOut.open("Data/DataBase.txt");
for (int i{0}; i < int(roads_data.size()); i++) {
for (int j{0}; j<6; j++) {
fileOut << roads_data[i][j] << " ";
}
fileOut << "\n";
}
fileOut.close();
}
//set our table
fill_table_from_txt();
}
void MainWindow::on_actionDeleteElement_triggered() // deleting choosen element
{
QMessageBox::StandardButton delete_data;
delete_data = QMessageBox::critical(this,"Видалити рядок","Ви дійсно бажаєте безповоротно видалити цей рядок?", QMessageBox::No | QMessageBox::Yes);
if(delete_data == QMessageBox::Yes){
QTableWidgetItem* theItem = ui->RoadData->item(ui->RoadData->currentRow(), 0);
string road_name_to_delete = theItem->text().toUtf8().constData(); //take our road name to delete right line from txt
ifstream roads("Data/DataBase.txt");
ofstream temp("Data/Temp.txt");
string road_name, road_type, road_length, road_lines, road_pavement, road_divider;
while(roads >> road_name >> road_type >> road_length >> road_lines >> road_pavement>> road_divider)
{
if(road_name_to_delete!=road_name){ // write data to Temp.txt our without delete line
temp << road_name << " "
<< road_type << " "
<< road_length << " "
<< road_lines << " "
<< road_pavement << " "
<< road_divider << "\n";
}
}
roads.close();
temp.close();
remove("Data/DataBase.txt");
rename("Data/Temp.txt","Data/DataBase.txt");
fill_table_from_txt();
//ui->RoadData->removeRow(ui->RoadData->currentRow());
}
}
void MainWindow::on_actionAscendingSortColumn_triggered() // sorting
{
ui->RoadData->sortItems(ui->RoadData->currentColumn(), Qt::AscendingOrder);
}
void MainWindow::on_actionDescendingSortColumn_triggered() // sorting
{
ui->RoadData->sortItems(ui->RoadData->currentColumn(), Qt::DescendingOrder);
}
void MainWindow::on_actionAddElement_triggered()
{
int result{};
DialogAddItem addItem(this); //creating dialog window
addItem.setWindowTitle("Додати дорогу");
result = addItem.exec();
string road_name = addItem.road_name().toUtf8().constData(); // earn data from dialog
string road_type = addItem.road_type().toUtf8().constData();
double road_length = addItem.road_length();
int road_lines = addItem.road_lines();
string road_pavement = addItem.road_pavement().toUtf8().constData();
string road_divider = addItem.road_divider().toUtf8().constData();
if (result == QDialog::Accepted){ // pass taken data to class
Road(road_name,road_type,road_length,road_lines,road_pavement,road_divider);
}
fill_table_from_txt(); //fill our table
}
void MainWindow::on_actionDeleteAllElement_triggered() // clear table
{
QApplication::beep();
QMessageBox::StandardButton delete_data;
delete_data = QMessageBox::critical(this,"Обнулити таблицю","Ви дійсно бажаєте безповоротно видалити таблицю?", QMessageBox::No | QMessageBox::Yes);
if(delete_data == QMessageBox::Yes){
ofstream reset_file;
reset_file.open("Data/DataBase.txt", ios::trunc); //small cheat to clean file :)
reset_file.close();
fill_table_from_txt();
}
}
void MainWindow::on_actionOpenNewFile_triggered() // open from file
{
QApplication::beep();
QMessageBox::StandardButton rewrite_data;
rewrite_data = QMessageBox::warning(this,"Перезаписати таблицю","Ви дійсно бажаєте перезаписати таблицю?\nВаші теперешні дані буде втрачено", QMessageBox::No | QMessageBox::Yes);
if(rewrite_data == QMessageBox::Yes){
QString filter = "All File (*.*) ;; Text File (*.txt)"; // seting filter data in explorer
QString opened_file = QFileDialog::getOpenFileName(this,"Open a file", QDir::homePath(), filter); // creating string with file adress
if(opened_file == ""){ // do nothing when if path is empty "nothing selected"
return;
}
ofstream our_data_base;
ifstream chosen_data_base;
our_data_base.open("Data/DataBase.txt", ios::trunc); //cleaning our .txt
chosen_data_base.open(opened_file.toUtf8().constData());
string road_name, road_type, road_length, road_lines, road_pavement, road_divider; // temp values
while(chosen_data_base >> road_name >> road_type >> road_length >> road_lines >> road_pavement>> road_divider)
{
our_data_base << road_name << " "
<< road_type << " "
<< road_length << " "
<< road_lines << " "
<< road_pavement << " "
<< road_divider << "\n";
}
our_data_base.close();
chosen_data_base.close();
}
fill_table_from_txt();
}
void MainWindow::on_actionFirstTask_triggered()
{
ifstream file("Data/DataBase.txt", ios::ate); // ios::ate means open at end
if (file.tellg() == 0){
QApplication::beep();
QMessageBox::warning(this,"Увага!","Ваша таблиця пуста!");
return;
}
taskOutput outputTask(this); //creating dialog window
outputTask.setWindowTitle("Перше завдання");
outputTask.output_task_1();
outputTask.exec();
}
void MainWindow::on_actionSecondTask_triggered()
{
ifstream file("Data/DataBase.txt", ios::ate); // ios::ate means open at end
if (file.tellg() == 0){
QApplication::beep();
QMessageBox::warning(this,"Увага!","Ваша таблиця пуста!");
return;
}
taskOutput outputTask(this); //creating dialog window
outputTask.setWindowTitle("Друге завдання");
outputTask.output_task_2();
outputTask.exec();
}
void MainWindow::on_actionThirdTask_triggered()
{
ifstream file("Data/DataBase.txt", ios::ate); // ios::ate means open at end
if (file.tellg() == 0){
QApplication::beep();
QMessageBox::warning(this,"Увага!","Ваша таблиця пуста!");
return;
}
taskOutput outputTask(this); //creating dialog window
outputTask.setWindowTitle("Третє завдання");
outputTask.output_task_3();
outputTask.exec();
}
void MainWindow::on_actionFourthTask_triggered()
{
ifstream file("Data/DataBase.txt", ios::ate); // ios::ate means open at end
if (file.tellg() == 0){
QApplication::beep();
QMessageBox::warning(this,"Увага!","Ваша таблиця пуста!");
return;
}
taskOutput outputTask(this); //creating dialog window
outputTask.setWindowTitle("Четверте завдання");
outputTask.output_task_4();
outputTask.exec();
}
Road::Road(string _name, string _type, double _length, int _lines, string _pavement, string _divider) // set up constructor
: road_name{_name}, road_type{_type}, road_length{_length}, road_lines{_lines}, road_pavement{_pavement}, road_divider{_divider}{ //pass data to constructor
add_data_from_class(); //call class "Road" function
}
void Road::add_data_from_class(){
ofstream fileout ("Data/DataBase.txt",ios::app); //adding data to our txt
fileout << road_name << " "
<< road_type << " "
<< road_length << " "
<< road_lines << " "
<< road_pavement << " "
<< road_divider << "\n";
fileout.close();
}