summaryrefslogtreecommitdiff
path: root/src/mainwindow.cc
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2018-12-27 11:05:52 +0100
committerBent Bisballe Nyeng <deva@aasimon.org>2018-12-27 11:05:52 +0100
commitf4729babb394fbd41b55fba5c53bfc6afe1cda09 (patch)
tree492f7696ed5d36d238ddd14048800ff919930fad /src/mainwindow.cc
parentcf19a88c5a37fead401df39849ad7ba56aaa851d (diff)
Add file menu with load/save etc.
Diffstat (limited to 'src/mainwindow.cc')
-rw-r--r--src/mainwindow.cc267
1 files changed, 251 insertions, 16 deletions
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 <QToolBar>
#include <QAction>
#include <QFile>
+#include <QMenu>
+#include <QMenuBar>
+#include <QMessageBox>
+#include <QStatusBar>
+#include <QFileInfo>
+#include <QFileDialog>
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();
}