|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
5 ** |
|
6 ** This file is part of the examples of the Qt Toolkit. |
|
7 ** |
|
8 ** $QT_BEGIN_LICENSE:LGPL$ |
|
9 ** No Commercial Usage |
|
10 ** This file contains pre-release code and may not be distributed. |
|
11 ** You may use this file in accordance with the terms and conditions |
|
12 ** contained in the either Technology Preview License Agreement or the |
|
13 ** Beta Release License Agreement. |
|
14 ** |
|
15 ** GNU Lesser General Public License Usage |
|
16 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
17 ** General Public License version 2.1 as published by the Free Software |
|
18 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
19 ** packaging of this file. Please review the following information to |
|
20 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
22 ** |
|
23 ** In addition, as a special exception, Nokia gives you certain |
|
24 ** additional rights. These rights are described in the Nokia Qt LGPL |
|
25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this |
|
26 ** package. |
|
27 ** |
|
28 ** GNU General Public License Usage |
|
29 ** Alternatively, this file may be used under the terms of the GNU |
|
30 ** General Public License version 3.0 as published by the Free Software |
|
31 ** Foundation and appearing in the file LICENSE.GPL included in the |
|
32 ** packaging of this file. Please review the following information to |
|
33 ** ensure the GNU General Public License version 3.0 requirements will be |
|
34 ** met: http://www.gnu.org/copyleft/gpl.html. |
|
35 ** |
|
36 ** If you are unsure which license is appropriate for your use, please |
|
37 ** contact the sales department at http://qt.nokia.com/contact. |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include <QDebug> |
|
43 #include <QApplication> |
|
44 #include <QGraphicsLinearLayout> |
|
45 #if ENABLE_OPENGL |
|
46 #ifndef QT_NO_OPENGL |
|
47 #include <QGLWidget> |
|
48 #endif |
|
49 #endif |
|
50 #include <QObject> |
|
51 |
|
52 #include "button.h" |
|
53 #include "label.h" |
|
54 #include "menu.h" |
|
55 #include "topbar.h" |
|
56 #include "backgrounditem.h" |
|
57 #include "theme.h" |
|
58 #include "mainview.h" |
|
59 #include "gvbwidget.h" |
|
60 |
|
61 MainView::MainView(const bool enableOpenGL, const bool outputFps, const bool imageRendering, QWidget *parent) |
|
62 : QGraphicsView(parent) |
|
63 , m_scene(0) |
|
64 , m_mainLayout(0) |
|
65 , m_mainWidget(0) |
|
66 , m_testWidget(0) |
|
67 , m_imageBasedRendering(imageRendering) |
|
68 , m_pixmapToRender(0) |
|
69 , m_OutputFps(outputFps) |
|
70 , m_fpsUpdated() |
|
71 , m_Fpss() |
|
72 , m_angle(0) |
|
73 , m_enableOpenGL(enableOpenGL) |
|
74 { |
|
75 construct(); |
|
76 } |
|
77 |
|
78 MainView::~MainView() |
|
79 { |
|
80 if (!m_scene->parent()) |
|
81 delete m_scene; |
|
82 |
|
83 delete m_pixmapToRender; |
|
84 } |
|
85 |
|
86 void MainView::setTestWidget(QGraphicsWidget *testWidget) |
|
87 { |
|
88 if (!testWidget) |
|
89 return; |
|
90 |
|
91 if (m_testWidget) { |
|
92 m_mainLayout->removeItem(m_testWidget); |
|
93 if (!m_testWidget->parent() && !m_testWidget->parentLayoutItem()) |
|
94 delete m_testWidget; |
|
95 } |
|
96 m_testWidget = testWidget; |
|
97 m_mainLayout->addItem(m_testWidget); |
|
98 resizeContent(size()); |
|
99 } |
|
100 |
|
101 QGraphicsWidget *MainView::takeTestWidget() |
|
102 { |
|
103 if (m_testWidget) { |
|
104 m_mainLayout->removeItem(m_testWidget); |
|
105 QGraphicsWidget *tmp = m_testWidget; |
|
106 m_testWidget = 0; |
|
107 return tmp; |
|
108 } |
|
109 return 0; |
|
110 } |
|
111 |
|
112 QGraphicsWidget *MainView::testWidget() |
|
113 { |
|
114 return m_testWidget; |
|
115 } |
|
116 |
|
117 void MainView::setImageBasedRendering(const bool imageBasedRendering) |
|
118 { |
|
119 m_imageBasedRendering = imageBasedRendering; |
|
120 viewport()->update(); |
|
121 } |
|
122 |
|
123 bool MainView::imageBasedRendering() const |
|
124 { |
|
125 return m_imageBasedRendering; |
|
126 } |
|
127 |
|
128 qreal MainView::fps() |
|
129 { |
|
130 if (m_Fpss.count() <= 0) |
|
131 updateFps(); |
|
132 |
|
133 if (m_Fpss.count() <= 0) |
|
134 return 0.0; |
|
135 |
|
136 qreal sum = 0; |
|
137 int count = m_Fpss.count(); |
|
138 for (int i = 0; i<count; ++i) |
|
139 sum += m_Fpss.at(i); |
|
140 m_Fpss.clear(); |
|
141 fpsReset(); |
|
142 return sum/qreal(count); |
|
143 } |
|
144 |
|
145 void MainView::fpsReset() |
|
146 { |
|
147 m_frameCount = 0; |
|
148 m_fpsFirstTs.start(); |
|
149 m_fpsLatestTs = m_fpsFirstTs; |
|
150 m_fpsUpdated.start(); |
|
151 } |
|
152 |
|
153 void MainView::rotateContent(int angle) |
|
154 { |
|
155 bool portrait = ((m_angle+angle)%90 == 0) && ((m_angle+angle)%180 != 0); |
|
156 bool landscape = ((m_angle+angle)%180 == 0); |
|
157 if (!portrait && !landscape) |
|
158 return; |
|
159 |
|
160 m_angle = (m_angle + angle)%360; |
|
161 |
|
162 rotate(angle); |
|
163 |
|
164 resizeContent(size()); |
|
165 } |
|
166 |
|
167 int MainView::rotationAngle() const |
|
168 { |
|
169 return m_angle; |
|
170 } |
|
171 |
|
172 void MainView::resizeContent(const QSize &s) |
|
173 { |
|
174 QSizeF l_size(s); |
|
175 QSizeF p_size(l_size.height(), l_size.width()); |
|
176 bool portrait = (m_angle%90 == 0) && (m_angle%180 != 0); |
|
177 if (portrait) { |
|
178 m_mainWidget->resize(p_size); |
|
179 m_backGround->resize(p_size); |
|
180 } |
|
181 else { |
|
182 m_mainWidget->resize(l_size); |
|
183 m_backGround->resize(l_size); |
|
184 } |
|
185 m_menu->setPos(m_topBar->getStatusBarLocation()); |
|
186 setSceneRect(QRectF(m_mainWidget->pos(), m_mainWidget->size())); |
|
187 } |
|
188 |
|
189 void MainView::resizeEvent(QResizeEvent * event) |
|
190 { |
|
191 QGraphicsView::resizeEvent(event); |
|
192 resizeContent(event->size()); |
|
193 } |
|
194 |
|
195 void MainView::paintEvent (QPaintEvent *event) |
|
196 { |
|
197 if (m_imageBasedRendering) { |
|
198 if (!m_pixmapToRender) |
|
199 m_pixmapToRender = new QPixmap(size()); |
|
200 |
|
201 if (m_pixmapToRender->size() != size()) { |
|
202 delete m_pixmapToRender; |
|
203 m_pixmapToRender = new QPixmap(size()); |
|
204 } |
|
205 QPainter p(m_pixmapToRender); |
|
206 render(&p); |
|
207 p.end(); |
|
208 } |
|
209 else { |
|
210 QGraphicsView::paintEvent(event); |
|
211 } |
|
212 |
|
213 m_frameCount++; |
|
214 m_fpsLatestTs.start(); |
|
215 if(m_fpsUpdated.elapsed() > 2000) { |
|
216 updateFps(); |
|
217 m_fpsUpdated.start(); |
|
218 } |
|
219 } |
|
220 |
|
221 void MainView::keyPressEvent(QKeyEvent *event) |
|
222 { |
|
223 if (event->key() == Qt::Key_F) { |
|
224 if (isFullScreen()) |
|
225 showNormal(); |
|
226 else |
|
227 showFullScreen(); |
|
228 } |
|
229 |
|
230 //S60 3.x specific |
|
231 if(m_menu->menuVisible()) { |
|
232 m_menu->keyPressEvent(event); |
|
233 return; |
|
234 } |
|
235 |
|
236 if(event->key() == 16777235 ) { //Up Arrow |
|
237 GvbWidget* widget = qobject_cast<GvbWidget*>(m_testWidget); |
|
238 if(widget) |
|
239 widget->keyPressEvent(event); |
|
240 } |
|
241 |
|
242 if(event->key() == 16777237 ) { //Down Arrow |
|
243 GvbWidget* widget = qobject_cast<GvbWidget*>(m_testWidget); |
|
244 if(widget) |
|
245 widget->keyPressEvent(event); |
|
246 } |
|
247 |
|
248 if(event->key() == 17825792 ) { //LSK |
|
249 if(!m_menu->menuVisible()) |
|
250 m_menu->menuShowHide(); |
|
251 } |
|
252 |
|
253 if(event->key() == 17825793 ) { //RSK |
|
254 QApplication::quit(); |
|
255 } |
|
256 } |
|
257 |
|
258 void MainView::construct() |
|
259 { |
|
260 m_scene = new QGraphicsScene; |
|
261 |
|
262 #ifdef ENABLE_OPENGL |
|
263 #ifndef QT_NO_OPENGL |
|
264 if (m_enableOpenGL) { |
|
265 qDebug() << "OpenGL enabled"; |
|
266 m_scene->setSortCacheEnabled(false); |
|
267 setViewport(new QGLWidget); |
|
268 |
|
269 // Qt doc says: This is the preferred update mode for |
|
270 // viewports that do not support partial updates, such as QGLWidget... |
|
271 setViewportUpdateMode(QGraphicsView::FullViewportUpdate); |
|
272 } |
|
273 #endif |
|
274 #endif |
|
275 |
|
276 setScene(m_scene); |
|
277 |
|
278 if (!m_enableOpenGL) |
|
279 setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate); |
|
280 m_scene->setItemIndexMethod(QGraphicsScene::NoIndex); |
|
281 |
|
282 //setCacheMode(QGraphicsView::CacheBackground); |
|
283 setAlignment(Qt::AlignLeft | Qt::AlignTop); |
|
284 |
|
285 // Turn off automatic background |
|
286 setAttribute(Qt::WA_OpaquePaintEvent); |
|
287 setAttribute(Qt::WA_NoBackground); |
|
288 setAttribute(Qt::WA_NoSystemBackground); |
|
289 setAutoFillBackground(false); |
|
290 |
|
291 //Background |
|
292 m_backGround = new BackgroundItem("background.svg"); |
|
293 m_scene->addItem(m_backGround); |
|
294 m_backGround->setZValue(0); |
|
295 |
|
296 //Menu |
|
297 m_menu = new Menu(this); |
|
298 m_scene->addItem(m_menu); //Add menu to the scene directly |
|
299 m_menu->setZValue(10); //Bring to front |
|
300 |
|
301 m_mainLayout = new QGraphicsLinearLayout(Qt::Vertical); |
|
302 m_mainLayout->setContentsMargins(0,0,0,0); |
|
303 m_mainLayout->setSpacing(0); |
|
304 |
|
305 m_mainWidget = new QGraphicsWidget; |
|
306 m_mainWidget->setLayout(m_mainLayout); |
|
307 m_mainWidget->setZValue(1); |
|
308 m_scene->addItem(m_mainWidget); |
|
309 |
|
310 //Topbar |
|
311 m_topBar = new TopBar(this, 0); |
|
312 m_mainLayout->addItem(m_topBar); |
|
313 m_topBar->setZValue(1); |
|
314 connect(m_topBar, SIGNAL(clicked(bool)), m_menu, SLOT(menuShowHide())); |
|
315 |
|
316 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
|
317 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
|
318 setContentsMargins(0,0,0,0); |
|
319 setViewportMargins(0,0,0,0); |
|
320 setFrameShape(QFrame::NoFrame); |
|
321 |
|
322 fpsReset(); |
|
323 m_fpsUpdated.start(); |
|
324 } |
|
325 |
|
326 void MainView::updateFps() |
|
327 { |
|
328 int msecs = m_fpsFirstTs.msecsTo(m_fpsLatestTs); |
|
329 qreal fps = 0; |
|
330 if (msecs > 0) { |
|
331 fps = m_frameCount * 1000.0 / msecs; |
|
332 |
|
333 if (m_OutputFps) |
|
334 qDebug() << "FPS: " << fps; |
|
335 |
|
336 m_Fpss.append(fps); |
|
337 } |
|
338 m_fpsFirstTs = m_fpsLatestTs; |
|
339 m_frameCount = 0; |
|
340 } |
|
341 |
|
342 Menu *MainView::menu() |
|
343 { |
|
344 return m_menu; |
|
345 } |