view.cpp 22.2 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>
17
#include "Utils/Qt/qtInputs.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
18 19 20 21 22 23 24 25

namespace CGoGN
{

namespace SCHNApps
{

unsigned int View::viewCount = 0;
Sylvain Thery's avatar
Sylvain Thery committed
26 27 28

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

Sylvain Thery's avatar
Sylvain Thery committed
53 54 55
	setSnapshotFormat("BMP");
	setSnapshotFileName(m_name);
	setSnapshotQuality(100);
56

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

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

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
	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);

84 85
	m_currentCamera = m_schnapps->addCamera();
	m_currentCamera->linkView(this);
86
	m_dialogCameras->check(m_currentCamera->getName(),Qt::Checked);
Sylvain Thery's avatar
Sylvain Thery committed
87 88

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

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

	delete m_dialogMaps;
	delete m_dialogPlugins;
	delete m_dialogCameras;
}

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

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

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

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

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

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

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

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

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

Sylvain Thery's avatar
Sylvain Thery committed
172 173


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

181
	DEBUG_SLOT();
Pierre Kraemer's avatar
Pierre Kraemer committed
182 183 184 185
	if(plugin && !l_plugins.contains(plugin))
	{
		l_plugins.push_back(plugin);
		plugin->linkView(this);
Sylvain Thery's avatar
Sylvain Thery committed
186

187
		DEBUG_EMIT("pluginLinked");
Pierre Kraemer's avatar
Pierre Kraemer committed
188
		emit(pluginLinked(plugin));
189 190

		b_updatingUI = true;
Sylvain Thery's avatar
Sylvain Thery committed
191
		m_dialogPlugins->check(plugin->getName(), Qt::Checked);
192 193
		b_updatingUI = false;

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


Pierre Kraemer's avatar
Pierre Kraemer committed
216
		plugin->unlinkView(this);
Sylvain Thery's avatar
Sylvain Thery committed
217

218
		DEBUG_EMIT("pluginUnlinked");
Pierre Kraemer's avatar
Pierre Kraemer committed
219
		emit(pluginUnlinked(plugin));
220 221

		b_updatingUI = true;
Sylvain Thery's avatar
Sylvain Thery committed
222
		m_dialogPlugins->check(plugin->getName(), Qt::Unchecked);
223 224
		b_updatingUI = false;

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

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
244 245 246 247 248
	// RECORDING
	QTextStream* rec = m_schnapps->pythonStreamRecorder();
	if (rec)
		*rec << this->getName() << ".linkMap(\"" << map->getName() << "\");" << endl;

249
	DEBUG_SLOT();
Pierre Kraemer's avatar
Pierre Kraemer committed
250 251 252 253
	if(map && !l_maps.contains(map))
	{
		l_maps.push_back(map);
		map->linkView(this);
Sylvain Thery's avatar
Sylvain Thery committed
254

255
		DEBUG_EMIT("mapLinked");
Pierre Kraemer's avatar
Pierre Kraemer committed
256
		emit(mapLinked(map));
257

258
		connect(map, SIGNAL(selectedCellsChanged(CellSelectorGen*)), this, SLOT(updateGL()));
Sylvain Thery's avatar
Sylvain Thery committed
259
		connect(map, SIGNAL(boundingBoxModified()), this, SLOT(updateBoundingBox()));
260

261
		if(map->isSelectedMap())
262
			setManipulatedFrame(map->getFrame());
263

Sylvain Thery's avatar
Sylvain Thery committed
264 265
		updateBoundingBox();

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

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

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


Pierre Kraemer's avatar
Pierre Kraemer committed
289
		map->unlinkView(this);
Sylvain Thery's avatar
Sylvain Thery committed
290

291
		DEBUG_EMIT("mapUnlinked");
Pierre Kraemer's avatar
Pierre Kraemer committed
292
		emit(mapUnlinked(map));
293

294
		disconnect(map, SIGNAL(selectedCellsChanged(CellSelectorGen*)), this, SLOT(updateGL()));
Sylvain Thery's avatar
Sylvain Thery committed
295
		disconnect(map, SIGNAL(boundingBoxModified()), this, SLOT(updateBoundingBox()));
296 297 298

		if(map == m_schnapps->getSelectedMap())
			setManipulatedFrame(NULL);
299

Sylvain Thery's avatar
Sylvain Thery committed
300 301
		updateBoundingBox();

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

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

Sylvain Thery's avatar
Sylvain Thery committed
321 322 323 324




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

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

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

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

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

355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
	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)));

