|
1 #include "view.h" |
|
2 #include "accelerometercontroller.h" |
|
3 #include "magnetometercontroller.h" |
|
4 #include "rotationcontroller.h" |
|
5 #include "orientationcontroller.h" |
|
6 #include "compasscontroller.h" |
|
7 #include "tapcontroller.h" |
|
8 #include <QDebug> |
|
9 #include <iostream> |
|
10 |
|
11 |
|
12 |
|
13 int View::m_imageWidth; |
|
14 int View::m_imageHeight; |
|
15 QString View::m_currentSensor; |
|
16 bool View::m_isStarted = true; |
|
17 |
|
18 |
|
19 |
|
20 View::View(QGraphicsScene *scene) : |
|
21 QGraphicsView(scene), m_timer(this) ,m_delay(30){ |
|
22 |
|
23 qDebug()<<"in View now"; |
|
24 |
|
25 QPixmap bgPix(":/images/koskipuisto_pieni.jpg"); |
|
26 m_pix = bgPix; |
|
27 |
|
28 setupWindow(); |
|
29 |
|
30 int h = height(); |
|
31 int y = qAbs(m_imageHeight-h)/2; |
|
32 setSceneRect(0, y, width(), h); |
|
33 |
|
34 |
|
35 m_sensors.append(InputController::QACCELEROMETER); |
|
36 m_sensors.append(InputController::QORIENTATIONSENSOR); |
|
37 m_sensors.append(InputController::QMAGNETOMETER); |
|
38 m_sensors.append(InputController::QROTATIONSENSOR); |
|
39 m_sensors.append(InputController::QTAPSENSOR); |
|
40 m_sensors.append(InputController::QCOMPASS); |
|
41 m_sensors.append(InputController::QKEYS); |
|
42 |
|
43 |
|
44 m_menu = new QMenu(this); |
|
45 createActions(); |
|
46 handleAction(NULL,InputController::QACCELEROMETER); |
|
47 |
|
48 m_timer.setSingleShot(false); |
|
49 m_timer.start(m_delay); |
|
50 |
|
51 connect(&m_timer, SIGNAL(timeout()), this, SLOT(update())); |
|
52 connect(this,SIGNAL(sceneRectChanged(QRectF)), this, SLOT(checkSensors(QRectF))); |
|
53 |
|
54 } |
|
55 |
|
56 |
|
57 View::~View(){ |
|
58 m_timer.stop(); |
|
59 if (m_controller) delete m_controller; |
|
60 if (m_exController) delete m_exController; |
|
61 } |
|
62 |
|
63 |
|
64 void View::resizeEvent(QResizeEvent *event) |
|
65 { |
|
66 QGraphicsView::resizeEvent(event); |
|
67 fitInView(sceneRect(), Qt::KeepAspectRatio); |
|
68 } |
|
69 |
|
70 |
|
71 |
|
72 void View::handleAction(QString oldSensor, QString newSensor){ |
|
73 |
|
74 m_menu->setVisible(false); |
|
75 |
|
76 if (oldSensor==newSensor) return; |
|
77 |
|
78 |
|
79 QList<QAction*> tmpActions = m_menu->actions(); |
|
80 for (int i=0; i<tmpActions.length(); i++){ |
|
81 if (tmpActions.at(i)->text() == oldSensor){ |
|
82 tmpActions.at(i)->setEnabled(true); |
|
83 continue; |
|
84 } |
|
85 if (tmpActions.at(i)->text() == newSensor){ |
|
86 tmpActions.at(i)->setEnabled(false); |
|
87 } |
|
88 } |
|
89 m_currentSensor = newSensor; |
|
90 switchController(m_currentSensor); |
|
91 |
|
92 } |
|
93 |
|
94 |
|
95 |
|
96 void View::createActions() |
|
97 { |
|
98 |
|
99 for (int i=0; i<m_sensors.length();i++){ |
|
100 QAction* tmp = new QAction(m_sensors.at(i), this); |
|
101 |
|
102 const QString sensor = m_sensors.at(i); |
|
103 do{ |
|
104 if (sensor==InputController::QACCELEROMETER){ |
|
105 connect(tmp, SIGNAL(triggered()), this, SLOT(startAccelerometer())); |
|
106 break; |
|
107 } |
|
108 if (sensor==InputController::QORIENTATIONSENSOR){ |
|
109 connect(tmp, SIGNAL(triggered()), this, SLOT(startOrientationSensor())); |
|
110 break; |
|
111 } |
|
112 if (sensor==InputController::QROTATIONSENSOR){ |
|
113 connect(tmp, SIGNAL(triggered()), this, SLOT(startRotationSensor())); |
|
114 break; |
|
115 } |
|
116 if (sensor==InputController::QMAGNETOMETER){ |
|
117 connect(tmp, SIGNAL(triggered()), this, SLOT(startMagnetometer())); |
|
118 break; |
|
119 } |
|
120 if (sensor==InputController::QTAPSENSOR){ |
|
121 connect(tmp, SIGNAL(triggered()), this, SLOT(startTapSensor())); |
|
122 break; |
|
123 } |
|
124 if (sensor==InputController::QCOMPASS){ |
|
125 connect(tmp, SIGNAL(triggered()), this, SLOT(startCompass())); |
|
126 break; |
|
127 } |
|
128 if (sensor==InputController::QKEYS){ |
|
129 connect(tmp, SIGNAL(triggered()), this, SLOT(startKeys())); |
|
130 break; |
|
131 } |
|
132 }while (true); |
|
133 m_menu->addAction(tmp); |
|
134 } |
|
135 |
|
136 } |
|
137 |
|
138 void View::startAccelerometer(){ |
|
139 handleAction(m_currentSensor, InputController::QACCELEROMETER); |
|
140 } |
|
141 |
|
142 void View::startOrientationSensor(){ |
|
143 handleAction(m_currentSensor, InputController::QORIENTATIONSENSOR); |
|
144 } |
|
145 |
|
146 void View::startTapSensor(){ |
|
147 handleAction(m_currentSensor, InputController::QTAPSENSOR); |
|
148 } |
|
149 |
|
150 void View::startMagnetometer(){ |
|
151 handleAction(m_currentSensor, InputController::QMAGNETOMETER); |
|
152 } |
|
153 |
|
154 void View::startRotationSensor(){ |
|
155 handleAction(m_currentSensor, InputController::QROTATIONSENSOR); |
|
156 } |
|
157 |
|
158 void View::startKeys(){ |
|
159 handleAction(m_currentSensor, InputController::QKEYS); |
|
160 } |
|
161 |
|
162 void View::startCompass(){ |
|
163 handleAction(m_currentSensor, InputController::QCOMPASS); |
|
164 } |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 void View::keyPressEvent(QKeyEvent *e) |
|
170 { |
|
171 if (m_currentSensor!=InputController::QKEYS) return; |
|
172 |
|
173 if (m_controller) m_controller->keyPressEvent(e); |
|
174 } |
|
175 |
|
176 |
|
177 void View::switchController(QString sensor){ |
|
178 |
|
179 if (m_controller){ |
|
180 delete m_controller; |
|
181 m_controller =0; |
|
182 } |
|
183 |
|
184 if (sensor==InputController::QACCELEROMETER){ |
|
185 m_controller = new AccelerometerController(); |
|
186 return; |
|
187 } |
|
188 if (sensor==InputController::QROTATIONSENSOR){ |
|
189 m_controller = new RotationController(); |
|
190 return; |
|
191 } |
|
192 if (sensor==InputController::QMAGNETOMETER){ |
|
193 m_controller = new MagnetometerController(); |
|
194 return; |
|
195 } |
|
196 if (sensor==InputController::QORIENTATIONSENSOR){ |
|
197 m_controller = new OrientationController(); |
|
198 return; |
|
199 } |
|
200 if (sensor==InputController::QTAPSENSOR){ |
|
201 m_controller = new TapController(); |
|
202 return; |
|
203 } |
|
204 if (sensor==InputController::QCOMPASS){ |
|
205 m_controller = new CompassController(); |
|
206 return; |
|
207 } |
|
208 if (sensor==InputController::QKEYS){ |
|
209 m_controller = new KeyController(); |
|
210 return; |
|
211 } |
|
212 |
|
213 |
|
214 |
|
215 } |
|
216 |
|
217 |
|
218 int View::checkX(int x){ |
|
219 int tmpX = qAbs(x) < m_imageWidth ? x : x % m_imageWidth; |
|
220 m_mouseMode?m_exController->setX(tmpX):m_controller->setX(tmpX); |
|
221 return tmpX; |
|
222 |
|
223 } |
|
224 |
|
225 int View::checkY(int y){ |
|
226 int limit = m_imageHeight-height(); |
|
227 if (y<0){ |
|
228 y = limit<0?-limit/2:0; |
|
229 } |
|
230 else if (limit<0){ |
|
231 y=-limit/2; |
|
232 } |
|
233 else if (y > limit){y=limit;} |
|
234 |
|
235 m_mouseMode?m_exController->setY(y):m_controller->setY(y); |
|
236 return y; |
|
237 } |
|
238 |
|
239 |
|
240 |
|
241 void View::mousePressEvent ( QMouseEvent* ){ |
|
242 m_mousePressTime = QTime::currentTime(); |
|
243 m_exController = m_controller; |
|
244 m_mouseMode = false; |
|
245 } |
|
246 |
|
247 |
|
248 void View::mouseMoveEvent(QMouseEvent* event){ |
|
249 |
|
250 // first time |
|
251 if (!m_mouseMode){ |
|
252 m_mouseMode = true; |
|
253 m_eventX = event->x(); |
|
254 m_eventY = event->y(); |
|
255 return; |
|
256 } |
|
257 |
|
258 if (m_menu->isVisible()){ |
|
259 if (m_mousePressTime.msecsTo(QTime::currentTime())>300) |
|
260 m_menu->setVisible(false); |
|
261 } |
|
262 |
|
263 int cur_x = event->x(); |
|
264 int cur_y = event->y(); |
|
265 |
|
266 m_dx = m_eventX - cur_x; |
|
267 m_dy = m_eventY - cur_y; |
|
268 |
|
269 m_eventX = cur_x; |
|
270 m_eventY = cur_y; |
|
271 |
|
272 update(); |
|
273 |
|
274 } |
|
275 |
|
276 |
|
277 |
|
278 void View::mouseReleaseEvent(QMouseEvent* event){ |
|
279 m_controller = m_exController; |
|
280 m_exController = 0; |
|
281 m_mouseMode = false; |
|
282 |
|
283 |
|
284 if (m_menu->isVisible()){ |
|
285 m_menu->setVisible(false); |
|
286 return; |
|
287 } |
|
288 |
|
289 // slow press -> move |
|
290 if (m_mousePressTime.msecsTo(QTime::currentTime())>300) return; |
|
291 |
|
292 // quick press -> show menu |
|
293 int x = event->x(); |
|
294 int y = event->y(); |
|
295 m_menu->move(x, y); |
|
296 m_menu->setVisible(true); |
|
297 |
|
298 } |
|
299 |
|
300 |
|
301 void View::update(){ |
|
302 |
|
303 if (!m_isStarted) return; |
|
304 |
|
305 if (!m_mouseMode && m_controller) |
|
306 m_controller->updateCoordinates(); |
|
307 |
|
308 if (m_menu->isVisible()){ |
|
309 if (m_mousePressTime.msecsTo(QTime::currentTime())>5000) |
|
310 m_menu->setVisible(false); |
|
311 } |
|
312 |
|
313 |
|
314 int x = m_mouseMode?m_exController->getX():m_controller->getX(); |
|
315 int y = m_mouseMode?m_exController->getY():m_controller->getY(); |
|
316 |
|
317 x = checkX(m_mouseMode?x+m_dx:x); |
|
318 y = checkY(m_mouseMode?y+m_dy:y); |
|
319 |
|
320 setSceneRect(x, y, width(), height()); |
|
321 } |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 void View::setupWindow(){ |
|
327 int w = m_pix.width(); |
|
328 int h = m_pix.height(); |
|
329 |
|
330 if (m_imageWidth>0 && m_imageHeight > 0){ |
|
331 int x = m_controller? m_controller->getX(): (m_exController?m_exController->getX():0); |
|
332 int y = m_controller? m_controller->getY(): (m_exController?m_exController->getY():0); |
|
333 |
|
334 checkX(x*w/m_imageWidth); |
|
335 checkY(y*h/m_imageHeight); |
|
336 } |
|
337 |
|
338 |
|
339 m_imageWidth = w; |
|
340 m_imageHeight = h; |
|
341 |
|
342 |
|
343 setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate); |
|
344 setBackgroundBrush(m_pix); |
|
345 setCacheMode(QGraphicsView::CacheBackground); |
|
346 setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); |
|
347 setWindowTitle("PanoramaWithSense"); |
|
348 |
|
349 } |
|
350 |
|
351 |
|
352 void View::focusInEvent(QFocusEvent*) { |
|
353 if (m_controller) m_controller->doStart(); |
|
354 m_isStarted = true; |
|
355 } |
|
356 |
|
357 void View::focusOutEvent(QFocusEvent*) { |
|
358 if (m_controller) m_controller->doStop(); |
|
359 m_isStarted = false; |
|
360 } |
|
361 |
|
362 |