1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set et sw=2 ts=2: */
/***************************************************************************
* mainwindow.cc
*
* Sat Aug 4 13:37:18 CEST 2012
* Copyright 2012 Bent Bisballe Nyeng
* deva@aasimon.org
****************************************************************************/
/*
* This file is part of Kaiman.
*
* Kaiman is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Kaiman is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Kaiman; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#include "mainwindow.h"
#include "outputwindow.h"
#include <QApplication>
#include <QSplitter>
#include <QToolBar>
#include <QAction>
#include <QFile>
#include <QMenu>
#include <QMenuBar>
#include <QMessageBox>
#include <QStatusBar>
#include <QFileInfo>
#include <QFileDialog>
#include <QSettings>
MainWindow::MainWindow(QString p)
{
// Watch file on disk?
//connect(&watcher, SIGNAL(fileChanged(const QString &)),
// this, SLOT(reset()));
//watcher.addPath(program);
splitter = new QSplitter();
setCentralWidget(splitter);
editor = new CodeEditor();
splitter->addWidget(editor);
connect(editor, SIGNAL(textChanged()), this, SLOT(programChanged()));
highlighter = new Highlighter(editor->document());
out = new OutputWindow();
splitter->addWidget(out);
l = new LUAScript(*out);
connect(l, SIGNAL(lineChanged(int)), editor, SLOT(runningLine(int)));
connect(l, SIGNAL(errorLine(QString, int, QString)),
editor, SLOT(errorLine(QString, int, QString)));
// 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();
toolbar->setObjectName("Execution");
addToolBar(Qt::TopToolBarArea, toolbar);
{
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()));
}
{
QAction *act = toolbar->addAction("Image");
connect(act, SIGNAL(triggered()), this, SLOT(image()));
}
loadSettings();
statusBar()->showMessage(tr("Ready"));
updateWindowTitle();
setWindowEnabled(false);
reset();
if(!p.isEmpty())
{
loadFile(p);
}
}
void MainWindow::closeEvent(QCloseEvent* event)
{
if(!checkDirty())
{
event->ignore();
return;
}
saveSettings();
event->accept();
}
void MainWindow::loadSettings()
{
QByteArray state;
QByteArray geometry;
QSettings settings;
settings.beginGroup("MainWindow");
state = settings.value("state").toByteArray();
geometry = settings.value("geometry").toByteArray();
splitter->restoreState(settings.value("splitter").toByteArray());
settings.endGroup();
settings.beginGroup("Canvas");
out->setScale(settings.value("scale", 2.0).toDouble());
settings.endGroup();
restoreGeometry(geometry);
restoreState(state);
}
void MainWindow::saveSettings()
{
QSettings settings;
settings.beginGroup("MainWindow");
settings.setValue("state", saveState());
settings.setValue("geometry", saveGeometry());
settings.setValue("splitter", splitter->saveState());
settings.endGroup();
settings.beginGroup("Canvas");
settings.setValue("scale", out->getScale());
settings.endGroup();
}
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)
{
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);
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();
}
void MainWindow::image()
{
QString filename
= QFileDialog::getSaveFileName(this, tr("Save Kaiman File"),
"",
tr("Image Files (*.png)"));
if(filename == "")
{
// User clicked cancel
return;
}
if(filename.right(4) != ".png")
{
filename += ".png";
}
auto image = out->acquire(true);
image.save(filename);
}
void MainWindow::programChanged()
{
dirty = true;
updateWindowTitle();
}
|