plotwindow can load csv files

This commit is contained in:
Robert 2020-11-18 16:24:12 +01:00
parent e5d1df9dbc
commit 81de09d563
8 changed files with 150 additions and 22 deletions

View file

@ -5,7 +5,6 @@ project(Regression)
set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON) set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON) set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC_SEARCH_PATHS include/ui) set(CMAKE_AUTOUIC_SEARCH_PATHS include/ui)
@ -18,12 +17,18 @@ file(GLOB_RECURSE source_files
file(GLOB_RECURSE include_files file(GLOB_RECURSE include_files
"include/*.hpp" "include/*.hpp"
"include/*.ui"
) )
file(GLOB_RECURSE ui_files
"include/ui/*.ui"
)
qt5_wrap_ui(target_ui_files ${ui_files})
add_executable(regression add_executable(regression
${include_files} ${include_files}
${source_files} ${source_files}
${target_ui_files}
) )
target_include_directories(regression PRIVATE target_include_directories(regression PRIVATE

View file

@ -11,9 +11,12 @@ class MainWindow : public QMainWindow
Q_OBJECT Q_OBJECT
public: public:
explicit MainWindow(QWidget* parent = nullptr); explicit MainWindow(char** argv, QWidget* parent = nullptr);
~MainWindow(); ~MainWindow();
int Failed() { return error_code; }
private: private:
Ui::MainWindow* ui; Ui::MainWindow* ui;
int error_code;
}; };

28
include/PlotWidget.hpp Normal file
View file

@ -0,0 +1,28 @@
#pragma once
#include <QWidget>
#include <vector>
namespace Ui {
class PlotWidget;
}
typedef struct {
double x, y;
} DataPoint;
class PlotWidget : public QWidget
{
Q_OBJECT
public:
PlotWidget(QWidget* parent = nullptr);
~PlotWidget();
int SetDataSource(const std::string& filename);
private:
Ui::PlotWidget* ui;
std::vector<DataPoint>* data;
};

View file

@ -11,27 +11,20 @@
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
<string>MainWindow</string> <string>Regression</string>
</property> </property>
<widget class="QWidget" name="centralwidget"> <widget class="QWidget" name="centralwidget">
<widget class="QLabel" name="label"> <widget class="PlotWidget" name="plot" native="true">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>146</x> <x>-1</x>
<y>102</y> <y>-1</y>
<width>271</width> <width>801</width>
<height>91</height> <height>551</height>
</rect> </rect>
</property> </property>
<property name="font"> <property name="autoFillBackground">
<font> <bool>false</bool>
<pointsize>28</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>TextLabel</string>
</property> </property>
</widget> </widget>
</widget> </widget>
@ -41,12 +34,20 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>800</width> <width>800</width>
<height>21</height> <height>26</height>
</rect> </rect>
</property> </property>
</widget> </widget>
<widget class="QStatusBar" name="statusbar"/> <widget class="QStatusBar" name="statusbar"/>
</widget> </widget>
<customwidgets>
<customwidget>
<class>PlotWidget</class>
<extends>QWidget</extends>
<header>PlotWidget.hpp</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/> <resources/>
<connections/> <connections/>
</ui> </ui>

36
include/ui/PlotWidget.ui Normal file
View file

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>PlotWidget</class>
<widget class="QWidget" name="PlotWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>718</width>
<height>572</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<widget class="QTextBrowser" name="tmp_CSVDisplay">
<property name="geometry">
<rect>
<x>35</x>
<y>31</y>
<width>651</width>
<height>511</height>
</rect>
</property>
<property name="html">
<string>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt;
&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
p, li { white-space: pre-wrap; }
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'MS Shell Dlg 2'; font-size:6.6pt; font-weight:400; font-style:normal;&quot;&gt;
&lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>

View file

@ -1,11 +1,12 @@
#include "MainWindow.hpp" #include "MainWindow.hpp"
#include "ui_MainWindow.h" #include "ui_MainWindow.h"
MainWindow::MainWindow(QWidget* parent) : MainWindow::MainWindow(char** argv, QWidget* parent) :
QMainWindow(parent), ui(new Ui::MainWindow) QMainWindow(parent), ui(new Ui::MainWindow)
{ {
ui->setupUi(this); ui->setupUi(this);
ui->label->setText("Hello, World!");
error_code = ui->plot->SetDataSource(argv[1]);
} }
MainWindow::~MainWindow() MainWindow::~MainWindow()

50
src/PlotWidget.cpp Normal file
View file

@ -0,0 +1,50 @@
#include "PlotWidget.hpp"
#include "ui_PlotWidget.h"
#include <iostream>
#include <qmessagebox.h>
#include "csv.h"
PlotWidget::PlotWidget(QWidget* parent) :
QWidget(parent), ui(new Ui::PlotWidget), data(nullptr)
{
ui->setupUi(this);
}
PlotWidget::~PlotWidget()
{
delete data;
delete ui;
}
int PlotWidget::SetDataSource(const std::string& filename)
{
try
{
io::CSVReader<2> reader(filename);
data = new std::vector<DataPoint>;
DataPoint dp;
while (reader.read_row(dp.x, dp.y))
data->push_back(dp);
}
catch (io::error::base& err)
{
if (data != nullptr) delete data;
QMessageBox::critical(this, "Error", err.what());
return 1;
}
// TODO: Remove, just for testing the CSV reader
QString tmp_string = "<table>";
for (DataPoint& dp : *data)
tmp_string += QString::asprintf("<tr><td>%f</td><td>%f</td></tr>", dp.x, dp.y);
tmp_string += "</table>";
ui->tmp_CSVDisplay->insertHtml(tmp_string);
return 0;
}

View file

@ -25,7 +25,11 @@ int main(int argc, char** argv)
return 1; return 1;
} }
MainWindow window; MainWindow window(argv);
int err_code = window.Failed();
if (err_code)
return err_code;
window.show(); window.show();
return app.exec(); return app.exec();