File: interactionModeManager.C

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 239,888 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; makefile: 95
file content (283 lines) | stat: -rw-r--r-- 6,335 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include <BALL/VIEW/KERNEL/MODES/interactionModeManager.h>

#include <BALL/VIEW/WIDGETS/scene.h>

#include <BALL/VIEW/KERNEL/MODES/editMode.h>
#include <BALL/VIEW/KERNEL/MODES/moveMode.h>
#include <BALL/VIEW/KERNEL/MODES/rotateMode.h>
#include <BALL/VIEW/KERNEL/MODES/pickingMode.h>

#include <QtWidgets/QMenu>

namespace BALL
{
	namespace VIEW
	{
		InteractionModeManager::InteractionModeManager(Scene* scene)
			: stereo_mode_(Renderer::NO_STEREO),
			  current_mode_(0),
			  last_mode_(0),
			  scene_(scene)
		{
			init_();
		}

		InteractionModeManager::~InteractionModeManager()
		{
			for(std::list<InteractionMode*>::iterator it = modes_.begin();
					it != modes_.end(); ++it)
			{
				delete *it;
			}
		}

		void InteractionModeManager::init_()
		{
			actions_ = new QActionGroup(this);
			//Rotate mode is the default mode
			setMode_(new RotateMode(scene_));

			modes_.push_back(current_mode_);
			modes_.push_back(new EditMode(scene_));
			modes_.push_back(new MoveMode(scene_));
			modes_.push_back(new PickingMode(scene_));

			for(std::list<InteractionMode*>::iterator it = modes_.begin(); it != modes_.end(); ++it)
			{
				if((*it)->getMainAction())
				{
					actions_->addAction((*it)->getMainAction());
					connect(*it, SIGNAL(requestModeChange(InteractionMode*)), this, SLOT(setMode_(InteractionMode*)));
				}
			}
		}

		QString InteractionModeManager::getCurrentModeName() const
		{
			return current_mode_ ? current_mode_->getName() : QString::null;
		}

		void InteractionModeManager::keyPressEvent(QKeyEvent* evt)
		{
			for(std::list<InteractionMode*>::iterator it = modes_.begin(); it != modes_.end(); ++it)
			{
				if(evt->key() == (*it)->getKey())
				{
					setMode_(*it);
					return;
				}
			}

			if(handleStereoKeyPress_(evt))
			{
				return;
			}

			//Let the current mode process the key event
			current_mode_->keyPressEvent(evt);
		}

		void InteractionModeManager::setMode_(InteractionMode* new_mode)
		{
			if(new_mode == 0)
			{
				return;
			}

			last_mode_ = current_mode_;
			current_mode_ = new_mode;

			if(last_mode_) last_mode_->deactivate();
			current_mode_->activate();
		}

		void InteractionModeManager::switchToLastMode()
		{
			setMode_(last_mode_);
		}

		bool InteractionModeManager::handleStereoKeyPress_(QKeyEvent* evt)
		{
			// TODO make keys configurable in shortcutRegistry 
			if ((stereo_mode_ == Renderer::NO_STEREO) &&
					(evt->key() == Qt::Key_Escape))
			{
				setMode_(last_mode_);
				return true;
			}

			if (stereo_mode_ == Renderer::NO_STEREO)
			{
				evt->ignore();
				return false;
			}

			// TODO make keys configurable in shortcutRegistry 
			if ((evt->key() == Qt::Key_Y && evt->modifiers() == Qt::AltModifier) ||
					evt->key() == Qt::Key_Escape)
			{
				scene_->exitStereo();
				return true;
			}

			// TODO make keys configurable in shortcutRegistry 
			// setting of eye and focal distance
			if (evt->key() != Qt::Key_Left  &&
					evt->key() != Qt::Key_Right &&
					evt->key() != Qt::Key_Up    &&
					evt->key() != Qt::Key_Down)
			{
				evt->ignore();
				return false;
			}

			// TODO make keys configurable in shortcutRegistry 
			// setting of eye distance
			if (evt->key() == Qt::Key_Left ||
					evt->key() == Qt::Key_Right)
			{
				float new_distance = scene_->getStage()->getEyeDistance();

				// TODO make keys configurable in shortcutRegistry 
				float modifier = 1;
				if (evt->key() == Qt::Key_Left)
				{
					modifier = -0.1;
				}
				else
				{
					modifier = +0.1;
				}

				// TODO make keys configurable in shortcutRegistry 
				if (evt->modifiers() == Qt::ShiftModifier)
				{
					modifier *= 10;
				}

				new_distance += modifier;

				// prevent strange values
				if (new_distance < 0)
				{
					new_distance = 0;
				}

				if (new_distance > 4)
				{
					new_distance = 4;
				}

				scene_->getStage()->setEyeDistance(new_distance);
			}
			// setting of focal distance
			else
			{
				float new_focal_distance = scene_->getStage()->getFocalDistance();

				// TODO make keys configurable in shortcutRegistry 
				float modifier = 1;
				if (evt->key() == Qt::Key_Down)
				{
					modifier = -1;
				}
				else
				{
					modifier = +1;
				}

				// TODO make keys configurable in shortcutRegistry 
				if (evt->modifiers() == Qt::ShiftModifier)
				{
					modifier *= 10;
				}

				new_focal_distance += modifier;

				// prevent strange values
				if (new_focal_distance < 7)
				{
					new_focal_distance = 7;
				}

				if (new_focal_distance > 1000)
				{
					new_focal_distance = 1000;
				}

				scene_->getStage()->setFocalDistance(new_focal_distance);
			}

			//TODO: readd this
			//stage_settings_->updateFromStage();
			scene_->updateGL();

			return true;
		}

		void InteractionModeManager::keyReleaseEvent(QKeyEvent* evt)
		{
			current_mode_->keyReleaseEvent(evt);
		}

		void InteractionModeManager::mouseDoubleClickEvent(QMouseEvent* evt)
		{
			current_mode_->mouseDoubleClickEvent(evt);
		}

		void InteractionModeManager::mouseMoveEvent(QMouseEvent* evt)
		{
			current_mode_->mouseMoveEvent(evt);
		}

		void InteractionModeManager::mousePressEvent(QMouseEvent* evt)
		{
			current_mode_->mousePressEvent(evt);
		}

		void InteractionModeManager::mouseReleaseEvent(QMouseEvent* evt)
		{
			current_mode_->mouseReleaseEvent(evt);
		}

		void InteractionModeManager::wheelEvent(QWheelEvent* evt)
		{
			current_mode_->wheelEvent(evt);
		}

		void InteractionModeManager::populateContextMenu(QMenu* menu)
		{
			for(std::list<InteractionMode*>::iterator it = modes_.begin(); it != modes_.end(); ++it)
			{
				menu->addAction((*it)->getMainAction());
			}

			menu->addSeparator();

			for(std::list<InteractionMode*>::iterator it = modes_.begin(); it != modes_.end(); ++it)
			{
				(*it)->populateContextMenu(menu);
			}
		}

		void InteractionModeManager::addToolBarEntries(QToolBar* tb)
		{
			for(std::list<InteractionMode*>::iterator it = modes_.begin(); it != modes_.end(); ++it)
			{
				(*it)->addToolBarEntries(tb);
			}
		}

		void InteractionModeManager::setMouseSensitivity(float sensitivity)
		{
			for(const auto& mode: modes_)
				mode->setMouseSensitivity(sensitivity);
		}

		void InteractionModeManager::setMouseWheelSensitivity(float sensitivity)
		{
			for(const auto& mode: modes_)
				mode->setMouseWheelSensitivity(sensitivity);
		}
	}
}