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

Annotation of /infoscreen/mainview.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20