view.cpp 9.74 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 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#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),
30
	m_lastSelectedMap(NULL),
Pierre Kraemer's avatar
Pierre Kraemer committed
31 32 33 34 35 36 37 38 39
	m_buttonArea(NULL),
	m_closeButton(NULL),
	m_VsplitButton(NULL),
	m_HsplitButton(NULL)
{
	++viewCount;

	m_currentCamera = m_schnapps->addCamera();

40
	connect(m_schnapps, SIGNAL(selectedMapChanged(MapHandlerGen*,MapHandlerGen*)), this, SLOT(selectedMapChanged(MapHandlerGen*,MapHandlerGen*)));
Pierre Kraemer's avatar
Pierre Kraemer committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
}

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)
{
61
	DEBUG_SLOT();
Pierre Kraemer's avatar
Pierre Kraemer committed
62 63 64 65 66 67 68 69 70 71
	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);

72
		DEBUG_EMIT("currentCameraChanged");
Pierre Kraemer's avatar
Pierre Kraemer committed
73 74
		emit(currentCameraChanged(prev, c));

75
		updateCurrentCameraBB();
Pierre Kraemer's avatar
Pierre Kraemer committed
76 77 78 79 80
		updateGL();
	}
}

void View::setCurrentCamera(const QString& name)
81
{	
Pierre Kraemer's avatar
Pierre Kraemer committed
82 83 84 85 86 87 88 89 90 91 92 93
	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)
{
94
	DEBUG_SLOT();
Pierre Kraemer's avatar
Pierre Kraemer committed
95 96 97 98
	if(plugin && !l_plugins.contains(plugin))
	{
		l_plugins.push_back(plugin);
		plugin->linkView(this);
99
		DEBUG_EMIT("pluginLinked");
Pierre Kraemer's avatar
Pierre Kraemer committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113
		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)
{
114
	DEBUG_SLOT();
Pierre Kraemer's avatar
Pierre Kraemer committed
115 116 117
	if(l_plugins.removeOne(plugin))
	{
		plugin->unlinkView(this);
118
		DEBUG_EMIT("pluginUnlinked");
Pierre Kraemer's avatar
Pierre Kraemer committed
119
		emit(pluginUnlinked(plugin));
120
		updateGL();
Pierre Kraemer's avatar
Pierre Kraemer committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
	}
}

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)
{
139
	DEBUG_SLOT();
Pierre Kraemer's avatar
Pierre Kraemer committed
140 141 142 143
	if(map && !l_maps.contains(map))
	{
		l_maps.push_back(map);
		map->linkView(this);
144
		DEBUG_EMIT("mapLinked");
Pierre Kraemer's avatar
Pierre Kraemer committed
145
		emit(mapLinked(map));
146

147
		updateCurrentCameraBB();
Pierre Kraemer's avatar
Pierre Kraemer committed
148
		updateGL();
149

Pierre Kraemer's avatar
Pierre Kraemer committed
150
		connect(map->getFrame(), SIGNAL(modified()), this, SLOT(updateGL()));
151
		connect(map, SIGNAL(selectedCellsChanged(CellSelectorGen*)), this, SLOT(updateGL()));
152

153
		if(map->isSelectedMap())
154
			setManipulatedFrame(map->getFrame());
Pierre Kraemer's avatar
Pierre Kraemer committed
155 156 157 158 159 160 161 162 163 164 165 166
	}
}

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

void View::unlinkMap(MapHandlerGen* map)
{
167
	DEBUG_SLOT();
Pierre Kraemer's avatar
Pierre Kraemer committed
168 169 170
	if(l_maps.removeOne(map))
	{
		map->unlinkView(this);
171
		DEBUG_EMIT("mapUnlinked");
Pierre Kraemer's avatar
Pierre Kraemer committed
172
		emit(mapUnlinked(map));
173

174
		updateCurrentCameraBB();
175
		updateGL();
176

Pierre Kraemer's avatar
Pierre Kraemer committed
177
		disconnect(map->getFrame(), SIGNAL(modified()), this, SLOT(updateGL()));
Pierre Kraemer's avatar
Pierre Kraemer committed
178
		disconnect(map, SIGNAL(selectedCellsChanged(CellSelectorGen*)), this, SLOT(updateGL()));
179 180 181

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

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

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
	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
264 265

	foreach(PluginInteraction* plugin, l_plugins)
266
		plugin->draw(this);
Pierre Kraemer's avatar
Pierre Kraemer committed
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 387 388 389 390 391 392 393 394 395 396 397
}

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

398
void View::updateCurrentCameraBB()
Pierre Kraemer's avatar
Pierre Kraemer committed
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
{
	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();
}

424 425
void View::selectedMapChanged(MapHandlerGen* prev, MapHandlerGen* cur)
{
426
	DEBUG_SLOT();
427 428 429 430 431
	if(cur && isLinkedToMap(cur))
		setManipulatedFrame(cur->getFrame());
	updateGL();
}

Pierre Kraemer's avatar
Pierre Kraemer committed
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
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);
}

447 448
//void View::setLastSelectedMap(MapHandlerGen* m)
//{m_lastSelectedMap=m;}
449

Pierre Kraemer's avatar
Pierre Kraemer committed
450 451 452
} // namespace SCHNApps

} // namespace CGoGN