schnapps.cpp 33.6 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1 2 3 4 5 6 7 8 9 10 11 12
#include <QVBoxLayout>
#include <QSplitter>
#include <QMessageBox>
#include <QDockWidget>
#include <QPluginLoader>
#include <QFile>
#include <QFileDialog>
#include <QFileInfo>
#include <QDir>
#include <QKeyEvent>
#include <QMouseEvent>
#include <QWheelEvent>
Sylvain Thery's avatar
Sylvain Thery committed
13
#include <QFile>
14
#include <QByteArray>
Pierre Kraemer's avatar
Pierre Kraemer committed
15

Thery Sylvain's avatar
Thery Sylvain committed
16 17 18
#include "mapHandler.h"
#include "schnapps.h"

Pierre Kraemer's avatar
Pierre Kraemer committed
19 20 21 22 23 24 25 26 27 28 29
#include "controlDock_cameraTab.h"
#include "controlDock_mapTab.h"
#include "controlDock_pluginTab.h"

#include "camera.h"
#include "view.h"
#include "texture.h"
#include "plugin.h"
#include "plugin_interaction.h"
#include "plugin_processing.h"

Sylvain Thery's avatar
Sylvain Thery committed
30
#include "plugins_default_directory.h"
31
#include "Topology/generic/genericmap.h"
Sylvain Thery's avatar
Sylvain Thery committed
32

Pierre Kraemer's avatar
Pierre Kraemer committed
33 34 35 36 37 38 39 40
namespace CGoGN
{

namespace SCHNApps
{

SCHNApps::SCHNApps(const QString& appPath, PythonQtObjectPtr& pythonContext, PythonQtScriptingConsole& pythonConsole) :
	QMainWindow(),
Sylvain Thery's avatar
Sylvain Thery committed
41 42
	m_pyRecording(NULL),
	m_pyRecFile(NULL),
Pierre Kraemer's avatar
Pierre Kraemer committed
43 44 45 46 47 48
	m_appPath(appPath),
	m_pythonContext(pythonContext),
	m_pythonConsole(pythonConsole),
	m_firstView(NULL),
	m_selectedView(NULL)
{
49 50
	GenericMap::initAllStatics(&m_sp);

Pierre Kraemer's avatar
Pierre Kraemer committed
51 52 53 54 55 56 57
	this->setupUi(this);

	// create & setup control dock

	m_controlDock = new QDockWidget("Control Dock", this);
	m_controlDock->setAllowedAreas(Qt::LeftDockWidgetArea);
	m_controlDock->setFeatures(QDockWidget::DockWidgetFloatable | QDockWidget::DockWidgetClosable);
58
	m_controlDock->setMaximumWidth(250);
Pierre Kraemer's avatar
Pierre Kraemer committed
59 60 61 62 63

	m_controlDockTabWidget = new QTabWidget(m_controlDock);
	m_controlDockTabWidget->setObjectName("ControlDockTabWidget");
	m_controlDockTabWidget->setLayoutDirection(Qt::LeftToRight);
	m_controlDockTabWidget->setTabPosition(QTabWidget::North);
Pierre Kraemer's avatar
Pierre Kraemer committed
64
	m_controlDockTabWidget->setMovable(true);
Pierre Kraemer's avatar
Pierre Kraemer committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

	addDockWidget(Qt::LeftDockWidgetArea, m_controlDock);
	m_controlDock->setVisible(true);
	m_controlDock->setWidget(m_controlDockTabWidget);

	m_controlCameraTab = new ControlDock_CameraTab(this);
	m_controlDockTabWidget->addTab(m_controlCameraTab, m_controlCameraTab->title());
	m_controlMapTab = new ControlDock_MapTab(this);
	m_controlDockTabWidget->addTab(m_controlMapTab, m_controlMapTab->title());
	m_controlPluginTab = new ControlDock_PluginTab(this);
	m_controlDockTabWidget->addTab(m_controlPluginTab, m_controlPluginTab->title());

	connect(action_ShowHideControlDock, SIGNAL(triggered()), this, SLOT(showHideControlDock()));

	// create & setup plugin dock

	m_pluginDock = new QDockWidget("Plugins Dock", this);
	m_pluginDock->setAllowedAreas(Qt::RightDockWidgetArea);
	m_pluginDock->setFeatures(QDockWidget::DockWidgetFloatable | QDockWidget::DockWidgetClosable);

	m_pluginDockTabWidget = new QTabWidget(m_pluginDock);
	m_pluginDockTabWidget->setObjectName("PluginDockTabWidget");
	m_pluginDockTabWidget->setLayoutDirection(Qt::LeftToRight);
	m_pluginDockTabWidget->setTabPosition(QTabWidget::East);
Pierre Kraemer's avatar
Pierre Kraemer committed
89
	m_pluginDockTabWidget->setMovable(true);
Pierre Kraemer's avatar
Pierre Kraemer committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

	addDockWidget(Qt::RightDockWidgetArea, m_pluginDock);
	m_pluginDock->setVisible(false);
	m_pluginDock->setWidget(m_pluginDockTabWidget);

	connect(action_ShowHidePluginDock, SIGNAL(triggered()), this, SLOT(showHidePluginDock()));

	// create & setup python dock

	m_pythonDock = new QDockWidget("Python Dock", this);
	m_pythonDock->setAllowedAreas(Qt::BottomDockWidgetArea);
	m_pythonDock->setFeatures(QDockWidget::DockWidgetFloatable | QDockWidget::DockWidgetClosable);

	addDockWidget(Qt::BottomDockWidgetArea, m_pythonDock);
	m_pythonDock->setVisible(false);
	m_pythonDock->setWidget(&m_pythonConsole);

	connect(action_ShowHidePythonDock, SIGNAL(triggered()), this, SLOT(showHidePythonDock()));
	connect(action_LoadPythonScript, SIGNAL(triggered()), this, SLOT(loadPythonScriptFromFileDialog()));

110
	connect(action_Python_Recording, SIGNAL(triggered()), this, SLOT(pyRecording()));
Sylvain Thery's avatar
Sylvain Thery committed
111
	connect(action_Append_Python_Recording, SIGNAL(triggered()), this, SLOT(appendPyRecording()));
Sylvain Thery's avatar
Sylvain Thery committed
112 113
	connect(action_Clean_All, SIGNAL(triggered()), this, SLOT(cleanAll()));

Sylvain Thery's avatar
Sylvain Thery committed
114 115 116 117 118

	action_Python_Recording->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_P));
	action_Append_Python_Recording->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_P));


Sylvain Thery's avatar
Sylvain Thery committed
119 120


121
	// create & setup central widget (views)
Pierre Kraemer's avatar
Pierre Kraemer committed
122
	m_centralLayout = new QVBoxLayout(centralwidget);
