|
1 #include "view.h" |
|
2 #include "inputcontroller.h" |
|
3 #include "keycontroller.h" |
|
4 #include "accelerometercontroller.h" |
|
5 #include "magnetometercontroller.h" |
|
6 #include "rotationcontroller.h" |
|
7 #include "orientationcontroller.h" |
|
8 #include "compasscontroller.h" |
|
9 |
|
10 int View::m_imageWidth; |
|
11 int View::m_imageHeight; |
|
12 QString View::m_currentSensor; |
|
13 |
|
14 View::View(QGraphicsScene *scene) : |
|
15 QGraphicsView(scene), m_timer(this) ,m_delay(30){ |
|
16 |
|
17 m_controller = 0; |
|
18 |
|
19 QPixmap bgPix(":/images/koskipuisto_pieni.jpg"); |
|
20 m_pix = bgPix; |
|
21 m_imageWidth = bgPix.width(); |
|
22 m_imageHeight = bgPix.height(); |
|
23 int h= height(); |
|
24 int y = qAbs(m_imageHeight-h)/2; |
|
25 setSceneRect(0, y, width(), h); |
|
26 |
|
27 setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate); |
|
28 setBackgroundBrush(bgPix); |
|
29 setCacheMode(QGraphicsView::CacheBackground); |
|
30 setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); |
|
31 setWindowTitle("Panorama"); |
|
32 |
|
33 m_sensors.append(InputController::QACCELEROMETER); |
|
34 m_sensors.append(InputController::QORIENTATIONSENSOR); |
|
35 m_sensors.append(InputController::QMAGNETOMETER); |
|
36 m_sensors.append(InputController::QROTATIONSENSOR); |
|
37 m_sensors.append(InputController::QTAPSENSOR); |
|
38 m_sensors.append(InputController::QCOMPASS); |
|
39 m_sensors.append(InputController::QKEYS); |
|
40 |
|
41 |
|
42 m_menu = new QMenu(this); |
|
43 |
|
44 createActions(); |
|
45 handleAction(NULL,InputController::QACCELEROMETER); |
|
46 |
|
47 m_timer.setSingleShot(false); |
|
48 m_timer.start(m_delay); |
|
49 connect(&m_timer, SIGNAL(timeout()), this, SLOT(update())); |
|
50 } |
|
51 |
|
52 View::~View(){ if (m_controller) delete m_controller;} |
|
53 |
|
54 |
|
55 void View::resizeEvent(QResizeEvent *event) |
|
56 { |
|
57 QGraphicsView::resizeEvent(event); |
|
58 fitInView(sceneRect(), Qt::KeepAspectRatio); |
|
59 } |
|
60 |
|
61 |
|
62 |
|
63 void View::handleAction(QString oldSensor, QString newSensor){ |
|
64 |
|
65 m_menu->setVisible(false); |
|
66 |
|
67 if (oldSensor==newSensor) return; |
|
68 |
|
69 |
|
70 QList<QAction*> tmpActions = m_menu->actions(); |
|
71 for (int i=0; i<tmpActions.length(); i++){ |
|
72 if (tmpActions.at(i)->text() == oldSensor){ |
|
73 tmpActions.at(i)->setEnabled(true); |
|
74 continue; |
|
75 } |
|
76 if (tmpActions.at(i)->text() == newSensor){ |
|
77 tmpActions.at(i)->setEnabled(false); |
|
78 } |
|
79 } |
|
80 m_currentSensor = newSensor; |
|
81 switchController(m_currentSensor); |
|
82 |
|
83 } |
|
84 |
|
85 |
|
86 |
|
87 void View::createActions() |
|
88 { |
|
89 |
|
90 for (int i=0; i<m_sensors.length();i++){ |
|
91 QAction* tmp = new QAction(m_sensors.at(i), this); |
|
92 |
|
93 const QString sensor = m_sensors.at(i); |
|
94 do{ |
|
95 if (sensor==InputController::QACCELEROMETER){ |
|
96 connect(tmp, SIGNAL(triggered()), this, SLOT(startAccelerometer())); |
|
97 break; |
|
98 } |
|
99 if (sensor==InputController::QORIENTATIONSENSOR){ |
|
100 connect(tmp, SIGNAL(triggered()), this, SLOT(startOrientationSensor())); |
|
101 break; |
|
102 } |
|
103 if (sensor==InputController::QROTATIONSENSOR){ |
|
104 connect(tmp, SIGNAL(triggered()), this, SLOT(startRotationSensor())); |
|
105 break; |
|
106 } |
|
107 if (sensor==InputController::QMAGNETOMETER){ |
|
108 connect(tmp, SIGNAL(triggered()), this, SLOT(startMagnetometer())); |
|
109 break; |
|
110 } |
|
111 if (sensor==InputController::QTAPSENSOR){ |
|
112 connect(tmp, SIGNAL(triggered()), this, SLOT(startTapSensor())); |
|
113 break; |
|
114 } |
|
115 if (sensor==InputController::QCOMPASS){ |
|
116 connect(tmp, SIGNAL(triggered()), this, SLOT(startCompass())); |
|
117 break; |
|
118 } |
|
119 if (sensor==InputController::QKEYS){ |
|
120 connect(tmp, SIGNAL(triggered()), this, SLOT(startKeys())); |
|
121 break; |
|
122 } |
|
123 }while (true); |
|
124 m_menu->addAction(tmp); |
|
125 } |
|
126 |
|
127 } |
|
128 |
|
129 void View::startAccelerometer(){ |
|
130 handleAction(m_currentSensor, InputController::QACCELEROMETER); |
|
131 } |
|
132 |
|
133 void View::startOrientationSensor(){ |
|
134 handleAction(m_currentSensor, InputController::QORIENTATIONSENSOR); |
|
135 } |
|
136 |
|
137 void View::startTapSensor(){ |
|
138 handleAction(m_currentSensor, InputController::QTAPSENSOR); |
|
139 } |
|
140 |
|
141 void View::startMagnetometer(){ |
|
142 handleAction(m_currentSensor, InputController::QMAGNETOMETER); |
|
143 } |
|
144 |
|
145 void View::startRotationSensor(){ |
|
146 handleAction(m_currentSensor, InputController::QROTATIONSENSOR); |
|
147 } |
|
148 |
|
149 void View::startKeys(){ |
|
150 handleAction(m_currentSensor, InputController::QKEYS); |
|
151 } |
|
152 |
|
153 void View::startCompass(){ |
|
154 handleAction(m_currentSensor, InputController::QCOMPASS); |
|
155 } |
|
156 |
|
157 |
|
158 void View::update(){ |
|
159 if (m_mouseMode){ |
|
160 m_x = checkX(m_x + m_dx); |
|
161 m_y = checkY(m_y+ m_dy); |
|
162 } |
|
163 else{ |
|
164 if (m_controller){ |
|
165 m_x = checkX(m_controller->getX()); |
|
166 m_y = checkY(m_controller->getY()); |
|
167 } |
|
168 } |
|
169 setSceneRect(m_x, m_y, width(), height()); |
|
170 } |
|
171 |
|
172 |
|
173 void View::keyPressEvent(QKeyEvent *e) |
|
174 { |
|
175 if (m_currentSensor!=InputController::QKEYS) return; |
|
176 |
|
177 if (m_controller) m_controller->keyPressEvent(e); |
|
178 } |
|
179 |
|
180 |
|
181 void View::switchController(QString sensor){ |
|
182 |
|
183 if (m_controller){ |
|
184 delete m_controller; |
|
185 m_controller =0; |
|
186 } |
|
187 |
|
188 do{ |
|
189 if (sensor==InputController::QACCELEROMETER){ |
|
190 m_controller = new AccelerometerController(); |
|
191 break; |
|
192 } |
|
193 if (sensor==InputController::QROTATIONSENSOR){ |
|
194 m_controller = new RotationController(); |
|
195 break; |
|
196 } |
|
197 if (sensor==InputController::QMAGNETOMETER){ |
|
198 m_controller = new MagnetometerController(); |
|
199 break; |
|
200 } |
|
201 if (sensor==InputController::QORIENTATIONSENSOR){ |
|
202 m_controller = new OrientationController(); |
|
203 break; |
|
204 } |
|
205 |
|
206 if (sensor==InputController::QTAPSENSOR){ |
|
207 break; |
|
208 } |
|
209 if (sensor==InputController::QCOMPASS){ |
|
210 m_controller = new CompassController(); |
|
211 break; |
|
212 } |
|
213 if (sensor==InputController::QKEYS){ |
|
214 m_controller = new KeyController(); |
|
215 break; |
|
216 } |
|
217 |
|
218 }while (true); |
|
219 |
|
220 if (m_controller) m_controller->start(); |
|
221 |
|
222 } |
|
223 |
|
224 |
|
225 int View::checkX(int x){ |
|
226 return qAbs(x) < m_imageWidth ? x : x % m_imageWidth; |
|
227 |
|
228 } |
|
229 |
|
230 int View::checkY(int y){ |
|
231 int limit = m_imageHeight-height(); |
|
232 if (y<0){ |
|
233 y = limit<0?-limit/2:0; |
|
234 if (m_controller) m_controller->setY(y); |
|
235 return y; |
|
236 } |
|
237 |
|
238 if (limit<0){ |
|
239 y=-limit/2; |
|
240 if (m_controller) m_controller->setY(y); |
|
241 return y; |
|
242 } |
|
243 |
|
244 |
|
245 if (y< limit){ return y;} |
|
246 |
|
247 y = limit; |
|
248 if (m_controller) m_controller->setY(y); |
|
249 return y; |
|
250 } |
|
251 |
|
252 |
|
253 |
|
254 void View::mousePressEvent ( QMouseEvent* ){ |
|
255 m_mousePressTime = QTime::currentTime(); |
|
256 m_exController = m_controller; |
|
257 m_mouseMode = false; |
|
258 } |
|
259 |
|
260 |
|
261 |
|
262 void View::mouseMoveEvent(QMouseEvent* event){ |
|
263 |
|
264 if (m_mouseMode){ |
|
265 |
|
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; |
|
279 m_eventX = event->x(); |
|
280 m_eventY = event->y(); |
|
281 m_controller = 0; |
|
282 } |
|
283 |
|
284 } |
|
285 |
|
286 |
|
287 |
|
288 void View::mouseReleaseEvent(QMouseEvent* event){ |
|
289 m_controller = m_exController; |
|
290 |
|
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; |
|
298 |
|
299 if (m_mousePressTime.msecsTo(QTime::currentTime())>200) return; |
|
300 |
|
301 int x = event->x(); |
|
302 int y = event->y(); |
|
303 m_menu->move(x, y); |
|
304 m_menu->setVisible(true); |
|
305 |
|
306 } |