From f4729babb394fbd41b55fba5c53bfc6afe1cda09 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Thu, 27 Dec 2018 11:05:52 +0100 Subject: Add file menu with load/save etc. --- src/mainwindow.cc | 267 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 251 insertions(+), 16 deletions(-) (limited to 'src/mainwindow.cc') diff --git a/src/mainwindow.cc b/src/mainwindow.cc index bd52c0c..e2fe4b4 100644 --- a/src/mainwindow.cc +++ b/src/mainwindow.cc @@ -34,11 +34,15 @@ #include #include #include +#include +#include +#include +#include +#include +#include MainWindow::MainWindow(QString p) { - program = p; - // Watch file on disk? //connect(&watcher, SIGNAL(fileChanged(const QString &)), // this, SLOT(reset())); @@ -49,39 +53,270 @@ MainWindow::MainWindow(QString p) editor = new CodeEditor(); splitter->addWidget(editor); - QFile file(program); - file.open(QIODevice::ReadOnly); - editor->setPlainText(file.readAll()); - file.close(); + connect(editor, SIGNAL(textChanged()), this, SLOT(programChanged())); out = new OutputWindow(); splitter->addWidget(out); - l = new LUAScript(out, program); + l = new LUAScript(*out); connect(l, SIGNAL(lineChanged(int)), editor, SLOT(runningLine(int))); + // Setup menu + QMenu* fileMenu = menuBar()->addMenu(tr("&File")); + + { + QAction* act = new QAction(tr("&New File"), this); + fileMenu->addAction(act); + connect(act, SIGNAL(triggered()), this, SLOT(newFile())); + } + + { + QAction* act = new QAction(tr("&Load File..."), this); + fileMenu->addAction(act); + connect(act, SIGNAL(triggered()), this, SLOT(loadFile())); + } + + { + act_save = new QAction(tr("&Save File"), this); + fileMenu->addAction(act_save); + connect(act_save, SIGNAL(triggered()), this, SLOT(saveFile())); + } + + { + QAction* act = new QAction(tr("Save File As..."), this); + fileMenu->addAction(act); + connect(act, SIGNAL(triggered()), this, SLOT(saveFileAs())); + } + + { + QAction* act = new QAction(tr("&Quit"), this); + fileMenu->addAction(act); + connect(act, SIGNAL(triggered()), this, SLOT(close())); + } + + // Setup toolbar QToolBar *toolbar = new QToolBar(); addToolBar(Qt::TopToolBarArea, toolbar); - QAction *act_run = toolbar->addAction("Run"); - connect(act_run, SIGNAL(triggered()), this, SLOT(reset())); - QAction *act_stop = toolbar->addAction("Stop"); - connect(act_stop, SIGNAL(triggered()), l, SLOT(stopScript())); + { + QAction *act = toolbar->addAction("Start"); + connect(act, SIGNAL(triggered()), this, SLOT(start())); + } + + { + QAction *act = toolbar->addAction("Stop"); + connect(act, SIGNAL(triggered()), this, SLOT(stop())); + } + + { + QAction *act = toolbar->addAction("Reset"); + connect(act, SIGNAL(triggered()), this, SLOT(reset())); + } + + statusBar()->showMessage(tr("Ready")); + updateWindowTitle(); + + setWindowEnabled(false); reset(); + + if(!p.isEmpty()) + { + loadFile(p); + } } -void MainWindow::reset() +void MainWindow::closeEvent(QCloseEvent* event) +{ + if(!checkDirty()) + { + event->ignore(); + return; + } + + event->accept(); +} + +bool MainWindow::checkDirty() +{ + if(dirty) + { + int ret = + QMessageBox::question(this, tr("Save before closing project?"), + tr("The file has changed. Do you want to save " + "before closing?"), + QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel); + switch(ret) + { + case QMessageBox::Yes: + saveFile(); + if(dirty) + { + // Project still dirty - it was not saved (user pressed cancel?) + return false; + } + break; + case QMessageBox::No: + // Proceed to quit + break; + case QMessageBox::Cancel: + return false; + default: + break; + } + } + + return true; // not dirty or user chose not to save +} + +void MainWindow::setWindowEnabled(bool enabled) +{ + editor->setEnabled(enabled); +} + +void MainWindow::updateWindowTitle() +{ + QString window_title_string; + + if(program != "") + { + window_title_string += " (" + QFileInfo(program).fileName() + ")"; + } + + act_save->setEnabled(!program.isEmpty()); + + if(dirty) + { + window_title_string += "*"; + } + + setWindowTitle("Kaiman - " + window_title_string); +} + +void MainWindow::newFile() +{ + if(!checkDirty()) + { + return; + } + + reset(); + + setWindowEnabled(true); +} + +void MainWindow::loadFile() +{ + if(!checkDirty()) + { + return; + } + + QString filename = + QFileDialog::getOpenFileName(this, + tr("Load Kaiman File"), + "", + tr("Kaiman Files (*.lua)")); + if(filename == "") + { + // User clicked cancel + return; + } + + loadFile(filename); +} + +void MainWindow::loadFile(QString filename) { - printf("Resetting...\n"); + QFile file(filename); + if(!file.open(QIODevice::ReadOnly)) + { + return; + } + editor->setPlainText(file.readAll()); + file.close(); + + l->setScriptFile(filename); + + program = filename; + + dirty = false; + updateWindowTitle(); + + statusBar()->showMessage(tr("Loaded")); + setWindowEnabled(true); +} + +void MainWindow::saveFile() +{ + QString filename = program; + if(filename == "") + { + saveFileAs(); + return; + } + QFile file(program); - file.open(QIODevice::WriteOnly); + if(!file.open(QIODevice::WriteOnly)) + { + return; + } QString code = editor->toPlainText(); file.write(code.toLocal8Bit()); file.close(); + + dirty = false; + updateWindowTitle(); + + statusBar()->showMessage(tr("Saved")); +} + +void MainWindow::saveFileAs() +{ + QString filename + = QFileDialog::getSaveFileName(this, tr("Save Kaiman File"), + "", + tr("Kaiman Files (*.lua)")); + + if(filename == "") + { + // User clicked cancel + return; + } + + if(filename.right(4) != ".lua") + { + filename += ".lua"; + } + + program = filename; + + saveFile(); +} + +void MainWindow::start() +{ + reset(); + + saveFile(); + + l->start(); +} + +void MainWindow::stop() +{ out->stopScript(); l->stopScript(); +} + +void MainWindow::reset() +{ + stop(); out->reset(); - l->start(); - printf("Reset done\n"); +} + +void MainWindow::programChanged() +{ + dirty = true; + updateWindowTitle(); } -- cgit v1.2.3