# HG changeset patch
# User insilmaril
# Date 1237799211 0
# Node ID 2d4cc445a86aa893a25ff1bb1bc7d7cbb785ab89
# Parent  94ed4372ef08b54c1e19f1138f6d2c40773896ac
still working on insert/remove of rows

diff -r 94ed4372ef08 -r 2d4cc445a86a branchobj.cpp
--- a/branchobj.cpp	Thu Mar 19 11:45:28 2009 +0000
+++ b/branchobj.cpp	Mon Mar 23 09:06:51 2009 +0000
@@ -60,7 +60,7 @@
 		model->stopAnimation (this);
 	}
 
-	//cout << "Destr BranchObj of "<<this<<endl;
+	cout << "Destr BranchObj of "<<this<<endl;
 	// Check, if this branch was the last child to be deleted
 	// If so, unset the scrolled flags
 
diff -r 94ed4372ef08 -r 2d4cc445a86a treeitem.cpp
--- a/treeitem.cpp	Thu Mar 19 11:45:28 2009 +0000
+++ b/treeitem.cpp	Mon Mar 23 09:06:51 2009 +0000
@@ -23,7 +23,6 @@
 void TreeItem::appendChild(TreeItem *item)
 {
     childItems.append(item);
-	
 
 	if (item->type == Branch || item->type ==MapCenter)
 	{
@@ -33,14 +32,17 @@
 	}
 }
 
+#include <iostream>
+using namespace std;
 void TreeItem::removeChild(int row)
 {
-    if (row>=0)
+    if (row<0 || row > childItems.size()-1)
+		qWarning ("TreeItem::removeChild tried to remove non existing item?!\n");
+	else
 	{
-		delete (childItems.at(row) );
-		childItems.removeAt (row);
-	} else
-		qWarning ("TreeItem::removeChild tried to remove non existing item?!\n");
+		cout << "TI::removeChild this="<<this<<"  row="<<row<<endl;
+		delete childItems.takeAt (row);
+	}
 }
 
 TreeItem *TreeItem::child(int row)
@@ -53,6 +55,14 @@
     return childItems.count();
 }
 
+int TreeItem::childNumber() const
+{
+    if (parentItem)
+        return parentItem->childItems.indexOf(const_cast<TreeItem*>(this));
+
+    return 0;
+}
+
 int TreeItem::columnCount() const
 {
     return itemData.count();
diff -r 94ed4372ef08 -r 2d4cc445a86a treeitem.h
--- a/treeitem.h	Thu Mar 19 11:45:28 2009 +0000
+++ b/treeitem.h	Mon Mar 23 09:06:51 2009 +0000
@@ -19,6 +19,7 @@
 
     TreeItem *child(int row);
     int childCount() const;
+    int childNumber() const;
     int columnCount() const;
 	int branchCount() const;
 
diff -r 94ed4372ef08 -r 2d4cc445a86a treemodel.cpp
--- a/treemodel.cpp	Thu Mar 19 11:45:28 2009 +0000
+++ b/treemodel.cpp	Mon Mar 23 09:06:51 2009 +0000
@@ -27,7 +27,7 @@
     if (role != Qt::DisplayRole)
         return QVariant();
 
-    TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
+    TreeItem *item = getItem (index);
 
     return item->data(index.column());
 }
@@ -57,7 +57,7 @@
     if (!parent.isValid())
         parentItem = rootItem;
     else
-        parentItem = static_cast<TreeItem*>(parent.internalPointer());
+        parentItem = getItem (parent);
 
     TreeItem *childItem = parentItem->child(row);
     if (childItem)
@@ -71,13 +71,20 @@
     if (!index.isValid())
         return QModelIndex();
 
-    TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
-    TreeItem *parentItem = childItem->parent();
+    TreeItem *ti= getItem (index);
+    TreeItem *parentItem = ti->parent();
 
+	//cout << "TreeModel::parent  ti="<<ti<<" "<<ti->getHeading().toStdString()<<"  pi="<<parentItem<<"  "<<endl;
     if (parentItem == rootItem)
         return QModelIndex();
 
-    return createIndex(parentItem->row(), 0, parentItem);
+	if (!parentItem)
+	{
+		cout <<"TreeModel::parent      ti=="<<ti<<"  "<<ti->getHeading().toStdString()<<endl;
+        return QModelIndex();	// FIXME do this to avoid segfault, but why?
+	}	
+
+    return createIndex(parentItem->childNumber(), 0, parentItem);
 }
 
 int TreeModel::rowCount(const QModelIndex &parent) const
