view.cpp 19.6 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1 2 3 4 5 6 7 8 9 10
#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"

11 12
#include "slot_debug.h"

Pierre Kraemer's avatar
Pierre Kraemer committed
13 14 15
#include <QKeyEvent>
#include <QMouseEvent>
#include <QWheelEvent>
Sylvain Thery's avatar
Sylvain Thery committed
16
#include <QMessageBox>
Pierre Kraemer's avatar
Pierre Kraemer committed
17 18 19 20 21 22 23 24

namespace CGoGN
{

namespace SCHNApps
{

unsigned int View::viewCount = 0;
25 26 27

View::View(const QString& name, SCHNApps* s, const QGLWidget* shareWidget) :
	QGLViewer( NULL, shareWidget),
28 29 30 31
	b_updatingUI(false),
	m_name(name),
	m_schnapps(s),
	m_currentCamera(NULL),
32 33
	m_bbMin(0.0, 0.0, 0.0),
	m_bbMax(0.0, 0.0, 0.0),
34 35 36 37 38 39 40 41
	m_buttonArea(NULL),
	m_closeButton(NULL),
	m_VsplitButton(NULL),
	m_HsplitButton(NULL),
	m_buttonAreaLeft(NULL),
	m_mapsButton(NULL),
	m_pluginsButton(NULL),
	m_camerasButton(NULL),
42 43 44 45 46
	m_dialogMaps(NULL),
	m_dialogPlugins(NULL),
	m_dialogCameras(NULL),
	m_frameDrawer(NULL),
	m_textureWallpaper(NULL),
47 48
	m_shaderWallpaper(NULL),
	b_saveSnapshots(false)
49 50 51
{
	++viewCount;

52
	setSnapshotFormat("BMP");
53
	setSnapshotFileName(m_name);
54
	setSnapshotQuality(100);
55

56 57
	connect(m_schnapps, SIGNAL(selectedMapChanged(MapHandlerGen*,MapHandlerGen*)), this, SLOT(selectedMapChanged(MapHandlerGen*,MapHandlerGen*)));

58 59 60
	m_dialogMaps = new ListPopUp("Linked Maps");
	m_dialogPlugins = new ListPopUp("Enabled Plugins");
	m_dialogCameras = new ListPopUp("Cameras");
Pierre Kraemer's avatar
Pierre Kraemer committed
61

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
	connect(m_schnapps, SIGNAL(mapAdded(MapHandlerGen*)), this, SLOT(mapAdded(MapHandlerGen*)));
	connect(m_schnapps, SIGNAL(mapRemoved(MapHandlerGen*)), this, SLOT(mapRemoved(MapHandlerGen*)));
	connect(m_dialogMaps->list(), SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(mapCheckStateChanged(QListWidgetItem*)));

	foreach(MapHandlerGen* map, m_schnapps->getMapSet().values())
		mapAdded(map);

	connect(m_schnapps, SIGNAL(pluginEnabled(Plugin*)), this, SLOT(pluginEnabled(Plugin*)));
	connect(m_schnapps, SIGNAL(pluginDisabled(Plugin*)), this, SLOT(pluginDisabled(Plugin*)));
	connect(m_dialogPlugins->list(), SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(pluginCheckStateChanged(QListWidgetItem*)));

	foreach(Plugin* plug, m_schnapps->getPluginSet().values())
		pluginEnabled(plug);

	connect(m_schnapps, SIGNAL(cameraAdded(Camera*)), this, SLOT(cameraAdded(Camera*)));
	connect(m_schnapps, SIGNAL(cameraRemoved(Camera*)), this, SLOT(cameraRemoved(Camera*)));
	connect(m_dialogCameras->list(), SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(cameraCheckStateChanged(QListWidgetItem*)));

	foreach(Camera* cam, m_schnapps->getCameraSet().values())
		cameraAdded(cam);

83 84
	m_currentCamera = m_schnapps->addCamera();
	m_currentCamera->linkView(this);
85
	m_dialogCameras->check(m_currentCamera->getName(),Qt::Checked);
86 87

	connect(m_schnapps, SIGNAL(schnappsClosing()), this, SLOT(closeDialogs()));
88 89
}

Pierre Kraemer's avatar
Pierre Kraemer committed
90 91 92 93 94 95 96 97 98 99 100 101 102
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;
Sylvain Thery's avatar
Sylvain Thery committed
103 104 105 106 107 108 109
	delete m_buttonAreaLeft;

