back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/mainwindow.cpp
blob: 50860a749e3850e2252506339f711725990c028a (plain)
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QFileDialog"
#include <QTextStream>
#include <QFile>
#include <QDebug>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent),
      ui(new Ui::MainWindow),
      target_doc()
{
    ui->setupUi(this);
    thread = new QThread(this);
    alg = new DetectionAlgorithm();
    chart_window = new QWidget();
}

MainWindow::~MainWindow()
{
    delete ui;

}


void MainWindow::on_file_button_clicked()
{
    QString filename = QFileDialog::getOpenFileName(this, "Open File",
                                                    ui->set_line->text(),
                                                    "Text Files(*.txt)");
    if(!filename.isEmpty())
        ui->set_line->setText((filename));
    QFile file(filename);
    if (!file.open(QIODevice::ReadOnly))
        qWarning("Cannot open file for reading");
    QTextStream in(&file);
    while (!in.atEnd())
        target_doc += in.readAll();
    file.close();
}

void MainWindow::on_analysys_push_button_clicked()
{

        alg = new DetectionAlgorithm();

        thread = new QThread(this);
    std::string current_locale_text;

    qRegisterMetaType<QList<QPointF>>("QList<QPointF>");
    if(!ui->textEdit->toPlainText().isEmpty()){
        target_doc = ui->textEdit->toPlainText();
        current_locale_text = target_doc.toLocal8Bit().constData();
    }
    else if(!ui->set_line->drop_document.isEmpty()){
        current_locale_text = ui->set_line->drop_document.toLocal8Bit().constData();
    }
    else if (!target_doc.isEmpty()){
        current_locale_text = target_doc.toLocal8Bit().constData();
    }

    alg->setOptions(ui->buttonGroup_1->checkedButton()->text().toInt(),
                    ui->buttonGroup_2->checkedButton()->text().toInt(),
                    current_locale_text,
                    ui->comboBox->currentIndex());

    alg->moveToThread(thread);
    QObject::connect(this, SIGNAL(run()), alg, SLOT(run_algorithm()));
    QObject::connect(thread, SIGNAL(started()), SLOT(show_dialog()));
    QObject::connect(alg, SIGNAL(terminate()), SLOT(slotCancel()));
    QObject::connect(alg, SIGNAL(run_result(int, int)), SLOT(show_result(int, int)));

    QObject::connect(alg, SIGNAL(run_chart(QList<QPointF>, QList<QPointF>,
                                           QList<QPointF>, QList<QPointF>)),
                     SLOT(show_chart(QList<QPointF>, QList<QPointF>,
                                     QList<QPointF>, QList<QPointF>)));

    thread->start();
    ui->set_line->drop_document = QString();
    ui->set_line->clear();
    target_doc = QString();
}

//connect(alg->dialog, SIGNAL(canceled()), SLOT(slotCancel()));


void MainWindow::slotCancel()
{
    thread->quit();
    thread->wait();
    delete thread;
    alg->~DetectionAlgorithm();
}

void MainWindow::show_dialog()
{
    QProgressDialog* dialog = new QProgressDialog("Идет выполнение операции", "Cancel", 0, 5);
    QObject::connect(alg, SIGNAL(value(int)), dialog, SLOT(setValue(int)));
    QObject::connect(dialog, SIGNAL(canceled()), this, SLOT(slotCancel()));
    dialog->setWindowModality(Qt::WindowModal);
    QApplication::processEvents();

    dialog->setMinimumDuration(0);
    dialog->setWindowTitle("Пожалуйста, подождите");
    emit run();
}

void MainWindow::show_chart(QList<QPointF> pred_first, QList<QPointF> pred_second,
                            QList<QPointF> real_first, QList<QPointF> real_second)
{
    QtCharts::QChartView* real_chartView = new QtCharts::QChartView(chart_window);
    QtCharts::QChartView* pred_chartView = new QtCharts::QChartView(chart_window);
    QHBoxLayout* hl = new QHBoxLayout();
    hl->addWidget(real_chartView);
    hl->addWidget(pred_chartView);
    chart_window->setLayout(hl);

    QtCharts::QChart *real_chart = new QtCharts::QChart();
    QtCharts::QChart *pred_chart = new QtCharts::QChart();

    QtCharts::QScatterSeries *real_series1 = new QtCharts::QScatterSeries();
    real_series1->setName("robot documents");
    real_series1->setMarkerShape(QtCharts::QScatterSeries::MarkerShapeCircle);
    real_series1->setMarkerSize(10.0);
    real_series1->append(real_first);
    real_series1->setColor(Qt::blue);
    QtCharts::QScatterSeries *real_series2 = new QtCharts::QScatterSeries();
    real_series2->setName("received document");
    real_series2->setMarkerShape(QtCharts::QScatterSeries::MarkerShapeCircle);
    real_series2->setMarkerSize(10.0);
    real_series2->append(real_second);
    real_series2->setColor(Qt::red);

    real_chart->addSeries(real_series1);
    real_chart->addSeries(real_series2);
    real_chart->setTitle("real labels");
    real_chart->createDefaultAxes();
    real_chart->setDropShadowEnabled(false);
    real_chart->legend()->setMarkerShape(QtCharts::QLegend::MarkerShapeFromSeries);

    QtCharts::QScatterSeries *pred_series1 = new QtCharts::QScatterSeries();
    pred_series1->setName("1 class");
    pred_series1->setMarkerShape(QtCharts::QScatterSeries::MarkerShapeCircle);
    pred_series1->setMarkerSize(10.0);
    pred_series1->append(pred_first);
    pred_series1->setColor(Qt::blue);
    QtCharts::QScatterSeries *pred_series2 = new QtCharts::QScatterSeries();
    pred_series2->setName("2 class");
    pred_series2->setMarkerShape(QtCharts::QScatterSeries::MarkerShapeCircle);
    pred_series2->setMarkerSize(10.0);
    pred_series2->append(pred_second);
    pred_series2->setColor(Qt::red);

    pred_chart->addSeries(pred_series1);
    pred_chart->addSeries(pred_series2);
    pred_chart->setTitle("prediction");
    pred_chart->createDefaultAxes();
    pred_chart->setDropShadowEnabled(false);
    pred_chart->legend()->setMarkerShape(QtCharts::QLegend::MarkerShapeFromSeries);

    pred_chartView->setChart(pred_chart);
    real_chartView->setChart(real_chart);

    chart_window->setMinimumSize(900, 600);
    chart_window->show();
    thread->quit();
    thread->wait();
    delete thread;
    alg->~DetectionAlgorithm();
}

void MainWindow::show_result(int rob, int ret)
{
    QMessageBox::StandardButton info;

    if (rob != ret)
        info = QMessageBox::information(this, "Результат", "human text", QMessageBox::Ok);
    else
        info = QMessageBox::information(this, "Результат","artifical text", QMessageBox::Ok);

}