#include "ui_mainwindow.h"
#include "dccreator.h"
+#include "dccontent.h"
#include "dcglvisualizerwidget.h"
#include "dctreeviewwidget.h"
#include "TKConsole.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QSettings>
+#include <QLocalSocket>
+
+#ifdef Q_WS_WIN
+ const QString DENNCO_ENGINE = "QtDennco.exe";
+#elif Q_WS_MACX
+
+#elif Q_OS_UNIX
+
+#endif
+
+class SleeperThread : public QThread
+{
+public:
+ static void msleep(unsigned long msecs)
+ {
+ QThread::msleep(msecs);
+ }
+};
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
TKLog::setDestination(new TKConsole);
- mCreator = new DCCreator(this);
- mVisualizerWidget = new DCGLVisualizerWidget(mCreator, this) ;
- setCentralWidget(mVisualizerWidget);
+ d_IPCServerName = "denncoCreator_";
+ QTextStream s(&d_IPCServerName);
+ s.setIntegerBase(16);
+ s << qrand();
- mTreeViewWidget = new DCTreeViewWidget(this, mCreator);
- ui->treeViewDock->layout()->addWidget(mTreeViewWidget);
+ d_creator = new DCCreator(this);
+ d_visualizerWidget = new DCGLVisualizerWidget(d_creator, this) ;
+ setCentralWidget(d_visualizerWidget);
+
+ d_treeViewWidget = new DCTreeViewWidget(this, d_creator);
+ ui->treeViewDock->layout()->addWidget(d_treeViewWidget);
setCurrentContent("");
setUnifiedTitleAndToolBarOnMac(true);
MainWindow::~MainWindow()
{
delete ui;
- if (mCreator)
- delete mCreator;
+ if (d_creator)
+ delete d_creator;
}
pasteAct->setShortcuts(QKeySequence::Paste);
pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current "
"selection"));
-// connect(pasteAct, SIGNAL(triggered()), textEdit, SLOT(paste()));
+
+ // connect(pasteAct, SIGNAL(triggered()), textEdit, SLOT(paste()));
+
+ playAct = new QAction(QIcon(":/images/playbutton.png"), tr("Play"), this);
+ playAct->setStatusTip(tr("Play the editing content"));
+
+ connect(playAct, SIGNAL(triggered()), this, SLOT(playContent()));
aboutAct = new QAction(tr("&About"), this);
aboutAct->setStatusTip(tr("Show the application's About box"));
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
cutAct->setEnabled(false);
-//! [23] //! [24]
copyAct->setEnabled(false);
+ playAct->setEnabled(false);
// connect(textEdit, SIGNAL(copyAvailable(bool)),
// cutAct, SLOT(setEnabled(bool)));
// connect(textEdit, SIGNAL(copyAvailable(bool)),
menuBar()->addSeparator();
+ editMenu = menuBar()->addMenu(tr("&Play"));
+ editMenu->addAction(playAct);
+
+ menuBar()->addSeparator();
+
helpMenu = menuBar()->addMenu(tr("&Help"));
helpMenu->addAction(aboutAct);
}
-//! [27]
-//! [29] //! [30]
void MainWindow::createToolBars()
{
fileToolBar = addToolBar(tr("File"));
fileToolBar->addAction(newAct);
-//! [29] //! [31]
fileToolBar->addAction(openAct);
-//! [31]
fileToolBar->addAction(saveAct);
editToolBar = addToolBar(tr("Edit"));
editToolBar->addAction(cutAct);
editToolBar->addAction(copyAct);
editToolBar->addAction(pasteAct);
+
+ playToolBar = addToolBar(tr("Play"));
+ playToolBar->addAction(playAct);
}
-//! [30]
-//! [32]
void MainWindow::createStatusBar()
//! [32] //! [33]
{
//! [42]
void MainWindow::loadContent(const QString &contentDirectory)
{
- if (!mCreator)
+ if (!d_creator)
return;
- mCreator->loadContent(contentDirectory);
+ if (d_creator->loadContent(contentDirectory))
+ {
+ playAct->setEnabled(true);
+ }
+ else
+ {
+ playAct->setEnabled(false);
+ }
setCurrentContent(contentDirectory);
}
bool MainWindow::saveContent()
{
- if (!mCreator)
+ if (!d_creator)
return false;
- if (mCreator->getCurrentContainer())
+ if (d_creator->getCurrentContainer())
{
- mCreator->saveAll(true);
+ d_creator->saveAll(true);
}
return true;
shownName = "untitled.txt";
setWindowFilePath(shownName);
}
-//! [47]
-//! [48]
QString MainWindow::strippedName(const QString &fullFileName)
-//! [48] //! [49]
{
return QFileInfo(fullFileName).fileName();
}
-//! [49]
+
+void MainWindow::playContent()
+{
+ if (!d_creator || !d_creator->getCurrentContent())
+ return;
+
+ if (d_player.state() != QProcess::NotRunning)
+ {
+ QLocalSocket *socket = new QLocalSocket(this);
+ connect(socket, SIGNAL(disconnected()), socket,SLOT(deleteLater()));
+
+ for (int retry = 0; retry < 10; retry++)
+ {
+ socket->connectToServer(d_IPCServerName);
+ if (socket->waitForConnected())
+ {
+ break;
+ }
+ SleeperThread::msleep(500);
+ }
+ QString requestData = "close,";
+ socket->write(requestData.toLocal8Bit());
+ socket->flush();
+ socket->close();
+ }
+
+ if (!d_player.waitForFinished(5000))
+ d_player.kill();
+
+ if (d_player.state() == QProcess::NotRunning)
+ {
+ QStringList args;
+ args << "-creatorControlled";
+ args << d_IPCServerName;
+
+ d_player.start(DENNCO_ENGINE, args);
+ d_player.waitForStarted(5000);
+ }
+
+ if (d_player.state() == QProcess::Running)
+ {
+ QLocalSocket *socket = new QLocalSocket(this);
+ connect(socket, SIGNAL(disconnected()), socket,SLOT(deleteLater()));
+ for (int retry = 0; retry < 10; retry++)
+ {
+ socket->connectToServer(d_IPCServerName);
+ if (socket->waitForConnected())
+ {
+ break;
+ }
+ SleeperThread::msleep(500);
+ }
+ QString requestData = "load,";
+ requestData.append(d_creator->getCurrentContent()->getContentRootPath());
+ socket->write(requestData.toLocal8Bit());
+ socket->flush();
+ socket->close();
+ }
+}