-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
146 lines (133 loc) · 4.94 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <bisness_logic.h>
#include <QFile>
#include <QFileDialog>
#include <QStandardItemModel>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent): QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
this->setFixedSize(QSize(956, 640));
CsvItemModel = new QStandardItemModel(this);
CsvItemModel->setColumnCount(7);
}
MainWindow::~MainWindow()
{
delete ui;
}
QStandardItemModel *csv_main_model = new QStandardItemModel;
QStringList headers;
void mdlcopy(QStandardItemModel* first, QStandardItemModel* second){
second->clear();
for (int rows = 0 ; rows < first->rowCount() ; rows++){
QList<QStandardItem *> result;
for (int columns = 0; columns < first->columnCount(); columns++){
result.append(new QStandardItem(first->item(rows, columns)->text()));
}
second->appendRow(result);
}
}
bool is_normal_file(QString file){
bool flag;
flag = file.contains(".csv");
return flag;
}
void MainWindow::on_load_clicked()
{
QString file = QFileDialog::getOpenFileName(this, tr("Open file"));
if (is_normal_file(file)){
FuncArgument fa = {
.path = file.toStdString() //Приводим строку с путем к файлу от типа QString к стандартному типу строки
};
FuncReturningValue frv = entryPoint(readCsv, &fa);
std::vector<std::vector<std::string>> csv = frv.result;
//free(frv);
//std::vector<std::vector<std::string>> csv = read_csv(file);
CsvItemModel->clear();
CsvItemModel->setColumnCount(csv.at(0).size());
headers.clear();
for (std::string str : csv.at(0)) {
headers.push_back(QString::fromStdString(str));
}
CsvItemModel->setHorizontalHeaderLabels(headers);
bool is_header = true;
for (std::vector<std::string> item_list : csv) {
if (is_header){
is_header = false;
continue;
}
QList<QStandardItem *> standardItemsList;
for (std::string item_str : item_list){
QString q_item_str = QString::fromStdString(item_str);
standardItemsList.append(new QStandardItem(q_item_str));
}
CsvItemModel->insertRow(CsvItemModel->rowCount(), standardItemsList);
}
ui->tableView->setModel(CsvItemModel);}
else {
ui->metrics->setText("Ошибка при чтении\nфайла");
}
}
bool check_for_correct_input(QString region, QString column){
bool flag = true;
bool ok;
if (region == "" || column == ""){
flag = false;
} else {
int colum_num = column.toInt(&ok);
if (ok){
if (colum_num < 1 || colum_num > 7 || colum_num == 2){
flag = false;
}}}
return flag;
}
void MainWindow::on_calculate_clicked()
{
QString region = ui->region->text();
QString column = ui->line_input->text();
if (check_for_correct_input(region, column)){
float minimum = 0;
float maximum = 0;
float medium = 0;
int column_number = column.toInt() - 1;
csv_main_model->clear();
csv_main_model->setColumnCount(7);
csv_main_model->setHorizontalHeaderLabels(headers);
for (int row = 0; row < CsvItemModel->rowCount(); row++){
if (CsvItemModel->item(row, 1)->text() == region){
QList<QStandardItem *> result;
for (int columns = 0; columns < CsvItemModel->columnCount(); columns++){
result.append(new QStandardItem(CsvItemModel->item(row, columns)->text()));
}
csv_main_model->insertRow(csv_main_model->rowCount(), result);
}
}
ui->tableView->setModel(csv_main_model);
std::vector<float> arr;
for (int row = 0; row < csv_main_model->rowCount(); ++row){
FuncArgument fa = {
.text = csv_main_model->item(row, column_number)->text()
};
FuncReturningValue frv = entryPoint(isNormalMetric, &fa);
if (frv.isok){
arr.push_back(csv_main_model->item(row, column_number)->text().toFloat());
}
}
FuncArgument fa = {
.arr = arr,
.minimum = &minimum,
.maximum = &maximum,
.medium = &medium
};
FuncReturningValue frv = entryPoint(calculateMetrics, &fa);
//calculate_metrics(arr, &minimum, &maximum, &medium);
QString final_text = "Минимум: "+ QString::number(minimum) +"\nМаксимум: "+ QString::number(maximum)
+"\nМедиана: "+ QString::number(medium);
ui->metrics->setText(final_text);
} else {
ui->metrics->setText("Введены неверные\nзначения!");
}
}