vymmodel.cpp
author insilmaril
Wed, 16 Jul 2008 10:46:14 +0000
changeset 721 12958f987bcf
parent 696 0c2d74acf035
child 722 462d39502273
permissions -rw-r--r--
Started to restructure for later use of Model/View
insilmaril@650
     1
#include <QApplication>
insilmaril@687
     2
#include <typeinfo>
insilmaril@650
     3
insilmaril@721
     4
#include "vymmodel.h"
insilmaril@721
     5
insilmaril@721
     6
#include "editxlinkdialog.h"
insilmaril@721
     7
#include "exports.h"
insilmaril@721
     8
#include "exportxhtmldialog.h"
insilmaril@721
     9
#include "file.h"
insilmaril@650
    10
#include "geometry.h"		// for addBBox
insilmaril@721
    11
#include "mainwindow.h"
insilmaril@721
    12
#include "mapcenterobj.h"
insilmaril@721
    13
#include "misc.h"
insilmaril@721
    14
#include "parser.h"
insilmaril@721
    15
#include "selection.h"
insilmaril@721
    16
insilmaril@721
    17
insilmaril@721
    18
#include "warningdialog.h"
insilmaril@721
    19
#include "xml-freemind.h"
insilmaril@721
    20
#include "xml-vym.h"
insilmaril@721
    21
insilmaril@721
    22
insilmaril@721
    23
extern bool debug;
insilmaril@721
    24
extern Main *mainWindow;
insilmaril@696
    25
extern Settings settings;
insilmaril@721
    26
extern QString tmpVymDir;
insilmaril@721
    27
insilmaril@721
    28
extern TextEditor *textEditor;
insilmaril@721
    29
insilmaril@721
    30
insilmaril@721
    31
extern QString clipboardDir;
insilmaril@721
    32
extern QString clipboardFile;
insilmaril@721
    33
extern bool clipboardEmpty;
insilmaril@721
    34
insilmaril@721
    35
extern ImageIO imageIO;
insilmaril@721
    36
insilmaril@721
    37
extern QString vymName;
insilmaril@721
    38
extern QString vymVersion;
insilmaril@721
    39
extern QDir vymBaseDir;
insilmaril@721
    40
insilmaril@721
    41
extern QDir lastImageDir;
insilmaril@721
    42
extern QDir lastFileDir;
insilmaril@721
    43
insilmaril@721
    44
extern FlagRowObj *standardFlagsDefault;
insilmaril@721
    45
insilmaril@721
    46
extern Settings settings;
insilmaril@721
    47
insilmaril@721
    48
insilmaril@721
    49
insilmaril@721
    50
int VymModel::mapNum=0;	// make instance
insilmaril@696
    51
insilmaril@650
    52
VymModel::VymModel() 
insilmaril@650
    53
{
insilmaril@650
    54
//    cout << "Const VymModel\n";
insilmaril@721
    55
	init();
insilmaril@650
    56
}
insilmaril@650
    57
insilmaril@650
    58
insilmaril@650
    59
VymModel::~VymModel() 
insilmaril@650
    60
{
insilmaril@650
    61
//    cout << "Destr VymModel\n";
insilmaril@721
    62
	autosaveTimer->stop();
insilmaril@721
    63
	fileChangedTimer->stop();
insilmaril@721
    64
	clear();
insilmaril@650
    65
}	
insilmaril@650
    66
insilmaril@650
    67
void VymModel::clear() 
insilmaril@650
    68
{
insilmaril@721
    69
	selection.clear();
insilmaril@721
    70
insilmaril@665
    71
	while (!mapCenters.isEmpty())
insilmaril@665
    72
		delete mapCenters.takeFirst();
insilmaril@650
    73
}
insilmaril@650
    74
insilmaril@650
    75
void VymModel::init () 
insilmaril@650
    76
{
insilmaril@721
    77
	// We should have at least one map center to start with
insilmaril@721
    78
	// addMapCenter();  FIXME create this in MapEditor as long as model is part of that
insilmaril@721
    79
insilmaril@721
    80
	// History 
insilmaril@721
    81
	mapNum++;
insilmaril@721
    82
    mapChanged=false;
insilmaril@721
    83
	mapDefault=true;
insilmaril@721
    84
	mapUnsaved=false;
insilmaril@721
    85
insilmaril@721
    86
	curStep=0;
insilmaril@721
    87
	redosAvail=0;
insilmaril@721
    88
	undosAvail=0;
insilmaril@721
    89
insilmaril@721
    90
 	stepsTotal=settings.readNumEntry("/history/stepsTotal",100);
insilmaril@721
    91
	undoSet.setEntry ("/history/stepsTotal",QString::number(stepsTotal));
insilmaril@721
    92
	mainWindow->updateHistory (undoSet);
insilmaril@721
    93
insilmaril@721
    94
	// Create tmp dirs
insilmaril@721
    95
	makeTmpDirectories();
insilmaril@721
    96
	
insilmaril@721
    97
	// Files
insilmaril@721
    98
	zipped=true;
insilmaril@721
    99
	filePath="";
insilmaril@721
   100
	fileName=tr("unnamed");
insilmaril@721
   101
	mapName="";
insilmaril@721
   102
	blockReposition=false;
insilmaril@721
   103
	blockSaveState=false;
insilmaril@721
   104
insilmaril@721
   105
	autosaveTimer=new QTimer (this);
insilmaril@721
   106
	connect(autosaveTimer, SIGNAL(timeout()), this, SLOT(autosave()));
insilmaril@721
   107
insilmaril@721
   108
	fileChangedTimer=new QTimer (this);
insilmaril@721
   109
	fileChangedTimer->start(3000);
insilmaril@721
   110
	connect(fileChangedTimer, SIGNAL(timeout()), this, SLOT(fileChanged()));
insilmaril@721
   111
insilmaril@721
   112
insilmaril@721
   113
	// selections
insilmaril@721
   114
	selection.setModel (this);
insilmaril@721
   115
	selection.unselect();
insilmaril@721
   116
insilmaril@721
   117
	// find routine
insilmaril@721
   118
	itFind=NULL;				
insilmaril@721
   119
	EOFind=false;
insilmaril@696
   120
insilmaril@696
   121
	// animations
insilmaril@696
   122
	animationUse=settings.readBoolEntry("/animation/use",false);
insilmaril@696
   123
	animationTicks=settings.readNumEntry("/animation/ticks",10);
insilmaril@696
   124
	animationInterval=settings.readNumEntry("/animation/interval",50);
insilmaril@696
   125
	animObjList.clear();
insilmaril@696
   126
	animationTimer=new QTimer (this);
insilmaril@696
   127
	connect(animationTimer, SIGNAL(timeout()), this, SLOT(animate()));
insilmaril@696
   128
insilmaril@721
   129
	// View - map
insilmaril@721
   130
	defLinkColor=QColor (0,0,255);
insilmaril@721
   131
	defXLinkColor=QColor (180,180,180);
insilmaril@721
   132
	linkcolorhint=LinkableMapObj::DefaultColor;
insilmaril@721
   133
	linkstyle=LinkableMapObj::PolyParabel;
insilmaril@721
   134
	defXLinkWidth=1;
insilmaril@721
   135
	defXLinkColor=QColor (230,230,230);
insilmaril@721
   136
insilmaril@721
   137
	hidemode=HideNone;
insilmaril@721
   138
insilmaril@721
   139
insilmaril@721
   140
insilmaril@721
   141
	// Network
insilmaril@721
   142
	netstate=Offline;
insilmaril@721
   143
insilmaril@721
   144
	// Create MapCenter
insilmaril@721
   145
	//  addMapCenter();  FIXME create this in MapEditor until BO and MCO are independent of scene
insilmaril@721
   146
insilmaril@650
   147
}
insilmaril@650
   148
insilmaril@721
   149
void VymModel::makeTmpDirectories()
insilmaril@721
   150
{
insilmaril@721
   151
	// Create unique temporary directories
insilmaril@721
   152
	tmpMapDir = tmpVymDir+QString("/model-%1").arg(mapNum);
insilmaril@721
   153
	histPath = tmpMapDir+"/history";
insilmaril@721
   154
	QDir d;
insilmaril@721
   155
	d.mkdir (tmpMapDir);
insilmaril@721
   156
}
insilmaril@721
   157
insilmaril@721
   158
insilmaril@650
   159
void VymModel::setMapEditor(MapEditor *me)
insilmaril@650
   160
{
insilmaril@650
   161
	mapEditor=me;
insilmaril@650
   162
	for (int i=0; i<mapCenters.count(); i++)
insilmaril@650
   163
		mapCenters.at(i)->setMapEditor(mapEditor);
insilmaril@650
   164
}
insilmaril@650
   165
insilmaril@650
   166
MapEditor* VymModel::getMapEditor()
insilmaril@650
   167
{
insilmaril@650
   168
	return mapEditor;
insilmaril@650
   169
}
insilmaril@650
   170
insilmaril@721
   171
bool VymModel::isRepositionBlocked()
insilmaril@650
   172
{
insilmaril@721
   173
	return blockReposition;
insilmaril@650
   174
}
insilmaril@650
   175
insilmaril@721
   176
void VymModel::updateActions()	// FIXME  maybe don't update if blockReposition is set
insilmaril@650
   177
{
insilmaril@721
   178
	// Tell mainwindow to update states of actions
insilmaril@721
   179
	mainWindow->updateActions();
insilmaril@650
   180
}
insilmaril@650
   181
insilmaril@721
   182
insilmaril@721
   183
insilmaril@721
   184
QString VymModel::saveToDir(const QString &tmpdir, const QString &prefix, bool writeflags, const QPointF &offset, LinkableMapObj *saveSel)
insilmaril@650
   185
{
insilmaril@721
   186
	// tmpdir		temporary directory to which data will be written
insilmaril@721
   187
	// prefix		mapname, which will be appended to images etc.
insilmaril@721
   188
	// writeflags	Only write flags for "real" save of map, not undo
insilmaril@721
   189
	// offset		offset of bbox of whole map in scene. 
insilmaril@721
   190
	//				Needed for XML export
insilmaril@721
   191
	
insilmaril@721
   192
	// Save Header
insilmaril@721
   193
	QString ls;
insilmaril@721
   194
	switch (linkstyle)
insilmaril@721
   195
	{
insilmaril@721
   196
		case LinkableMapObj::Line: 
insilmaril@721
   197
			ls="StyleLine";
insilmaril@721
   198
			break;
insilmaril@721
   199
		case LinkableMapObj::Parabel:
insilmaril@721
   200
			ls="StyleParabel";
insilmaril@721
   201
			break;
insilmaril@721
   202
		case LinkableMapObj::PolyLine:	
insilmaril@721
   203
			ls="StylePolyLine";
insilmaril@721
   204
			break;
insilmaril@721
   205
		default:
insilmaril@721
   206
			ls="StylePolyParabel";
insilmaril@721
   207
			break;
insilmaril@721
   208
	}	
insilmaril@721
   209
insilmaril@721
   210
	QString s="<?xml version=\"1.0\" encoding=\"utf-8\"?><!DOCTYPE vymmap>\n";
insilmaril@721
   211
	QString colhint="";
insilmaril@721
   212
	if (linkcolorhint==LinkableMapObj::HeadingColor) 
insilmaril@721
   213
		colhint=attribut("linkColorHint","HeadingColor");
insilmaril@721
   214
insilmaril@721
   215
	QString mapAttr=attribut("version",vymVersion);
insilmaril@721
   216
	if (!saveSel)
insilmaril@721
   217
		mapAttr+= attribut("author",author) +
insilmaril@721
   218
				  attribut("comment",comment) +
insilmaril@721
   219
			      attribut("date",getDate()) +
insilmaril@721
   220
		          attribut("backgroundColor", mapScene->backgroundBrush().color().name() ) +
insilmaril@721
   221
		          attribut("selectionColor", selection.getColor().name() ) +
insilmaril@721
   222
		          attribut("linkStyle", ls ) +
insilmaril@721
   223
		          attribut("linkColor", defLinkColor.name() ) +
insilmaril@721
   224
		          attribut("defXLinkColor", defXLinkColor.name() ) +
insilmaril@721
   225
		          attribut("defXLinkWidth", QString().setNum(defXLinkWidth,10) ) +
insilmaril@721
   226
		          colhint; 
insilmaril@721
   227
	s+=beginElement("vymmap",mapAttr);
insilmaril@721
   228
	incIndent();
insilmaril@721
   229
insilmaril@721
   230
	// Find the used flags while traversing the tree
insilmaril@721
   231
	standardFlagsDefault->resetUsedCounter();
insilmaril@721
   232
	
insilmaril@721
   233
	// Reset the counters before saving
insilmaril@721
   234
	// TODO constr. of FIO creates lots of objects, better do this in some other way...
insilmaril@721
   235
	FloatImageObj (mapScene).resetSaveCounter();
insilmaril@721
   236
insilmaril@721
   237
	// Build xml recursivly
insilmaril@721
   238
	if (!saveSel || typeid (*saveSel) == typeid (MapCenterObj))
insilmaril@721
   239
		// Save complete map, if saveSel not set
insilmaril@721
   240
		s+=saveToDir(tmpdir,prefix,writeflags,offset);
insilmaril@721
   241
	else
insilmaril@721
   242
	{
insilmaril@721
   243
		if ( typeid(*saveSel) == typeid(BranchObj) )
insilmaril@721
   244
			// Save Subtree
insilmaril@721
   245
			s+=((BranchObj*)(saveSel))->saveToDir(tmpdir,prefix,offset);
insilmaril@721
   246
		else if ( typeid(*saveSel) == typeid(FloatImageObj) )
insilmaril@721
   247
			// Save image
insilmaril@721
   248
			s+=((FloatImageObj*)(saveSel))->saveToDir(tmpdir,prefix);
insilmaril@721
   249
	}
insilmaril@721
   250
insilmaril@721
   251
	// Save local settings
insilmaril@721
   252
	s+=settings.getDataXML (destPath);
insilmaril@721
   253
insilmaril@721
   254
	// Save selection
insilmaril@721
   255
	if (!selection.isEmpty() && !saveSel ) 
insilmaril@721
   256
		s+=valueElement("select",selection.getSelectString());
insilmaril@721
   257
insilmaril@721
   258
	decIndent();
insilmaril@721
   259
	s+=endElement("vymmap");
insilmaril@721
   260
insilmaril@721
   261
	if (writeflags)
insilmaril@721
   262
		standardFlagsDefault->saveToDir (tmpdir+"/flags/","",writeflags);
insilmaril@721
   263
	return s;
insilmaril@650
   264
}
insilmaril@650
   265
insilmaril@721
   266
void VymModel::setFilePath(QString fpath, QString destname)
insilmaril@650
   267
{
insilmaril@721
   268
	if (fpath.isEmpty() || fpath=="")
insilmaril@721
   269
	{
insilmaril@721
   270
		filePath="";
insilmaril@721
   271
		fileName="";
insilmaril@721
   272
		destPath="";
insilmaril@721
   273
	} else
insilmaril@721
   274
	{
insilmaril@721
   275
		filePath=fpath;		// becomes absolute path
insilmaril@721
   276
		fileName=fpath;		// gets stripped of path
insilmaril@721
   277
		destPath=destname;	// needed for vymlinks and during load to reset fileChangedTime
insilmaril@721
   278
insilmaril@721
   279
		// If fpath is not an absolute path, complete it
insilmaril@721
   280
		filePath=QDir(fpath).absPath();
insilmaril@721
   281
		fileDir=filePath.left (1+filePath.findRev ("/"));
insilmaril@721
   282
insilmaril@721
   283
		// Set short name, too. Search from behind:
insilmaril@721
   284
		int i=fileName.findRev("/");
insilmaril@721
   285
		if (i>=0) fileName=fileName.remove (0,i+1);
insilmaril@721
   286
insilmaril@721
   287
		// Forget the .vym (or .xml) for name of map
insilmaril@721
   288
		mapName=fileName.left(fileName.findRev(".",-1,true) );
insilmaril@721
   289
	}
insilmaril@650
   290
}
insilmaril@650
   291
insilmaril@721
   292
void VymModel::setFilePath(QString fpath)
insilmaril@650
   293
{
insilmaril@721
   294
	setFilePath (fpath,fpath);
insilmaril@650
   295
}
insilmaril@650
   296
insilmaril@721
   297
QString VymModel::getFilePath()
insilmaril@650
   298
{
insilmaril@721
   299
	return filePath;
insilmaril@650
   300
}
insilmaril@650
   301
insilmaril@721
   302
QString VymModel::getFileName()
insilmaril@650
   303
{
insilmaril@721
   304
	return fileName;
insilmaril@650
   305
}
insilmaril@650
   306
insilmaril@721
   307
QString VymModel::getMapName()
insilmaril@721
   308
{
insilmaril@721
   309
	return mapName;
insilmaril@721
   310
}
insilmaril@721
   311
insilmaril@721
   312
QString VymModel::getDestPath()
insilmaril@721
   313
{
insilmaril@721
   314
	return destPath;
insilmaril@721
   315
}
insilmaril@721
   316
insilmaril@721
   317