370
	// FRAME DRAWER
371

Sylvain Thery's avatar
Sylvain Thery committed
372
	m_frameDrawer = new Utils::Drawer(1);
373 374
	glm::mat4 mm(1.0);
	glm::mat4 pm(1.0);
Sylvain Thery's avatar
Sylvain Thery committed
375 376
//	m_frameDrawer->getShader()->updateMatrices(mm, pm);
	m_frameDrawer->updateMatrices(mm, pm);
377 378 379

	m_frameDrawer->newList(GL_COMPILE);
	m_frameDrawer->color3f(0.0f,1.0f,0.0f);
380
	m_frameDrawer->lineWidth(4.0f);
381 382 383 384 385 386 387 388
	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();

Sylvain Thery's avatar
Sylvain Thery committed
389
	// WALLPAPER
390

Sylvain Thery's avatar
Sylvain Thery committed
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
	m_textureWallpaper = new Utils::Texture<2, Geom::Vec3uc>(GL_UNSIGNED_BYTE);
	m_textureWallpaper->create(Geom::Vec2ui(16, 16));
	const float max = m_textureWallpaper->size()[0];
	for (unsigned int ki = 0; ki < m_textureWallpaper->size()[0]; ++ki)
	{
		for (unsigned int kj = 0; kj < m_textureWallpaper->size()[1]; ++kj)
		{
			float col = 255 - (255 * ((kj/max)*0.6 + 0.2));
			(*m_textureWallpaper)(ki, kj) = Geom::Vec3f(col, col, col);
		}
	}
	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);
408

Pierre Kraemer's avatar
Pierre Kraemer committed
409 410 411 412 413
	this->setBackgroundColor(QColor(0,0,0));
}

void View::preDraw()
{
Sylvain Thery's avatar
Sylvain Thery committed
414
	if (Utils::GLSLShader::CURRENT_OGL_VERSION >= 3)
415
		makeCurrent();
Pierre Kraemer's avatar
Pierre Kraemer committed
416 417 418 419 420 421 422
	m_currentCamera->setScreenWidthAndHeight(width(), height());

	QGLViewer::preDraw();
}

