-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
80 lines (64 loc) · 2.67 KB
/
main.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
#include "read_file.h"
#include "combination_counter.h"
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QAbstractTableModel>
#include <QTableWidget>
#include <QStandardItemModel>
#include <QChart>
#include <QBarSet>
#include <QBarSeries>
#include <QBarCategoryAxis>
#include <QChartView>
#include <QHBoxLayout>
#include <QFileDialog>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QTableView *table = new QTableView;
QStandardItemModel *model = new QStandardItemModel;
QStandardItem *it = new QStandardItem(QObject::tr("Combination"));
model->setHorizontalHeaderItem(0,it);
QStandardItem *it1 = new QStandardItem(QObject::tr("Frequency"));
model->setHorizontalHeaderItem(1,it1);
QWidget *widget = new QWidget;
auto file1Name = QFileDialog::getOpenFileName(widget,QObject::tr("Open text file"), "/home", QObject::tr("Text Files (*.txt)"));
const std::string data = file_to_string(file1Name.toStdString());
auto most_common = get_combination_statistics(std::string_view{data});
QtCharts::QBarSet *bar_set = new QtCharts::QBarSet("");
QStringList combinations;
std::vector<std::string> percentage_strings;
for(size_t row{0}; auto& elem: most_common){
QStandardItem *it1 = new QStandardItem(QObject::tr(elem.first.c_str()));
std::string percentage = std::to_string(elem.second) + '%';
if(percentage.length() < 4)
percentage = std::string(4 - percentage.length(), '0').append(percentage);
percentage.insert(percentage.size()-3, ".");
auto ref_to_percentage_string = percentage_strings.emplace_back(std::move(percentage));
QStandardItem *it2 = new QStandardItem(QObject::tr(ref_to_percentage_string.c_str()));
model->setItem(row, 0, it1);
model->setItem(row, 1, it2);
row++;
bar_set->append(static_cast<float>(elem.second) / 100);
combinations.append(elem.first.c_str());
}
table->setModel(model);
QtCharts::QBarSeries *series = new QtCharts::QBarSeries();
series->append(bar_set);
QtCharts::QChart *chart = new QtCharts::QChart();
chart->addSeries(series);
chart->setTitle("Frequency");
QtCharts::QBarCategoryAxis *axis = new QtCharts::QBarCategoryAxis();
axis->append(combinations);
chart->createDefaultAxes();
chart->setAxisX(axis, series);
QtCharts::QChartView *chartView = new QtCharts::QChartView(chart);
QMainWindow window;
QHBoxLayout *layout = new QHBoxLayout(widget);
window.setCentralWidget(widget);
layout->addWidget(table, 25);
layout->addWidget(chartView, 75);
window.resize(1280, 480);
window.show();
return app.exec();
}