ErrorCode VymModel::load (QString fname, const LoadMode &lmode, const FileType &ftype)
insilmaril@721
   318
{
insilmaril@721
   319
	ErrorCode err=success;
insilmaril@721
   320
insilmaril@721
   321
	parseBaseHandler *handler;
insilmaril@721
   322
	fileType=ftype;
insilmaril@721
   323
	switch (fileType)
insilmaril@721
   324
	{
insilmaril@721
   325
		case VymMap: handler=new parseVYMHandler; break;
insilmaril@721
   326
		case FreemindMap : handler=new parseFreemindHandler; break;
insilmaril@721
   327
		default: 
insilmaril@721
   328
			QMessageBox::critical( 0, tr( "Critical Parse Error" ),
insilmaril@721
   329
				   "Unknown FileType in VymModel::load()");
insilmaril@721
   330
		return aborted;	
insilmaril@721
   331
	}
insilmaril@721
   332
	if (lmode==NewMap)
insilmaril@721
   333
	{
insilmaril@721
   334
		selection.clear();
insilmaril@721
   335
		// FIXME not needed??? model->setMapEditor(this);
insilmaril@721
   336
		// (map state is set later at end of load...)
insilmaril@721
   337
	} else
insilmaril@721
   338
	{
insilmaril@721
   339
		BranchObj *bo=selection.getBranch();
insilmaril@721
   340
		if (!bo) return aborted;
insilmaril@721
   341
		if (lmode==ImportAdd)
insilmaril@721
   342
			saveStateChangingPart(
insilmaril@721
   343
				bo,
insilmaril@721
   344
				bo,
insilmaril@721
   345
				QString("addMapInsert (%1)").arg(fname),
insilmaril@721
   346
				QString("Add map %1 to %2").arg(fname).arg(getObjectName(bo)));
insilmaril@721
   347
		else	
insilmaril@721
   348
			saveStateChangingPart(
insilmaril@721
   349
				bo,
insilmaril@721
   350
				bo,
insilmaril@721
   351
				QString("addMapReplace(%1)").arg(fname),
insilmaril@721
   352
				QString("Add map %1 to %2").arg(fname).arg(getObjectName(bo)));
insilmaril@721
   353
	}	
insilmaril@721
   354
    
insilmaril@721
   355
insilmaril@721
   356
	// Create temporary directory for packing
insilmaril@721
   357
	bool ok;
insilmaril@721
   358
	QString tmpZipDir=makeTmpDir (ok,"vym-pack");
insilmaril@721
   359
	if (!ok)
insilmaril@721
   360
	{
insilmaril@721
   361
		QMessageBox::critical( 0, tr( "Critical Load Error" ),
insilmaril@721
   362
		   tr("Couldn't create temporary directory before load\n"));
insilmaril@721
   363
		return aborted; 
insilmaril@721
   364
	}
insilmaril@721
   365
insilmaril@721
   366
	// Try to unzip file
insilmaril@721
   367
	err=unzipDir (tmpZipDir,fname);
insilmaril@721
   368
	QString xmlfile;
insilmaril@721
   369
	if (err==nozip)
insilmaril@721
   370
	{
insilmaril@721
   371
		xmlfile=fname;
insilmaril@721
   372
		zipped=false;
insilmaril@721
   373
	} else
insilmaril@721
   374
	{
insilmaril@721
   375
		zipped=true;
insilmaril@721
   376
		
insilmaril@721
   377
		// Look for mapname.xml
insilmaril@721
   378
		xmlfile= fname.left(fname.findRev(".",-1,true));
insilmaril@721
   379
		xmlfile=xmlfile.section( '/', -1 );
insilmaril@721
   380
		QFile mfile( tmpZipDir + "/" + xmlfile + ".xml");
insilmaril@721
   381
		if (!mfile.exists() )
insilmaril@721
   382
		{
insilmaril@721
   383
			// mapname.xml does not exist, well, 
insilmaril@721
   384
			// maybe someone renamed the mapname.vym file...
insilmaril@721
   385
			// Try to find any .xml in the toplevel 
insilmaril@721
   386
			// directory of the .vym file
insilmaril@721
   387
			QStringList flist=QDir (tmpZipDir).entryList("*.xml");
insilmaril@721
   388
			if (flist.count()==1) 
insilmaril@721
   389
			{
insilmaril@721
   390
				// Only one entry, take this one
insilmaril@721
   391
				xmlfile=tmpZipDir + "/"+flist.first();
insilmaril@721
   392
			} else
insilmaril@721
   393
			{
insilmaril@721
   394
				for ( QStringList::Iterator it = flist.begin(); it != flist.end(); ++it ) 
insilmaril@721
   395
					*it=tmpZipDir + "/" + *it;
insilmaril@721
   396
				// TODO Multiple entries, load all (but only the first one into this ME)
insilmaril@721
   397
				//mainWindow->fileLoadFromTmp (flist);
insilmaril@721
   398
				//returnCode=1;	// Silently forget this attempt to load
insilmaril@721
   399
				qWarning ("MainWindow::load (fn)  multimap found...");
insilmaril@721
   400
			}	
insilmaril@721
   401
				
insilmaril@721
   402
			if (flist.isEmpty() )
insilmaril@721
   403
			{
insilmaril@721
   404
				QMessageBox::critical( 0, tr( "Critical Load Error" ),
insilmaril@721
   405
						   tr("Couldn't find a map (*.xml) in .vym archive.\n"));
insilmaril@721
   406
				err=aborted;				   
insilmaril@721
   407
			}	
insilmaril@721
   408
		} //file doesn't exist	
insilmaril@721
   409
		else
insilmaril@721
   410
			xmlfile=mfile.name();
insilmaril@721
   411
	}
insilmaril@721
   412
insilmaril@721
   413
	QFile file( xmlfile);
insilmaril@721
   414
insilmaril@721
   415
	// I am paranoid: file should exist anyway
insilmaril@721
   416
	// according to check in mainwindow.
insilmaril@721
   417
	if (!file.exists() )
insilmaril@721
   418
	{
insilmaril@721
   419
		QMessageBox::critical( 0, tr( "Critical Parse Error" ),
insilmaril@721
   420
				   tr(QString("Couldn't open map %1").arg(file.name())));
insilmaril@721
   421
		err=aborted;	
insilmaril@721
   422
	} else
insilmaril@721
   423
	{
insilmaril@721
   424
		bool blockSaveStateOrg=blockSaveState;
insilmaril@721
   425
		blockReposition=true;
insilmaril@721
   426
		blockSaveState=true;
insilmaril@721
   427
		QXmlInputSource source( file);
insilmaril@721
   428
		QXmlSimpleReader reader;
insilmaril@721
   429
		reader.setContentHandler( handler );
insilmaril@721
   430
		reader.setErrorHandler( handler );
insilmaril@721
   431
		handler->setModel ( this);
insilmaril@721
   432
insilmaril@721
   433
insilmaril@721
   434
		// We need to set the tmpDir in order  to load files with rel. path
insilmaril@721
   435
		QString tmpdir;
insilmaril@721
   436
		if (zipped)
insilmaril@721
   437
			tmpdir=tmpZipDir;
insilmaril@721
   438
		else
insilmaril@721
   439
			tmpdir=fname.left(fname.findRev("/",-1));	
insilmaril@721
   440
		handler->setTmpDir (tmpdir);
insilmaril@721
   441
		handler->setInputFile (file.name());
insilmaril@721
   442
		handler->setLoadMode (lmode);
insilmaril@721
   443
		bool ok = reader.parse( source );
insilmaril@721
   444
		blockReposition=false;
insilmaril@721
   445
		blockSaveState=blockSaveStateOrg;
insilmaril@721
   446
		file.close();
insilmaril@721
   447
		if ( ok ) 
insilmaril@721
   448
		{
insilmaril@721
   449
			reposition();	// FIXME reposition the view instead...
insilmaril@721
   450
			selection.update();
insilmaril@721
   451
			if (lmode==NewMap)
insilmaril@721
   452
			{
insilmaril@721
   453
				mapDefault=false;
insilmaril@721
   454
				mapChanged=false;
insilmaril@721
   455
				mapUnsaved=false;
insilmaril@721
   456
				autosaveTimer->stop();
insilmaril@721
   457
			}
insilmaril@721
   458
insilmaril@721
   459
			// Reset timestamp to check for later updates of file
insilmaril@721
   460
			fileChangedTime=QFileInfo (destPath).lastModified();
insilmaril@721
   461
		} else 
insilmaril@721
   462
		{
insilmaril@721
   463
			QMessageBox::critical( 0, tr( "Critical Parse Error" ),
insilmaril@721
   464
					   tr( handler->errorProtocol() ) );
insilmaril@721
   465
			// returnCode=1;	
insilmaril@721
   466
			// Still return "success": the map maybe at least
insilmaril@721
   467
			// partially read by the parser
insilmaril@721
   468
		}	
insilmaril@721
   469
	}	
insilmaril@721
   470
insilmaril@721
   471
	// Delete tmpZipDir
insilmaril@721
   472
	removeDir (QDir(tmpZipDir));
insilmaril@721
   473
insilmaril@721
   474
	updateActions();
insilmaril@721
   475
insilmaril@721
   476
	return err;
insilmaril@721
   477
}
insilmaril@721
   478
insilmaril@721
   479
ErrorCode VymModel::save (const SaveMode &savemode)
insilmaril@721
   480
{
insilmaril@721
   481
	QString tmpZipDir;
insilmaril@721
   482
	QString mapFileName;
insilmaril@721
   483
	QString safeFilePath;
insilmaril@721
   484
insilmaril@721
   485
	ErrorCode err=success;
insilmaril@721
   486
insilmaril@721
   487
	if (zipped)
insilmaril@721
   488
		// save as .xml
insilmaril@721
   489
		mapFileName=mapName+".xml";
insilmaril@721
   490
	else
insilmaril@721
   491
		// use name given by user, even if he chooses .doc
insilmaril@721
   492
		mapFileName=fileName;
insilmaril@721
   493
insilmaril@721
   494
	// Look, if we should zip the data:
insilmaril@721
   495
	if (!zipped)
insilmaril@721
   496
	{
insilmaril@721
   497
		QMessageBox mb( vymName,
insilmaril@721
   498
			tr("The map %1\ndid not use the compressed "
insilmaril@721
   499
			"vym file format.\nWriting it uncompressed will also write images \n"
insilmaril@721
   500
			"and flags and thus may overwrite files in the "
insilmaril@721
   501
			"given directory\n\nDo you want to write the map").arg(filePath),
insilmaril@721
   502
			QMessageBox::Warning,
insilmaril@721
   503
			QMessageBox::Yes | QMessageBox::Default,
insilmaril@721
   504
			QMessageBox::No ,
insilmaril@721
   505
			QMessageBox::Cancel | QMessageBox::Escape);
insilmaril@721
   506
		mb.setButtonText( QMessageBox::Yes, tr("compressed (vym default)") );
insilmaril@721
   507
		mb.setButtonText( QMessageBox::No, tr("uncompressed") );
insilmaril@721
   508
		mb.setButtonText( QMessageBox::Cancel, tr("Cancel"));
insilmaril@721
   509
		switch( mb.exec() ) 
insilmaril@721
   510
		{
insilmaril@721
   511
			case QMessageBox::Yes:
insilmaril@721
   512
				// save compressed (default file format)
insilmaril@721
   513
				zipped=true;
insilmaril@721
   514
				break;
insilmaril@721
   515
			case QMessageBox::No:
insilmaril@721
   516
				// save uncompressed
insilmaril@721
   517
				zipped=false;
insilmaril@721
   518
				break;
insilmaril@721
   519
			case QMessageBox::Cancel:
insilmaril@721
   520
				// do nothing
insilmaril@721
   521
				return aborted;
insilmaril@721
   522
				break;
insilmaril@721
   523
		}
insilmaril@721
   524
	}
insilmaril@721
   525
insilmaril@721
   526
	// First backup existing file, we 
insilmaril@721
   527
	// don't want to add to old zip archives
insilmaril@721
   528
	QFile f(destPath);
insilmaril@721
   529
	if (f.exists())
insilmaril@721
   530
	{
insilmaril@721
   531
		if ( settings.value ("/mapeditor/writeBackupFile").toBool())
insilmaril@721
   532
		{
insilmaril@721
   533
			QString backupFileName(destPath + "~");
insilmaril@721
   534
			QFile backupFile(backupFileName);
insilmaril@721
   535
			if (backupFile.exists() && !backupFile.remove())
insilmaril@721
   536
			{
insilmaril@721
   537
				QMessageBox::warning(0, tr("Save Error"),
insilmaril@721
   538
									 tr("%1\ncould not be removed before saving").arg(backupFileName));
insilmaril@721
   539
			}
insilmaril@721
   540
			else if (!f.rename(backupFileName))
insilmaril@721
   541
			{
insilmaril@721
   542
				QMessageBox::warning(0, tr("Save Error"),
insilmaril@721
   543
									 tr("%1\ncould not be renamed before saving").arg(destPath));
insilmaril@721
   544
			}
insilmaril@721
   545
		}
insilmaril@721
   546
	}
insilmaril@721
   547
insilmaril@721
   548
	if (zipped)
insilmaril@721
   549
	{
insilmaril@721
   550
		// Create temporary directory for packing
insilmaril@721
   551
		bool ok;
insilmaril@721
   552
		tmpZipDir=makeTmpDir (ok,"vym-zip");
insilmaril@721
   553
		if (!ok)
insilmaril@721
   554
		{
insilmaril@721
   555
			QMessageBox::critical( 0, tr( "Critical Load Error" ),
insilmaril@721
   556
			   tr("Couldn't create temporary directory before save\n"));
insilmaril@721
   557
			return aborted; 
insilmaril@721
   558
		}
insilmaril@721
   559
insilmaril@721
   560
		safeFilePath=filePath;
insilmaril@721
   561
		setFilePath (tmpZipDir+"/"+ mapName+ ".xml", safeFilePath);
insilmaril@721
   562
	} // zipped
insilmaril@721
   563
insilmaril@721
   564
	// Create mapName and fileDir
insilmaril@721
   565
	makeSubDirs (fileDir);
insilmaril@721
   566
insilmaril@721
   567
	QString saveFile;
insilmaril@721
   568
	if (savemode==CompleteMap || selection.isEmpty())
insilmaril@721
   569
	{
insilmaril@721
   570
		// Save complete map
insilmaril@721
   571
		saveFile=saveToDir (fileDir,mapName+"-",true,QPointF(),NULL);
insilmaril@721
   572
		mapChanged=false;
insilmaril@721
   573
		mapUnsaved=false;
insilmaril@721
   574
		autosaveTimer->stop();
insilmaril@721
   575
	}
insilmaril@721
   576
	else	
insilmaril@721
   577
	{
insilmaril@721
   578
		// Save part of map
insilmaril@721
   579
		if (selection.type()==Selection::FloatImage)
insilmaril@721
   580
			saveFloatImage();
insilmaril@721
   581
		else	
insilmaril@721
   582
			saveFile=saveToDir (fileDir,mapName+"-",true,QPointF(),selection.getBranch());	
insilmaril@721
   583
		// TODO take care of multiselections
insilmaril@721
   584
	}	
insilmaril@721
   585
insilmaril@721
   586
	if (!saveStringToDisk(fileDir+mapFileName,saveFile))
insilmaril@721
   587
	{
insilmaril@721
   588
		err=aborted;
insilmaril@721
   589
		qWarning ("ME::saveStringToDisk failed!");
insilmaril@721
   590
	}
insilmaril@721
   591
insilmaril@721
   592
	if (zipped)
insilmaril@721
   593
	{
insilmaril@721
   594
		// zip
insilmaril@721
   595
		if (err==success) err=zipDir (tmpZipDir,destPath);
insilmaril@721
   596
insilmaril@721
   597
		// Delete tmpDir
insilmaril@721
   598
		removeDir (QDir(tmpZipDir));
insilmaril@721
   599
insilmaril@721
   600
		// Restore original filepath outside of tmp zip dir
insilmaril@721
   601
		setFilePath (safeFilePath);
insilmaril@721
   602
	}
insilmaril@721
   603
insilmaril@721
   604
	updateActions();
insilmaril@721
   605
	fileChangedTime=QFileInfo (destPath).lastModified();
insilmaril@721
   606
	return err;
insilmaril@721
   607
}
insilmaril@721
   608
insilmaril@721
   609
void VymModel::addMapReplaceInt(const QString &undoSel, const QString &path)
insilmaril@721
   610
{
insilmaril@721
   611
	QString pathDir=path.left(path.findRev("/"));
insilmaril@721
   612
	QDir d(pathDir);
insilmaril@721
   613
	QFile file (path);
insilmaril@721
   614
insilmaril@721
   615
	if (d.exists() )
insilmaril@721
   616
	{
insilmaril@721
   617
		// We need to parse saved XML data
insilmaril@721
   618
		parseVYMHandler handler;
insilmaril@721
   619
		QXmlInputSource source( file);
insilmaril@721
   620
		QXmlSimpleReader reader;
insilmaril@721
   621
		reader.setContentHandler( &handler );
insilmaril@721
   622
		reader.setErrorHandler( &handler );
insilmaril@721
   623
		handler.setModel ( this);
insilmaril@721
   624
		handler.setTmpDir ( pathDir );	// needed to load files with rel. path
insilmaril@721
   625
		if (undoSel.isEmpty())
insilmaril@721
   626
		{
insilmaril@721
   627
			unselect();
insilmaril@721
   628
			clear();
insilmaril@721
   629
			handler.setLoadMode (NewMap);
insilmaril@721
   630
		} else	
insilmaril@721
   631
		{
insilmaril@721
   632
			select (undoSel);
insilmaril@721
   633
			handler.setLoadMode (ImportReplace);
insilmaril@721
   634
		}	
insilmaril@721
   635
		blockReposition=true;
insilmaril@721
   636
		bool ok = reader.parse( source );
insilmaril@721
   637
		blockReposition=false;
insilmaril@721
   638
		if (! ok ) 
insilmaril@721
   639
		{	
insilmaril@721
   640
			// This should never ever happen
insilmaril@721
   641
			QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path),
insilmaril@721
   642
								    handler.errorProtocol());
insilmaril@721
   643
		}
insilmaril@721
   644
	} else	
insilmaril@721
   645
		QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
insilmaril@721
   646
}
insilmaril@721
   647
insilmaril@721
   648
void VymModel::addMapInsertInt (const QString &path, int pos)
insilmaril@721
   649
{
insilmaril@721
   650
	BranchObj *sel=selection.getBranch();
insilmaril@721
   651
	if (sel)
insilmaril@721
   652
	{
insilmaril@721
   653
		QString pathDir=path.left(path.findRev("/"));
insilmaril@721
   654
		QDir d(pathDir);
insilmaril@721
   655
		QFile file (path);
insilmaril@721
   656
insilmaril@721
   657
		if (d.exists() )
insilmaril@721
   658
		{
insilmaril@721
   659
			// We need to parse saved XML data
insilmaril@721
   660
			parseVYMHandler handler;
insilmaril@721
   661
			QXmlInputSource source( file);
insilmaril@721
   662
			QXmlSimpleReader reader;
insilmaril@721
   663
			reader.setContentHandler( &handler );
insilmaril@721
   664
			reader.setErrorHandler( &handler );
insilmaril@721
   665
			handler.setModel (this);
insilmaril@721
   666
			handler.setTmpDir ( pathDir );	// needed to load files with rel. path
insilmaril@721
   667
			handler.setLoadMode (ImportAdd);
insilmaril@721
   668
			blockReposition=true;
insilmaril@721
   669
			bool ok = reader.parse( source );
insilmaril@721
   670
			blockReposition=false;
insilmaril@721
   671
			if (! ok ) 
insilmaril@721
   672
			{	
insilmaril@721
   673
				// This should never ever happen
insilmaril@721
   674
				QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path),
insilmaril@721
   675
										handler.errorProtocol());
insilmaril@721
   676
			}
insilmaril@721
   677
			if (sel->getDepth()>0)
insilmaril@721
   678
				sel->getLastBranch()->linkTo (sel,pos);
insilmaril@721
   679
		} else	
insilmaril@721
   680
			QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
insilmaril@721
   681
	}		
insilmaril@721
   682
}
insilmaril@721
   683
insilmaril@721
   684
FloatImageObj* VymModel::loadFloatImageInt (QString fn)
insilmaril@721
   685
{
insilmaril@721
   686
	BranchObj *bo=selection.getBranch();
insilmaril@721
   687
	if (bo)
insilmaril@721
   688
	{
insilmaril@721
   689
		FloatImageObj *fio;
insilmaril@721
   690
		bo->addFloatImage();
insilmaril@721
   691
		fio=bo->getLastFloatImage();
insilmaril@721
   692
		fio->load(fn);
insilmaril@721
   693
		reposition();
insilmaril@721
   694
		// FIXME needed? scene()->update();
insilmaril@721
   695
		return fio;
insilmaril@721
   696
	}
insilmaril@721
   697
	return NULL;
insilmaril@721
   698
}	
insilmaril@721
   699
insilmaril@721
   700