void View::draw()
{
Sylvain Thery's avatar
Sylvain Thery committed
423
	if (Utils::GLSLShader::CURRENT_OGL_VERSION >= 3)
424 425 426 427 428 429
	{
		glEnable(GL_DEPTH_TEST);
		glDepthFunc(GL_LESS);
		glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
	}

Sylvain Thery's avatar
Sylvain Thery committed
430 431
	m_shaderWallpaper->draw();

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

443 444 445 446 447 448 449
	glm::mat4 mm = getCurrentModelViewMatrix();
	glm::mat4 pm = getCurrentProjectionMatrix();

	MapHandlerGen* selectedMap = m_schnapps->getSelectedMap();

	foreach(MapHandlerGen* map, l_maps)
	{
450
		glm::mat4 map_mm = mm * map->getFrameMatrix() * map->getTransfoMatrix();
451 452 453

		if(map == selectedMap)
		{
Sylvain Thery's avatar
Sylvain Thery committed
454 455 456
			Utils::Drawer* bbDr = map->getBBDrawer();
			if(bbDr)
				bbDr->updateMatrices(pm, map_mm);
457 458 459 460 461 462 463 464 465 466
			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
467 468

	foreach(PluginInteraction* plugin, l_plugins)
469
		plugin->draw(this);
Pierre Kraemer's avatar
Pierre Kraemer committed
470 471 472 473 474 475
}

void View::postDraw()
{
	if(isSelectedView())
		drawFrame();
476
	drawButtons();
Pierre Kraemer's avatar
Pierre Kraemer committed
477 478 479 480 481 482
	QGLViewer::postDraw();
}

void View::resizeGL(int width, int height)
{
	QGLViewer::resizeGL(width, height);
Sylvain Thery's avatar
Sylvain Thery committed
483

Pierre Kraemer's avatar
Pierre Kraemer committed
484
	if(m_buttonArea)
Sylvain Thery's avatar
Sylvain Thery committed
485
		m_buttonArea->setTopRightPosition(width/ this->pixelRatio(), 0);
486 487 488

	if(m_buttonAreaLeft)
		m_buttonAreaLeft->setTopLeftPosition(0, 0);
Pierre Kraemer's avatar
Pierre Kraemer committed
489 490 491 492 493
}

void View::drawButtons()
{
	m_buttonArea->draw();
494
	m_buttonAreaLeft->draw();
Pierre Kraemer's avatar
Pierre Kraemer committed
495 496 497 498
}

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

504

Sylvain Thery's avatar
Sylvain Thery committed
505
void View::keyPressEvent(QKeyEvent* event)
506
{
Sylvain Thery's avatar
Sylvain Thery committed
507 508 509
	quint64 k = event->modifiers();
	k <<= 32;
	k |= event->key();
510

Sylvain Thery's avatar
Sylvain Thery committed
511 512 513
	// exec python shortcuts if exist and stop propagation of event !
	if (m_schnapps->execPythonShortcut(k))
		return;
514 515

	switch (event->key())
Sylvain Thery's avatar
Sylvain Thery committed
516
	{
517
		case Qt::Key_S:
Sylvain Thery's avatar
Sylvain Thery committed
518
		{
519 520 521
			b_saveSnapshots = !b_saveSnapshots;

			if (b_saveSnapshots)
Sylvain Thery's avatar
Sylvain Thery committed
522
			{
523 524 525 526 527 528 529 530 531 532 533 534 535 536
				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;
				}
Sylvain Thery's avatar
Sylvain Thery committed
537 538 539
			}
			else
			{
540 541
				disconnect(this, SIGNAL(drawFinished(bool)), this, SLOT(saveSnapshot(bool)));
				m_schnapps->statusBarMessage("Stop frame snapshot", 2000);
Sylvain Thery's avatar
Sylvain Thery committed
542 543 544
			}

		}
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
		break;

		default:
			foreach(PluginInteraction* plugin, l_plugins)
				plugin->keyPress(this, event);

			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)
					m_schnapps->close();
			}
			else
				QGLViewer::keyPressEvent(event);
Sylvain Thery's avatar
Sylvain Thery committed
562
	}
563 564 565 566 567 568 569 570 571 572 573 574 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

	//if (event->key() == Qt::Key_X)
	//{
	//	int isX = scaleIntSlideVal(m_scaleView[0][0]);
	//	int isY = scaleIntSlideVal(m_scaleView[1][1]);
	//	int isZ = scaleIntSlideVal(m_scaleView[2][2]);

	//	Utils::QT::inputValues(
	//		Utils::QT::VarSlider(0, 100, isX, "Scale X",
	//		Utils::QT::VarSlider(0, 100, isY, "Scale Y",
	//		Utils::QT::VarSlider(0, 100, isZ, "Scale Z"))));

	//	float sx = scaleRealSlideVal(isX);
	//	float sy = scaleRealSlideVal(isY);
	//	float sz = scaleRealSlideVal(isZ);

	//	setViewScaling(sx,sy,sz);

	//	QString msg = QString("Sx=") + QString::number(m_scaleView[0][0]) + QString(" / Sy=") + QString::number(m_scaleView[1][1]) + QString(" / Sz=") + QString::number(m_scaleView[2][2]);
	//	m_schnapps->statusBar()->showMessage(msg, 2000);

	//	QTextStream* rec = m_schnapps->pythonStreamRecorder();
	//	if (rec)
	//		*rec << this->getName() << ".setViewScaling(" << sx << ", "<< sy << ", "<< sz << ");" << endl;
	//}
	//else
	//if (event->key() == Qt::Key_S)
	//{
	//	b_saveSnapshots = !b_saveSnapshots;

	//	if (b_saveSnapshots)
	//	{
	//		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;
	//		}
	//	}
	//	else
	//	{
	//		disconnect(this, SIGNAL(drawFinished(bool)), this, SLOT(saveSnapshot(bool)));
	//		m_schnapps->statusBarMessage("Stop frame snapshot", 2000);
	//	}
	//		
	//}
	//else
	//{
	//	foreach(PluginInteraction* plugin, l_plugins)
	//		plugin->keyPress(this, event);

	//	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)
	//			m_schnapps->close();
	//	}
	//	else
	//		QGLViewer::keyPressEvent(event);
	//}
