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

Annotation of /infoscreen/mainview.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20