void VymModel::loadFloatImage ()
insilmaril@721
   701
{
insilmaril@721
   702
	BranchObj *bo=selection.getBranch();
insilmaril@721
   703
	if (bo)
insilmaril@721
   704
	{
insilmaril@721
   705
insilmaril@721
   706
		Q3FileDialog *fd=new Q3FileDialog( NULL);
insilmaril@721
   707
		fd->setMode (Q3FileDialog::ExistingFiles);
insilmaril@721
   708
		fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
insilmaril@721
   709
		ImagePreview *p =new ImagePreview (fd);
insilmaril@721
   710
		fd->setContentsPreviewEnabled( TRUE );
insilmaril@721
   711
		fd->setContentsPreview( p, p );
insilmaril@721
   712
		fd->setPreviewMode( Q3FileDialog::Contents );
insilmaril@721
   713
		fd->setCaption(vymName+" - " +tr("Load image"));
insilmaril@721
   714
		fd->setDir (lastImageDir);
insilmaril@721
   715
		fd->show();
insilmaril@721
   716
insilmaril@721
   717
		if ( fd->exec() == QDialog::Accepted )
insilmaril@721
   718
		{
insilmaril@721
   719
			// TODO loadFIO in QT4 use:	lastImageDir=fd->directory();
insilmaril@721
   720
			lastImageDir=QDir (fd->dirPath());
insilmaril@721
   721
			QString s;
insilmaril@721
   722
			FloatImageObj *fio;
insilmaril@721
   723
			for (int j=0; j<fd->selectedFiles().count(); j++)
insilmaril@721
   724
			{
insilmaril@721
   725
				s=fd->selectedFiles().at(j);
insilmaril@721
   726
				fio=loadFloatImageInt (s);
insilmaril@721
   727
				if (fio)
insilmaril@721
   728
					saveState(
insilmaril@721
   729
						(LinkableMapObj*)fio,
insilmaril@721
   730
						"delete ()",
insilmaril@721
   731
						bo, 
insilmaril@721
   732
						QString ("loadImage (%1)").arg(s ),
insilmaril@721
   733
						QString("Add image %1 to %2").arg(s).arg(getObjectName(bo))
insilmaril@721
   734
					);
insilmaril@721
   735
				else
insilmaril@721
   736
					// TODO loadFIO error handling
insilmaril@721
   737
					qWarning ("Failed to load "+s);
insilmaril@721
   738
			}
insilmaril@721
   739
		}
insilmaril@721
   740
		delete (p);
insilmaril@721
   741
		delete (fd);
insilmaril@721
   742
	}
insilmaril@721
   743
}
insilmaril@721
   744
insilmaril@721
   745
void VymModel::saveFloatImageInt  (FloatImageObj *fio, const QString &type, const QString &fn)
insilmaril@721
   746
{
insilmaril@721
   747
	fio->save (fn,type);
insilmaril@721
   748
}
insilmaril@721
   749
insilmaril@721
   750
void VymModel::saveFloatImage ()
insilmaril@721
   751
{
insilmaril@721
   752
	FloatImageObj *fio=selection.getFloatImage();
insilmaril@721
   753
	if (fio)
insilmaril@721
   754
	{
insilmaril@721
   755
		QFileDialog *fd=new QFileDialog( NULL);
insilmaril@721
   756
		fd->setFilters (imageIO.getFilters());
insilmaril@721
   757
		fd->setCaption(vymName+" - " +tr("Save image"));
insilmaril@721
   758
		fd->setFileMode( QFileDialog::AnyFile );
insilmaril@721
   759
		fd->setDirectory (lastImageDir);
insilmaril@721
   760
//		fd->setSelection (fio->getOriginalFilename());
insilmaril@721
   761
		fd->show();
insilmaril@721
   762
insilmaril@721
   763
		QString fn;
insilmaril@721
   764
		if ( fd->exec() == QDialog::Accepted && fd->selectedFiles().count()==1)
insilmaril@721
   765
		{
insilmaril@721
   766
			fn=fd->selectedFiles().at(0);
insilmaril@721
   767
			if (QFile (fn).exists() )
insilmaril@721
   768
			{
insilmaril@721
   769
				QMessageBox mb( vymName,
insilmaril@721
   770
					tr("The file %1 exists already.\n"
insilmaril@721
   771
					"Do you want to overwrite it?").arg(fn),
insilmaril@721
   772
				QMessageBox::Warning,
insilmaril@721
   773
				QMessageBox::Yes | QMessageBox::Default,
insilmaril@721
   774
				QMessageBox::Cancel | QMessageBox::Escape,
insilmaril@721
   775
				QMessageBox::NoButton );
insilmaril@721
   776
insilmaril@721
   777
				mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
insilmaril@721
   778
				mb.setButtonText( QMessageBox::No, tr("Cancel"));
insilmaril@721
   779
				switch( mb.exec() ) 
insilmaril@721
   780
				{
insilmaril@721
   781
					case QMessageBox::Yes:
insilmaril@721
   782
						// save 
insilmaril@721
   783
						break;
insilmaril@721
   784
					case QMessageBox::Cancel:
insilmaril@721
   785
						// do nothing
insilmaril@721
   786
						delete (fd);
insilmaril@721
   787
						return;
insilmaril@721
   788
						break;
insilmaril@721
   789
				}
insilmaril@721
   790
			}
insilmaril@721
   791
			saveFloatImageInt (fio,fd->selectedFilter(),fn );
insilmaril@721
   792
		}
insilmaril@721
   793
		delete (fd);
insilmaril@721
   794
	}
insilmaril@721
   795
}
insilmaril@721
   796
insilmaril@721
   797
insilmaril@721
   798
void VymModel::importDirInt(BranchObj *dst, QDir d)
insilmaril@721
   799
{
insilmaril@721
   800
	BranchObj *bo=selection.getBranch();
insilmaril@721
   801
	if (bo)
insilmaril@721
   802
	{
insilmaril@721
   803
		// Traverse directories
insilmaril@721
   804
		d.setFilter( QDir::Dirs| QDir::Hidden | QDir::NoSymLinks );
insilmaril@721
   805
		QFileInfoList list = d.entryInfoList();
insilmaril@721
   806
		QFileInfo fi;
insilmaril@721
   807
insilmaril@721
   808
		for (int i = 0; i < list.size(); ++i) 
insilmaril@721
   809
		{
insilmaril@721
   810
			fi=list.at(i);
insilmaril@721
   811
			if (fi.fileName() != "." && fi.fileName() != ".." )
insilmaril@721
   812
			{
insilmaril@721
   813
				dst->addBranch();
insilmaril@721
   814
				bo=dst->getLastBranch();
insilmaril@721
   815
				bo->setHeading (fi.fileName() );
insilmaril@721
   816
				bo->setColor (QColor("blue"));
insilmaril@721
   817
				bo->toggleScroll();
insilmaril@721
   818
				if ( !d.cd(fi.fileName()) ) 
insilmaril@721
   819
					QMessageBox::critical (0,tr("Critical Import Error"),tr("Cannot find the directory %1").arg(fi.fileName()));
insilmaril@721
   820
				else 
insilmaril@721
   821
				{
insilmaril@721
   822
					// Recursively add subdirs
insilmaril@721
   823
					importDirInt (bo,d);
insilmaril@721
   824
					d.cdUp();
insilmaril@721
   825
				}
insilmaril@721
   826
			}	
insilmaril@721
   827
		}		
insilmaril@721
   828
		// Traverse files
insilmaril@721
   829
		d.setFilter( QDir::Files| QDir::Hidden | QDir::NoSymLinks );
insilmaril@721
   830
		list = d.entryInfoList();
insilmaril@721
   831
insilmaril@721
   832
		for (int i = 0; i < list.size(); ++i) 
insilmaril@721
   833
		{
insilmaril@721
   834
			fi=list.at(i);
insilmaril@721
   835
			dst->addBranch();
insilmaril@721
   836
			bo=dst->getLastBranch();
insilmaril@721
   837
			bo->setHeading (fi.fileName() );
insilmaril@721
   838
			bo->setColor (QColor("black"));
insilmaril@721
   839
			if (fi.fileName().right(4) == ".vym" )
insilmaril@721
   840
				bo->setVymLink (fi.filePath());
insilmaril@721
   841
		}	
insilmaril@721
   842
	}		
insilmaril@721
   843
}
insilmaril@721
   844
insilmaril@721
   845
void VymModel::importDirInt (const QString &s)
insilmaril@721
   846
{
insilmaril@721
   847
	BranchObj *bo=selection.getBranch();
insilmaril@721
   848
	if (bo)
insilmaril@721
   849
	{
insilmaril@721
   850
		saveStateChangingPart (bo,bo,QString ("importDir (\"%1\")").arg(s),QString("Import directory structure from %1").arg(s));
insilmaril@721
   851
insilmaril@721
   852
		QDir d(s);
insilmaril@721
   853
		importDirInt (bo,d);
insilmaril@721
   854
	}
insilmaril@721
   855
}	
insilmaril@721
   856
insilmaril@721
   857
void VymModel::importDir()
insilmaril@721
   858
{
insilmaril@721
   859
	BranchObj *bo=selection.getBranch();
insilmaril@721
   860
	if (bo)
insilmaril@721
   861
	{
insilmaril@721
   862
		QStringList filters;
insilmaril@721
   863
		filters <<"VYM map (*.vym)";
insilmaril@721
   864
		QFileDialog *fd=new QFileDialog( NULL,vymName+ " - " +tr("Choose directory structure to import"));
insilmaril@721
   865
		fd->setMode (QFileDialog::DirectoryOnly);
insilmaril@721
   866
		fd->setFilters (filters);
insilmaril@721
   867
		fd->setCaption(vymName+" - " +tr("Choose directory structure to import"));
insilmaril@721
   868
		fd->show();
insilmaril@721
   869
insilmaril@721
   870
		QString fn;
insilmaril@721
   871
		if ( fd->exec() == QDialog::Accepted )
insilmaril@721
   872
		{
insilmaril@721
   873
			importDirInt (fd->selectedFile() );
insilmaril@721
   874
			reposition();
insilmaril@721
   875
			//FIXME needed? scene()->update();
insilmaril@721
   876
		}
insilmaril@721
   877
	}	
insilmaril@721
   878
}
insilmaril@721
   879
insilmaril@721
   880
insilmaril@721
   881
void VymModel::autosave()
insilmaril@721
   882
{
insilmaril@721
   883
	QDateTime now=QDateTime().currentDateTime();
insilmaril@721
   884
insilmaril@721
   885
	// Disable autosave, while we have gone back in history
insilmaril@721
   886
	int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
insilmaril@721
   887
	if (redosAvail>0) return;
insilmaril@721
   888
insilmaril@721
   889
	// Also disable autosave for new map without filename
insilmaril@721
   890
	if (filePath.isEmpty()) return;
insilmaril@721
   891
insilmaril@721
   892
insilmaril@721
   893
	if (mapUnsaved &&mapChanged && settings.value ("/autosave/use",true).toBool() )
insilmaril@721
   894
	{
insilmaril@721
   895
		if (QFileInfo(filePath).lastModified()<=fileChangedTime) 
insilmaril@721
   896
			mainWindow->fileSave (this);
insilmaril@721
   897
		else
insilmaril@721
   898
			if (debug)
insilmaril@721
   899
				cout <<"  ME::autosave  rejected, file on disk is newer than last save.\n"; 
insilmaril@721
   900
insilmaril@721
   901
	}	
insilmaril@721
   902
}
insilmaril@721
   903
insilmaril@721
   904
void VymModel::fileChanged()
insilmaril@721
   905
{
insilmaril@721
   906
	// Check if file on disk has changed meanwhile
insilmaril@721
   907
	if (!filePath.isEmpty())
insilmaril@721
   908
	{
insilmaril@721
   909
		QDateTime tmod=QFileInfo (filePath).lastModified();
insilmaril@721
   910
		if (tmod>fileChangedTime)
insilmaril@721
   911
		{
insilmaril@721
   912
			// FIXME switch to current mapeditor and finish lineedits...
insilmaril@721
   913
			QMessageBox mb( vymName,
insilmaril@721
   914
				tr("The file of the map  on disk has changed:\n\n"  
insilmaril@721
   915
				   "   %1\n\nDo you want to reload that map with the new file?").arg(filePath),
insilmaril@721
   916
				QMessageBox::Question,
insilmaril@721
   917
				QMessageBox::Yes ,
insilmaril@721
   918
				QMessageBox::Cancel | QMessageBox::Default,
insilmaril@721
   919
				QMessageBox::NoButton );
insilmaril@721
   920
insilmaril@721
   921
			mb.setButtonText( QMessageBox::Yes, tr("Reload"));
insilmaril@721
   922
			mb.setButtonText( QMessageBox::No, tr("Ignore"));
insilmaril@721
   923
			switch( mb.exec() ) 
insilmaril@721
   924
			{
insilmaril@721
   925
				case QMessageBox::Yes:
insilmaril@721
   926
					// Reload map
insilmaril@721
   927
					load (filePath,NewMap,fileType);
insilmaril@721
   928
		        case QMessageBox::Cancel:
insilmaril@721
   929
					fileChangedTime=tmod; // allow autosave to overwrite newer file!
insilmaril@721
   930
			}
insilmaril@721
   931
		}
insilmaril@721
   932
	}	
insilmaril@721
   933
}
insilmaril@721
   934
insilmaril@721
   935
bool VymModel::isDefault()
insilmaril@721
   936
{
insilmaril@721
   937
    return mapDefault;
insilmaril@721
   938
}
insilmaril@721
   939
insilmaril@721
   940
void VymModel::makeDefault()
insilmaril@721
   941
{
insilmaril@721
   942
	mapChanged=false;
insilmaril@721
   943
	mapDefault=true;
insilmaril@721
   944
}
insilmaril@721
   945
insilmaril@721
   946
bool VymModel::hasChanged()
insilmaril@721
   947
{
insilmaril@721
   948
    return mapChanged;
insilmaril@721
   949
}
insilmaril@721
   950
insilmaril@721
   951
void VymModel::setChanged()
insilmaril@721
   952
{
insilmaril@721
   953
	if (!mapChanged)
insilmaril@721
   954
		autosaveTimer->start(settings.value("/autosave/ms/",300000).toInt());
insilmaril@721
   955
	mapChanged=true;
insilmaril@721
   956
	mapDefault=false;
insilmaril@721
   957
	mapUnsaved=true;
insilmaril@721
   958
	findReset();
insilmaril@721
   959
}
insilmaril@721
   960
insilmaril@721
   961
insilmaril@721
   962
QString VymModel::getObjectName (const LinkableMapObj *lmo)
insilmaril@721
   963
{
insilmaril@721
   964
	QString s;
insilmaril@721
   965
	if (!lmo) return QString("Error: NULL has no name!");
insilmaril@721
   966
insilmaril@721
   967
	if ((typeid(*lmo) == typeid(BranchObj) ||
insilmaril@721
   968
				      typeid(*lmo) == typeid(MapCenterObj))) 
insilmaril@721
   969
	{
insilmaril@721
   970
		
insilmaril@721
   971
		s=(((BranchObj*)lmo)->getHeading());
insilmaril@721
   972
		if (s=="") s="unnamed";
insilmaril@721
   973
		return QString("branch (%1)").arg(s);
insilmaril@721
   974
	}	
insilmaril@721
   975
	if ((typeid(*lmo) == typeid(FloatImageObj) ))
insilmaril@721
   976
		return QString ("floatimage [%1]").arg(((FloatImageObj*)lmo)->getOriginalFilename());
insilmaril@721
   977
	return QString("Unknown type has no name!");
insilmaril@721
   978
}
insilmaril@721
   979
insilmaril@721
   980
void VymModel::redo()
insilmaril@721
   981
{
insilmaril@721
   982
	// Can we undo at all?
insilmaril@721
   983
	if (redosAvail<1) return;
insilmaril@721
   984
insilmaril@721
   985
	bool blockSaveStateOrg=blockSaveState;
insilmaril@721
   986
	blockSaveState=true;
insilmaril@721
   987
	
insilmaril@721
   988
	redosAvail--;
insilmaril@721
   989
insilmaril@721
   990
	if (undosAvail<stepsTotal) undosAvail++;
insilmaril@721
   991
	curStep++;
insilmaril@721
   992
	if (curStep>stepsTotal) curStep=1;
insilmaril@721
   993
	QString undoCommand=  undoSet.readEntry (QString("/history/step-%1/undoCommand").arg(curStep));
insilmaril@721
   994
	QString undoSelection=undoSet.readEntry (QString("/history/step-%1/undoSelection").arg(curStep));
insilmaril@721
   995
	QString redoCommand=  undoSet.readEntry (QString("/history/step-%1/redoCommand").arg(curStep));
insilmaril@721
   996
	QString redoSelection=undoSet.readEntry (QString("/history/step-%1/redoSelection").arg(curStep));
insilmaril@721
   997
	QString comment=undoSet.readEntry (QString("/history/step-%1/comment").arg(curStep));
insilmaril@721
   998
	QString version=undoSet.readEntry ("/history/version");
insilmaril@721
   999
insilmaril@721
  1000
	/* TODO Maybe check for version, if we save the history
insilmaril@721
  1001
	if (!checkVersion(version))
insilmaril@721
  1002
		QMessageBox::warning(0,tr("Warning"),
insilmaril@721
  1003
			tr("Version %1 of saved undo/redo data\ndoes not match current vym version %2.").arg(version).arg(vymVersion));
insilmaril@721
  1004
	*/ 
insilmaril@721
  1005
insilmaril@721
  1006
	// Find out current undo directory
insilmaril@721
  1007
	QString bakMapDir(QString(tmpMapDir+"/undo-%1").arg(curStep));
insilmaril@721
  1008
insilmaril@721
  1009
	if (debug)
insilmaril@721
  1010
	{
insilmaril@721
  1011
		cout << "VymModel::redo() begin\n";
insilmaril@721
  1012
		cout << "    undosAvail="<<undosAvail<<endl;
insilmaril@721
  1013
		cout << "    redosAvail="<<redosAvail<<endl;
insilmaril@721
  1014
		cout << "       curStep="<<curStep<<endl;
insilmaril@721
  1015
		cout << "    ---------------------------"<<endl;
insilmaril@721
  1016
		cout << "    comment="<<comment.toStdString()<<endl;
insilmaril@721
  1017
		cout << "    undoCom="<<undoCommand.toStdString()<<endl;
insilmaril@721
  1018
		cout << "    undoSel="<<undoSelection.toStdString()<<endl;
insilmaril@721
  1019
		cout << "    redoCom="<<redoCommand.toStdString()<<endl;
insilmaril@721
  1020
		cout << "    redoSel="<<redoSelection.toStdString()<<endl;
insilmaril@721
  1021
		cout << "    ---------------------------"<<endl<<endl;
insilmaril@721
  1022
	}
insilmaril@721
  1023
insilmaril@721
  1024
	// select  object before redo
insilmaril@721
  1025
	if (!redoSelection.isEmpty())
insilmaril@721
  1026
		select (redoSelection);
insilmaril@721
  1027
insilmaril@721
  1028
insilmaril@721
  1029
	parseAtom (redoCommand);
insilmaril@721
  1030
	reposition();
insilmaril@721
  1031
insilmaril@721
  1032
	blockSaveState=blockSaveStateOrg;
insilmaril@721
  1033
insilmaril@721
  1034
	undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
insilmaril@721
  1035
	undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
insilmaril@721
  1036
	undoSet.setEntry ("/history/curStep",QString::number(curStep));
insilmaril@721
  1037
	undoSet.writeSettings(histPath);
insilmaril@721
  1038
insilmaril@721
  1039
	mainWindow->updateHistory (undoSet);
insilmaril@721
  1040
	updateActions();
insilmaril@721
  1041
insilmaril@721
  1042
	/* TODO remove testing
insilmaril@721
  1043
	cout << "ME::redo() end\n";
insilmaril@721
  1044
	cout << "    undosAvail="<<undosAvail<<endl;
insilmaril@721
  1045
	cout << "    redosAvail="<<redosAvail<<endl;
insilmaril@721
  1046
	cout << "       curStep="<<curStep<<endl;
insilmaril@721
  1047
	cout << "    ---------------------------"<<endl<<endl;
insilmaril@721
  1048
	*/
insilmaril@721
  1049
insilmaril@721
  1050
insilmaril@721
  1051
}
insilmaril@721
  1052