@@ -87,7 +94,7 @@
     if (!parent.isValid())
         parentItem = rootItem;
     else
-        parentItem = static_cast<TreeItem*>(parent.internalPointer());
+        parentItem = getItem (parent);
 
     return parentItem->childCount();
 }
@@ -95,7 +102,7 @@
 int TreeModel::columnCount(const QModelIndex &parent) const
 {
     if (parent.isValid())
-        return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
+        return getItem (parent)->columnCount();
     else
         return rootItem->columnCount();
 }
@@ -158,20 +165,47 @@
 	return current;
 }
 
+bool TreeModel::insertRows ( int row, int count, const QModelIndex & parent)
+{
+	std::cout << "TreeModel insertRows()\n";
+	int last=row+count-1;
+	beginInsertRows (parent,row,last);
+
+	for (int i=row; i<=last; i++)
+	{
+		std::cout << "TreeModel::insertRows inserting i="<<i<<std::endl;
+	}
+	endInsertRows ();
+	return true;
+}
+
 bool TreeModel::removeRows ( int row, int count, const QModelIndex & parent)
 {
 	int last=row+count-1;
+	cout << "TreeModel::removeRows row="<<row<<"  count="<<count<<"  last="<<last<<endl;
+    TreeItem *pi= getItem (parent);
+	cout << "  pi="<<pi<<"  "<<pi->getHeading().toStdString()<<endl;
+	cout << "  ok0\n";
 	beginRemoveRows (parent,row,last);
+	cout << "  ok1\n";
 
-    TreeItem *pi= static_cast<TreeItem*>(parent.internalPointer());
 	for (int i=row; i<=last; i++)
 	{
-		std::cout << "TreeModel::removeRows removing i="<<i<<std::endl;
+		cout << "TreeModel::removeRows removing i="<<i<<std::endl;
 		pi->removeChild (row);
 	}
+	endRemoveRows ();
+	return true;
+}
 
-	endRemoveRows ();
-	
+TreeItem *TreeModel::getItem(const QModelIndex &index) const
+{
+    if (index.isValid()) {
+		TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
+
+        if (item) return item;
+    }
+    return rootItem;
 }
 
 QModelIndex TreeModel::index (TreeItem* ti)
diff -r 94ed4372ef08 -r 2d4cc445a86a treemodel.h
--- a/treemodel.h	Thu Mar 19 11:45:28 2009 +0000
+++ b/treemodel.h	Mon Mar 23 09:06:51 2009 +0000
@@ -29,9 +29,12 @@
 
 	TreeItem* next(TreeItem* &current, TreeItem* &previous, int &d0);
 
+	bool insertRows ( int row, int count, 
+				const QModelIndex & parent = QModelIndex() ); 
 	bool removeRows ( int row, int count, 
 				const QModelIndex & parent = QModelIndex() ); 
 
+	TreeItem* getItem (const QModelIndex &index) const;
 	QModelIndex index (TreeItem* ti);
 
 protected:
diff -r 94ed4372ef08 -r 2d4cc445a86a vymmodel.cpp
--- a/vymmodel.cpp	Thu Mar 19 11:45:28 2009 +0000
+++ b/vymmodel.cpp	Mon Mar 23 09:06:51 2009 +0000
@@ -73,7 +73,7 @@
 		delete mapCenters.takeFirst();
 
 	QModelIndex ri=index(rootItem);
-	removeRows (0, rowCount(ri),ri);	
+	//removeRows (0, rowCount(ri),ri);		// FIXME here should be at least a beginRemoveRows...
 }
 
 void VymModel::init () 
@@ -233,17 +233,17 @@
 	s+=xml.beginElement("vymmap",mapAttr);
 	xml.incIndent();
 
-	// Find the used flags while traversing the tree
+	// Find the used flags while traversing the tree	// FIXME this can be done local to vymmodel maybe...
 	standardFlagsDefault->resetUsedCounter();
 	
 	// Reset the counters before saving
 	// TODO constr. of FIO creates lots of objects, better do this in some other way...
