4 #include "accelerometercontroller.h" |
4 #include "accelerometercontroller.h" |
5 #include "magnetometercontroller.h" |
5 #include "magnetometercontroller.h" |
6 #include "rotationcontroller.h" |
6 #include "rotationcontroller.h" |
7 #include "orientationcontroller.h" |
7 #include "orientationcontroller.h" |
8 #include "compasscontroller.h" |
8 #include "compasscontroller.h" |
|
9 #include "tapcontroller.h" |
9 |
10 |
10 int View::m_imageWidth; |
11 int View::m_imageWidth; |
11 int View::m_imageHeight; |
12 int View::m_imageHeight; |
12 QString View::m_currentSensor; |
13 QString View::m_currentSensor; |
|
14 bool View::m_isToBeZoomed; |
|
15 int View::m_lightLevel = -1; |
|
16 int View::m_scaledHeight[4]; |
|
17 int View::m_index = 3; |
13 |
18 |
14 View::View(QGraphicsScene *scene) : |
19 View::View(QGraphicsScene *scene) : |
15 QGraphicsView(scene), m_timer(this) ,m_delay(30){ |
20 QGraphicsView(scene), m_timer(this) ,m_delay(30){ |
16 |
21 |
17 m_controller = 0; |
|
18 |
|
19 QPixmap bgPix(":/images/koskipuisto_pieni.jpg"); |
22 QPixmap bgPix(":/images/koskipuisto_pieni.jpg"); |
20 m_pix = bgPix; |
23 m_pix = bgPix; |
21 m_imageWidth = bgPix.width(); |
24 setupWindow(); |
22 m_imageHeight = bgPix.height(); |
25 m_scaledHeight[0] = m_imageHeight*1.5; |
23 int h= height(); |
26 m_scaledHeight[1] = m_imageHeight*2; |
|
27 m_scaledHeight[2] = (int)(m_imageHeight*0.5); |
|
28 m_scaledHeight[3] = m_imageHeight; |
|
29 |
|
30 int h = height(); |
24 int y = qAbs(m_imageHeight-h)/2; |
31 int y = qAbs(m_imageHeight-h)/2; |
25 setSceneRect(0, y, width(), h); |
32 setSceneRect(0, y, width(), h); |
26 |
33 |
27 setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate); |
|
28 setBackgroundBrush(bgPix); |
|
29 setCacheMode(QGraphicsView::CacheBackground); |
|
30 setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); |
|
31 setWindowTitle("Panorama"); |
|
32 |
34 |
33 m_sensors.append(InputController::QACCELEROMETER); |
35 m_sensors.append(InputController::QACCELEROMETER); |
34 m_sensors.append(InputController::QORIENTATIONSENSOR); |
36 m_sensors.append(InputController::QORIENTATIONSENSOR); |
35 m_sensors.append(InputController::QMAGNETOMETER); |
37 m_sensors.append(InputController::QMAGNETOMETER); |
36 m_sensors.append(InputController::QROTATIONSENSOR); |
38 m_sensors.append(InputController::QROTATIONSENSOR); |
38 m_sensors.append(InputController::QCOMPASS); |
40 m_sensors.append(InputController::QCOMPASS); |
39 m_sensors.append(InputController::QKEYS); |
41 m_sensors.append(InputController::QKEYS); |
40 |
42 |
41 |
43 |
42 m_menu = new QMenu(this); |
44 m_menu = new QMenu(this); |
43 |
|
44 createActions(); |
45 createActions(); |
45 handleAction(NULL,InputController::QACCELEROMETER); |
46 handleAction(NULL,InputController::QACCELEROMETER); |
46 |
47 |
47 m_timer.setSingleShot(false); |
48 m_timer.setSingleShot(false); |
48 m_timer.start(m_delay); |
49 m_timer.start(m_delay); |
49 connect(&m_timer, SIGNAL(timeout()), this, SLOT(update())); |
50 connect(&m_timer, SIGNAL(timeout()), this, SLOT(update())); |
50 } |
51 |
51 |
52 |
52 View::~View(){ if (m_controller) delete m_controller;} |
53 m_proximitySensor.connectToBackend(); |
|
54 m_proximitySensor.start(); |
|
55 connect(&m_proximitySensor, SIGNAL(readingChanged()), this, SLOT(handleProximity())); |
|
56 |
|
57 m_ambientLightSensor.connectToBackend(); |
|
58 m_ambientLightSensor.start(); |
|
59 connect(&m_ambientLightSensor, SIGNAL(readingChanged()), this, SLOT(handleALS())); |
|
60 |
|
61 } |
|
62 |
|
63 |
|
64 View::~View(){ |
|
65 m_timer.stop(); |
|
66 if (m_controller) delete m_controller; |
|
67 if (m_exController) delete m_exController; |
|
68 } |
53 |
69 |
54 |
70 |
55 void View::resizeEvent(QResizeEvent *event) |
71 void View::resizeEvent(QResizeEvent *event) |
56 { |
72 { |
57 QGraphicsView::resizeEvent(event); |
73 QGraphicsView::resizeEvent(event); |
183 if (m_controller){ |
186 if (m_controller){ |
184 delete m_controller; |
187 delete m_controller; |
185 m_controller =0; |
188 m_controller =0; |
186 } |
189 } |
187 |
190 |
188 do{ |
191 if (sensor==InputController::QACCELEROMETER){ |
189 if (sensor==InputController::QACCELEROMETER){ |
192 m_controller = new AccelerometerController(); |
190 m_controller = new AccelerometerController(); |
193 return; |
191 break; |
194 } |
192 } |
195 if (sensor==InputController::QROTATIONSENSOR){ |
193 if (sensor==InputController::QROTATIONSENSOR){ |
196 m_controller = new RotationController(); |
194 m_controller = new RotationController(); |
197 return; |
195 break; |
198 } |
196 } |
199 if (sensor==InputController::QMAGNETOMETER){ |
197 if (sensor==InputController::QMAGNETOMETER){ |
200 m_controller = new MagnetometerController(); |
198 m_controller = new MagnetometerController(); |
201 return; |
199 break; |
202 } |
200 } |
203 if (sensor==InputController::QORIENTATIONSENSOR){ |
201 if (sensor==InputController::QORIENTATIONSENSOR){ |
204 m_controller = new OrientationController(); |
202 m_controller = new OrientationController(); |
205 return; |
203 break; |
206 } |
204 } |
207 if (sensor==InputController::QTAPSENSOR){ |
205 |
208 m_controller = new TapController(); |
206 if (sensor==InputController::QTAPSENSOR){ |
209 return; |
207 break; |
210 } |
208 } |
211 if (sensor==InputController::QCOMPASS){ |
209 if (sensor==InputController::QCOMPASS){ |
212 m_controller = new CompassController(); |
210 m_controller = new CompassController(); |
213 return; |
211 break; |
214 } |
212 } |
215 if (sensor==InputController::QKEYS){ |
213 if (sensor==InputController::QKEYS){ |
216 m_controller = new KeyController(); |
214 m_controller = new KeyController(); |
217 return; |
215 break; |
218 } |
216 } |
219 |
217 |
220 |
218 }while (true); |
|
219 |
|
220 if (m_controller) m_controller->start(); |
|
221 |
221 |
222 } |
222 } |
223 |
223 |
224 |
224 |
225 int View::checkX(int x){ |
225 int View::checkX(int x){ |
226 return qAbs(x) < m_imageWidth ? x : x % m_imageWidth; |
226 int tmpX = qAbs(x) < m_imageWidth ? x : x % m_imageWidth; |
|
227 m_mouseMode?m_exController->setX(tmpX):m_controller->setX(tmpX); |
|
228 return tmpX; |
227 |
229 |
228 } |
230 } |
229 |
231 |
230 int View::checkY(int y){ |
232 int View::checkY(int y){ |
231 int limit = m_imageHeight-height(); |
233 int limit = m_imageHeight-height(); |
232 if (y<0){ |
234 if (y<0){ |
233 y = limit<0?-limit/2:0; |
235 y = limit<0?-limit/2:0; |
234 if (m_controller) m_controller->setY(y); |
236 } |
235 return y; |
237 else if (limit<0){ |
236 } |
|
237 |
|
238 if (limit<0){ |
|
239 y=-limit/2; |
238 y=-limit/2; |
240 if (m_controller) m_controller->setY(y); |
239 } |
241 return y; |
240 else if (y > limit){y=limit;} |
242 } |
241 |
243 |
242 m_mouseMode?m_exController->setY(y):m_controller->setY(y); |
244 |
|
245 if (y< limit){ return y;} |
|
246 |
|
247 y = limit; |
|
248 if (m_controller) m_controller->setY(y); |
|
249 return y; |
243 return y; |
250 } |
244 } |
251 |
245 |
252 |
246 |
253 |
247 |
256 m_exController = m_controller; |
250 m_exController = m_controller; |
257 m_mouseMode = false; |
251 m_mouseMode = false; |
258 } |
252 } |
259 |
253 |
260 |
254 |
261 |
|
262 void View::mouseMoveEvent(QMouseEvent* event){ |
255 void View::mouseMoveEvent(QMouseEvent* event){ |
263 |
256 |
264 if (m_mouseMode){ |
257 // first time |
265 |
258 if (!m_mouseMode){ |
266 m_dx = m_eventX - event->x(); |
|
267 m_dy = m_eventY - event->y(); |
|
268 |
|
269 update(); |
|
270 |
|
271 m_eventX = event->x(); |
|
272 m_eventY = event->y(); |
|
273 return; |
|
274 } |
|
275 |
|
276 if (m_mousePressTime.msecsTo(QTime::currentTime())>200){ |
|
277 m_menu->setVisible(false); |
|
278 m_mouseMode = true; |
259 m_mouseMode = true; |
279 m_eventX = event->x(); |
260 m_eventX = event->x(); |
280 m_eventY = event->y(); |
261 m_eventY = event->y(); |
281 m_controller = 0; |
262 return; |
282 } |
263 } |
|
264 |
|
265 if (m_menu->isVisible()){ |
|
266 if (m_mousePressTime.msecsTo(QTime::currentTime())>300) |
|
267 m_menu->setVisible(false); |
|
268 } |
|
269 |
|
270 int cur_x = event->x(); |
|
271 int cur_y = event->y(); |
|
272 |
|
273 m_dx = m_eventX - cur_x; |
|
274 m_dy = m_eventY - cur_y; |
|
275 |
|
276 m_eventX = cur_x; |
|
277 m_eventY = cur_y; |
|
278 |
|
279 update(); |
283 |
280 |
284 } |
281 } |
285 |
282 |
286 |
283 |
287 |
284 |
288 void View::mouseReleaseEvent(QMouseEvent* event){ |
285 void View::mouseReleaseEvent(QMouseEvent* event){ |
289 m_controller = m_exController; |
286 m_controller = m_exController; |
290 |
287 m_exController = 0; |
291 if (m_mouseMode){ |
|
292 if (m_controller){ |
|
293 m_controller->setX(m_x); |
|
294 m_controller->setY(m_y); |
|
295 } |
|
296 } |
|
297 m_mouseMode = false; |
288 m_mouseMode = false; |
298 |
289 |
299 if (m_mousePressTime.msecsTo(QTime::currentTime())>200) return; |
290 |
300 |
291 if (m_menu->isVisible()){ |
|
292 m_menu->setVisible(false); |
|
293 return; |
|
294 } |
|
295 |
|
296 // slow press -> move |
|
297 if (m_mousePressTime.msecsTo(QTime::currentTime())>300) return; |
|
298 |
|
299 // quick press -> show menu |
301 int x = event->x(); |
300 int x = event->x(); |
302 int y = event->y(); |
301 int y = event->y(); |
303 m_menu->move(x, y); |
302 m_menu->move(x, y); |
304 m_menu->setVisible(true); |
303 m_menu->setVisible(true); |
305 |
304 |
306 } |
305 } |
|
306 |
|
307 |
|
308 void View::update(){ |
|
309 if (!m_mouseMode) |
|
310 m_controller->updateCoordinates(); |
|
311 |
|
312 if (m_menu->isVisible()){ |
|
313 if (m_mousePressTime.msecsTo(QTime::currentTime())>5000) |
|
314 m_menu->setVisible(false); |
|
315 } |
|
316 |
|
317 |
|
318 int x = m_mouseMode?m_exController->getX():m_controller->getX(); |
|
319 int y = m_mouseMode?m_exController->getY():m_controller->getY(); |
|
320 |
|
321 x = checkX(m_mouseMode?x+m_dx:x); |
|
322 y = checkY(m_mouseMode?y+m_dy:y); |
|
323 |
|
324 setSceneRect(x, y, width(), height()); |
|
325 } |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 void View::handleProximity(){ |
|
331 |
|
332 // close - not close - close, quite fast sequence required for zoom |
|
333 // zoom factors 0.5, 1, 2, 3 - cyclic iterations |
|
334 |
|
335 bool isClose = m_proximitySensor.reading()->close(); |
|
336 |
|
337 |
|
338 // zoom-in, once? |
|
339 if (isClose){ |
|
340 if (!m_isToBeZoomed){ |
|
341 m_zoomTime = QTime::currentTime(); |
|
342 m_isToBeZoomed = true; |
|
343 } |
|
344 else{ |
|
345 m_isToBeZoomed = false; // ready to start again |
|
346 |
|
347 if (m_zoomTime.msecsTo(QTime::currentTime())>1000) return; |
|
348 m_index = m_index==3?0:m_index+1; |
|
349 qDebug()<< " m_index="<<m_index; |
|
350 m_pix = m_pix.scaledToHeight(m_scaledHeight[m_index]); |
|
351 } |
|
352 } |
|
353 |
|
354 setupWindow(); |
|
355 update(); |
|
356 |
|
357 } |
|
358 |
|
359 |
|
360 void View::handleALS(){ |
|
361 |
|
362 int lightLevel = m_ambientLightSensor.reading()->lightLevel(); |
|
363 qDebug()<<"lightLevel="<<lightLevel; |
|
364 |
|
365 switch (lightLevel){ |
|
366 case 0: |
|
367 case 1:{ |
|
368 // if dark, make image "brighter" |
|
369 QPainter painter; |
|
370 painter.begin(&m_pix); |
|
371 painter.setPen(Qt::NoPen); |
|
372 QColor slightlyOpaqueWhite(255, 255, 255, 63); |
|
373 painter.fillRect(m_pix.rect(), slightlyOpaqueWhite); |
|
374 painter.end(); |
|
375 break; |
|
376 } |
|
377 default:{ |
|
378 if (m_lightLevel>1) break; |
|
379 QPixmap bgPix(":/images/koskipuisto_pieni.jpg"); |
|
380 m_pix = bgPix.scaledToHeight(m_scaledHeight[m_index]); |
|
381 break; |
|
382 } |
|
383 } |
|
384 m_lightLevel = lightLevel; |
|
385 |
|
386 } |
|
387 |
|
388 |
|
389 void View::setupWindow(){ |
|
390 m_imageWidth = m_pix.width(); |
|
391 m_imageHeight = m_pix.height(); |
|
392 |
|
393 setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate); |
|
394 setBackgroundBrush(m_pix); |
|
395 setCacheMode(QGraphicsView::CacheBackground); |
|
396 setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); |
|
397 setWindowTitle("Panorama"); |
|
398 |
|
399 } |
|
400 |
|
401 |
|
402 |