insilmaril@721
  1053
bool VymModel::isRedoAvailable()
insilmaril@721
  1054
{
insilmaril@721
  1055
	if (undoSet.readNumEntry("/history/redosAvail",0)>0)
insilmaril@721
  1056
		return true;
insilmaril@721
  1057
	else	
insilmaril@721
  1058
		return false;
insilmaril@721
  1059
}
insilmaril@721
  1060
insilmaril@721
  1061
void VymModel::undo()
insilmaril@721
  1062
{
insilmaril@721
  1063
	// Can we undo at all?
insilmaril@721
  1064
	if (undosAvail<1) return;
insilmaril@721
  1065
insilmaril@721
  1066
	mainWindow->statusMessage (tr("Autosave disabled during undo."));
insilmaril@721
  1067
insilmaril@721
  1068
	bool blockSaveStateOrg=blockSaveState;
insilmaril@721
  1069
	blockSaveState=true;
insilmaril@721
  1070
	
insilmaril@721
  1071
	QString undoCommand=  undoSet.readEntry (QString("/history/step-%1/undoCommand").arg(curStep));
insilmaril@721
  1072
	QString undoSelection=undoSet.readEntry (QString("/history/step-%1/undoSelection").arg(curStep));
insilmaril@721
  1073
	QString redoCommand=  undoSet.readEntry (QString("/history/step-%1/redoCommand").arg(curStep));
insilmaril@721
  1074
	QString redoSelection=undoSet.readEntry (QString("/history/step-%1/redoSelection").arg(curStep));
insilmaril@721
  1075
	QString comment=undoSet.readEntry (QString("/history/step-%1/comment").arg(curStep));
insilmaril@721
  1076
	QString version=undoSet.readEntry ("/history/version");
insilmaril@721
  1077
insilmaril@721
  1078
	/* TODO Maybe check for version, if we save the history
insilmaril@721
  1079
	if (!checkVersion(version))
insilmaril@721
  1080
		QMessageBox::warning(0,tr("Warning"),
insilmaril@721
  1081
			tr("Version %1 of saved undo/redo data\ndoes not match current vym version %2.").arg(version).arg(vymVersion));
insilmaril@721
  1082
	*/
insilmaril@721
  1083
insilmaril@721
  1084
	// Find out current undo directory
insilmaril@721
  1085
	QString bakMapDir(QString(tmpMapDir+"/undo-%1").arg(curStep));
insilmaril@721
  1086
insilmaril@721
  1087
	// select  object before undo
insilmaril@721
  1088
	if (!undoSelection.isEmpty())
insilmaril@721
  1089
		select (undoSelection);
insilmaril@721
  1090
insilmaril@721
  1091
	if (debug)
insilmaril@721
  1092
	{
insilmaril@721
  1093
		cout << "VymModel::undo() begin\n";
insilmaril@721
  1094
		cout << "    undosAvail="<<undosAvail<<endl;
insilmaril@721
  1095
		cout << "    redosAvail="<<redosAvail<<endl;
insilmaril@721
  1096
		cout << "       curStep="<<curStep<<endl;
insilmaril@721
  1097
		cout << "    ---------------------------"<<endl;
insilmaril@721
  1098
		cout << "    comment="<<comment.toStdString()<<endl;
insilmaril@721
  1099
		cout << "    undoCom="<<undoCommand.toStdString()<<endl;
insilmaril@721
  1100
		cout << "    undoSel="<<undoSelection.toStdString()<<endl;
insilmaril@721
  1101
		cout << "    redoCom="<<redoCommand.toStdString()<<endl;
insilmaril@721
  1102
		cout << "    redoSel="<<redoSelection.toStdString()<<endl;
insilmaril@721
  1103
		cout << "    ---------------------------"<<endl<<endl;
insilmaril@721
  1104
	}	
insilmaril@721
  1105
	parseAtom (undoCommand);
insilmaril@721
  1106
	reposition();
insilmaril@721
  1107
insilmaril@721
  1108
	undosAvail--;
insilmaril@721
  1109
	curStep--; 
insilmaril@721
  1110
	if (curStep<1) curStep=stepsTotal;
insilmaril@721
  1111
insilmaril@721
  1112
	redosAvail++;
insilmaril@721
  1113
insilmaril@721
  1114
	blockSaveState=blockSaveStateOrg;
insilmaril@721
  1115
/* TODO remove testing
insilmaril@721
  1116
	cout << "VymModel::undo() end\n";
insilmaril@721
  1117
	cout << "    undosAvail="<<undosAvail<<endl;
insilmaril@721
  1118
	cout << "    redosAvail="<<redosAvail<<endl;
insilmaril@721
  1119
	cout << "       curStep="<<curStep<<endl;
insilmaril@721
  1120
	cout << "    ---------------------------"<<endl<<endl;
insilmaril@721
  1121
*/
insilmaril@721
  1122
insilmaril@721
  1123
	undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
insilmaril@721
  1124
	undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
insilmaril@721
  1125
	undoSet.setEntry ("/history/curStep",QString::number(curStep));
insilmaril@721
  1126
	undoSet.writeSettings(histPath);
insilmaril@721
  1127
insilmaril@721
  1128
	mainWindow->updateHistory (undoSet);
insilmaril@721
  1129
	updateActions();
insilmaril@721
  1130
	selection.update();
insilmaril@721
  1131
	ensureSelectionVisible();
insilmaril@721
  1132
}
insilmaril@721
  1133
insilmaril@721
  1134
bool VymModel::isUndoAvailable()
insilmaril@721
  1135
{
insilmaril@721
  1136
	if (undoSet.readNumEntry("/history/undosAvail",0)>0)
insilmaril@721
  1137
		return true;
insilmaril@721
  1138
	else	
insilmaril@721
  1139
		return false;
insilmaril@721
  1140
}
insilmaril@721
  1141
insilmaril@721
  1142
void VymModel::gotoHistoryStep (int i)
insilmaril@721
  1143
{
insilmaril@721
  1144
	// Restore variables
insilmaril@721
  1145
	int undosAvail=undoSet.readNumEntry (QString("/history/undosAvail"));
insilmaril@721
  1146
	int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
insilmaril@721
  1147
insilmaril@721
  1148
	if (i<0) i=undosAvail+redosAvail;
insilmaril@721
  1149
insilmaril@721
  1150
	// Clicking above current step makes us undo things
insilmaril@721
  1151
	if (i<undosAvail) 
insilmaril@721
  1152
	{	
insilmaril@721
  1153
		for (int j=0; j<undosAvail-i; j++) undo();
insilmaril@721
  1154
		return;
insilmaril@721
  1155
	}	
insilmaril@721
  1156
	// Clicking below current step makes us redo things
insilmaril@721
  1157
	if (i>undosAvail) 
insilmaril@721
  1158
		for (int j=undosAvail; j<i; j++) 
insilmaril@721
  1159
		{
insilmaril@721
  1160
			if (debug) cout << "VymModel::gotoHistoryStep redo "<<j<<"/"<<undosAvail<<" i="<<i<<endl;
insilmaril@721
  1161
			redo();
insilmaril@721
  1162
		}
insilmaril@721
  1163
insilmaril@721
  1164
	// And ignore clicking the current row ;-)	
insilmaril@721
  1165
}
insilmaril@721
  1166
insilmaril@721
  1167
insilmaril@721
  1168
QString VymModel::getHistoryPath()
insilmaril@721
  1169
{
insilmaril@721
  1170
	QString histName(QString("history-%1").arg(curStep));
insilmaril@721
  1171
	return (tmpMapDir+"/"+histName);
insilmaril@721
  1172
}
insilmaril@721
  1173
insilmaril@721
  1174
void VymModel::saveState(const SaveMode &savemode, const QString &undoSelection, const QString &undoCom, const QString &redoSelection, const QString &redoCom, const QString &comment, LinkableMapObj *saveSel)
insilmaril@721
  1175
{
insilmaril@721
  1176
	sendData(redoCom);	//FIXME testing
insilmaril@721
  1177
insilmaril@721
  1178
	// Main saveState
insilmaril@721
  1179
insilmaril@721
  1180
insilmaril@721
  1181
	if (blockSaveState) return;
insilmaril@721
  1182
insilmaril@721
  1183
	if (debug) cout << "ME::saveState() for  "<<qPrintable (mapName)<<endl;
insilmaril@721
  1184
	
insilmaril@721
  1185
	// Find out current undo directory
insilmaril@721
  1186
	if (undosAvail<stepsTotal) undosAvail++;
insilmaril@721
  1187
	curStep++;
insilmaril@721
  1188
	if (curStep>stepsTotal) curStep=1;
insilmaril@721
  1189
	
insilmaril@721
  1190
	QString backupXML="";
insilmaril@721
  1191
	QString histDir=getHistoryPath();
insilmaril@721
  1192
	QString bakMapPath=histDir+"/map.xml";
insilmaril@721
  1193
insilmaril@721
  1194
	// Create histDir if not available
insilmaril@721
  1195
	QDir d(histDir);
insilmaril@721
  1196
	if (!d.exists()) 
insilmaril@721
  1197
		makeSubDirs (histDir);
insilmaril@721
  1198
insilmaril@721
  1199
	// Save depending on how much needs to be saved	
insilmaril@721
  1200
	if (saveSel)
insilmaril@721
  1201
		backupXML=saveToDir (histDir,mapName+"-",false, QPointF (),saveSel);
insilmaril@721
  1202
		
insilmaril@721
  1203
	QString undoCommand="";
insilmaril@721
  1204
	if (savemode==UndoCommand)
insilmaril@721
  1205
	{
insilmaril@721
  1206
		undoCommand=undoCom;
insilmaril@721
  1207
	}	
insilmaril@721
  1208
	else if (savemode==PartOfMap )
insilmaril@721
  1209
	{
insilmaril@721
  1210
		undoCommand=undoCom;
insilmaril@721
  1211
		undoCommand.replace ("PATH",bakMapPath);
insilmaril@721
  1212
	}
insilmaril@721
  1213
insilmaril@721
  1214
	if (!backupXML.isEmpty())
insilmaril@721
  1215
		// Write XML Data to disk
insilmaril@721
  1216
		saveStringToDisk (bakMapPath,backupXML);
insilmaril@721
  1217
insilmaril@721
  1218
	// We would have to save all actions in a tree, to keep track of 
insilmaril@721
  1219
	// possible redos after a action. Possible, but we are too lazy: forget about redos.
insilmaril@721
  1220
	redosAvail=0;
insilmaril@721
  1221
insilmaril@721
  1222
	// Write the current state to disk
insilmaril@721
  1223
	undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
insilmaril@721
  1224
	undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
insilmaril@721
  1225
	undoSet.setEntry ("/history/curStep",QString::number(curStep));
insilmaril@721
  1226
	undoSet.setEntry (QString("/history/step-%1/undoCommand").arg(curStep),undoCommand);
insilmaril@721
  1227
	undoSet.setEntry (QString("/history/step-%1/undoSelection").arg(curStep),undoSelection);
insilmaril@721
  1228
	undoSet.setEntry (QString("/history/step-%1/redoCommand").arg(curStep),redoCom);
insilmaril@721
  1229
	undoSet.setEntry (QString("/history/step-%1/redoSelection").arg(curStep),redoSelection);
insilmaril@721
  1230
	undoSet.setEntry (QString("/history/step-%1/comment").arg(curStep),comment);
insilmaril@721
  1231
	undoSet.setEntry (QString("/history/version"),vymVersion);
insilmaril@721
  1232
	undoSet.writeSettings(histPath);
insilmaril@721
  1233
insilmaril@721
  1234
	if (debug)
insilmaril@721
  1235
	{
insilmaril@721
  1236
		// TODO remove after testing
insilmaril@721
  1237
		//cout << "          into="<< histPath.toStdString()<<endl;
insilmaril@721
  1238
		cout << "    stepsTotal="<<stepsTotal<<
insilmaril@721
  1239
		", undosAvail="<<undosAvail<<
insilmaril@721
  1240
		", redosAvail="<<redosAvail<<
insilmaril@721
  1241
		", curStep="<<curStep<<endl;
insilmaril@721
  1242
		cout << "    ---------------------------"<<endl;
insilmaril@721
  1243
		cout << "    comment="<<comment.toStdString()<<endl;
insilmaril@721
  1244
		cout << "    undoCom="<<undoCommand.toStdString()<<endl;
insilmaril@721
  1245
		cout << "    undoSel="<<undoSelection.toStdString()<<endl;
insilmaril@721
  1246
		cout << "    redoCom="<<redoCom.toStdString()<<endl;
insilmaril@721
  1247
		cout << "    redoSel="<<redoSelection.toStdString()<<endl;
insilmaril@721
  1248
		if (saveSel) cout << "    saveSel="<<qPrintable (getSelectString(saveSel))<<endl;
insilmaril@721
  1249
		cout << "    ---------------------------"<<endl;
insilmaril@721
  1250
	}
insilmaril@721
  1251
insilmaril@721
  1252
	mainWindow->updateHistory (undoSet);
insilmaril@721
  1253
	setChanged();
insilmaril@721
  1254
	updateActions();
insilmaril@721
  1255
}
insilmaril@721
  1256
insilmaril@721
  1257
insilmaril@721
  1258
void VymModel::saveStateChangingPart(LinkableMapObj *undoSel, LinkableMapObj* redoSel, const QString &rc, const QString &comment)
insilmaril@721
  1259
{
insilmaril@721
  1260
	// save the selected part of the map, Undo will replace part of map 
insilmaril@721
  1261
	QString undoSelection="";
insilmaril@721
  1262
	if (undoSel)
insilmaril@721
  1263
		undoSelection=getSelectString(undoSel);
insilmaril@721
  1264
	else
insilmaril@721
  1265
		qWarning ("VymModel::saveStateChangingPart  no undoSel given!");
insilmaril@721
  1266
	QString redoSelection="";
insilmaril@721
  1267
	if (redoSel)
insilmaril@721
  1268
		redoSelection=getSelectString(undoSel);
insilmaril@721
  1269
	else
insilmaril@721
  1270
		qWarning ("VymModel::saveStateChangingPart  no redoSel given!");
insilmaril@721
  1271
		
insilmaril@721
  1272
insilmaril@721
  1273
	saveState (PartOfMap,
insilmaril@721
  1274
		undoSelection, "addMapReplace (\"PATH\")",
insilmaril@721
  1275
		redoSelection, rc, 
insilmaril@721
  1276
		comment, 
insilmaril@721
  1277
		undoSel);
insilmaril@721
  1278
}
insilmaril@721
  1279
insilmaril@721
  1280
void VymModel::saveStateRemovingPart(LinkableMapObj *redoSel, const QString &comment)
insilmaril@721
  1281
{
insilmaril@721
  1282
	if (!redoSel)
insilmaril@721
  1283
	{
insilmaril@721
  1284
		qWarning ("VymModel::saveStateRemovingPart  no redoSel given!");
insilmaril@721
  1285
		return;
insilmaril@721
  1286
	}
insilmaril@721
  1287
	QString undoSelection=getSelectString (redoSel->getParObj());
insilmaril@721
  1288
	QString redoSelection=getSelectString(redoSel);
insilmaril@721
  1289
	if (typeid(*redoSel) == typeid(BranchObj)  ) 
insilmaril@721
  1290
	{
insilmaril@721
  1291
		// save the selected branch of the map, Undo will insert part of map 
insilmaril@721
  1292
		saveState (PartOfMap,
insilmaril@721
  1293
			undoSelection, QString("addMapInsert (\"PATH\",%1)").arg(((BranchObj*)redoSel)->getNum()),
insilmaril@721
  1294
			redoSelection, "delete ()", 
insilmaril@721
  1295
			comment, 
insilmaril@721
  1296
			redoSel);
insilmaril@721
  1297
	}
insilmaril@721
  1298
}
insilmaril@721
  1299
insilmaril@721
  1300
insilmaril@721
  1301
void VymModel::saveState(LinkableMapObj *undoSel, const QString &uc, LinkableMapObj *redoSel, const QString &rc, const QString &comment) 
insilmaril@721
  1302
{
insilmaril@721
  1303
	// "Normal" savestate: save commands, selections and comment
insilmaril@721
  1304
	// so just save commands for undo and redo
insilmaril@721
  1305
	// and use current selection
insilmaril@721
  1306
insilmaril@721
  1307
	QString redoSelection="";
insilmaril@721
  1308
	if (redoSel) redoSelection=getSelectString(redoSel);
insilmaril@721
  1309
	QString undoSelection="";
insilmaril@721
  1310
	if (undoSel) undoSelection=getSelectString(undoSel);
insilmaril@721
  1311
insilmaril@721
  1312
	saveState (UndoCommand,
insilmaril@721
  1313
		undoSelection, uc,
insilmaril@721
  1314
		redoSelection, rc, 
insilmaril@721
  1315
		comment, 
insilmaril@721
  1316
		NULL);
insilmaril@721
  1317
}
insilmaril@721
  1318
insilmaril@721
  1319
void VymModel::saveState(const QString &undoSel, const QString &uc, const QString &redoSel, const QString &rc, const QString &comment) 
insilmaril@721
  1320
{
insilmaril@721
  1321
	// "Normal" savestate: save commands, selections and comment
insilmaril@721
  1322
	// so just save commands for undo and redo
insilmaril@721
  1323
	// and use current selection
insilmaril@721
  1324
	saveState (UndoCommand,
insilmaril@721
  1325
		undoSel, uc,
insilmaril@721
  1326
		redoSel, rc, 
insilmaril@721
  1327
		comment, 
insilmaril@721
  1328
		NULL);
insilmaril@721
  1329
}
insilmaril@721
  1330
insilmaril@721
  1331
void VymModel::saveState(const QString &uc, const QString &rc, const QString &comment) 
insilmaril@721
  1332
{
insilmaril@721
  1333
	// "Normal" savestate applied to model (no selection needed): 
insilmaril@721
  1334
	// save commands  and comment
insilmaril@721
  1335
	saveState (UndoCommand,
insilmaril@721
  1336
		NULL, uc,
insilmaril@721
  1337
		NULL, rc, 
insilmaril@721
  1338
		comment, 
insilmaril@721
  1339
		NULL);
insilmaril@721
  1340
}
insilmaril@721
  1341
insilmaril@721
  1342
insilmaril@650
  1343
QGraphicsScene* VymModel::getScene ()
insilmaril@650
  1344
{
insilmaril@650
  1345
	return mapScene;
insilmaril@650
  1346
}
insilmaril@650
  1347
insilmaril@650
  1348
BranchObj* VymModel::first()
insilmaril@650
  1349
{
insilmaril@650
  1350
	if (mapCenters.count()>0) 
insilmaril@650
  1351
		return mapCenters.first();
insilmaril@650
  1352
	else	
insilmaril@650
  1353
		return NULL;
insilmaril@650
  1354
}
insilmaril@650
  1355
	
insilmaril@650
  1356
