Implement actions
This commit is contained in:
parent
a08cf6a682
commit
2d58946f3f
107
mainwindow.cpp
107
mainwindow.cpp
|
@ -1,6 +1,9 @@
|
||||||
#include <eismultiplexer.h>
|
#include <eismultiplexer.h>
|
||||||
#include <QMessageBox>
|
#include <QMessageBox>
|
||||||
#include <QMessageBox>
|
#include <QMessageBox>
|
||||||
|
#include <QFileDialog>
|
||||||
|
#include <QTextStream>
|
||||||
|
#include <QFile>
|
||||||
#include "mainwindow.h"
|
#include "mainwindow.h"
|
||||||
#include "ui_mainwindow.h"
|
#include "ui_mainwindow.h"
|
||||||
#include "triggerwidget.h"
|
#include "triggerwidget.h"
|
||||||
|
@ -8,7 +11,9 @@
|
||||||
MainWindow::MainWindow(QWidget *parent):
|
MainWindow::MainWindow(QWidget *parent):
|
||||||
QMainWindow(parent),
|
QMainWindow(parent),
|
||||||
ui(new Ui::MainWindow),
|
ui(new Ui::MainWindow),
|
||||||
codeEditor(this)
|
codeEditor(this),
|
||||||
|
currentFilePath(""),
|
||||||
|
isFileModified(false)
|
||||||
{
|
{
|
||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
enumerateDevices();
|
enumerateDevices();
|
||||||
|
@ -24,6 +29,15 @@ MainWindow::MainWindow(QWidget *parent):
|
||||||
ui->codeLayout->addWidget(&codeEditor);
|
ui->codeLayout->addWidget(&codeEditor);
|
||||||
|
|
||||||
connect(ui->actionQuit, &QAction::triggered, this, [this]() {close();});
|
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()
|
MainWindow::~MainWindow()
|
||||||
|
@ -31,6 +45,97 @@ MainWindow::~MainWindow()
|
||||||
delete ui;
|
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()
|
void MainWindow::enumerateDevices()
|
||||||
{
|
{
|
||||||
size_t count = 0;
|
size_t count = 0;
|
||||||
|
|
10
mainwindow.h
10
mainwindow.h
|
@ -34,9 +34,17 @@ public:
|
||||||
explicit MainWindow(QWidget *parent = nullptr);
|
explicit MainWindow(QWidget *parent = nullptr);
|
||||||
~MainWindow();
|
~MainWindow();
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void onActionOpenTriggered();
|
||||||
|
void onActionSaveTriggered();
|
||||||
|
void onActionSaveAsTriggered();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void enumerateDevices();
|
void enumerateDevices();
|
||||||
void generateExample();
|
void generateExample();
|
||||||
|
void updateTitle();
|
||||||
|
QString currentFilePath;
|
||||||
|
bool isFileModified;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // MAINWINDOW_H
|
#endif // MAINWINDOW_H
|
||||||
|
|
Loading…
Reference in a new issue