vymmodel.cpp
changeset 788 78ba80b54bc4
parent 787 c6bb4fdcc55f
child 790 133e2ed6b9c5
     1.1 --- a/vymmodel.cpp	Sat Aug 08 21:58:26 2009 +0000
     1.2 +++ b/vymmodel.cpp	Tue Aug 18 12:39:07 2009 +0000
     1.3 @@ -241,22 +241,29 @@
     1.4  	// Find the used flags while traversing the tree	// FIXME-2 this can be done local to vymmodel maybe...
     1.5  	standardFlagsMaster->resetUsedCounter();
     1.6  	
     1.7 -	// Reset the counters before saving
     1.8 -	// TODO constr. of FIO creates lots of objects, better do this in some other way...
     1.9 -	ImageItem().resetSaveCounter();// FIXME-2 this can be done local to vymmodel maybe...
    1.10 -
    1.11  	// Build xml recursivly
    1.12 -	if (!saveSel || saveSel->getType()==TreeItem::MapCenter)
    1.13 +	if (!saveSel)
    1.14  		// Save all mapcenters as complete map, if saveSel not set
    1.15  		s+=saveTreeToDir(tmpdir,prefix,writeflags,offset);
    1.16  	else
    1.17  	{
    1.18 -		if (saveSel->isBranchLikeType())
    1.19 -			// Save Subtree
    1.20 -			s+=((BranchItem*)saveSel)->saveToDir(tmpdir,prefix,offset);
    1.21 -		//FIXME-2 else if (saveSel->getType()==TreeItem::Image)
    1.22 -			// Save image
    1.23 -			//s+=((FloatImageObj*)(saveSel))->saveToDir(tmpdir,prefix);
    1.24 +		switch (saveSel->getType())
    1.25 +		{
    1.26 +			case TreeItem::Branch:
    1.27 +				// Save Subtree
    1.28 +				s+=((BranchItem*)saveSel)->saveToDir(tmpdir,prefix,offset);
    1.29 +				break;
    1.30 +			case TreeItem::MapCenter:
    1.31 +				// Save Subtree
    1.32 +				s+=((BranchItem*)saveSel)->saveToDir(tmpdir,prefix,offset);
    1.33 +				break;
    1.34 +			case TreeItem::Image:
    1.35 +				// Save Image
    1.36 +				s+=((ImageItem*)saveSel)->saveToDir(tmpdir,prefix);
    1.37 +				break;
    1.38 +			default://FIXME-4 other types shouldn't be safed...
    1.39 +				break;
    1.40 +		}
    1.41  	}
    1.42  
    1.43  	// Save local settings
    1.44 @@ -673,42 +680,55 @@
    1.45  		QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
    1.46  }
    1.47  
    1.48 -void VymModel::addMapInsertInt (const QString &path, int pos)
    1.49 -{
    1.50 -/* FIXME-2  addMapInsertInt not ported yet
    1.51 -	BranchObj *sel=getSelectedBranch();
    1.52 -	if (sel)
    1.53 +bool VymModel::addMapInsertInt (const QString &path)
    1.54 +{
    1.55 +	QString pathDir=path.left(path.findRev("/"));
    1.56 +	QDir d(pathDir);
    1.57 +	QFile file (path);
    1.58 +
    1.59 +	if (d.exists() )
    1.60  	{
    1.61 -		QString pathDir=path.left(path.findRev("/"));
    1.62 -		QDir d(pathDir);
    1.63 -		QFile file (path);
    1.64 -
    1.65 -		if (d.exists() )
    1.66 +		// We need to parse saved XML data
    1.67 +		parseVYMHandler handler;
    1.68 +		QXmlInputSource source( file);
    1.69 +		QXmlSimpleReader reader;
    1.70 +		reader.setContentHandler( &handler );
    1.71 +		reader.setErrorHandler( &handler );
    1.72 +		handler.setModel (this);
    1.73 +		handler.setTmpDir ( pathDir );	// needed to load files with rel. path
    1.74 +		handler.setLoadMode (ImportAdd);
    1.75 +		blockReposition=true;
    1.76 +		bool ok = reader.parse( source );
    1.77 +		blockReposition=false;
    1.78 +		if ( ok ) return true;
    1.79 +		{	
    1.80 +			// This should never ever happen
    1.81 +			QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path),
    1.82 +									handler.errorProtocol());
    1.83 +		}
    1.84 +	} else	
    1.85 +		QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
    1.86 +	return false;
    1.87 +}
    1.88 +
    1.89 +bool VymModel::addMapInsertInt (const QString &path, int pos)
    1.90 +{
    1.91 +	BranchItem *selbi=getSelectedBranch();
    1.92 +	if (selbi)
    1.93 +	{
    1.94 +		if (addMapInsertInt (path))
    1.95  		{
    1.96 -			// We need to parse saved XML data
    1.97 -			parseVYMHandler handler;
    1.98 -			QXmlInputSource source( file);
    1.99 -			QXmlSimpleReader reader;
   1.100 -			reader.setContentHandler( &handler );
   1.101 -			reader.setErrorHandler( &handler );
   1.102 -			handler.setModel (this);
   1.103 -			handler.setTmpDir ( pathDir );	// needed to load files with rel. path
   1.104 -			handler.setLoadMode (ImportAdd);
   1.105 -			blockReposition=true;
   1.106 -			bool ok = reader.parse( source );
   1.107 -			blockReposition=false;
   1.108 -			if (! ok ) 
   1.109 -			{	
   1.110 -				// This should never ever happen
   1.111 -				QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path),
   1.112 -										handler.errorProtocol());
   1.113 -			}
   1.114 -			if (sel->getDepth()>0)
   1.115 -				sel->getLastBranch()->linkTo (sel,pos);
   1.116 -		} else	
   1.117 +			if (selbi->depth()>0)
   1.118 +				relinkBranch (selbi->getLastBranch(), selbi,pos);
   1.119 +			return true;	
   1.120 +		} else
   1.121 +		{
   1.122  			QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
   1.123 +			return false;
   1.124 +		}	
   1.125  	}		
   1.126 -*/
   1.127 +	qWarning ("ME::addMapInsertInt nothing selected");
   1.128 +	return false;
   1.129  }
   1.130  
   1.131  ImageItem* VymModel::loadFloatImageInt (BranchItem *dst,QString fn)
   1.132 @@ -1013,11 +1033,7 @@
   1.133  	s=ti->getHeading();
   1.134  	if (s=="") s="unnamed";
   1.135  
   1.136 -	if (ti->isBranchLikeType() )	//FIXME-3 shouldnt there also be mapcenter??
   1.137 -		return QString("branch (%1)").arg(s);
   1.138 -	if (ti->getType()==TreeItem::Image)
   1.139 -		return QString ("image (%1)").arg(ti->getHeading());
   1.140 -	return QString("Unknown type has no name!");
   1.141 +	return QString ("%1 (%2)").arg(ti->getTypeName()).arg(s);
   1.142  }
   1.143  
   1.144  void VymModel::redo()
   1.145 @@ -1128,8 +1144,7 @@
   1.146  	QString bakMapDir(QString(tmpMapDir+"/undo-%1").arg(curStep));
   1.147  
   1.148  	// select  object before undo
   1.149 -	if (!undoSelection.isEmpty())
   1.150 -		select (undoSelection);
   1.151 +	select (undoSelection);
   1.152  
   1.153  	if (debug)
   1.154  	{
   1.155 @@ -1155,7 +1170,7 @@
   1.156  	redosAvail++;
   1.157  
   1.158  	blockSaveState=blockSaveStateOrg;
   1.159 -/* TODO remove testing
   1.160 +/* testing only
   1.161  	cout << "VymModel::undo() end\n";
   1.162  	cout << "    undosAvail="<<undosAvail<<endl;
   1.163  	cout << "    redosAvail="<<redosAvail<<endl;
   1.164 @@ -1327,10 +1342,11 @@
   1.165  		qWarning ("VymModel::saveStateRemovingPart  no redoSel given!");
   1.166  		return;
   1.167  	}
   1.168 -	QString undoSelection=getSelectString (redoSel->parent() );
   1.169 +	QString undoSelection;
   1.170  	QString redoSelection=getSelectString(redoSel);
   1.171  	if (redoSel->getType()==TreeItem::Branch) 
   1.172  	{
   1.173 +		undoSelection=getSelectString (redoSel->parent());
   1.174  		// save the selected branch of the map, Undo will insert part of map 
   1.175  		saveState (PartOfMap,
   1.176  			undoSelection, QString("addMapInsert (\"PATH\",%1)").arg(redoSel->num()),
   1.177 @@ -1338,6 +1354,15 @@
   1.178  			comment, 
   1.179  			redoSel);
   1.180  	}
   1.181 +	if (redoSel->getType()==TreeItem::MapCenter) 
   1.182 +	{
   1.183 +		// save the selected branch of the map, Undo will insert part of map 
   1.184 +		saveState (PartOfMap,
   1.185 +			undoSelection, QString("addMapInsert (\"PATH\")"),
   1.186 +			redoSelection, "delete ()", 
   1.187 +			comment, 
   1.188 +			redoSel);
   1.189 +	}
   1.190  }
   1.191  
   1.192  
   1.193 @@ -1983,10 +2008,10 @@
   1.194  	return NULL;
   1.195  }
   1.196  
   1.197 -AttributeItem* VymModel::addAttribute()
   1.198 -{
   1.199 -	TreeItem *selti=getSelectedItem();
   1.200 -	if (selti)
   1.201 +AttributeItem* VymModel::addAttribute()	// FIXME-2 savestate missing
   1.202 +{
   1.203 +	BranchItem *selbi=getSelectedBranch();
   1.204 +	if (selbi)
   1.205  	{
   1.206  		QList<QVariant> cData;
   1.207  		cData << "new attribute" << "undef"<<"undef";
   1.208 @@ -1994,10 +2019,10 @@
   1.209  
   1.210  		emit (layoutAboutToBeChanged() );
   1.211  
   1.212 -		QModelIndex parix=index(selti);
   1.213 -		int n=selti->getRowNumAppend (a);
   1.214 +		QModelIndex parix=index(selbi);
   1.215 +		int n=selbi->getRowNumAppend (a);
   1.216  		beginInsertRows (parix,n,n+1);	
   1.217 -		selti->appendChild (a);	
   1.218 +		selbi->appendChild (a);	
   1.219  		endInsertRows ();
   1.220  
   1.221  		emit (layoutChanged() );
   1.222 @@ -2037,7 +2062,7 @@
   1.223  
   1.224  	QList<QVariant> cData;
   1.225  	cData << "VM:addMapCenter" << "undef"<<"undef";
   1.226 -	BranchItem *newbi=new BranchItem (cData);
   1.227 +	BranchItem *newbi=new BranchItem (cData,rootItem);
   1.228  	newbi->setHeading (QApplication::translate("Heading of mapcenter in new map", "New map"));
   1.229  	rootItem->appendChild (newbi);
   1.230  
   1.231 @@ -2197,6 +2222,10 @@
   1.232  		dst->insertBranch (pos,branch);
   1.233  		endInsertRows();
   1.234  
   1.235 +		// Correct type if necessesary
   1.236 +		if (branch->getType()==TreeItem::MapCenter) 
   1.237 +			branch->setType(TreeItem::Branch);
   1.238 +
   1.239  		// reset parObj, fonts, frame, etc in related LMO or other view-objects
   1.240  		branch->updateStyles();
   1.241  
   1.242 @@ -2251,11 +2280,11 @@
   1.243  	return false;
   1.244  }
   1.245  
   1.246 -void VymModel::deleteSelection()	// FIXME-2 include fix for deleted mapcenters from 1.12.4
   1.247 +void VymModel::deleteSelection()	
   1.248  {
   1.249  	BranchItem *selbi=getSelectedBranch();
   1.250  
   1.251 -	if (selbi && selbi->isBranchLikeType() )
   1.252 +	if (selbi)
   1.253  	{
   1.254  		unselect();
   1.255  		saveStateRemovingPart (selbi, QString ("Delete %1").arg(getObjectName(selbi)));
   1.256 @@ -2268,7 +2297,7 @@
   1.257  		}
   1.258  		return;
   1.259  	}
   1.260 -	TreeItem *ti=getSelectedImage();
   1.261 +	TreeItem *ti=getSelectedItem();
   1.262  	if (ti->getType()==TreeItem::Image || ti->getType()==TreeItem::Attribute)
   1.263  	{
   1.264  		TreeItem *pi=ti->parent();
   1.265 @@ -2378,7 +2407,7 @@
   1.266  		reposition();
   1.267  
   1.268  		emit (layoutChanged() );
   1.269 -		if (pi->depth()>0) return pi;
   1.270 +		if (pi->depth()>=0) return pi;
   1.271  	}	
   1.272  	return NULL;
   1.273  }
   1.274 @@ -2873,22 +2902,29 @@
   1.275  	/////////////////////////////////////////////////////////////////////
   1.276  	} else if (com==QString("addMapInsert"))
   1.277  	{
   1.278 -		if (!selti)
   1.279 +		if (parser.parCount()==2)
   1.280  		{
   1.281 -			parser.setError (Aborted,"Nothing selected");
   1.282 -		} else if (! selbi )
   1.283 -		{				  
   1.284 -			parser.setError (Aborted,"Type of selection is not a branch");
   1.285 -		} else 
   1.286 -		{	
   1.287 -			if (parser.checkParCount(2))
   1.288 +
   1.289 +			if (!selti)
   1.290  			{
   1.291 +				parser.setError (Aborted,"Nothing selected");
   1.292 +			} else if (! selbi )
   1.293 +			{				  
   1.294 +				parser.setError (Aborted,"Type of selection is not a branch");
   1.295 +			} else 
   1.296 +			{	
   1.297  				t=parser.parString (ok,0);	// path to map
   1.298  				n=parser.parInt(ok,1);		// position
   1.299  				if (QDir::isRelativePath(t)) t=(tmpMapDir + "/"+t);
   1.300  				addMapInsertInt(t,n);	
   1.301  			}
   1.302 -		}
   1.303 +		} else if (parser.parCount()==1)
   1.304 +		{
   1.305 +			t=parser.parString (ok,0);	// path to map
   1.306 +			if (QDir::isRelativePath(t)) t=(tmpMapDir + "/"+t);
   1.307 +			addMapInsertInt(t);	
   1.308 +		} else
   1.309 +			parser.setError (Aborted,"Wrong number of parameters");
   1.310  	/////////////////////////////////////////////////////////////////////
   1.311  	} else if (com=="clearFlags")	
   1.312  	{
   1.313 @@ -4610,6 +4646,11 @@
   1.314  
   1.315  bool VymModel::select (const QString &s)
   1.316  {
   1.317 +	if (s.isEmpty())
   1.318 +	{
   1.319 +		unselect();
   1.320 +		return true;
   1.321 +	}
   1.322  	TreeItem *ti=findBySelectString(s);
   1.323  	if (ti) return select (index(ti));
   1.324  	return false;