view.cpp 9.48 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
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
#include "view.h"

#include "camera.h"
#include "viewButtonArea.h"
#include "plugin_interaction.h"
#include "mapHandler.h"

#include "Utils/GLSLShader.h"
#include "Algo/Geometry/boundingbox.h"

#include <QKeyEvent>
#include <QMouseEvent>
#include <QWheelEvent>

namespace CGoGN
{

namespace SCHNApps
{

unsigned int View::viewCount = 0;

View::View(const QString& name, SCHNApps* s, const QGLWidget* shareWidget) :
	QGLViewer(NULL, shareWidget),
	m_name(name),
	m_schnapps(s),
	m_currentCamera(NULL),
	m_buttonArea(NULL),
	m_closeButton(NULL),
	m_VsplitButton(NULL),
	m_HsplitButton(NULL)
{
	++viewCount;

	m_currentCamera = m_schnapps->addCamera();

37
	connect(m_schnapps, SIGNAL(selectedMapChanged(MapHandlerGen*,MapHandlerGen*)), this, SLOT(selectedMapChanged(MapHandlerGen*,MapHandlerGen*)));
Pierre Kraemer's avatar
Pierre Kraemer committed
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
}

View::~View()
{
	qglviewer::Camera* c = new qglviewer::Camera();
	this->setCamera(c);

	m_currentCamera->unlinkView(this);

	foreach(PluginInteraction* p, l_plugins)
		unlinkPlugin(p);

	foreach(MapHandlerGen* m, l_maps)
		unlinkMap(m);

	delete m_buttonArea;
}

void View::setCurrentCamera(Camera* c)
{
	if(c != m_currentCamera && c != NULL)
	{
		Camera* prev = m_currentCamera;
		if(prev)
			prev->unlinkView(this);

		m_currentCamera = c;
		this->setCamera(m_currentCamera);
		m_currentCamera->linkView(this);

		emit(currentCameraChanged(prev, c));

70
		updateCurrentCameraBB();
Pierre Kraemer's avatar
Pierre Kraemer committed
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
		updateGL();
	}
}

void View::setCurrentCamera(const QString& name)
{
	Camera* c = m_schnapps->getCamera(name);
	if(c)
		setCurrentCamera(c);
}

bool View::usesCamera(const QString& cameraName) const
{
	return m_currentCamera->getName() == cameraName;
}

void View::linkPlugin(PluginInteraction* plugin)
{
	if(plugin && !l_plugins.contains(plugin))
	{
		l_plugins.push_back(plugin);
		plugin->linkView(this);
		emit(pluginLinked(plugin));
		updateGL();
	}
}

void View::linkPlugin(const QString& name)
{
	PluginInteraction* p = dynamic_cast<PluginInteraction*>(m_schnapps->getPlugin(name));
	if(p)
		linkPlugin(p);
}

void View::unlinkPlugin(PluginInteraction* plugin)
{
	if(l_plugins.removeOne(plugin))
	{
		plugin->unlinkView(this);
		emit(pluginUnlinked(plugin));
111
112
//		removed because of crash when application close
//		updateGL();
Pierre Kraemer's avatar
Pierre Kraemer committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
	}
}

void View::unlinkPlugin(const QString& name)
{
	PluginInteraction* p = dynamic_cast<PluginInteraction*>(m_schnapps->getPlugin(name));
	if(p)
		unlinkPlugin(p);
}

bool View::isLinkedToPlugin(const QString& name) const
{
	PluginInteraction* p = dynamic_cast<PluginInteraction*>(m_schnapps->getPlugin(name));
	return l_plugins.contains(p);
}

void View::linkMap(MapHandlerGen* map)
{
	if(map && !l_maps.contains(map))
	{
		l_maps.push_back(map);
		map->linkView(this);
		emit(mapLinked(map));
136

137
		updateCurrentCameraBB();
Pierre Kraemer's avatar
Pierre Kraemer committed
138
		updateGL();
139

Pierre Kraemer's avatar
Pierre Kraemer committed
140
		connect(map->getFrame(), SIGNAL(modified()), this, SLOT(updateGL()));
141
		connect(map, SIGNAL(selectedCellsChanged(CellSelectorGen*)), this, SLOT(updateGL()));
142

143
		if(map->isSelectedMap())
144
			setManipulatedFrame(map->getFrame());
Pierre Kraemer's avatar
Pierre Kraemer committed
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
	}
}

void View::linkMap(const QString& name)
{
	MapHandlerGen* m = m_schnapps->getMap(name);
	if(m)
		linkMap(m);
}

void View::unlinkMap(MapHandlerGen* map)
{
	if(l_maps.removeOne(map))
	{
		map->unlinkView(this);
		emit(mapUnlinked(map));
161

162
		updateCurrentCameraBB();
163
164
//		removed because of crash when application close
//		updateGL();
165

Pierre Kraemer's avatar
Pierre Kraemer committed
166
		disconnect(map->getFrame(), SIGNAL(modified()), this, SLOT(updateGL()));
Pierre Kraemer's avatar
Pierre Kraemer committed
167
		disconnect(map, SIGNAL(selectedCellsChanged(CellSelectorGen*)), this, SLOT(updateGL()));
168
169
170

		if(map == m_schnapps->getSelectedMap())
			setManipulatedFrame(NULL);
Pierre Kraemer's avatar
Pierre Kraemer committed
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
	}
}

void View::unlinkMap(const QString& name)
{
	MapHandlerGen* m = m_schnapps->getMap(name);
	if(m)
		unlinkMap(m);
}

bool View::isLinkedToMap(const QString& name) const
{
	MapHandlerGen* m = m_schnapps->getMap(name);
	return l_maps.contains(m);
}

void View::init()
{
	m_buttonArea = new ViewButtonArea(this);
	m_buttonArea->setTopRightPosition(this->width(), 0);

	m_VsplitButton = new ViewButton(":icons/icons/Vsplit.png", this);
	m_buttonArea->addButton(m_VsplitButton);
	connect(m_VsplitButton, SIGNAL(clicked(int, int, int, int)), this, SLOT(ui_verticalSplitView(int, int, int, int)));

	m_HsplitButton = new ViewButton(":icons/icons/Hsplit.png", this);
	m_buttonArea->addButton(m_HsplitButton);
	connect(m_HsplitButton, SIGNAL(clicked(int, int, int, int)), this, SLOT(ui_horizontalSplitView(int, int, int, int)));

	m_closeButton = new ViewButton(":icons/icons/close.png", this);
	m_buttonArea->addButton(m_closeButton);
	connect(m_closeButton, SIGNAL(clicked(int, int, int, int)), this, SLOT(ui_closeView(int, int, int, int)));

	qglviewer::Camera* c = this->camera();
	this->setCamera(m_currentCamera);
	delete c;

	this->setBackgroundColor(QColor(0,0,0));
}

void View::preDraw()
{
	m_currentCamera->setScreenWidthAndHeight(width(), height());

	QGLViewer::preDraw();
}

void View::draw()
{
	foreach(Camera* camera, m_schnapps->getCameraSet().values())
	{
		if(camera != m_currentCamera)
		{
			if(camera->getDraw()) camera->draw();
			if(camera->getDrawPath()) camera->drawAllPaths();
		}
	}

229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
	glm::mat4 mm = getCurrentModelViewMatrix();
	glm::mat4 pm = getCurrentProjectionMatrix();

	MapHandlerGen* selectedMap = m_schnapps->getSelectedMap();

	foreach(MapHandlerGen* map, l_maps)
	{
		glm::mat4 map_mm = mm * map->getFrameMatrix();

		if(map == selectedMap)
		{
			Utils::GLSLShader* bbShader = map->getBBDrawerShader();
			if(bbShader)
				bbShader->updateMatrices(pm, map_mm);
			map->drawBB();
		}

		foreach(PluginInteraction* plugin, l_plugins)
		{
			foreach(Utils::GLSLShader* shader, plugin->getShaders())
				shader->updateMatrices(pm, map_mm);
			plugin->drawMap(this, map);
		}
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
253
254

	foreach(PluginInteraction* plugin, l_plugins)
255
		plugin->draw(this);
Pierre Kraemer's avatar
Pierre Kraemer committed
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
}

void View::postDraw()
{
	glPushAttrib(GL_ALL_ATTRIB_BITS);
	drawButtons();
	if(isSelectedView())
		drawFrame();
	glPopAttrib();

	QGLViewer::postDraw();
}

void View::resizeGL(int width, int height)
{
	QGLViewer::resizeGL(width, height);
	if(m_buttonArea)
		m_buttonArea->setTopRightPosition(width, 0);
}

void View::drawButtons()
{
	glEnable(GL_TEXTURE_2D);
	startScreenCoordinatesSystem();
	m_buttonArea->draw();
	stopScreenCoordinatesSystem();
	glDisable(GL_TEXTURE_2D);
}

void View::drawFrame()
{
	startScreenCoordinatesSystem();
	glColor3f(0.0f, 1.0f, 0.0f);
	glLineWidth(2.0);
	glBegin(GL_LINE_LOOP);
		glVertex2i(1, 1);
		glVertex2i(1, height()-1);
		glVertex2i(width()-1, height()-1);
		glVertex2i(width()-1, 1);
	glEnd();
	stopScreenCoordinatesSystem();
}

void View::keyPressEvent(QKeyEvent* event)
{
	foreach(PluginInteraction* plugin, l_plugins)
		plugin->keyPress(this, event);
	QGLViewer::keyPressEvent(event);
}

void View::keyReleaseEvent(QKeyEvent *event)
{
	foreach(PluginInteraction* plugin, l_plugins)
		plugin->keyRelease(this, event);
	QGLViewer::keyReleaseEvent(event);
}

void View::mousePressEvent(QMouseEvent* event)
{
	if(m_buttonArea->isClicked(event->x(), event->y()))
		m_buttonArea->clickButton(event->x(), event->y(), event->globalX(), event->globalY());
	else
	{
		if(!isSelectedView())
			m_schnapps->setSelectedView(this);
		foreach(PluginInteraction* plugin, l_plugins)
			plugin->mousePress(this, event);
		QGLViewer::mousePressEvent(event);
	}
}

void View::mouseReleaseEvent(QMouseEvent* event)
{
	foreach(PluginInteraction* plugin, l_plugins)
		plugin->mouseRelease(this, event);
	QGLViewer::mouseReleaseEvent(event);
}

void View::mouseMoveEvent(QMouseEvent* event)
{
	foreach(PluginInteraction* plugin, l_plugins)
		plugin->mouseMove(this, event);
	QGLViewer::mouseMoveEvent(event);
}

void View::wheelEvent(QWheelEvent* event)
{
	foreach(PluginInteraction* plugin, l_plugins)
		plugin->wheelEvent(this, event);
	QGLViewer::wheelEvent(event);
}

glm::mat4 View::getCurrentModelViewMatrix() const
{
	GLdouble gl_mvm[16];
	camera()->getModelViewMatrix(gl_mvm);
	glm::mat4 mvm;
	for(unsigned int i = 0; i < 4; ++i)
	{
		for(unsigned int j = 0; j < 4; ++j)
			mvm[i][j] = (float)gl_mvm[i*4+j];
	}
	return mvm;
}

glm::mat4 View::getCurrentProjectionMatrix() const
{
	GLdouble gl_pm[16];
	camera()->getProjectionMatrix(gl_pm);
	glm::mat4 pm;
	for(unsigned int i = 0; i < 4; ++i)
	{
		for(unsigned int j = 0; j < 4; ++j)
			pm[i][j] = (float)gl_pm[i*4+j];
	}
	return pm;
}

glm::mat4 View::getCurrentModelViewProjectionMatrix() const
{
	GLdouble gl_mvpm[16];
	camera()->getModelViewProjectionMatrix(gl_mvpm);
	glm::mat4 mvpm;
	for(unsigned int i = 0; i < 4; ++i)
	{
		for(unsigned int j = 0; j < 4; ++j)
			mvpm[i][j] = (float)gl_mvpm[i*4+j];
	}
	return mvpm;
}

387
void View::updateCurrentCameraBB()
Pierre Kraemer's avatar
Pierre Kraemer committed
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
{
	qglviewer::Vec bbMin(0,0,0);
	qglviewer::Vec bbMax(1,1,1);
	if(!l_maps.empty())
	{
		bbMin = l_maps[0]->getBBmin();
		bbMax = l_maps[0]->getBBmax();
		for(int i = 1; i < l_maps.size(); ++i)
		{
			MapHandlerGen* m = l_maps[i];
			qglviewer::Vec min = m->getBBmin();
			qglviewer::Vec max = m->getBBmax();
			for(unsigned int dim = 0; dim < 3; ++dim)
			{
				if(min[dim] < bbMin[dim])
					bbMin[dim] = min[dim];
				if(max[dim] > bbMax[dim])
					bbMax[dim] = max[dim];
			}
		}
	}
	camera()->setSceneBoundingBox(bbMin, bbMax);
	camera()->showEntireScene();
}

413
414
415
416
417
418
419
void View::selectedMapChanged(MapHandlerGen* prev, MapHandlerGen* cur)
{
	if(cur && isLinkedToMap(cur))
		setManipulatedFrame(cur->getFrame());
	updateGL();
}

Pierre Kraemer's avatar
Pierre Kraemer committed
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
void View::ui_verticalSplitView(int x, int y, int globalX, int globalY)
{
	m_schnapps->splitView(m_name, Qt::Horizontal);
}

void View::ui_horizontalSplitView(int x, int y, int globalX, int globalY)
{
	m_schnapps->splitView(m_name, Qt::Vertical);
}

void View::ui_closeView(int x, int y, int globalX, int globalY)
{
	m_schnapps->removeView(m_name);
}

} // namespace SCHNApps

} // namespace CGoGN