-	FloatImageObj (mapScene).resetSaveCounter();
+	FloatImageObj (mapScene).resetSaveCounter();// FIXME this can be done local to vymmodel maybe...
 
 	// Build xml recursivly
 	if (!saveSel || typeid (*saveSel) == typeid (MapCenterObj))
-		// Save complete map, if saveSel not set
-		s+=saveToDir(tmpdir,prefix,writeflags,offset);
+		// Save all mapcenters as complete map, if saveSel not set
+		s+=saveTreeToDir(tmpdir,prefix,writeflags,offset);
 	else
 	{
 		if ( typeid(*saveSel) == typeid(BranchObj) )
@@ -269,6 +269,15 @@
 	return s;
 }
 
+QString VymModel::saveTreeToDir (const QString &tmpdir,const QString &prefix, int verbose, const QPointF &offset)
+{
+    QString s;
+
+	for (int i=0; i<mapCenters.count(); i++)
+		s+=mapCenters.at(i)->saveToDir (tmpdir,prefix,verbose,offset);
+    return s;
+}
+
 void VymModel::setFilePath(QString fpath, QString destname)
 {
 	if (fpath.isEmpty() || fpath=="")
@@ -1409,15 +1418,6 @@
 	return NULL;
 }
 
-QString VymModel::saveToDir (const QString &tmpdir,const QString &prefix, int verbose, const QPointF &offset)
-{
-    QString s;
-
-	for (int i=0; i<mapCenters.count(); i++)
-		s+=mapCenters.at(i)->saveToDir (tmpdir,prefix,verbose,offset);
-    return s;
-}
-
 //////////////////////////////////////////////
 // Interface 
 //////////////////////////////////////////////
@@ -2032,6 +2032,14 @@
 	mapCenters.append(mapCenter);
 
 	// Create TreeItem
+	QModelIndex parix=index(rootItem);
+
+	int n=rootItem->branchCount();
+	cout << "VM::addMapCenter n="<<n<<endl;
+
+	emit (layoutAboutToBeChanged() );
+	beginInsertRows (parix,n,n+1);
+
 	QList<QVariant> cData;
 	cData << "VM:addMapCenter" << "undef"<<"undef";
 	TreeItem *ti=new TreeItem (cData,rootItem);
@@ -2040,6 +2048,20 @@
 	mapCenter->setTreeItem (ti);
 	rootItem->appendChild (ti);
 
+	endInsertRows();
+	emit (newChildObject (parix));
+	emit (layoutChanged() );
+
+	// Testing
+/*
+	qWarning ("MW::insertRow a");
+    if (!insertRow(0, parix))
+	{
+		std::cout << "  war nix...\n";
+	}	
+	qWarning ("MW::insertRow b");
+*/
+
 	return mapCenter;
 }
 
@@ -2084,12 +2106,22 @@
 
 			// Create TreeItem
 			QList<QVariant> cData;
-			cData << "VM:createBranch" << "undef"<<"undef";
+			cData << "new" << "undef"<<"undef";
+
 			TreeItem *parti=bo->getTreeItem();
+			QModelIndex parix=index(parti);
+			int n=parti->branchCount();
+
+			emit (layoutAboutToBeChanged() );
+			beginInsertRows (parix,n,n+1);
 			TreeItem *ti=new TreeItem (cData,parti);
 			ti->setLMO (newbo);
 			ti->setType (TreeItem::Branch);
+
 			parti->appendChild (ti);
+			endInsertRows ();
+			emit (newChildObject (parix));
+			emit (layoutChanged() );
 
 			if (newbo)
 			{
@@ -2101,12 +2133,12 @@
 		{
 			num=bo->getNum()+1;
 			bo=(BranchObj*)bo->getParObj();
-			if (bo) newbo=bo->insertBranch(num);
+			if (bo) newbo=bo->insertBranch(num);	//FIXME VM still missing 
 		}else if (num==-3)
 		{
 			num=bo->getNum();
 			bo=(BranchObj*)bo->getParObj();
-			if (bo) newbo=bo->insertBranch(num);
+			if (bo) newbo=bo->insertBranch(num);	//FIXME VM still missing
 		}
 	}	
 	return newbo;
@@ -2165,7 +2197,8 @@
 		BranchObj *parbo=(BranchObj*)(bo->getParObj());
 
 		// add below selection
