OSDN Git Service

34f32437e510c2e35c8d8f7708052437c69eaf66
[dennco/dennco.git] / Source / layer3 / QtDennco / mainwindow.cpp
1 //  Copyright (c) 2012 Dennco Project
2 //
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.
7 //
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.
12 //
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/>.
15
16 //
17 //  Created by tkawata on 2/25/2012.
18 //
19 #include "mainwindow.h"
20 #include "ui_mainwindow.h"
21
22 #include <QFileDialog>
23 #include <QDir>
24 #include <QDebug>
25 #include <QWebFrame>
26
27 #include "TKLog.h"
28 #include "DNGlobal.h"
29 #include "DNAlert.h"
30 #include "DNEngine.h"
31 #include "TKConsole.h"
32 #include "dnwebinterface.h"
33 #include "DNSettings.h"
34 #include "versioninfo.h"
35
36 //static
37 QColor MainWindow::NORMAL_COLOR(0,0,0);
38 //static
39 QColor MainWindow::WARN_COLOR(0xff, 0x95, 0x2b);
40 //static
41 QColor MainWindow::ERROR_COLOR(0xff, 0,0);
42 //static
43 QWidget *MainWindow::instance = NULL;
44
45 MainWindow::MainWindow(QWidget *parent) :
46     QMainWindow(parent),
47     ui(new Ui::MainWindow),
48     mEngine(NULL), mWebInterface(NULL)
49 {
50     instance = this;
51     ui->setupUi(this);
52
53     QString windowTitle = "dennco engine - ";
54     windowTitle.append(ENGINE_VERSION);
55
56     this->setWindowTitle(windowTitle);
57
58     QString defaultPath(QDir::homePath());
59     QString contentPath = QString::fromStdString(DNSettings::getValue(DNSettings::CONTEXT_PATH, defaultPath.toStdString()));
60
61     ui->filePath->setText(contentPath);
62
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);
69
70     std::string savedWindowStateStr = DNSettings::getValue(DNSettings::WINDOWLAYOUT,"");
71     QByteArray savedWindowStateQArray(savedWindowStateStr.c_str());
72     restoreState(QByteArray::fromHex(savedWindowStateQArray));
73
74     std::string savedWindowGeometoryStr = DNSettings::getValue(DNSettings::WINDOWGEOMETORY,"");
75     QByteArray savedWindowGeometoryQArray(savedWindowGeometoryStr.c_str());
76     restoreGeometry(QByteArray::fromHex(savedWindowGeometoryQArray));
77 }
78
79 MainWindow::~MainWindow()
80 {
81     instance = 0;
82     delete ui;
83 }
84
85 void MainWindow::closeEvent(QCloseEvent *event)
86 {
87     QByteArray savingWindowStateQArray = saveState().toHex();
88     DNSettings::setValue(DNSettings::WINDOWLAYOUT, savingWindowStateQArray.constData());
89
90     QByteArray savingWindowGeometoryQArray = saveGeometry().toHex();
91     DNSettings::setValue(DNSettings::WINDOWGEOMETORY, savingWindowGeometoryQArray.constData());
92
93     stopEngine();
94     deleteCurrentEngine();
95     QMainWindow::closeEvent(event);
96 }
97
98
99 void MainWindow::on_chooseDirButton_clicked()
100 {
101     if (QString::compare(ui->startButton->text(), "Stop", Qt::CaseInsensitive) == 0)
102     {
103         DNAlert::show("Please stop engine", "Dennco engine is running.\nPlease stop engine before switching directory.");
104         return;
105     }
106     QString dirPath = QFileDialog::getExistingDirectory(this,"Set dennco setting file",ui->filePath->text());
107     qDebug()<< dirPath;
108     if (dirPath.length() > 0)
109     {
110         ui->filePath->setText(dirPath);
111
112         DNSettings::setValue(DNSettings::CONTEXT_PATH, dirPath.toStdString());
113     }
114 }
115
116
117 void MainWindow::on_startButton_clicked()
118 {
119     qDebug()<< "start button clicked";
120     if (QString::compare(ui->startButton->text(), "Start", Qt::CaseInsensitive) == 0)
121     {
122         deleteCurrentEngine();
123
124         if (startEngine())
125         {
126             ui->startButton->setText("Stop");
127         }
128     }
129     else
130     {
131         stopEngine();
132         ui->startButton->setText("Start");
133     }
134 }
135
136 bool MainWindow::startEngine()
137 {
138     bool initializationFailed = false;
139
140     mEngine = new DNEngine(ui->filePath->text().toLocal8Bit().data());
141     if (!dnGlobal()->isErrorStatusNormal())
142     {
143         DNAlert::show(dnGlobal()->getMessage1(), dnGlobal()->getMessage2());
144         dnGlobal()->resetErrorStatus();
145         initializationFailed = true;
146     }
147     else if (!mEngine->isValid())
148     {
149         DNAlert::show("DNEngine initialize error", "ERROR. DNEngine not configured properly");
150         initializationFailed = true;
151     }
152
153     if (!initializationFailed)
154     {
155         mEngine->startEngine();
156         loadUI();
157     }
158     return !initializationFailed;
159 }
160
161 bool MainWindow::stopEngine()
162 {
163     if (mEngine)
164     {
165         mEngine->stopEngine();
166     }
167     return true;
168 }
169
170 void MainWindow::loadUI()
171 {
172     QString path = "file:///";
173     path.append(ui->filePath->text().append(QString::fromStdString(mEngine->getUIPath())));
174     QUrl pathUrl(path);
175     ui->webView->load(pathUrl);
176     mWebInterface = new DNWebInterface(mEngine);
177     attachWebInterface();
178 }
179
180 void MainWindow::attachWebInterface()
181 {
182     if (!mWebInterface)
183         return;
184
185     QWebPage *page = ui->webView->page();
186     if (page)
187     {
188         QWebFrame *frame = page->mainFrame();
189         if (frame)
190         {
191             frame->addToJavaScriptWindowObject("engine", mWebInterface);
192             connect(frame, SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(attachWebInterface()));
193             connect(frame, SIGNAL(initialLayoutCompleted()), this, SLOT(adjustWindowSize()));
194         }
195     }
196 }
197
198 void MainWindow::adjustWindowSize()
199 {
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);
206 }
207
208 void MainWindow::deleteCurrentEngine()
209 {
210     if (mEngine)
211     {
212         delete mEngine;
213         mEngine = NULL;
214     }
215     if (mWebInterface)
216     {
217         delete mWebInterface;
218         mWebInterface = NULL;
219     }
220 }
221
222 void MainWindow::paintEvent( QPaintEvent * event )
223 {
224     mConsoleLock.lock();
225     while (!mPendingConsoleMessages.isEmpty())
226     {
227         PendingConsoleMessage aMessage = mPendingConsoleMessages.dequeue();
228         switch(aMessage.messageType)
229         {
230         case TKLog::NORMAL :
231             ui->console->setTextColor(NORMAL_COLOR);
232             break;
233         case TKLog::WARNING :
234             ui->console->setTextColor(WARN_COLOR);
235             break;
236         case TKLog::ERROR :
237             ui->console->setTextColor(ERROR_COLOR);
238             break;
239         }
240         ui->console->append(aMessage.text);
241     }
242     mConsoleLock.unlock();
243
244     QMainWindow::paintEvent(event);
245 }
246
247
248 void MainWindow::vprintf(TKLog::MessageType type, const char *fmt, va_list ap)
249 {
250     QString msg = QString().vsprintf(fmt,ap);
251 #ifdef DEBUG
252     qDebug() << msg << endl;
253 #endif
254     mConsoleLock.lock();
255     PendingConsoleMessage newMessage;
256     newMessage.messageType = type;
257     newMessage.text = msg;
258     mPendingConsoleMessages.enqueue(newMessage);
259     mConsoleLock.unlock();
260     emit consoleUpdated();
261 }
262
263 void MainWindow::vDebugPrintf(const char *fmt, va_list ap)
264 {
265 #ifdef DEBUG
266     QString msg = QString().vsprintf(fmt,ap);
267     qDebug() << "DEBUG:" << msg << endl;
268 #endif
269 }
270
271