	delete m_dialogMaps;
	delete m_dialogPlugins;
	delete m_dialogCameras;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
110 111
void View::setCurrentCamera(Camera* c)
{
Sylvain Thery's avatar
Sylvain Thery committed
112
//	DEBUG_SLOT();
113
	if(c != m_currentCamera && c)
Pierre Kraemer's avatar
Pierre Kraemer committed
114
	{
Sylvain Thery's avatar
Sylvain Thery committed
115 116 117 118 119
		// RECORDING
		QTextStream* rec = m_schnapps->pythonStreamRecorder();
		if (rec)
			*rec << this->getName() << ".setCurrentCamera(\"" << c->getName() << "\");" << endl;

Pierre Kraemer's avatar
Pierre Kraemer committed
120 121 122 123 124 125 126 127
		Camera* prev = m_currentCamera;
		if(prev)
			prev->unlinkView(this);

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

128 129
		DEBUG_EMIT("currentCameraChanged");
		emit(currentCameraChanged(prev, c));
Pierre Kraemer's avatar
Pierre Kraemer committed
130

131
		if (prev)
Sylvain Thery's avatar
Sylvain Thery committed
132 133 134 135 136 137 138 139 140
		{
			QListWidgetItem* prevItem = m_dialogCameras->findItem(prev->getName());
			if(prevItem)
			{
				b_updatingUI = true;
				prevItem->setCheckState(Qt::Unchecked);
				b_updatingUI = false;
			}
		}
141 142

		if (m_currentCamera)
Sylvain Thery's avatar
Sylvain Thery committed
143 144 145 146 147 148 149 150 151 152
		{
			QListWidgetItem* curItem = m_dialogCameras->findItem(m_currentCamera->getName());
			if(curItem)
			{
				b_updatingUI = true;
				curItem->setCheckState(Qt::Checked);
				b_updatingUI = false;
			}
		}

153
		m_currentCamera->fitToViewsBoundingBox();
Pierre Kraemer's avatar
Pierre Kraemer committed
154 155 156 157 158
		updateGL();
	}
}

void View::setCurrentCamera(const QString& name)
159
{	
Pierre Kraemer's avatar
Pierre Kraemer committed
160 161 162 163 164
	Camera* c = m_schnapps->getCamera(name);
	if(c)
		setCurrentCamera(c);
}

165
bool View::usesCamera(const QString& name) const
Pierre Kraemer's avatar
Pierre Kraemer committed
166
{
167 168
	Camera* c = m_schnapps->getCamera(name);
	return usesCamera(c);
Pierre Kraemer's avatar
Pierre Kraemer committed
169 170 171 172
}

void View::linkPlugin(PluginInteraction* plugin)
{
Sylvain Thery's avatar
Sylvain Thery committed
173 174 175 176 177
	// RECORDING
	QTextStream* rec = m_schnapps->pythonStreamRecorder();
	if (rec)
		*rec << this->getName() << ".linkPlugin(\"" << plugin->getName() << "\");" << endl;

178
	DEBUG_SLOT();
Pierre Kraemer's avatar
Pierre Kraemer committed
179 180 181 182
	if(plugin && !l_plugins.contains(plugin))
	{
		l_plugins.push_back(plugin);
		plugin->linkView(this);
183

184
		DEBUG_EMIT("pluginLinked");
Pierre Kraemer's avatar
Pierre Kraemer committed
185
		emit(pluginLinked(plugin));
Sylvain Thery's avatar
Sylvain Thery committed
186 187

		b_updatingUI = true;
188
		m_dialogPlugins->check(plugin->getName(), Qt::Checked);
Sylvain Thery's avatar
Sylvain Thery committed
189 190
		b_updatingUI = false;

Pierre Kraemer's avatar
Pierre Kraemer committed
191 192 193 194 195 196 197 198 199 200 201 202 203
		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)
{
204
	DEBUG_SLOT();
Pierre Kraemer's avatar
Pierre Kraemer committed
205 206
	if(l_plugins.removeOne(plugin))
	{
Sylvain Thery's avatar
Sylvain Thery committed
207 208 209 210 211 212
		// RECORDING
		QTextStream* rec = m_schnapps->pythonStreamRecorder();
		if (rec)
			*rec << this->getName() << ".unlinkPlugin(\"" << plugin->getName() << "\");" << endl;


Pierre Kraemer's avatar
Pierre Kraemer committed
213
		plugin->unlinkView(this);
214

215
		DEBUG_EMIT("pluginUnlinked");
Pierre Kraemer's avatar
Pierre Kraemer committed
216
		emit(pluginUnlinked(plugin));
Sylvain Thery's avatar
Sylvain Thery committed
217 218

		b_updatingUI = true;
219
		m_dialogPlugins->check(plugin->getName(), Qt::Unchecked);
Sylvain Thery's avatar
Sylvain Thery committed
220 221
		b_updatingUI = false;

222
		updateGL();
Pierre Kraemer's avatar
Pierre Kraemer committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
	}
}

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)
{
Sylvain Thery's avatar
Sylvain Thery committed
241 242 243 244 245
	// RECORDING
	QTextStream* rec = m_schnapps->pythonStreamRecorder();
	if (rec)
		*rec << this->getName() << ".linkMap(\"" << map->getName() << "\");" << endl;

246
	DEBUG_SLOT();
Pierre Kraemer's avatar
Pierre Kraemer committed
247 248 249 250
	if(map && !l_maps.contains(map))
	{
		l_maps.push_back(map);
		map->linkView(this);
251

252
		DEBUG_EMIT("mapLinked");
Pierre Kraemer's avatar
Pierre Kraemer committed
253
		emit(mapLinked(map));
254

255
		connect(map, SIGNAL(selectedCellsChanged(CellSelectorGen*)), this, SLOT(updateGL()));
256
		connect(map, SIGNAL(boundingBoxModified()), this, SLOT(updateBoundingBox()));
257

258
		if(map->isSelectedMap())
259
			setManipulatedFrame(map->getFrame());
Sylvain Thery's avatar
Sylvain Thery committed
260

261 262
		updateBoundingBox();

Sylvain Thery's avatar
Sylvain Thery committed
263
		b_updatingUI = true;
264
		m_dialogMaps->check(map->getName(), Qt::Checked);
Sylvain Thery's avatar
Sylvain Thery committed
265
		b_updatingUI = false;
Pierre Kraemer's avatar
Pierre Kraemer committed
266 267 268 269 270 271 272 273 274 275 276 277
	}
}

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

void View::unlinkMap(MapHandlerGen* map)
{
278
	DEBUG_SLOT();
Pierre Kraemer's avatar
Pierre Kraemer committed
279 280
	if(l_maps.removeOne(map))
	{
Sylvain Thery's avatar
Sylvain Thery committed
281 282 283 284 285
		QTextStream* rec = m_schnapps->pythonStreamRecorder();
		if (rec)
			*rec << this->getName() << ".unlinkMap(\"" << map->getName() << "\");" << endl;


Pierre Kraemer's avatar
Pierre Kraemer committed
286
		map->unlinkView(this);
287

288
		DEBUG_EMIT("mapUnlinked");
Pierre Kraemer's avatar
Pierre Kraemer committed
289
		emit(mapUnlinked(map));
290

Pierre Kraemer's avatar
Pierre Kraemer committed
291
		disconnect(map, SIGNAL(selectedCellsChanged(CellSelectorGen*)), this, SLOT(updateGL()));
292
		disconnect(map, SIGNAL(boundingBoxModified()), this, SLOT(updateBoundingBox()));
293 294 295

		if(map == m_schnapps->getSelectedMap())
			setManipulatedFrame(NULL);
Sylvain Thery's avatar
Sylvain Thery committed
296

297 298
		updateBoundingBox();

Sylvain Thery's avatar
Sylvain Thery committed
299
		b_updatingUI = true;
300
		m_dialogMaps->check(map->getName(), Qt::Unchecked);
Sylvain Thery's avatar
Sylvain Thery committed
301
		b_updatingUI = false;
Pierre Kraemer's avatar
Pierre Kraemer committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
	}
}

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);
}

