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