-		newbo=parbo->insertBranch(bo->getNum()+1);
+		newbo=parbo->insertBranch(bo->getNum()+1);		//FIXME VM still missing
+
 		if (newbo)
 		{
 			newbo->move2RelPos (p);
@@ -2186,15 +2219,24 @@
 
 void VymModel::deleteSelection()
 {
-	BranchObj *bo = getSelectedBranch();
+	BranchObj *bo = getSelectedBranch();	// FIXME VM should not be necessary
+	if (!bo) return;
+
+	QModelIndex ix=getSelectedIndex();
+	if (!ix.isValid() ) return;
+
+	QModelIndex parentIndex=parent(ix);
+	if (!parentIndex.isValid()) return;
+	TreeItem *ti=bo->getTreeItem();
 	
-	if (bo && selectionType()==TreeItem::MapCenter)
+	if (selectionType()==TreeItem::MapCenter)		//FIXME VM still missing
 	{
 	//	BranchObj* par=(BranchObj*)(bo->getParObj());
-		selection.unselect();
-	/* FIXME VM Note:  does saveStateRemovingPart work for MCO? (No parent!)
+		//selection.unselect();
+		/* FIXME VM Note:  does saveStateRemovingPart work for MCO? (No parent!)
 		saveStateRemovingPart (bo, QString ("Delete %1").arg(getObjectName(bo)));
 		*/
+		/*
 		bo=removeMapCenter ((MapCenterObj*)bo);
 		if (bo) 
 		{
@@ -2204,22 +2246,34 @@
 		}	
 		reposition();
 		return;
+		*/
 	}
-	if (bo && selectionType()==TreeItem::Branch)
+	if (selectionType()==TreeItem::Branch)
 	{
-		QModelIndex ix=getSelectedIndex();
+		int n=ti->branchCount();
 
 		BranchObj* par=(BranchObj*)bo->getParObj();
 		unselect();
 		saveStateRemovingPart (bo, QString ("Delete %1").arg(getObjectName(bo)));
+
+		emit (layoutAboutToBeChanged() );
+
+		cout << "VM::delete  ti="<<ti<<"  row="<<ix.row()<<endl;
+		parentIndex=parent(index(ti));
+		cout << "            valid parentIndex="<<parentIndex.isValid()<<endl;
+		beginRemoveRows (parentIndex,n,n);
+		removeRows (ix.row(),1,parentIndex);
+		endRemoveRows();
 		par->removeBranch(bo);
 		select (par);
 		ensureSelectionVisible();
 		reposition();
-		selection.update();
+
+		emit (layoutChanged() );
 		return;
 	}
-	FloatImageObj *fio=selection.getFloatImage();
+	FloatImageObj *fio=selection.getFloatImage(); 	//FIXME VM still missing
+
 	if (fio)
 	{
 		BranchObj* par=(BranchObj*)fio->getParObj();
@@ -2233,13 +2287,13 @@
 		par->removeFloatImage(fio);
 		select (par);
 		reposition();
-		selection.update();
 		ensureSelectionVisible();
 		return;
 	}
 }
 
-void VymModel::deleteKeepChildren()
+void VymModel::deleteKeepChildren()		//FIXME VM still missing
+
 {
 	BranchObj *bo=getSelectedBranch();
 	BranchObj *par;
@@ -2275,7 +2329,8 @@
 	}	
 }
 