BranchObj* VymModel::next(BranchObj *bo_start)
insilmaril@650
  1357
{
insilmaril@650
  1358
	BranchObj *rbo;
insilmaril@650
  1359
	BranchObj *bo=bo_start;
insilmaril@650
  1360
	if (bo)
insilmaril@650
  1361
	{
insilmaril@665
  1362
		// Try to find next branch in current MapCenter
insilmaril@650
  1363
		rbo=bo->next();
insilmaril@650
  1364
		if (rbo) return rbo;
insilmaril@650
  1365
insilmaril@650
  1366
		// Try to find MapCenter of bo
insilmaril@650
  1367
		while (bo->getDepth()>0) bo=(BranchObj*)bo->getParObj();
insilmaril@650
  1368
insilmaril@650
  1369
		// Try to find next MapCenter
insilmaril@650
  1370
		int i=mapCenters.indexOf ((MapCenterObj*)bo);
insilmaril@684
  1371
		if (i+2 > mapCenters.count() || i<0) return NULL;
insilmaril@684
  1372
		if (mapCenters.at(i+1)!=bo_start)
insilmaril@684
  1373
			return mapCenters.at(i+1);
insilmaril@650
  1374
	} 
insilmaril@650
  1375
	return NULL;
insilmaril@650
  1376
}
insilmaril@650
  1377
insilmaril@650
  1378
LinkableMapObj* VymModel::findMapObj(QPointF p, LinkableMapObj *excludeLMO)
insilmaril@650
  1379
{
insilmaril@650
  1380
	LinkableMapObj *lmo;
insilmaril@650
  1381
insilmaril@650
  1382
	for (int i=0;i<mapCenters.count(); i++)
insilmaril@650
  1383
	{
insilmaril@650
  1384
		lmo=mapCenters.at(i)->findMapObj (p,excludeLMO);
insilmaril@650
  1385
		if (lmo) return lmo;
insilmaril@650
  1386
	}
insilmaril@650
  1387
	return NULL;
insilmaril@650
  1388
}
insilmaril@650
  1389
insilmaril@650
  1390
LinkableMapObj* VymModel::findObjBySelect(const QString &s)
insilmaril@650
  1391
{
insilmaril@650
  1392
	LinkableMapObj *lmo;
insilmaril@650
  1393
	if (!s.isEmpty() )
insilmaril@650
  1394
	{
insilmaril@650
  1395
		QString part;
insilmaril@650
  1396
		QString typ;
insilmaril@650
  1397
		QString num;
insilmaril@650
  1398
		part=s.section(",",0,0);
insilmaril@652
  1399
		typ=part.left (2);
insilmaril@650
  1400
		num=part.right(part.length() - 3);
insilmaril@650
  1401
		if (typ=="mc" && num.toInt()>=0 && num.toInt() <mapCenters.count() )
insilmaril@650
  1402
			return mapCenters.at(num.toInt() );
insilmaril@650
  1403
	}		
insilmaril@650
  1404
insilmaril@650
  1405
	for (int i=0; i<mapCenters.count(); i++)
insilmaril@650
  1406
	{
insilmaril@650
  1407
		lmo=mapCenters.at(i)->findObjBySelect(s);
insilmaril@650
  1408
		if (lmo) return lmo;
insilmaril@650
  1409
	}	
insilmaril@650
  1410
	return NULL;
insilmaril@650
  1411
}
insilmaril@650
  1412
insilmaril@650
  1413
LinkableMapObj* VymModel::findID (const QString &s)
insilmaril@650
  1414
{
insilmaril@650
  1415
	LinkableMapObj *lmo;
insilmaril@650
  1416
	for (int i=0; i<mapCenters.count(); i++)
insilmaril@650
  1417
	{
insilmaril@650
  1418
		lmo=mapCenters.at(i)->findID (s);
insilmaril@650
  1419
		if (lmo) return lmo;
insilmaril@650
  1420
	}	
insilmaril@650
  1421
	return NULL;
insilmaril@650
  1422
}
insilmaril@650
  1423
insilmaril@721
  1424
void VymModel::removeSelection()
insilmaril@721
  1425
{
insilmaril@721
  1426
}
insilmaril@721
  1427
insilmaril@650
  1428
QString VymModel::saveToDir (const QString &tmpdir,const QString &prefix, int verbose, const QPointF &offset)
insilmaril@650
  1429
{
insilmaril@650
  1430
    QString s;
insilmaril@650
  1431
insilmaril@650
  1432
	for (int i=0; i<mapCenters.count(); i++)
insilmaril@650
  1433
		s+=mapCenters.at(i)->saveToDir (tmpdir,prefix,verbose,offset);
insilmaril@650
  1434
    return s;
insilmaril@650
  1435
}
insilmaril@650
  1436
insilmaril@721
  1437
//////////////////////////////////////////////
insilmaril@721
  1438
// Interface 
insilmaril@721
  1439
//////////////////////////////////////////////
insilmaril@721
  1440
void VymModel::setVersion (const QString &s)
insilmaril@721
  1441
{
insilmaril@721
  1442
	version=s;
insilmaril@721
  1443
}
insilmaril@721
  1444
insilmaril@721
  1445
void VymModel::setAuthor (const QString &s)
insilmaril@721
  1446
{
insilmaril@721
  1447
	saveState (
insilmaril@721
  1448
		QString ("setMapAuthor (\"%1\")").arg(author),
insilmaril@721
  1449
		QString ("setMapAuthor (\"%1\")").arg(s),
insilmaril@721
  1450
		QString ("Set author of map to \"%1\"").arg(s)
insilmaril@721
  1451
	);
insilmaril@721
  1452
insilmaril@721
  1453
	author=s;
insilmaril@721
  1454
}
insilmaril@721
  1455
insilmaril@721
  1456
QString VymModel::getAuthor()
insilmaril@721
  1457
{
insilmaril@721
  1458
	return author;
insilmaril@721
  1459
}
insilmaril@721
  1460
insilmaril@721
  1461
void VymModel::setComment (const QString &s)
insilmaril@721
  1462
{
insilmaril@721
  1463
	saveState (
insilmaril@721
  1464
		QString ("setMapComment (\"%1\")").arg(comment),
insilmaril@721
  1465
		QString ("setMapComment (\"%1\")").arg(s),
insilmaril@721
  1466
		QString ("Set comment of map")
insilmaril@721
  1467
	);
insilmaril@721
  1468
insilmaril@721
  1469
	comment=s;
insilmaril@721
  1470
}
insilmaril@721
  1471
insilmaril@721
  1472
QString VymModel::getComment ()
insilmaril@721
  1473
{
insilmaril@721
  1474
	return comment;
insilmaril@721
  1475
}
insilmaril@721
  1476
insilmaril@721
  1477
QString VymModel::getDate ()
insilmaril@721
  1478
{
insilmaril@721
  1479
	return QDate::currentDate().toString ("yyyy-MM-dd");
insilmaril@721
  1480
}
insilmaril@721
  1481
insilmaril@721
  1482
void VymModel::setHeading(const QString &s)
insilmaril@721
  1483
{
insilmaril@721
  1484
	BranchObj *sel=selection.getBranch();
insilmaril@721
  1485
	if (sel)
insilmaril@721
  1486
	{
insilmaril@721
  1487
		saveState(
insilmaril@721
  1488
			sel,
insilmaril@721
  1489
			"setHeading (\""+sel->getHeading()+"\")", 
insilmaril@721
  1490
			sel,
insilmaril@721
  1491
			"setHeading (\""+s+"\")", 
insilmaril@721
  1492
			QString("Set heading of %1 to \"%2\"").arg(getObjectName(sel)).arg(s) );
insilmaril@721
  1493
		sel->setHeading(s );
insilmaril@721
  1494
		reposition();
insilmaril@721
  1495
		selection.update();
insilmaril@721
  1496
		ensureSelectionVisible();
insilmaril@721
  1497
	}
insilmaril@721
  1498
}
insilmaril@721
  1499
insilmaril@721
  1500
QString VymModel::getHeading(bool &ok, QPoint &p)
insilmaril@721
  1501
{
insilmaril@721
  1502
	BranchObj *bo=selection.getBranch();
insilmaril@721
  1503
	if (bo)
insilmaril@721
  1504
	{
insilmaril@721
  1505
		ok=true;
insilmaril@721
  1506
		//p=scene->mapFromScene(bo->getAbsPos());	// FIXME this is view-dependant!!!
insilmaril@721
  1507
		return bo->getHeading();
insilmaril@721
  1508
	}
insilmaril@721
  1509
	ok=false;
insilmaril@721
  1510
	return QString();
insilmaril@721
  1511
}
insilmaril@721
  1512
insilmaril@721
  1513
insilmaril@721
  1514
void VymModel::setHeadingInt(const QString &s)
insilmaril@721
  1515
{
insilmaril@721
  1516
	BranchObj *bo=selection.getBranch();
insilmaril@721
  1517
	if (bo)
insilmaril@721
  1518
	{
insilmaril@721
  1519
		bo->setHeading(s);
insilmaril@721
  1520
		reposition();
insilmaril@721
  1521
		selection.update();
insilmaril@721
  1522
		ensureSelectionVisible();
insilmaril@721
  1523
	}
insilmaril@721
  1524
}
insilmaril@721
  1525
insilmaril@721
  1526
BranchObj* VymModel::findText (QString s, bool cs)
insilmaril@721
  1527
{
insilmaril@721
  1528
	QTextDocument::FindFlags flags=0;
insilmaril@721
  1529
	if (cs) flags=QTextDocument::FindCaseSensitively;
insilmaril@721
  1530
insilmaril@721
  1531
	if (!itFind) 
insilmaril@721
  1532
	{	// Nothing found or new find process
insilmaril@721
  1533
		if (EOFind)
insilmaril@721
  1534
			// nothing found, start again
insilmaril@721
  1535
			EOFind=false;
insilmaril@721
  1536
		itFind=first();
insilmaril@721
  1537
	}	
insilmaril@721
  1538
	bool searching=true;
insilmaril@721
  1539
	bool foundNote=false;
insilmaril@721
  1540
	while (searching && !EOFind)
insilmaril@721
  1541
	{
insilmaril@721
  1542
		if (itFind)
insilmaril@721
  1543
		{
insilmaril@721
  1544
			// Searching in Note
insilmaril@721
  1545
			if (itFind->getNote().contains(s,cs))
insilmaril@721
  1546
			{
insilmaril@721
  1547
				if (selection.single()!=itFind) 
insilmaril@721
  1548
				{
insilmaril@721
  1549
					selection.select(itFind);
insilmaril@721
  1550
					ensureSelectionVisible();
insilmaril@721
  1551
				}
insilmaril@721
  1552
				if (textEditor->findText(s,flags)) 
insilmaril@721
  1553
				{
insilmaril@721
  1554
					searching=false;
insilmaril@721
  1555
					foundNote=true;
insilmaril@721
  1556
				}	
insilmaril@721
  1557
			}
insilmaril@721
  1558
			// Searching in Heading
insilmaril@721
  1559
			if (searching && itFind->getHeading().contains (s,cs) ) 
insilmaril@721
  1560
			{
insilmaril@721
  1561
				selection.select(itFind);
insilmaril@721
  1562
				ensureSelectionVisible();
insilmaril@721
  1563
				searching=false;
insilmaril@721
  1564
			}
insilmaril@721
  1565
		}	
insilmaril@721
  1566
		if (!foundNote)
insilmaril@721
  1567
		{
insilmaril@721
  1568
			itFind=next(itFind);
insilmaril@721
  1569
			if (!itFind) EOFind=true;
insilmaril@721
  1570
		}
insilmaril@721
  1571
	//cout <<"still searching...  "<<qPrintable( itFind->getHeading())<<endl;
insilmaril@721
  1572
	}	
insilmaril@721
  1573
	if (!searching)
insilmaril@721
  1574
		return selection.getBranch();
insilmaril@721
  1575
	else
insilmaril@721
  1576
		return NULL;
insilmaril@721
  1577
}
insilmaril@721
  1578
insilmaril@721
  1579
void VymModel::findReset()
insilmaril@721
  1580
{	// Necessary if text to find changes during a find process
insilmaril@721
  1581
	itFind=NULL;
insilmaril@721
  1582
	EOFind=false;
insilmaril@721
  1583
}
insilmaril@721
  1584
insilmaril@721
  1585
insilmaril@721
  1586
insilmaril@721
  1587
void VymModel::setScene (QGraphicsScene *s)
insilmaril@721
  1588
{
insilmaril@721
  1589
	mapScene=s;
insilmaril@721
  1590
    init();	// Here we have a mapScene set, 
insilmaril@721
  1591
			// which is (still) needed to create MapCenters
insilmaril@721
  1592
}
insilmaril@721
  1593
insilmaril@721
  1594
void VymModel::setURL(const QString &url)
insilmaril@721
  1595
{
insilmaril@721
  1596
	BranchObj *bo=selection.getBranch();
insilmaril@721
  1597
	if (bo)
insilmaril@721
  1598
	{
insilmaril@721
  1599
		QString oldurl=bo->getURL();
insilmaril@721
  1600
		bo->setURL (url);
insilmaril@721
  1601
		saveState (
insilmaril@721
  1602
			bo,
insilmaril@721
  1603
			QString ("setURL (\"%1\")").arg(oldurl),
insilmaril@721
  1604
			bo,
insilmaril@721
  1605
			QString ("setURL (\"%1\")").arg(url),
insilmaril@721
  1606
			QString ("set URL of %1 to %2").arg(getObjectName(bo)).arg(url)
insilmaril@721
  1607
		);
insilmaril@721
  1608
		updateActions();
insilmaril@721
  1609
		reposition();
insilmaril@721
  1610
		selection.update();
insilmaril@721
  1611
		ensureSelectionVisible();
insilmaril@721
  1612
	}
insilmaril@721
  1613
}	
insilmaril@721
  1614
insilmaril@721
  1615
QString VymModel::getURL()
insilmaril@721
  1616
{
insilmaril@721
  1617
	BranchObj *bo=selection.getBranch();
insilmaril@721
  1618
	if (bo)
insilmaril@721
  1619
		return bo->getURL();
insilmaril@721
  1620
	else
insilmaril@721
  1621
		return "";
insilmaril@721
  1622
}
insilmaril@721
  1623
insilmaril@721
  1624
QStringList VymModel::getURLs()
insilmaril@721
  1625
{
insilmaril@721
  1626
	QStringList urls;
insilmaril@721
  1627
	BranchObj *bo=selection.getBranch();
insilmaril@721
  1628
	if (bo)
insilmaril@721
  1629
	{		
insilmaril@721
  1630
		bo=bo->first();	
insilmaril@721
  1631
		while (bo) 
insilmaril@721
  1632
		{
insilmaril@721
  1633
			if (!bo->getURL().isEmpty()) urls.append( bo->getURL());
insilmaril@721
  1634
			bo=bo->next();
insilmaril@721
  1635
		}	
insilmaril@721
  1636
	}	
insilmaril@721
  1637
	return urls;
insilmaril@721
  1638
}
insilmaril@721
  1639
insilmaril@721
  1640
void VymModel::linkFloatImageTo(const QString &dstString)	
insilmaril@721
  1641
{
insilmaril@721
  1642
	FloatImageObj *fio=selection.getFloatImage();
insilmaril@721
  1643
	if (fio)
insilmaril@721
  1644
	{
insilmaril@721
  1645
		BranchObj *dst=(BranchObj*)findObjBySelect(dstString);
insilmaril@721
  1646
		if (dst && (typeid(*dst)==typeid (BranchObj) || 
insilmaril@721
  1647
					typeid(*dst)==typeid (MapCenterObj)))
insilmaril@721
  1648
		{			
insilmaril@721
  1649
			LinkableMapObj *dstPar=dst->getParObj();
insilmaril@721
  1650
			QString parString=getSelectString(dstPar);
insilmaril@721
  1651
			QString fioPreSelectString=getSelectString(fio);
insilmaril@721
  1652
			QString fioPreParentSelectString=getSelectString (fio->getParObj());
insilmaril@721
  1653
			((BranchObj*)dst)->addFloatImage (fio);
insilmaril@721
  1654
			selection.unselect();
insilmaril@721
  1655
			((BranchObj*)(fio->getParObj()))->removeFloatImage (fio);
insilmaril@721
  1656
			fio=((BranchObj*)dst)->getLastFloatImage();
insilmaril@721
  1657
			fio->setRelPos();
insilmaril@721
  1658
			fio->reposition();
insilmaril@721
  1659
			selection.select(fio);
insilmaril@721
  1660
			saveState(
insilmaril@721
  1661
				getSelectString(fio),
insilmaril@721
  1662
				QString("linkTo (\"%1\")").arg(fioPreParentSelectString), 
insilmaril@721
  1663
				fioPreSelectString, 
insilmaril@721
  1664
				QString ("linkTo (\"%1\")").arg(dstString),
insilmaril@721
  1665
				QString ("Link floatimage to %1").arg(getObjectName(dst)));
insilmaril@721
  1666
		}
insilmaril@721
  1667
	}
insilmaril@721
  1668
}
insilmaril@721
  1669
insilmaril@721
  1670
insilmaril@721
  1671
void VymModel::setFrameType(const FrameObj::FrameType &t)
insilmaril@721
  1672
{
insilmaril@721
  1673
	BranchObj *bo=selection.getBranch();
insilmaril@721
  1674
	if (bo)
insilmaril@721
  1675
	{
insilmaril@721
  1676
		QString s=bo->getFrameTypeName();
insilmaril@721
  1677
		bo->setFrameType (t);
insilmaril@721
  1678
		saveState (bo, QString("setFrameType (\"%1\")").arg(s),
insilmaril@721
  1679
			bo, QString ("setFrameType (\"%1\")").arg(bo->getFrameTypeName()),QString ("set type of frame to %1").arg(s));
insilmaril@721
  1680
		reposition();
insilmaril@721
  1681
		bo->updateLink();
insilmaril@721
  1682
	}
insilmaril@721
  1683
}
insilmaril@721
  1684
insilmaril@721
  1685
void VymModel::setFrameType(const QString &s)	
insilmaril@721
  1686
{
insilmaril@721
  1687
	BranchObj *bo=selection.getBranch();
insilmaril@721
  1688
	if (bo)
insilmaril@721
  1689
	{
insilmaril@721
  1690
		saveState (bo, QString("setFrameType (\"%1\")").arg(bo->getFrameTypeName()),
insilmaril@721
  1691
			bo, QString ("setFrameType (\"%1\")").arg(s),QString ("set type of frame to %1").arg(s));
insilmaril@721
  1692
		bo->setFrameType (s);
insilmaril@721
  1693
		reposition();
insilmaril@721
  1694
		bo->updateLink();
insilmaril@721
  1695
	}
insilmaril@721
  1696
}
insilmaril@721
  1697
insilmaril@721
  1698
void VymModel::setFramePenColor(const QColor &c)	
insilmaril@721
  1699
{
insilmaril@721
  1700
	BranchObj *bo=selection.getBranch();
insilmaril@721
  1701
	if (bo)
insilmaril@721
  1702
	{
insilmaril@721
  1703
		saveState (bo, QString("setFramePenColor (\"%1\")").arg(bo->getFramePenColor().name() ),
insilmaril@721
  1704
			bo, QString ("setFramePenColor (\"%1\")").arg(c.name() ),QString ("set pen color of frame to %1").arg(c.name() ));
insilmaril@721
  1705
		bo->setFramePenColor (c);
insilmaril@721
  1706
	}	
insilmaril@721
  1707
}
insilmaril@721
  1708
insilmaril@721
  1709
