-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
179 lines (152 loc) · 5.7 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
void MainWindow::contextMenuEvent(QContextMenuEvent *event)
{
QMenu menu(this);
menu.addAction(loadAction);
menu.addAction(createDataAction);
menu.setTitle("&Archivo");
menu.exec(event->globalPos());
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->ui->plainTextEdit->setReadOnly(true);
this->ui->runButton->setDisabled(true);
this->createActions();
this->createMenu();
this->ui->progressBar->hide();
this->ui->datasetProgressBar->hide();
}
void MainWindow::printMessage(const QString & message) {
this->ui->plainTextEdit->insertPlainText(message);
qApp->processEvents();
}
void MainWindow::printMessageLine(const QString & message) {
printMessage("\n" + message);
qApp->processEvents();
}
void MainWindow::clear() {
this->ui->plainTextEdit->clear();
qApp->processEvents();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::setClusterer(Clusterer * c) {
clusterer = c;
}
void MainWindow::on_loadButton_clicked()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Cargar Dataset"), "./", tr("Archivos de texto (*.txt *.csv)"));
if (fileName != NULL) {
this->loadDataset(fileName);
}
}
void MainWindow::on_runButton_clicked()
{
if (datasetReady) {
int k = this->ui->clustersBox->value();
int max_iterations = this->ui->maxIterationsBox->value();
int metric = this->ui->metricsBox->currentIndex();
bool testingMode = this->ui->modeBox->currentIndex();
Clusterer * clusterer = new Clusterer(datasetMatrix, k, metric, max_iterations, this->datasetDir, this->datasetName, this->ui->progressBar ,testingMode);
QThread * thread = new QThread;
clusterer->moveToThread(thread);
connect (clusterer, SIGNAL (finished(QString,bool)), this, SLOT(handleResult(QString, bool)));
connect (thread, SIGNAL(started()), clusterer, SLOT(runClustering()));
this->ui->runButton->setDisabled(true);
this->clear();
this->printMessageLine("Ejecutando Clustering...");
thread->start();
}
}
void MainWindow::create_dataset_clicked()
{
DatasetWindow * window = new DatasetWindow();
window->show();
}
void MainWindow::loadDataset(const QString &dataset)
{
this->ui->datasetLabel->setText("Cargando dataset...");
this->ui->loadButton->setDisabled(true);
qApp->processEvents();
DatasetLoader * datasetLoader = new DatasetLoader (dataset, &datasetMatrix, this->ui->datasetProgressBar);
QThread * thread = new QThread;
datasetLoader->moveToThread(thread);
connect (datasetLoader, SIGNAL (finished(QString, QString, bool)), this, SLOT(handleDataset(QString, QString, bool)));
connect (thread, SIGNAL(started()), datasetLoader, SLOT(loadDataset()));
thread->start();
}
QString MainWindow::getDirectory(const QString &filePath) {
QString ret = filePath;
ret.remove(this->getFileName(filePath));
return ret;
}
QString MainWindow::getFileName(const QString &filePath)
{
QFileInfo i(filePath);
QString ret = i.fileName();
return ret;
}
void MainWindow::changeCursor(Qt::CursorShape cursor, bool loadingFile)
{
this->setCursor(cursor);
this->ui->loadButton->setDisabled(loadingFile);
qApp->processEvents();
}
void MainWindow::createActions()
{
loadAction = new QAction(tr("&Cargar Dataset"), this);
loadAction->setStatusTip(tr("Carga un dataset para ejecutar K-Means"));
connect(loadAction, &QAction::triggered, this, &MainWindow::on_loadButton_clicked);
createDataAction = new QAction(tr("&Crear Dataset"), this);
createDataAction->setStatusTip(tr("Crea un nuevo dataset"));
connect (createDataAction, &QAction::triggered, this, &MainWindow::create_dataset_clicked);
}
void MainWindow::createMenu()
{
fileMenu = menuBar()->addMenu(tr("&Archivo"));
fileMenu->addAction(loadAction);
fileMenu->addAction(createDataAction);
button = new QToolButton();
button->setText("Archivo ");
button->setMenu(fileMenu);
button->setPopupMode(QToolButton::InstantPopup);
this->ui->mainToolBar->addWidget(button);
}
void MainWindow::on_helpButton_clicked()
{
QString info = QString("\nEl modo de ejecución \"Descubrir Clusters\" permite ejecutar el algoritmo K-Means sobre bases de datos sin etiquetas de cluster (con la distribución de los puntos a su cluster desconocida). \n\nEl modo de ejecución \"Test de Clustering\" sólo está disponible para aquellas bases de datos que contengan la etiqueta de asignación de cada punto a su cluster (En la primer columna). Luego del agrupamiento, se evaluarán las asignaciones y se generará un reporte con los puntos mal clasificados.\n");
QMessageBox::information(this, "Ayuda", info);
}
void MainWindow::handleResult(QString message, bool success)
{
this->clear();
if (!success) {
QMessageBox messageBox;
messageBox.critical(0, "Error", message.toStdString().c_str());
messageBox.setFixedSize(500,200);
}
this->printMessageLine(message);
this->ui->runButton->setDisabled(false);
qApp->processEvents();
}
void MainWindow::handleDataset(QString datasetDir, QString datasetName, bool success)
{
this->datasetDir = datasetDir;
this->datasetName= datasetName;
this->datasetReady = success;
this->ui->runButton->setDisabled(!success);
if (success) {
this->ui->datasetLabel->setText(this->datasetName);
} else {
this->ui->datasetLabel->setText("Ningún dataset cargado");
QMessageBox::warning(this, "ERROR", "Ocurrio un error al cargar el dataset");
}
this->ui->loadButton->setDisabled(false);
qApp->processEvents();
}