318 319 320 321




Pierre Kraemer's avatar
Pierre Kraemer committed
322 323
void View::init()
{
324 325 326
	glewExperimental = GL_TRUE; // needed for GL Core Profile 3.3
	glewInit();

Sylvain Thery's avatar
Sylvain Thery committed
327 328
	std::cout << "using " << glGetString(GL_VERSION) << std::endl;

329 330
	qglviewer::Camera* c = this->camera();
	this->setCamera(m_currentCamera);
Sylvain Thery's avatar
Sylvain Thery committed
331
//	delete c;
332 333 334 335 336

	this->setBackgroundColor(QColor(0,0,0));
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LESS);

Pierre Kraemer's avatar
Pierre Kraemer committed
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
	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)));

Sylvain Thery's avatar
Sylvain Thery committed
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
	m_buttonAreaLeft = new ViewButtonArea(this);
	m_buttonAreaLeft->setTopLeftPosition(0, 0);

	m_mapsButton = new ViewButton(":icons/icons/maps.png", this);
	m_buttonAreaLeft->addButton(m_mapsButton);
	connect(m_mapsButton, SIGNAL(clicked(int, int, int, int)), this, SLOT(ui_mapsListView(int, int, int, int)));

	m_pluginsButton = new ViewButton(":icons/icons/plugins.png", this);
	m_buttonAreaLeft->addButton(m_pluginsButton);
	connect(m_pluginsButton, SIGNAL(clicked(int, int, int, int)), this, SLOT(ui_pluginsListView(int, int, int, int)));

	m_camerasButton = new ViewButton(":icons/icons/cameras.png", this);
	m_buttonAreaLeft->addButton(m_camerasButton);
	connect(m_camerasButton, SIGNAL(clicked(int, int, int, int)), this, SLOT(ui_camerasListView(int, int, int, int)));