-void VymModel::deleteChildren()
+void VymModel::deleteChildren()		//FIXME VM still missing
+
 {
 	BranchObj *bo=getSelectedBranch();
 	if (bo)
@@ -4439,7 +4494,7 @@
 {
 	QModelIndex index=selModel->selectedIndexes().first();	// TODO no multiselections yet
 
-	TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
+	TreeItem *item = getItem (index);
 	return select (item->getLMO() );
 }
 
@@ -4848,7 +4903,7 @@
 {
 	QModelIndexList list=selModel->selectedIndexes();
 	if (list.isEmpty()) return TreeItem::Undefined;	
-	TreeItem *ti = static_cast<TreeItem*>(list.first().internalPointer());
+	TreeItem *ti = getItem (list.first() );
 	return ti->getType();
 
 }
@@ -4858,7 +4913,7 @@
 	QModelIndexList list=selModel->selectedIndexes();
 	if (!list.isEmpty() )
 	{
-		TreeItem *ti = static_cast<TreeItem*>(list.first().internalPointer());
+		TreeItem *ti = getItem (list.first() );
 		TreeItem::Type type=ti->getType();
 		if (type ==TreeItem::Branch || type==TreeItem::MapCenter || type==TreeItem::Image)
 		{
@@ -4882,7 +4937,7 @@
 	QModelIndexList list=selModel->selectedIndexes();
 	if (!list.isEmpty() )
 	{
-		TreeItem *ti = static_cast<TreeItem*>(list.first().internalPointer());
+		TreeItem *ti = getItem (list.first() );
 		TreeItem::Type type=ti->getType();
 		if (type ==TreeItem::Branch || type==TreeItem::MapCenter)
 			return ti;
diff -r 94ed4372ef08 -r 2d4cc445a86a vymmodel.h
--- a/vymmodel.h	Thu Mar 19 11:45:28 2009 +0000
+++ b/vymmodel.h	Mon Mar 23 09:06:51 2009 +0000
@@ -68,6 +68,10 @@
 	*/	
     QString saveToDir (const QString &tmpdir, const QString &prefix, bool writeflags, const QPointF &offset, LinkableMapObj *saveSel);
 
+	/*! Save all data in tree*/
+	QString saveTreeToDir (const QString&,const QString&,int, const QPointF&);// Save data recursivly to tempdir
+
+
 	/*! \brief Sets filepath, filename and mapname
 
 	     If the filepath is "/home/tux/map.xml", then the filename will be set
@@ -216,8 +220,6 @@
     LinkableMapObj* findObjBySelect (const QString &s);		// find MapObj by select string
     LinkableMapObj* findID (const QString &s);				// find MapObj by previously set ID
 
-	QString saveToDir (const QString&,const QString&,int, const QPointF&);// Save data recursivly to tempdir
-
 
 ////////////////////////////////////////////
 // Interface 
@@ -415,6 +417,9 @@
 
 	//void ensureSelectionVisible();		//!< Show selection in all views
 
+signals:
+	void newChildObject(QModelIndex ix);
+
 private:
 	MapEditor *mapEditor;
 
diff -r 94ed4372ef08 -r 2d4cc445a86a vymview.cpp
--- a/vymview.cpp	Thu Mar 19 11:45:28 2009 +0000
+++ b/vymview.cpp	Mon Mar 23 09:06:51 2009 +0000
@@ -39,6 +39,9 @@
 	connect (
 		model, SIGNAL (selectionChanged(const QItemSelection &, const QItemSelection &)), 
 		me,SLOT (updateSelection(const QItemSelection &,const QItemSelection &)));
+	connect (
+		model, SIGNAL (newChildObject(QModelIndex) ),
+		this,SLOT (updateChilds (QModelIndex) ) );
 
 	//me->viewport()->setFocus();	//FIXME needed?
 	me->setAntiAlias (mainWindow->isAliased());
@@ -64,13 +67,16 @@
 }
 
 
+void VymView::updateChilds (QModelIndex ix)
+{
+	treeview->setExpanded (ix,true);
+}
+
 void VymView::changeSelection (const QItemSelection &, const QItemSelection &)
 {
 	cout << "VymView::changeSelection (newsel,delsel)\n";
 	//treeview->expandAll();	//FIXME only for testing
 
-	//((VymModel*)treeview->model())->select ();
-
 	// Show URL and link in statusbar
 	QString status;
 	QString s=model->getURL();
@@ -79,12 +85,9 @@
 	if (!s.isEmpty() ) status+="Link: "+s;
 	if (!status.isEmpty() ) mainWindow->statusMessage (status);
 
-/* FIXME, was so far in BranchObj
-	// Update Toolbar
+	// Update Toolbar // FIXME, was so far in BranchObj
 	//updateFlagsToolbar();
 
-*/
-
 	// Update actions
 	mainWindow->updateActions();
 }
diff -r 94ed4372ef08 -r 2d4cc445a86a vymview.h
--- a/vymview.h	Thu Mar 19 11:45:28 2009 +0000
+++ b/vymview.h	Mon Mar 23 09:06:51 2009 +0000
@@ -16,6 +16,7 @@
 	QItemSelectionModel* selectionModel();
 
 public slots:
+	void updateChilds (QModelIndex ix);
 	void changeSelection (const QItemSelection &newSel, const QItemSelection &delSel);
 
 private: