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

Annotation of /infoscreen/mainview.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20