1 // Copyright (c) 2012 Dennco Project
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 // Created by tkawata on Sep-30, 2012.
21 #include "mainwindow.h"
22 #include "ui_mainwindow.h"
24 #include "dccreator.h"
25 #include "dccontent.h"
26 #include "dcglvisualizerwidget.h"
27 #include "dctreeviewwidget.h"
28 #include "TKConsole.h"
29 #include "utils/dcresources.h"
30 #include "utils/dcskeltoncreatorutil.h"
32 #include <QCloseEvent>
33 #include <QFileDialog>
34 #include <QMessageBox>
36 #include <QLocalSocket>
39 const QString DENNCO_ENGINE = "QtDennco.exe";
46 class SleeperThread : public QThread
49 static void msleep(unsigned long msecs)
51 QThread::msleep(msecs);
55 MainWindow::MainWindow(QWidget *parent) :
57 ui(new Ui::MainWindow)
61 DCResources::initResources();
70 TKLog::setDestination(new TKConsole);
72 d_IPCServerName = "denncoCreator_";
73 QTextStream s(&d_IPCServerName);
77 d_creator = new DCCreator(this);
78 d_visualizerWidget = new DCGLVisualizerWidget(d_creator, this) ;
79 setCentralWidget(d_visualizerWidget);
81 d_treeViewWidget = new DCTreeViewWidget(this, d_creator);
82 ui->treeViewDock->layout()->addWidget(d_treeViewWidget);
84 setCurrentContent("");
85 setUnifiedTitleAndToolBarOnMac(true);
88 MainWindow::~MainWindow()
97 void MainWindow::closeEvent(QCloseEvent *event)
108 void MainWindow::newFile()
110 QString dirName = QFileDialog::getSaveFileName(this, tr("Create new content"));
111 if (!dirName.isEmpty() && DCSkeltonCreatorUtil::createNewContent(dirName))
113 loadContent(dirName);
117 void MainWindow::open()
120 QString dirName = QFileDialog::getExistingDirectory(this, tr("Open a content"));
121 if (!dirName.isEmpty())
122 loadContent(dirName);
128 bool MainWindow::save()
130 return saveContent();
133 bool MainWindow::saveAs()
139 void MainWindow::about()
141 QMessageBox::about(this, tr("About Application"),
142 tr("TODO: exampalin
\81@about <b>dennco creator</b> "
143 "TODO: exampalin
\81@about <b>dennco creator</b> "
144 "TODO: exampalin
\81@about <b>dennco creator</b>"));
147 void MainWindow::documentWasModified()
150 // setWindowModified(textEdit->document()->isModified());
155 void MainWindow::createActions()
158 newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this);
159 newAct->setShortcuts(QKeySequence::New);
160 newAct->setStatusTip(tr("Create a new file"));
161 connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
164 openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this);
165 openAct->setShortcuts(QKeySequence::Open);
166 openAct->setStatusTip(tr("Open an existing file"));
167 connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
170 saveAct = new QAction(QIcon(":/images/save.png"), tr("&Save"), this);
171 saveAct->setShortcuts(QKeySequence::Save);
172 saveAct->setStatusTip(tr("Save the document to disk"));
173 connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
175 saveAsAct = new QAction(tr("Save &As..."), this);
176 saveAsAct->setShortcuts(QKeySequence::SaveAs);
177 saveAsAct->setStatusTip(tr("Save the document under a new name"));
178 connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
181 exitAct = new QAction(tr("E&xit"), this);
182 exitAct->setShortcuts(QKeySequence::Quit);
184 exitAct->setStatusTip(tr("Exit the application"));
185 connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
188 cutAct = new QAction(QIcon(":/images/cut.png"), tr("Cu&t"), this);
190 cutAct->setShortcuts(QKeySequence::Cut);
191 cutAct->setStatusTip(tr("Cut the current selection's contents to the "
193 // connect(cutAct, SIGNAL(triggered()), textEdit, SLOT(cut()));
195 copyAct = new QAction(QIcon(":/images/copy.png"), tr("&Copy"), this);
196 copyAct->setShortcuts(QKeySequence::Copy);
197 copyAct->setStatusTip(tr("Copy the current selection's contents to the "
199 // connect(copyAct, SIGNAL(triggered()), textEdit, SLOT(copy()));
201 pasteAct = new QAction(QIcon(":/images/paste.png"), tr("&Paste"), this);
202 pasteAct->setShortcuts(QKeySequence::Paste);
203 pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current "
206 // connect(pasteAct, SIGNAL(triggered()), textEdit, SLOT(paste()));
208 playAct = new QAction(QIcon(":/images/playbutton.png"), tr("Play"), this);
209 playAct->setStatusTip(tr("Play the editing content"));
211 connect(playAct, SIGNAL(triggered()), this, SLOT(playContent()));
213 aboutAct = new QAction(tr("&About"), this);
214 aboutAct->setStatusTip(tr("Show the application's About box"));
215 connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
217 cutAct->setEnabled(false);
218 copyAct->setEnabled(false);
219 playAct->setEnabled(false);
220 // connect(textEdit, SIGNAL(copyAvailable(bool)),
221 // cutAct, SLOT(setEnabled(bool)));
222 // connect(textEdit, SIGNAL(copyAvailable(bool)),
223 // copyAct, SLOT(setEnabled(bool)));
228 void MainWindow::createMenus()
231 fileMenu = menuBar()->addMenu(tr("&File"));
232 fileMenu->addAction(newAct);
234 fileMenu->addAction(openAct);
236 fileMenu->addAction(saveAct);
238 fileMenu->addAction(saveAsAct);
239 fileMenu->addSeparator();
240 fileMenu->addAction(exitAct);
242 editMenu = menuBar()->addMenu(tr("&Edit"));
243 editMenu->addAction(cutAct);
244 editMenu->addAction(copyAct);
245 editMenu->addAction(pasteAct);
247 menuBar()->addSeparator();
249 editMenu = menuBar()->addMenu(tr("&Play"));
250 editMenu->addAction(playAct);
252 menuBar()->addSeparator();
254 helpMenu = menuBar()->addMenu(tr("&Help"));
255 helpMenu->addAction(aboutAct);
258 void MainWindow::createToolBars()
260 fileToolBar = addToolBar(tr("File"));
261 fileToolBar->addAction(newAct);
262 fileToolBar->addAction(openAct);
263 fileToolBar->addAction(saveAct);
265 editToolBar = addToolBar(tr("Edit"));
266 editToolBar->addAction(cutAct);
267 editToolBar->addAction(copyAct);
268 editToolBar->addAction(pasteAct);
270 playToolBar = addToolBar(tr("Play"));
271 playToolBar->addAction(playAct);
274 void MainWindow::createStatusBar()
277 statusBar()->showMessage(tr("Ready"));
282 void MainWindow::readSettings()
285 QSettings settings("dennco project", "dennco creator");
286 QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
287 QSize size = settings.value("size", QSize(400, 400)).toSize();
294 void MainWindow::writeSettings()
297 QSettings settings("dennco project", "dennco creator");
298 settings.setValue("pos", pos());
299 settings.setValue("size", size());
304 bool MainWindow::maybeSave()
307 // if (textEdit->document()->isModified()) {
308 // QMessageBox::StandardButton ret;
309 // ret = QMessageBox::warning(this, tr("Application"),
310 // tr("The document has been modified.\n"
311 // "Do you want to save your changes?"),
312 // QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
313 // if (ret == QMessageBox::Save)
315 // else if (ret == QMessageBox::Cancel)
323 void MainWindow::loadContent(const QString &contentDirectory)
328 if (d_creator->loadContent(contentDirectory))
330 playAct->setEnabled(true);
334 playAct->setEnabled(false);
337 setCurrentContent(contentDirectory);
340 bool MainWindow::saveContent()
345 if (d_creator->getCurrentContainer())
347 d_creator->saveAll(true);
353 void MainWindow::setCurrentContent(const QString &contentDirectory)
355 curContent = contentDirectory;
356 // textEdit->document()->setModified(false);
357 setWindowModified(false);
359 QString shownName = curContent;
360 if (curContent.isEmpty())
361 shownName = "untitled.txt";
362 setWindowFilePath(shownName);
365 QString MainWindow::strippedName(const QString &fullFileName)
367 return QFileInfo(fullFileName).fileName();
370 void MainWindow::playContent()
372 if (!d_creator || !d_creator->getCurrentContent())
375 if (d_player.state() != QProcess::NotRunning)
377 QLocalSocket *socket = new QLocalSocket(this);
378 connect(socket, SIGNAL(disconnected()), socket,SLOT(deleteLater()));
380 for (int retry = 0; retry < 10; retry++)
382 socket->connectToServer(d_IPCServerName);
383 if (socket->waitForConnected())
387 SleeperThread::msleep(500);
389 QString requestData = "close,";
390 socket->write(requestData.toLocal8Bit());
395 if (!d_player.waitForFinished(5000))
398 if (d_player.state() == QProcess::NotRunning)
401 args << "-creatorControlled";
402 args << d_IPCServerName;
404 d_player.start(DENNCO_ENGINE, args);
405 d_player.waitForStarted(5000);
408 if (d_player.state() == QProcess::Running)
410 QLocalSocket *socket = new QLocalSocket(this);
411 connect(socket, SIGNAL(disconnected()), socket,SLOT(deleteLater()));
412 for (int retry = 0; retry < 10; retry++)
414 socket->connectToServer(d_IPCServerName);
415 if (socket->waitForConnected())
419 SleeperThread::msleep(500);
421 QString requestData = "load,";
422 requestData.append(d_creator->getCurrentContent()->getContentRootPath());
423 socket->write(requestData.toLocal8Bit());