367
	// FRAME DRAWER
Sylvain Thery's avatar
Sylvain Thery committed
368

369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
	m_frameDrawer = new Utils::Drawer();
	glm::mat4 mm(1.0);
	glm::mat4 pm(1.0);
	m_frameDrawer->getShader()->updateMatrices(mm, pm);

	m_frameDrawer->newList(GL_COMPILE);
	m_frameDrawer->color3f(0.0f,1.0f,0.0f);
	m_frameDrawer->lineWidth(4.0f);
	m_frameDrawer->begin(GL_LINE_LOOP);
	m_frameDrawer->vertex3f(-1.0f,-1.0f, 0.0f);
	m_frameDrawer->vertex3f( 1.0f,-1.0f, 0.0f);
	m_frameDrawer->vertex3f( 1.0f, 1.0f, 0.0f);
	m_frameDrawer->vertex3f(-1.0f, 1.0f, 0.0f);
	m_frameDrawer->end();
	m_frameDrawer->endList();

385 386
	// WALLPAPER

387
	m_textureWallpaper = new Utils::Texture<2, Geom::Vec3uc>(GL_UNSIGNED_BYTE);
388
	m_textureWallpaper->create(Geom::Vec2ui(16, 16));
389 390 391
	const float max = m_textureWallpaper->size()[0];
	for (unsigned int ki = 0; ki < m_textureWallpaper->size()[0]; ++ki)
	{
392
		for (unsigned int kj = 0; kj < m_textureWallpaper->size()[1]; ++kj)
393
		{
394 395
			float col = 255 - (255 * ((kj/max)*0.6 + 0.2));
			(*m_textureWallpaper)(ki, kj) = Geom::Vec3f(col, col, col);
396 397 398 399 400 401 402 403 404
		}
	}
	m_textureWallpaper->update();
	m_textureWallpaper->setWrapping(GL_CLAMP_TO_EDGE);

	m_shaderWallpaper = new Utils::ShaderWallPaper();
	m_shaderWallpaper->setTextureUnit(GL_TEXTURE0);
	m_shaderWallpaper->setTexture(m_textureWallpaper);

Pierre Kraemer's avatar
Pierre Kraemer committed
405 406 407 408 409
	this->setBackgroundColor(QColor(0,0,0));
}

void View::preDraw()
{
410
	if (Utils::GLSLShader::CURRENT_OGL_VERSION >= 3)
411
		makeCurrent();
Pierre Kraemer's avatar
Pierre Kraemer committed
412 413 414 415 416 417 418
	m_currentCamera->setScreenWidthAndHeight(width(), height());

	QGLViewer::preDraw();
}