123
	m_centralLayout->setMargin(2);
Pierre Kraemer's avatar
Pierre Kraemer committed
124 125 126 127 128

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

Sylvain Thery's avatar
Sylvain Thery committed
129 130 131 132 133 134 135 136 137
	if (Utils::GLSLShader::CURRENT_OGL_VERSION >= 3)
	{
		QGLFormat glFormat;
		glFormat.setVersion( Utils::GLSLShader::MAJOR_OGL_CORE, Utils::GLSLShader::MINOR_OGL_CORE);
		glFormat.setProfile( QGLFormat::CoreProfile ); // Requires >=Qt-4.8.0
		glFormat.setSampleBuffers( true );
		QGLFormat::setDefaultFormat(glFormat);
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
138 139 140 141 142 143 144 145 146 147
	m_firstView = addView();
	setSelectedView(m_firstView);
	m_rootSplitter->addWidget(m_firstView);

	// connect basic actions

	connect(action_AboutSCHNApps, SIGNAL(triggered()), this, SLOT(aboutSCHNApps()));
	connect(action_AboutCGoGN, SIGNAL(triggered()), this, SLOT(aboutCGoGN()));

	// register a first plugins directory
Sylvain Thery's avatar
Sylvain Thery committed
148 149
//	registerPluginsDirectory(m_appPath + QString("/../lib"));

150
	// using macro in plugins_default_directory.h generated by cmake for lib/Release or lib/Debug auto selection !
Sylvain Thery's avatar
Sylvain Thery committed
151
	registerPluginsDirectory(QString(PLUGINS_DEFAULT_DIRECTORY));
Pierre Kraemer's avatar
Pierre Kraemer committed
152 153 154 155
}

SCHNApps::~SCHNApps()
{
Sylvain Thery's avatar
Sylvain Thery committed
156 157
	// TODO check that cameras are destructed
	m_cameras.clear();
Pierre Kraemer's avatar
Pierre Kraemer committed
158 159 160 161 162 163 164 165 166 167 168 169 170
}

/*********************************************************
 * MANAGE CAMERAS
 *********************************************************/

Camera* SCHNApps::addCamera(const QString& name)
{
	if (m_cameras.contains(name))
		return NULL;

	Camera* camera = new Camera(name, this);
	m_cameras.insert(name, camera);
171
	DEBUG_EMIT("cameraAdded");
Pierre Kraemer's avatar
Pierre Kraemer committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
	emit(cameraAdded(camera));
	return camera;
}

Camera* SCHNApps::addCamera()
{
	return addCamera(QString("camera_") + QString::number(Camera::cameraCount));
}

void SCHNApps::removeCamera(const QString& name)
{
	Camera* camera = getCamera(name);
	if (camera && !camera->isUsed())
	{
		m_cameras.remove(name);
187
		DEBUG_EMIT("cameraRemoved");
Pierre Kraemer's avatar
Pierre Kraemer committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
		emit(cameraRemoved(camera));
		delete camera;
	}
}

Camera* SCHNApps::getCamera(const QString& name) const
{
	if (m_cameras.contains(name))
		return m_cameras[name];
	else
		return NULL;
}

/*********************************************************
 * MANAGE VIEWS
 *********************************************************/

