window.cpp 15.5 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1
#include "window.h"
2

Pierre Kraemer's avatar
Pierre Kraemer committed
3 4
#include <QVBoxLayout>
#include <QSplitter>
5 6 7 8
#include <QMessageBox>
#include <QDockWidget>
#include <QPluginLoader>
#include <QFileInfo>
9 10 11
#include <QKeyEvent>
#include <QMouseEvent>
#include <QWheelEvent>
12

Pierre Kraemer's avatar
Pierre Kraemer committed
13 14
#include "plugin.h"
#include "view.h"
15
#include "texture.h"
16

Pierre Kraemer's avatar
Pierre Kraemer committed
17 18 19 20 21 22 23 24 25
#include "dialogs/camerasDialog.h"
#include "dialogs/pluginsDialog.h"
#include "dialogs/mapsDialog.h"

namespace CGoGN
{

namespace SCHNApps
{
Pierre Kraemer's avatar
Pierre Kraemer committed
26

27
Window::Window(const QString& appPath, QWidget *parent) :
Pierre Kraemer's avatar
Pierre Kraemer committed
28
	QMainWindow(parent),
29
	m_appPath(appPath),
30
	m_firstView(NULL),
31
	m_currentView(NULL)
32
{
Pierre Kraemer's avatar
Pierre Kraemer committed
33
	// program in its initialization phase
34 35
	m_initialization = true;

Pierre Kraemer's avatar
Pierre Kraemer committed
36 37 38
	m_camerasDialog = new CamerasDialog(this);
	m_pluginsDialog = new PluginsDialog(this);
	m_mapsDialog = new MapsDialog(this);
39

40
	this->setupUi(this);
41

42 43 44 45
	m_dock = new QDockWidget(tr("Control"), this);
	m_dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
	m_dock->setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable | QDockWidget::DockWidgetClosable);
	addDockWidget(Qt::RightDockWidgetArea, m_dock);
Pierre Kraemer's avatar
Pierre Kraemer committed
46
	m_dock->setVisible(false);
47 48 49

	m_dockTabWidget = new QTabWidget(m_dock);
	m_dockTabWidget->setObjectName("DockTabWidget");
50
	m_dockTabWidget->setLayoutDirection(Qt::LeftToRight);
51 52 53
	m_dockTabWidget->setTabPosition(QTabWidget::East);
	m_dock->setWidget(m_dockTabWidget);

54 55
	connect(actionShowHideDock, SIGNAL(triggered()), this, SLOT(cb_showHideDock()));

Pierre Kraemer's avatar
Pierre Kraemer committed
56 57 58 59 60
	m_centralLayout = new QVBoxLayout(centralwidget);

	m_rootSplitter = new QSplitter(centralwidget);
	b_rootSplitterInitialized = false;
	m_centralLayout->addWidget(m_rootSplitter);
61

62 63
	// add first view
	m_firstView = addView();
64
	m_currentView = m_firstView;
Pierre Kraemer's avatar
Pierre Kraemer committed
65
	m_rootSplitter->addWidget(m_firstView);
66

67 68
	glewInit();

Pierre Kraemer's avatar
Pierre Kraemer committed
69
	// connect the basic actions
70 71
	connect(actionAboutSCHNApps, SIGNAL(triggered()), this, SLOT(cb_aboutSCHNApps()));
	connect(actionAboutCGoGN, SIGNAL(triggered()), this, SLOT(cb_aboutCGoGN()));
72

Pierre Kraemer's avatar
Pierre Kraemer committed
73
	connect(actionManageCameras, SIGNAL(triggered()), this, SLOT(cb_manageCameras()));
74
	connect(actionManagePlugins, SIGNAL(triggered()), this, SLOT(cb_managePlugins()));
75
	connect(actionManageMaps, SIGNAL(triggered()), this, SLOT(cb_manageMaps()));
76

77
//	System::StateHandler::loadState(this, &h_plugin, &h_scene, m_splitArea);
78

Pierre Kraemer's avatar
Pierre Kraemer committed
79
	// program in its initialization phase
80 81 82 83 84
	m_initialization = false;
}

Window::~Window()
{
85
//	System::StateHandler::saveState(this, &h_plugins, &h_scenes, m_splitArea);
86 87
}

Pierre Kraemer's avatar
Pierre Kraemer committed
88 89 90 91
/*********************************************************
 * MANAGE DOCK
 *********************************************************/

92
void Window::addTabInDock(QWidget* tabWidget, const QString& tabText)
Pierre Kraemer's avatar
Pierre Kraemer committed
93
{
94
	if(tabWidget)
95 96
	{
		int idx = m_dockTabWidget->addTab(tabWidget, tabText);
Pierre Kraemer's avatar
Pierre Kraemer committed
97
		m_dock->setVisible(true);
98 99
		m_dockTabWidget->setTabEnabled(idx, false);
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
100 101 102 103
}

void Window::removeTabInDock(QWidget *tabWidget)
{
104
	if(tabWidget)
Pierre Kraemer's avatar
Pierre Kraemer committed
105 106 107
		m_dockTabWidget->removeTab(m_dockTabWidget->indexOf(tabWidget));
}

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
void Window::enablePluginTabWidgets(Plugin* plugin)
{
	const QList<QWidget*> tabWidgets = plugin->getTabWidgets();
	foreach(QWidget* w, tabWidgets)
	{
		int idx = m_dockTabWidget->indexOf(w);
		m_dockTabWidget->setTabEnabled(idx, true);
	}
}

void Window::disablePluginTabWidgets(Plugin* plugin)
{
	const QList<QWidget*> tabWidgets = plugin->getTabWidgets();
	foreach(QWidget* w, tabWidgets)
	{
		int idx = m_dockTabWidget->indexOf(w);
		m_dockTabWidget->setTabEnabled(idx, false);
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
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 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
/*********************************************************
 * MANAGE MENU ACTIONS
 *********************************************************/

bool Window::addMenuAction(const QString& menuPath, QAction* action)
{
	// if menu path = empty string: set error + failure
	if (menuPath.isEmpty())
	{
		System::Error::code = System::Error::BAD_ACTION_MENU_PATH_f(menuPath);
		return false;
	}

	if (!action)
		return false;

	// extracting all the substring separated by ';'
	QStringList stepNames = menuPath.split(";");
	stepNames.removeAll("");
	unsigned int nbStep = stepNames.count();

	// if only one substring: error + failure
	// No action directly in the menu bar
	if (nbStep < 1)
	{
		System::Error::code = System::Error::BAD_ACTION_MENU_PATH_f(menuPath);
		return false;
	}

	// for every extracted substring
	unsigned int i = 0;
	QMenu* lastMenu;
	QAction* lastAction;
	foreach(QString step, stepNames)
	{
		++i;
		bool newMenu = true;
		// if not last substring (= menu)
		if (i < nbStep)
		{
			// if first substring
			if (i == 1)
			{
				// checking if it's an existing menu or not
				QList<QAction *> actions = menubar->actions();
				foreach(QAction * action, actions)
				{
					if (action->menu()->title() == step)
					{
						lastAction = action;
						newMenu = false;
						break;
					}
				}
				// if new menu: adding it to menubar
				if (newMenu)
				{
					lastMenu = menubar->addMenu(step);
					lastMenu->setParent(menubar);
				}
				// if not: next operation in the already existing corresponding menu
				else
				{
					lastMenu = lastAction->menu();
					// setting the parent will be useful for the deleting function
					lastMenu->setParent(menubar);
				}
			}
			// if not first substring
			else
			{
				// same thing: except not operating on the menubar
				QList<QAction *> actions = lastMenu->actions();
				foreach(QAction * action, actions)
				{
					if (action->menu()->title() == step)
					{
						lastAction = action;
						newMenu = false;
						break;
					}
				}
				if (newMenu)
				{
					QMenu *tmp = lastMenu;
					lastMenu = lastMenu->addMenu(step);
					// setting the parent will be useful for the deleting function
					lastMenu->setParent(tmp);
				}
				else
				{
					QMenu *tmp = lastMenu;
					lastMenu = lastAction->menu();
					// setting the parent will be useful for the deleting function
					lastMenu->setParent(tmp);
				}
			}
		}
		// if last substring (= action name)
		else
		{
			// adding it to the corresponding menu
			lastMenu->addAction(action);
			action->setText(step);
			// setting the parent will be useful for the deleting function
			action->setParent(lastMenu);
			break;
		}
	}

	// success
	return true;
}

void Window::removeMenuAction(QAction *action)
{
	if(action)
	{
		action->setEnabled(false);
		// parent of the action
		// which is an instance of QMenu if the action was created
		// using the addMenuActionMethod()
		QObject* parent = action->parent();
		// deleting action
		delete action;

		//if parent is an instance of QMenu
		if (parent->inherits("QMenu"))
		{
			// for this menu and all of its parent that are instance of QMenu
			// that are also fit for deleting
			while (parent != NULL)
			{
				QMenu *menuParent = (QMenu*)(parent);
				parent = parent->parent();

				// if the menu doesn't have any other actions/menus
				// it is deleted
				if (menuParent->actions().empty())
					delete menuParent;
				// otherwise: end function
				else
					break;

				if (!parent->inherits("QMenu"))
					parent = NULL;
			}
		}
	}
}

/*********************************************************
 * MANAGE TOOLBAR ACTIONS
 *********************************************************/

bool Window::addToolbarAction(QAction* action)
{
	if (action)
	{
		toolBar->addAction(action);
		return true;
	}
	return false;
}

void Window::removeToolbarAction(QAction* action)
{
	if (action)
		toolBar->removeAction(action);
}

/*********************************************************
300
 * MANAGE CAMERAS
Pierre Kraemer's avatar
Pierre Kraemer committed
301 302
 *********************************************************/

303
Camera* Window::addCamera(const QString& name)
Pierre Kraemer's avatar
Pierre Kraemer committed
304
{
305
	if (h_cameras.contains(name))
306
	{
307
		System::Error::code = System::Error::CAMERA_EXISTS;
308 309 310
		return NULL;
	}

311 312
	Camera* camera = new Camera(name, this);
	h_cameras.insert(name, camera);
Pierre Kraemer's avatar
Pierre Kraemer committed
313

314
	emit(cameraAdded(camera));
Pierre Kraemer's avatar
Pierre Kraemer committed
315

316 317
	return camera;
}
Pierre Kraemer's avatar
Pierre Kraemer committed
318

319 320 321
Camera* Window::addCamera()
{
	return addCamera(QString("camera_") + QString::number(Camera::cameraCount));
Pierre Kraemer's avatar
Pierre Kraemer committed
322 323
}

324
void Window::removeCamera(const QString& name)
Pierre Kraemer's avatar
Pierre Kraemer committed
325
{
326
	if (h_cameras.contains(name))
Pierre Kraemer's avatar
Pierre Kraemer committed
327
	{
328 329
		Camera* camera = h_cameras[name];
		h_cameras.remove(name);
Pierre Kraemer's avatar
Pierre Kraemer committed
330

331
		emit(cameraRemoved(camera));
Pierre Kraemer's avatar
Pierre Kraemer committed
332

333
		delete camera;
334 335
	}
}
Pierre Kraemer's avatar
Pierre Kraemer committed
336

337
Camera* Window::getCamera(const QString& name) const
Pierre Kraemer's avatar
Pierre Kraemer committed
338
{
339 340
	if (h_cameras.contains(name))
		return h_cameras[name];
Pierre Kraemer's avatar
Pierre Kraemer committed
341 342
	else
	{
343
		System::Error::code = System::Error::CAMERA_DOES_NOT_EXIST;
Pierre Kraemer's avatar
Pierre Kraemer committed
344 345 346 347
		return NULL;
	}
}

348 349 350
/*********************************************************
 * MANAGE VIEWS
 *********************************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
351

352
View* Window::addView(const QString& name)
353
{
354
	if (h_views.contains(name))
355
	{
356 357
		System::Error::code = System::Error::VIEW_EXISTS;
		return NULL;
358 359
	}

360 361
	View* view = NULL;
	if(m_firstView == NULL)
Pierre Kraemer's avatar
Pierre Kraemer committed
362
		view = new View(name, this);
363
	else
Pierre Kraemer's avatar
Pierre Kraemer committed
364
		view = new View(name, this, m_firstView);
365
	h_views.insert(name, view);
Pierre Kraemer's avatar
Pierre Kraemer committed
366

367 368
	emit(viewAdded(view));

369
	return view;
370 371
}

372 373 374 375 376
View* Window::addView()
{
	return addView(QString("view_") + QString::number(View::viewCount));
}

377
void Window::removeView(const QString& name)
378
{
379
	if (h_views.contains(name))
380
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
381 382 383
		if(h_views.count() > 1)
		{
			View* view = h_views[name];
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
			if(view == m_firstView)
			{
				ViewHash::const_iterator i = h_views.constBegin();
				while (i != h_views.constEnd())
				{
					if(i.value() != view)
					{
						m_firstView = i.value();
						i = h_views.constEnd();
					}
					else
						++i;
				}
			}
//			if(view == m_currentView)
			setCurrentView(m_firstView);
Pierre Kraemer's avatar
Pierre Kraemer committed
400
			h_views.remove(name);
401 402 403

			emit(viewRemoved(view));

Pierre Kraemer's avatar
Pierre Kraemer committed
404 405
			delete view;
		}
406 407 408
	}
}

409
View* Window::getView(const QString& name) const
410
{
411 412
	if (h_views.contains(name))
		return h_views[name];
413 414
	else
	{
415 416
		System::Error::code = System::Error::VIEW_DOES_NOT_EXIST;
		return NULL;
417 418 419
	}
}

420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
void Window::setCurrentView(View* view)
{
	const QList<Plugin*>& oldPlugins = m_currentView->getLinkedPlugins();
	foreach(Plugin* p, oldPlugins)
		disablePluginTabWidgets(p);

	View* oldCurrent = m_currentView;
	m_currentView = view;

	const QList<Plugin*>& newPlugins = m_currentView->getLinkedPlugins();
	foreach(Plugin* p, newPlugins)
	{
		enablePluginTabWidgets(p);
		p->currentViewChanged(m_currentView);
	}

	oldCurrent->updateGL();
	m_currentView->updateGL();
}

Pierre Kraemer's avatar
Pierre Kraemer committed
440
void Window::splitView(const QString& name, Qt::Orientation orientation)
441
{
Pierre Kraemer's avatar
Pierre Kraemer committed
442
	View* newView = addView();
443

Pierre Kraemer's avatar
Pierre Kraemer committed
444
//	std::cout << "splitView" << std::endl;
445

Pierre Kraemer's avatar
Pierre Kraemer committed
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
	View* view = h_views[name];
	QSplitter* parent = (QSplitter*)(view->parentWidget());
	if(parent == m_rootSplitter && !b_rootSplitterInitialized)
	{
//		std::cout << "init root splitter" << std::endl;
		m_rootSplitter->setOrientation(orientation);
		b_rootSplitterInitialized = true;
	}
	if(parent->orientation() == orientation)
	{
//		std::cout << "same orientation" << std::endl;
		parent->insertWidget(parent->indexOf(view)+1, newView);
	}
	else
	{
//		std::cout << "new orientation" << std::endl;
		int idx = parent->indexOf(view);
		view->setParent(NULL);
		QSplitter* spl = new QSplitter(orientation);
		spl->addWidget(view);
		spl->addWidget(newView);
		parent->insertWidget(idx, spl);
468 469 470
	}
}

471
/*********************************************************
472
 * MANAGE PLUGINS
473 474
 *********************************************************/

475
Plugin* Window::loadPlugin(const QString& pluginFilePath)
476
{
477 478 479
	QString pluginName = QFileInfo(pluginFilePath).baseName().remove(0, 3);

	if (h_plugins.contains(pluginName))
480
	{
481 482
		// set message error + function fails
		System::Error::code = System::Error::PLUGIN_EXISTS;
483
		return NULL;
484 485
	}

486
	QPluginLoader loader(pluginFilePath);
Pierre Kraemer's avatar
Pierre Kraemer committed
487

488 489 490 491
	// if the loader loads a plugin instance
	if (QObject* pluginObject = loader.instance())
	{
		Plugin* plugin = qobject_cast<Plugin*>(pluginObject);
Pierre Kraemer's avatar
Pierre Kraemer committed
492

493 494 495 496 497 498 499 500 501 502 503
		// we set the plugin with correct parameters (name, filepath, window)
		plugin->setName(pluginName);
		plugin->setFilePath(pluginFilePath);
		plugin->setWindow(this);

		// then we call its enable() methods
		if (plugin->enable())
		{
			// if it succeeded we reference this plugin
			h_plugins.insert(pluginName, plugin);

504
			statusbar->showMessage(pluginName + QString(" successfully loaded."), 2000);
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
			emit(pluginAdded(plugin));

			// method success
			return plugin;
		}
		else
		{
			delete plugin;
			return NULL;
		}
	}
	// if loading fails
	else
	{
		// set error message + method failure
		System::Error::code = System::Error::ERROR_PLUGIN_LOAD_f(pluginName);
		return NULL;
	}
523 524
}

525
void Window::unloadPlugin(const QString& pluginName)
526
{
527 528 529 530 531 532 533 534
	if (h_plugins.contains(pluginName))
	{
		Plugin* plugin = h_plugins[pluginName];

		// calling its disable() method and dereferencing it
		plugin->disable();
		h_plugins.remove(pluginName);

535 536 537 538
		QPluginLoader loader(plugin->getFilePath());
		loader.unload();

		statusbar->showMessage(pluginName + QString(" successfully unloaded."), 2000);
539 540 541 542 543
		emit(pluginRemoved(plugin));

		// delete plugin
		delete plugin;
	}
544 545
}

546
Plugin* Window::getPlugin(const QString& name) const
547
{
548 549 550
	if (h_plugins.contains(name))
		return h_plugins[name];
	else
551
	{
552 553
		System::Error::code = System::Error::PLUGIN_DOES_NOT_EXIST;
		return NULL;
554 555
	}
}
556 557
/*
Plugin* Window::checkPluginDependencie(QString name, Plugin* dependantPlugin)
558
{
559 560 561 562 563 564 565 566 567 568
	// if the plugin is referenced and found
	PluginHash::iterator it;

	if ((it = h_plugin.find(name)) != h_plugin.end())
	{
		// the plugin calling for the depencie is added to the found plugin's list of dependant plugins
		(*it)->addDependantPlugin(dependantPlugin);
		return (*it);
	}
	//if not found: set error message + failure
569 570
	else
	{
571
		System::Error::code = System::Error::UNSATSIFIED_PLUGIN_DEPENDENCIE_f(name, dependantPlugin->getName());
572
		return NULL;
573 574
	}
}
575
*/
576

577 578 579
/*********************************************************
 * MANAGE MAPS
 *********************************************************/
580

581
bool Window::addMap(MapHandlerGen* map)
582
{
583
	if (h_maps.contains(map->getName()))
584
	{
585
		System::Error::code = System::Error::MAP_EXISTS;
586 587
		return false;
	}
588

589
	h_maps.insert(map->getName(), map);
590 591 592

	emit(mapAdded(map));

593
	return true;
594 595
}

596
void Window::removeMap(const QString& name)
597
{
598
	if (h_maps.contains(name))
599
	{
600
		MapHandlerGen* map = h_maps[name];
601
		h_maps.remove(name);
602 603 604

		emit(mapRemoved(map));

605
		delete map;
606
	}
607 608
}

609
MapHandlerGen* Window::getMap(const QString& name) const
610 611 612
{
	if (h_maps.contains(name))
		return h_maps[name];
613 614
	else
	{
615
		System::Error::code = System::Error::MAP_DOES_NOT_EXIST;
616 617 618 619
		return NULL;
	}
}

620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
/*********************************************************
 * MANAGE TEXTURES
 *********************************************************/

Texture* Window::getTexture(const QString& image)
{
	if(h_textures.contains(image))
	{
		Texture* t = h_textures[image];
		t->ref++;
		return t;
	}
	else
	{
		Texture* t = NULL;
		QImage img(image);
		if(!img.isNull())
		{
			GLuint texID = m_firstView->bindTexture(img);
			t = new Texture(texID, img.size(), 1);
			h_textures.insert(image, t);
		}
		return t;
	}
}

void Window::releaseTexture(const QString& image)
{
	if(h_textures.contains(image))
	{
		Texture* t = h_textures[image];
		t->ref--;
		if(t->ref == 0)
		{
			m_firstView->deleteTexture(h_textures[image]->texID);
			h_textures.remove(image);
			delete t;
		}
	}
}

661 662 663 664 665 666 667 668 669 670 671


void Window::cb_aboutSCHNApps()
{
	QString str("SCHNApps:\nS... CGoGN Holder for Nice Applications\n"
	            "Web site: http://cgogn.unistra.fr \n"
	            "Contact information: cgogn@unistra.fr");
	QMessageBox::about(this, tr("About SCHNApps"), str);
}

void Window::cb_aboutCGoGN()
672 673 674 675 676 677 678 679
{
	QString str("CGoGN:\nCombinatorial and Geometric modeling\n"
	            "with Generic N-dimensional Maps\n"
	            "Web site: http://cgogn.unistra.fr \n"
	            "Contact information: cgogn@unistra.fr");
	QMessageBox::about(this, tr("About CGoGN"), str);
}

680 681 682 683 684
void Window::cb_showHideDock()
{
	m_dock->setVisible(m_dock->isHidden());
}

685
void Window::cb_manageCameras()
686
{
Pierre Kraemer's avatar
Pierre Kraemer committed
687
	m_camerasDialog->show();
688 689
}

690
void Window::cb_managePlugins()
691
{
Pierre Kraemer's avatar
Pierre Kraemer committed
692
	m_pluginsDialog->show();
693 694
}

695
void Window::cb_manageMaps()
696
{
Pierre Kraemer's avatar
Pierre Kraemer committed
697
	m_mapsDialog->show();
698
}
Pierre Kraemer's avatar
Pierre Kraemer committed
699 700 701 702

} // namespace SCHNApps

} // namespace CGoGN