1 |
#include "MainView.h" |
#include "mainview.h" |
2 |
|
#include <QApplication> |
3 |
#include <QPushButton> |
#include <QPushButton> |
4 |
#include <QVBoxLayout> |
#include <QLabel> |
5 |
|
#include <QStackedLayout> |
6 |
|
#include <QSvgWidget> |
7 |
|
|
8 |
|
#include <QDir> |
9 |
#include <QTimer> |
#include <QTimer> |
10 |
|
#include <QKeyEvent> |
11 |
|
#include <QSettings> |
12 |
|
#include <QMessageBox> |
13 |
|
|
14 |
|
#include "webview.h" |
15 |
|
#include "clientsiderender.h" |
16 |
|
#include "pictureview.h" |
17 |
|
#include "videoview.h" |
18 |
|
#include "clockoverlay.h" |
19 |
|
#include "httpwrapper.h" |
20 |
|
#include "screenmanager.h" |
21 |
|
|
22 |
|
|
23 |
|
|
24 |
MainView::MainView(QWidget* parent) |
MainView::MainView(QWidget* parent) |
25 |
: QWidget(parent) |
: QWidget(parent), timer(0) |
26 |
{ |
{ |
27 |
this->resize(400,400); |
loadSettings(); |
28 |
this->setPalette( QPalette( Qt::blue ) ); |
|
29 |
|
if ( currentMode == ModeXml) { |
30 |
|
xmlUrl = url + "?screen_id=" + screenid; |
31 |
|
qDebug() << "Starting XML mode"; |
32 |
|
qDebug() << "xmlUrl" << xmlUrl; |
33 |
|
qDebug() << "xmlInterval" << xmlInterval/(60*1000); |
34 |
|
} |
35 |
|
|
36 |
|
if (currentMode == ModeSimpleWeb){ |
37 |
|
qDebug() << "Starting plain browser mode"; |
38 |
|
} |
39 |
|
|
40 |
|
if (currentMode == ModeLocal ) { |
41 |
|
qDebug() << "Starting local mode"; |
42 |
|
qDebug() << "path" << path; |
43 |
|
|
44 |
|
readLocalFiles(); |
45 |
|
} |
46 |
|
|
47 |
|
this->resize(640,480); |
48 |
|
this->setWindowState( Qt::WindowFullScreen ); |
49 |
|
this->grabKeyboard(); |
50 |
|
|
51 |
|
qApp->setOverrideCursor( QCursor( Qt::BlankCursor) ); |
52 |
|
|
53 |
red = new QWidget(this); |
render = new ClientSideRender(this); |
|
red->setStyleSheet("QWidget { background-color: red; }"); |
|
54 |
|
|
55 |
green = new QWidget(this); |
web = new WebView(this); |
|
green->setStyleSheet("QWidget { background-color: green; }"); |
|
|
green->setVisible(false); |
|
56 |
|
|
57 |
current = red; |
picture = new PictureView(this); |
58 |
|
|
59 |
QVBoxLayout* layout = new QVBoxLayout(); |
video = new VideoView(this); |
|
layout->addWidget(red,1 ); |
|
|
layout->addWidget(green,1); |
|
|
layout->addStretch(); |
|
|
layout->setContentsMargins(0,0,0,0); |
|
|
setLayout(layout); |
|
60 |
|
|
61 |
|
svg = new QSvgWidget(this); |
62 |
|
|
63 |
|
layout = new QStackedLayout(); |
64 |
|
layout->addWidget(web); |
65 |
|
layout->addWidget(render); |
66 |
|
layout->addWidget(picture); |
67 |
|
layout->addWidget(video); |
68 |
|
layout->addWidget(svg); |
69 |
|
layout->setContentsMargins(0,0,0,0); |
70 |
|
setLayout(layout); |
71 |
|
|
72 |
QTimer* timer = new QTimer(this); |
if (showClock == true) { |
73 |
connect(timer, SIGNAL(timeout() ), this, SLOT(onTimer() )); |
clockOverlay = new ClockOverlay(this); |
74 |
timer->start(5000); |
clockOverlay->move(10,10); |
75 |
|
clockOverlay->resize(150,75); |
76 |
|
} |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
if (currentMode == ModeSimpleWeb) { |
81 |
|
web->setVisible(true); |
82 |
|
web->start(url,screenid); |
83 |
|
} |
84 |
|
|
85 |
|
if (enableScreenManager) { |
86 |
|
screenManager = new ScreenManager(screenManagerOn, screenManagerOff); |
87 |
|
} else { |
88 |
|
qDebug() << "ScreenManager is disabled"; |
89 |
|
} |
90 |
|
|
91 |
|
|
92 |
|
qDebug() << "Starting timer..."; |
93 |
|
timer = new QTimer(this); |
94 |
|
connect(timer, SIGNAL(timeout() ), this, SLOT(onTimer() )); |
95 |
|
timer->start(100); |
96 |
} |
} |
97 |
|
|
98 |
|
void MainView::loadSettings() |
99 |
|
{ |
100 |
|
settings = new QSettings("Caddi", "infoscreen"); |
101 |
|
|
102 |
|
qDebug() << "Loading settings" << settings->fileName(); |
103 |
|
|
104 |
|
QString mode = settings->value("mode").toString().toLower(); |
105 |
|
if (mode == "simpleweb") { |
106 |
|
currentMode = ModeSimpleWeb; |
107 |
|
} else if (mode == "xml") { |
108 |
|
currentMode = ModeXml; |
109 |
|
} else if (mode == "local") { |
110 |
|
currentMode = ModeLocal; |
111 |
|
} else { |
112 |
|
currentMode = ModeNone; |
113 |
|
QMessageBox::warning(this, "infoscreen", "no operation mode set or mode given an invalid value"); |
114 |
|
exit(1); |
115 |
|
} |
116 |
|
|
117 |
|
enableScreenManager = settings->value("enablescreenmanager").toBool(); |
118 |
|
if (enableScreenManager) { |
119 |
|
screenManagerOn = settings->value("screenmanager_on").toTime(); |
120 |
|
screenManagerOff = settings->value("screenmanager_off").toTime(); |
121 |
|
} |
122 |
|
|
123 |
|
showClock = settings->value("showclock").toBool(); |
124 |
|
|
125 |
|
xmlInterval = settings->value("xmlinterval", 30).toInt(); |
126 |
|
xmlInterval = (xmlInterval * 60 * 1000); //convert to milliseconds |
127 |
|
|
128 |
|
|
129 |
|
if (currentMode == ModeSimpleWeb || currentMode == ModeXml) { |
130 |
|
url = settings->value("url").toString(); |
131 |
|
screenid = settings->value("screenid").toString(); |
132 |
|
if (url == "" || screenid == "") { |
133 |
|
QMessageBox::warning(this,"infoscreen","Could not find url or screenid in config file " + settings->fileName()); |
134 |
|
|
135 |
|
exit(1); //Normal qApp->exit() doesn't terminate the init sequence so use std C exit function |
136 |
|
} |
137 |
|
} |
138 |
|
|
139 |
|
if (currentMode == ModeLocal) { |
140 |
|
path = settings->value("path").toString(); |
141 |
|
|
142 |
|
if (path == "") { |
143 |
|
QMessageBox::warning(this,"infoscreen","Could not find path in config file " + settings->fileName()); |
144 |
|
exit(1); |
145 |
|
} |
146 |
|
} |
147 |
|
} |
148 |
|
|
149 |
|
void MainView::closeEvent ( QCloseEvent * event ) |
150 |
|
{ |
151 |
|
Q_UNUSED(event); |
152 |
|
exit(0); //force application shutdown |
153 |
|
} |
154 |
|
|
155 |
|
void MainView::keyPressEvent ( QKeyEvent* event ) |
156 |
|
{ |
157 |
|
int key = event->key(); |
158 |
|
if (key == ' ' || key == Qt::Key_Return || key == Qt::Key_Enter) { |
159 |
|
close(); |
160 |
|
} |
161 |
|
|
162 |
|
if (key == 'f' || key == 'F') { |
163 |
|
Qt::WindowStates current = this->windowState(); |
164 |
|
if (current == Qt::WindowFullScreen) { |
165 |
|
this->setWindowState(Qt::WindowActive); |
166 |
|
} else { |
167 |
|
this->setWindowState(Qt::WindowFullScreen); |
168 |
|
} |
169 |
|
} |
170 |
|
|
171 |
|
} |
172 |
|
|
173 |
void MainView::onTimer() |
void MainView::onTimer() |
174 |
{ |
{ |
175 |
if( current == red) { |
if (enableScreenManager == true) { |
176 |
red->setVisible(false); |
screenManager->timerTick(); |
177 |
green->setVisible(true); |
} |
178 |
current = green; |
|
179 |
} else { |
if (currentMode == ModeXml) { |
180 |
red->setVisible(true); |
readXml(); |
181 |
green->setVisible(false); |
} |
182 |
current = red; |
|
183 |
} |
if (currentMode == ModeXml || currentMode == ModeLocal) { |
184 |
|
switchScreens(); |
185 |
|
} |
186 |
|
if (showClock == true) { |
187 |
|
clockOverlay->timerTick(); |
188 |
|
} |
189 |
|
} |
190 |
|
|
191 |
|
|
192 |
|
void MainView::readLocalFiles() |
193 |
|
{ |
194 |
|
QDir dir(path); |
195 |
|
if (! dir.exists()) { |
196 |
|
QMessageBox::warning(this,"infoscreen","Local Source directory not found: " + path); |
197 |
|
exit(1); |
198 |
|
} |
199 |
|
QFileInfoList files = dir.entryInfoList(QDir::Files, QDir::Name); //only files, sort by name |
200 |
|
|
201 |
|
for (int i=0; i<files.size(); ++i) { |
202 |
|
QFileInfo file = files[i]; |
203 |
|
qDebug() << "Found" << file.fileName(); |
204 |
|
|
205 |
|
ScreenItem item; |
206 |
|
item.url = file.filePath(); |
207 |
|
item.module = ModuleUnknown; |
208 |
|
|
209 |
|
QString ext = file.suffix().toLower(); |
210 |
|
if (ext == "avi" || ext == "mpg" || ext == "mpeg") { |
211 |
|
item.module = ModuleVideo; |
212 |
|
item.runtime = 1; |
213 |
|
} |
214 |
|
|
215 |
|
if (ext == "jpg" || ext == "jpeg" || ext == "png" ) { |
216 |
|
item.module = ModuleImage; |
217 |
|
item.runtime = 10; |
218 |
|
} |
219 |
|
|
220 |
|
if (ext == "svg") { |
221 |
|
item.module = ModuleSvg; |
222 |
|
item.runtime = 10; |
223 |
|
} |
224 |
|
|
225 |
|
if (ext == "htm" || ext == "html") { |
226 |
|
item.module = ModuleWeb; |
227 |
|
item.runtime = 10; |
228 |
|
} |
229 |
|
|
230 |
|
if (item.module != ModuleUnknown) { //no need to enqueue unknown modules |
231 |
|
screenItems.push_back( item ); |
232 |
|
} |
233 |
|
|
234 |
|
} |
235 |
|
|
236 |
|
qDebug() << "Found " << screenItems.size() << " playable items"; |
237 |
} |
} |
238 |
|
|
239 |
|
|
240 |
|
bool MainView::readXml() |
241 |
|
{ |
242 |
|
if ( lastXml.isNull() || lastXml.elapsed() > xmlInterval) { |
243 |
|
qDebug() << "Reading XML"; |
244 |
|
|
245 |
|
|
246 |
|
bool res = xmlHandler.readXml( xmlUrl ); |
247 |
|
|
248 |
|
lastXml = QTime::currentTime(); |
249 |
|
|
250 |
|
screenItems = xmlHandler.getScreenSet(); |
251 |
|
|
252 |
|
if ( currentItemIdx >= screenItems.size() ) |
253 |
|
currentItemIdx = screenItems.size()-1; //avoid overflow |
254 |
|
|
255 |
|
|
256 |
|
return true; |
257 |
|
} else { |
258 |
|
return false; |
259 |
|
} |
260 |
|
} |
261 |
|
|
262 |
|
void MainView::switchScreens() |
263 |
|
{ |
264 |
|
if (video->isVisible() && video->isPlaying() ) { |
265 |
|
return; //wait until current clip has finished |
266 |
|
} |
267 |
|
|
268 |
|
if (lastScreenSwitch.isNull() || lastScreenSwitch.elapsed() > (currentItem.runtime*1000)) { |
269 |
|
QTime now = QTime::currentTime(); |
270 |
|
if (lastScreenSwitch.isNull()) |
271 |
|
currentItemIdx = -1; |
272 |
|
|
273 |
|
bool found = false; |
274 |
|
int tries = 0; |
275 |
|
|
276 |
|
if (screenItems.size() > 0) { //only try if we have a any screens |
277 |
|
while (found == false && tries <= screenItems.size()) { //find next with valid display time |
278 |
|
tries++; |
279 |
|
currentItemIdx = (currentItemIdx+1) % screenItems.size(); |
280 |
|
currentItem = screenItems.at(currentItemIdx); |
281 |
|
|
282 |
|
if (currentItem.start.isValid() && currentItem.stop.isValid()) { |
283 |
|
if (currentItem.start <= now && now <= currentItem.stop ) |
284 |
|
found = true; |
285 |
|
} else { // if start or stop time was invalid - show them always |
286 |
|
found = true; |
287 |
|
} |
288 |
|
} |
289 |
|
} |
290 |
|
|
291 |
|
if (found) { |
292 |
|
switch(currentItem.module) { |
293 |
|
case ModuleImage: |
294 |
|
ensureVisible(picture); |
295 |
|
picture->loadFromUrl( currentItem.url ); |
296 |
|
break; |
297 |
|
case ModuleWeb: |
298 |
|
ensureVisible(web); |
299 |
|
web->load(currentItem.url); |
300 |
|
break; |
301 |
|
case ModuleVideo: |
302 |
|
ensureVisible(video); |
303 |
|
video->loadUrl(currentItem.url); |
304 |
|
break; |
305 |
|
case ModuleSvg: |
306 |
|
ensureVisible(svg); |
307 |
|
svg->load(currentItem.url); |
308 |
|
break; |
309 |
|
default: |
310 |
|
// ModuleUnknown - what should we do?? |
311 |
|
break; |
312 |
|
} |
313 |
|
|
314 |
|
} else { |
315 |
|
errorInfoScreen("Der er ingen information at vise"); |
316 |
|
currentItem = ScreenItem(); |
317 |
|
currentItem.runtime = 60; //switch screens again in 1 minute |
318 |
|
} |
319 |
|
lastScreenSwitch = QTime::currentTime(); |
320 |
|
|
321 |
|
} |
322 |
|
} |
323 |
|
|
324 |
|
void MainView::errorInfoScreen(QString msg) |
325 |
|
{ |
326 |
|
ensureVisible(web); |
327 |
|
QString html("<html><body text='#505050' bgcolor='#000000'>"); |
328 |
|
html.append("<table width='100%' height='100%'><tr><td align='center' valign='middel'><h1>") |
329 |
|
.append(msg) |
330 |
|
.append("</h1></td></tr></table></body></html>"); |
331 |
|
web->setHtml(html); |
332 |
|
} |
333 |
|
|
334 |
|
void MainView::ensureVisible(QWidget* widget) |
335 |
|
{ |
336 |
|
layout->setCurrentWidget(widget); |
337 |
|
} |
338 |
|
|