File: demoTutorialDialog.C

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 239,888 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; makefile: 95
file content (987 lines) | stat: -rw-r--r-- 25,935 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
114
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
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
471
472
473
474
475
476
477
478
479
480
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
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
726
727
728
729
730
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
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
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
937
938
939
940
941
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
983
984
985
986
987
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//

#include "demoTutorialDialog.h"
#include "mainframe.h"

#include <BALL/VIEW/KERNEL/message.h>

#include <BALL/VIEW/DIALOGS/displayProperties.h>
#include <BALL/VIEW/DIALOGS/FDPBDialog.h>
#include <BALL/VIEW/DIALOGS/modifyRepresentationDialog.h>
#include <BALL/VIEW/DIALOGS/molecularFileDialog.h>
#include <BALL/VIEW/DIALOGS/lightSettings.h>
#include <BALL/VIEW/DIALOGS/stageSettings.h>

#include <BALL/VIEW/DATATYPE/standardDatasets.h>

#include <BALL/VIEW/WIDGETS/molecularStructure.h>
#include <BALL/VIEW/WIDGETS/logView.h>
#include <BALL/VIEW/WIDGETS/datasetControl.h>
#include <BALL/VIEW/WIDGETS/molecularControl.h>
#include <BALL/VIEW/WIDGETS/geometricControl.h>
#include <BALL/SYSTEM/path.h>

#include <QtWidgets/QPushButton>
#include <QtWidgets/QMessageBox>
#include <QtWidgets/QTextBrowser>

using namespace std;

namespace BALL
{
	namespace VIEW
	{

enum TutorialSteps
{
	TUTORIAL_PEPTIDE = 1,
	TUTORIAL_ROTATE,
	TUTORIAL_HIERARCHY,
	TUTORIAL_MDS,
	TUTORIAL_TRAJECTORY,
	TUTORIAL_ES,
	TUTORIAL_SES,
	TUTORIAL_SES_COLORING,
	TUTORIAL_CS
};


DemoTutorialDialog::DemoTutorialDialog(QWidget* parent, const char* name)
	: QDialog(parent),
	  Ui_DemoTutorialDialogData(),
	  ModularWidget(name),
	  tutorial_type_(DEMO),
	  composites_(),
	  grid_(nullptr),
	  system_(nullptr),
	  prefix_(""),
	  current_step_(0),
	  surface_(nullptr),
	  demo_action_(nullptr),
	  tutorial_action_(nullptr),
	  raytracing_tutorial_action_(nullptr)
{
#ifdef BALL_VIEW_DEBUG
	Log.error() << "new DemoTutorialDialog " << this << std::endl;
#endif

	setupUi(this);
	setObjectName(name);

	// register the widget with the MainControl
 	ModularWidget::registerWidget(this);
	hide();
	connect(next_button, SIGNAL(clicked()), this, SLOT(nextStepClicked()));
	connect(cancel_button, SIGNAL(clicked()), this, SLOT(hide()));
}

DemoTutorialDialog::~DemoTutorialDialog()
{
#ifdef BALL_VIEW_DEBUG
	Log.error() << "deleting DemoTutorialDialog " << this << std::endl;
#endif

	delete surface_;
}

void DemoTutorialDialog::initDemo_()
{
	setWindowTitle(tr("BALLView Demo"));

	prefix_ = getBaseDir_() + "demo";

	next_button->setEnabled(true);
	QDialog::show();
	raise();
	move(20,20);

	// hide some dockwidgets
	if (LogView::getInstance(0) != 0) LogView::getInstance(0)->hide();
 	if (DatasetControl::getInstance(0) != 0) DatasetControl::getInstance(0)->hide();

#ifdef	BALL_HAS_RTFACT
	// Set the background to black
	ColorRGBA color(0, 0, 0, 255);
	Stage* stage = Scene::getInstance(0)->getStage();
		
	stage->setBackgroundColor(color);
	StageSettings* stage_settings = Scene::getInstance(0)->getStageSettings();
	stage_settings->updateFromStage();

	//TODO: get rid of this hack. 
	//TODO: BALLView should come up with good light settings by itself!
	// set a good light source	
	stage->clearLightSources();

	LightSource ls;
		
	ls.setPosition(Vector3(1, -2, -15));
	ls.setAttenuation(Vector3(0., 0., 0.3));
	ls.setType(LightSource::POSITIONAL);
	ls.setColor(ColorRGBA(255, 255, 255, 255));
	ls.setIntensity(500./100);
	stage->addLightSource(ls);
	LightSettings::getInstance(0)->updateFromStage();
	// apply everything to the scene...
	Scene::getInstance(0)->applyPreferences();
#endif
}

String DemoTutorialDialog::getBaseDir_()
{
	Path p;
	return p.find(String("BALLView") + FileSystem::PATH_SEPARATOR + "html" + FileSystem::PATH_SEPARATOR);
}

void DemoTutorialDialog::initTutorials_()
{
	if (tutorial_type_ == TUTORIAL)
	{
		setWindowTitle(tr("BALLView Tutorial"));
		prefix_ = getBaseDir_() + "tutorial";
	}
	else if (tutorial_type_ == RAYTRACING_TUTORIAL)
	{
		setWindowTitle(tr("Ray tracing Tutorial"));
		prefix_ = getBaseDir_() + "raytracing_tutorial";
	}
	
	next_button->setEnabled(false);

#ifdef BALL_HAS_RTFACT
	// Set the defaults
	((Mainframe*)getMainControl())->reset();

	Scene::getInstance(0)->show();
	MolecularControl::getInstance(0)->show();
	MolecularControl::getInstance(0)->setFloating(false);
	MolecularControl::getInstance(0)->applyPreferences();
 	GeometricControl::getInstance(0)->show();
	GeometricControl::getInstance(0)->applyPreferences();
	GeometricControl::getInstance(0)->setFloating(false);

	// Set the background to black
	ColorRGBA color(0, 0, 0, 255);
	Stage* stage = Scene::getInstance(0)->getStage();
		
	stage->setBackgroundColor(color);

	StageSettings* stage_settings = Scene::getInstance(0)->getStageSettings();
	stage_settings->updateFromStage();

	// get one useable light source
	stage->clearLightSources();

	LightSource ls;
		
	ls.setPosition(Vector3(1, -2, -15));
	ls.setAttenuation(Vector3(0., 0., 0.7));
	ls.setType(LightSource::POSITIONAL);
	ls.setColor(ColorRGBA(255, 255, 255, 255));
	ls.setIntensity(500./100);

	stage->addLightSource(ls);
	LightSettings::getInstance(0)->updateFromStage();
#endif
	if (tutorial_type_ == TUTORIAL)
	{
		DatasetControl::getInstance(0)->show();
 		DatasetControl::getInstance(0)->applyPreferences();
 		DatasetControl::getInstance(0)->setFloating(false);
	}
	else if  (tutorial_type_ == RAYTRACING_TUTORIAL)
	{ 
#ifdef BALL_HAS_RTFACT
		Stage::Material& rt_material = stage->getMaterial();

		rt_material.ambient_color = ColorRGBA(255, 255, 255, 255);
		rt_material.ambient_intensity = 0.;

		rt_material.specular_color = ColorRGBA(255, 255, 255, 255);
		rt_material.specular_intensity = 1.;

		rt_material.reflective_color = ColorRGBA(255, 255, 255, 255);
		rt_material.reflective_intensity = 0.;

		rt_material.shininess = 75.031;
		rt_material.transparency = 0;

		Scene::getInstance(0)->getMaterialSettings()->updateDefaultMaterialsFromStage();

		// set ball and stick as next model
		DisplayProperties::getInstance(0)->selectModel(MODEL_BALL_AND_STICK);

		// apply everything to the scene...
		Scene::getInstance(0)->applyPreferences();

		next_button->setEnabled(true);
#else
		Log.info() << "DemoTutorialDialog: no RTFact available! Close the dialog!" << __FILE__ << " " << __LINE__ << endl;
		return;
#endif
	}
	LogView::getInstance(0)->hide();
}

void DemoTutorialDialog::show()
{
	current_step_ = 1;

	if (tutorial_type_ == DEMO)
	{
		initDemo_();
	}
	else  
	{
		int result = QMessageBox::question(this, tr("Warning"),
				tr("To start the tutorial, all loaded structures and molecules will be deleted."),
				QMessageBox::Ok| QMessageBox::Cancel, QMessageBox::Ok);
		if (result != QMessageBox::Ok) return;
		
		initTutorials_();
	}
	
	QUrl qurl = QUrl::fromLocalFile((prefix_ + "01.html").c_str());
	text_browser->setSource(qurl);

	QDialog::show();
	resize(350, 650);
	raise();
}

void DemoTutorialDialog::onNotify(Message* message)
{
	if (!isVisible()) return;

	switch (tutorial_type_)
	{
		case DEMO: 
			onNotifyDemo_(message);
			break;
		case TUTORIAL: 
			onNotifyTutorial_(message);
			break;
		case RAYTRACING_TUTORIAL:
			onNotifyRaytracingTutorial_(message);
			break;
	}
}

void DemoTutorialDialog::onNotifyDemo_(Message *message)
{
	RepresentationMessage* rmsg = RTTI::castTo<RepresentationMessage>(*message);

	if (current_step_ == 13 || current_step_ == 14)
	{
		if (!RTTI::isKindOf<FinishedSimulationMessage>(message)) return;
	}
	else if (current_step_ == 15)
	{
		DatasetMessage* msg = RTTI::castTo<DatasetMessage>(*message);
		if (!msg) return;

		if (!msg->getDataset())
		{
			BALLVIEW_DEBUG
			return;
		}

		RegularData3DDataset* set = dynamic_cast<RegularData3DDataset*>(msg->getDataset());
		if (set->getType() != RegularData3DController::type) return;

		grid_ = set->getData();
	}
	else if (current_step_ == 16)
	{
		SceneMessage* msg = RTTI::castTo<SceneMessage>(*message);
		if (!msg || msg->getType() != SceneMessage::REBUILD_DISPLAY_LISTS)
		{
			return;
		}
	}
	else if (!rmsg || rmsg->getType() != RepresentationMessage::UPDATE)
	{
		return;
	}

	enableNextStep_();
}


void DemoTutorialDialog::enableNextStep_()
{
	next_button->setEnabled(true);
}

// TODO: split into several functions...
void DemoTutorialDialog::nextStepClicked()
{
	String id = String(current_step_ + 1); //TODO WHY??
	if (id.size() == 1) id = "0" + id;

	id = prefix_ + id + ".html";

	QUrl qurl = QUrl::fromLocalFile(id.c_str());
	text_browser->setSource(qurl);
	next_button->setEnabled(false);

	if (tutorial_type_ == DEMO) 
	{
		if (current_step_ == 17)
		{
			showTutorial();
			return;
		}

		nextStepDemo_();
		
		#ifdef BALL_HAS_RTFACT
		// we do not want to show energy minimization and MD simulation
		if (current_step_ == 11)
		{
			current_step_ +=2;	
			next_button->setEnabled(true);
		}
		#endif

	}
	else if (tutorial_type_ == TUTORIAL) 
	{
		if (current_step_ == 8)
		{
			next_button->setEnabled(true);
		}
	}
	else if (tutorial_type_ == RAYTRACING_TUTORIAL)
	{
		switch (current_step_)
		{
			case 2:
			{
				// prepare the background for the next step
				ColorRGBA color(255, 255, 255, 255); // white
				Scene::getInstance(0)->getStage()->setBackgroundColor(color);
				StageSettings* stage_settings = Scene::getInstance(0)->getStageSettings();
				stage_settings->updateFromStage();
				Scene::getInstance(0)->applyPreferences();
				break;
			}
			
			case 4: // preparing downgraded light settings 
			{
				// There should be just a single light source!
				// first manipulate the light
				LightSource& ls = Scene::getInstance(0)->getStage()->getLightSource(0);
				ls.setPosition(Vector3(1, -8, -45));        //(Vector3(1, -2, -15));
				ls.setAttenuation(Vector3(0, 0, 0.2f));    //0.7
				ls.setType(LightSource::POSITIONAL);
				ls.setColor(ColorRGBA(255, 255, 255, 255)); //ColorRGBA(255, 245, 208, 255));
				ls.setIntensity(0.25f);
				LightSettings::getInstance(0)->updateFromStage();

				// then change the camera position
				Camera& camera = Scene::getInstance(0)->getStage()->getCamera();
				camera.setViewPoint(camera.getViewPoint()+Vector3(20,20,20));

				// update everything
				Scene::getInstance(0)->applyPreferences();
				break;
			}
			case 5: //preparing bad materials
			{
				// Add a plane to be used as a mirror
				if (getMainControl()->getCompositeManager().getComposites().size() == 0)
				{
					Log.info() << "DemoTutorialDialog: no system available! " << __FILE__ << " " << __LINE__ << endl;
				 	return;	
				}
				
				addPlane_('x', 5, 5);
				break;
			}

			default: // nothing to see here...
				break;
		}
	}
 
	current_step_++;		
}

void  DemoTutorialDialog::addPlane_(char plane_specifier, int height, int boundary, bool bottom)
{
	HashSet<Composite*> composites = MainControl::getInstance(0)->getCompositeManager().getComposites();
	HashSet<Composite*>::Iterator sit = composites.begin();

	BoundingBoxProcessor bbp;
	Vector3 v_low(0, 0, 0);
	Vector3 v_upp(0, 0, 0);

	System* system = dynamic_cast<System*>(*sit);
	if (system)
	{
		system->apply(bbp);
		v_low = Vector3(bbp.getLower().x, bbp.getLower().y, bbp.getLower().z);
		v_upp = Vector3(bbp.getUpper().x, bbp.getUpper().y, bbp.getUpper().z);
	}
	else
	{
		Log.info() << "DemoTutorialDialog.addPlane(): No system given! "<< __FILE__ << " " << __LINE__ << endl;
		return;
	}

	++sit;
	for (; +sit; ++sit)
	{
		system = dynamic_cast<System*>(*sit);
		if (system)
		{
			system->apply(bbp);
			Vector3 low = Vector3(bbp.getLower().x, bbp.getLower().y, bbp.getLower().z);
			Vector3 upp = Vector3(bbp.getUpper().x, bbp.getUpper().y, bbp.getUpper().z);

			// find the boundaries over all systems	
			if (v_low.x > low.x) v_low.x = low.x;
			if (v_low.y > low.y) v_low.y = low.y; 
			if (v_low.z > low.z) v_low.z = low.z;

			if (v_upp.x < upp.x) v_upp.x = upp.x; 
			if (v_upp.y < upp.y) v_upp.y = upp.y;
			if (v_upp.z < upp.z) v_upp.z = upp.z;

		}
	}

 	if (! bottom)
	{ 
		Vector3 v_tmp = v_low;
		v_low = v_upp;
		v_upp = v_tmp;
		height = height*(-1);
		boundary = boundary*(-1);
	}

	Vector3 v_low_left (0, 0, 0);
	Vector3 v_low_right(0, 0, 0);
	Vector3 v_upp_right(0, 0, 0);
	Vector3 v_upp_left (0, 0, 0);

	Vector3 normal(0, 0, 0);

	
	if (plane_specifier == 'x')
	{
		v_low       = v_low - Vector3(height, boundary, boundary);
		v_upp       = v_upp + Vector3(height, boundary, boundary);
		v_low_left  = Vector3(v_low.x, v_low.y, v_low.z);
		v_low_right = Vector3(v_low.x, v_upp.y, v_low.z);
		v_upp_right = Vector3(v_low.x, v_upp.y, v_upp.z);
		v_upp_left  = Vector3(v_low.x, v_low.y, v_upp.z);
		normal      = Vector3(1, 0, 0);
	}
	else if (plane_specifier == 'y')
	{ 
		v_low       = v_low - Vector3(boundary, height, boundary);
		v_upp       = v_upp + Vector3(boundary, height, boundary);
		v_low_left  = Vector3(v_low.x, v_low.y, v_low.z);
		v_low_right = Vector3(v_low.x, v_low.y, v_upp.z);
		v_upp_right = Vector3(v_upp.x, v_low.y, v_upp.z);
		v_upp_left  = Vector3(v_upp.x, v_low.y, v_low.z);
		normal      = Vector3(0, 1, 0);
	}
	else if (plane_specifier == 'z')
	{
		v_low 			= v_low - Vector3(boundary, boundary, height);
		v_upp 			= v_upp + Vector3(boundary, boundary, height);
		v_low_left  = Vector3(v_low.x, v_low.y, v_low.z);
		v_low_right = Vector3(v_low.x, v_upp.y, v_low.z);
		v_upp_right = Vector3(v_upp.x, v_upp.y, v_low.z);
		v_upp_left  = Vector3(v_upp.x, v_low.y, v_low.z);
		normal 			= Vector3(0, 0, 1);
	}
	else
	{
		Log.info() << "DemoTutorialDialog.addPlane(): unknown plane_specifier! "<< __FILE__ << " " << __LINE__ << endl;
		return;
	}

	// create the plane
	Mesh* plane = new Mesh();

	// the vertices
	plane->vertex.push_back(v_low_left);
	plane->vertex.push_back(v_low_right);
	plane->vertex.push_back(v_upp_right);
	plane->vertex.push_back(v_upp_left);

	// the first triangle of the plane
	Mesh::Triangle t1;
	t1.v1 = 0; // v_low_left
	t1.v2 = 1; // v_low_right
	t1.v3 = 2; // v_upp_right

	plane->triangle.push_back(t1);

	// the second triangle of the plane
	Mesh::Triangle t2;
	t2.v1 = 2; // v_upp_right
	t2.v2 = 3; // v_upp_left
	t2.v3 = 0; // v_low_left

	plane->triangle.push_back(t2);

	// the normals
	for (int i=0; i<4; i++)
		plane->normal.push_back(normal);

	// color
	ColorRGBA color(0,0,0,1);
	plane->setColor(color);

	// a representation
	Representation* rep = getMainControl()->getRepresentationManager().createRepresentation();
	rep->setName("Mirror Plane");

	// insert 
	rep->insert(*plane);

	// and commit
	getMainControl()->insert(*rep);
	getMainControl()->update(*rep);			
	notify_(new RepresentationMessage(*rep, RepresentationMessage::ADD_TO_GEOMETRIC_CONTROL));
}



void DemoTutorialDialog::nextStepDemo_()
{
	// initialisation for first real step
	if (current_step_ == 1)
	{
		DisplayProperties* dp = DisplayProperties::getInstance(0);
		dp->setDrawingPrecision(DRAWING_PRECISION_HIGH);

		((Mainframe*)getMainControl())->reset();

		// open bpti 
		try
		{
			Path path;
			String file_name = path.find("structures/bpti.pdb");

			MolecularFileDialog* dialog = MolecularFileDialog::getInstance(0);
			if (!dialog) return;

			dp->enableCreationForNewMolecules(false);
			system_ = dialog->openMolecularFile(file_name);
			dp->enableCreationForNewMolecules(true);

		  if (!system_)
			{
				String msg((String)tr("Could not open bpti.pdb. Maybe the file was deleted?")+"\n");
				msg += (String)tr("It should be found in") + " " + file_name;

				QMessageBox::critical(0, tr("Error while starting BALLView Demo"), msg.c_str(),
						QMessageBox::Ok, Qt::NoButton, Qt::NoButton);
				return;
			}
			
			system_->apply(getFragmentDB().add_hydrogens);
			system_->apply(getFragmentDB().build_bonds);
			getMainControl()->update(*system_, true);
		}
		catch(Exception::FileNotFound e)
		{
			Log.error() << (String)tr("Could not open") << " " << e.getFilename() << std::endl;
			return;
		}

		composites_.clear();
		composites_.push_back(system_);
	}
	
	if (current_step_ == 17) // last page
	{
		hide();
		return;
	}
	MolecularStructure* ms = MolecularStructure::getInstance(0);

	next_button->setEnabled(current_step_ >= 14);

	// remove representations
	RepresentationManager& pm = getMainControl()->getRepresentationManager();
	Size nr = pm.getNumberOfRepresentations();
	std::list<Representation*> reps = pm.getRepresentations();

	if (!surface_ && nr == 1 && current_step_ == 6)
	{
		GeometricObject* go = *(**reps.begin()).getGeometricObjects().begin();
		Mesh* mesh = dynamic_cast<Mesh*>(go);
		if (mesh)
		{
			surface_ = new Mesh(*mesh);
		}
		else
		{
			// should not happen
			BALLVIEW_DEBUG
			surface_ = new Mesh();
		}
	}

	for (Position p = 0; p < nr; p++)
	{
		getMainControl()->remove(**reps.begin());
		reps.pop_front();
	}

	if (current_step_ < 6)
	{
		ModelType type = (ModelType) (current_step_ - 1);
		if (type >= MODEL_SA_SURFACE)
		{
			type = (ModelType)((Index)type + 1);
		}
		notify_(new CreateRepresentationMessage(composites_, type, COLORING_ELEMENT));
	}
	else if (current_step_ < 8)
	{	
		ModelType type = (ModelType) (current_step_ - 1);
		if (type >= MODEL_SA_SURFACE)
		{
			type = (ModelType)((Index)type + 1);
		}
		notify_(new CreateRepresentationMessage(composites_, type, COLORING_MOLECULE));
	}
	else if (current_step_ == 8)
	{
		getMainControl()->getMolecularControlSelection().clear();
		getMainControl()->getMolecularControlSelection().push_back(system_);
		ms->calculateHBonds();
   	notify_(new CreateRepresentationMessage(composites_, MODEL_STICK, COLORING_ELEMENT));
#ifndef BALL_HAS_RTFACT
 		notify_(new CreateRepresentationMessage(composites_, MODEL_HBONDS, COLORING_ELEMENT));
#endif
	}
	else if (current_step_ == 9)
	{
		notify_(new CreateRepresentationMessage(composites_, MODEL_VDW, COLORING_TEMPERATURE_FACTOR));
	}
	else if (current_step_ == 10)
	{
		notify_(new CreateRepresentationMessage(composites_, MODEL_CARTOON, COLORING_SECONDARY_STRUCTURE));
	}
	else if (current_step_ == 11)
	{
		notify_(new CreateRepresentationMessage(composites_, MODEL_CARTOON, COLORING_RESIDUE_INDEX));
	}
	else if (current_step_ == 12 || current_step_ == 13)
	{
		getMainControl()->setMultithreading(false);
		notify_(new CreateRepresentationMessage(composites_, MODEL_STICK, COLORING_ELEMENT));
		getMainControl()->setMultithreading(true);

		list<Composite*> composites;
		composites.push_back(*getMainControl()->getCompositeManager().getComposites().begin());
 		MolecularControl::getInstance(0)->highlight(composites);

		if (current_step_ == 12)
		{
			ms->getAmberConfigurationDialog().resetOptions();
			ms->chooseAmberFF();
			ms->getMinimizationDialog().setMaxGradient(1.);
			ms->getMinimizationDialog().setMaxIterations(20);
#ifdef BALL_HAS_RTFACT
			ms->getMinimizationDialog().setRefresh(10);
#else
			ms->getMinimizationDialog().setRefresh(5);
#endif
			ms->runMinimization(false);
		}
		else
		{
			ms->getMDSimulationDialog().setTimeStep(0.002f);
			ms->getMDSimulationDialog().setNumberOfSteps(30);
			ms->MDSimulation(false);
		}
	}
	else if (current_step_ == 14) //FDPB
	{
		getMainControl()->setMultithreading(0);
		if (!ms->calculateFDPB(false))
		{
			BALLVIEW_DEBUG;
		}
		getMainControl()->setMultithreading(1);
	}
	else if (current_step_ == 15) // SES colored 
	{
		// Create a new representation containing the contour surface.
		Representation* rep = getMainControl()->getRepresentationManager().createRepresentation();
		rep->setModelType(MODEL_SE_SURFACE); 
		rep->insert(*new Mesh(*surface_));
		getMainControl()->insert(*rep);

		ModifyRepresentationDialog* cdialog = ModifyRepresentationDialog::getInstance(0);
		cdialog->setMode(0);
		cdialog->setRepresentation(rep);
		cdialog->setGrid(grid_);
		cdialog->setMinValue(-0.7f);
		cdialog->setMaxValue(0.7f);
		cdialog->accept();

		getMainControl()->update(*rep);
	}
	else if (current_step_ == 16)
	{
		getMainControl()->setMultithreading(0);
		notify_(new CreateRepresentationMessage(composites_, MODEL_STICK, COLORING_ELEMENT));
		getMainControl()->setMultithreading(1);

		notify_(new CreateRepresentationMessage(composites_, MODEL_STICK, COLORING_ELEMENT));

		DatasetController* dc = DatasetControl::getInstance(0)->getController(RegularData3DController::type);
		RegularData3DController& rcon = *(RegularData3DController*) dc;
		vector<Dataset*> grids = rcon.getDatasets();
		if (grids.empty()) return;
		rcon.computeIsoContourSurface(*grids[0], ColorRGBA(255,0,0), -0.1f);
		rcon.computeIsoContourSurface(*grids[0], ColorRGBA(0,0,255), 0.1f);

		// last entry: we are done
	}
}

void DemoTutorialDialog::showRaytracingTutorial()
{
	tutorial_type_ = RAYTRACING_TUTORIAL;
	show();
}


void DemoTutorialDialog::showTutorial()
{
	tutorial_type_ = TUTORIAL;
	show();
}

void DemoTutorialDialog::showDemo()
{
	tutorial_type_ = DEMO;
	show();
}

void DemoTutorialDialog::onNotifyTutorial_(Message *message)
{
	CompositeMessage* cmsg = RTTI::castTo<CompositeMessage>(*message);
	RepresentationMessage* rmsg = RTTI::castTo<RepresentationMessage>(*message);

	if (rmsg && !rmsg->getRepresentation()) return;

	switch (current_step_)
	{
		case TUTORIAL_PEPTIDE: // "Building a peptide from a given sequence"
		{
			if (!cmsg || cmsg->getType() != CompositeMessage::NEW_MOLECULE) return;
			break;
		}

		case TUTORIAL_ROTATE: // "Rotating"
		{
            if (!RTTI::isKindOf<SceneMessage>(message)) return;
			break;
		}

		case TUTORIAL_HIERARCHY: // "Hierarchy of molecules"
		{
			break;
		}

		case TUTORIAL_MDS: // "Molecular Dynamics Simulation")
		{
			if (!RTTI::isKindOf<DatasetMessage>(message)) return;
			DatasetMessage* msg = dynamic_cast<DatasetMessage*>(message);
			if (!msg->getDataset())
			{
				BALLVIEW_DEBUG
				return;
			}

			if (msg->getDataset()->getType() != TrajectoryController::type || msg->getType() != DatasetMessage::ADD)
			{
				return;
			}

			break;
		}

		case TUTORIAL_TRAJECTORY: // "Visualisation of trajectories")
		{
			if (cmsg && cmsg->getType() == CompositeMessage::CHANGED_COMPOSITE)
			{
				enableNextStep_();
			}
			break;
		}

		case TUTORIAL_ES:  // "Calculation of electrostatics"
		{
            if (!RTTI::isKindOf<DatasetMessage>(message)) return;
			DatasetMessage* msg = dynamic_cast<DatasetMessage*>(message);
			if (!msg->getDataset())
			{
				BALLVIEW_DEBUG
				return;
			}

			if (msg->getDataset()->getType() != RegularData3DController::type ||
					msg->getType() != DatasetMessage::ADD)
			{
				return;
			}

			break;
		}

		case TUTORIAL_SES: // "Creating a Solvent Excluded Surface"
		{
			if (!rmsg ||
					rmsg->getType() != RepresentationMessage::ADD_TO_GEOMETRIC_CONTROL ||
					rmsg->getRepresentation()->getModelType() != MODEL_SE_SURFACE)
			{
				return;
			}
			break;
		}

		case TUTORIAL_SES_COLORING: // "Coloring a SES by electrostatics"
		{
			if (!rmsg ||
					(rmsg->getType() != RepresentationMessage::UPDATE &&
					rmsg->getRepresentation()->getModelType() != MODEL_SE_SURFACE))
			{
				return;
			}
			break;
		}

		case TUTORIAL_CS: // "Creating a isocontour surface"
		{
			if (!rmsg || rmsg->getRepresentation()->getModelType() != MODEL_CONTOUR_SURFACE)
			{
				return;
			}
			break;
		}

		default:
			BALLVIEW_DEBUG;
			Log.error() << (String)tr("Current step") << ": " << current_step_ << std::endl;
			return;
	}

	enableNextStep_();
}

void DemoTutorialDialog::onNotifyRaytracingTutorial_(Message *message)
{
	CompositeMessage* cmsg = RTTI::castTo<CompositeMessage>(*message);
	RepresentationMessage* rmsg = RTTI::castTo<RepresentationMessage>(*message);

	if (rmsg && !rmsg->getRepresentation()) return;

	switch (current_step_)
	{
		case 1:
		{
			// nothing to be checked
			break;
		}
		case 2: // "Building a peptide from a given sequence"
		{
			if (!cmsg || cmsg->getType() != CompositeMessage::NEW_MOLECULE) return;
			break;
		}
		case 3: // "Set the background color"
		{
			if (Scene::getInstance(0)->getStage()->getBackgroundColor() != ColorRGBA(0, 0, 0, 255)) return;
			break;
		}	
		case 4: // "Rotating"
		{
			if (!RTTI::isKindOf<SceneMessage>(message)) return;
			break;
		}	
		case 5: // "Setting light sources"
		{		
			//if (Scene::getInstance(0)->getStage()->getLightSource(0).getColor() != ColorRGBA(255, 245, 208, 255)) return;
			if (Scene::getInstance(0)->getStage()->getLightSources().size() != 2) return;
			break;
		}
		case 6: // "Setting the materials"
		{	
			// check, if we got an SES 
			if (!rmsg ||
					rmsg->getType() != RepresentationMessage::ADD_TO_GEOMETRIC_CONTROL ||
					rmsg->getRepresentation()->getModelType() != MODEL_SE_SURFACE)
			{
				return;
			}
			break;
		}
		case 7:  // "downsampling/PNGs"
		{	
			//TODO find a checker!!
			if (cmsg)
				cout <<  "*7*" << cmsg->getType() << endl;
			break; 
		}	
		default:
			BALLVIEW_DEBUG;
			Log.error() << "Current step: " << current_step_ << std::endl;
			return;
	}

	enableNextStep_();
}

void DemoTutorialDialog::initializeWidget(MainControl&)
{
	getMainControl()->insertPopupMenuSeparator(MainControl::HELP);
	
	String description = "Shortcut|Help|Demo";
	demo_action_ = insertMenuEntry(MainControl::HELP, tr("Demo"), this, SLOT(showDemo()), 
	                               description, QKeySequence(),
	                               tr("Show a demonstration of BALLView's features"),
	                               UIOperationMode::MODE_ADVANCED);

	description = "Shortcut|Help|Tutorial";
	tutorial_action_ = insertMenuEntry(MainControl::HELP, tr("Tutorial"), this, SLOT(showTutorial()), 
	                                   description, QKeySequence(), tr("Perform a step-by-step tutorial"),
	                                   UIOperationMode::MODE_ADVANCED);

#ifdef BALL_HAS_RTFACT
	description = "Shortcut|Help|RaytracingTutorial";
	raytracing_tutorial_action_ = insertMenuEntry(MainControl::HELP, tr("Ray tracing Tutorial"), this, 
	                                              SLOT(showRaytracingTutorial()), description, QKeySequence(),
	                                              tr("Learn how to use RTFact"), UIOperationMode::MODE_ADVANCED);
#endif

	getMainControl()->insertPopupMenuSeparator(MainControl::HELP);

}

void DemoTutorialDialog::checkMenu(MainControl& main_control)
{
	bool busy = main_control.isBusy();
	if (demo_action_)
		demo_action_->setEnabled(!busy);
	if (tutorial_action_)
		tutorial_action_->setEnabled(!busy);
	if (raytracing_tutorial_action_)
		raytracing_tutorial_action_->setEnabled(!busy);
}

} } // namespaces