#include "mainwindow.h" #include "ui_mainwindow.h" #include "QFileDialog" #include #include #include #include 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"); 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, QList, QList, QList)), SLOT(show_chart(QList, QList, QList, QList))); 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 pred_first, QList pred_second, QList real_first, QList 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); }