schnapps.cpp 22.8 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
14
#include <QFile>

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
41
42
43
namespace CGoGN
{

namespace SCHNApps
{

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

Pierre Kraemer's avatar
Pierre Kraemer committed
50
51
52
53
54
55
56
	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);
57
	m_controlDock->setMaximumWidth(250);
Pierre Kraemer's avatar
Pierre Kraemer committed
58
59
60
61
62

	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
63
	m_controlDockTabWidget->setMovable(true);
Pierre Kraemer's avatar
Pierre Kraemer committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

	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
88
	m_pluginDockTabWidget->setMovable(true);
Pierre Kraemer's avatar
Pierre Kraemer committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108

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

Sylvain Thery's avatar
Sylvain Thery committed
109
110
111
112
113
114
115
	connect(action_Python_Recording, SIGNAL(triggered()), this, SLOT(beginPyRecording()));
	connect(action_Append_Python_Recording, SIGNAL(triggered()), this, SLOT(appendPyRecording()));

	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
116
117


118
	// create & setup central widget (views)
Pierre Kraemer's avatar
Pierre Kraemer committed
119
120
121
122
123
124
	m_centralLayout = new QVBoxLayout(centralwidget);

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

Sylvain Thery's avatar
Sylvain Thery committed
125
126
127
128
129
130
131
132
133
	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
134
135
136
137
138
139
140
141
142
143
	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
144
145
//	registerPluginsDirectory(m_appPath + QString("/../lib"));

146
	// 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
147
	registerPluginsDirectory(QString(PLUGINS_DEFAULT_DIRECTORY));
Pierre Kraemer's avatar
Pierre Kraemer committed
148
149
150
151
}

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

/*********************************************************
 * 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);
167
	DEBUG_EMIT("cameraAdded");
Pierre Kraemer's avatar
Pierre Kraemer committed
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
	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);
183
		DEBUG_EMIT("cameraRemoved");
Pierre Kraemer's avatar
Pierre Kraemer committed
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
		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
201
202
203
204
205
206
207
208
void SCHNApps::redrawAllViews()
{
	foreach(View *v, m_views)
	{
		v->updateGL();
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
209
210
211
212
213
View* SCHNApps::addView(const QString& name)
{
	if (m_views.contains(name))
		return NULL;

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

Pierre Kraemer's avatar
Pierre Kraemer committed
216
217
	m_views.insert(name, view);

218
	DEBUG_EMIT("viewAdded");
Pierre Kraemer's avatar
Pierre Kraemer committed
219
220
	emit(viewAdded(view));

Sylvain Thery's avatar
Sylvain Thery committed
221
222
223
224
	// RECORDING
	if (m_pyRecording)
		*m_pyRecording << view->getName() << " = schnapps.getView(\"" << view->getName() << "\");" << endl;

Pierre Kraemer's avatar
Pierre Kraemer committed
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
	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);

259
			DEBUG_EMIT("viewRemoved");
Pierre Kraemer's avatar
Pierre Kraemer committed
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
			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)
{
277
278
	int currentTab = m_pluginDockTabWidget->currentIndex();

279
280
281
282
283
284
285
	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
286
287
288

	View* oldSelected = m_selectedView;
	m_selectedView = view;
Sylvain Thery's avatar
Sylvain Thery committed
289
290
	if (oldSelected)
		oldSelected->hideDialogs();
Pierre Kraemer's avatar
Pierre Kraemer committed
291

292
293
294
295
	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
296

297
298
	m_pluginDockTabWidget->setCurrentIndex(currentTab);

299
	DEBUG_EMIT("selectedViewChanged");
Pierre Kraemer's avatar
Pierre Kraemer committed
300
301
302
303
304
305
306
307
308
	emit(selectedViewChanged(oldSelected, m_selectedView));

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

void SCHNApps::splitView(const QString& name, Qt::Orientation orientation)
{
Sylvain Thery's avatar
Sylvain Thery committed
309
310
311
312
	// RECORDING
	if (m_pyRecording)
		*m_pyRecording << "schnapps.splitView(\"" << name << "\", " << orientation << ");" << endl;

Pierre Kraemer's avatar
Pierre Kraemer committed
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
	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;
	}
	if(parent->orientation() == orientation)
		parent->insertWidget(parent->indexOf(view)+1, newView);
	else
	{
		int idx = parent->indexOf(view);
		view->setParent(NULL);
		QSplitter* spl = new QSplitter(orientation);
		spl->addWidget(view);
		spl->addWidget(newView);
		parent->insertWidget(idx, spl);
	}
Sylvain Thery's avatar
Sylvain Thery committed
333
	
Pierre Kraemer's avatar
Pierre Kraemer committed
334
335
336
337
338
339
340
341
}

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

void SCHNApps::registerPluginsDirectory(const QString& path)
{
342
343
344

#ifdef WIN32
#ifdef _DEBUG
345
	QDir directory(path+QString("Debug/"));
346
347
348
349
#else
	QDir directory(path + QString("Release/"));
#endif
#else
Pierre Kraemer's avatar
Pierre Kraemer committed
350
	QDir directory(path);
351
#endif
Pierre Kraemer's avatar
Pierre Kraemer committed
352
353
354
355
356
357
358
359
360
361
362
363
	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
364
365
366
#ifdef WIN32
			QString pluginName = pfi.baseName();
#else
Pierre Kraemer's avatar
Pierre Kraemer committed
367
			QString pluginName = pfi.baseName().remove(0, 3);
Sylvain Thery's avatar
Sylvain Thery committed
368
#endif
Pierre Kraemer's avatar
Pierre Kraemer committed
369
370
371
372
373
			QString pluginFilePath = directory.absoluteFilePath(pluginFile);

			if(!m_availablePlugins.contains(pluginName))
			{
				m_availablePlugins.insert(pluginName, pluginFilePath);
374
				DEBUG_EMIT("pluginAvailableAdded");
Pierre Kraemer's avatar
Pierre Kraemer committed
375
376
377
378
379
380
381
382
				emit(pluginAvailableAdded(pluginName));
			}
		}
	}
}

Plugin* SCHNApps::enablePlugin(const QString& pluginName)
{
Sylvain Thery's avatar
Sylvain Thery committed
383
384
385
386
	// RECORDING
	if (m_pyRecording)
		*m_pyRecording << pluginName << " = schnapps.enablePlugin(\"" << pluginName << "\");" << endl;

Pierre Kraemer's avatar
Pierre Kraemer committed
387
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
	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);
413
				DEBUG_EMIT("pluginEnabled");
Pierre Kraemer's avatar
Pierre Kraemer committed
414
				emit(pluginEnabled(plugin));
Sylvain Thery's avatar
Sylvain Thery committed
415
//				menubar->repaint();
Pierre Kraemer's avatar
Pierre Kraemer committed
416
417
418
419
420
421
422
423
424
425
426
427
				// method success
				return plugin;
			}
			else
			{
				delete plugin;
				return NULL;
			}
		}
		// if loading fails
		else
		{
428
			std::cout << "loader.instance() failed.." << std::endl;
Pierre Kraemer's avatar
Pierre Kraemer committed
429
430
431
432
433
434
435
436
437
438
439
440
441
			return NULL;
		}
	}
	else
	{
		return NULL;
	}
}

void SCHNApps::disablePlugin(const QString& pluginName)
{
	if (m_plugins.contains(pluginName))
	{
Sylvain Thery's avatar
Sylvain Thery committed
442
443
444
445
		// RECORDING
		if (m_pyRecording)
			*m_pyRecording << "schnapps.disablePlugin(\"" << pluginName << "\");" << endl;

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
468
469
470
		Plugin* plugin = m_plugins[pluginName];

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

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

		QPluginLoader loader(plugin->getFilePath());
		loader.unload();

		statusbar->showMessage(pluginName + QString(" successfully unloaded."), 2000);
471
		DEBUG_EMIT("pluginDisabled");
Pierre Kraemer's avatar
Pierre Kraemer committed
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
		emit(pluginDisabled(plugin));

		delete plugin;
	}
}

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);
494
495
496
497
498
499
500
501
502

		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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520

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

521
522
523
524
525
526
527
528
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
529

530
531
532
533
534
535
536
537
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564

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

MapHandlerGen* SCHNApps::addMap(const QString& name, unsigned int dim)
{
	if (m_maps.contains(name))
		return NULL;

	MapHandlerGen* mh = NULL;
	switch(dim)
	{
		case 2 : {
			PFP2::MAP* map = new PFP2::MAP();
			mh = new MapHandler<PFP2>(name, this, map);
			break;
		}
		case 3 : {
			PFP3::MAP* map = new PFP3::MAP();
			mh = new MapHandler<PFP3>(name, this, map);
			break;
		}
	}

	m_maps.insert(name, mh);

565
	DEBUG_EMIT("mapAdded");
Pierre Kraemer's avatar
Pierre Kraemer committed
566
567
568
569
570
571
572
573
574
575
576
	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())
577
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
578
			view->unlinkMap(map);
579
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
580
581
582

		m_maps.remove(name);

583
		DEBUG_EMIT("mapRemoved");
Pierre Kraemer's avatar
Pierre Kraemer committed
584
585
		emit(mapRemoved(map));

586
587
588
589
		// unselect map if it is removed
		if (this->getSelectedMap() == map)
			setSelectedMap(QString("NONE"));

Pierre Kraemer's avatar
Pierre Kraemer committed
590
591
592
593
		delete map;
	}
}

594
595
596
597
598
void SCHNApps::setSelectedMap(const QString& mapName)
{
	m_controlMapTab->setSelectedMap(mapName);
}

Pierre Kraemer's avatar
Pierre Kraemer committed
599
600
601
602
603
604
605
606
607
608
609
610
611
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();
}

612
613
614
615
616
617
618
619
620
621
unsigned int SCHNApps::getCurrentOrbit() const
{
	return m_controlMapTab->getCurrentOrbit();
}

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

Pierre Kraemer's avatar
Pierre Kraemer committed
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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
/*********************************************************
 * 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);
				}
			}
		}
Sylvain Thery's avatar
Sylvain Thery committed
726
727
728
729
730
		// 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
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
		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
772
	QMessageBox::about(this, "About SCHNApps", str);
Pierre Kraemer's avatar
Pierre Kraemer committed
773
774
775
776
777
778
779
780
}

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
781
	QMessageBox::about(this, "About CGoGN", str);
Pierre Kraemer's avatar
Pierre Kraemer committed
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
}

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

void SCHNApps::loadPythonScriptFromFile(const QString& fileName)
{
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
#ifdef WIN32
	// NOT VERY NICE BUT ONLY WAY ON WINDOWS TO LOAD SCRIPT FILES
	QFile file(fileName);
	if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
		return;
	QTextStream in(&file);
	while (!in.atEnd())
	{
		QString line = in.readLine();
		if (line.size() >= 2)
		{
			int j = 0;
			while (line[j] == ' ') ++j;
			QString spaces(4 - j, QChar(' ')); // need 4 spaces at begin of line
			line.replace(QChar('\\'), QChar('/')); // replace \ by / in win path
			m_pythonConsole.consoleMessage(spaces + line);
			m_pythonConsole.executeLine(false);
		}
	}
#else
Pierre Kraemer's avatar
Pierre Kraemer committed
821
822
823
	QFileInfo fi(fileName);
	if(fi.exists())
		m_pythonContext.evalFile(fi.filePath());
824
#endif
Pierre Kraemer's avatar
Pierre Kraemer committed
825
826
827
828
829
830
831
832
}

void SCHNApps::loadPythonScriptFromFileDialog()
{
	QString fileName = QFileDialog::getOpenFileName(this, "Load Python script", getAppPath(), "Python script (*.py)");
	loadPythonScriptFromFile(fileName);
}

Sylvain Thery's avatar
Sylvain Thery committed
833
834
835

void SCHNApps::beginPyRecording()
{
Sylvain Thery's avatar
Sylvain Thery committed
836
837
838
839
840
841
842
843
844
845
846
847
848
849
	if (m_pyRecording != NULL) //  WRITE & CLOSE
	{
		QTextStream out(m_pyRecFile);
		out << m_pyRecording->readAll();
		m_pyRecFile->close();

		delete m_pyRecording;
		delete m_pyRecFile;
		m_pyRecording = NULL;
		m_pyRecFile = NULL;
		statusbar->showMessage(QString(" Stop recording python"), 2000);
		return;
	}

Sylvain Thery's avatar
Sylvain Thery committed
850
851
852
853
854
855
856
857
858
859
	QString fileName = QFileDialog::getSaveFileName(this, "Save python script", this->getAppPath(), " python script (*.py)");
	if (fileName.size() != 0)
	{
		m_pyRecFile = new QFile(fileName);
		if (!m_pyRecFile->open(QIODevice::WriteOnly | QIODevice::Text))
			return;
		m_pyRecording = new QTextStream(m_pyRecFile);

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

		foreach(MapHandlerGen *m, m_maps)
			*m_pyRecording << m->getName() << " = schnapps.getMap(\"" << m->getName() << "\");" << endl;
Sylvain Thery's avatar
Sylvain Thery committed
863
864
865
866
867
868
869
870
871
872
873

		statusbar->showMessage(QString(" Recording python in ") + fileName);
	}
	else
	{
		m_pyRecFile = NULL;
		m_pyRecording = NULL;
		statusbar->showMessage(QString(" Cancel recording python"), 2000);
	}
}

Sylvain Thery's avatar
Sylvain Thery committed
874
875

void SCHNApps::appendPyRecording()
Sylvain Thery's avatar
Sylvain Thery committed
876
{
Sylvain Thery's avatar
Sylvain Thery committed
877
878
879
880
881
882
883
884
885
886
887
	if (m_pyRecording != NULL)	//  WRITE & CLOSE
	{
		QTextStream out(m_pyRecFile);
		out << m_pyRecording->readAll();
		m_pyRecFile->close();

		delete m_pyRecording;
		delete m_pyRecFile;
		m_pyRecording = NULL;
		m_pyRecFile = NULL;
		statusbar->showMessage(QString(" Stop recording python"), 2000);
Sylvain Thery's avatar
Sylvain Thery committed
888
		return;
Sylvain Thery's avatar
Sylvain Thery committed
889
	}
Sylvain Thery's avatar
Sylvain Thery committed
890

Sylvain Thery's avatar
Sylvain Thery committed
891
892
893
894
895
896
897
	QString fileName = QFileDialog::getSaveFileName(this, "Save python script", this->getAppPath(), " python script (*.py)");
	if (fileName.size() != 0)
	{
		m_pyRecFile = new QFile(fileName);
		if (!m_pyRecFile->open(QIODevice::Append | QIODevice::Text))
			return;	
		m_pyRecording = new QTextStream(m_pyRecFile);
Sylvain Thery's avatar
Sylvain Thery committed
898

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

Sylvain Thery's avatar
Sylvain Thery committed
902
903
904
905
906
907
908
909
910
911
912
913
		foreach(MapHandlerGen *m, m_maps)
			*m_pyRecording << m->getName() << " = schnapps.getMap(\"" << m->getName() << "\");" << endl;

		statusbar->showMessage(QString(" Append recording python in ") + fileName);
	}
	else
	{
		m_pyRecFile = NULL;
		m_pyRecording = NULL;
		statusbar->showMessage(QString(" Cancel recording python"), 2000);
	}
}
Sylvain Thery's avatar
Sylvain Thery committed
914
915


Sylvain Thery's avatar
Sylvain Thery committed
916
917
void SCHNApps::closeEvent(QCloseEvent *event)
{
918
	DEBUG_EMIT("schnappsClosing");
919
	emit(schnappsClosing());
Sylvain Thery's avatar
Sylvain Thery committed
920
921
922
	QMainWindow::closeEvent(event);
}

Sylvain Thery's avatar
Sylvain Thery committed
923

Pierre Kraemer's avatar
Pierre Kraemer committed
924
925
926
} // namespace SCHNApps

} // namespace CGoGN