Skip to content
Snippets Groups Projects
Commit 32089b95 authored by tobiglaser's avatar tobiglaser
Browse files

added input option csv-paste

parent cda8d011
No related branches found
No related tags found
No related merge requests found
...@@ -39,11 +39,13 @@ ConfigPanel::ConfigPanel(QWidget* parent) : QDockWidget{parent} ...@@ -39,11 +39,13 @@ ConfigPanel::ConfigPanel(QWidget* parent) : QDockWidget{parent}
stackedWidget->addWidget(fileWidget); stackedWidget->addWidget(fileWidget);
QHBoxLayout* fileLayout = new QHBoxLayout(); QHBoxLayout* fileLayout = new QHBoxLayout();
fileWidget->setLayout(fileLayout); fileWidget->setLayout(fileLayout);
openButton = new QPushButton("Open..."); fileLayout->setContentsMargins(0, 0, 0, 0);
openButton = new QPushButton("Browse...");
fileWidget->layout()->addWidget(openButton); fileWidget->layout()->addWidget(openButton);
fileLine = new QLineEdit("YourFile"); fileLine = new QLineEdit();
fileLine->setPlaceholderText("/path/to/yourFile.csv");
fileWidget->layout()->addWidget(fileLine); fileWidget->layout()->addWidget(fileLine);
fileGenerateButton = new QPushButton("Generate"); fileGenerateButton = new QPushButton("Preview");
fileLayout->addWidget(fileGenerateButton); fileLayout->addWidget(fileGenerateButton);
fileLayout->addStretch(); fileLayout->addStretch();
connect(openButton, &QPushButton::clicked, this, &ConfigPanel::onOpenButton); connect(openButton, &QPushButton::clicked, this, &ConfigPanel::onOpenButton);
...@@ -53,16 +55,33 @@ ConfigPanel::ConfigPanel(QWidget* parent) : QDockWidget{parent} ...@@ -53,16 +55,33 @@ ConfigPanel::ConfigPanel(QWidget* parent) : QDockWidget{parent}
QHBoxLayout* randomLayout = new QHBoxLayout(); QHBoxLayout* randomLayout = new QHBoxLayout();
randomWidget->setLayout(randomLayout); randomWidget->setLayout(randomLayout);
randomLayout->setContentsMargins(0, 0, 0, 0);
problemSizeBox = new QSpinBox(); problemSizeBox = new QSpinBox();
randomLayout->addWidget(problemSizeBox); randomLayout->addWidget(problemSizeBox);
randomGenerateButton = new QPushButton("Generate"); randomGenerateButton = new QPushButton("Preview");
randomLayout->addWidget(randomGenerateButton); randomLayout->addWidget(randomGenerateButton);
randomLayout->addStretch(); randomLayout->addStretch();
problemSizeBox->setMinimum(1); problemSizeBox->setMinimum(1);
problemSizeBox->setMaximum(INT_MAX); problemSizeBox->setMaximum(INT_MAX);
stackedWidget->setCurrentWidget(randomWidget); pasteWidget = new QWidget();
stackedWidget->addWidget(pasteWidget);
QHBoxLayout* pasteLayout = new QHBoxLayout();
pasteWidget->setLayout(pasteLayout);
pasteLayout->setContentsMargins(0, 0, 0, 0);
pasteBox = new QPlainTextEdit();
pasteLayout->addWidget(pasteBox, 1);
pasteBox->setPlaceholderText("Paste or Drop here...");
pasteBox->setMinimumHeight(25);
pasteBox->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
pasteGenerateButton = new QPushButton("Preview");
pasteLayout->addWidget(pasteGenerateButton);
pasteLayout->addStretch();
stackedWidget->setCurrentWidget(pasteWidget);
} }
void ConfigPanel::setRunning(bool running) void ConfigPanel::setRunning(bool running)
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include <QCheckBox> #include <QCheckBox>
#include <QComboBox> #include <QComboBox>
#include <QDockWidget> #include <QDockWidget>
#include <QPlainTextEdit>
#include <QPushButton> #include <QPushButton>
#include <QSpinBox> #include <QSpinBox>
#include <QStackedWidget> #include <QStackedWidget>
...@@ -18,13 +19,16 @@ public: ...@@ -18,13 +19,16 @@ public:
QPushButton* openButton; QPushButton* openButton;
QPushButton* randomGenerateButton; QPushButton* randomGenerateButton;
QPushButton* fileGenerateButton; QPushButton* fileGenerateButton;
QPushButton* pasteGenerateButton;
QLineEdit* fileLine; QLineEdit* fileLine;
QPlainTextEdit* pasteBox;
QComboBox* algoBox; QComboBox* algoBox;
QSpinBox* problemSizeBox; QSpinBox* problemSizeBox;
QComboBox* verbosityBox; QComboBox* verbosityBox;
QStackedWidget* stackedWidget; QStackedWidget* stackedWidget;
QWidget* fileWidget; QWidget* fileWidget;
QWidget* randomWidget; QWidget* randomWidget;
QWidget* pasteWidget;
public: public:
explicit ConfigPanel(QWidget* parent = nullptr); explicit ConfigPanel(QWidget* parent = nullptr);
......
...@@ -47,6 +47,7 @@ void Control::makeConnections() ...@@ -47,6 +47,7 @@ void Control::makeConnections()
connect(w->configPanel->runButton, &QPushButton::clicked, this, &Control::onRunButton); connect(w->configPanel->runButton, &QPushButton::clicked, this, &Control::onRunButton);
connect(w->configPanel->fileGenerateButton, &QPushButton::clicked, this, &Control::onGenerateButton); connect(w->configPanel->fileGenerateButton, &QPushButton::clicked, this, &Control::onGenerateButton);
connect(w->configPanel->randomGenerateButton, &QPushButton::clicked, this, &Control::onGenerateButton); connect(w->configPanel->randomGenerateButton, &QPushButton::clicked, this, &Control::onGenerateButton);
connect(w->configPanel->pasteGenerateButton, &QPushButton::clicked, this, &Control::onGenerateButton);
connect(w->configPanel->fileLine, &QLineEdit::textChanged, this, &Control::onProblemSettingChanged); connect(w->configPanel->fileLine, &QLineEdit::textChanged, this, &Control::onProblemSettingChanged);
connect(w->configPanel->problemSizeBox, &QSpinBox::textChanged, this, &Control::onProblemSettingChanged); connect(w->configPanel->problemSizeBox, &QSpinBox::textChanged, this, &Control::onProblemSettingChanged);
connect(w, &MainWindow::saveProblem, this, &Control::saveProblem); connect(w, &MainWindow::saveProblem, this, &Control::saveProblem);
...@@ -264,8 +265,8 @@ void Control::generateProblem() ...@@ -264,8 +265,8 @@ void Control::generateProblem()
else if (w->configPanel->stackedWidget->currentWidget() == w->configPanel->fileWidget) else if (w->configPanel->stackedWidget->currentWidget() == w->configPanel->fileWidget)
{ {
std::string filename = w->configPanel->fileLine->text().toStdString(); std::string filename = w->configPanel->fileLine->text().toStdString();
CSVParser csv(filename); CSVParser csv;
bool success = csv.parse(); bool success = csv.parseFile(filename);
if (!success) if (!success)
{ {
std::string errorMessage = "Error parsing file: " + filename; std::string errorMessage = "Error parsing file: " + filename;
...@@ -284,6 +285,25 @@ void Control::generateProblem() ...@@ -284,6 +285,25 @@ void Control::generateProblem()
problem.emplace_back(cute::Point{row[0], row[1]}); problem.emplace_back(cute::Point{row[0], row[1]});
} }
} }
else if (w->configPanel->stackedWidget->currentWidget() == w->configPanel->pasteWidget)
{
std::string input = w->configPanel->pasteBox->toPlainText().toStdString();
CSVParser csv;
csv.parseString(input);
auto data = csv.getFloatRows();
for (auto&& row : data)
{
if (row.size() < 2)
continue;
else if (std::isnan(row[0]) || std::isnan(row[1]))
continue;
else if (std::isinf(row[0]) || std::isinf(row[1]))
continue;
else
problem.emplace_back(cute::Point{row[0], row[1]});
}
}
} }
void Control::onGenerateButton() void Control::onGenerateButton()
......
...@@ -26,34 +26,24 @@ Below is the complete, self-contained code: ...@@ -26,34 +26,24 @@ Below is the complete, self-contained code:
class CSVParser { class CSVParser {
public: public:
// Constructor: takes the filename, an optional delimiter (default is comma), CSVParser(char delimiter = ',') : delimiter(delimiter) {}
// and an option to skip the header line (default is false).
CSVParser(const std::string& filename, char delimiter = ',', bool skipHeader = false)
: filename(filename), delimiter(delimiter), skipHeader(skipHeader) {}
// Parses the CSV file. bool parseFile(const std::string& filename) {
bool parse() {
rows.clear(); // Ensure that rows is empty before starting.
std::ifstream file(filename); std::ifstream file(filename);
if (!file.is_open()) { if (!file.is_open()) {
std::cerr << "Error opening file: " << filename << std::endl; std::cerr << "Error opening file: " << filename << std::endl;
return false; return false;
} }
parseStream(file);
std::string line;
// If skipHeader is true, read and discard the first line.
if (skipHeader && std::getline(file, line)) {
// Optionally process or log the header if needed.
}
// Read each subsequent line and parse it.
while (std::getline(file, line)) {
rows.push_back(parseLine(line));
}
file.close(); file.close();
return true; return true;
} }
void parseString(const std::string& rawInput) {
std::istringstream input(rawInput);
parseStream(input);
}
// Returns the parsed CSV data as a vector of rows containing strings. // Returns the parsed CSV data as a vector of rows containing strings.
const std::vector<std::vector<std::string>>& getRows() const { const std::vector<std::vector<std::string>>& getRows() const {
return rows; return rows;
...@@ -83,11 +73,19 @@ public: ...@@ -83,11 +73,19 @@ public:
} }
private: private:
std::string filename; char delimiter;
char delimiter;
bool skipHeader;
std::vector<std::vector<std::string>> rows; std::vector<std::vector<std::string>> rows;
void parseStream(std::istream& input)
{
rows.clear(); // Ensure that rows is empty before starting.
std::string line;
// Read each subsequent line and parse it.
while (std::getline(input, line)) {
rows.push_back(parseLine(line));
}
}
// Helper function that parses a single line of CSV. // Helper function that parses a single line of CSV.
// It supports quoted fields and the basic escaping of quotes by doubling (""). // It supports quoted fields and the basic escaping of quotes by doubling ("").
std::vector<std::string> parseLine(const std::string& line) { std::vector<std::string> parseLine(const std::string& line) {
......
...@@ -71,6 +71,12 @@ void MainWindow::setupMenuBar() ...@@ -71,6 +71,12 @@ void MainWindow::setupMenuBar()
probM->addAction(probMenuFile); probM->addAction(probMenuFile);
connect(probMenuFile, &QAction::triggered, this, &MainWindow::switchToFile); connect(probMenuFile, &QAction::triggered, this, &MainWindow::switchToFile);
probMenuPaste = new QAction("Paste", this);
probMenuPaste->setCheckable(true);
probMenuPaste->setChecked(false);
probM->addAction(probMenuPaste);
connect(probMenuPaste, &QAction::triggered, this, &MainWindow::switchToPaste);
QAction* probSave = new QAction("Save...", this); QAction* probSave = new QAction("Save...", this);
probM->addAction(probSave); probM->addAction(probSave);
connect(probSave, &QAction::triggered, this, &MainWindow::saveProblem); connect(probSave, &QAction::triggered, this, &MainWindow::saveProblem);
...@@ -85,8 +91,10 @@ void MainWindow::switchToFile() ...@@ -85,8 +91,10 @@ void MainWindow::switchToFile()
{ {
probMenuFile->setChecked(true); probMenuFile->setChecked(true);
probMenuRandom->setChecked(false); probMenuRandom->setChecked(false);
probMenuPaste->setChecked(false);
probMenuFile->setDisabled(true); probMenuFile->setDisabled(true);
probMenuRandom->setDisabled(false); probMenuRandom->setDisabled(false);
probMenuPaste->setDisabled(false);
configPanel->stackedWidget->setCurrentWidget(configPanel->fileWidget); configPanel->stackedWidget->setCurrentWidget(configPanel->fileWidget);
} }
...@@ -94,11 +102,24 @@ void MainWindow::switchToRandom() ...@@ -94,11 +102,24 @@ void MainWindow::switchToRandom()
{ {
probMenuFile->setChecked(false); probMenuFile->setChecked(false);
probMenuRandom->setChecked(true); probMenuRandom->setChecked(true);
probMenuPaste->setChecked(false);
probMenuFile->setDisabled(false); probMenuFile->setDisabled(false);
probMenuRandom->setDisabled(true); probMenuRandom->setDisabled(true);
probMenuPaste->setDisabled(false);
configPanel->stackedWidget->setCurrentWidget(configPanel->randomWidget); configPanel->stackedWidget->setCurrentWidget(configPanel->randomWidget);
} }
void MainWindow::switchToPaste()
{
probMenuFile->setChecked(false);
probMenuRandom->setChecked(false);
probMenuPaste->setChecked(true);
probMenuFile->setDisabled(false);
probMenuRandom->setDisabled(false);
probMenuPaste->setDisabled(true);
configPanel->stackedWidget->setCurrentWidget(configPanel->pasteWidget);
}
void MainWindow::setupCharts() void MainWindow::setupCharts()
{ {
...@@ -183,4 +204,4 @@ void MainWindow::chartContextMenu(const QPoint& pos) ...@@ -183,4 +204,4 @@ void MainWindow::chartContextMenu(const QPoint& pos)
void MainWindow::changeVerbosity(int level) void MainWindow::changeVerbosity(int level)
{ {
configPanel->verbosityBox->setCurrentIndex(level); configPanel->verbosityBox->setCurrentIndex(level);
} }
\ No newline at end of file
...@@ -19,6 +19,7 @@ class MainWindow : public QMainWindow ...@@ -19,6 +19,7 @@ class MainWindow : public QMainWindow
private: private:
QAction* probMenuFile; QAction* probMenuFile;
QAction* probMenuRandom; QAction* probMenuRandom;
QAction* probMenuPaste;
void incrementChartTheme(); void incrementChartTheme();
public: public:
...@@ -46,6 +47,7 @@ private slots: ...@@ -46,6 +47,7 @@ private slots:
public slots: public slots:
void switchToFile(); void switchToFile();
void switchToRandom(); void switchToRandom();
void switchToPaste();
void changeChartTheme(int themeNumber); void changeChartTheme(int themeNumber);
void changeVerbosity(int level); void changeVerbosity(int level);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment