window.cpp 15.2 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1
#include "window.h"
2 3 4 5 6

#include <QMessageBox>
#include <QDockWidget>
#include <QPluginLoader>
#include <QFileInfo>
7 8 9
#include <QKeyEvent>
#include <QMouseEvent>
#include <QWheelEvent>
10

Pierre Kraemer's avatar
Pierre Kraemer committed
11 12
#include "plugin.h"
#include "view.h"
13 14
#include "splitArea.h"

15
#include "viewSelector.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
16
#include "cameraDialog.h"
17
#include "pluginDialog.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
18 19 20

Window::Window(QWidget *parent) :
	QMainWindow(parent),
21
	m_firstView(NULL),
22 23 24
	m_dock(NULL),
	m_dockTabWidget(NULL)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
25
	// program in its initialization phase
26 27
	m_initialization = true;

Pierre Kraemer's avatar
Pierre Kraemer committed
28
	m_pluginDialog = new PluginDialog(this);
Pierre Kraemer's avatar
Pierre Kraemer committed
29
	m_cameraDialog = new CameraDialog(this);
30

Pierre Kraemer's avatar
Pierre Kraemer committed
31
	glewInit();
32

33
	this->setupUi(this);
34 35 36
	System::splash->showMessage("Welcome to SCHNApps", Qt::AlignBottom | Qt::AlignCenter);
	sleep(1);

Pierre Kraemer's avatar
Pierre Kraemer committed
37 38 39
	// layout in which we store the main area
	m_verticalLayout = new QVBoxLayout(centralwidget);
	m_verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
40

Pierre Kraemer's avatar
Pierre Kraemer committed
41
	// the main area: multi GL views display area
42
	m_splitArea = new SplitArea(centralwidget);
Pierre Kraemer's avatar
Pierre Kraemer committed
43
	m_verticalLayout->addWidget(m_splitArea);
44

Pierre Kraemer's avatar
Pierre Kraemer committed
45 46 47 48
	// init keys as unpressed
	keys[0] = false;
	keys[1] = false;
	keys[2] = false;
49

50 51 52 53
	// add first view
	m_firstView = addView();
	m_splitArea->addFitElement(m_firstView);

Pierre Kraemer's avatar
Pierre Kraemer committed
54
	// connect the basic actions
55 56
	connect(actionAboutSCHNApps, SIGNAL(triggered()), this, SLOT(cb_aboutSCHNApps()));
	connect(actionAboutCGoGN, SIGNAL(triggered()), this, SLOT(cb_aboutCGoGN()));
57

58 59 60
	connect(actionManagePlugins, SIGNAL(triggered()), this, SLOT(cb_managePlugins()));
	connect(actionManageViews, SIGNAL(triggered()), this, SLOT(cb_manageViews()));
	connect(actionManageCameras, SIGNAL(triggered()), this, SLOT(cb_manageCameras()));
61

62
//	System::StateHandler::loadState(this, &h_plugin, &h_scene, m_splitArea);
63

Pierre Kraemer's avatar
Pierre Kraemer committed
64
	// program in its initialization phase
65 66 67 68 69
	m_initialization = false;
}

Window::~Window()
{
70
//	System::StateHandler::saveState(this, &h_plugins, &h_scenes, m_splitArea);
71 72
}

Pierre Kraemer's avatar
Pierre Kraemer committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
/*********************************************************
 * MANAGE DOCK
 *********************************************************/

QTabWidget* Window::getDockTabWidget()
{
	// if no dock or no tab widget: set error message
	if (!m_dock || !m_dockTabWidget)
		System::Error::code = System::Error::NO_DOCK;

	return m_dockTabWidget;
}

void Window::addTabInDock(QWidget *tabWidget, const QString& tabText)
{
	// if there is still no dock
	if (!m_dock)
	{
		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);
	}

	// if there is still no tab widget in the dock
	if (!m_dockTabWidget)
	{
		QWidget* dockWidgetContents = new QWidget();
		QVBoxLayout* verticalLayout = new QVBoxLayout(dockWidgetContents);
		m_dockTabWidget = new QTabWidget(dockWidgetContents);
		m_dockTabWidget->setObjectName(QString::fromUtf8("tabWidget"));
		m_dockTabWidget->setLayoutDirection(Qt::RightToLeft);
		m_dockTabWidget->setTabPosition(QTabWidget::East);

		verticalLayout->addWidget(m_dockTabWidget);

		m_dock->setWidget(dockWidgetContents);
	}

	// adding a new tab containing the specified widget
	m_dockTabWidget->addTab(tabWidget, tabText);
114
//	m_dockTabWidget->setTabText(m_dockTabWidget->indexOf(tabWidget), tabText);
Pierre Kraemer's avatar
Pierre Kraemer committed
115 116 117 118 119 120 121 122 123 124 125 126 127 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 300 301
}

void Window::removeTabInDock(QWidget *tabWidget)
{
	// if there is a dock and a tab widget
	if (m_dock && m_dockTabWidget)
	{
		// remove the tab containing the specified widget
		m_dockTabWidget->removeTab(m_dockTabWidget->indexOf(tabWidget));
	}
}

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

/*********************************************************
 * MANAGE PLUGINS
 *********************************************************/

302
Plugin* Window::loadPlugin(QString pluginFilePath)
Pierre Kraemer's avatar
Pierre Kraemer committed
303
{
304
	QString pluginName = QFileInfo(pluginFilePath).baseName().remove(0, 3);
305 306 307 308 309 310 311 312

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

Pierre Kraemer's avatar
Pierre Kraemer committed
313
	// QT's plugin loader class
314
	QPluginLoader loader(pluginFilePath);
Pierre Kraemer's avatar
Pierre Kraemer committed
315 316 317 318 319 320

	// if the loader loads a plugin instance
	if (QObject* pluginObject = loader.instance())
	{
		Plugin* plugin = qobject_cast<Plugin*>(pluginObject);

321
		// we set the plugin with correct parameters (name, filepath, window)
Pierre Kraemer's avatar
Pierre Kraemer committed
322 323
		plugin->setName(pluginName);
		plugin->setFilePath(pluginFilePath);
324
		plugin->setWindow(this);
Pierre Kraemer's avatar
Pierre Kraemer committed
325 326 327 328 329 330

		// then we call its enable() methods
		if (plugin->enable())
		{
			// if it succeeded we reference this plugin
			h_plugins.insert(pluginName, plugin);
Pierre Kraemer's avatar
Pierre Kraemer committed
331
			statusbar->showMessage(pluginName + QString(" successfully loaded."), 2000);
Pierre Kraemer's avatar
Pierre Kraemer committed
332
			// method success
333
			return plugin;
Pierre Kraemer's avatar
Pierre Kraemer committed
334 335 336
		}
		else
		{
337 338
			delete plugin;
			return NULL;
Pierre Kraemer's avatar
Pierre Kraemer committed
339 340 341 342 343 344
		}
	}
	// if loading fails
	else
	{
		// set error message + method failure
345 346
		System::Error::code = System::Error::ERROR_PLUGIN_LOAD_f(pluginName);
		return NULL;
Pierre Kraemer's avatar
Pierre Kraemer committed
347 348 349 350 351
	}
}

void Window::unloadPlugin(const QString& pluginName)
{
352
	if (h_plugins.contains(pluginName))
Pierre Kraemer's avatar
Pierre Kraemer committed
353
	{
354
		Plugin* plugin = h_plugins[pluginName];
Pierre Kraemer's avatar
Pierre Kraemer committed
355 356 357

		// calling its disable() method and dereferencing it
		plugin->disable();
358
		h_plugins.remove(pluginName);
Pierre Kraemer's avatar
Pierre Kraemer committed
359

360 361
		// delete plugin
		delete plugin;
Pierre Kraemer's avatar
Pierre Kraemer committed
362 363

		statusbar->showMessage(pluginName + QString(" successfully unloaded."), 2000);
364 365
	}
}
Pierre Kraemer's avatar
Pierre Kraemer committed
366

367 368 369 370 371 372 373 374
Plugin* Window::getPlugin(const QString& name)
{
	if (h_plugins.contains(name))
		return h_plugins[name];
	else
	{
		System::Error::code = System::Error::PLUGIN_DOES_NOT_EXIST;
		return NULL;
Pierre Kraemer's avatar
Pierre Kraemer committed
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
	}
}
/*
Plugin* Window::checkPluginDependencie(QString name, Plugin* dependantPlugin)
{
	// 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
	else
	{
		System::Error::code = System::Error::UNSATSIFIED_PLUGIN_DEPENDENCIE_f(name, dependantPlugin->getName());
		return NULL;
	}
}
*/

398 399 400
/*********************************************************
 * MANAGE VIEWS
 *********************************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
401

402
View* Window::addView(const QString& name)
403
{
404
	if (h_views.contains(name))
405
	{
406 407
		System::Error::code = System::Error::VIEW_EXISTS;
		return NULL;
408 409
	}

410 411 412 413 414
	View* view = NULL;
	if(m_firstView == NULL)
		view = new View(name, this, this);
	else
		view = new View(name, this, this, m_firstView);
415
	h_views.insert(name, view);
Pierre Kraemer's avatar
Pierre Kraemer committed
416

417
	return view;
418 419
}

420 421 422 423 424
View* Window::addView()
{
	return addView(QString("view_") + QString::number(View::viewCount));
}

425
void Window::removeView(const QString& name)
426
{
427
	if (h_views.contains(name))
428
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
429 430 431 432 433 434 435 436
		if(h_views.count() > 1)
		{
			View* view = h_views[name];
			if(m_firstView == view)
				m_firstView = h_views.constBegin().value();
			h_views.remove(name);
			delete view;
		}
437 438 439
	}
}

440
View* Window::getView(const QString& name)
441
{
442 443
	if (h_views.contains(name))
		return h_views[name];
444 445
	else
	{
446 447
		System::Error::code = System::Error::VIEW_DOES_NOT_EXIST;
		return NULL;
448 449 450
	}
}

451 452 453 454 455
/*********************************************************
 * MANAGE CAMERAS
 *********************************************************/

Camera* Window::addCamera(const QString& name)
456
{
457
	if (h_cameras.contains(name))
458
	{
459 460
		System::Error::code = System::Error::CAMERA_EXISTS;
		return NULL;
461 462
	}

463 464
	Camera* camera = new Camera(name, this);
	h_cameras.insert(name, camera);
Pierre Kraemer's avatar
Pierre Kraemer committed
465

Pierre Kraemer's avatar
Pierre Kraemer committed
466
	m_cameraDialog->addCameraToList(camera->getName());
Pierre Kraemer's avatar
Pierre Kraemer committed
467

468
	return camera;
469 470
}

471 472 473 474 475
Camera* Window::addCamera()
{
	return addCamera(QString("camera_") + QString::number(Camera::cameraCount));
}

476
void Window::removeCamera(const QString& name)
477
{
478
	if (h_cameras.contains(name))
479
	{
480 481 482
		Camera* camera = h_cameras[name];
		h_cameras.remove(name);
		delete camera;
483 484 485
	}
}

486
Camera* Window::getCamera(const QString& name)
487
{
488 489
	if (h_cameras.contains(name))
		return h_cameras[name];
490 491
	else
	{
492 493
		System::Error::code = System::Error::CAMERA_DOES_NOT_EXIST;
		return NULL;
494 495 496
	}
}

497 498 499
/*********************************************************
 * MANAGE MAPS
 *********************************************************/
500

501
bool Window::addMap(const QString& name, MapHandler* map)
502
{
503
	if (h_maps.contains(name))
504
	{
505
		System::Error::code = System::Error::MAP_EXISTS;
506 507
		return false;
	}
508 509 510

	h_maps.insert(name, map);
	return true;
511 512
}

513
void Window::removeMap(const QString& name)
514
{
515
	if (h_maps.contains(name))
516
	{
517 518 519
		MapHandler* map = h_maps[name];
		h_maps.remove(name);
		delete map;
520
	}
521 522 523 524 525 526
}