void VymModel::setFrameBrushColor(const QColor &c)	
insilmaril@721
  1710
{
insilmaril@721
  1711
	BranchObj *bo=selection.getBranch();
insilmaril@721
  1712
	if (bo)
insilmaril@721
  1713
	{
insilmaril@721
  1714
		saveState (bo, QString("setFrameBrushColor (\"%1\")").arg(bo->getFrameBrushColor().name() ),
insilmaril@721
  1715
			bo, QString ("setFrameBrushColor (\"%1\")").arg(c.name() ),QString ("set brush color of frame to %1").arg(c.name() ));
insilmaril@721
  1716
		bo->setFrameBrushColor (c);
insilmaril@721
  1717
	}	
insilmaril@721
  1718
}
insilmaril@721
  1719
insilmaril@721
  1720
void VymModel::setFramePadding (const int &i)
insilmaril@721
  1721
{
insilmaril@721
  1722
	BranchObj *bo=selection.getBranch();
insilmaril@721
  1723
	if (bo)
insilmaril@721
  1724
	{
insilmaril@721
  1725
		saveState (bo, QString("setFramePadding (\"%1\")").arg(bo->getFramePadding() ),
insilmaril@721
  1726
			bo, QString ("setFramePadding (\"%1\")").arg(i),QString ("set brush color of frame to %1").arg(i));
insilmaril@721
  1727
		bo->setFramePadding (i);
insilmaril@721
  1728
		reposition();
insilmaril@721
  1729
		bo->updateLink();
insilmaril@721
  1730
	}	
insilmaril@721
  1731
}
insilmaril@721
  1732
insilmaril@721
  1733
void VymModel::setFrameBorderWidth(const int &i)
insilmaril@721
  1734
{
insilmaril@721
  1735
	BranchObj *bo=selection.getBranch();
insilmaril@721
  1736
	if (bo)
insilmaril@721
  1737
	{
insilmaril@721
  1738
		saveState (bo, QString("setFrameBorderWidth (\"%1\")").arg(bo->getFrameBorderWidth() ),
insilmaril@721
  1739
			bo, QString ("setFrameBorderWidth (\"%1\")").arg(i),QString ("set border width of frame to %1").arg(i));
insilmaril@721
  1740
		bo->setFrameBorderWidth (i);
insilmaril@721
  1741
		reposition();
insilmaril@721
  1742
		bo->updateLink();
insilmaril@721
  1743
	}	
insilmaril@721
  1744
}
insilmaril@721
  1745
insilmaril@721
  1746
void VymModel::setIncludeImagesVer(bool b)	
insilmaril@721
  1747
{
insilmaril@721
  1748
	BranchObj *bo=selection.getBranch();
insilmaril@721
  1749
	if (bo)
insilmaril@721
  1750
	{
insilmaril@721
  1751
		QString u= b ? "false" : "true";
insilmaril@721
  1752
		QString r=!b ? "false" : "true";
insilmaril@721
  1753
		
insilmaril@721
  1754
		saveState(
insilmaril@721
  1755
			bo,
insilmaril@721
  1756
			QString("setIncludeImagesVertically (%1)").arg(u),
insilmaril@721
  1757
			bo, 
insilmaril@721
  1758
			QString("setIncludeImagesVertically (%1)").arg(r),
insilmaril@721
  1759
			QString("Include images vertically in %1").arg(getObjectName(bo))
insilmaril@721
  1760
		);	
insilmaril@721
  1761
		bo->setIncludeImagesVer(b);
insilmaril@721
  1762
		reposition();
insilmaril@721
  1763
	}	
insilmaril@721
  1764
}
insilmaril@721
  1765
insilmaril@721
  1766
void VymModel::setIncludeImagesHor(bool b)	
insilmaril@721
  1767
{
insilmaril@721
  1768
	BranchObj *bo=selection.getBranch();
insilmaril@721
  1769
	if (bo)
insilmaril@721
  1770
	{
insilmaril@721
  1771
		QString u= b ? "false" : "true";
insilmaril@721
  1772
		QString r=!b ? "false" : "true";
insilmaril@721
  1773
		
insilmaril@721
  1774
		saveState(
insilmaril@721
  1775
			bo,
insilmaril@721
  1776
			QString("setIncludeImagesHorizontally (%1)").arg(u),
insilmaril@721
  1777
			bo, 
insilmaril@721
  1778
			QString("setIncludeImagesHorizontally (%1)").arg(r),
insilmaril@721
  1779
			QString("Include images horizontally in %1").arg(getObjectName(bo))
insilmaril@721
  1780
		);	
insilmaril@721
  1781
		bo->setIncludeImagesHor(b);
insilmaril@721
  1782
		reposition();
insilmaril@721
  1783
	}	
insilmaril@721
  1784
}
insilmaril@721
  1785
insilmaril@721
  1786
void VymModel::setHideLinkUnselected (bool b)
insilmaril@721
  1787
{
insilmaril@721
  1788
	LinkableMapObj *sel=selection.single();
insilmaril@721
  1789
	if (sel &&
insilmaril@721
  1790
		(selection.type() == Selection::Branch || 
insilmaril@721
  1791
		selection.type() == Selection::MapCenter  ||
insilmaril@721
  1792
		selection.type() == Selection::FloatImage ))
insilmaril@721
  1793
	{
insilmaril@721
  1794
		QString u= b ? "false" : "true";
insilmaril@721
  1795
		QString r=!b ? "false" : "true";
insilmaril@721
  1796
		
insilmaril@721
  1797
		saveState(
insilmaril@721
  1798
			sel,
insilmaril@721
  1799
			QString("setHideLinkUnselected (%1)").arg(u),
insilmaril@721
  1800
			sel, 
insilmaril@721
  1801
			QString("setHideLinkUnselected (%1)").arg(r),
insilmaril@721
  1802
			QString("Hide link of %1 if unselected").arg(getObjectName(sel))
insilmaril@721
  1803
		);	
insilmaril@721
  1804
		sel->setHideLinkUnselected(b);
insilmaril@721
  1805
	}
insilmaril@721
  1806
}
insilmaril@721
  1807
insilmaril@721
  1808
void VymModel::setHideExport(bool b)
insilmaril@721
  1809
{
insilmaril@721
  1810
	BranchObj *bo=selection.getBranch();
insilmaril@721
  1811
	if (bo)
insilmaril@721
  1812
	{
insilmaril@721
  1813
		bo->setHideInExport (b);
insilmaril@721
  1814
		QString u= b ? "false" : "true";
insilmaril@721
  1815
		QString r=!b ? "false" : "true";
insilmaril@721
  1816
		
insilmaril@721
  1817
		saveState(
insilmaril@721
  1818
			bo,
insilmaril@721
  1819
			QString ("setHideExport (%1)").arg(u),
insilmaril@721
  1820
			bo,
insilmaril@721
  1821
			QString ("setHideExport (%1)").arg(r),
insilmaril@721
  1822
			QString ("Set HideExport flag of %1 to %2").arg(getObjectName(bo)).arg (r)
insilmaril@721
  1823
		);	
insilmaril@721
  1824
		updateActions();
insilmaril@721
  1825
		reposition();
insilmaril@721
  1826
		selection.update();
insilmaril@721
  1827
		// FIXME needed? scene()->update();
insilmaril@721
  1828
	}
insilmaril@721
  1829
}
insilmaril@721
  1830
insilmaril@721
  1831
void VymModel::toggleHideExport()
insilmaril@721
  1832
{
insilmaril@721
  1833
	BranchObj *bo=selection.getBranch();
insilmaril@721
  1834
	if (bo)
insilmaril@721
  1835
		setHideExport ( !bo->hideInExport() );
insilmaril@721
  1836
}
insilmaril@721
  1837
insilmaril@721
  1838
insilmaril@721
  1839
void VymModel::copy()
insilmaril@721
  1840
{
insilmaril@721
  1841
	LinkableMapObj *sel=selection.single();
insilmaril@721
  1842
	if (sel)
insilmaril@721
  1843
	{
insilmaril@721
  1844
		if (redosAvail == 0)
insilmaril@721
  1845
		{
insilmaril@721
  1846
			// Copy to history
insilmaril@721
  1847
			QString s=getSelectString(sel);
insilmaril@721
  1848
			saveState (PartOfMap, s, "nop ()", s, "copy ()","Copy selection to clipboard",sel  );
insilmaril@721
  1849
			curClipboard=curStep;
insilmaril@721
  1850
		}
insilmaril@721
  1851
insilmaril@721
  1852
		// Copy also to global clipboard, because we are at last step in history
insilmaril@721
  1853
		QString bakMapName(QString("history-%1").arg(curStep));
insilmaril@721
  1854
		QString bakMapDir(tmpMapDir +"/"+bakMapName);
insilmaril@721
  1855
		copyDir (bakMapDir,clipboardDir );
insilmaril@721
  1856
insilmaril@721
  1857
		clipboardEmpty=false;
insilmaril@721
  1858
		updateActions();
insilmaril@721
  1859
	}	    
insilmaril@721
  1860
}
insilmaril@721
  1861
insilmaril@721
  1862
insilmaril@721
  1863
void VymModel::pasteNoSave(const int &n)
insilmaril@721
  1864
{
insilmaril@721
  1865
	bool old=blockSaveState;
insilmaril@721
  1866
	blockSaveState=true;
insilmaril@721
  1867
	bool zippedOrg=zipped;
insilmaril@721
  1868
	if (redosAvail > 0 || n!=0)
insilmaril@721
  1869
	{
insilmaril@721
  1870
		// Use the "historical" buffer
insilmaril@721
  1871
		QString bakMapName(QString("history-%1").arg(n));
insilmaril@721
  1872
		QString bakMapDir(tmpMapDir +"/"+bakMapName);
insilmaril@721
  1873
		load (bakMapDir+"/"+clipboardFile,ImportAdd, VymMap);
insilmaril@721
  1874
	} else
insilmaril@721
  1875
		// Use the global buffer
insilmaril@721
  1876
		load (clipboardDir+"/"+clipboardFile,ImportAdd, VymMap);
insilmaril@721
  1877
	zipped=zippedOrg;
insilmaril@721
  1878
	blockSaveState=old;
insilmaril@721
  1879
}
insilmaril@721
  1880
insilmaril@721
  1881
void VymModel::paste()		
insilmaril@721
  1882
{   
insilmaril@721
  1883
	BranchObj *sel=selection.getBranch();
insilmaril@721
  1884
	if (sel)
insilmaril@721
  1885
	{
insilmaril@721
  1886
		saveStateChangingPart(
insilmaril@721
  1887
			sel,
insilmaril@721
  1888
			sel,
insilmaril@721
  1889
			QString ("paste (%1)").arg(curClipboard),
insilmaril@721
  1890
			QString("Paste to %1").arg( getObjectName(sel))
insilmaril@721
  1891
		);
insilmaril@721
  1892
		pasteNoSave(0);
insilmaril@721
  1893
		reposition();
insilmaril@721
  1894
	}
insilmaril@721
  1895
}
insilmaril@721
  1896
insilmaril@721
  1897
void VymModel::cut()
insilmaril@721
  1898
{
insilmaril@721
  1899
	LinkableMapObj *sel=selection.single();
insilmaril@721
  1900
	if ( sel && (selection.type() == Selection::Branch ||
insilmaril@721
  1901
		selection.type()==Selection::MapCenter ||
insilmaril@721
  1902
		selection.type()==Selection::FloatImage))
insilmaril@721
  1903
	{
insilmaril@721
  1904
	/* No savestate! savestate is called in cutNoSave
insilmaril@721
  1905
		saveStateChangingPart(
insilmaril@721
  1906
			sel->getParObj(),
insilmaril@721
  1907
			sel,
insilmaril@721
  1908
			"cut ()",
insilmaril@721
  1909
			QString("Cut %1").arg(getObjectName(sel ))
insilmaril@721
  1910
		);
insilmaril@721
  1911
	*/	
insilmaril@721
  1912
		copy();
insilmaril@721
  1913
		deleteSelection();
insilmaril@721
  1914
		reposition();
insilmaril@721
  1915
	}
insilmaril@721
  1916
}
insilmaril@721
  1917
insilmaril@721
  1918
void VymModel::moveBranchUp()
insilmaril@721
  1919
{
insilmaril@721
  1920
	BranchObj* bo=selection.getBranch();
insilmaril@721
  1921
	BranchObj* par;
insilmaril@721
  1922
	if (bo)
insilmaril@721
  1923
	{
insilmaril@721
  1924
		if (!bo->canMoveBranchUp()) return;
insilmaril@721
  1925
		par=(BranchObj*)(bo->getParObj());
insilmaril@721
  1926
		BranchObj *obo=par->moveBranchUp (bo);	// bo will be the one below selection
insilmaril@721
  1927
		saveState (getSelectString(bo),"moveBranchDown ()",getSelectString(obo),"moveBranchUp ()",QString("Move up %1").arg(getObjectName(bo)));
insilmaril@721
  1928
		reposition();
insilmaril@721
  1929
		//FIXME needed? scene()->update();
insilmaril@721
  1930
		selection.update();
insilmaril@721
  1931
		ensureSelectionVisible();
insilmaril@721
  1932
	}
insilmaril@721
  1933
}
insilmaril@721
  1934
insilmaril@721
  1935
void VymModel::moveBranchDown()
insilmaril@721
  1936
{
insilmaril@721
  1937
	BranchObj* bo=selection.getBranch();
insilmaril@721
  1938
	BranchObj* par;
insilmaril@721
  1939
	if (bo)
insilmaril@721
  1940
	{
insilmaril@721
  1941
		if (!bo->canMoveBranchDown()) return;
insilmaril@721
  1942
		par=(BranchObj*)(bo->getParObj());
insilmaril@721
  1943
		BranchObj *obo=par->moveBranchDown(bo);	// bo will be the one above selection
insilmaril@721
  1944
		saveState(getSelectString(bo),"moveBranchUp ()",getSelectString(obo),"moveBranchDown ()",QString("Move down %1").arg(getObjectName(bo)));
insilmaril@721
  1945
		reposition();
insilmaril@721
  1946
		//FIXME needed? scene()->update();
insilmaril@721
  1947
		selection.update();
insilmaril@721
  1948
		ensureSelectionVisible();
insilmaril@721
  1949
	}	
insilmaril@721
  1950
}
insilmaril@721
  1951
insilmaril@721
  1952
void VymModel::sortChildren()
insilmaril@721
  1953
{
insilmaril@721
  1954
	BranchObj* bo=selection.getBranch();
insilmaril@721
  1955
	if (bo)
insilmaril@721
  1956
	{
insilmaril@721
  1957
		if(bo->countBranches()>1)
insilmaril@721
  1958
		{
insilmaril@721
  1959
			saveStateChangingPart(bo,bo, "sortChildren ()",QString("Sort children of %1").arg(getObjectName(bo)));
insilmaril@721
  1960
			bo->sortChildren();
insilmaril@721
  1961
			reposition();
insilmaril@721
  1962
			ensureSelectionVisible();
insilmaril@721
  1963
		}
insilmaril@721
  1964
	}
insilmaril@721
  1965
}
insilmaril@721
  1966
insilmaril@721
  1967
MapCenterObj* VymModel::addMapCenter ()
insilmaril@721
  1968
{
insilmaril@721
  1969
	MapCenterObj *mco=addMapCenter (QPointF(0,0));
insilmaril@721
  1970
	selection.select (mco);
insilmaril@721
  1971
	updateActions();
insilmaril@721
  1972
	ensureSelectionVisible();
insilmaril@721
  1973
	saveState (
insilmaril@721
  1974
		mco,
insilmaril@721
  1975
		"delete()",
insilmaril@721
  1976
		NULL,
insilmaril@721
  1977
		// FIXME  how to position LineEdit without contextMenuPos ?
insilmaril@721
  1978
		// QString ("addMapCenter (%1,%2)").arg (contextMenuPos.x()).arg(contextMenuPos.y()),
insilmaril@721
  1979
		// QString ("Adding MapCenter to (%1,%2").arg (contextMenuPos.x()).arg(contextMenuPos.y())
insilmaril@721
  1980
		QString ("addMapCenter (%1,%2)").arg (0).arg(0),
insilmaril@721
  1981
		QString ("Adding MapCenter to (%1,%2").arg (0).arg(0)
insilmaril@721
  1982
	);	
insilmaril@721
  1983
	return mco;	
insilmaril@721
  1984
}
insilmaril@721
  1985
insilmaril@721
  1986
MapCenterObj* VymModel::addMapCenter(QPointF absPos)
insilmaril@721
  1987
{
insilmaril@721
  1988
	MapCenterObj *mapCenter = new MapCenterObj(mapScene);
insilmaril@721
  1989
	mapCenter->move (absPos);
insilmaril@721
  1990
    mapCenter->setVisibility (true);
insilmaril@721
  1991
	mapCenter->setHeading (QApplication::translate("Heading of mapcenter in new map", "New map"));
insilmaril@721
  1992
	mapCenter->setMapEditor(mapEditor);		//FIXME needed to get defLinkStyle, mapLinkColorHint ... for later added objects
insilmaril@721
  1993
	mapCenters.append(mapCenter);
insilmaril@721
  1994
	return mapCenter;
insilmaril@721
  1995
}
insilmaril@721
  1996
insilmaril@721
  1997
MapCenterObj *VymModel::removeMapCenter(MapCenterObj* mco)
insilmaril@721
  1998
{
insilmaril@721
  1999
	int i=mapCenters.indexOf (mco);
insilmaril@721
  2000
	if (i>=0)
insilmaril@721
  2001
	{
insilmaril@721
  2002
		mapCenters.removeAt (i);
insilmaril@721
  2003
		delete (mco);
insilmaril@721
  2004
		if (i>0) return mapCenters.at(i-1);	// Return previous MCO
insilmaril@721
  2005
	}
insilmaril@721
  2006
	return NULL;
insilmaril@721
  2007
}
insilmaril@721
  2008
insilmaril@721
  2009
insilmaril@721
  2010
BranchObj* VymModel::addNewBranchInt(int num)
insilmaril@721
  2011
{
insilmaril@721
  2012
	// Depending on pos:
insilmaril@721
  2013
	// -3		insert in children of parent  above selection 
insilmaril@721
  2014
	// -2		add branch to selection 
insilmaril@721
  2015
	// -1		insert in children of parent below selection 
insilmaril@721
  2016
	// 0..n		insert in children of parent at pos
insilmaril@721
  2017
	BranchObj *newbo=NULL;
insilmaril@721
  2018
	BranchObj *bo=selection.getBranch();
insilmaril@721
  2019
	if (bo)
insilmaril@721
  2020
	{
insilmaril@721
  2021
		if (num==-2)
insilmaril@721
  2022
		{
insilmaril@721
  2023
			// save scroll state. If scrolled, automatically select
insilmaril@721
  2024
			// new branch in order to tmp unscroll parent...
insilmaril@721
  2025
			newbo=bo->addBranch();
insilmaril@721
  2026
			
insilmaril@721
  2027
		}else if (num==-1)
insilmaril@721
  2028
		{
insilmaril@721
  2029
			num=bo->getNum()+1;
insilmaril@721
  2030
			bo=(BranchObj*)bo->getParObj();
insilmaril@721
  2031
			if (bo) newbo=bo->insertBranch(num);
insilmaril@721
  2032
		}else if (num==-3)
insilmaril@721
  2033
		{
insilmaril@721
  2034
			num=bo->getNum();
insilmaril@721
  2035
			bo=(BranchObj*)bo->getParObj();
insilmaril@721
  2036
			if (bo) newbo=bo->insertBranch(num);
insilmaril@721
  2037
		}
insilmaril@721
  2038
		if (!newbo) return NULL;
insilmaril@721
  2039
	}	
insilmaril@721
  2040
	return newbo;
insilmaril@721
  2041
}	
insilmaril@721
  2042
