# HG changeset patch
# User insilmaril
# Date 1242834014 0
# Node ID e3f722759c7e44cb9c53567d6053da7bbb3eae4d
# Parent  01f2f6d6789d8026658bf5dd1551ce3762c55853
Fixed segfault when closing a map

diff -r 01f2f6d6789d -r e3f722759c7e branchitem.cpp
--- a/branchitem.cpp	Mon May 18 11:22:41 2009 +0000
+++ b/branchitem.cpp	Wed May 20 15:40:14 2009 +0000
@@ -18,7 +18,7 @@
 
 BranchItem::~BranchItem()
 {
-//	cout << "Destr. BranchItem "<<getHeadingStd()<<endl;
+	//cout << "Destr. BranchItem "<<getHeadingStd()<<endl;
 	if (lmo) 
 	{
 		delete lmo;
@@ -205,8 +205,6 @@
 
 bool BranchItem::toggleScroll()	
 {
-
-	if (!branchCount()>0) return false;
 	BranchObj *bo=NULL;
 	if (scrolled)
 	{
@@ -220,7 +218,6 @@
 				if (bo) bo->setVisibility(true);
 			}
 		}
-		model->reposition();
 	} else
 	{
 		scrolled=true;
@@ -234,6 +231,7 @@
 			}
 		}
 	}
+	model->reposition();	// FIXME-3 we don't really want to update view from here...
 	return true;
 }
 
@@ -350,9 +348,12 @@
 	// Initialize BranchObj, order of things is important...
     BranchObj* newbo=new BranchObj(scene);
 	lmo=newbo;
-    newbo->setParObj(parent()->getLMO() );
+	BranchObj* parbo=(BranchObj*)(parentItem->getLMO());
+    newbo->setParObj(parbo);
 	newbo->setTreeItem (this);
 	newbo->setDefAttr(BranchObj::NewBranch);
+	if (((BranchItem*)parentItem)->scrolled || !parbo->isVisibleObj() )
+		newbo->setVisibility (false);
 	
 	initLMO();
 
diff -r 01f2f6d6789d -r e3f722759c7e branchobj.cpp
--- a/branchobj.cpp	Mon May 18 11:22:41 2009 +0000
+++ b/branchobj.cpp	Wed May 20 15:40:14 2009 +0000
@@ -638,58 +638,6 @@
 	calcBBoxSize();
 }
 
-void BranchObj::removeBranchHere(BranchObj* borem)	// FIXME-2 getNum no longer available
-{
-/*
-	// This removes the branch bo from list, but 
-	// inserts its children at the place of bo
-	BranchObj *bo;
-	bo=borem->getLastBranch();
-	int pos=borem->getNum();
-	while (bo)
-	{
-		bo->linkTo (this,pos+1);
-		bo=borem->getLastBranch();
-	}	
-	removeBranch (borem);
-	*/
-}
-
-void BranchObj::removeChildren()	// FIXME-3 not needed here
-{
-	clear();
-}
-
-void BranchObj::removeBranch(BranchObj* bo)	// FIXME-2 not needed here
-{
-/*
-    // if bo is not in branch remove returns false, we
-    // don't care...
-	
-	int i=branch.indexOf(bo);
-    if (i>=0)
-	{
-		delete (bo);
-		branch.removeAt (i);
-	} else
-		qWarning ("BranchObj::removeBranch tried to remove non existing branch?!\n");
-	requestReposition();
-	*/
-}
-
-void BranchObj::removeBranchPtr(BranchObj* bo)	// FIXME-3 not needed here
-{
-/*
-	int i=branch.indexOf(bo);
-	
-	if (i>=0)
-		branch.removeAt (i);
-	else	
-		qWarning ("BranchObj::removeBranchPtr tried to remove non existing branch?!\n");
-	requestReposition();
-*/	
-}
-
 void BranchObj::sortChildren() //FIXME-2  
 {
 /*
diff -r 01f2f6d6789d -r e3f722759c7e branchobj.h
--- a/branchobj.h	Mon May 18 11:22:41 2009 +0000
+++ b/branchobj.h	Wed May 20 15:40:14 2009 +0000
@@ -72,11 +72,6 @@
 public:	
 	virtual void setDefAttr (BranchModification);	// set default attributes (font, size, ...)
 
-    virtual void removeBranchHere(BranchObj*);  //FIXME-3
-    virtual void removeChildren();  
-    virtual void removeBranch(BranchObj*);  
-    virtual void removeBranchPtr (BranchObj*);  
-
     virtual void sortChildren();
     virtual void alignRelativeTo(const QPointF, bool alignSelf=false );
 	virtual void reposition();
diff -r 01f2f6d6789d -r e3f722759c7e mainwindow.cpp
--- a/mainwindow.cpp	Mon May 18 11:22:41 2009 +0000
+++ b/mainwindow.cpp	Wed May 20 15:40:14 2009 +0000
@@ -542,34 +542,6 @@
 	editMenu->addAction (a);
 	actionPaste=a;
 
-/*	FIXME-5
-    // Shortcuts to modify heading:
-    a = new QAction(tr( "Edit heading","Edit menu" ),this);
-	a->setStatusTip ( tr( "edit Heading" ));
-	a->setShortcut ( Qt::Key_Enter);				//Edit heading
-//	a->setShortcutContext (Qt::WindowShortcut);
-	addAction (a);
-    connect( a, SIGNAL( triggered() ), this, SLOT( editHeading() ) );
-	actionListBranches.append(a);
-    a = new QAction( tr( "Edit heading","Edit menu" ), this);
-	a->setStatusTip (tr( "edit Heading" ));
-	a->setShortcut (Qt::Key_Return );				//Edit heading
-	//a->setShortcutContext (Qt::WindowShortcut);
-	addAction (a);
-    connect( a, SIGNAL( triggered() ), this, SLOT( editHeading() ) );
-	actionListBranches.append(a);
-	editMenu->addAction (a);
-	actionHeading=a;
-
-    a = new QAction( tr( "Edit heading","Edit menu" ), this);
-	a->setStatusTip (tr( "edit Heading" ));
-	//a->setShortcut ( Qt::Key_F2 );					//Edit heading
-	a->setShortcutContext (Qt::WindowShortcut);
-	addAction (a);
-    connect( a, SIGNAL( triggered() ), this, SLOT( editHeading() ) );
-	actionListBranches.append(a);
-*/	
-    
     // Shortcut to delete selection
     a = new QAction( tr( "Delete Selection","Edit menu" ),this);
 	a->setStatusTip (tr( "Delete Selection" ));
@@ -1144,6 +1116,7 @@
 	QToolBar *tb=NULL;
 
 	Flag *flag=new Flag;;
+	flag->setVisible(true);
 
 	flag->load(QPixmap(flagsPath+"flag-note.png"));
 	setupFlag (flag,tb,"system-note",tr("Note","SystemFlag"));
@@ -1167,7 +1140,6 @@
 	tb=addToolBar (tr ("Standard Flags","Standard Flag Toolbar"));
 	tb->setObjectName ("standardFlagTB");
 	standardFlagsMaster->setToolBar (tb);
-
 	
 	flag->load(flagsPath+"flag-exclamationmark.png");
 	flag->setGroup("standard-mark");
@@ -1259,7 +1231,7 @@
 	setupFlag (flag,tb,"lifebelt",tr("This will help","Standardflag"));
 
 	// Freemind flags
-
+	flag->setVisible(false);
 	flag->load(flagsPath+"freemind/warning.png");
 	setupFlag (flag,tb,  "freemind-warning",tr("Important","Freemind-Flag"));
 
@@ -1343,6 +1315,7 @@
 		// StandardFlag
 		tb->addAction (a);
 		flag->setAction (a);
+		a->setVisible (flag->isVisible());
 		a->setCheckable(true);
 		a->setObjectName(name);
 		a->setToolTip(tooltip);
@@ -1696,14 +1669,14 @@
 MapEditor* Main::currentMapEditor() const
 {
     if ( tabWidget->currentPage())
-		return tabModel.at(tabWidget->currentIndex())->getMapEditor();
+		return vymViews.at(tabWidget->currentIndex())->getMapEditor();
     return NULL;	
 }
 
 VymModel* Main::currentModel() const
 {
     if ( tabWidget->currentPage())
-		return tabModel.at(tabWidget->currentIndex());
+		return vymViews.at(tabWidget->currentIndex())->getModel();
     return NULL;	
 }
 
@@ -1712,11 +1685,13 @@
 {
 	// Unselect all possibly selected objects
 	// (Important to update note editor)
+	VymModel *m;
 	for (int i=0;i<=tabWidget->count() -1;i++)
 	{
-		tabModel.at(i)->unselect();
-	}	
-	VymModel *m=currentModel();
+		m= vymViews.at(tabWidget->currentIndex())->getModel();
+		if (m) m->unselect();
+	}
+	m=currentModel();
 	if (m) m->reselect();
 
 	// Update actions to in menus and toolbars according to editor
@@ -1726,11 +1701,11 @@
 void Main::fileNew()
 {
 	VymModel *vm=new VymModel;
-	tabModel.append (vm);
 
 	VymView *vv=new VymView (vm);
+	vymViews.append (vv);
 	tabWidget->addTab (vv,tr("unnamed","MainWindow: name for new and empty file"));
-	tabWidget->setCurrentIndex (tabModel.count() );
+	tabWidget->setCurrentIndex (vymViews.count() );
 	vv->initFocus();
 
 	
@@ -1746,7 +1721,7 @@
 	{
 		srcModel->copy();
 		fileNew();
-		VymModel *dstModel=tabModel.last ();
+		VymModel *dstModel=vymViews.last()->getModel();
 		dstModel->select("mc:");
 		dstModel->load (clipboardDir+"/"+clipboardFile,ImportReplace, VymMap);
 	}
@@ -1770,7 +1745,7 @@
 		int i=0;
 		while (i<=tabWidget->count() -1)
 		{
-			if (tabModel.at(i)->getFilePath() == fn)
+			if (vymViews.at(i)->getModel()->getFilePath() == fn)
 			{
 				// Already there, ask for confirmation
 				QMessageBox mb( vymName,
@@ -1813,7 +1788,7 @@
 		{
 			vm=new VymModel;
 			VymView *vv=new VymView (vm);
-			tabModel.append (vm);
+			vymViews.append (vv);
 			tabWidget->addTab (vv,fn);
 			tabIndex=tabWidget->count()-1;
 			tabWidget->setCurrentPage (tabIndex);
@@ -2310,16 +2285,15 @@
 	}
 }
 
-void Main::fileCloseMap()
+void Main::fileCloseMap()	
 {
-	MapEditor *me = currentMapEditor();
 	VymModel *m=currentModel();
 	if (m)
 	{
 		if (m->hasChanged())
 		{
 			QMessageBox mb( vymName,
-				tr("The map %1 has been modified but not saved yet. Do you want to").arg(me->getModel()->getFileName()),
+				tr("The map %1 has been modified but not saved yet. Do you want to").arg(m->getFileName()),
 				QMessageBox::Warning,
 				QMessageBox::Yes | QMessageBox::Default,
 				QMessageBox::No,
@@ -2340,14 +2314,16 @@
 				return;
 			}
 		} 
-		tabModel.removeAt (tabWidget->currentIndex() );
+		// And here comes the segfault, because removeTab triggers 
+		// currentChanged->Main::editorChanged -> updateActions and VM is not NULL yet...
+		vymViews.removeAt (tabWidget->currentIndex() );
 		tabWidget->removeTab (tabWidget->currentIndex() );
 
-		// Remove mapEditor;
+		// Remove mapEditor/model FIXME-5
 		// Better would be delete (me), but then we could have a Qt error:
 		// "QObject: Do not delete object, 'MapEditor', during its event handler!"
 		// So we only remove data now and call deconstructor when vym closes later
-		// FIXME-3  this needs to be moved to vymview...   me->clear();
+		// this needs to be moved to vymview...   me->clear();
 		// some model->clear is needed to free up memory ...
 
 		updateActions();
@@ -2364,10 +2340,10 @@
 {
 	// Check if one or more editors have changed
 	int i;
-	for (i=0;i<=tabModel.count() -1;i++)
+	for (i=0;i<=vymViews.count() -1;i++)
 	{
 		// If something changed, ask what to do
-		if (tabModel.at(i)->hasChanged())
+		if (vymViews.at(i)->getModel()->hasChanged())
 		{
 			tabWidget->setCurrentPage(i);
 			QMessageBox mb( vymName,
@@ -2641,9 +2617,9 @@
 		// compare path with already loaded maps
 		int index=-1;
 		int i;
-		for (i=0;i<=tabModel.count() -1;i++)
+		for (i=0;i<=vymViews.count() -1;i++)
 		{
-			if (vl.at(j)==tabModel.at(i)->getFilePath() )
+			if (vl.at(j)==vymViews.at(i)->getModel()->getFilePath() )
 			{
 				index=i;
 				break;
@@ -2789,11 +2765,7 @@
 void Main::editAddMapCenter()
 {
 	VymModel *m=currentModel();
-	if (m)
-	{
-		cout <<"Main::editAddMapCenter\n";
-		m->addMapCenter ();
-	}	
+	if (m) m->addMapCenter ();
 }
 
 void Main::editNewBranch()
@@ -3226,9 +3198,9 @@
 {
 	bool b=actionViewToggleAntiAlias->isOn();
 	MapEditor *me;
-	for (int i=0;i<tabModel.count();i++)
+	for (int i=0;i<vymViews.count();i++)
 	{
-		me=tabModel.at(i)->getMapEditor();
+		me=vymViews.at(i)->getMapEditor();
 		if (me) me->setAntiAlias(b);
 	}	
 
@@ -3248,10 +3220,10 @@
 {
 	bool b=actionViewToggleSmoothPixmapTransform->isOn();
 	MapEditor *me;
-	for (int i=0;i<tabModel.count();i++)
+	for (int i=0;i<vymViews.count();i++)
 	{
 		
-		me=tabModel.at(i)->getMapEditor();
+		me=vymViews.at(i)->getMapEditor();
 		if (me) me->setSmoothPixmap(b);
 	}	
 }
@@ -3388,139 +3360,142 @@
 	else	
 		actionRedo->setEnabled( false);
 
-	TreeItem *selti=m->getSelectedItem();
-	BranchItem *selbi=m->getSelectedBranchItem();
-	if (selti)
+	if (m)
 	{
-		if (selbi)
+		TreeItem *selti=m->getSelectedItem();
+		BranchItem *selbi=m->getSelectedBranchItem();
+		if (selti)
 		{
-			// Take care of links  // FIXME-1
-			/*
-			if (bo->countXLinks()==0)
+			if (selbi)
 			{
-				branchXLinksContextMenuEdit->clear();
-				branchXLinksContextMenuFollow->clear();
-			} else
+				// Take care of links  // FIXME-1
+				/*
+				if (bo->countXLinks()==0)
+				{
+					branchXLinksContextMenuEdit->clear();
+					branchXLinksContextMenuFollow->clear();
+				} else
+				{
+					BranchObj *bot;
+					QString s;
+					branchXLinksContextMenuEdit->clear();
+					branchXLinksContextMenuFollow->clear();
+					for (int i=0; i<=bo->countXLinks();i++)
+					{
+						bot=bo->XLinkTargetAt(i);
+						if (bot)
+						{
+							s=bot->getHeading();
+							if (s.length()>xLinkMenuWidth)
+								s=s.left(xLinkMenuWidth)+"...";
+							branchXLinksContextMenuFollow->addAction (s);
+							branchXLinksContextMenuEdit->addAction (s);
+						}	
+					}
+				}
+				*/
+				//Standard Flags
+				standardFlagsMaster->updateToolBar (selbi->activeStandardFlagNames() );
+
+				// System Flags
+				actionToggleScroll->setEnabled (true);
+				if ( selbi->isScrolled() )
+					actionToggleScroll->setOn(true);
+				else	
+					actionToggleScroll->setOn(false);
+
+				if ( selti->getURL().isEmpty() )
+				{
+					actionOpenURL->setEnabled (false);
+					actionOpenURLTab->setEnabled (false);
+				}	
+				else	
+				{
+					actionOpenURL->setEnabled (true);
+					actionOpenURLTab->setEnabled (true);
+				}
+				if ( selti->getVymLink().isEmpty() )
+				{
+					actionOpenVymLink->setEnabled (false);
+					actionDeleteVymLink->setEnabled (false);
+				} else	
+				{
+					actionOpenVymLink->setEnabled (true);
+					actionDeleteVymLink->setEnabled (true);
+				}	
+
+				if (selbi->canMoveUp()) 
+					actionMoveUp->setEnabled (true);
+				else	
+					actionMoveUp->setEnabled (false);
+				if (selbi->canMoveDown()) 
+					actionMoveDown->setEnabled (true);
+				else	
+					actionMoveDown->setEnabled (false);
+
+				actionSortChildren->setEnabled (true);
+
+				actionToggleHideExport->setEnabled (true);	
+				actionToggleHideExport->setOn (selbi->hideInExport() );	
+
+				actionFileSave->setEnabled (true);	
+				actionCopy->setEnabled (true);	
+				actionCut->setEnabled (true);	
+				if (!clipboardEmpty)
+					actionPaste->setEnabled (true);	
+				else	
+					actionPaste->setEnabled (false);	
+				for (int i=0; i<actionListBranches.size(); ++i)	
+					actionListBranches.at(i)->setEnabled(true);
+				actionDelete->setEnabled (true);
+				//FIXME-2 actionFormatHideLinkUnselected->setOn (selection->getHideLinkUnselected());
+			}
+			if ( selti->getType()==TreeItem::Image)
 			{
-				BranchObj *bot;
-				QString s;
-				branchXLinksContextMenuEdit->clear();
-				branchXLinksContextMenuFollow->clear();
-				for (int i=0; i<=bo->countXLinks();i++)
-				{
-					bot=bo->XLinkTargetAt(i);
-					if (bot)
-					{
-						s=bot->getHeading();
-						if (s.length()>xLinkMenuWidth)
-							s=s.left(xLinkMenuWidth)+"...";
-						branchXLinksContextMenuFollow->addAction (s);
-						branchXLinksContextMenuEdit->addAction (s);
-					}	
-				}
-			}
-			*/
-			//Standard Flags
-			standardFlagsMaster->updateToolBar (selbi->activeStandardFlagNames() );
-
-			// System Flags
-			actionToggleScroll->setEnabled (true);
-			if ( selbi->isScrolled() )
-				actionToggleScroll->setOn(true);
-			else	
-				actionToggleScroll->setOn(false);
-
-			if ( selti->getURL().isEmpty() )
-			{
+			/* FIXME-2
+				FloatObj *fo=(FloatImageObj*)selection;
+
 				actionOpenURL->setEnabled (false);
-				actionOpenURLTab->setEnabled (false);
-			}	
-			else	
-			{
-				actionOpenURL->setEnabled (true);
-				actionOpenURLTab->setEnabled (true);
-			}
-			if ( selti->getVymLink().isEmpty() )
-			{
 				actionOpenVymLink->setEnabled (false);
-				actionDeleteVymLink->setEnabled (false);
-			} else	
-			{
-				actionOpenVymLink->setEnabled (true);
-				actionDeleteVymLink->setEnabled (true);
-			}	
-
-            if (selbi->canMoveUp()) 
-				actionMoveUp->setEnabled (true);
-			else	
+				actionDeleteVymLink->setEnabled (false);	
+				actionToggleHideExport->setEnabled (true);	
+				actionToggleHideExport->setOn (fo->hideInExport() );	
+
+
+				actionCopy->setEnabled (true);
+				actionCut->setEnabled (true);	
+				actionPaste->setEnabled (false);	//FIXME-4 why not allowing copy of images?
+				for (int i=0; i<actionListBranches.size(); ++i)	
+					actionListBranches.at(i)->setEnabled(false);
+				actionDelete->setEnabled (true);
+				actionFormatHideLinkUnselected->setOn
+					( selection->getHideLinkUnselected());
 				actionMoveUp->setEnabled (false);
-			if (selbi->canMoveDown()) 
-				actionMoveDown->setEnabled (true);
-			else	
 				actionMoveDown->setEnabled (false);
-
-			actionSortChildren->setEnabled (true);
-
-			actionToggleHideExport->setEnabled (true);	
-			actionToggleHideExport->setOn (selbi->hideInExport() );	
-
-			actionFileSave->setEnabled (true);	
-			actionCopy->setEnabled (true);	
-			actionCut->setEnabled (true);	
-			if (!clipboardEmpty)
-				actionPaste->setEnabled (true);	
-			else	
-				actionPaste->setEnabled (false);	
+				*/
+			}	//image
+
+		} else
+		{	// !selti
+			actionFileSave->setEnabled (false);	
+			actionCopy->setEnabled (false);	
+			actionCut->setEnabled (false);	
+			actionPaste->setEnabled (false);	
 			for (int i=0; i<actionListBranches.size(); ++i)	
-				actionListBranches.at(i)->setEnabled(true);
-			actionDelete->setEnabled (true);
-			//FIXME-2 actionFormatHideLinkUnselected->setOn (selection->getHideLinkUnselected());
-		}
-		if ( selti->getType()==TreeItem::Image)
-		{
-		/* FIXME-2
-			FloatObj *fo=(FloatImageObj*)selection;
-
+				actionListBranches.at(i)->setEnabled(false);
+
+			actionToggleScroll->setEnabled (false);
 			actionOpenURL->setEnabled (false);
 			actionOpenVymLink->setEnabled (false);
 			actionDeleteVymLink->setEnabled (false);	
-			actionToggleHideExport->setEnabled (true);	
-			actionToggleHideExport->setOn (fo->hideInExport() );	
-
-
-			actionCopy->setEnabled (true);
-			actionCut->setEnabled (true);	
-			actionPaste->setEnabled (false);	//FIXME-4 why not allowing copy of images?
-			for (int i=0; i<actionListBranches.size(); ++i)	
-				actionListBranches.at(i)->setEnabled(false);
-			actionDelete->setEnabled (true);
-			actionFormatHideLinkUnselected->setOn
-				( selection->getHideLinkUnselected());
+			actionHeading2URL->setEnabled (false);	
+			actionDelete->setEnabled (false);
 			actionMoveUp->setEnabled (false);
 			actionMoveDown->setEnabled (false);
-			*/
-		}
-
-	} else
-	{
-		actionFileSave->setEnabled (false);	
-		actionCopy->setEnabled (false);	
-		actionCut->setEnabled (false);	
-		actionPaste->setEnabled (false);	
-		for (int i=0; i<actionListBranches.size(); ++i)	
-			actionListBranches.at(i)->setEnabled(false);
-
-		actionToggleScroll->setEnabled (false);
-		actionOpenURL->setEnabled (false);
-		actionOpenVymLink->setEnabled (false);
-		actionDeleteVymLink->setEnabled (false);	
-		actionHeading2URL->setEnabled (false);	
-		actionDelete->setEnabled (false);
-		actionMoveUp->setEnabled (false);
-		actionMoveDown->setEnabled (false);
-		actionSortChildren->setEnabled (false);
-		actionToggleHideExport->setEnabled (false);	
-	}	
+			actionSortChildren->setEnabled (false);
+			actionToggleHideExport->setEnabled (false);	
+		}	
+	} // m
 }
 
 Main::ModMode Main::getModMode()
diff -r 01f2f6d6789d -r e3f722759c7e mainwindow.h
--- a/mainwindow.h	Mon May 18 11:22:41 2009 +0000
+++ b/mainwindow.h	Wed May 20 15:40:14 2009 +0000
@@ -243,8 +243,7 @@
 
 	QStringList imageTypes;
 
-	QList <VymModel*> tabModel;		//!< the corresponding model to a tab
-
+	QList <VymView*> vymViews;		//! Keeps track of models and views related to a tab 
 	QString prevSelection;
 
 	HistoryWindow *historyWindow;
diff -r 01f2f6d6789d -r e3f722759c7e mapcenteritem.cpp
--- a/mapcenteritem.cpp	Mon May 18 11:22:41 2009 +0000
+++ b/mapcenteritem.cpp	Wed May 20 15:40:14 2009 +0000
@@ -12,7 +12,7 @@
 
 MapCenterItem::~MapCenterItem()
 {
-	cout << "Destr. MapCenterItem\n";
+//	cout << "Destr. MapCenterItem\n";
     qDeleteAll(childItems);
 }
 
diff -r 01f2f6d6789d -r e3f722759c7e mapeditor.cpp
--- a/mapeditor.cpp	Mon May 18 11:22:41 2009 +0000
+++ b/mapeditor.cpp	Wed May 20 15:40:14 2009 +0000
@@ -118,7 +118,7 @@
 	lineEdit=new QLineEdit;
 	lineEdit->hide();
 	QGraphicsProxyWidget *pw=scene()->addWidget (lineEdit);
-	pw->setZValue (100);
+	pw->setZValue (Z_LINEEDIT);
 
 	a = new QAction( tr( "Edit heading","MapEditor" ), this);
 	a->setShortcut ( Qt::Key_Return );					//Edit heading
@@ -182,6 +182,15 @@
     return mapScene;
 }
 
+void MapEditor::scrollTo (const QModelIndex &index)
+{
+	if (index.isValid())
+	{
+		LinkableMapObj* lmo=(static_cast<TreeItem*>(index.internalPointer()))->getLMO();
+		if (lmo) setScrollBarPosTarget (lmo->getBBox() );
+	}
+}
+
 void MapEditor::setScrollBarPosTarget (const QRectF &rect)
 {
 	// Code copied from Qt sources
@@ -838,7 +847,10 @@
 		// Check if we could link 
 		TreeItem *dsti=findMapItem (p, seli);
 		LinkableMapObj* dst=NULL;
-		if (dsti) dst=dsti->getLMO();	//FIXME-2 get rid of lmo...
+		if (dsti && dsti!=seli && dsti->isBranchLikeType())
+			dst=dsti->getLMO(); //FIXME-2 get rid of lmo...
+		else
+			dsti=NULL;
 		
 
 		FloatObj *fio=model->getSelectedFloatImage();
@@ -851,15 +863,10 @@
 
 			// Relink float to new mapcenter or branch, if shift is pressed	
 			// Only relink, if selection really has a new parent
-			if ( (e->modifiers()==Qt::ShiftModifier) && dst &&
-				( (typeid(*dst)==typeid(BranchObj)) ||
-				  (typeid(*dst)==typeid(MapCenterObj)) ) &&
-				( dst != fio->getParObj())  
+			if ( (e->modifiers()==Qt::ShiftModifier) && dst && ( dst != fio->getParObj())  
 				)
 			{
-				if (typeid(*fio) == typeid(FloatImageObj) && 
-				( (typeid(*dst)==typeid(BranchObj) ||
-				  typeid(*dst)==typeid(MapCenterObj)) ))  
+				if (typeid(*fio) == typeid(FloatImageObj) )  
 				{
 
 					// Also save the move which was done so far
@@ -911,7 +918,7 @@
 				} 
 
 				// Maybe we can relink temporary?
-				if (dsti && (dst!=lmosel) && seli->isBranchLikeType() && dsti->isBranchLikeType() )
+				if (dsti)
 				{
 					if (e->modifiers()==Qt::ControlModifier)
 					{
@@ -963,9 +970,13 @@
     QPointF p = mapToScene(e->pos());
 	TreeItem *seli=model->getSelectedItem();
 
-	TreeItem *dsti=findMapItem(p, seli);
+	TreeItem *dsti=NULL;
+	if (seli) dsti=findMapItem(p, seli);
 	LinkableMapObj* dst=NULL;
-	if (dsti) dst=dsti->getLMO();	//FIXME-2 get rid of dst...
+	if (dsti && dsti->isBranchLikeType ()) 
+		dst=dsti->getLMO();	//FIXME-2 get rid of dst...
+	else
+		dsti=NULL;
 
 
 	// Have we been picking color?
@@ -974,7 +985,7 @@
 		pickingColor=false;
 		setCursor (Qt::ArrowCursor);
 		// Check if we are over another branch
-		if (dsti && seli) 
+		if (dst) 
 		{	
 			if (e->state() & Qt::ShiftModifier)
 				model->colorBranch (((BranchObj*)dst)->getColor());
@@ -989,7 +1000,7 @@
 	{
 		drawingLink=false;
 		// Check if we are over another branch
-		if (dsti && seli && dsti->isBranchLikeType() ) 
+		if (dsti)
 		{	
 			tmpXLink->setEnd ( ((BranchObj*)(dst)) );
 			tmpXLink->updateXLink();
@@ -1006,11 +1017,6 @@
     // Have we been moving something?
     if ( seli && movingObj ) 
     {	
-		if (dsti==seli) 
-		{
-			dsti=NULL;
-			dst=NULL;
-		}
 		FloatImageObj *fo=model->getSelectedFloatImage();
 		if(fo)
 		{
@@ -1028,15 +1034,6 @@
 			model->reposition();
 		}	
 
-		// Check if we are over another branch, but ignore 
-		// any found LMOs, which are FloatObjs
-
-		if (dsti && (!dsti->isBranchLikeType() )) 
-		{
-			dst=NULL;
-			dsti=NULL;
-		}	
-		
 		BranchItem *bi=model->getSelectedBranchItem();
 		if (bi && bi->depth()==0)
 		{	
@@ -1069,7 +1066,7 @@
 			QString preSelStr=model->getSelectString(lmosel);
 
 			copyingObj=false;	
-			if (dsti && dsti->isBranchLikeType() ) 
+			if (dsti)
 			{
 				// We have a destination, relink to that
 
diff -r 01f2f6d6789d -r e3f722759c7e mapeditor.h
--- a/mapeditor.h	Mon May 18 11:22:41 2009 +0000
+++ b/mapeditor.h	Wed May 20 15:40:14 2009 +0000
@@ -31,6 +31,7 @@
 	QPointF scrollBarPosTarget;
 	QtPropertyAnimation scrollBarPosAnimation;
 public:
+	void scrollTo (const QModelIndex &index);
 	void setScrollBarPosTarget (const QRectF &rect);	//!  ensureVisible of rect
 	QPointF getScrollBarPosTarget ();
 	void setScrollBarPos (const QPointF &p);
@@ -135,7 +136,6 @@
 public:
 	void setSelectionColor (QColor c);
 	QColor getSelectionColor ();
-
 };
 #endif
 
diff -r 01f2f6d6789d -r e3f722759c7e mapobj.h
--- a/mapobj.h	Mon May 18 11:22:41 2009 +0000
+++ b/mapobj.h	Wed May 20 15:40:14 2009 +0000
@@ -6,14 +6,15 @@
 
 #include "xmlobj.h"
 
-#define Z_BBOX      0
-#define Z_XLINK    10
-#define Z_LINK     20
-#define Z_FRAME    50
-#define Z_SELBOX   60
-#define Z_FLOATIMG 65
-#define Z_ICON     80
-#define Z_TEXT    100
+#define Z_BBOX       0
+#define Z_XLINK     10
+#define Z_LINK      20
+#define Z_FRAME     50
+#define Z_SELBOX    60
+#define Z_FLOATIMG  65
+#define Z_ICON      80
+#define Z_TEXT     100
+#define Z_LINEEDIT 110
 
 /*! \brief Base class for all objects visible on a map
 */
diff -r 01f2f6d6789d -r e3f722759c7e treeitem.cpp
--- a/treeitem.cpp	Mon May 18 11:22:41 2009 +0000
+++ b/treeitem.cpp	Wed May 20 15:40:14 2009 +0000
@@ -10,6 +10,7 @@
 
 using namespace std;
 
+extern FlagRow* standardFlagsMaster;
 
 TreeItem::TreeItem(const QList<QVariant> &data, TreeItem *parent):MapItem()
 {
@@ -64,6 +65,8 @@
 
 	// Reset ID
 	objID="";
+
+	standardFlags.setMasterRow (standardFlagsMaster);
 }
 
 QString TreeItem::saveToDir (const QString &tmpdir,const QString &prefix, const QPointF& offset)
@@ -120,6 +123,15 @@
 	}
 }
 
+void TreeItem::removeChildBranches()
+{
+	while ( childItems.size()>0)
+	{
+		branchCounter--;
+		childItems.removeAt (0);
+	}
+}
+
 TreeItem *TreeItem::child(int row)
 {
     return childItems.value(row);
diff -r 01f2f6d6789d -r e3f722759c7e treeitem.h
--- a/treeitem.h	Mon May 18 11:22:41 2009 +0000
+++ b/treeitem.h	Wed May 20 15:40:14 2009 +0000
@@ -35,6 +35,7 @@
 
     virtual void appendChild (TreeItem *child);
 	virtual void removeChild (int row);
+	virtual void removeChildBranches ();
 
     virtual TreeItem *child(int row);
     virtual int childCount() const;
@@ -121,7 +122,6 @@
 	virtual bool isActiveStandardFlag (const QString &flag);
 	virtual QStringList activeStandardFlagNames();
 	virtual FlagRow* getStandardFlagRow ();
-	//virtual void updateToolBar();
 
 	virtual QStringList activeSystemFlagNames();
 
diff -r 01f2f6d6789d -r e3f722759c7e treemodel.cpp
--- a/treemodel.cpp	Mon May 18 11:22:41 2009 +0000
+++ b/treemodel.cpp	Wed May 20 15:40:14 2009 +0000
@@ -164,6 +164,7 @@
 	return current;
 }
 
+/*
 bool TreeModel::insertRows ( int row, int count, const QModelIndex & parent)
 {
 	std::cout << "TreeModel insertRows()\n";
@@ -177,6 +178,7 @@
 	endInsertRows ();
 	return true;
 }
+*/
 
 bool TreeModel::removeRows ( int row, int count, const QModelIndex & parent)
 {
diff -r 01f2f6d6789d -r e3f722759c7e treemodel.h
--- a/treemodel.h	Mon May 18 11:22:41 2009 +0000
+++ b/treemodel.h	Wed May 20 15:40:14 2009 +0000
@@ -30,8 +30,10 @@
 
 	BranchItem* next(BranchItem* &current, BranchItem* &previous, int &d0);
 
+/*
 	bool insertRows ( int row, int count, 
 				const QModelIndex & parent = QModelIndex() ); 
+*/				
 	bool removeRows ( int row, int count, 
 				const QModelIndex & parent = QModelIndex() ); 
 
diff -r 01f2f6d6789d -r e3f722759c7e vymmodel.cpp
--- a/vymmodel.cpp	Mon May 18 11:22:41 2009 +0000
+++ b/vymmodel.cpp	Wed May 20 15:40:14 2009 +0000
@@ -30,6 +30,7 @@
 extern QString tmpVymDir;
 
 extern TextEditor *textEditor;
+extern FlagRow *standardFlagsMaster;
 
 extern QString clipboardDir;
 extern QString clipboardFile;
@@ -60,7 +61,7 @@
 
 VymModel::~VymModel() 
 {
-//    cout << "Destr VymModel\n";
+    cout << "Destr VymModel\n";
 	autosaveTimer->stop();
 	fileChangedTimer->stop();
 	clear();
@@ -71,7 +72,7 @@
 	selModel->clearSelection();
 
 	//QModelIndex ri=index(rootItem);
-	//removeRows (0, rowCount(ri),ri);		// FIXME-2 here should be at least a beginRemoveRows...
+	//removeRows (0, rowCount(ri),ri);		// FIXME-3 here should be at least a beginRemoveRows...
 }
 
 void VymModel::init () 
@@ -232,7 +233,7 @@
 	xml.incIndent();
 
 	// Find the used flags while traversing the tree	// FIXME-2 this can be done local to vymmodel maybe...
-	//FIXME-2 not used any longer: standardFlagsDefault->resetUsedCounter();
+	standardFlagsMaster->resetUsedCounter();
 	
 	// Reset the counters before saving
 	// TODO constr. of FIO creates lots of objects, better do this in some other way...
@@ -262,7 +263,7 @@
 	xml.decIndent();
 	s+=xml.endElement("vymmap");
 
-	// FIXME-2 if (writeflags) standardFlagsDefault->saveToDir (tmpdir+"/flags/","",writeflags);
+	if (writeflags) standardFlagsMaster->saveToDir (tmpdir+"/flags/","",writeflags);
 	return s;
 }
 
@@ -2008,7 +2009,6 @@
 MapCenterItem* VymModel::addMapCenter ()
 {
 	MapCenterItem *mci=addMapCenter (contextPos);
-	//FIXME-3 selection.select (mco);
 	updateActions();
 	emitShowSelection();
 	saveState (
@@ -2044,26 +2044,6 @@
 	// Create MapObj
 	BranchObj *newbo=mci->createMapObj(mapScene);
 		
-	/*
-
-	if (!mci->getHeading().isEmpty() ) 
-	{
-		newbo->updateHeading();
-		newbo->setColor (headingColor);
-	}	
-*/
-		
-	//newbo->updateLink();	//FIXME-3
-
-/*
-	//mapCenter->setMapEditor(mapEditor);		//FIXME-3 VM needed to get defLinkStyle, mapLinkColorHint ... for later added objects
-	mapCenter->setTreeItem (mci);	// TreeItem needs to exist before setVisibility
-	mci->setLMO (mapCenter);
-	mapCenter->updateHeading();
-	mapCenter->move (absPos);
-    mapCenter->setVisibility (true);
-	//mapCenter->setHeading (QApplication::translate("Heading of mapcenter in new map", "New map"));
-*/
 	return mci;
 }
 
@@ -2095,30 +2075,22 @@
 		beginInsertRows (parix,n,n+1);	
 		parbi->appendChild (newbi);	
 		endInsertRows ();
-	}else if (num==-1)
+	}else if (num==-1 || num==-3)
 	{
 		// insert below selection
 		parbi=(BranchItem*)dst->parent();
 		parix=index(parbi);
-		n=dst->childNumber()+1;
+		n=dst->childNumber()+ (3+num)/2;	//-1 |-> 1;-3 |-> 0
 		beginInsertRows (parix,n,n);	
 		parbi->insertBranch(n,newbi);	
 		endInsertRows ();
-	}else if (num==-3)
-	{
-		// insert above selection
-		parbi=(BranchItem*)dst->parent();
-		parix=index(parbi);
-		n=dst->childNumber();
-		beginInsertRows (parix,n,n);	
-		parbi->insertBranch(n,newbi);	
-		endInsertRows ();
-	} 
+	}
 	emit (layoutChanged() );
 
 	// save scroll state. If scrolled, automatically select
 	// new branch in order to tmp unscroll parent...
 	newbi->createMapObj(mapScene);
+	reposition();
 	select (newbi);
 	return newbi;
 }	
@@ -2165,7 +2137,7 @@
 }
 
 
-BranchItem* VymModel::addNewBranchBefore()	//FIXME-0
+BranchItem* VymModel::addNewBranchBefore()	
 {
 	BranchItem *newbi=NULL;
 	BranchItem *selbi=getSelectedBranchItem();
@@ -2315,24 +2287,29 @@
 	}	
 */}
 
-void VymModel::deleteChildren()		//FIXME-2 VM still missing
-
-{
-/*
-	BranchObj *bo=getSelectedBranch();
-	if (bo)
+void VymModel::deleteChildren()		
+
+{
+	BranchItem *selbi=getSelectedBranchItem();
+	if (selbi)
 	{		
 		saveStateChangingPart(
-			bo, 
-			bo,
+			selbi, 
+			selbi,
 			"deleteChildren ()",
-			QString( "Remove children of branch %1").arg(getObjectName(bo))
+			QString( "Remove children of branch %1").arg(getObjectName(selbi))
 		);
-		bo->removeChildren();
+		emit (layoutAboutToBeChanged() );
+
+		QModelIndex ix=index (selbi);
+		int n=selbi->branchCount()-1;
+		beginRemoveRows (ix,0,n);
+		removeRows (0,n+1,ix);
+		endRemoveRows();
+		emit (layoutChanged() );
 		reposition();
 	}	
-*/}
-
+}
 
 bool VymModel::scrollBranch(BranchItem *bi)
 {
@@ -4972,7 +4949,7 @@
 	return NULL;
 }
 
-TreeItem* VymModel::getSelectedItem()
+TreeItem* VymModel::getSelectedItem()	
 {
 	QModelIndexList list=selModel->selectedIndexes();
 	if (!list.isEmpty() )
diff -r 01f2f6d6789d -r e3f722759c7e vymview.cpp
--- a/vymview.cpp	Mon May 18 11:22:41 2009 +0000
+++ b/vymview.cpp	Wed May 20 15:40:14 2009 +0000
@@ -3,7 +3,6 @@
 #include <iostream>
 using namespace std;
 
-#include "linkablemapobj.h"
 #include "mainwindow.h"
 #include "mapeditor.h"
 #include "treeeditor.h"
@@ -34,13 +33,7 @@
 	connect (
 		selModel, SIGNAL (selectionChanged(const QItemSelection &, const QItemSelection &)), 
 		mapEditor,SLOT (updateSelection(const QItemSelection &,const QItemSelection &)));
-		/*
-	connect (
-		selModel, SIGNAL (currentChanged(const QModelIndex &, const QModelIndex &)), 
-		me,SLOT (updateCurrent(const QModelIndex &,const QModelIndex &)));
-		*/
-		/*
-*/
+
 	connect (
 		model, SIGNAL (dataChanged(const QModelIndex &, const QModelIndex &)), 
 		mapEditor,SLOT (updateData(const QModelIndex &) ) );
@@ -76,6 +69,21 @@
 	setSizes(widths);
 }
 
+VymView::~VymView()
+{
+	//cout << "Destructor VymView\n";
+}
+
+VymModel* VymView::getModel()
+{
+	return model;
+}
+
+MapEditor* VymView::getMapEditor()
+{
+	return mapEditor;
+}
+
 void VymView::initFocus()
 {
 	mapEditor->setFocus();
@@ -92,16 +100,6 @@
 
 void VymView::changeSelection (const QItemSelection &newsel, const QItemSelection &oldsel)
 {
-	/*
-	cout <<"VymView::changeSelection (";
-	if (!newsel.indexes().isEmpty() )
-		cout << model->getItem(newsel.indexes().first() )->getHeading().toStdString();
-	cout << " <- ";
-	if (!oldsel.indexes().isEmpty() )
-		cout << model->getItem(oldsel.indexes().first() )->getHeading().toStdString();
-	cout << ")\n";
-	*/
-
 	// Notify mainwindow to update satellites like NoteEditor, if needed (model==currenModel...)
 	mainWindow->changeSelection (model,newsel,oldsel);	// FIXME-3 maybe connect VymModel <-> MainWindow directly?
 	showSelection();
@@ -114,14 +112,8 @@
 
 void VymView::showSelection()
 {
-	treeEditor->scrollTo(
-		model->getSelectedIndex(), 
-		//QAbstractItemView::PositionAtCenter   
-		QAbstractItemView::EnsureVisible
-	);
-
-	LinkableMapObj* lmo=model->getSelectedLMO();
-	if (lmo) 
-		mapEditor->setScrollBarPosTarget (lmo->getBBox() );
+	QModelIndex ix=model->getSelectedIndex();
+	treeEditor->scrollTo( ix, QAbstractItemView::EnsureVisible);
+	mapEditor->scrollTo ( ix);
 }
 
diff -r 01f2f6d6789d -r e3f722759c7e vymview.h
--- a/vymview.h	Mon May 18 11:22:41 2009 +0000
+++ b/vymview.h	Wed May 20 15:40:14 2009 +0000
@@ -15,6 +15,9 @@
 	Q_OBJECT
 public:
 	VymView(VymModel *model);
+	~VymView();
+	VymModel* getModel();
+	MapEditor* getMapEditor();
 	void initFocus();
 	QItemSelectionModel* selectionModel();
 
diff -r 01f2f6d6789d -r e3f722759c7e xml-vym.cpp
--- a/xml-vym.cpp	Mon May 18 11:22:41 2009 +0000
+++ b/xml-vym.cpp	Wed May 20 15:40:14 2009 +0000
@@ -356,8 +356,7 @@
 
 bool parseVYMHandler::readBranchAttr (const QXmlAttributes& a)	//FIXME-2
 {
-	branchesCurrent++;
-	mainWindow->setProgressValue (branchesCurrent);
+	mainWindow->setProgressValue (branchesCurrent++);
 
 	if (!readOOAttr(a)) return false;