Pierre Kraemer's avatar
Pierre Kraemer committed
634 635 636 637 638 639 640 641 642 643 644
}

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
645
	if (!isSelectedView())
646
	{
Sylvain Thery's avatar
Sylvain Thery committed
647
		m_schnapps->setSelectedView(this);
648 649 650 651
		m_schnapps->statusBarMessage(QString("Selecting ") + this->getName(), 2000);
	}
	else if (event->y() < 20)
		m_schnapps->statusBarMessage(this->getName(), 2000);
Sylvain Thery's avatar
Sylvain Thery committed
652 653

	if (m_buttonAreaLeft->isClicked(event->x(), event->y()))
654
		m_buttonAreaLeft->clickButton(event->x(), event->y(), event->globalX(), event->globalY());
Pierre Kraemer's avatar
Pierre Kraemer committed
655 656
	else
	{
Sylvain Thery's avatar
Sylvain Thery committed
657 658 659 660 661 662 663 664 665 666
		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
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
	}
}

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

730

Sylvain Thery's avatar
Sylvain Thery committed
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749

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

void View::hideDialogs()
{
	if (m_dialogCameras->isVisible())
		m_dialogCameras->hide();
	if (m_dialogMaps->isVisible())
		m_dialogMaps->hide();
	if (m_dialogPlugins->isVisible())
		m_dialogPlugins->hide();
}


750 751
void View::selectedMapChanged(MapHandlerGen* prev, MapHandlerGen* cur)
{
752
	DEBUG_SLOT();
753 754 755 756 757
	if(cur && isLinkedToMap(cur))
		setManipulatedFrame(cur->getFrame());
	updateGL();
}

Sylvain Thery's avatar
Sylvain Thery committed
758
void View::ui_verticalSplitView(int x, int y, int /*globalX*/, int /*globalY*/)
Pierre Kraemer's avatar
Pierre Kraemer committed
759 760 761 762
{
	m_schnapps->splitView(m_name, Qt::Horizontal);
}

Sylvain Thery's avatar
Sylvain Thery committed
763
void View::ui_horizontalSplitView(int x, int y, int /*globalX*/, int /*globalY*/)
Pierre Kraemer's avatar
Pierre Kraemer committed
764 765 766 767
{
	m_schnapps->splitView(m_name, Qt::Vertical);
}

Sylvain Thery's avatar
Sylvain Thery committed
768
void View::ui_closeView(int x, int y, int /*globalX*/, int /*globalY*/)
Pierre Kraemer's avatar
Pierre Kraemer committed
769 770 771 772
{
	m_schnapps->removeView(m_name);
}

773 774 775 776 777 778 779 780 781
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
782
	else
Sylvain Thery's avatar
Sylvain Thery committed
783
	{
Sylvain Thery's avatar
Sylvain Thery committed
784
		m_dialogMaps->hide();
Sylvain Thery's avatar
Sylvain Thery committed
785
	}
