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 2/25/2012.
19 #include "mainwindow.h"
20 #include "ui_mainwindow.h"
22 #include <QFileDialog>
31 #include "TKConsole.h"
32 #include "dnwebinterface.h"
33 #include "DNSettings.h"
34 #include "versioninfo.h"
37 QColor MainWindow::NORMAL_COLOR(0,0,0);
39 QColor MainWindow::WARN_COLOR(0xff, 0x95, 0x2b);
41 QColor MainWindow::ERROR_COLOR(0xff, 0,0);
43 QWidget *MainWindow::instance = NULL;
45 MainWindow::MainWindow(QWidget *parent) :
47 ui(new Ui::MainWindow),
48 mEngine(NULL), mWebInterface(NULL)
53 QString windowTitle = "dennco engine - ";
54 windowTitle.append(ENGINE_VERSION);
56 this->setWindowTitle(windowTitle);
58 QString defaultPath(QDir::homePath());
59 QString contentPath = QString::fromStdString(DNSettings::getValue(DNSettings::CONTEXT_PATH, defaultPath.toStdString()));
61 ui->filePath->setText(contentPath);
63 ui->console->document()->setMaximumBlockCount(500);
64 QFont monofont("Courier");
65 monofont.setStyleHint(QFont::Monospace);
66 ui->console->setFont(monofont);
67 TKLog::setDestination(this);
68 connect(this, SIGNAL(consoleUpdated()), this, SLOT(update()), Qt::AutoConnection);
70 std::string savedWindowStateStr = DNSettings::getValue(DNSettings::WINDOWLAYOUT,"");
71 QByteArray savedWindowStateQArray(savedWindowStateStr.c_str());
72 restoreState(QByteArray::fromHex(savedWindowStateQArray));
74 std::string savedWindowGeometoryStr = DNSettings::getValue(DNSettings::WINDOWGEOMETORY,"");
75 QByteArray savedWindowGeometoryQArray(savedWindowGeometoryStr.c_str());
76 restoreGeometry(QByteArray::fromHex(savedWindowGeometoryQArray));
79 MainWindow::~MainWindow()
85 void MainWindow::closeEvent(QCloseEvent *event)
87 QByteArray savingWindowStateQArray = saveState().toHex();
88 DNSettings::setValue(DNSettings::WINDOWLAYOUT, savingWindowStateQArray.constData());
90 QByteArray savingWindowGeometoryQArray = saveGeometry().toHex();
91 DNSettings::setValue(DNSettings::WINDOWGEOMETORY, savingWindowGeometoryQArray.constData());
94 deleteCurrentEngine();
95 QMainWindow::closeEvent(event);
99 void MainWindow::on_chooseDirButton_clicked()
101 if (QString::compare(ui->startButton->text(), "Stop", Qt::CaseInsensitive) == 0)
103 DNAlert::show("Please stop engine", "Dennco engine is running.\nPlease stop engine before switching directory.");
106 QString dirPath = QFileDialog::getExistingDirectory(this,"Set dennco setting file",ui->filePath->text());
108 if (dirPath.length() > 0)
110 ui->filePath->setText(dirPath);
112 DNSettings::setValue(DNSettings::CONTEXT_PATH, dirPath.toStdString());
117 void MainWindow::on_startButton_clicked()
119 qDebug()<< "start button clicked";
120 if (QString::compare(ui->startButton->text(), "Start", Qt::CaseInsensitive) == 0)
122 deleteCurrentEngine();
126 ui->startButton->setText("Stop");
132 ui->startButton->setText("Start");
136 bool MainWindow::startEngine()
138 bool initializationFailed = false;
140 mEngine = new DNEngine(ui->filePath->text().toLocal8Bit().data());
141 if (!dnGlobal()->isErrorStatusNormal())
143 DNAlert::show(dnGlobal()->getMessage1(), dnGlobal()->getMessage2());
144 dnGlobal()->resetErrorStatus();
145 initializationFailed = true;
147 else if (!mEngine->isValid())
149 DNAlert::show("DNEngine initialize error", "ERROR. DNEngine not configured properly");
150 initializationFailed = true;
153 if (!initializationFailed)
155 mEngine->startEngine();
158 return !initializationFailed;
161 bool MainWindow::stopEngine()
165 mEngine->stopEngine();
170 void MainWindow::loadUI()
172 QString path = "file:///";
173 path.append(ui->filePath->text().append(QString::fromStdString(mEngine->getUIPath())));
175 ui->webView->load(pathUrl);
176 mWebInterface = new DNWebInterface(mEngine);
177 attachWebInterface();
180 void MainWindow::attachWebInterface()
185 QWebPage *page = ui->webView->page();
188 QWebFrame *frame = page->mainFrame();
191 frame->addToJavaScriptWindowObject("engine", mWebInterface);
192 connect(frame, SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(attachWebInterface()));
193 connect(frame, SIGNAL(initialLayoutCompleted()), this, SLOT(adjustWindowSize()));
198 void MainWindow::adjustWindowSize()
200 int hadjust = ui->webView->page()->mainFrame()->scrollBarMaximum(Qt::Horizontal);
201 int vadjust = ui->webView->page()->mainFrame()->scrollBarMaximum(Qt::Vertical);
202 hadjust = hadjust > 0 ? hadjust : 0;
203 vadjust = vadjust > 0 ? vadjust : 0;
204 QSize currentSize = size();
205 resize(currentSize.width() + hadjust, currentSize.height() + vadjust);
208 void MainWindow::deleteCurrentEngine()
217 delete mWebInterface;
218 mWebInterface = NULL;
222 void MainWindow::paintEvent( QPaintEvent * event )
225 while (!mPendingConsoleMessages.isEmpty())
227 PendingConsoleMessage aMessage = mPendingConsoleMessages.dequeue();
228 switch(aMessage.messageType)
231 ui->console->setTextColor(NORMAL_COLOR);
233 case TKLog::WARNING :
234 ui->console->setTextColor(WARN_COLOR);
237 ui->console->setTextColor(ERROR_COLOR);
240 ui->console->append(aMessage.text);
242 mConsoleLock.unlock();
244 QMainWindow::paintEvent(event);
248 void MainWindow::vprintf(TKLog::MessageType type, const char *fmt, va_list ap)
250 QString msg = QString().vsprintf(fmt,ap);
252 qDebug() << msg << endl;
255 PendingConsoleMessage newMessage;
256 newMessage.messageType = type;
257 newMessage.text = msg;
258 mPendingConsoleMessages.enqueue(newMessage);
259 mConsoleLock.unlock();
260 emit consoleUpdated();
263 void MainWindow::vDebugPrintf(const char *fmt, va_list ap)
266 QString msg = QString().vsprintf(fmt,ap);
267 qDebug() << "DEBUG:" << msg << endl;