void View::draw()
{
419
	if (Utils::GLSLShader::CURRENT_OGL_VERSION >= 3)
420 421 422 423 424 425
	{
		glEnable(GL_DEPTH_TEST);
		glDepthFunc(GL_LESS);
		glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
	}

426 427
	m_shaderWallpaper->draw();

Sylvain Thery's avatar
Sylvain Thery committed
428 429
	const CameraSet& cams = m_schnapps->getCameraSet();
	QList<Camera*> lc = cams.values();
430
	foreach (Camera* camera, lc)
Pierre Kraemer's avatar
Pierre Kraemer committed
431
	{
432
		if (camera != m_currentCamera)
Pierre Kraemer's avatar
Pierre Kraemer committed
433
		{
434 435
			if (camera->getDraw()) camera->draw();
			if (camera->getDrawPath()) camera->drawAllPaths();
Pierre Kraemer's avatar
Pierre Kraemer committed
436 437 438
		}
	}

439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
	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
463 464

	foreach(PluginInteraction* plugin, l_plugins)
465
		plugin->draw(this);
Pierre Kraemer's avatar
Pierre Kraemer committed
466 467 468 469 470 471
}

void View::postDraw()
{
	if(isSelectedView())
		drawFrame();
472
	drawButtons();
Pierre Kraemer's avatar
Pierre Kraemer committed
473 474 475 476 477 478
	QGLViewer::postDraw();
}

void View::resizeGL(int width, int height)
{
	QGLViewer::resizeGL(width, height);
479

Pierre Kraemer's avatar
Pierre Kraemer committed
480
	if(m_buttonArea)
481
		m_buttonArea->setTopRightPosition(width/ this->pixelRatio(), 0);
Sylvain Thery's avatar
Sylvain Thery committed
482 483 484

	if(m_buttonAreaLeft)
		m_buttonAreaLeft->setTopLeftPosition(0, 0);
Pierre Kraemer's avatar
Pierre Kraemer committed
485 486 487 488 489
}

void View::drawButtons()
{
	m_buttonArea->draw();
Sylvain Thery's avatar
Sylvain Thery committed
490
	m_buttonAreaLeft->draw();
Pierre Kraemer's avatar
Pierre Kraemer committed
491 492 493 494
}

void View::drawFrame()
{
495 496 497
	glDisable(GL_DEPTH_TEST);
	m_frameDrawer->callList();
	glEnable(GL_DEPTH_TEST);
Pierre Kraemer's avatar
Pierre Kraemer committed
498 499 500 501
}

