summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/kaiman.cc6
-rw-r--r--src/luascript.cc28
-rw-r--r--src/luascript.h5
-rw-r--r--src/mainwindow.cc267
-rw-r--r--src/mainwindow.h21
5 files changed, 294 insertions, 33 deletions
diff --git a/src/kaiman.cc b/src/kaiman.cc
index 8a605e1..d7a2707 100644
--- a/src/kaiman.cc
+++ b/src/kaiman.cc
@@ -40,10 +40,10 @@ void __attribute__ ((constructor)) premain()
int main(int argc, char *argv[])
{
- if(argc != 2)
+ QString file;
+ if(argc == 2)
{
- printf("Usage: %s script\n", argv[0]);
- return 1;
+ file = argv[1];
}
// QLocale::setDefault(QLocale(QLocale::Danish, QLocale::Denmark));
diff --git a/src/luascript.cc b/src/luascript.cc
index e023a1d..dce5e2c 100644
--- a/src/luascript.cc
+++ b/src/luascript.cc
@@ -81,7 +81,7 @@ static int _forward(lua_State *L)
lua_error(L);
}
- lua->out->forward(x * 5);
+ lua->out.forward(x * 5);
return 0;
}
@@ -109,8 +109,8 @@ static int _coord(lua_State *L)
lua_error(L);
}
- lua_pushnumber(L, lua->out->coordX());
- lua_pushnumber(L, lua->out->coordY());
+ lua_pushnumber(L, lua->out.coordX());
+ lua_pushnumber(L, lua->out.coordY());
return 2;
}
@@ -142,7 +142,7 @@ static int _loadpen(lua_State *L)
lua_error(L);
}
- lua->out->loadPen(pen);
+ lua->out.loadPen(pen);
return 0;
}
@@ -174,7 +174,7 @@ static int _speed(lua_State *L)
lua_error(L);
}
- lua->out->setSpeed(x);
+ lua->out.setSpeed(x);
return 0;
}
@@ -224,7 +224,7 @@ static int _scale(lua_State *L)
lua_error(L);
}
- lua->out->setScale(x);
+ lua->out.setScale(x);
return 0;
}
@@ -259,7 +259,7 @@ static int _colour(lua_State *L)
lua_error(L);
}
- lua->out->setColour(r,g,b,a);
+ lua->out.setColour(r,g,b,a);
return 0;
}
@@ -291,7 +291,7 @@ static int _turn(lua_State *L)
lua_error(L);
}
- lua->out->turn(-x * 10);
+ lua->out.turn(-x * 10);
return 0;
}
@@ -336,7 +336,7 @@ static int _reset(lua_State *L)
lua_error(L);
}
- lua->out->reset();
+ lua->out.reset();
return 0;
}
@@ -372,12 +372,11 @@ void hook(lua_State *L, lua_Debug *ar)
//printf(" currentline: %d\n", ar->currentline);
}
-LUAScript::LUAScript(OutputWindow *o, QString f)
+LUAScript::LUAScript(OutputWindow& o)
+ : out(o)
{
DEBUG(luascript, "LUAScript()\n");
- file = f;
L = NULL;
- out = o;
// Don't call init from here, it need to be done in the thread.
@@ -508,6 +507,11 @@ void LUAScript::runScript()
}
}
+void LUAScript::setScriptFile(QString file)
+{
+ this->file = file;
+}
+
void LUAScript::sleep(int n)
{
::sleep(n);
diff --git a/src/luascript.h b/src/luascript.h
index c7ea9b7..b88e023 100644
--- a/src/luascript.h
+++ b/src/luascript.h
@@ -51,7 +51,7 @@ public:
std::string msg;
};
- LUAScript(OutputWindow *out, QString file);
+ LUAScript(OutputWindow& out);
~LUAScript();
void init();
@@ -59,8 +59,9 @@ public:
void run();
void runScript();
+ void setScriptFile(QString file);
- OutputWindow *out;
+ OutputWindow& out;
void lineChange(int lineno);
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();
}
diff --git a/src/mainwindow.h b/src/mainwindow.h
index 1138dfb..a2e2271 100644
--- a/src/mainwindow.h
+++ b/src/mainwindow.h
@@ -42,10 +42,31 @@ class MainWindow
public:
MainWindow(QString program);
+protected:
+ void closeEvent(QCloseEvent*);
+
private slots:
+ // Menubar
+ void newFile();
+ void loadFile();
+ void loadFile(QString filename);
+ void saveFile();
+ void saveFileAs();
+
+ // Toolbar
+ void stop();
+ void start();
void reset();
+ void programChanged();
+
private:
+ bool checkDirty();
+ void setWindowEnabled(bool enabled);
+ void updateWindowTitle();
+
+ bool dirty{false};
+ QAction* act_save;
CodeEditor *editor;
QFileSystemWatcher watcher;