MapHandler* Window::getMap(const QString& name)
{
	if (h_maps.contains(name))
		return h_maps[name];
527 528
	else
	{
529
		System::Error::code = System::Error::MAP_DOES_NOT_EXIST;
530 531 532 533
		return NULL;
	}
}

534 535 536



537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
void Window::keyPressEvent(QKeyEvent *event)
{
	if (event->key() == Qt::Key_M)
		keys[0] = true;
	else if (event->key() == Qt::Key_Shift)
		keys[1] = true;
	else if (event->key() == Qt::Key_Control)
		keys[2] = true;

	if (keys[0] && keys[1] && keys[2])
		this->moveView();
}

void Window::keyReleaseEvent(QKeyEvent *event)
{
	if (event->key() == Qt::Key_M)
		keys[0] = false;
	else if (event->key() == Qt::Key_Shift)
		keys[1] = false;
	else if (event->key() == Qt::CTRL)
		keys[2] = false;
}

void Window::moveView()
{
562 563
	// if splitArea not empty or has more than 1 element
	if (m_splitArea->getNbRows() > 1 || ((QSplitter *)m_splitArea->widget(0))->count() > 1)
564
	{
565
		// map the splitArea
566 567 568
		ViewPixMaps pm, finalPm;
		pm.fromSplitArea(m_splitArea);

569
		// build a GLViewerSelector using this map
570 571
		ViewSelector selector(pm, this, ViewSelector::MOVE);

572
		// show the move dialog box
573 574
		selector.exec();

575
		// the dialog is accepted: it has some modification
576 577
		if (selector.result() == QDialog::Accepted)
		{
578
			// get back the modifier map
579 580
			finalPm = selector.getGLVMap();

581
			// creating a new split area and switch it with the old one
582 583
			SplitArea *old = m_splitArea;
			m_splitArea = new SplitArea(centralwidget);
584
			m_verticalLayout->addWidget(m_splitArea);
585

586 587 588
			// fill the new SplitArea using the modified map
			int x = 0;
			int y = 0;
589 590 591 592 593 594 595

			for (ViewPixMaps::y_iterator y_it = finalPm.y_begin(); y_it != finalPm.y_end(); ++y_it)
			{
				for (ViewPixMaps::x_iterator x_it = finalPm.x_begin(y_it); x_it != finalPm.x_end(y_it); ++x_it)
				{
					x_it->view->setParent(m_splitArea);
					m_splitArea->addElementAt(x_it->view, x, y);
596
					++x;
597 598 599 600 601
				}
				x = 0;
				++y;
			}

602 603
			// delete the old splitArea
			delete old;
604 605
		}

606 607 608 609
		// key states at the end of the move dialog
		keys[0] = selector.keys[0];
		keys[1] = selector.keys[1];
		keys[2] = selector.keys[2];
610 611 612
	}
}

613 614 615 616 617 618 619 620 621
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()
622 623 624 625 626 627 628 629
{
	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);
}

630
void Window::cb_managePlugins()
631
{
Pierre Kraemer's avatar
Pierre Kraemer committed
632
	m_pluginDialog->show();
633 634
}

635
void Window::cb_manageViews()
636
{
637 638 639 640 641
	ViewPixMaps pm;
	pm.fromSplitArea(m_splitArea);

	ViewSelector selector(pm, this, ViewSelector::SELECT);

Pierre Kraemer's avatar
Pierre Kraemer committed
642
	selector.setInsertionName("new view");
643 644 645 646 647 648 649 650 651

	selector.exec();

	if (selector.result() == QDialog::Accepted)
	{
		View* v = addView();
		QPoint insertPoint = selector.getInsertPoint();
		m_splitArea->addElementAt(v, insertPoint.x(), insertPoint.y());
	}
652 653
}

654
void Window::cb_manageCameras()
655
{
Pierre Kraemer's avatar
Pierre Kraemer committed
656
	m_cameraDialog->show();
657
}