Implement actions

This commit is contained in:
Carl Philipp Klemm 2025-10-13 14:28:34 +02:00
parent a08cf6a682
commit 2d58946f3f
2 changed files with 115 additions and 2 deletions

View file

@ -1,6 +1,9 @@
#include <eismultiplexer.h>
#include <QMessageBox>
#include <QMessageBox>
#include <QFileDialog>
#include <QTextStream>
#include <QFile>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "triggerwidget.h"
@ -8,7 +11,9 @@
MainWindow::MainWindow(QWidget *parent):
QMainWindow(parent),
ui(new Ui::MainWindow),
codeEditor(this)
codeEditor(this),
currentFilePath(""),
isFileModified(false)
{
ui->setupUi(this);
enumerateDevices();
@ -24,6 +29,15 @@ MainWindow::MainWindow(QWidget *parent):
ui->codeLayout->addWidget(&codeEditor);
connect(ui->actionQuit, &QAction::triggered, this, [this]() {close();});
connect(ui->actionOpen, &QAction::triggered, this, &MainWindow::onActionOpenTriggered);
connect(ui->actionSave, &QAction::triggered, this, &MainWindow::onActionSaveTriggered);
connect(ui->actionSave_As, &QAction::triggered, this, &MainWindow::onActionSaveAsTriggered);
// Connect text changed signal to track modifications
connect(&codeEditor, &QTextEdit::textChanged, this, [this]() {
isFileModified = true;
updateTitle();
});
}
MainWindow::~MainWindow()
@ -31,6 +45,97 @@ MainWindow::~MainWindow()
delete ui;
}
void MainWindow::updateTitle()
{
QString windowTitle = "EisMultiplexer-Qt";
if (!currentFilePath.isEmpty()) {
windowTitle = QString("%1 - %2").arg(currentFilePath);
}
if (isFileModified) {
windowTitle += "[*]";
}
setWindowTitle(windowTitle);
}
void MainWindow::onActionOpenTriggered()
{
QString filePath = QFileDialog::getOpenFileName(this, tr("Open Python Script"),
"",
tr("Python Files (*.py);;All Files (*)"));
if (filePath.isEmpty()) {
return;
}
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::warning(this, tr("Error"), tr("Could not open file: %1").arg(filePath));
return;
}
QTextStream in(&file);
QString content = in.readAll();
file.close();
codeEditor.setPlainText(content);
currentFilePath = filePath;
isFileModified = false;
updateTitle();
}
void MainWindow::onActionSaveTriggered()
{
if (currentFilePath.isEmpty()) {
onActionSaveAsTriggered();
return;
}
QFile file(currentFilePath);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QMessageBox::warning(this, tr("Error"), tr("Could not save file: %1").arg(currentFilePath));
return;
}
QTextStream out(&file);
out << codeEditor.toPlainText();
file.close();
isFileModified = false;
updateTitle();
}
void MainWindow::onActionSaveAsTriggered()
{
QString filePath = QFileDialog::getSaveFileName(this, tr("Save Python Script"),
"",
tr("Python Files (*.py);;All Files (*)"));
if (filePath.isEmpty()) {
return;
}
// Ensure the file has a .py extension if it's a Python file
if (filePath.endsWith(".py", Qt::CaseInsensitive)) {
// File already has .py extension
} else {
filePath += ".py";
}
QFile file(filePath);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QMessageBox::warning(this, tr("Error"), tr("Could not save file: %1").arg(filePath));
return;
}
QTextStream out(&file);
out << codeEditor.toPlainText();
file.close();
currentFilePath = filePath;
isFileModified = false;
updateTitle();
}
void MainWindow::enumerateDevices()
{
size_t count = 0;

View file

@ -34,9 +34,17 @@ public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void onActionOpenTriggered();
void onActionSaveTriggered();
void onActionSaveAsTriggered();
private:
void enumerateDevices();
void generateExample();
void generateExample();
void updateTitle();
QString currentFilePath;
bool isFileModified;
};
#endif // MAINWINDOW_H