insilmaril@721
  2043
BranchObj* VymModel::addNewBranch(int pos)
insilmaril@721
  2044
{
insilmaril@721
  2045
	// Different meaning than num in addNewBranchInt!
insilmaril@721
  2046
	// -1	add above
insilmaril@721
  2047
	//  0	add as child
insilmaril@721
  2048
	// +1	add below
insilmaril@721
  2049
	BranchObj *bo = selection.getBranch();
insilmaril@721
  2050
	BranchObj *newbo=NULL;
insilmaril@721
  2051
insilmaril@721
  2052
	if (bo)
insilmaril@721
  2053
	{
insilmaril@721
  2054
		// FIXME  do we still need this in model? setCursor (Qt::ArrowCursor);
insilmaril@721
  2055
insilmaril@721
  2056
		newbo=addNewBranchInt (pos-2);
insilmaril@721
  2057
insilmaril@721
  2058
		if (newbo)
insilmaril@721
  2059
		{
insilmaril@721
  2060
			saveState(
insilmaril@721
  2061
				newbo,		
insilmaril@721
  2062
				"delete ()",
insilmaril@721
  2063
				bo,
insilmaril@721
  2064
				QString ("addBranch (%1)").arg(pos),
insilmaril@721
  2065
				QString ("Add new branch to %1").arg(getObjectName(bo)));	
insilmaril@721
  2066
insilmaril@721
  2067
			reposition();
insilmaril@721
  2068
			selection.update();
insilmaril@721
  2069
			latestSelectionString=getSelectString(newbo);
insilmaril@721
  2070
			// In Network mode, the client needs to know where the new branch is,
insilmaril@721
  2071
			// so we have to pass on this information via saveState.
insilmaril@721
  2072
			// TODO: Get rid of this positioning workaround
insilmaril@721
  2073
			QString ps=qpointfToString (newbo->getAbsPos());
insilmaril@721
  2074
			sendData ("selectLatestAdded ()");
insilmaril@721
  2075
			sendData (QString("move %1").arg(ps));
insilmaril@721
  2076
			sendSelection();
insilmaril@721
  2077
		}
insilmaril@721
  2078
	}	
insilmaril@721
  2079
	return newbo;
insilmaril@721
  2080
}
insilmaril@721
  2081
insilmaril@721
  2082
insilmaril@721
  2083
BranchObj* VymModel::addNewBranchBefore()
insilmaril@721
  2084
{
insilmaril@721
  2085
	BranchObj *newbo=NULL;
insilmaril@721
  2086
	BranchObj *bo = selection.getBranch();
insilmaril@721
  2087
	if (bo && selection.type()==Selection::Branch)
insilmaril@721
  2088
		 // We accept no MapCenterObj here, so we _have_ a parent
insilmaril@721
  2089
	{
insilmaril@721
  2090
		QPointF p=bo->getRelPos();
insilmaril@721
  2091
insilmaril@721
  2092
insilmaril@721
  2093
		BranchObj *parbo=(BranchObj*)(bo->getParObj());
insilmaril@721
  2094
insilmaril@721
  2095
		// add below selection
insilmaril@721
  2096
		newbo=parbo->insertBranch(bo->getNum()+1);
insilmaril@721
  2097
		if (newbo)
insilmaril@721
  2098
		{
insilmaril@721
  2099
			newbo->move2RelPos (p);
insilmaril@721
  2100
insilmaril@721
  2101
			// Move selection to new branch
insilmaril@721
  2102
			bo->linkTo (newbo,-1);
insilmaril@721
  2103
insilmaril@721
  2104
			saveState (newbo, "deleteKeepChildren ()", newbo, "addBranchBefore ()", 
insilmaril@721
  2105
				QString ("Add branch before %1").arg(getObjectName(bo)));
insilmaril@721
  2106
insilmaril@721
  2107
			reposition();
insilmaril@721
  2108
			selection.update();
insilmaril@721
  2109
		}
insilmaril@721
  2110
	}	
insilmaril@721
  2111
	latestSelectionString=selection.getSelectString();
insilmaril@721
  2112
	return newbo;
insilmaril@721
  2113
}
insilmaril@721
  2114
insilmaril@721
  2115
void VymModel::deleteSelection()
insilmaril@721
  2116
{
insilmaril@721
  2117
	BranchObj *bo = selection.getBranch();
insilmaril@721
  2118
	if (bo && selection.type()==Selection::MapCenter)
insilmaril@721
  2119
	{
insilmaril@721
  2120
	//	BranchObj* par=(BranchObj*)(bo->getParObj());
insilmaril@721
  2121
		selection.unselect();
insilmaril@721
  2122
	/* FIXME Note:  does saveStateRemovingPart work for MCO? (No parent!)
insilmaril@721
  2123
		saveStateRemovingPart (bo, QString ("Delete %1").arg(getObjectName(bo)));
insilmaril@721
  2124
		*/
insilmaril@721
  2125
		bo=removeMapCenter ((MapCenterObj*)bo);
insilmaril@721
  2126
		if (bo) 
insilmaril@721
  2127
		{
insilmaril@721
  2128
			selection.select (bo);
insilmaril@721
  2129
			ensureSelectionVisible();
insilmaril@721
  2130
			selection.update();
insilmaril@721
  2131
		}	
insilmaril@721
  2132
		reposition();
insilmaril@721
  2133
		return;
insilmaril@721
  2134
	}
insilmaril@721
  2135
	if (bo && selection.type()==Selection::Branch)
insilmaril@721
  2136
	{
insilmaril@721
  2137
		BranchObj* par=(BranchObj*)bo->getParObj();
insilmaril@721
  2138
		selection.unselect();
insilmaril@721
  2139
		saveStateRemovingPart (bo, QString ("Delete %1").arg(getObjectName(bo)));
insilmaril@721
  2140
		par->removeBranch(bo);
insilmaril@721
  2141
		selection.select (par);
insilmaril@721
  2142
		ensureSelectionVisible();
insilmaril@721
  2143
		reposition();
insilmaril@721
  2144
		selection.update();
insilmaril@721
  2145
		return;
insilmaril@721
  2146
	}
insilmaril@721
  2147
	FloatImageObj *fio=selection.getFloatImage();
insilmaril@721
  2148
	if (fio)
insilmaril@721
  2149
	{
insilmaril@721
  2150
		BranchObj* par=(BranchObj*)fio->getParObj();
insilmaril@721
  2151
		saveStateChangingPart(
insilmaril@721
  2152
			par, 
insilmaril@721
  2153
			fio,
insilmaril@721
  2154
			"delete ()",
insilmaril@721
  2155
			QString("Delete %1").arg(getObjectName(fio))
insilmaril@721
  2156
		);
insilmaril@721
  2157
		selection.unselect();
insilmaril@721
  2158
		par->removeFloatImage(fio);
insilmaril@721
  2159
		selection.select (par);
insilmaril@721
  2160
		reposition();
insilmaril@721
  2161
		selection.update();
insilmaril@721
  2162
		ensureSelectionVisible();
insilmaril@721
  2163
		return;
insilmaril@721
  2164
	}
insilmaril@721
  2165
}
insilmaril@721
  2166
insilmaril@721
  2167
void VymModel::deleteKeepChildren()
insilmaril@721
  2168
{
insilmaril@721
  2169
	BranchObj *bo=selection.getBranch();
insilmaril@721
  2170
	BranchObj *par;
insilmaril@721
  2171
	if (bo)
insilmaril@721
  2172
	{
insilmaril@721
  2173
		par=(BranchObj*)(bo->getParObj());
insilmaril@721
  2174
		QPointF p=bo->getRelPos();
insilmaril@721
  2175
		saveStateChangingPart(
insilmaril@721
  2176
			bo->getParObj(),
insilmaril@721
  2177
			bo,
insilmaril@721
  2178
			"deleteKeepChildren ()",
insilmaril@721
  2179
			QString("Remove %1 and keep its children").arg(getObjectName(bo))
insilmaril@721
  2180
		);
insilmaril@721
  2181
insilmaril@721
  2182
		QString sel=getSelectString(bo);
insilmaril@721
  2183
		unselect();
insilmaril@721
  2184
		par->removeBranchHere(bo);
insilmaril@721
  2185
		reposition();
insilmaril@721
  2186
		select (sel);
insilmaril@721
  2187
		selection.getBranch()->move2RelPos (p);
insilmaril@721
  2188
		reposition();
insilmaril@721
  2189
	}	
insilmaril@721
  2190
}
insilmaril@721
  2191
insilmaril@721
  2192
void VymModel::deleteChildren()
insilmaril@721
  2193
{
insilmaril@721
  2194
	BranchObj *bo=selection.getBranch();
insilmaril@721
  2195
	if (bo)
insilmaril@721
  2196
	{		
insilmaril@721
  2197
		saveStateChangingPart(
insilmaril@721
  2198
			bo, 
insilmaril@721
  2199
			bo,
insilmaril@721
  2200
			"deleteChildren ()",
insilmaril@721
  2201
			QString( "Remove children of branch %1").arg(getObjectName(bo))
insilmaril@721
  2202
		);
insilmaril@721
  2203
		bo->removeChildren();
insilmaril@721
  2204
		reposition();
insilmaril@721
  2205
	}	
insilmaril@721
  2206
}
insilmaril@721
  2207
insilmaril@721
  2208
insilmaril@721
  2209
bool VymModel::scrollBranch(BranchObj *bo)
insilmaril@721
  2210
{
insilmaril@721
  2211
	if (bo)
insilmaril@721
  2212
	{
insilmaril@721
  2213
		if (bo->isScrolled()) return false;
insilmaril@721
  2214
		if (bo->countBranches()==0) return false;
insilmaril@721
  2215
		if (bo->getDepth()==0) return false;
insilmaril@721
  2216
		QString u,r;
insilmaril@721
  2217
		r="scroll";
insilmaril@721
  2218
		u="unscroll";
insilmaril@721
  2219
		saveState(
insilmaril@721
  2220
			bo,
insilmaril@721
  2221
			QString ("%1 ()").arg(u),
insilmaril@721
  2222
			bo,
insilmaril@721
  2223
			QString ("%1 ()").arg(r),
insilmaril@721
  2224
			QString ("%1 %2").arg(r).arg(getObjectName(bo))
insilmaril@721
  2225
		);
insilmaril@721
  2226
		bo->toggleScroll();
insilmaril@721
  2227
		selection.update();
insilmaril@721
  2228
		// FIXME needed? scene()->update();
insilmaril@721
  2229
		return true;
insilmaril@721
  2230
	}	
insilmaril@721
  2231
	return false;
insilmaril@721
  2232
}
insilmaril@721
  2233
insilmaril@721
  2234
bool VymModel::unscrollBranch(BranchObj *bo)
insilmaril@721
  2235
{
insilmaril@721
  2236
	if (bo)
insilmaril@721
  2237
	{
insilmaril@721
  2238
		if (!bo->isScrolled()) return false;
insilmaril@721
  2239
		if (bo->countBranches()==0) return false;
insilmaril@721
  2240
		if (bo->getDepth()==0) return false;
insilmaril@721
  2241
		QString u,r;
insilmaril@721
  2242
		u="scroll";
insilmaril@721
  2243
		r="unscroll";
insilmaril@721
  2244
		saveState(
insilmaril@721
  2245
			bo,
insilmaril@721
  2246
			QString ("%1 ()").arg(u),
insilmaril@721
  2247
			bo,
insilmaril@721
  2248
			QString ("%1 ()").arg(r),
insilmaril@721
  2249
			QString ("%1 %2").arg(r).arg(getObjectName(bo))
insilmaril@721
  2250
		);
insilmaril@721
  2251
		bo->toggleScroll();
insilmaril@721
  2252
		selection.update();
insilmaril@721
  2253
		// FIXME needed? scene()->update();
insilmaril@721
  2254
		return true;
insilmaril@721
  2255
	}	
insilmaril@721
  2256
	return false;
insilmaril@721
  2257
}
insilmaril@721
  2258
insilmaril@721
  2259
void VymModel::toggleScroll()
insilmaril@721
  2260
{
insilmaril@721
  2261
	BranchObj *bo=selection.getBranch();
insilmaril@721
  2262
	if (selection.type()==Selection::Branch )
insilmaril@721
  2263
	{
insilmaril@721
  2264
		if (bo->isScrolled())
insilmaril@721
  2265
			unscrollBranch (bo);
insilmaril@721
  2266
		else
insilmaril@721
  2267
			scrollBranch (bo);
insilmaril@721
  2268
	}
insilmaril@721
  2269
}
insilmaril@721
  2270
insilmaril@721
  2271
void VymModel::unscrollChildren() 
insilmaril@721
  2272
{
insilmaril@721
  2273
	BranchObj *bo=selection.getBranch();
insilmaril@721
  2274
	if (bo)
insilmaril@721
  2275
	{
insilmaril@721
  2276
		bo->first();
insilmaril@721
  2277
		while (bo) 
insilmaril@721
  2278
		{
insilmaril@721
  2279
			if (bo->isScrolled()) unscrollBranch (bo);
insilmaril@721
  2280
			bo=bo->next();
insilmaril@721
  2281
		}
insilmaril@721
  2282
	}	
insilmaril@721
  2283
}
insilmaril@721
  2284
void VymModel::addFloatImage (const QPixmap &img) 
insilmaril@721
  2285
{
insilmaril@721
  2286
	BranchObj *bo=selection.getBranch();
insilmaril@721
  2287
	if (bo)
insilmaril@721
  2288
  {
insilmaril@721
  2289
	FloatImageObj *fio=bo->addFloatImage();
insilmaril@721
  2290
    fio->load(img);
insilmaril@721
  2291
    fio->setOriginalFilename("No original filename (image added by dropevent)");	
insilmaril@721
  2292
	QString s=getSelectString(bo);
insilmaril@721
  2293
	saveState (PartOfMap, s, "nop ()", s, "copy ()","Copy dropped image to clipboard",fio  );
insilmaril@721
  2294
	saveState (fio,"delete ()", bo,QString("paste(%1)").arg(curStep),"Pasting dropped image");
insilmaril@721
  2295
    reposition();
insilmaril@721
  2296
    // FIXME needed? scene()->update();
insilmaril@721
  2297
  }
insilmaril@721
  2298
}
insilmaril@721
  2299
insilmaril@721
  2300
insilmaril@721
  2301
void VymModel::colorBranch (QColor c)
insilmaril@721
  2302
{
insilmaril@721
  2303
	BranchObj *bo=selection.getBranch();
insilmaril@721
  2304
	if (bo)
insilmaril@721
  2305
	{
insilmaril@721
  2306
		saveState(
insilmaril@721
  2307
			bo, 
insilmaril@721
  2308
			QString ("colorBranch (\"%1\")").arg(bo->getColor().name()),
insilmaril@721
  2309
			bo,
insilmaril@721
  2310
			QString ("colorBranch (\"%1\")").arg(c.name()),
insilmaril@721
  2311
			QString("Set color of %1 to %2").arg(getObjectName(bo)).arg(c.name())
insilmaril@721
  2312
		);	
insilmaril@721
  2313
		bo->setColor(c); // color branch
insilmaril@721
  2314
	}
insilmaril@721
  2315
}
insilmaril@721
  2316
insilmaril@721
  2317
void VymModel::colorSubtree (QColor c)
insilmaril@721
  2318
{
insilmaril@721
  2319
	BranchObj *bo=selection.getBranch();
insilmaril@721
  2320
	if (bo) 
insilmaril@721
  2321
	{
insilmaril@721
  2322
		saveStateChangingPart(
insilmaril@721
  2323
			bo, 
insilmaril@721
  2324
			bo,
insilmaril@721
  2325
			QString ("colorSubtree (\"%1\")").arg(c.name()),
insilmaril@721
  2326
			QString ("Set color of %1 and children to %2").arg(getObjectName(bo)).arg(c.name())
insilmaril@721
  2327
		);	
insilmaril@721
  2328
		bo->setColorSubtree (c); // color links, color children
insilmaril@721
  2329
	}
insilmaril@721
  2330
}
insilmaril@721
  2331
insilmaril@721
  2332
QColor VymModel::getCurrentHeadingColor()
insilmaril@721
  2333
{
insilmaril@721
  2334
	BranchObj *bo=selection.getBranch();
insilmaril@721
  2335
	if (bo) return bo->getColor(); 
insilmaril@721
  2336
	
insilmaril@721
  2337
	QMessageBox::warning(0,"Warning","Can't get color of heading,\nthere's no branch selected");
insilmaril@721
  2338
	return Qt::black;
insilmaril@721
  2339
}
insilmaril@721
  2340
insilmaril@721
  2341
insilmaril@721
  2342
insilmaril@721
  2343
void VymModel::editURL()
insilmaril@721
  2344
{
insilmaril@721
  2345
	BranchObj *bo=selection.getBranch();
insilmaril@721
  2346
	if (bo)
insilmaril@721
  2347
	{		
insilmaril@721
  2348
		bool ok;
insilmaril@721
  2349
		QString text = QInputDialog::getText(
insilmaril@721
  2350
				"VYM", tr("Enter URL:"), QLineEdit::Normal,
insilmaril@721
  2351
				bo->getURL(), &ok, NULL);
insilmaril@721
  2352
		if ( ok) 
insilmaril@721
  2353
			// user entered something and pressed OK
insilmaril@721
  2354
			setURL(text);
insilmaril@721
  2355
	}
insilmaril@721
  2356
}
insilmaril@721
  2357
insilmaril@721
  2358
void VymModel::editLocalURL()
insilmaril@721
  2359
{
insilmaril@721
  2360
	BranchObj *bo=selection.getBranch();
insilmaril@721
  2361
	if (bo)
insilmaril@721
  2362
	{		
insilmaril@721
  2363
		QStringList filters;
insilmaril@721
  2364
		filters <<"All files (*)";
insilmaril@721
  2365
		filters << tr("Text","Filedialog") + " (*.txt)";
insilmaril@721
  2366
		filters << tr("Spreadsheet","Filedialog") + " (*.odp,*.sxc)";
insilmaril@721
  2367
		filters << tr("Textdocument","Filedialog") +" (*.odw,*.sxw)";
insilmaril@721
  2368
		filters << tr("Images","Filedialog") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)";
insilmaril@721
  2369
		QFileDialog *fd=new QFileDialog( NULL,vymName+" - " +tr("Set URL to a local file"));
insilmaril@721
  2370
		fd->setFilters (filters);
insilmaril@721
  2371
		fd->setCaption(vymName+" - " +tr("Set URL to a local file"));
insilmaril@721
  2372
		fd->setDirectory (lastFileDir);
insilmaril@721
  2373
		if (! bo->getVymLink().isEmpty() )
insilmaril@721
  2374
			fd->selectFile( bo->getURL() );
insilmaril@721
  2375
		fd->show();
insilmaril@721
  2376
insilmaril@721
  2377
		if ( fd->exec() == QDialog::Accepted )
insilmaril@721
  2378
		{
insilmaril@721
  2379
			lastFileDir=QDir (fd->directory().path());
insilmaril@721
  2380
			setURL (fd->selectedFile() );
insilmaril@721
  2381
		}
insilmaril@721
  2382
	}