void View::keyPressEvent(QKeyEvent* event)
{
502 503 504
	if (event->key() == Qt::Key_S)
	{
		b_saveSnapshots = !b_saveSnapshots;
Sylvain Thery's avatar
Sylvain Thery committed
505

506
		if (b_saveSnapshots)
Sylvain Thery's avatar
Sylvain Thery committed
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
		{
			QMessageBox msgBox;
			msgBox.setText("Snapshot every frame?");
			msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
			msgBox.setDefaultButton(QMessageBox::Ok);
			if (msgBox.exec() == QMessageBox::Ok)
			{
				m_schnapps->statusBarMessage("frame snapshot !!", 2000);
				connect(this, SIGNAL(drawFinished(bool)), this, SLOT(saveSnapshot(bool)));
			}
			else
			{
				m_schnapps->statusBarMessage("cancel frame snapshot", 2000);
				b_saveSnapshots = false;
			}
		}
523
		else
Sylvain Thery's avatar
Sylvain Thery committed
524
		{
525
			disconnect(this, SIGNAL(drawFinished(bool)), this, SLOT(saveSnapshot(bool)));
Sylvain Thery's avatar
Sylvain Thery committed
526 527 528
			m_schnapps->statusBarMessage("Stop frame snapshot", 2000);
		}
			
529 530 531 532 533
	}
	else
	{
		foreach(PluginInteraction* plugin, l_plugins)
			plugin->keyPress(this, event);
Sylvain Thery's avatar
Sylvain Thery committed
534 535 536 537 538 539 540 541 542 543 544 545

		if (event->key() == Qt::Key_Escape)
		{
			QMessageBox msgBox;
			msgBox.setText("Really quit SCHNApps ?");
			msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
			msgBox.setDefaultButton(QMessageBox::Ok);
			if (msgBox.exec() == QMessageBox::Ok)
				exit(0);
		}
		else
			QGLViewer::keyPressEvent(event);
546
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
547 548 549 550 551 552 553 554 555 556 557
}

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

void View::mousePressEvent(QMouseEvent* event)
{
Sylvain Thery's avatar
Sylvain Thery committed
558 559 560 561
	if (!isSelectedView())
		m_schnapps->setSelectedView(this);

	if (m_buttonAreaLeft->isClicked(event->x(), event->y()))
Sylvain Thery's avatar
Sylvain Thery committed
562
		m_buttonAreaLeft->clickButton(event->x(), event->y(), event->globalX(), event->globalY());
Pierre Kraemer's avatar
Pierre Kraemer committed
563 564
	else
	{
Sylvain Thery's avatar
Sylvain Thery committed
565 566 567 568 569 570 571 572 573 574
		hideDialogs();
		if (m_buttonArea->isClicked(event->x(), event->y()))
			m_buttonArea->clickButton(event->x(), event->y(), event->globalX(), event->globalY());
		else
		{
			foreach(PluginInteraction* plugin, l_plugins)
				plugin->mousePress(this, event);

			QGLViewer::mousePressEvent(event);
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
	}
}

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;
}

638 639 640 641 642 643 644 645 646 647 648




void View::closeDialogs()
{
	m_dialogMaps->close();
	m_dialogPlugins->close();
	m_dialogCameras->close();
}

Sylvain Thery's avatar
Sylvain Thery committed
649 650 651 652 653 654 655 656 657 658 659
void View::hideDialogs()
{
	if (m_dialogCameras->isVisible())
		m_dialogCameras->hide();
	if (m_dialogMaps->isVisible())
		m_dialogMaps->hide();
	if (m_dialogPlugins->isVisible())
		m_dialogPlugins->hide();
}


660 661
void View::selectedMapChanged(MapHandlerGen* prev, MapHandlerGen* cur)
{
662
	DEBUG_SLOT();
663 664 665 666 667
	if(cur && isLinkedToMap(cur))
		setManipulatedFrame(cur->getFrame());
	updateGL();
}

Sylvain Thery's avatar
Sylvain Thery committed
668
void View::ui_verticalSplitView(int x, int y, int /*globalX*/, int /*globalY*/)
Pierre Kraemer's avatar
Pierre Kraemer committed
669 670 671 672
{
	m_schnapps->splitView(m_name, Qt::Horizontal);
}

Sylvain Thery's avatar
Sylvain Thery committed
673
void View::ui_horizontalSplitView(int x, int y, int /*globalX*/, int /*globalY*/)
Pierre Kraemer's avatar
Pierre Kraemer committed
674 675 676 677
{
	m_schnapps->splitView(m_name, Qt::Vertical);
}

Sylvain Thery's avatar
Sylvain Thery committed
678
void View::ui_closeView(int x, int y, int /*globalX*/, int /*globalY*/)
Pierre Kraemer's avatar
Pierre Kraemer committed
679 680 681 682
{
	m_schnapps->removeView(m_name);
}

Sylvain Thery's avatar
Sylvain Thery committed
683 684 685 686 687 688 689 690 691
void View::ui_mapsListView(int x, int y, int globalX, int globalY)
{
	if (m_dialogMaps->isHidden())
	{
		m_dialogMaps->show();
		m_dialogMaps->move(QPoint(globalX,globalY+8));
		m_dialogCameras->hide();
		m_dialogPlugins->hide();
	}
Sylvain Thery's avatar
Sylvain Thery committed
692 693 694 695 696
	else
	{
		m_dialogMaps->hide();
	}

Sylvain Thery's avatar
Sylvain Thery committed
697 698 699 700 701 702 703 704 705 706 707
}

void View::ui_pluginsListView(int x, int y, int globalX, int globalY)
{
	if (m_dialogPlugins->isHidden())
	{
		m_dialogPlugins->show();
		m_dialogPlugins->move(QPoint(globalX,globalY+8));
		m_dialogMaps->hide();
		m_dialogCameras->hide();
	}
Sylvain Thery's avatar
Sylvain Thery committed
708 709 710 711
	else
	{
		m_dialogPlugins->hide();
	}
Sylvain Thery's avatar
Sylvain Thery committed
712 713 714 715 716 717 718 719 720 721 722
}

void View::ui_camerasListView(int x, int y, int globalX, int globalY)
{
	if (m_dialogCameras->isHidden())
	{
		m_dialogCameras->show();
		m_dialogCameras->move(QPoint(globalX,globalY+8));
		m_dialogPlugins->hide();
		m_dialogMaps->hide();
	}
Sylvain Thery's avatar
Sylvain Thery committed
723 724 725 726
	else
	{
		m_dialogCameras->hide();
	}
Sylvain Thery's avatar
Sylvain Thery committed
727 728
}

729
void View::mapAdded(MapHandlerGen* mh)
Sylvain Thery's avatar
Sylvain Thery committed
730 731 732 733 734 735
{
	DEBUG_SLOT();
	if (mh)
		m_dialogMaps->addItem(mh->getName());
}

736
void View::mapRemoved(MapHandlerGen* mh)
Sylvain Thery's avatar
Sylvain Thery committed
737 738 739 740
{
	DEBUG_SLOT();
	if (mh)
		m_dialogMaps->removeItem(mh->getName());
741 742 743 744 745 746 747 748 749 750 751
}

void View::mapCheckStateChanged(QListWidgetItem* item)
{
	if (!b_updatingUI)
	{
		DEBUG_SLOT();
		if (item->checkState() == Qt::Checked)
			linkMap(item->text());
		else
			unlinkMap(item->text());
Sylvain Thery's avatar
Sylvain Thery committed
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
	}
}

void View::pluginEnabled(Plugin *plugin)
{
	DEBUG_SLOT();
	if (dynamic_cast<PluginInteraction*>(plugin))
		m_dialogPlugins->addItem(plugin->getName());
}

void View::pluginDisabled(Plugin *plugin)
{
	DEBUG_SLOT();
	if (dynamic_cast<PluginInteraction*>(plugin))
		m_dialogPlugins->removeItem(plugin->getName());
}

769 770 771 772 773 774 775 776 777 778 779
void View::pluginCheckStateChanged(QListWidgetItem* item)
{
	if (!b_updatingUI)
	{
		DEBUG_SLOT();
		if (item->checkState() == Qt::Checked)
			linkPlugin(item->text());
		else
			unlinkPlugin(item->text());
	}
}
Sylvain Thery's avatar
Sylvain Thery committed
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794

void View::cameraAdded(Camera* camera)
{
	DEBUG_SLOT();
	if (camera)
		m_dialogCameras->addItem(camera->getName());
}

void View::cameraRemoved(Camera* camera)
{
	DEBUG_SLOT();
	if (camera)
		m_dialogCameras->removeItem(camera->getName());
}

795
void View::cameraCheckStateChanged(QListWidgetItem* item)
Sylvain Thery's avatar
Sylvain Thery committed
796
{
797 798 799 800 801 802
	if (!b_updatingUI)
	{
		DEBUG_SLOT();
		if (item->checkState() == Qt::Checked)
			setCurrentCamera(item->text());
	}
Sylvain Thery's avatar
Sylvain Thery committed
803 804
}

805
void View::updateBoundingBox()
Sylvain Thery's avatar
Sylvain Thery committed
806
{
807 808
	if (!l_maps.empty())
	{
809
		bool initialized = false;
Sylvain Thery's avatar
Sylvain Thery committed
810

811 812 813 814
		foreach (MapHandlerGen* mhg, l_maps)
		{
			qglviewer::Vec minbb;
			qglviewer::Vec maxbb;
815
			if (mhg->transformedBB(minbb, maxbb))
816
			{
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
				if (initialized)
				{
					for (unsigned int dim = 0; dim < 3; ++dim)
					{
						if (minbb[dim] < m_bbMin[dim])
							m_bbMin[dim] = minbb[dim];
						if (maxbb[dim] > m_bbMax[dim])
							m_bbMax[dim] = maxbb[dim];
					}
				}
				else
				{
					for (unsigned int dim = 0; dim < 3; ++dim)
					{
						m_bbMin[dim] = minbb[dim];
						m_bbMax[dim] = maxbb[dim];
					}
					initialized = true;
				}
836 837
			}
		}
838 839 840 841 842 843

		if (!initialized)
		{
			m_bbMin.setValue(0, 0, 0);
			m_bbMax.setValue(0, 0, 0);
		}
844
	}
Sylvain Thery's avatar
Sylvain Thery committed
845
	else
846 847 848 849
	{
		m_bbMin.setValue(0, 0, 0);
		m_bbMax.setValue(0, 0, 0);
	}
Sylvain Thery's avatar
Sylvain Thery committed
850

851 852
	DEBUG_EMIT("View::boundingBoxChanged");
	emit(boundingBoxChanged());
Sylvain Thery's avatar
Sylvain Thery committed
853 854
}

Pierre Kraemer's avatar
Pierre Kraemer committed
855 856 857
} // namespace SCHNApps

} // namespace CGoGN