Sylvain Thery's avatar
Sylvain Thery committed
205 206 207 208 209 210 211 212
void SCHNApps::redrawAllViews()
{
	foreach(View *v, m_views)
	{
		v->updateGL();
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
213 214 215 216 217
View* SCHNApps::addView(const QString& name)
{
	if (m_views.contains(name))
		return NULL;

Sylvain Thery's avatar
Sylvain Thery committed
218
	View* view = new View(name, this, m_firstView);
219

Pierre Kraemer's avatar
Pierre Kraemer committed
220 221
	m_views.insert(name, view);

222
	DEBUG_EMIT("viewAdded");
Pierre Kraemer's avatar
Pierre Kraemer committed
223 224
	emit(viewAdded(view));

Sylvain Thery's avatar
Sylvain Thery committed
225 226 227 228
	// RECORDING
	if (m_pyRecording)
		*m_pyRecording << view->getName() << " = schnapps.getView(\"" << view->getName() << "\");" << endl;

Pierre Kraemer's avatar
Pierre Kraemer committed
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
	return view;
}

View* SCHNApps::addView()
{
	return addView(QString("view_") + QString::number(View::viewCount));
}

void SCHNApps::removeView(const QString& name)
{
	if (m_views.contains(name))
	{
		if(m_views.count() > 1)
		{
			View* view = m_views[name];
			if(view == m_firstView)
			{
				ViewSet::const_iterator i = m_views.constBegin();
				while (i != m_views.constEnd())
				{
					if(i.value() != view)
					{
						m_firstView = i.value();
						i = m_views.constEnd();
					}
					else
						++i;
				}
			}
			if(view == m_selectedView)
				setSelectedView(m_firstView);

			m_views.remove(name);

263
			DEBUG_EMIT("viewRemoved");
Pierre Kraemer's avatar
Pierre Kraemer committed
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
			emit(viewRemoved(view));

			delete view;
		}
	}
}

View* SCHNApps::getView(const QString& name) const
{
	if (m_views.contains(name))
		return m_views[name];
	else
		return NULL;
}

void SCHNApps::setSelectedView(View* view)
{
281 282
	int currentTab = m_pluginDockTabWidget->currentIndex();

283 284 285 286 287 288 289
	if(m_selectedView)
	{
		foreach(PluginInteraction* p, m_selectedView->getLinkedPlugins())
			disablePluginTabWidgets(p);
		disconnect(m_selectedView, SIGNAL(pluginLinked(PluginInteraction*)), this, SLOT(enablePluginTabWidgets(PluginInteraction*)));
		disconnect(m_selectedView, SIGNAL(pluginUnlinked(PluginInteraction*)), this, SLOT(disablePluginTabWidgets(PluginInteraction*)));
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
290 291 292

	View* oldSelected = m_selectedView;
	m_selectedView = view;
Sylvain Thery's avatar
Sylvain Thery committed
293 294
	if (oldSelected)
		oldSelected->hideDialogs();
Pierre Kraemer's avatar
Pierre Kraemer committed
295

296 297 298 299
	foreach(PluginInteraction* p, m_selectedView->getLinkedPlugins())
		enablePluginTabWidgets(p);
	connect(m_selectedView, SIGNAL(pluginLinked(PluginInteraction*)), this, SLOT(enablePluginTabWidgets(PluginInteraction*)));
	connect(m_selectedView, SIGNAL(pluginUnlinked(PluginInteraction*)), this, SLOT(disablePluginTabWidgets(PluginInteraction*)));
Pierre Kraemer's avatar
Pierre Kraemer committed
300

301 302
	m_pluginDockTabWidget->setCurrentIndex(currentTab);

303
	DEBUG_EMIT("selectedViewChanged");
Pierre Kraemer's avatar
Pierre Kraemer committed
304 305 306 307 308 309 310
	emit(selectedViewChanged(oldSelected, m_selectedView));

	if(oldSelected)
		oldSelected->updateGL();
	m_selectedView->updateGL();
}

Sylvain Thery's avatar
Sylvain Thery committed
311
void SCHNApps::setSelectedView(const QString& name)
Pierre Kraemer's avatar
Pierre Kraemer committed
312
{
Sylvain Thery's avatar
Sylvain Thery committed
313 314 315
	View* v = this->getView(name);
	setSelectedView(v);
}
Sylvain Thery's avatar
Sylvain Thery committed
316

Sylvain Thery's avatar
Sylvain Thery committed
317 318
View* SCHNApps::splitView(const QString& name, Qt::Orientation orientation)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
319 320 321 322 323 324 325 326 327
	View* newView = addView();

	View* view = m_views[name];
	QSplitter* parent = (QSplitter*)(view->parentWidget());
	if(parent == m_rootSplitter && !b_rootSplitterInitialized)
	{
		m_rootSplitter->setOrientation(orientation);
		b_rootSplitterInitialized = true;
	}
328 329 330 331 332 333 334 335 336 337 338 339
	if (parent->orientation() == orientation)
	{
		parent->insertWidget(parent->indexOf(view) + 1, newView);
		QList<int> sz = parent->sizes();
		int tot = 0;
		for (int i = 0; i < parent->count(); ++i)
			tot += sz[i];
		sz[0] = tot / parent->count() + tot % parent->count();
		for (int i = 1; i < parent->count(); ++i)
			sz[i] = tot / parent->count();
		parent->setSizes(sz);
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
340 341 342 343 344 345 346 347
	else
	{
		int idx = parent->indexOf(view);
		view->setParent(NULL);
		QSplitter* spl = new QSplitter(orientation);
		spl->addWidget(view);
		spl->addWidget(newView);
		parent->insertWidget(idx, spl);
348 349 350 351 352 353 354

		QList<int> sz = spl->sizes();
		int tot = sz[0] + sz[1];
		sz[0] = tot / 2;
		sz[1] = tot - sz[0];
		spl->setSizes(sz);
	}
Sylvain Thery's avatar
Sylvain Thery committed
355 356 357 358 359 360

	// RECORDING
	if (m_pyRecording)
		*m_pyRecording << newView->getName() << " = schnapps.splitView(\"" << name << "\", " << orientation << ");" << endl;

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

QString SCHNApps::saveSplitViewPositions()
{
	QList<QSplitter*> liste;
	liste.push_back(m_rootSplitter);

	QString result;
	QTextStream qts(&result);
	while (!liste.empty())
	{
		QSplitter* spl = liste.first();
		for (int i = 0; i < spl->count(); ++i)
		{
			QWidget *w = spl->widget(i);
			QSplitter* qw = dynamic_cast<QSplitter*>(w);
			if (qw != NULL)
				liste.push_back(qw);
		}
		QByteArray ba = spl->saveState();
		qts << ba.count() << " ";
		for (int j = 0; j < ba.count(); ++j)
			qts << int(ba[j]) << " ";
		//qts << endl;
		liste.pop_front();
Pierre Kraemer's avatar
Pierre Kraemer committed
386
	}
387
	return result;
Pierre Kraemer's avatar
Pierre Kraemer committed
388 389
}

390 391 392 393 394 395 396 397 398 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 424 425 426 427 428 429

void SCHNApps::restoreSplitViewPositions(QString stringStates)
{
	QList<QSplitter*> liste;
	liste.push_back(m_rootSplitter);

	QTextStream qts(&stringStates);
	while (!liste.empty())
	{
		QSplitter* spl = liste.first();
		for (int i = 0; i < spl->count(); ++i)
		{
			QWidget *w = spl->widget(i);
			QSplitter* qw = dynamic_cast<QSplitter*>(w);
			if (qw != NULL)
				liste.push_back(qw);
		}
		if (qts.atEnd())
		{
			std::cerr << "Problem restoring view split configuration" << std::endl;
			return;
		}

		int nb;
		qts >> nb;
		QByteArray ba(nb + 1, 0);
		for (int j = 0; j < nb; ++j)
		{
			int v;
			qts >> v;
			ba[j] = char(v);
		}
		spl->restoreState(ba);
		liste.pop_front();
	}
}




Pierre Kraemer's avatar
Pierre Kraemer committed
430 431 432 433 434 435
/*********************************************************
 * MANAGE PLUGINS
 *********************************************************/

void SCHNApps::registerPluginsDirectory(const QString& path)
{
436 437 438

#ifdef WIN32
#ifdef _DEBUG
439
	QDir directory(path+QString("Debug/"));
440 441 442 443
#else
	QDir directory(path + QString("Release/"));
#endif
#else
Pierre Kraemer's avatar
Pierre Kraemer committed
444
	QDir directory(path);
445
#endif
Pierre Kraemer's avatar
Pierre Kraemer committed
446 447 448 449 450 451 452 453 454 455 456 457
	if(directory.exists())
	{
		QStringList filters;
		filters << "lib*.so";
		filters << "lib*.dylib";
		filters << "*.dll";

		QStringList pluginFiles = directory.entryList(filters, QDir::Files);

		foreach(QString pluginFile, pluginFiles)
		{
			QFileInfo pfi(pluginFile);
Sylvain Thery's avatar
Sylvain Thery committed
458 459 460
#ifdef WIN32
			QString pluginName = pfi.baseName();
#else
Pierre Kraemer's avatar
Pierre Kraemer committed
461
			QString pluginName = pfi.baseName().remove(0, 3);
Sylvain Thery's avatar
Sylvain Thery committed
462
#endif
Pierre Kraemer's avatar
Pierre Kraemer committed
463 464 465 466 467
			QString pluginFilePath = directory.absoluteFilePath(pluginFile);

			if(!m_availablePlugins.contains(pluginName))
			{
				m_availablePlugins.insert(pluginName, pluginFilePath);
468
				DEBUG_EMIT("pluginAvailableAdded");
Pierre Kraemer's avatar
Pierre Kraemer committed
469 470 471 472 473 474 475 476
				emit(pluginAvailableAdded(pluginName));
			}
		}
	}
}

Plugin* SCHNApps::enablePlugin(const QString& pluginName)
{
Sylvain Thery's avatar
Sylvain Thery committed
477 478 479 480
	// RECORDING
	if (m_pyRecording)
		*m_pyRecording << pluginName << " = schnapps.enablePlugin(\"" << pluginName << "\");" << endl;

Pierre Kraemer's avatar
Pierre Kraemer committed
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
	if (m_plugins.contains(pluginName))
		return m_plugins[pluginName];

	if (m_availablePlugins.contains(pluginName))
	{
		QString pluginFilePath = m_availablePlugins[pluginName];

		QPluginLoader loader(pluginFilePath);

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

			// set the plugin with correct parameters (name, filepath, SCHNApps)
			plugin->setName(pluginName);
			plugin->setFilePath(pluginFilePath);
			plugin->setSCHNApps(this);

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

				statusbar->showMessage(pluginName + QString(" successfully loaded."), 2000);
507
				DEBUG_EMIT("pluginEnabled");
Pierre Kraemer's avatar
Pierre Kraemer committed
508
				emit(pluginEnabled(plugin));
509
//				menubar->repaint();
Pierre Kraemer's avatar
Pierre Kraemer committed
510 511 512 513 514 515 516 517 518 519 520 521
				// method success
				return plugin;
			}
			else
			{
				delete plugin;
				return NULL;
			}
		}
		// if loading fails
		else
		{
522
			std::cout << "loader.instance() failed.." << std::endl;
Pierre Kraemer's avatar
Pierre Kraemer committed
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
			return NULL;
		}
	}
	else
	{
		return NULL;
	}
}

void SCHNApps::disablePlugin(const QString& pluginName)
{
	if (m_plugins.contains(pluginName))
	{
		Plugin* plugin = m_plugins[pluginName];

		// unlink linked views (for interaction plugins)
		PluginInteraction* pi = dynamic_cast<PluginInteraction*>(plugin);
		if(pi)
		{
			foreach(View* view, pi->getLinkedViews())
				view->unlinkPlugin(pi);
		}

		// call disable() method and dereference plugin
		plugin->disable();
		m_plugins.remove(pluginName);

Sylvain Thery's avatar
Sylvain Thery committed
550 551 552 553 554 555 556 557
		// remove plugin dock tabs
		foreach(QWidget* tab, m_pluginTabs[plugin])
			removePluginDockTab(plugin, tab);
		// remove plugin menu actions
		foreach(QAction* action, m_pluginMenuActions[plugin])
			removeMenuAction(plugin, action);


Pierre Kraemer's avatar
Pierre Kraemer committed
558 559 560 561
		QPluginLoader loader(plugin->getFilePath());
		loader.unload();

		statusbar->showMessage(pluginName + QString(" successfully unloaded."), 2000);
562
		DEBUG_EMIT("pluginDisabled");
Pierre Kraemer's avatar
Pierre Kraemer committed
563 564 565
		emit(pluginDisabled(plugin));

		delete plugin;
Sylvain Thery's avatar
Sylvain Thery committed
566 567 568 569 570 571

		// RECORDING
		if (m_pyRecording)
			*m_pyRecording << "schnapps.disablePlugin(\"" << pluginName << "\");" << endl;


Pierre Kraemer's avatar
Pierre Kraemer committed
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
	}
}

Plugin* SCHNApps::getPlugin(const QString& name) const
{
	if (m_plugins.contains(name))
		return m_plugins[name];
	else
		return NULL;
}

void SCHNApps::addPluginDockTab(Plugin* plugin, QWidget* tabWidget, const QString& tabText)
{
	if(tabWidget && !m_pluginTabs[plugin].contains(tabWidget))
	{
		int currentTab = m_pluginDockTabWidget->currentIndex();

		int idx = m_pluginDockTabWidget->addTab(tabWidget, tabText);
		m_pluginDock->setVisible(true);
591 592 593 594 595 596 597 598 599

		PluginInteraction* pi = dynamic_cast<PluginInteraction*>(plugin);
		if(pi)
		{
			if(pi->isLinkedToView(m_selectedView))
				m_pluginDockTabWidget->setTabEnabled(idx, true);
			else
				m_pluginDockTabWidget->setTabEnabled(idx, false);
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617

		if(currentTab != -1)
			m_pluginDockTabWidget->setCurrentIndex(currentTab);

		m_pluginTabs[plugin].append(tabWidget);
	}
}

void SCHNApps::removePluginDockTab(Plugin* plugin, QWidget *tabWidget)
{
	if(tabWidget && m_pluginTabs[plugin].contains(tabWidget))
	{
		m_pluginDockTabWidget->removeTab(m_pluginDockTabWidget->indexOf(tabWidget));

		m_pluginTabs[plugin].removeOne(tabWidget);
	}
}

618 619 620 621 622 623 624 625
void SCHNApps::enablePluginTabWidgets(PluginInteraction* plugin)
{
	if(m_pluginTabs.contains(plugin))
	{
		foreach(QWidget* w, m_pluginTabs[plugin])
			m_pluginDockTabWidget->setTabEnabled(m_pluginDockTabWidget->indexOf(w), true);
	}
}
Pierre Kraemer's avatar
Pierre Kraemer committed
626

627 628 629 630 631 632 633 634
void SCHNApps::disablePluginTabWidgets(PluginInteraction* plugin)
{
	if(m_pluginTabs.contains(plugin))
	{
		foreach(QWidget* w, m_pluginTabs[plugin])
			m_pluginDockTabWidget->setTabEnabled(m_pluginDockTabWidget->indexOf(w), false);
	}
}
Pierre Kraemer's avatar
Pierre Kraemer committed
635 636 637 638 639

/*********************************************************
 * MANAGE MAPS
 *********************************************************/

640 641 642 643 644 645 646 647 648 649
MapHandlerGen* SCHNApps::duplicateMap(const QString& name, bool properties)
{
	if (! m_maps.contains(name))
		return NULL;

	QString newName = name + "_copy";

	if (m_maps.contains(newName))
		return NULL;

650 651 652 653 654
	// RECORDING
	QTextStream* rec = this->pythonStreamRecorder();
	if (rec)
		*rec << newName << " = schnapps.duplicateMap(" << name << ".getName(),1);" << endl;

655
	MapHandlerGen* maph = m_maps[name];
Sylvain Thery's avatar
Sylvain Thery committed
656
	MapHandlerGen* new_mh = NULL; // avoid warning
657 658 659 660 661 662 663 664 665

	unsigned int dim = maph->getGenericMap()->dimension();

	switch (dim)
	{
	case 2: {
				PFP2::MAP* map = new PFP2::MAP();
				map->copyFrom(*(maph->getGenericMap()));
				new_mh = new MapHandler<PFP2>(newName, this, map);
666 667 668 669 670 671 672 673 674 675 676

				for (unsigned int orbit = VERTEX; orbit <= VOLUME; orbit++)
				{
					AttributeContainer& cont = map->getAttributeContainer(orbit);
					std::vector<std::string> names;
					std::vector<std::string> types;
					cont.getAttributesNames(names);
					cont.getAttributesTypes(types);
					for (unsigned int i = 0; i < names.size(); ++i)
						new_mh->registerAttribute(orbit, QString::fromStdString(names[i]), QString::fromStdString(types[i]));
				}
677 678 679 680 681 682
				break;
			}
	case 3: {
				PFP3::MAP* map = new PFP3::MAP();
				map->copyFrom(*(maph->getGenericMap()));
				new_mh = new MapHandler<PFP3>(newName, this, map);
683 684 685 686 687 688 689 690 691 692
				for (unsigned int orbit = VERTEX; orbit <= VOLUME; orbit++)
				{
					AttributeContainer& cont = map->getAttributeContainer(orbit);
					std::vector<std::string> names;
					std::vector<std::string> types;
					cont.getAttributesNames(names);
					cont.getAttributesTypes(types);
					for (unsigned int i = 0; i < names.size(); ++i)
						new_mh->registerAttribute(orbit, QString::fromStdString(names[i]), QString::fromStdString(types[i]));
				}
693 694
				break;
			}
Sylvain Thery's avatar
Sylvain Thery committed
695 696 697
	default:
		std::cerr << "Dimension " << dim << " not yet supported in map duplication" << std::endl;
		break;
698 699
	}

700 701 702



703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
	m_maps.insert(newName, new_mh);
	DEBUG_EMIT("mapAdded");
	emit(mapAdded(new_mh));

	if (properties)
	{
		// BB
		new_mh->setBBVertexAttribute(maph->getBBVertexAttributeName());

		//VBOs
		const VBOSet& vbos = maph->getVBOSet();
		foreach(QString s, vbos.keys())
		{
			new_mh->createVBO(s);
		}
	}

720 721
	pythonVarDeclare(new_mh->getName());

722 723 724 725 726 727
	return new_mh;
}




Pierre Kraemer's avatar
Pierre Kraemer committed
728 729
MapHandlerGen* SCHNApps::addMap(const QString& name, unsigned int dim)
{
730
	QString finalName = name;
Pierre Kraemer's avatar
Pierre Kraemer committed
731
	if (m_maps.contains(name))
732 733 734 735 736 737 738 739
	{
		int i = 1;
		do
		{
			finalName = name + QString("_") + QString::number(i);
			++i;
		} while (m_maps.contains(finalName));
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
740 741 742 743 744 745

	MapHandlerGen* mh = NULL;
	switch(dim)
	{
		case 2 : {
			PFP2::MAP* map = new PFP2::MAP();
746
			mh = new MapHandler<PFP2>(finalName, this, map);
Pierre Kraemer's avatar
Pierre Kraemer committed
747 748 749 750
			break;
		}
		case 3 : {
			PFP3::MAP* map = new PFP3::MAP();
751
			mh = new MapHandler<PFP3>(finalName, this, map);
Pierre Kraemer's avatar
Pierre Kraemer committed
752 753 754 755
			break;
		}
	}

756
	m_maps.insert(finalName, mh);
Pierre Kraemer's avatar
Pierre Kraemer committed
757

758
	DEBUG_EMIT("mapAdded");
Pierre Kraemer's avatar
Pierre Kraemer committed
759 760 761 762 763 764 765 766 767 768 769
	emit(mapAdded(mh));

	return mh;
}

void SCHNApps::removeMap(const QString& name)
{
	if (m_maps.contains(name))
	{
		MapHandlerGen* map = m_maps[name];
		foreach(View* view, map->getLinkedViews())
770
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
771
			view->unlinkMap(map);
772
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
773 774 775

		m_maps.remove(name);

776
		DEBUG_EMIT("mapRemoved");
Pierre Kraemer's avatar
Pierre Kraemer committed
777 778
		emit(mapRemoved(map));

779 780
		// unselect map if it is removed
		if (this->getSelectedMap() == map)
781
		{
782
			setSelectedMap(QString("NONE"));
783
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
784 785 786 787
		delete map;
	}
}

788 789 790 791 792
void SCHNApps::setSelectedMap(const QString& mapName)
{
	m_controlMapTab->setSelectedMap(mapName);
}

Pierre Kraemer's avatar
Pierre Kraemer committed
793 794 795 796 797 798 799 800 801 802 803 804 805
MapHandlerGen* SCHNApps::getMap(const QString& name) const
{
	if (m_maps.contains(name))
		return m_maps[name];
	else
		return NULL;
}

MapHandlerGen* SCHNApps::getSelectedMap() const
{
	return m_controlMapTab->getSelectedMap();
}

806 807 808 809 810 811 812 813 814 815
unsigned int SCHNApps::getCurrentOrbit() const
{
	return m_controlMapTab->getCurrentOrbit();
}

CellSelectorGen* SCHNApps::getSelectedSelector(unsigned int orbit) const
{
	return m_controlMapTab->getSelectedSelector(orbit);
}

Sylvain Thery's avatar
Sylvain Thery committed
816 817 818 819 820 821 822 823 824 825 826 827

void SCHNApps::setSelectedSelectorCurrentMap(unsigned int orbit, const QString& name)
{
	QList<QListWidgetItem*> items;
	switch (orbit)
	{
	case DART: items = m_controlMapTab->list_dartSelectors->findItems(name,Qt::MatchExactly); break;
	case VERTEX: items = m_controlMapTab->list_vertexSelectors->findItems(name, Qt::MatchExactly); break;
	case EDGE: items = m_controlMapTab->list_edgeSelectors->findItems(name, Qt::MatchExactly); break;
	case FACE: items = m_controlMapTab->list_faceSelectors->findItems(name, Qt::MatchExactly); break;
	case VOLUME: items = m_controlMapTab->list_volumeSelectors->findItems(name, Qt::MatchExactly); break;
	}
828
	m_controlMapTab->tabWidget_mapInfo->setCurrentIndex(orbit);
Sylvain Thery's avatar
Sylvain Thery committed
829 830 831 832
	if (!items.empty())
		items[0]->setSelected(true);
}

Pierre Kraemer's avatar
Pierre Kraemer committed
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 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 928 929 930 931 932 933 934 935 936
/*********************************************************
 * MANAGE TEXTURES
 *********************************************************/

Texture* SCHNApps::getTexture(const QString& image)
{
	if(m_textures.contains(image))
	{
		Texture* t = m_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);
			m_textures.insert(image, t);
		}
		return t;
	}
}

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

/*********************************************************
 * MANAGE MENU ACTIONS
 *********************************************************/

void SCHNApps::addMenuAction(Plugin* plugin, const QString& menuPath, QAction* action)
{
	if(action && !menuPath.isEmpty() && !m_pluginMenuActions[plugin].contains(action))
	{
		// 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)
		{
			unsigned int i = 0;
			QMenu* lastMenu = NULL;
			foreach(QString step, stepNames)
			{
				++i;
				if (i < nbStep) // if not last substring (= menu)
				{
					// try to find an existing submenu with step name
					bool found = false;
					QList<QAction*> actions;
					if(i == 1) actions = menubar->actions();
					else actions = lastMenu->actions();
					foreach(QAction* action, actions)
					{
						QMenu* subMenu = action->menu();
						if (subMenu && subMenu->title() == step)
						{
							lastMenu = subMenu;
							found = true;
							break;
						}
					}
					if (!found)
					{
						QMenu* newMenu;
						if(i == 1)
						{
							newMenu = menubar->addMenu(step);
							newMenu->setParent(menubar);
						}
						else
						{
							newMenu = lastMenu->addMenu(step);
							newMenu->setParent(lastMenu);
						}
						lastMenu = newMenu;
					}
				}
				else // if last substring (= action name)
				{
					lastMenu->addAction(action);
					action->setText(step);
					action->setParent(lastMenu);
				}
			}
		}
937 938 939 940 941
		// just for update the menu in buggy Qt5 on macOS
		#if (defined CGOGN_APPLE) && ((QT_VERSION>>16) == 5)
		QMenu* fakemenu = menuBar()->addMenu("X");
		delete fakemenu;
		#endif
Pierre Kraemer's avatar
Pierre Kraemer committed
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
		m_pluginMenuActions[plugin].append(action);
	}
}

void SCHNApps::removeMenuAction(Plugin* plugin, QAction *action)
{
	if(action)
	{
		action->setEnabled(false);

		m_pluginMenuActions[plugin].removeOne(action);

		// parent of the action
		// which is an instance of QMenu if the action was created
		// using the addMenuActionMethod()
		QObject* parent = action->parent();
		delete action;

		while(parent != NULL)
		{
			QMenu* parentMenu = dynamic_cast<QMenu*>(parent);
			if(parentMenu && parentMenu->actions().empty())
			{
				parent = parent->parent();
				delete parentMenu;
			}
			else
				parent = NULL;
		}
	}
}





void SCHNApps::aboutSCHNApps()
{
	QString str("SCHNApps:\nS... CGoGN Holder for Nice Applications\n"
	            "Web site: http://cgogn.unistra.fr \n"
	            "Contact information: cgogn@unistra.fr");
Sylvain Thery's avatar
Sylvain Thery committed
983
	QMessageBox::about(this, "About SCHNApps", str);
Pierre Kraemer's avatar
Pierre Kraemer committed
984 985 986 987 988 989 990 991
}

void SCHNApps::aboutCGoGN()
{
	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");
Sylvain Thery's avatar
Sylvain Thery committed
992
	QMessageBox::about(this, "About CGoGN", str);
Pierre Kraemer's avatar
Pierre Kraemer committed
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
}

void SCHNApps::showHideControlDock()
{
	m_controlDock->setVisible(m_controlDock->isHidden());
}

void SCHNApps::showHidePluginDock()
{
	m_pluginDock->setVisible(m_pluginDock->isHidden());
}

void SCHNApps::showHidePythonDock()
{
	m_pythonDock->setVisible(m_pythonDock->isHidden());
}

Sylvain Thery's avatar
Sylvain Thery committed
1010

1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
void SCHNApps::setFloatingControlDock(bool f)
{
	m_controlDock->setFloating(f);
}

void SCHNApps::setFloatingPluginDock(bool f)
{
	m_pluginDock->setFloating(f);
}

void SCHNApps::setFloatingPythonDock(bool f)
{
	if (m_pythonDock->isVisible())
		m_pythonDock->setFloating(f);
}

Sylvain Thery's avatar
Sylvain Thery committed
1027 1028 1029

void SCHNApps::execPythonCmd(const QString& cmd)
{
1030
	m_pythonContext.evalScript(cmd);
Sylvain Thery's avatar
Sylvain Thery committed
1031 1032
}

1033
bool SCHNApps::loadPythonScriptFromFile(const QString& fileName)
Pierre Kraemer's avatar
Pierre Kraemer committed
1034
{
1035 1036
	QString fullName = fileName;
	if (!QFile::exists(fullName))
1037
	{
1038 1039 1040 1041 1042
		QString pypath = m_pyPathFile;
		if (pypath.isEmpty())
			pypath = this->getAppPath();
		fullName = pypath + "/" + fileName;
		if (!QFile::exists(fullName))
1043
		{
1044 1045
			std::cerr << "python script " << fullName.toStdString() << " not found" << std::endl;
			return false;
1046 1047
		}
	}
1048

1049 1050 1051 1052 1053
	QFile file(fullName);
	if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
		return false;
	QTextStream in(&file);
	m_pythonContext.evalScript(in.readAll());
1054 1055 1056 1057

// not using eval file because of pb with non ascii char in file name and path
//	m_pythonContext.evalFile(fullName);

1058
	return true;
Pierre Kraemer's avatar
Pierre Kraemer committed
1059 1060 1061 1062
}

void SCHNApps::loadPythonScriptFromFileDialog()
{
Sylvain Thery's avatar
Sylvain Thery committed
1063 1064 1065 1066 1067
	QString pypath = m_pyPathFile;
	if (pypath.isEmpty())
		pypath = this->getAppPath();

	QString fileName = QFileDialog::getOpenFileName(this, "Load Python script", pypath, "Python script (*.py)");
Pierre Kraemer's avatar
Pierre Kraemer committed
1068 1069 1070
	loadPythonScriptFromFile(fileName);
}

1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
QString SCHNApps::noBackSlash(const QString& name)
{
#ifdef WIN32
	// if copy from interface need to replace \ by / !!
	QString slashed = name;
	slashed.replace(QString("\t"), QString("/t"));
	slashed.replace(QString("\n"), QString("/n"));
	slashed.replace(QString("\v"), QString("/v"));
	slashed.replace(QString("\f"), QString("/f"));
	slashed.replace(QString("\r"), QString("/r"));
	slashed.replace(QChar('\\'), QChar('/'));
	return slashed;
#else
	return name;
#endif
}

1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106

std::string SCHNApps::niceStdString(const QString& qstr)
{
	return std::string(qstr.toLocal8Bit().data());
}

QString SCHNApps::forceASCII(const QString& qstr)
{
	QString str(qstr);

	for (QString::iterator it = str.begin(); it != str.end(); ++it)
	{
		if (it->unicode() > 127)
			*it = '_';
	}
	return str;
}


1107 1108 1109 1110 1111
void SCHNApps::setPythonPath(const QString& path)
{
	m_pyPathFile = SCHNApps::noBackSlash(path);
}

Sylvain Thery's avatar
Sylvain Thery committed
1112

1113
void SCHNApps::pyRecording()
Sylvain Thery's avatar
Sylvain Thery committed
1114
{
Sylvain Thery's avatar
Sylvain Thery committed
1115 1116 1117
	if (m_pyRecording != NULL) //  WRITE & CLOSE
	{
		QTextStream out(m_pyRecFile);
Sylvain Thery's avatar
Sylvain Thery committed
1118 1119 1120 1121
		foreach(QString var, m_pyVarNames)
		{
			m_pyBuffer.replace("\"" + var + "\"", var + ".getName()");
		}
1122

Sylvain Thery's avatar
Sylvain Thery committed
1123
		out << m_pyBuffer << endl;
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133

		// split view positions
		out << "schnapps.restoreSplitViewPositions(\"" << saveSplitViewPositions() << "\")" << endl;

		// cameras
		foreach(Camera* cam, m_cameras)
		{
			out << "schnapps.getCamera(\"" << cam->getName() << "\").fromString(\"" << cam->toString() << "\")" << endl;
		}

1134 1135 1136 1137 1138 1139
		// save frames of maps
		foreach(MapHandlerGen* mhg, m_maps)
		{
			out << mhg->getName() << ".frameFromString(\"" << mhg->frameToString() << "\")" << endl;
		}

1140 1141 1142
		//windows
		out << "schnapps.setWindowSize(" << this->width() << ", "<< this->height() << ")" << endl;

1143 1144 1145 1146 1147
		// docks
		out << "schnapps.setFloatingControlDock(" << m_controlDock->isFloating() << ")" << endl;
		out << "schnapps.setFloatingPluginDock(" << m_pluginDock->isFloating() << ")" << endl;
		out << "schnapps.setFloatingPythonDock(" << m_pythonDock->isFloating() << ")" << endl;

Sylvain Thery's avatar
Sylvain Thery committed
1148 1149 1150 1151 1152 1153 1154
		m_pyRecFile->close();

		delete m_pyRecording;
		delete m_pyRecFile;
		m_pyRecording = NULL;
		m_pyRecFile = NULL;
		statusbar->showMessage(QString(" Stop recording python"), 2000);
1155
		action_Python_Recording->setText("Python Recording");
Sylvain Thery's avatar
Sylvain Thery committed
1156 1157 1158
		return;
	}

Sylvain Thery's avatar
Sylvain Thery committed
1159 1160 1161 1162 1163
	QString pypath = m_pyPathFile;
	if (pypath.isEmpty())
		pypath = this->getAppPath();

	QString fileName = QFileDialog::getSaveFileName(this, "Save python script", pypath, " python script (*.py)");
Sylvain Thery's avatar
Sylvain Thery committed
1164 1165
	if (fileName.size() != 0)
	{
1166 1167 1168
		if (!fileName.endsWith(".py",Qt::CaseInsensitive))
			fileName += ".py";

Sylvain Thery's avatar
Sylvain Thery committed
1169 1170 1171
		m_pyRecFile = new QFile(fileName);
		if (!m_pyRecFile->open(QIODevice::WriteOnly | QIODevice::Text))
			return;
Sylvain Thery's avatar
Sylvain Thery committed
1172 1173 1174 1175

		m_pyBuffer.clear();
		m_pyRecording = new QTextStream(&m_pyBuffer);
		
Sylvain Thery's avatar
Sylvain Thery committed
1176 1177 1178

		foreach(View *v, m_views)
			*m_pyRecording << v->getName() << " = schnapps.getView(\"" << v->getName() << "\");" << endl;
Sylvain Thery's avatar
Sylvain Thery committed
1179 1180 1181

		foreach(MapHandlerGen *m, m_maps)
			*m_pyRecording << m->getName() << " = schnapps.getMap(\"" << m->getName() << "\");" << endl;
Sylvain Thery's avatar
Sylvain Thery committed
1182 1183

		statusbar->showMessage(QString(" Recording python in ") + fileName);
Sylvain Thery's avatar
Sylvain Thery committed
1184
		pythonVarsClear();
1185
		action_Python_Recording->setText("Stop Python Recording");
Sylvain Thery's avatar
Sylvain Thery committed
1186 1187 1188 1189 1190 1191 1192 1193 1194
	}
	else
	{
		m_pyRecFile = NULL;
		m_pyRecording = NULL;
		statusbar->showMessage(QString(" Cancel recording python"), 2000);
	}
}

Sylvain Thery's avatar
Sylvain Thery committed
1195 1196

void SCHNApps::appendPyRecording()
Sylvain Thery's avatar
Sylvain Thery committed
1197
{
Sylvain Thery's avatar
Sylvain Thery committed
1198 1199 1200
	if (m_pyRecording != NULL)	//  WRITE & CLOSE
	{
		QTextStream out(m_pyRecFile);
Sylvain Thery's avatar
Sylvain Thery committed
1201 1202 1203 1204 1205
		foreach(QString var, m_pyVarNames)
		{
			m_pyBuffer.replace("\"" + var + "\"", var + ".getName()");
		}
		out << m_pyBuffer << endl;
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215

		// split view positions
		out << "schnapps.restoreSplitViewPositions(\"" << saveSplitViewPositions() << "\")" << endl;

		// cameras
		foreach(Camera* cam, m_cameras)
		{
			out << "schnapps.getCamera(\"" << cam->getName() << "\").fromString(\"" << cam->toString() << "\")" << endl;
		}

1216 1217 1218 1219 1220 1221 1222
		// save frames of maps
		foreach(MapHandlerGen* mhg, m_maps)
		{
			out << mhg->getName() << ".frameFromString(\"" << mhg->frameToString() << "\")" << endl;
		}


1223 1224 1225
		//windows
		out << "schnapps.setWindowSize(" << this->width() << ", " << this->height() << ")" << endl;

Sylvain Thery's avatar
Sylvain Thery committed
1226 1227 1228 1229 1230 1231 1232
		m_pyRecFile->close();

		delete m_pyRecording;
		delete m_pyRecFile;
		m_pyRecording = NULL;
		m_pyRecFile = NULL;
		statusbar->showMessage(QString(" Stop recording python"), 2000);
1233
		action_Append_Python_Recording->setText("Append Python Recording");
Sylvain Thery's avatar
Sylvain Thery committed
1234
		return;
Sylvain Thery's avatar
Sylvain Thery committed
1235
	}
Sylvain Thery's avatar
Sylvain Thery committed
1236

Sylvain Thery's avatar
Sylvain Thery committed
1237 1238 1239 1240 1241
	QString pypath = m_pyPathFile;
	if (pypath.isEmpty())
		pypath = this->getAppPath();

	QString fileName = QFileDialog::getSaveFileName(this, "Append python script", pypath, " python script (*.py)");
Sylvain Thery's avatar
Sylvain Thery committed
1242 1243
	if (fileName.size() != 0)
	{
1244 1245 1246
		if (!fileName.endsWith(".py",Qt::CaseInsensitive))
			fileName += ".py";

Sylvain Thery's avatar
Sylvain Thery committed
1247 1248 1249
		m_pyRecFile = new QFile(fileName);
		if (!m_pyRecFile->open(QIODevice::Append | QIODevice::Text))
			return;	
Sylvain Thery's avatar
Sylvain Thery committed
1250 1251 1252 1253

		m_pyBuffer.clear();
		m_pyRecording = new QTextStream(&m_pyBuffer);
		pythonVarsClear();
Sylvain Thery's avatar
Sylvain Thery committed
1254

Sylvain Thery's avatar
Sylvain Thery committed
1255 1256
		foreach(View *v, m_views)
			*m_pyRecording << v->getName() << " = schnapps.getView(\"" << v->getName() << "\");" << endl;
Sylvain Thery's avatar
Sylvain Thery committed
1257

Sylvain Thery's avatar
Sylvain Thery committed
1258
		foreach(MapHandlerGen *m, m_maps)
Sylvain Thery's avatar
Sylvain Thery committed
1259 1260 1261 1262
		{
			*m_pyRecording << m->getName() << " = schnapps.getMap(\"" << m->getName() << "\");  #replace by  = previous_var if appending" << endl;
			pythonVarDeclare(m->getName());
		}
Sylvain Thery's avatar
Sylvain Thery committed
1263 1264

		statusbar->showMessage(QString(" Append recording python in ") + fileName);
1265
		action_Append_Python_Recording->setText("Stop Append Python Recording");
Sylvain Thery's avatar
Sylvain Thery committed
1266 1267 1268 1269 1270 1271 1272 1273
	}
	else
	{
		m_pyRecFile = NULL;
		m_pyRecording = NULL;
		statusbar->showMessage(QString(" Cancel recording python"), 2000);
	}
}
Sylvain Thery's avatar
Sylvain Thery committed
1274

Sylvain Thery's avatar
Sylvain Thery committed
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
bool SCHNApps::execPythonShortcut(quint64 key)
{
	if (m_pythonShortCuts.contains(key))
	{
		execPythonCmd(m_pythonShortCuts[key]);
		return true;
	}
	return false;
}


void SCHNApps::setPythonShortcut(const QString& keys, const QString& command)
{
	quint64 k = 0;

	if (keys.length() == 0)
		return;

	if (keys.length() == 1)
	{
Sylvain Thery's avatar
Sylvain Thery committed
1295
		k = keys[0].toUpper().unicode();
Sylvain Thery's avatar
Sylvain Thery committed
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
	}
	else
	{
		if (keys.contains("control", Qt::CaseInsensitive))
			k |= Qt::ControlModifier;
		if (keys.contains("shift", Qt::CaseInsensitive))
			k |= Qt::ShiftModifier;
		if (keys.contains("alt", Qt::CaseInsensitive))
			k |= Qt::AltModifier;
		if (keys.contains("meta", Qt::CaseInsensitive))
			k |= Qt::MetaModifier;
		if (keys.contains("keypad", Qt::CaseInsensitive))
			k |= Qt::KeypadModifier;
		k <<= 32;

Sylvain Thery's avatar
Sylvain Thery committed
1311
		QChar c = keys[keys.length() - 1].toUpper();
Sylvain Thery's avatar
Sylvain Thery committed
1312 1313 1314 1315 1316 1317 1318 1319 1320
		k |= c.unicode();
	}

	if (command.length() > 0)
		m_pythonShortCuts[k] = command;
	else
		m_pythonShortCuts.erase(m_pythonShortCuts.find(k));
}

Sylvain Thery's avatar
Sylvain Thery committed
1321

Sylvain Thery's avatar
Sylvain Thery committed
1322 1323
void SCHNApps::closeEvent(QCloseEvent *event)
{
1324
	DEBUG_EMIT("schnappsClosing");
1325 1326 1327 1328
	if (m_pyRecording != NULL)	//  WRITE & CLOSE
	{
		pyRecording();
	}
1329
	emit(schnappsClosing());
Sylvain Thery's avatar
Sylvain Thery committed
1330 1331 1332
	QMainWindow::closeEvent(event);
}

Sylvain Thery's avatar
Sylvain Thery committed
1333 1334 1335 1336
void SCHNApps::statusBarMessage(const QString& msg, int msec)
{
	statusbar->showMessage(msg, msec);
}
Sylvain Thery's avatar
Sylvain Thery committed
1337

1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350

QString SCHNApps::openFileDialog(const QString& title, const QString& dir, const QString& filter)
{
	return QFileDialog::getOpenFileName(this, title, dir, filter);
}


QString SCHNApps::saveFileDialog(const QString& title, const QString& dir, const QString& filter)
{
	return QFileDialog::getSaveFileName(this, title, dir, filter);
}


Sylvain Thery's avatar
Sylvain Thery committed
1351 1352 1353 1354 1355
void SCHNApps::cleanAll()
{
	//remove views
	foreach(View* v, m_views)
	{
Sylvain Thery's avatar
Sylvain Thery committed
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
		removeView(v->getName());
	}

	//  set the camera to camera_0 
	m_firstView->setCurrentCamera(QString("camera_0"));

	// change name of first view to view 0 if needed
	if (m_firstView->m_name != QString("view_0"))
	{
		m_views.remove(m_firstView->m_name);
		m_firstView->m_name = QString("view_0");
		m_views.insert(m_firstView->m_name, m_firstView);
Sylvain Thery's avatar
Sylvain Thery committed
1368 1369 1370 1371 1372
	}

	//remove cameras
	foreach(Camera* cam, m_cameras)
	{
Sylvain Thery's avatar
Sylvain Thery committed
1373
		removeCamera(cam->getName());
Sylvain Thery's avatar
Sylvain Thery committed
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
	}

	//remove maps
	foreach(MapHandlerGen* m, m_maps)
	{
		removeMap(m->getName());
	}

	// remove plugins
	foreach(Plugin* p, m_plugins)
	{
		disablePlugin(p->getName());
	}
Sylvain Thery's avatar
Sylvain Thery committed
1387 1388 1389

	Camera::cameraCount = 1;
	View::viewCount = 1;
Sylvain Thery's avatar
Sylvain Thery committed
1390 1391

	m_pluginDock->setVisible(false);
Sylvain Thery's avatar
Sylvain Thery committed
1392
}
1393 1394


Pierre Kraemer's avatar
Pierre Kraemer committed
1395 1396 1397
} // namespace SCHNApps

} // namespace CGoGN