insilmaril@721
  2383
}
insilmaril@721
  2384
insilmaril@721
  2385
insilmaril@721
  2386
void VymModel::editHeading2URL()
insilmaril@721
  2387
{
insilmaril@721
  2388
	BranchObj *bo=selection.getBranch();
insilmaril@721
  2389
	if (bo)
insilmaril@721
  2390
		setURL (bo->getHeading());
insilmaril@721
  2391
}	
insilmaril@721
  2392
insilmaril@721
  2393
void VymModel::editBugzilla2URL()
insilmaril@721
  2394
{
insilmaril@721
  2395
	BranchObj *bo=selection.getBranch();
insilmaril@721
  2396
	if (bo)
insilmaril@721
  2397
	{		
insilmaril@721
  2398
		QString url= "https://bugzilla.novell.com/show_bug.cgi?id="+bo->getHeading();
insilmaril@721
  2399
		setURL (url);
insilmaril@721
  2400
	}
insilmaril@721
  2401
}	
insilmaril@721
  2402
insilmaril@721
  2403
void VymModel::editFATE2URL()
insilmaril@721
  2404
{
insilmaril@721
  2405
	BranchObj *bo=selection.getBranch();
insilmaril@721
  2406
	if (bo)
insilmaril@721
  2407
	{		
insilmaril@721
  2408
		QString url= "http://keeper.suse.de:8080/webfate/match/id?value=ID"+bo->getHeading();
insilmaril@721
  2409
		saveState(
insilmaril@721
  2410
			bo,
insilmaril@721
  2411
			"setURL (\""+bo->getURL()+"\")",
insilmaril@721
  2412
			bo,
insilmaril@721
  2413
			"setURL (\""+url+"\")",
insilmaril@721
  2414
			QString("Use heading of %1 as link to FATE").arg(getObjectName(bo))
insilmaril@721
  2415
		);	
insilmaril@721
  2416
		bo->setURL (url);
insilmaril@721
  2417
		updateActions();
insilmaril@721
  2418
	}
insilmaril@721
  2419
}	
insilmaril@721
  2420
insilmaril@721
  2421
void VymModel::editVymLink()
insilmaril@721
  2422
{
insilmaril@721
  2423
	BranchObj *bo=selection.getBranch();
insilmaril@721
  2424
	if (bo)
insilmaril@721
  2425
	{		
insilmaril@721
  2426
		QStringList filters;
insilmaril@721
  2427
		filters <<"VYM map (*.vym)";
insilmaril@721
  2428
		QFileDialog *fd=new QFileDialog( NULL,vymName+" - " +tr("Link to another map"));
insilmaril@721
  2429
		fd->setFilters (filters);
insilmaril@721
  2430
		fd->setCaption(vymName+" - " +tr("Link to another map"));
insilmaril@721
  2431
		fd->setDirectory (lastFileDir);
insilmaril@721
  2432
		if (! bo->getVymLink().isEmpty() )
insilmaril@721
  2433
			fd->selectFile( bo->getVymLink() );
insilmaril@721
  2434
		fd->show();
insilmaril@721
  2435
insilmaril@721
  2436
		QString fn;
insilmaril@721
  2437
		if ( fd->exec() == QDialog::Accepted )
insilmaril@721
  2438
		{
insilmaril@721
  2439
			lastFileDir=QDir (fd->directory().path());
insilmaril@721
  2440
			saveState(
insilmaril@721
  2441
				bo,
insilmaril@721
  2442
				"setVymLink (\""+bo->getVymLink()+"\")",
insilmaril@721
  2443
				bo,
insilmaril@721
  2444
				"setVymLink (\""+fd->selectedFile()+"\")",
insilmaril@721
  2445
				QString("Set vymlink of %1 to %2").arg(getObjectName(bo)).arg(fd->selectedFile())
insilmaril@721
  2446
			);	
insilmaril@721
  2447
			setVymLink (fd->selectedFile() );	// FIXME ok?
insilmaril@721
  2448
		}
insilmaril@721
  2449
	}
insilmaril@721
  2450
}
insilmaril@721
  2451
insilmaril@721
  2452
void VymModel::setVymLink (const QString &s)
insilmaril@721
  2453
{
insilmaril@721
  2454
	// Internal function, no saveState needed
insilmaril@721
  2455
	BranchObj *bo=selection.getBranch();
insilmaril@721
  2456
	if (bo)
insilmaril@721
  2457
	{
insilmaril@721
  2458
		bo->setVymLink(s);
insilmaril@721
  2459
		reposition();
insilmaril@721
  2460
		updateActions();
insilmaril@721
  2461
		selection.update();
insilmaril@721
  2462
		ensureSelectionVisible();
insilmaril@721
  2463
	}
insilmaril@721
  2464
}
insilmaril@721
  2465
insilmaril@721
  2466
void VymModel::deleteVymLink()
insilmaril@721
  2467
{
insilmaril@721
  2468
	BranchObj *bo=selection.getBranch();
insilmaril@721
  2469
	if (bo)
insilmaril@721
  2470
	{		
insilmaril@721
  2471
		saveState(
insilmaril@721
  2472
			bo,
insilmaril@721
  2473
			"setVymLink (\""+bo->getVymLink()+"\")",
insilmaril@721
  2474
			bo,
insilmaril@721
  2475
			"setVymLink (\"\")",
insilmaril@721
  2476
			QString("Unset vymlink of %1").arg(getObjectName(bo))
insilmaril@721
  2477
		);	
insilmaril@721
  2478
		bo->setVymLink ("" );
insilmaril@721
  2479
		updateActions();
insilmaril@721
  2480
		reposition();
insilmaril@721
  2481
		// FIXME needed? scene()->update();
insilmaril@721
  2482
	}
insilmaril@721
  2483
}
insilmaril@721
  2484
insilmaril@721
  2485
QString VymModel::getVymLink()
insilmaril@721
  2486
{
insilmaril@721
  2487
	BranchObj *bo=selection.getBranch();
insilmaril@721
  2488
	if (bo)
insilmaril@721
  2489
		return bo->getVymLink();
insilmaril@721
  2490
	else	
insilmaril@721
  2491
		return "";
insilmaril@721
  2492
	
insilmaril@721
  2493
}
insilmaril@721
  2494
insilmaril@721
  2495
QStringList VymModel::getVymLinks()
insilmaril@721
  2496
{
insilmaril@721
  2497
	QStringList links;
insilmaril@721
  2498
	BranchObj *bo=selection.getBranch();
insilmaril@721
  2499
	if (bo)
insilmaril@721
  2500
	{		
insilmaril@721
  2501
		bo=bo->first();	
insilmaril@721
  2502
		while (bo) 
insilmaril@721
  2503
		{
insilmaril@721
  2504
			if (!bo->getVymLink().isEmpty()) links.append( bo->getVymLink());
insilmaril@721
  2505
			bo=bo->next();
insilmaril@721
  2506
		}	
insilmaril@721
  2507
	}	
insilmaril@721
  2508
	return links;
insilmaril@721
  2509
}
insilmaril@721
  2510
insilmaril@721
  2511
insilmaril@721
  2512
void VymModel::followXLink(int i)
insilmaril@721
  2513
{
insilmaril@721
  2514
	BranchObj *bo=selection.getBranch();
insilmaril@721
  2515
	if (bo)
insilmaril@721
  2516
	{
insilmaril@721
  2517
		bo=bo->XLinkTargetAt(i);
insilmaril@721
  2518
		if (bo) 
insilmaril@721
  2519
		{
insilmaril@721
  2520
			selection.select(bo);
insilmaril@721
  2521
			ensureSelectionVisible();
insilmaril@721
  2522
		}
insilmaril@721
  2523
	}
insilmaril@721
  2524
}
insilmaril@721
  2525
insilmaril@721
  2526
void VymModel::editXLink(int i)	// FIXME missing saveState
insilmaril@721
  2527
{
insilmaril@721
  2528
	BranchObj *bo=selection.getBranch();
insilmaril@721
  2529
	if (bo)
insilmaril@721
  2530
	{
insilmaril@721
  2531
		XLinkObj *xlo=bo->XLinkAt(i);
insilmaril@721
  2532
		if (xlo) 
insilmaril@721
  2533
		{
insilmaril@721
  2534
			EditXLinkDialog dia;
insilmaril@721
  2535
			dia.setXLink (xlo);
insilmaril@721
  2536
			dia.setSelection(bo);
insilmaril@721
  2537
			if (dia.exec() == QDialog::Accepted)
insilmaril@721
  2538
			{
insilmaril@721
  2539
				if (dia.useSettingsGlobal() )
insilmaril@721
  2540
				{
insilmaril@721
  2541
					setMapDefXLinkColor (xlo->getColor() );
insilmaril@721
  2542
					setMapDefXLinkWidth (xlo->getWidth() );
insilmaril@721
  2543
				}
insilmaril@721
  2544
				if (dia.deleteXLink())
insilmaril@721
  2545
					bo->deleteXLinkAt(i);
insilmaril@721
  2546
			}
insilmaril@721
  2547
		}	
insilmaril@721
  2548
	}
insilmaril@721
  2549
}
insilmaril@721
  2550
insilmaril@721
  2551
insilmaril@721
  2552
insilmaril@721
  2553
insilmaril@721
  2554
insilmaril@721
  2555
//////////////////////////////////////////////
insilmaril@721
  2556
// Scripting
insilmaril@721
  2557
//////////////////////////////////////////////
insilmaril@721
  2558
insilmaril@721
  2559
void VymModel::parseAtom(const QString &atom)
insilmaril@721
  2560
{
insilmaril@721
  2561
	BranchObj *selb=selection.getBranch();
insilmaril@721
  2562
	QString s,t;
insilmaril@721
  2563
	double x,y;
insilmaril@721
  2564
	int n;
insilmaril@721
  2565
	bool b,ok;
insilmaril@721
  2566
insilmaril@721
  2567
	// Split string s into command and parameters
insilmaril@721
  2568
	parser.parseAtom (atom);
insilmaril@721
  2569
	QString com=parser.getCommand();
insilmaril@721
  2570
	
insilmaril@721
  2571
	// External commands
insilmaril@721
  2572
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  2573
	if (com=="addBranch")
insilmaril@721
  2574
	{
insilmaril@721
  2575
		if (selection.isEmpty())
insilmaril@721
  2576
		{
insilmaril@721
  2577
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  2578
		} else if (! selb )
insilmaril@721
  2579
		{				  
insilmaril@721
  2580
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  2581
		} else 
insilmaril@721
  2582
		{	
insilmaril@721
  2583
			QList <int> pl;
insilmaril@721
  2584
			pl << 0 <<1;
insilmaril@721
  2585
			if (parser.checkParCount(pl))
insilmaril@721
  2586
			{
insilmaril@721
  2587
				if (parser.parCount()==0)
insilmaril@721
  2588
					addNewBranch (0);
insilmaril@721
  2589
				else
insilmaril@721
  2590
				{
insilmaril@721
  2591
					n=parser.parInt (ok,0);
insilmaril@721
  2592
					if (ok ) addNewBranch (n);
insilmaril@721
  2593
				}
insilmaril@721
  2594
			}
insilmaril@721
  2595
		}
insilmaril@721
  2596
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  2597
	} else if (com=="addBranchBefore")
insilmaril@721
  2598
	{
insilmaril@721
  2599
		if (selection.isEmpty())
insilmaril@721
  2600
		{
insilmaril@721
  2601
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  2602
		} else if (! selb )
insilmaril@721
  2603
		{				  
insilmaril@721
  2604
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  2605
		} else 
insilmaril@721
  2606
		{	
insilmaril@721
  2607
			if (parser.parCount()==0)
insilmaril@721
  2608
			{
insilmaril@721
  2609
				addNewBranchBefore ();
insilmaril@721
  2610
			}	
insilmaril@721
  2611
		}
insilmaril@721
  2612
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  2613
	} else if (com==QString("addMapCenter"))
insilmaril@721
  2614
	{
insilmaril@721
  2615
		if (parser.checkParCount(2))
insilmaril@721
  2616
		{
insilmaril@721
  2617
			x=parser.parDouble (ok,0);
insilmaril@721
  2618
			if (ok)
insilmaril@721
  2619
			{
insilmaril@721
  2620
				y=parser.parDouble (ok,1);
insilmaril@721
  2621
				if (ok) addMapCenter (QPointF(x,y));
insilmaril@721
  2622
			}
insilmaril@721
  2623
		}	
insilmaril@721
  2624
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  2625
	} else if (com==QString("addMapReplace"))
insilmaril@721
  2626
	{
insilmaril@721
  2627
		if (selection.isEmpty())
insilmaril@721
  2628
		{
insilmaril@721
  2629
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  2630
		} else if (! selb )
insilmaril@721
  2631
		{				  
insilmaril@721
  2632
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  2633
		} else if (parser.checkParCount(1))
insilmaril@721
  2634
		{
insilmaril@721
  2635
			//s=parser.parString (ok,0);	// selection
insilmaril@721
  2636
			t=parser.parString (ok,0);	// path to map
insilmaril@721
  2637
			if (QDir::isRelativePath(t)) t=(tmpMapDir + "/"+t);
insilmaril@721
  2638
			addMapReplaceInt(getSelectString(selb),t);	
insilmaril@721
  2639
		}
insilmaril@721
  2640
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  2641
	} else if (com==QString("addMapInsert"))
insilmaril@721
  2642
	{
insilmaril@721
  2643
		if (selection.isEmpty())
insilmaril@721
  2644
		{
insilmaril@721
  2645
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  2646
		} else if (! selb )
insilmaril@721
  2647
		{				  
insilmaril@721
  2648
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  2649
		} else 
insilmaril@721
  2650
		{	
insilmaril@721
  2651
			if (parser.checkParCount(2))
insilmaril@721
  2652
			{
insilmaril@721
  2653
				t=parser.parString (ok,0);	// path to map
insilmaril@721
  2654
				n=parser.parInt(ok,1);		// position
insilmaril@721
  2655
				if (QDir::isRelativePath(t)) t=(tmpMapDir + "/"+t);
insilmaril@721
  2656
				addMapInsertInt(t,n);	
insilmaril@721
  2657
			}
insilmaril@721
  2658
		}
insilmaril@721
  2659
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  2660
	} else if (com=="clearFlags")
insilmaril@721
  2661
	{
insilmaril@721
  2662
		if (selection.isEmpty() )
insilmaril@721
  2663
		{
insilmaril@721
  2664
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  2665
		} else if (! selb )
insilmaril@721
  2666
		{				  
insilmaril@721
  2667
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  2668
		} else if (parser.checkParCount(0))
insilmaril@721
  2669
		{
insilmaril@721
  2670
			selb->clearStandardFlags();	
insilmaril@721
  2671
			selb->updateFlagsToolbar();
insilmaril@721
  2672
		}
insilmaril@721
  2673
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  2674
	} else if (com=="colorBranch")
insilmaril@721
  2675
	{
insilmaril@721
  2676
		if (selection.isEmpty())
insilmaril@721
  2677
		{
insilmaril@721
  2678
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  2679
		} else if (! selb )
insilmaril@721
  2680
		{				  
insilmaril@721
  2681
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  2682
		} else if (parser.checkParCount(1))
insilmaril@721
  2683
		{	
insilmaril@721
  2684
			QColor c=parser.parColor (ok,0);
insilmaril@721
  2685
			if (ok) colorBranch (c);
insilmaril@721
  2686
		}	
insilmaril@721
  2687
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  2688
	} else if (com=="colorSubtree")
insilmaril@721
  2689
	{
insilmaril@721
  2690
		if (selection.isEmpty())
insilmaril@721
  2691
		{
insilmaril@721
  2692
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  2693
		} else if (! selb )
insilmaril@721
  2694
		{				  
insilmaril@721
  2695
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  2696
		} else if (parser.checkParCount(1))
insilmaril@721
  2697
		{	
insilmaril@721
  2698
			QColor c=parser.parColor (ok,0);
insilmaril@721
  2699
			if (ok) colorSubtree (c);
insilmaril@721
  2700
		}	
insilmaril@721
  2701
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  2702
	} else if (com=="copy")
insilmaril@721
  2703
	{
insilmaril@721
  2704
		if (selection.isEmpty())
insilmaril@721
  2705
		{
insilmaril@721
  2706
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  2707
		} else if (! selb )
insilmaril@721
  2708
		{				  
insilmaril@721
  2709
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  2710
		} else if (parser.checkParCount(0))
insilmaril@721
  2711
		{	
insilmaril@721
  2712
			//FIXME missing action for copy
insilmaril@721
  2713
		}	
insilmaril@721
  2714
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  2715
	} else if (com=="cut")
insilmaril@721
  2716
	{
insilmaril@721
  2717
		if (selection.isEmpty())
insilmaril@721
  2718
		{
insilmaril@721
  2719
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  2720
		} else if ( selection.type()!=Selection::Branch  && 
insilmaril@721
  2721
					selection.type()!=Selection::MapCenter  &&
insilmaril@721
  2722
					selection.type()!=Selection::FloatImage )
insilmaril@721
  2723
		{				  
insilmaril@721
  2724
			parser.setError (Aborted,"Type of selection is not a branch or floatimage");
insilmaril@721
  2725
		} else if (parser.checkParCount(0))
insilmaril@721
  2726
		{	
insilmaril@721
  2727
			cut();
insilmaril@721
  2728
		}	
insilmaril@721
  2729
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  2730
	} else if (com=="delete")
insilmaril@721
  2731
	{
insilmaril@721
  2732
		if (selection.isEmpty())
insilmaril@721
  2733
		{
insilmaril@721
  2734
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  2735
		} 
insilmaril@721
  2736
		/*else if (selection.type() != Selection::Branch && selection.type() != Selection::FloatImage )
insilmaril@721
  2737
		{
insilmaril@721
  2738
			parser.setError (Aborted,"Type of selection is wrong.");
insilmaril@721
  2739
		} 
insilmaril@721
  2740
		*/
insilmaril@721
  2741
		else if (parser.checkParCount(0))
insilmaril@721
  2742
		{	
insilmaril@721
  2743
			deleteSelection();
insilmaril@721
  2744
		}	
insilmaril@721
  2745
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  2746
	} else if (com=="deleteKeepChildren")
insilmaril@721
  2747
	{
insilmaril@721
  2748
		if (selection.isEmpty())
insilmaril@721
  2749
		{
insilmaril@721
  2750
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  2751
		} else if (! selb )
insilmaril@721
  2752
		{
insilmaril@721
  2753
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  2754
		} else if (parser.checkParCount(0))
insilmaril@721
  2755
		{	
insilmaril@721
  2756
			deleteKeepChildren();
insilmaril@721
  2757
		}	
insilmaril@721
  2758
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  2759
	} else if (com=="deleteChildren")
insilmaril@721
  2760
	{
insilmaril@721
  2761
		if (selection.isEmpty())
insilmaril@721
  2762
		{
insilmaril@721
  2763
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  2764
		} else if (! selb)
insilmaril@721
  2765
		{
insilmaril@721
  2766
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  2767
		} else if (parser.checkParCount(0))
insilmaril@721
  2768
		{	
insilmaril@721
  2769
			deleteChildren();
insilmaril@721
  2770
		}	
insilmaril@721
  2771
	/////////////////////////////////////////////////////////////////////