Init Commit
This commit is contained in:
175
mainwindow.cpp
Normal file
175
mainwindow.cpp
Normal file
@@ -0,0 +1,175 @@
|
||||
//
|
||||
// Created by fly on 2021/11/25.
|
||||
//
|
||||
|
||||
// You may need to build the project (run Qt uic code generator) to get "ui_MainWindow.h" resolved
|
||||
|
||||
#include "mainwindow.h"
|
||||
#include "ui_MainWindow.h"
|
||||
#include <QFileDialog>
|
||||
#include <QRegExp>
|
||||
#include <QStringList>
|
||||
#include <QDebug>
|
||||
#include <QList>
|
||||
#include <QProgressBar>
|
||||
#include <cmath>
|
||||
#include <QMessageBox>
|
||||
#include "Pearson.h"
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent) :
|
||||
QMainWindow(parent), ui(new Ui::MainWindow) {
|
||||
ui->setupUi(this);
|
||||
|
||||
m_threadPool.setMaxThreadCount(1);
|
||||
|
||||
InitWidgetEnableState();
|
||||
InitThreadCountComboBox();
|
||||
InitTableView();
|
||||
}
|
||||
|
||||
void MainWindow::InitHeaderComboBox() {
|
||||
ui->primaryColComboBox->setEnabled(true);
|
||||
ui->dropColComboBox->setEnabled(true);
|
||||
|
||||
ui->primaryColComboBox->clear();
|
||||
ui->dropColComboBox->clear();
|
||||
|
||||
for (uint64_t i = 0; i < m_dataHeader.size(); ++i) {
|
||||
ui->primaryColComboBox->addItem(m_dataHeader.at(i), i);
|
||||
ui->dropColComboBox->addItem(m_dataHeader.at(i));
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::InitThreadCountComboBox() {
|
||||
auto count = QThread::idealThreadCount();
|
||||
for (int i = 1; i <= count; ++i) {
|
||||
ui->threadCountComboBox->addItem(QString("%1").arg(i), i);
|
||||
}
|
||||
ui->threadCountComboBox->setCurrentIndex(0);
|
||||
}
|
||||
|
||||
void MainWindow::InitTableView() {
|
||||
ui->tableView->setModel(&m_dataTableModel);
|
||||
ui->tableView->setSelectionMode(QTableView::SingleSelection);
|
||||
ui->tableView->setSelectionBehavior(QTableView::SelectRows);
|
||||
ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
|
||||
ui->tableView->setSortingEnabled(true);
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow() {
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void MainWindow::InitWidgetEnableState() {
|
||||
ui->primaryColComboBox->setEnabled(false);
|
||||
ui->dropColComboBox->setEnabled(false);
|
||||
ui->progressBar->setValue(0);
|
||||
ui->threadCountComboBox->setEnabled(false);
|
||||
ui->exportDataButton->setEnabled(false);
|
||||
ui->startAnalysisButton->setEnabled(false);
|
||||
}
|
||||
|
||||
void MainWindow::on_exploreButton_clicked() {
|
||||
m_fileName = QFileDialog::getOpenFileName(this, "请选择要分析的csv文件", "", "CSV File (*.csv)");
|
||||
if (m_fileName.size()) {
|
||||
ui->openFileButton->setEnabled(true);
|
||||
ui->filePathEdit->setText(m_fileName);
|
||||
InitWidgetEnableState();
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::on_openFileButton_clicked() {
|
||||
static QRegExp split("\\s?,\\s?");
|
||||
static QRegExp remove("[ \\r\\n]");
|
||||
QFile file(m_fileName);
|
||||
if (file.open(QFile::ReadOnly)) {
|
||||
m_dataHeader.clear();
|
||||
m_dataVector.clear();
|
||||
QString headerLine = file.readLine();
|
||||
headerLine.remove(remove);
|
||||
auto headers = headerLine.split(split);
|
||||
for (const auto &header: headers) {
|
||||
m_dataHeader.emplace_back(header);
|
||||
m_dataVector.emplace_back();
|
||||
}
|
||||
|
||||
while (!file.atEnd()) {
|
||||
QString dataLine = file.readLine();
|
||||
dataLine.remove(remove);
|
||||
auto strData = dataLine.split(split);
|
||||
for (int i = 0; i < strData.size(); ++i) {
|
||||
m_dataVector[i].push_back(strData[i].toDouble());
|
||||
}
|
||||
}
|
||||
|
||||
InitHeaderComboBox();
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::on_primaryColComboBox_currentIndexChanged(int index) {
|
||||
if (0 <= index) {
|
||||
ui->threadCountComboBox->setEnabled(true);
|
||||
ui->exportDataButton->setEnabled(true);
|
||||
ui->startAnalysisButton->setEnabled(true);
|
||||
m_primaryIndex = ui->primaryColComboBox->currentData().toULongLong();
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::on_dropColComboBox_currentIndexChanged(int index) {
|
||||
if (index == -1) return;
|
||||
auto colName = ui->dropColComboBox->currentText();
|
||||
auto items = ui->dropListWidget->findItems(colName, Qt::MatchFixedString);
|
||||
if (!items.size()) {
|
||||
ui->dropListWidget->addItem(colName);
|
||||
}
|
||||
ui->dropColComboBox->setCurrentIndex(-1);
|
||||
}
|
||||
|
||||
void MainWindow::on_dropListWidget_itemDoubleClicked(QListWidgetItem *item) {
|
||||
ui->dropListWidget->removeItemWidget(item);
|
||||
ui->dropListWidget->takeItem(ui->dropListWidget->currentRow());
|
||||
}
|
||||
|
||||
void MainWindow::on_threadCountComboBox_currentIndexChanged(int index) {
|
||||
if (0 <= index) {
|
||||
m_threadPool.setMaxThreadCount(ui->threadCountComboBox->currentData().toInt());
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::on_startAnalysisButton_clicked() {
|
||||
|
||||
auto const &primaryData = m_dataVector.at(m_primaryIndex);
|
||||
ui->progressBar->setMaximum(m_dataHeader.size() - 1 - ui->dropListWidget->count());
|
||||
ui->progressBar->setValue(0);
|
||||
m_dataTableModel.clear();
|
||||
for (int i = 0; i < m_dataHeader.size(); ++i) {
|
||||
if (i == m_primaryIndex || !ui->dropListWidget->findItems(m_dataHeader[i], Qt::MatchFixedString).empty()) {
|
||||
continue;
|
||||
}
|
||||
auto *pearson = new Pearson(i, primaryData, m_dataVector.at(i), this);
|
||||
connect(pearson, SIGNAL(result(uint64_t, double)), this, SLOT(PearsonResult(uint64_t, double)));
|
||||
connect(pearson, SIGNAL(finished()), this, SLOT(PearsonFinished()));
|
||||
//pearson->run();
|
||||
m_threadPool.start(pearson);
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::on_exportDataButton_clicked() {
|
||||
auto saveFileName = QFileDialog::getSaveFileName(this, "文件保存", "", "CSV File (*.csv)");
|
||||
if (m_dataTableModel.SaveFile(saveFileName)) {
|
||||
QMessageBox::information(this, "保存成功", "文件保存成功", "确定");
|
||||
} else {
|
||||
QMessageBox::warning(this, "保存失败", "文件保存失败", "确定");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::PearsonResult(uint64_t index, double pearson) {
|
||||
qDebug() << m_dataHeader[index] << " " << pearson;
|
||||
m_dataTableModel.InsertRow(m_dataHeader[index], pearson);
|
||||
}
|
||||
|
||||
void MainWindow::PearsonFinished() {
|
||||
// delete sender();
|
||||
ui->progressBar->setValue(ui->progressBar->value() + 1);
|
||||
}
|
||||
Reference in New Issue
Block a user