/[projects]/infoscreen/MainView.cpp
ViewVC logotype

Contents of /infoscreen/MainView.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20