Sylvain Thery's avatar
Sylvain Thery committed
786

787 788 789 790 791 792 793 794 795 796 797
}

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
798
	else
Sylvain Thery's avatar
Sylvain Thery committed
799
	{
Sylvain Thery's avatar
Sylvain Thery committed
800
		m_dialogPlugins->hide();
Sylvain Thery's avatar
Sylvain Thery committed
801
	}
802 803 804 805 806 807 808 809 810 811 812
}

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
813
	else
Sylvain Thery's avatar
Sylvain Thery committed
814
	{
Sylvain Thery's avatar
Sylvain Thery committed
815
		m_dialogCameras->hide();
Sylvain Thery's avatar
Sylvain Thery committed
816
	}
817 818
}

Sylvain Thery's avatar
Sylvain Thery committed
819
void View::mapAdded(MapHandlerGen* mh)
820 821 822 823 824 825
{
	DEBUG_SLOT();
	if (mh)
		m_dialogMaps->addItem(mh->getName());
}

Sylvain Thery's avatar
Sylvain Thery committed
826
void View::mapRemoved(MapHandlerGen* mh)
827 828 829
{
	DEBUG_SLOT();
	if (mh)
830
		m_dialogMaps->removeItem(mh->getName());
831 832
}

Sylvain Thery's avatar
Sylvain Thery committed
833 834 835 836 837 838 839 840 841 842 843
void View::mapCheckStateChanged(QListWidgetItem* item)
{
	if (!b_updatingUI)
	{
		DEBUG_SLOT();
		if (item->checkState() == Qt::Checked)
			linkMap(item->text());
		else
			unlinkMap(item->text());
	}
}
844

845 846 847 848 849 850 851 852 853 854 855
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))
856
		m_dialogPlugins->removeItem(plugin->getName());
857
}
858

Sylvain Thery's avatar
Sylvain Thery committed
859 860 861 862 863 864 865 866 867 868 869
void View::pluginCheckStateChanged(QListWidgetItem* item)
{
	if (!b_updatingUI)
	{
		DEBUG_SLOT();
		if (item->checkState() == Qt::Checked)
			linkPlugin(item->text());
		else
			unlinkPlugin(item->text());
	}
}
870

871 872 873 874 875 876 877 878 879 880 881 882 883 884
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());
}

Sylvain Thery's avatar
Sylvain Thery committed
885
void View::cameraCheckStateChanged(QListWidgetItem* item)
886
{
Sylvain Thery's avatar
Sylvain Thery committed
887 888 889 890 891 892
	if (!b_updatingUI)
	{
		DEBUG_SLOT();
		if (item->checkState() == Qt::Checked)
			setCurrentCamera(item->text());
	}
893 894
}

Sylvain Thery's avatar
Sylvain Thery committed
895
void View::updateBoundingBox()
896
{
Sylvain Thery's avatar
Sylvain Thery committed
897 898 899
	if (!l_maps.empty())
	{
		bool initialized = false;
900

Sylvain Thery's avatar
Sylvain Thery committed
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
		foreach (MapHandlerGen* mhg, l_maps)
		{
			qglviewer::Vec minbb;
			qglviewer::Vec maxbb;
			if (mhg->transformedBB(minbb, maxbb))
			{
				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;
				}
			}
		}
928

Sylvain Thery's avatar
Sylvain Thery committed
929 930 931 932 933 934 935 936 937 938 939
		if (!initialized)
		{
			m_bbMin.setValue(0, 0, 0);
			m_bbMax.setValue(0, 0, 0);
		}
	}
	else
	{
		m_bbMin.setValue(0, 0, 0);
		m_bbMax.setValue(0, 0, 0);
	}
940

Sylvain Thery's avatar
Sylvain Thery committed
941 942
	DEBUG_EMIT("View::boundingBoxChanged");
	emit(boundingBoxChanged());
943 944
}

Pierre Kraemer's avatar
Pierre Kraemer committed
945 946 947
} // namespace SCHNApps

} // namespace CGoGN