vymmodel.cpp
author insilmaril
Mon Aug 04 13:35:54 2008 +0000 (2008-08-04)
changeset 724 cf14046909cd
parent 722 462d39502273
child 725 7ea31701156e
permissions -rw-r--r--
Adding/moving MCO works again
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@723
   159
MapEditor* VymModel::getMapEditor()	// FIXME better return favourite editor here
insilmaril@650
   160
{
insilmaril@650
   161
	return mapEditor;
insilmaril@650
   162
}
insilmaril@650
   163
insilmaril@721
   164
bool VymModel::isRepositionBlocked()
insilmaril@650
   165
{
insilmaril@721
   166
	return blockReposition;
insilmaril@650
   167
}
insilmaril@650
   168
insilmaril@721
   169
void VymModel::updateActions()	// FIXME  maybe don't update if blockReposition is set
insilmaril@650
   170
{
insilmaril@721
   171
	// Tell mainwindow to update states of actions
insilmaril@721
   172
	mainWindow->updateActions();
insilmaril@650
   173
}
insilmaril@650
   174
insilmaril@721
   175
insilmaril@721
   176
insilmaril@721
   177
QString VymModel::saveToDir(const QString &tmpdir, const QString &prefix, bool writeflags, const QPointF &offset, LinkableMapObj *saveSel)
insilmaril@650
   178
{
insilmaril@721
   179
	// tmpdir		temporary directory to which data will be written
insilmaril@721
   180
	// prefix		mapname, which will be appended to images etc.
insilmaril@721
   181
	// writeflags	Only write flags for "real" save of map, not undo
insilmaril@721
   182
	// offset		offset of bbox of whole map in scene. 
insilmaril@721
   183
	//				Needed for XML export
insilmaril@721
   184
	
insilmaril@721
   185
	// Save Header
insilmaril@721
   186
	QString ls;
insilmaril@721
   187
	switch (linkstyle)
insilmaril@721
   188
	{
insilmaril@721
   189
		case LinkableMapObj::Line: 
insilmaril@721
   190
			ls="StyleLine";
insilmaril@721
   191
			break;
insilmaril@721
   192
		case LinkableMapObj::Parabel:
insilmaril@721
   193
			ls="StyleParabel";
insilmaril@721
   194
			break;
insilmaril@721
   195
		case LinkableMapObj::PolyLine:	
insilmaril@721
   196
			ls="StylePolyLine";
insilmaril@721
   197
			break;
insilmaril@721
   198
		default:
insilmaril@721
   199
			ls="StylePolyParabel";
insilmaril@721
   200
			break;
insilmaril@721
   201
	}	
insilmaril@721
   202
insilmaril@721
   203
	QString s="<?xml version=\"1.0\" encoding=\"utf-8\"?><!DOCTYPE vymmap>\n";
insilmaril@721
   204
	QString colhint="";
insilmaril@721
   205
	if (linkcolorhint==LinkableMapObj::HeadingColor) 
insilmaril@721
   206
		colhint=attribut("linkColorHint","HeadingColor");
insilmaril@721
   207
insilmaril@721
   208
	QString mapAttr=attribut("version",vymVersion);
insilmaril@721
   209
	if (!saveSel)
insilmaril@721
   210
		mapAttr+= attribut("author",author) +
insilmaril@721
   211
				  attribut("comment",comment) +
insilmaril@721
   212
			      attribut("date",getDate()) +
insilmaril@721
   213
		          attribut("backgroundColor", mapScene->backgroundBrush().color().name() ) +
insilmaril@721
   214
		          attribut("selectionColor", selection.getColor().name() ) +
insilmaril@721
   215
		          attribut("linkStyle", ls ) +
insilmaril@721
   216
		          attribut("linkColor", defLinkColor.name() ) +
insilmaril@721
   217
		          attribut("defXLinkColor", defXLinkColor.name() ) +
insilmaril@721
   218
		          attribut("defXLinkWidth", QString().setNum(defXLinkWidth,10) ) +
insilmaril@721
   219
		          colhint; 
insilmaril@721
   220
	s+=beginElement("vymmap",mapAttr);
insilmaril@721
   221
	incIndent();
insilmaril@721
   222
insilmaril@721
   223
	// Find the used flags while traversing the tree
insilmaril@721
   224
	standardFlagsDefault->resetUsedCounter();
insilmaril@721
   225
	
insilmaril@721
   226
	// Reset the counters before saving
insilmaril@721
   227
	// TODO constr. of FIO creates lots of objects, better do this in some other way...
insilmaril@721
   228
	FloatImageObj (mapScene).resetSaveCounter();
insilmaril@721
   229
insilmaril@721
   230
	// Build xml recursivly
insilmaril@721
   231
	if (!saveSel || typeid (*saveSel) == typeid (MapCenterObj))
insilmaril@721
   232
		// Save complete map, if saveSel not set
insilmaril@721
   233
		s+=saveToDir(tmpdir,prefix,writeflags,offset);
insilmaril@721
   234
	else
insilmaril@721
   235
	{
insilmaril@721
   236
		if ( typeid(*saveSel) == typeid(BranchObj) )
insilmaril@721
   237
			// Save Subtree
insilmaril@721
   238
			s+=((BranchObj*)(saveSel))->saveToDir(tmpdir,prefix,offset);
insilmaril@721
   239
		else if ( typeid(*saveSel) == typeid(FloatImageObj) )
insilmaril@721
   240
			// Save image
insilmaril@721
   241
			s+=((FloatImageObj*)(saveSel))->saveToDir(tmpdir,prefix);
insilmaril@721
   242
	}
insilmaril@721
   243
insilmaril@721
   244
	// Save local settings
insilmaril@721
   245
	s+=settings.getDataXML (destPath);
insilmaril@721
   246
insilmaril@721
   247
	// Save selection
insilmaril@721
   248
	if (!selection.isEmpty() && !saveSel ) 
insilmaril@721
   249
		s+=valueElement("select",selection.getSelectString());
insilmaril@721
   250
insilmaril@721
   251
	decIndent();
insilmaril@721
   252
	s+=endElement("vymmap");
insilmaril@721
   253
insilmaril@721
   254
	if (writeflags)
insilmaril@721
   255
		standardFlagsDefault->saveToDir (tmpdir+"/flags/","",writeflags);
insilmaril@721
   256
	return s;
insilmaril@650
   257
}
insilmaril@650
   258
insilmaril@721
   259
void VymModel::setFilePath(QString fpath, QString destname)
insilmaril@650
   260
{
insilmaril@721
   261
	if (fpath.isEmpty() || fpath=="")
insilmaril@721
   262
	{
insilmaril@721
   263
		filePath="";
insilmaril@721
   264
		fileName="";
insilmaril@721
   265
		destPath="";
insilmaril@721
   266
	} else
insilmaril@721
   267
	{
insilmaril@721
   268
		filePath=fpath;		// becomes absolute path
insilmaril@721
   269
		fileName=fpath;		// gets stripped of path
insilmaril@721
   270
		destPath=destname;	// needed for vymlinks and during load to reset fileChangedTime
insilmaril@721
   271
insilmaril@721
   272
		// If fpath is not an absolute path, complete it
insilmaril@721
   273
		filePath=QDir(fpath).absPath();
insilmaril@721
   274
		fileDir=filePath.left (1+filePath.findRev ("/"));
insilmaril@721
   275
insilmaril@721
   276
		// Set short name, too. Search from behind:
insilmaril@721
   277
		int i=fileName.findRev("/");
insilmaril@721
   278
		if (i>=0) fileName=fileName.remove (0,i+1);
insilmaril@721
   279
insilmaril@721
   280
		// Forget the .vym (or .xml) for name of map
insilmaril@721
   281
		mapName=fileName.left(fileName.findRev(".",-1,true) );
insilmaril@721
   282
	}
insilmaril@650
   283
}
insilmaril@650
   284
insilmaril@721
   285
void VymModel::setFilePath(QString fpath)
insilmaril@650
   286
{
insilmaril@721
   287
	setFilePath (fpath,fpath);
insilmaril@650
   288
}
insilmaril@650
   289
insilmaril@721
   290
QString VymModel::getFilePath()
insilmaril@650
   291
{
insilmaril@721
   292
	return filePath;
insilmaril@650
   293
}
insilmaril@650
   294
insilmaril@721
   295
QString VymModel::getFileName()
insilmaril@650
   296
{
insilmaril@721
   297
	return fileName;
insilmaril@650
   298
}
insilmaril@650
   299
insilmaril@721
   300
QString VymModel::getMapName()
insilmaril@721
   301
{
insilmaril@721
   302
	return mapName;
insilmaril@721
   303
}
insilmaril@721
   304
insilmaril@721
   305
QString VymModel::getDestPath()
insilmaril@721
   306
{
insilmaril@721
   307
	return destPath;
insilmaril@721
   308
}
insilmaril@721
   309
insilmaril@721
   310
ErrorCode VymModel::load (QString fname, const LoadMode &lmode, const FileType &ftype)
insilmaril@721
   311
{
insilmaril@721
   312
	ErrorCode err=success;
insilmaril@721
   313
insilmaril@721
   314
	parseBaseHandler *handler;
insilmaril@721
   315
	fileType=ftype;
insilmaril@721
   316
	switch (fileType)
insilmaril@721
   317
	{
insilmaril@721
   318
		case VymMap: handler=new parseVYMHandler; break;
insilmaril@721
   319
		case FreemindMap : handler=new parseFreemindHandler; break;
insilmaril@721
   320
		default: 
insilmaril@721
   321
			QMessageBox::critical( 0, tr( "Critical Parse Error" ),
insilmaril@721
   322
				   "Unknown FileType in VymModel::load()");
insilmaril@721
   323
		return aborted;	
insilmaril@721
   324
	}
insilmaril@721
   325
	if (lmode==NewMap)
insilmaril@721
   326
	{
insilmaril@721
   327
		selection.clear();
insilmaril@721
   328
		// FIXME not needed??? model->setMapEditor(this);
insilmaril@721
   329
		// (map state is set later at end of load...)
insilmaril@721
   330
	} else
insilmaril@721
   331
	{
insilmaril@721
   332
		BranchObj *bo=selection.getBranch();
insilmaril@721
   333
		if (!bo) return aborted;
insilmaril@721
   334
		if (lmode==ImportAdd)
insilmaril@721
   335
			saveStateChangingPart(
insilmaril@721
   336
				bo,
insilmaril@721
   337
				bo,
insilmaril@721
   338
				QString("addMapInsert (%1)").arg(fname),
insilmaril@721
   339
				QString("Add map %1 to %2").arg(fname).arg(getObjectName(bo)));
insilmaril@721
   340
		else	
insilmaril@721
   341
			saveStateChangingPart(
insilmaril@721
   342
				bo,
insilmaril@721
   343
				bo,
insilmaril@721
   344
				QString("addMapReplace(%1)").arg(fname),
insilmaril@721
   345
				QString("Add map %1 to %2").arg(fname).arg(getObjectName(bo)));
insilmaril@721
   346
	}	
insilmaril@721
   347
    
insilmaril@721
   348
insilmaril@721
   349
	// Create temporary directory for packing
insilmaril@721
   350
	bool ok;
insilmaril@721
   351
	QString tmpZipDir=makeTmpDir (ok,"vym-pack");
insilmaril@721
   352
	if (!ok)
insilmaril@721
   353
	{
insilmaril@721
   354
		QMessageBox::critical( 0, tr( "Critical Load Error" ),
insilmaril@721
   355
		   tr("Couldn't create temporary directory before load\n"));
insilmaril@721
   356
		return aborted; 
insilmaril@721
   357
	}
insilmaril@721
   358
insilmaril@721
   359
	// Try to unzip file
insilmaril@721
   360
	err=unzipDir (tmpZipDir,fname);
insilmaril@721
   361
	QString xmlfile;
insilmaril@721
   362
	if (err==nozip)
insilmaril@721
   363
	{
insilmaril@721
   364
		xmlfile=fname;
insilmaril@721
   365
		zipped=false;
insilmaril@721
   366
	} else
insilmaril@721
   367
	{
insilmaril@721
   368
		zipped=true;
insilmaril@721
   369
		
insilmaril@721
   370
		// Look for mapname.xml
insilmaril@721
   371
		xmlfile= fname.left(fname.findRev(".",-1,true));
insilmaril@721
   372
		xmlfile=xmlfile.section( '/', -1 );
insilmaril@721
   373
		QFile mfile( tmpZipDir + "/" + xmlfile + ".xml");
insilmaril@721
   374
		if (!mfile.exists() )
insilmaril@721
   375
		{
insilmaril@721
   376
			// mapname.xml does not exist, well, 
insilmaril@721
   377
			// maybe someone renamed the mapname.vym file...
insilmaril@721
   378
			// Try to find any .xml in the toplevel 
insilmaril@721
   379
			// directory of the .vym file
insilmaril@721
   380
			QStringList flist=QDir (tmpZipDir).entryList("*.xml");
insilmaril@721
   381
			if (flist.count()==1) 
insilmaril@721
   382
			{
insilmaril@721
   383
				// Only one entry, take this one
insilmaril@721
   384
				xmlfile=tmpZipDir + "/"+flist.first();
insilmaril@721
   385
			} else
insilmaril@721
   386
			{
insilmaril@721
   387
				for ( QStringList::Iterator it = flist.begin(); it != flist.end(); ++it ) 
insilmaril@721
   388
					*it=tmpZipDir + "/" + *it;
insilmaril@721
   389
				// TODO Multiple entries, load all (but only the first one into this ME)
insilmaril@721
   390
				//mainWindow->fileLoadFromTmp (flist);
insilmaril@721
   391
				//returnCode=1;	// Silently forget this attempt to load
insilmaril@721
   392
				qWarning ("MainWindow::load (fn)  multimap found...");
insilmaril@721
   393
			}	
insilmaril@721
   394
				
insilmaril@721
   395
			if (flist.isEmpty() )
insilmaril@721
   396
			{
insilmaril@721
   397
				QMessageBox::critical( 0, tr( "Critical Load Error" ),
insilmaril@721
   398
						   tr("Couldn't find a map (*.xml) in .vym archive.\n"));
insilmaril@721
   399
				err=aborted;				   
insilmaril@721
   400
			}	
insilmaril@721
   401
		} //file doesn't exist	
insilmaril@721
   402
		else
insilmaril@721
   403
			xmlfile=mfile.name();
insilmaril@721
   404
	}
insilmaril@721
   405
insilmaril@721
   406
	QFile file( xmlfile);
insilmaril@721
   407
insilmaril@721
   408
	// I am paranoid: file should exist anyway
insilmaril@721
   409
	// according to check in mainwindow.
insilmaril@721
   410
	if (!file.exists() )
insilmaril@721
   411
	{
insilmaril@721
   412
		QMessageBox::critical( 0, tr( "Critical Parse Error" ),
insilmaril@721
   413
				   tr(QString("Couldn't open map %1").arg(file.name())));
insilmaril@721
   414
		err=aborted;	
insilmaril@721
   415
	} else
insilmaril@721
   416
	{
insilmaril@721
   417
		bool blockSaveStateOrg=blockSaveState;
insilmaril@721
   418
		blockReposition=true;
insilmaril@721
   419
		blockSaveState=true;
insilmaril@721
   420
		QXmlInputSource source( file);
insilmaril@721
   421
		QXmlSimpleReader reader;
insilmaril@721
   422
		reader.setContentHandler( handler );
insilmaril@721
   423
		reader.setErrorHandler( handler );
insilmaril@721
   424
		handler->setModel ( this);
insilmaril@721
   425
insilmaril@721
   426
insilmaril@721
   427
		// We need to set the tmpDir in order  to load files with rel. path
insilmaril@721
   428
		QString tmpdir;
insilmaril@721
   429
		if (zipped)
insilmaril@721
   430
			tmpdir=tmpZipDir;
insilmaril@721
   431
		else
insilmaril@721
   432
			tmpdir=fname.left(fname.findRev("/",-1));	
insilmaril@721
   433
		handler->setTmpDir (tmpdir);
insilmaril@721
   434
		handler->setInputFile (file.name());
insilmaril@721
   435
		handler->setLoadMode (lmode);
insilmaril@721
   436
		bool ok = reader.parse( source );
insilmaril@721
   437
		blockReposition=false;
insilmaril@721
   438
		blockSaveState=blockSaveStateOrg;
insilmaril@721
   439
		file.close();
insilmaril@721
   440
		if ( ok ) 
insilmaril@721
   441
		{
insilmaril@721
   442
			reposition();	// FIXME reposition the view instead...
insilmaril@721
   443
			selection.update();
insilmaril@721
   444
			if (lmode==NewMap)
insilmaril@721
   445
			{
insilmaril@721
   446
				mapDefault=false;
insilmaril@721
   447
				mapChanged=false;
insilmaril@721
   448
				mapUnsaved=false;
insilmaril@721
   449
				autosaveTimer->stop();
insilmaril@721
   450
			}
insilmaril@721
   451
insilmaril@721
   452
			// Reset timestamp to check for later updates of file
insilmaril@721
   453
			fileChangedTime=QFileInfo (destPath).lastModified();
insilmaril@721
   454
		} else 
insilmaril@721
   455
		{
insilmaril@721
   456
			QMessageBox::critical( 0, tr( "Critical Parse Error" ),
insilmaril@721
   457
					   tr( handler->errorProtocol() ) );
insilmaril@721
   458
			// returnCode=1;	
insilmaril@721
   459
			// Still return "success": the map maybe at least
insilmaril@721
   460
			// partially read by the parser
insilmaril@721
   461
		}	
insilmaril@721
   462
	}	
insilmaril@721
   463
insilmaril@721
   464
	// Delete tmpZipDir
insilmaril@721
   465
	removeDir (QDir(tmpZipDir));
insilmaril@721
   466
insilmaril@721
   467
	updateActions();
insilmaril@721
   468
insilmaril@721
   469
	return err;
insilmaril@721
   470
}
insilmaril@721
   471
insilmaril@721
   472
ErrorCode VymModel::save (const SaveMode &savemode)
insilmaril@721
   473
{
insilmaril@721
   474
	QString tmpZipDir;
insilmaril@721
   475
	QString mapFileName;
insilmaril@721
   476
	QString safeFilePath;
insilmaril@721
   477
insilmaril@721
   478
	ErrorCode err=success;
insilmaril@721
   479
insilmaril@721
   480
	if (zipped)
insilmaril@721
   481
		// save as .xml
insilmaril@721
   482
		mapFileName=mapName+".xml";
insilmaril@721
   483
	else
insilmaril@721
   484
		// use name given by user, even if he chooses .doc
insilmaril@721
   485
		mapFileName=fileName;
insilmaril@721
   486
insilmaril@721
   487
	// Look, if we should zip the data:
insilmaril@721
   488
	if (!zipped)
insilmaril@721
   489
	{
insilmaril@721
   490
		QMessageBox mb( vymName,
insilmaril@721
   491
			tr("The map %1\ndid not use the compressed "
insilmaril@721
   492
			"vym file format.\nWriting it uncompressed will also write images \n"
insilmaril@721
   493
			"and flags and thus may overwrite files in the "
insilmaril@721
   494
			"given directory\n\nDo you want to write the map").arg(filePath),
insilmaril@721
   495
			QMessageBox::Warning,
insilmaril@721
   496
			QMessageBox::Yes | QMessageBox::Default,
insilmaril@721
   497
			QMessageBox::No ,
insilmaril@721
   498
			QMessageBox::Cancel | QMessageBox::Escape);
insilmaril@721
   499
		mb.setButtonText( QMessageBox::Yes, tr("compressed (vym default)") );
insilmaril@721
   500
		mb.setButtonText( QMessageBox::No, tr("uncompressed") );
insilmaril@721
   501
		mb.setButtonText( QMessageBox::Cancel, tr("Cancel"));
insilmaril@721
   502
		switch( mb.exec() ) 
insilmaril@721
   503
		{
insilmaril@721
   504
			case QMessageBox::Yes:
insilmaril@721
   505
				// save compressed (default file format)
insilmaril@721
   506
				zipped=true;
insilmaril@721
   507
				break;
insilmaril@721
   508
			case QMessageBox::No:
insilmaril@721
   509
				// save uncompressed
insilmaril@721
   510
				zipped=false;
insilmaril@721
   511
				break;
insilmaril@721
   512
			case QMessageBox::Cancel:
insilmaril@721
   513
				// do nothing
insilmaril@721
   514
				return aborted;
insilmaril@721
   515
				break;
insilmaril@721
   516
		}
insilmaril@721
   517
	}
insilmaril@721
   518
insilmaril@721
   519
	// First backup existing file, we 
insilmaril@721
   520
	// don't want to add to old zip archives
insilmaril@721
   521
	QFile f(destPath);
insilmaril@721
   522
	if (f.exists())
insilmaril@721
   523
	{
insilmaril@721
   524
		if ( settings.value ("/mapeditor/writeBackupFile").toBool())
insilmaril@721
   525
		{
insilmaril@721
   526
			QString backupFileName(destPath + "~");
insilmaril@721
   527
			QFile backupFile(backupFileName);
insilmaril@721
   528
			if (backupFile.exists() && !backupFile.remove())
insilmaril@721
   529
			{
insilmaril@721
   530
				QMessageBox::warning(0, tr("Save Error"),
insilmaril@721
   531
									 tr("%1\ncould not be removed before saving").arg(backupFileName));
insilmaril@721
   532
			}
insilmaril@721
   533
			else if (!f.rename(backupFileName))
insilmaril@721
   534
			{
insilmaril@721
   535
				QMessageBox::warning(0, tr("Save Error"),
insilmaril@721
   536
									 tr("%1\ncould not be renamed before saving").arg(destPath));
insilmaril@721
   537
			}
insilmaril@721
   538
		}
insilmaril@721
   539
	}
insilmaril@721
   540
insilmaril@721
   541
	if (zipped)
insilmaril@721
   542
	{
insilmaril@721
   543
		// Create temporary directory for packing
insilmaril@721
   544
		bool ok;
insilmaril@721
   545
		tmpZipDir=makeTmpDir (ok,"vym-zip");
insilmaril@721
   546
		if (!ok)
insilmaril@721
   547
		{
insilmaril@721
   548
			QMessageBox::critical( 0, tr( "Critical Load Error" ),
insilmaril@721
   549
			   tr("Couldn't create temporary directory before save\n"));
insilmaril@721
   550
			return aborted; 
insilmaril@721
   551
		}
insilmaril@721
   552
insilmaril@721
   553
		safeFilePath=filePath;
insilmaril@721
   554
		setFilePath (tmpZipDir+"/"+ mapName+ ".xml", safeFilePath);
insilmaril@721
   555
	} // zipped
insilmaril@721
   556
insilmaril@721
   557
	// Create mapName and fileDir
insilmaril@721
   558
	makeSubDirs (fileDir);
insilmaril@721
   559
insilmaril@721
   560
	QString saveFile;
insilmaril@721
   561
	if (savemode==CompleteMap || selection.isEmpty())
insilmaril@721
   562
	{
insilmaril@721
   563
		// Save complete map
insilmaril@721
   564
		saveFile=saveToDir (fileDir,mapName+"-",true,QPointF(),NULL);
insilmaril@721
   565
		mapChanged=false;
insilmaril@721
   566
		mapUnsaved=false;
insilmaril@721
   567
		autosaveTimer->stop();
insilmaril@721
   568
	}
insilmaril@721
   569
	else	
insilmaril@721
   570
	{
insilmaril@721
   571
		// Save part of map
insilmaril@721
   572
		if (selection.type()==Selection::FloatImage)
insilmaril@721
   573
			saveFloatImage();
insilmaril@721
   574
		else	
insilmaril@721
   575
			saveFile=saveToDir (fileDir,mapName+"-",true,QPointF(),selection.getBranch());	
insilmaril@721
   576
		// TODO take care of multiselections
insilmaril@721
   577
	}	
insilmaril@721
   578
insilmaril@721
   579
	if (!saveStringToDisk(fileDir+mapFileName,saveFile))
insilmaril@721
   580
	{
insilmaril@721
   581
		err=aborted;
insilmaril@721
   582
		qWarning ("ME::saveStringToDisk failed!");
insilmaril@721
   583
	}
insilmaril@721
   584
insilmaril@721
   585
	if (zipped)
insilmaril@721
   586
	{
insilmaril@721
   587
		// zip
insilmaril@721
   588
		if (err==success) err=zipDir (tmpZipDir,destPath);
insilmaril@721
   589
insilmaril@721
   590
		// Delete tmpDir
insilmaril@721
   591
		removeDir (QDir(tmpZipDir));
insilmaril@721
   592
insilmaril@721
   593
		// Restore original filepath outside of tmp zip dir
insilmaril@721
   594
		setFilePath (safeFilePath);
insilmaril@721
   595
	}
insilmaril@721
   596
insilmaril@721
   597
	updateActions();
insilmaril@721
   598
	fileChangedTime=QFileInfo (destPath).lastModified();
insilmaril@721
   599
	return err;
insilmaril@721
   600
}
insilmaril@721
   601
insilmaril@721
   602
void VymModel::addMapReplaceInt(const QString &undoSel, const QString &path)
insilmaril@721
   603
{
insilmaril@721
   604
	QString pathDir=path.left(path.findRev("/"));
insilmaril@721
   605
	QDir d(pathDir);
insilmaril@721
   606
	QFile file (path);
insilmaril@721
   607
insilmaril@721
   608
	if (d.exists() )
insilmaril@721
   609
	{
insilmaril@721
   610
		// We need to parse saved XML data
insilmaril@721
   611
		parseVYMHandler handler;
insilmaril@721
   612
		QXmlInputSource source( file);
insilmaril@721
   613
		QXmlSimpleReader reader;
insilmaril@721
   614
		reader.setContentHandler( &handler );
insilmaril@721
   615
		reader.setErrorHandler( &handler );
insilmaril@721
   616
		handler.setModel ( this);
insilmaril@721
   617
		handler.setTmpDir ( pathDir );	// needed to load files with rel. path
insilmaril@721
   618
		if (undoSel.isEmpty())
insilmaril@721
   619
		{
insilmaril@721
   620
			unselect();
insilmaril@721
   621
			clear();
insilmaril@721
   622
			handler.setLoadMode (NewMap);
insilmaril@721
   623
		} else	
insilmaril@721
   624
		{
insilmaril@721
   625
			select (undoSel);
insilmaril@721
   626
			handler.setLoadMode (ImportReplace);
insilmaril@721
   627
		}	
insilmaril@721
   628
		blockReposition=true;
insilmaril@721
   629
		bool ok = reader.parse( source );
insilmaril@721
   630
		blockReposition=false;
insilmaril@721
   631
		if (! ok ) 
insilmaril@721
   632
		{	
insilmaril@721
   633
			// This should never ever happen
insilmaril@721
   634
			QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path),
insilmaril@721
   635
								    handler.errorProtocol());
insilmaril@721
   636
		}
insilmaril@721
   637
	} else	
insilmaril@721
   638
		QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
insilmaril@721
   639
}
insilmaril@721
   640
insilmaril@721
   641
void VymModel::addMapInsertInt (const QString &path, int pos)
insilmaril@721
   642
{
insilmaril@721
   643
	BranchObj *sel=selection.getBranch();
insilmaril@721
   644
	if (sel)
insilmaril@721
   645
	{
insilmaril@721
   646
		QString pathDir=path.left(path.findRev("/"));
insilmaril@721
   647
		QDir d(pathDir);
insilmaril@721
   648
		QFile file (path);
insilmaril@721
   649
insilmaril@721
   650
		if (d.exists() )
insilmaril@721
   651
		{
insilmaril@721
   652
			// We need to parse saved XML data
insilmaril@721
   653
			parseVYMHandler handler;
insilmaril@721
   654
			QXmlInputSource source( file);
insilmaril@721
   655
			QXmlSimpleReader reader;
insilmaril@721
   656
			reader.setContentHandler( &handler );
insilmaril@721
   657
			reader.setErrorHandler( &handler );
insilmaril@721
   658
			handler.setModel (this);
insilmaril@721
   659
			handler.setTmpDir ( pathDir );	// needed to load files with rel. path
insilmaril@721
   660
			handler.setLoadMode (ImportAdd);
insilmaril@721
   661
			blockReposition=true;
insilmaril@721
   662
			bool ok = reader.parse( source );
insilmaril@721
   663
			blockReposition=false;
insilmaril@721
   664
			if (! ok ) 
insilmaril@721
   665
			{	
insilmaril@721
   666
				// This should never ever happen
insilmaril@721
   667
				QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path),
insilmaril@721
   668
										handler.errorProtocol());
insilmaril@721
   669
			}
insilmaril@721
   670
			if (sel->getDepth()>0)
insilmaril@721
   671
				sel->getLastBranch()->linkTo (sel,pos);
insilmaril@721
   672
		} else	
insilmaril@721
   673
			QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
insilmaril@721
   674
	}		
insilmaril@721
   675
}
insilmaril@721
   676
insilmaril@721
   677
FloatImageObj* VymModel::loadFloatImageInt (QString fn)
insilmaril@721
   678
{
insilmaril@721
   679
	BranchObj *bo=selection.getBranch();
insilmaril@721
   680
	if (bo)
insilmaril@721
   681
	{
insilmaril@721
   682
		FloatImageObj *fio;
insilmaril@721
   683
		bo->addFloatImage();
insilmaril@721
   684
		fio=bo->getLastFloatImage();
insilmaril@721
   685
		fio->load(fn);
insilmaril@721
   686
		reposition();
insilmaril@721
   687
		// FIXME needed? scene()->update();
insilmaril@721
   688
		return fio;
insilmaril@721
   689
	}
insilmaril@721
   690
	return NULL;
insilmaril@721
   691
}	
insilmaril@721
   692
insilmaril@721
   693
void VymModel::loadFloatImage ()
insilmaril@721
   694
{
insilmaril@721
   695
	BranchObj *bo=selection.getBranch();
insilmaril@721
   696
	if (bo)
insilmaril@721
   697
	{
insilmaril@721
   698
insilmaril@721
   699
		Q3FileDialog *fd=new Q3FileDialog( NULL);
insilmaril@721
   700
		fd->setMode (Q3FileDialog::ExistingFiles);
insilmaril@721
   701
		fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
insilmaril@721
   702
		ImagePreview *p =new ImagePreview (fd);
insilmaril@721
   703
		fd->setContentsPreviewEnabled( TRUE );
insilmaril@721
   704
		fd->setContentsPreview( p, p );
insilmaril@721
   705
		fd->setPreviewMode( Q3FileDialog::Contents );
insilmaril@721
   706
		fd->setCaption(vymName+" - " +tr("Load image"));
insilmaril@721
   707
		fd->setDir (lastImageDir);
insilmaril@721
   708
		fd->show();
insilmaril@721
   709
insilmaril@721
   710
		if ( fd->exec() == QDialog::Accepted )
insilmaril@721
   711
		{
insilmaril@721
   712
			// TODO loadFIO in QT4 use:	lastImageDir=fd->directory();
insilmaril@721
   713
			lastImageDir=QDir (fd->dirPath());
insilmaril@721
   714
			QString s;
insilmaril@721
   715
			FloatImageObj *fio;
insilmaril@721
   716
			for (int j=0; j<fd->selectedFiles().count(); j++)
insilmaril@721
   717
			{
insilmaril@721
   718
				s=fd->selectedFiles().at(j);
insilmaril@721
   719
				fio=loadFloatImageInt (s);
insilmaril@721
   720
				if (fio)
insilmaril@721
   721
					saveState(
insilmaril@721
   722
						(LinkableMapObj*)fio,
insilmaril@721
   723
						"delete ()",
insilmaril@721
   724
						bo, 
insilmaril@721
   725
						QString ("loadImage (%1)").arg(s ),
insilmaril@721
   726
						QString("Add image %1 to %2").arg(s).arg(getObjectName(bo))
insilmaril@721
   727
					);
insilmaril@721
   728
				else
insilmaril@721
   729
					// TODO loadFIO error handling
insilmaril@721
   730
					qWarning ("Failed to load "+s);
insilmaril@721
   731
			}
insilmaril@721
   732
		}
insilmaril@721
   733
		delete (p);
insilmaril@721
   734
		delete (fd);
insilmaril@721
   735
	}
insilmaril@721
   736
}
insilmaril@721
   737
insilmaril@721
   738
void VymModel::saveFloatImageInt  (FloatImageObj *fio, const QString &type, const QString &fn)
insilmaril@721
   739
{
insilmaril@721
   740
	fio->save (fn,type);
insilmaril@721
   741
}
insilmaril@721
   742
insilmaril@721
   743
void VymModel::saveFloatImage ()
insilmaril@721
   744
{
insilmaril@721
   745
	FloatImageObj *fio=selection.getFloatImage();
insilmaril@721
   746
	if (fio)
insilmaril@721
   747
	{
insilmaril@721
   748
		QFileDialog *fd=new QFileDialog( NULL);
insilmaril@721
   749
		fd->setFilters (imageIO.getFilters());
insilmaril@721
   750
		fd->setCaption(vymName+" - " +tr("Save image"));
insilmaril@721
   751
		fd->setFileMode( QFileDialog::AnyFile );
insilmaril@721
   752
		fd->setDirectory (lastImageDir);
insilmaril@721
   753
//		fd->setSelection (fio->getOriginalFilename());
insilmaril@721
   754
		fd->show();
insilmaril@721
   755
insilmaril@721
   756
		QString fn;
insilmaril@721
   757
		if ( fd->exec() == QDialog::Accepted && fd->selectedFiles().count()==1)
insilmaril@721
   758
		{
insilmaril@721
   759
			fn=fd->selectedFiles().at(0);
insilmaril@721
   760
			if (QFile (fn).exists() )
insilmaril@721
   761
			{
insilmaril@721
   762
				QMessageBox mb( vymName,
insilmaril@721
   763
					tr("The file %1 exists already.\n"
insilmaril@721
   764
					"Do you want to overwrite it?").arg(fn),
insilmaril@721
   765
				QMessageBox::Warning,
insilmaril@721
   766
				QMessageBox::Yes | QMessageBox::Default,
insilmaril@721
   767
				QMessageBox::Cancel | QMessageBox::Escape,
insilmaril@721
   768
				QMessageBox::NoButton );
insilmaril@721
   769
insilmaril@721
   770
				mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
insilmaril@721
   771
				mb.setButtonText( QMessageBox::No, tr("Cancel"));
insilmaril@721
   772
				switch( mb.exec() ) 
insilmaril@721
   773
				{
insilmaril@721
   774
					case QMessageBox::Yes:
insilmaril@721
   775
						// save 
insilmaril@721
   776
						break;
insilmaril@721
   777
					case QMessageBox::Cancel:
insilmaril@721
   778
						// do nothing
insilmaril@721
   779
						delete (fd);
insilmaril@721
   780
						return;
insilmaril@721
   781
						break;
insilmaril@721
   782
				}
insilmaril@721
   783
			}
insilmaril@721
   784
			saveFloatImageInt (fio,fd->selectedFilter(),fn );
insilmaril@721
   785
		}
insilmaril@721
   786
		delete (fd);
insilmaril@721
   787
	}
insilmaril@721
   788
}
insilmaril@721
   789
insilmaril@721
   790
insilmaril@721
   791
void VymModel::importDirInt(BranchObj *dst, QDir d)
insilmaril@721
   792
{
insilmaril@721
   793
	BranchObj *bo=selection.getBranch();
insilmaril@721
   794
	if (bo)
insilmaril@721
   795
	{
insilmaril@721
   796
		// Traverse directories
insilmaril@721
   797
		d.setFilter( QDir::Dirs| QDir::Hidden | QDir::NoSymLinks );
insilmaril@721
   798
		QFileInfoList list = d.entryInfoList();
insilmaril@721
   799
		QFileInfo fi;
insilmaril@721
   800
insilmaril@721
   801
		for (int i = 0; i < list.size(); ++i) 
insilmaril@721
   802
		{
insilmaril@721
   803
			fi=list.at(i);
insilmaril@721
   804
			if (fi.fileName() != "." && fi.fileName() != ".." )
insilmaril@721
   805
			{
insilmaril@721
   806
				dst->addBranch();
insilmaril@721
   807
				bo=dst->getLastBranch();
insilmaril@721
   808
				bo->setHeading (fi.fileName() );
insilmaril@721
   809
				bo->setColor (QColor("blue"));
insilmaril@721
   810
				bo->toggleScroll();
insilmaril@721
   811
				if ( !d.cd(fi.fileName()) ) 
insilmaril@721
   812
					QMessageBox::critical (0,tr("Critical Import Error"),tr("Cannot find the directory %1").arg(fi.fileName()));
insilmaril@721
   813
				else 
insilmaril@721
   814
				{
insilmaril@721
   815
					// Recursively add subdirs
insilmaril@721
   816
					importDirInt (bo,d);
insilmaril@721
   817
					d.cdUp();
insilmaril@721
   818
				}
insilmaril@721
   819
			}	
insilmaril@721
   820
		}		
insilmaril@721
   821
		// Traverse files
insilmaril@721
   822
		d.setFilter( QDir::Files| QDir::Hidden | QDir::NoSymLinks );
insilmaril@721
   823
		list = d.entryInfoList();
insilmaril@721
   824
insilmaril@721
   825
		for (int i = 0; i < list.size(); ++i) 
insilmaril@721
   826
		{
insilmaril@721
   827
			fi=list.at(i);
insilmaril@721
   828
			dst->addBranch();
insilmaril@721
   829
			bo=dst->getLastBranch();
insilmaril@721
   830
			bo->setHeading (fi.fileName() );
insilmaril@721
   831
			bo->setColor (QColor("black"));
insilmaril@721
   832
			if (fi.fileName().right(4) == ".vym" )
insilmaril@721
   833
				bo->setVymLink (fi.filePath());
insilmaril@721
   834
		}	
insilmaril@721
   835
	}		
insilmaril@721
   836
}
insilmaril@721
   837
insilmaril@721
   838
void VymModel::importDirInt (const QString &s)
insilmaril@721
   839
{
insilmaril@721
   840
	BranchObj *bo=selection.getBranch();
insilmaril@721
   841
	if (bo)
insilmaril@721
   842
	{
insilmaril@721
   843
		saveStateChangingPart (bo,bo,QString ("importDir (\"%1\")").arg(s),QString("Import directory structure from %1").arg(s));
insilmaril@721
   844
insilmaril@721
   845
		QDir d(s);
insilmaril@721
   846
		importDirInt (bo,d);
insilmaril@721
   847
	}
insilmaril@721
   848
}	
insilmaril@721
   849
insilmaril@721
   850
void VymModel::importDir()
insilmaril@721
   851
{
insilmaril@721
   852
	BranchObj *bo=selection.getBranch();
insilmaril@721
   853
	if (bo)
insilmaril@721
   854
	{
insilmaril@721
   855
		QStringList filters;
insilmaril@721
   856
		filters <<"VYM map (*.vym)";
insilmaril@721
   857
		QFileDialog *fd=new QFileDialog( NULL,vymName+ " - " +tr("Choose directory structure to import"));
insilmaril@721
   858
		fd->setMode (QFileDialog::DirectoryOnly);
insilmaril@721
   859
		fd->setFilters (filters);
insilmaril@721
   860
		fd->setCaption(vymName+" - " +tr("Choose directory structure to import"));
insilmaril@721
   861
		fd->show();
insilmaril@721
   862
insilmaril@721
   863
		QString fn;
insilmaril@721
   864
		if ( fd->exec() == QDialog::Accepted )
insilmaril@721
   865
		{
insilmaril@721
   866
			importDirInt (fd->selectedFile() );
insilmaril@721
   867
			reposition();
insilmaril@721
   868
			//FIXME needed? scene()->update();
insilmaril@721
   869
		}
insilmaril@721
   870
	}	
insilmaril@721
   871
}
insilmaril@721
   872
insilmaril@721
   873
insilmaril@721
   874
void VymModel::autosave()
insilmaril@721
   875
{
insilmaril@721
   876
	QDateTime now=QDateTime().currentDateTime();
insilmaril@721
   877
insilmaril@721
   878
	// Disable autosave, while we have gone back in history
insilmaril@721
   879
	int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
insilmaril@721
   880
	if (redosAvail>0) return;
insilmaril@721
   881
insilmaril@721
   882
	// Also disable autosave for new map without filename
insilmaril@721
   883
	if (filePath.isEmpty()) return;
insilmaril@721
   884
insilmaril@721
   885
insilmaril@721
   886
	if (mapUnsaved &&mapChanged && settings.value ("/autosave/use",true).toBool() )
insilmaril@721
   887
	{
insilmaril@721
   888
		if (QFileInfo(filePath).lastModified()<=fileChangedTime) 
insilmaril@721
   889
			mainWindow->fileSave (this);
insilmaril@721
   890
		else
insilmaril@721
   891
			if (debug)
insilmaril@721
   892
				cout <<"  ME::autosave  rejected, file on disk is newer than last save.\n"; 
insilmaril@721
   893
insilmaril@721
   894
	}	
insilmaril@721
   895
}
insilmaril@721
   896
insilmaril@721
   897
void VymModel::fileChanged()
insilmaril@721
   898
{
insilmaril@721
   899
	// Check if file on disk has changed meanwhile
insilmaril@721
   900
	if (!filePath.isEmpty())
insilmaril@721
   901
	{
insilmaril@721
   902
		QDateTime tmod=QFileInfo (filePath).lastModified();
insilmaril@721
   903
		if (tmod>fileChangedTime)
insilmaril@721
   904
		{
insilmaril@721
   905
			// FIXME switch to current mapeditor and finish lineedits...
insilmaril@721
   906
			QMessageBox mb( vymName,
insilmaril@721
   907
				tr("The file of the map  on disk has changed:\n\n"  
insilmaril@721
   908
				   "   %1\n\nDo you want to reload that map with the new file?").arg(filePath),
insilmaril@721
   909
				QMessageBox::Question,
insilmaril@721
   910
				QMessageBox::Yes ,
insilmaril@721
   911
				QMessageBox::Cancel | QMessageBox::Default,
insilmaril@721
   912
				QMessageBox::NoButton );
insilmaril@721
   913
insilmaril@721
   914
			mb.setButtonText( QMessageBox::Yes, tr("Reload"));
insilmaril@721
   915
			mb.setButtonText( QMessageBox::No, tr("Ignore"));
insilmaril@721
   916
			switch( mb.exec() ) 
insilmaril@721
   917
			{
insilmaril@721
   918
				case QMessageBox::Yes:
insilmaril@721
   919
					// Reload map
insilmaril@721
   920
					load (filePath,NewMap,fileType);
insilmaril@721
   921
		        case QMessageBox::Cancel:
insilmaril@721
   922
					fileChangedTime=tmod; // allow autosave to overwrite newer file!
insilmaril@721
   923
			}
insilmaril@721
   924
		}
insilmaril@721
   925
	}	
insilmaril@721
   926
}
insilmaril@721
   927
insilmaril@721
   928
bool VymModel::isDefault()
insilmaril@721
   929
{
insilmaril@721
   930
    return mapDefault;
insilmaril@721
   931
}
insilmaril@721
   932
insilmaril@721
   933
void VymModel::makeDefault()
insilmaril@721
   934
{
insilmaril@721
   935
	mapChanged=false;
insilmaril@721
   936
	mapDefault=true;
insilmaril@721
   937
}
insilmaril@721
   938
insilmaril@721
   939
bool VymModel::hasChanged()
insilmaril@721
   940
{
insilmaril@721
   941
    return mapChanged;
insilmaril@721
   942
}
insilmaril@721
   943
insilmaril@721
   944
void VymModel::setChanged()
insilmaril@721
   945
{
insilmaril@721
   946
	if (!mapChanged)
insilmaril@721
   947
		autosaveTimer->start(settings.value("/autosave/ms/",300000).toInt());
insilmaril@721
   948
	mapChanged=true;
insilmaril@721
   949
	mapDefault=false;
insilmaril@721
   950
	mapUnsaved=true;
insilmaril@721
   951
	findReset();
insilmaril@721
   952
}
insilmaril@721
   953
insilmaril@721
   954
insilmaril@721
   955
QString VymModel::getObjectName (const LinkableMapObj *lmo)
insilmaril@721
   956
{
insilmaril@721
   957
	QString s;
insilmaril@721
   958
	if (!lmo) return QString("Error: NULL has no name!");
insilmaril@721
   959
insilmaril@721
   960
	if ((typeid(*lmo) == typeid(BranchObj) ||
insilmaril@721
   961
				      typeid(*lmo) == typeid(MapCenterObj))) 
insilmaril@721
   962
	{
insilmaril@721
   963
		
insilmaril@721
   964
		s=(((BranchObj*)lmo)->getHeading());
insilmaril@721
   965
		if (s=="") s="unnamed";
insilmaril@721
   966
		return QString("branch (%1)").arg(s);
insilmaril@721
   967
	}	
insilmaril@721
   968
	if ((typeid(*lmo) == typeid(FloatImageObj) ))
insilmaril@721
   969
		return QString ("floatimage [%1]").arg(((FloatImageObj*)lmo)->getOriginalFilename());
insilmaril@721
   970
	return QString("Unknown type has no name!");
insilmaril@721
   971
}
insilmaril@721
   972
insilmaril@721
   973
void VymModel::redo()
insilmaril@721
   974
{
insilmaril@721
   975
	// Can we undo at all?
insilmaril@721
   976
	if (redosAvail<1) return;
insilmaril@721
   977
insilmaril@721
   978
	bool blockSaveStateOrg=blockSaveState;
insilmaril@721
   979
	blockSaveState=true;
insilmaril@721
   980
	
insilmaril@721
   981
	redosAvail--;
insilmaril@721
   982
insilmaril@721
   983
	if (undosAvail<stepsTotal) undosAvail++;
insilmaril@721
   984
	curStep++;
insilmaril@721
   985
	if (curStep>stepsTotal) curStep=1;
insilmaril@721
   986
	QString undoCommand=  undoSet.readEntry (QString("/history/step-%1/undoCommand").arg(curStep));
insilmaril@721
   987
	QString undoSelection=undoSet.readEntry (QString("/history/step-%1/undoSelection").arg(curStep));
insilmaril@721
   988
	QString redoCommand=  undoSet.readEntry (QString("/history/step-%1/redoCommand").arg(curStep));
insilmaril@721
   989
	QString redoSelection=undoSet.readEntry (QString("/history/step-%1/redoSelection").arg(curStep));
insilmaril@721
   990
	QString comment=undoSet.readEntry (QString("/history/step-%1/comment").arg(curStep));
insilmaril@721
   991
	QString version=undoSet.readEntry ("/history/version");
insilmaril@721
   992
insilmaril@721
   993
	/* TODO Maybe check for version, if we save the history
insilmaril@721
   994
	if (!checkVersion(version))
insilmaril@721
   995
		QMessageBox::warning(0,tr("Warning"),
insilmaril@721
   996
			tr("Version %1 of saved undo/redo data\ndoes not match current vym version %2.").arg(version).arg(vymVersion));
insilmaril@721
   997
	*/ 
insilmaril@721
   998
insilmaril@721
   999
	// Find out current undo directory
insilmaril@721
  1000
	QString bakMapDir(QString(tmpMapDir+"/undo-%1").arg(curStep));
insilmaril@721
  1001
insilmaril@721
  1002
	if (debug)
insilmaril@721
  1003
	{
insilmaril@721
  1004
		cout << "VymModel::redo() begin\n";
insilmaril@721
  1005
		cout << "    undosAvail="<<undosAvail<<endl;
insilmaril@721
  1006
		cout << "    redosAvail="<<redosAvail<<endl;
insilmaril@721
  1007
		cout << "       curStep="<<curStep<<endl;
insilmaril@721
  1008
		cout << "    ---------------------------"<<endl;
insilmaril@721
  1009
		cout << "    comment="<<comment.toStdString()<<endl;
insilmaril@721
  1010
		cout << "    undoCom="<<undoCommand.toStdString()<<endl;
insilmaril@721
  1011
		cout << "    undoSel="<<undoSelection.toStdString()<<endl;
insilmaril@721
  1012
		cout << "    redoCom="<<redoCommand.toStdString()<<endl;
insilmaril@721
  1013
		cout << "    redoSel="<<redoSelection.toStdString()<<endl;
insilmaril@721
  1014
		cout << "    ---------------------------"<<endl<<endl;
insilmaril@721
  1015
	}
insilmaril@721
  1016
insilmaril@721
  1017
	// select  object before redo
insilmaril@721
  1018
	if (!redoSelection.isEmpty())
insilmaril@721
  1019
		select (redoSelection);
insilmaril@721
  1020
insilmaril@721
  1021
insilmaril@721
  1022
	parseAtom (redoCommand);
insilmaril@721
  1023
	reposition();
insilmaril@721
  1024
insilmaril@721
  1025
	blockSaveState=blockSaveStateOrg;
insilmaril@721
  1026
insilmaril@721
  1027
	undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
insilmaril@721
  1028
	undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
insilmaril@721
  1029
	undoSet.setEntry ("/history/curStep",QString::number(curStep));
insilmaril@721
  1030
	undoSet.writeSettings(histPath);
insilmaril@721
  1031
insilmaril@721
  1032
	mainWindow->updateHistory (undoSet);
insilmaril@721
  1033
	updateActions();
insilmaril@721
  1034
insilmaril@721
  1035
	/* TODO remove testing
insilmaril@721
  1036
	cout << "ME::redo() end\n";
insilmaril@721
  1037
	cout << "    undosAvail="<<undosAvail<<endl;
insilmaril@721
  1038
	cout << "    redosAvail="<<redosAvail<<endl;
insilmaril@721
  1039
	cout << "       curStep="<<curStep<<endl;
insilmaril@721
  1040
	cout << "    ---------------------------"<<endl<<endl;
insilmaril@721
  1041
	*/
insilmaril@721
  1042
insilmaril@721
  1043
insilmaril@721
  1044
}
insilmaril@721
  1045
insilmaril@721
  1046
bool VymModel::isRedoAvailable()
insilmaril@721
  1047
{
insilmaril@721
  1048
	if (undoSet.readNumEntry("/history/redosAvail",0)>0)
insilmaril@721
  1049
		return true;
insilmaril@721
  1050
	else	
insilmaril@721
  1051
		return false;
insilmaril@721
  1052
}
insilmaril@721
  1053
insilmaril@721
  1054
void VymModel::undo()
insilmaril@721
  1055
{
insilmaril@721
  1056
	// Can we undo at all?
insilmaril@721
  1057
	if (undosAvail<1) return;
insilmaril@721
  1058
insilmaril@721
  1059
	mainWindow->statusMessage (tr("Autosave disabled during undo."));
insilmaril@721
  1060
insilmaril@721
  1061
	bool blockSaveStateOrg=blockSaveState;
insilmaril@721
  1062
	blockSaveState=true;
insilmaril@721
  1063
	
insilmaril@721
  1064
	QString undoCommand=  undoSet.readEntry (QString("/history/step-%1/undoCommand").arg(curStep));
insilmaril@721
  1065
	QString undoSelection=undoSet.readEntry (QString("/history/step-%1/undoSelection").arg(curStep));
insilmaril@721
  1066
	QString redoCommand=  undoSet.readEntry (QString("/history/step-%1/redoCommand").arg(curStep));
insilmaril@721
  1067
	QString redoSelection=undoSet.readEntry (QString("/history/step-%1/redoSelection").arg(curStep));
insilmaril@721
  1068
	QString comment=undoSet.readEntry (QString("/history/step-%1/comment").arg(curStep));
insilmaril@721
  1069
	QString version=undoSet.readEntry ("/history/version");
insilmaril@721
  1070
insilmaril@721
  1071
	/* TODO Maybe check for version, if we save the history
insilmaril@721
  1072
	if (!checkVersion(version))
insilmaril@721
  1073
		QMessageBox::warning(0,tr("Warning"),
insilmaril@721
  1074
			tr("Version %1 of saved undo/redo data\ndoes not match current vym version %2.").arg(version).arg(vymVersion));
insilmaril@721
  1075
	*/
insilmaril@721
  1076
insilmaril@721
  1077
	// Find out current undo directory
insilmaril@721
  1078
	QString bakMapDir(QString(tmpMapDir+"/undo-%1").arg(curStep));
insilmaril@721
  1079
insilmaril@721
  1080
	// select  object before undo
insilmaril@721
  1081
	if (!undoSelection.isEmpty())
insilmaril@721
  1082
		select (undoSelection);
insilmaril@721
  1083
insilmaril@721
  1084
	if (debug)
insilmaril@721
  1085
	{
insilmaril@721
  1086
		cout << "VymModel::undo() begin\n";
insilmaril@721
  1087
		cout << "    undosAvail="<<undosAvail<<endl;
insilmaril@721
  1088
		cout << "    redosAvail="<<redosAvail<<endl;
insilmaril@721
  1089
		cout << "       curStep="<<curStep<<endl;
insilmaril@721
  1090
		cout << "    ---------------------------"<<endl;
insilmaril@721
  1091
		cout << "    comment="<<comment.toStdString()<<endl;
insilmaril@721
  1092
		cout << "    undoCom="<<undoCommand.toStdString()<<endl;
insilmaril@721
  1093
		cout << "    undoSel="<<undoSelection.toStdString()<<endl;
insilmaril@721
  1094
		cout << "    redoCom="<<redoCommand.toStdString()<<endl;
insilmaril@721
  1095
		cout << "    redoSel="<<redoSelection.toStdString()<<endl;
insilmaril@721
  1096
		cout << "    ---------------------------"<<endl<<endl;
insilmaril@721
  1097
	}	
insilmaril@721
  1098
	parseAtom (undoCommand);
insilmaril@721
  1099
	reposition();
insilmaril@721
  1100
insilmaril@721
  1101
	undosAvail--;
insilmaril@721
  1102
	curStep--; 
insilmaril@721
  1103
	if (curStep<1) curStep=stepsTotal;
insilmaril@721
  1104
insilmaril@721
  1105
	redosAvail++;
insilmaril@721
  1106
insilmaril@721
  1107
	blockSaveState=blockSaveStateOrg;
insilmaril@721
  1108
/* TODO remove testing
insilmaril@721
  1109
	cout << "VymModel::undo() end\n";
insilmaril@721
  1110
	cout << "    undosAvail="<<undosAvail<<endl;
insilmaril@721
  1111
	cout << "    redosAvail="<<redosAvail<<endl;
insilmaril@721
  1112
	cout << "       curStep="<<curStep<<endl;
insilmaril@721
  1113
	cout << "    ---------------------------"<<endl<<endl;
insilmaril@721
  1114
*/
insilmaril@721
  1115
insilmaril@721
  1116
	undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
insilmaril@721
  1117
	undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
insilmaril@721
  1118
	undoSet.setEntry ("/history/curStep",QString::number(curStep));
insilmaril@721
  1119
	undoSet.writeSettings(histPath);
insilmaril@721
  1120
insilmaril@721
  1121
	mainWindow->updateHistory (undoSet);
insilmaril@721
  1122
	updateActions();
insilmaril@721
  1123
	selection.update();
insilmaril@721
  1124
	ensureSelectionVisible();
insilmaril@721
  1125
}
insilmaril@721
  1126
insilmaril@721
  1127
bool VymModel::isUndoAvailable()
insilmaril@721
  1128
{
insilmaril@721
  1129
	if (undoSet.readNumEntry("/history/undosAvail",0)>0)
insilmaril@721
  1130
		return true;
insilmaril@721
  1131
	else	
insilmaril@721
  1132
		return false;
insilmaril@721
  1133
}
insilmaril@721
  1134
insilmaril@721
  1135
void VymModel::gotoHistoryStep (int i)
insilmaril@721
  1136
{
insilmaril@721
  1137
	// Restore variables
insilmaril@721
  1138
	int undosAvail=undoSet.readNumEntry (QString("/history/undosAvail"));
insilmaril@721
  1139
	int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
insilmaril@721
  1140
insilmaril@721
  1141
	if (i<0) i=undosAvail+redosAvail;
insilmaril@721
  1142
insilmaril@721
  1143
	// Clicking above current step makes us undo things
insilmaril@721
  1144
	if (i<undosAvail) 
insilmaril@721
  1145
	{	
insilmaril@721
  1146
		for (int j=0; j<undosAvail-i; j++) undo();
insilmaril@721
  1147
		return;
insilmaril@721
  1148
	}	
insilmaril@721
  1149
	// Clicking below current step makes us redo things
insilmaril@721
  1150
	if (i>undosAvail) 
insilmaril@721
  1151
		for (int j=undosAvail; j<i; j++) 
insilmaril@721
  1152
		{
insilmaril@721
  1153
			if (debug) cout << "VymModel::gotoHistoryStep redo "<<j<<"/"<<undosAvail<<" i="<<i<<endl;
insilmaril@721
  1154
			redo();
insilmaril@721
  1155
		}
insilmaril@721
  1156
insilmaril@721
  1157
	// And ignore clicking the current row ;-)	
insilmaril@721
  1158
}
insilmaril@721
  1159
insilmaril@721
  1160
insilmaril@721
  1161
QString VymModel::getHistoryPath()
insilmaril@721
  1162
{
insilmaril@721
  1163
	QString histName(QString("history-%1").arg(curStep));
insilmaril@721
  1164
	return (tmpMapDir+"/"+histName);
insilmaril@721
  1165
}
insilmaril@721
  1166
insilmaril@721
  1167
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
  1168
{
insilmaril@721
  1169
	sendData(redoCom);	//FIXME testing
insilmaril@721
  1170
insilmaril@721
  1171
	// Main saveState
insilmaril@721
  1172
insilmaril@721
  1173
insilmaril@721
  1174
	if (blockSaveState) return;
insilmaril@721
  1175
insilmaril@721
  1176
	if (debug) cout << "ME::saveState() for  "<<qPrintable (mapName)<<endl;
insilmaril@721
  1177
	
insilmaril@721
  1178
	// Find out current undo directory
insilmaril@721
  1179
	if (undosAvail<stepsTotal) undosAvail++;
insilmaril@721
  1180
	curStep++;
insilmaril@721
  1181
	if (curStep>stepsTotal) curStep=1;
insilmaril@721
  1182
	
insilmaril@721
  1183
	QString backupXML="";
insilmaril@721
  1184
	QString histDir=getHistoryPath();
insilmaril@721
  1185
	QString bakMapPath=histDir+"/map.xml";
insilmaril@721
  1186
insilmaril@721
  1187
	// Create histDir if not available
insilmaril@721
  1188
	QDir d(histDir);
insilmaril@721
  1189
	if (!d.exists()) 
insilmaril@721
  1190
		makeSubDirs (histDir);
insilmaril@721
  1191
insilmaril@721
  1192
	// Save depending on how much needs to be saved	
insilmaril@721
  1193
	if (saveSel)
insilmaril@721
  1194
		backupXML=saveToDir (histDir,mapName+"-",false, QPointF (),saveSel);
insilmaril@721
  1195
		
insilmaril@721
  1196
	QString undoCommand="";
insilmaril@721
  1197
	if (savemode==UndoCommand)
insilmaril@721
  1198
	{
insilmaril@721
  1199
		undoCommand=undoCom;
insilmaril@721
  1200
	}	
insilmaril@721
  1201
	else if (savemode==PartOfMap )
insilmaril@721
  1202
	{
insilmaril@721
  1203
		undoCommand=undoCom;
insilmaril@721
  1204
		undoCommand.replace ("PATH",bakMapPath);
insilmaril@721
  1205
	}
insilmaril@721
  1206
insilmaril@721
  1207
	if (!backupXML.isEmpty())
insilmaril@721
  1208
		// Write XML Data to disk
insilmaril@721
  1209
		saveStringToDisk (bakMapPath,backupXML);
insilmaril@721
  1210
insilmaril@721
  1211
	// We would have to save all actions in a tree, to keep track of 
insilmaril@721
  1212
	// possible redos after a action. Possible, but we are too lazy: forget about redos.
insilmaril@721
  1213
	redosAvail=0;
insilmaril@721
  1214
insilmaril@721
  1215
	// Write the current state to disk
insilmaril@721
  1216
	undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
insilmaril@721
  1217
	undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
insilmaril@721
  1218
	undoSet.setEntry ("/history/curStep",QString::number(curStep));
insilmaril@721
  1219
	undoSet.setEntry (QString("/history/step-%1/undoCommand").arg(curStep),undoCommand);
insilmaril@721
  1220
	undoSet.setEntry (QString("/history/step-%1/undoSelection").arg(curStep),undoSelection);
insilmaril@721
  1221
	undoSet.setEntry (QString("/history/step-%1/redoCommand").arg(curStep),redoCom);
insilmaril@721
  1222
	undoSet.setEntry (QString("/history/step-%1/redoSelection").arg(curStep),redoSelection);
insilmaril@721
  1223
	undoSet.setEntry (QString("/history/step-%1/comment").arg(curStep),comment);
insilmaril@721
  1224
	undoSet.setEntry (QString("/history/version"),vymVersion);
insilmaril@721
  1225
	undoSet.writeSettings(histPath);
insilmaril@721
  1226
insilmaril@721
  1227
	if (debug)
insilmaril@721
  1228
	{
insilmaril@721
  1229
		// TODO remove after testing
insilmaril@721
  1230
		//cout << "          into="<< histPath.toStdString()<<endl;
insilmaril@721
  1231
		cout << "    stepsTotal="<<stepsTotal<<
insilmaril@721
  1232
		", undosAvail="<<undosAvail<<
insilmaril@721
  1233
		", redosAvail="<<redosAvail<<
insilmaril@721
  1234
		", curStep="<<curStep<<endl;
insilmaril@721
  1235
		cout << "    ---------------------------"<<endl;
insilmaril@721
  1236
		cout << "    comment="<<comment.toStdString()<<endl;
insilmaril@721
  1237
		cout << "    undoCom="<<undoCommand.toStdString()<<endl;
insilmaril@721
  1238
		cout << "    undoSel="<<undoSelection.toStdString()<<endl;
insilmaril@721
  1239
		cout << "    redoCom="<<redoCom.toStdString()<<endl;
insilmaril@721
  1240
		cout << "    redoSel="<<redoSelection.toStdString()<<endl;
insilmaril@721
  1241
		if (saveSel) cout << "    saveSel="<<qPrintable (getSelectString(saveSel))<<endl;
insilmaril@721
  1242
		cout << "    ---------------------------"<<endl;
insilmaril@721
  1243
	}
insilmaril@721
  1244
insilmaril@721
  1245
	mainWindow->updateHistory (undoSet);
insilmaril@721
  1246
	setChanged();
insilmaril@721
  1247
	updateActions();
insilmaril@721
  1248
}
insilmaril@721
  1249
insilmaril@721
  1250
insilmaril@721
  1251
void VymModel::saveStateChangingPart(LinkableMapObj *undoSel, LinkableMapObj* redoSel, const QString &rc, const QString &comment)
insilmaril@721
  1252
{
insilmaril@721
  1253
	// save the selected part of the map, Undo will replace part of map 
insilmaril@721
  1254
	QString undoSelection="";
insilmaril@721
  1255
	if (undoSel)
insilmaril@721
  1256
		undoSelection=getSelectString(undoSel);
insilmaril@721
  1257
	else
insilmaril@721
  1258
		qWarning ("VymModel::saveStateChangingPart  no undoSel given!");
insilmaril@721
  1259
	QString redoSelection="";
insilmaril@721
  1260
	if (redoSel)
insilmaril@721
  1261
		redoSelection=getSelectString(undoSel);
insilmaril@721
  1262
	else
insilmaril@721
  1263
		qWarning ("VymModel::saveStateChangingPart  no redoSel given!");
insilmaril@721
  1264
		
insilmaril@721
  1265
insilmaril@721
  1266
	saveState (PartOfMap,
insilmaril@721
  1267
		undoSelection, "addMapReplace (\"PATH\")",
insilmaril@721
  1268
		redoSelection, rc, 
insilmaril@721
  1269
		comment, 
insilmaril@721
  1270
		undoSel);
insilmaril@721
  1271
}
insilmaril@721
  1272
insilmaril@721
  1273
void VymModel::saveStateRemovingPart(LinkableMapObj *redoSel, const QString &comment)
insilmaril@721
  1274
{
insilmaril@721
  1275
	if (!redoSel)
insilmaril@721
  1276
	{
insilmaril@721
  1277
		qWarning ("VymModel::saveStateRemovingPart  no redoSel given!");
insilmaril@721
  1278
		return;
insilmaril@721
  1279
	}
insilmaril@721
  1280
	QString undoSelection=getSelectString (redoSel->getParObj());
insilmaril@721
  1281
	QString redoSelection=getSelectString(redoSel);
insilmaril@721
  1282
	if (typeid(*redoSel) == typeid(BranchObj)  ) 
insilmaril@721
  1283
	{
insilmaril@721
  1284
		// save the selected branch of the map, Undo will insert part of map 
insilmaril@721
  1285
		saveState (PartOfMap,
insilmaril@721
  1286
			undoSelection, QString("addMapInsert (\"PATH\",%1)").arg(((BranchObj*)redoSel)->getNum()),
insilmaril@721
  1287
			redoSelection, "delete ()", 
insilmaril@721
  1288
			comment, 
insilmaril@721
  1289
			redoSel);
insilmaril@721
  1290
	}
insilmaril@721
  1291
}
insilmaril@721
  1292
insilmaril@721
  1293
insilmaril@721
  1294
void VymModel::saveState(LinkableMapObj *undoSel, const QString &uc, LinkableMapObj *redoSel, const QString &rc, const QString &comment) 
insilmaril@721
  1295
{
insilmaril@721
  1296
	// "Normal" savestate: save commands, selections and comment
insilmaril@721
  1297
	// so just save commands for undo and redo
insilmaril@721
  1298
	// and use current selection
insilmaril@721
  1299
insilmaril@721
  1300
	QString redoSelection="";
insilmaril@721
  1301
	if (redoSel) redoSelection=getSelectString(redoSel);
insilmaril@721
  1302
	QString undoSelection="";
insilmaril@721
  1303
	if (undoSel) undoSelection=getSelectString(undoSel);
insilmaril@721
  1304
insilmaril@721
  1305
	saveState (UndoCommand,
insilmaril@721
  1306
		undoSelection, uc,
insilmaril@721
  1307
		redoSelection, rc, 
insilmaril@721
  1308
		comment, 
insilmaril@721
  1309
		NULL);
insilmaril@721
  1310
}
insilmaril@721
  1311
insilmaril@721
  1312
void VymModel::saveState(const QString &undoSel, const QString &uc, const QString &redoSel, const QString &rc, const QString &comment) 
insilmaril@721
  1313
{
insilmaril@721
  1314
	// "Normal" savestate: save commands, selections and comment
insilmaril@721
  1315
	// so just save commands for undo and redo
insilmaril@721
  1316
	// and use current selection
insilmaril@721
  1317
	saveState (UndoCommand,
insilmaril@721
  1318
		undoSel, uc,
insilmaril@721
  1319
		redoSel, rc, 
insilmaril@721
  1320
		comment, 
insilmaril@721
  1321
		NULL);
insilmaril@721
  1322
}
insilmaril@721
  1323
insilmaril@721
  1324
void VymModel::saveState(const QString &uc, const QString &rc, const QString &comment) 
insilmaril@721
  1325
{
insilmaril@721
  1326
	// "Normal" savestate applied to model (no selection needed): 
insilmaril@721
  1327
	// save commands  and comment
insilmaril@721
  1328
	saveState (UndoCommand,
insilmaril@721
  1329
		NULL, uc,
insilmaril@721
  1330
		NULL, rc, 
insilmaril@721
  1331
		comment, 
insilmaril@721
  1332
		NULL);
insilmaril@721
  1333
}
insilmaril@721
  1334
insilmaril@721
  1335
insilmaril@650
  1336
QGraphicsScene* VymModel::getScene ()
insilmaril@650
  1337
{
insilmaril@650
  1338
	return mapScene;
insilmaril@650
  1339
}
insilmaril@650
  1340
insilmaril@650
  1341
BranchObj* VymModel::first()
insilmaril@650
  1342
{
insilmaril@650
  1343
	if (mapCenters.count()>0) 
insilmaril@650
  1344
		return mapCenters.first();
insilmaril@650
  1345
	else	
insilmaril@650
  1346
		return NULL;
insilmaril@650
  1347
}
insilmaril@650
  1348
	
insilmaril@650
  1349
BranchObj* VymModel::next(BranchObj *bo_start)
insilmaril@650
  1350
{
insilmaril@650
  1351
	BranchObj *rbo;
insilmaril@650
  1352
	BranchObj *bo=bo_start;
insilmaril@650
  1353
	if (bo)
insilmaril@650
  1354
	{
insilmaril@665
  1355
		// Try to find next branch in current MapCenter
insilmaril@650
  1356
		rbo=bo->next();
insilmaril@650
  1357
		if (rbo) return rbo;
insilmaril@650
  1358
insilmaril@650
  1359
		// Try to find MapCenter of bo
insilmaril@650
  1360
		while (bo->getDepth()>0) bo=(BranchObj*)bo->getParObj();
insilmaril@650
  1361
insilmaril@650
  1362
		// Try to find next MapCenter
insilmaril@650
  1363
		int i=mapCenters.indexOf ((MapCenterObj*)bo);
insilmaril@684
  1364
		if (i+2 > mapCenters.count() || i<0) return NULL;
insilmaril@684
  1365
		if (mapCenters.at(i+1)!=bo_start)
insilmaril@684
  1366
			return mapCenters.at(i+1);
insilmaril@650
  1367
	} 
insilmaril@650
  1368
	return NULL;
insilmaril@650
  1369
}
insilmaril@650
  1370
insilmaril@650
  1371
LinkableMapObj* VymModel::findMapObj(QPointF p, LinkableMapObj *excludeLMO)
insilmaril@650
  1372
{
insilmaril@650
  1373
	LinkableMapObj *lmo;
insilmaril@650
  1374
insilmaril@650
  1375
	for (int i=0;i<mapCenters.count(); i++)
insilmaril@650
  1376
	{
insilmaril@650
  1377
		lmo=mapCenters.at(i)->findMapObj (p,excludeLMO);
insilmaril@650
  1378
		if (lmo) return lmo;
insilmaril@650
  1379
	}
insilmaril@650
  1380
	return NULL;
insilmaril@650
  1381
}
insilmaril@650
  1382
insilmaril@650
  1383
LinkableMapObj* VymModel::findObjBySelect(const QString &s)
insilmaril@650
  1384
{
insilmaril@650
  1385
	LinkableMapObj *lmo;
insilmaril@650
  1386
	if (!s.isEmpty() )
insilmaril@650
  1387
	{
insilmaril@650
  1388
		QString part;
insilmaril@650
  1389
		QString typ;
insilmaril@650
  1390
		QString num;
insilmaril@650
  1391
		part=s.section(",",0,0);
insilmaril@652
  1392
		typ=part.left (2);
insilmaril@650
  1393
		num=part.right(part.length() - 3);
insilmaril@650
  1394
		if (typ=="mc" && num.toInt()>=0 && num.toInt() <mapCenters.count() )
insilmaril@650
  1395
			return mapCenters.at(num.toInt() );
insilmaril@650
  1396
	}		
insilmaril@650
  1397
insilmaril@650
  1398
	for (int i=0; i<mapCenters.count(); i++)
insilmaril@650
  1399
	{
insilmaril@650
  1400
		lmo=mapCenters.at(i)->findObjBySelect(s);
insilmaril@650
  1401
		if (lmo) return lmo;
insilmaril@650
  1402
	}	
insilmaril@650
  1403
	return NULL;
insilmaril@650
  1404
}
insilmaril@650
  1405
insilmaril@650
  1406
LinkableMapObj* VymModel::findID (const QString &s)
insilmaril@650
  1407
{
insilmaril@650
  1408
	LinkableMapObj *lmo;
insilmaril@650
  1409
	for (int i=0; i<mapCenters.count(); i++)
insilmaril@650
  1410
	{
insilmaril@650
  1411
		lmo=mapCenters.at(i)->findID (s);
insilmaril@650
  1412
		if (lmo) return lmo;
insilmaril@650
  1413
	}	
insilmaril@650
  1414
	return NULL;
insilmaril@650
  1415
}
insilmaril@650
  1416
insilmaril@650
  1417
QString VymModel::saveToDir (const QString &tmpdir,const QString &prefix, int verbose, const QPointF &offset)
insilmaril@650
  1418
{
insilmaril@650
  1419
    QString s;
insilmaril@650
  1420
insilmaril@650
  1421
	for (int i=0; i<mapCenters.count(); i++)
insilmaril@650
  1422
		s+=mapCenters.at(i)->saveToDir (tmpdir,prefix,verbose,offset);
insilmaril@650
  1423
    return s;
insilmaril@650
  1424
}
insilmaril@650
  1425
insilmaril@721
  1426
//////////////////////////////////////////////
insilmaril@721
  1427
// Interface 
insilmaril@721
  1428
//////////////////////////////////////////////
insilmaril@721
  1429
void VymModel::setVersion (const QString &s)
insilmaril@721
  1430
{
insilmaril@721
  1431
	version=s;
insilmaril@721
  1432
}
insilmaril@721
  1433
insilmaril@721
  1434
void VymModel::setAuthor (const QString &s)
insilmaril@721
  1435
{
insilmaril@721
  1436
	saveState (
insilmaril@721
  1437
		QString ("setMapAuthor (\"%1\")").arg(author),
insilmaril@721
  1438
		QString ("setMapAuthor (\"%1\")").arg(s),
insilmaril@721
  1439
		QString ("Set author of map to \"%1\"").arg(s)
insilmaril@721
  1440
	);
insilmaril@721
  1441
insilmaril@721
  1442
	author=s;
insilmaril@721
  1443
}
insilmaril@721
  1444
insilmaril@721
  1445
QString VymModel::getAuthor()
insilmaril@721
  1446
{
insilmaril@721
  1447
	return author;
insilmaril@721
  1448
}
insilmaril@721
  1449
insilmaril@721
  1450
void VymModel::setComment (const QString &s)
insilmaril@721
  1451
{
insilmaril@721
  1452
	saveState (
insilmaril@721
  1453
		QString ("setMapComment (\"%1\")").arg(comment),
insilmaril@721
  1454
		QString ("setMapComment (\"%1\")").arg(s),
insilmaril@721
  1455
		QString ("Set comment of map")
insilmaril@721
  1456
	);
insilmaril@721
  1457
insilmaril@721
  1458
	comment=s;
insilmaril@721
  1459
}
insilmaril@721
  1460
insilmaril@721
  1461
QString VymModel::getComment ()
insilmaril@721
  1462
{
insilmaril@721
  1463
	return comment;
insilmaril@721
  1464
}
insilmaril@721
  1465
insilmaril@721
  1466
QString VymModel::getDate ()
insilmaril@721
  1467
{
insilmaril@721
  1468
	return QDate::currentDate().toString ("yyyy-MM-dd");
insilmaril@721
  1469
}
insilmaril@721
  1470
insilmaril@721
  1471
void VymModel::setHeading(const QString &s)
insilmaril@721
  1472
{
insilmaril@721
  1473
	BranchObj *sel=selection.getBranch();
insilmaril@721
  1474
	if (sel)
insilmaril@721
  1475
	{
insilmaril@721
  1476
		saveState(
insilmaril@721
  1477
			sel,
insilmaril@721
  1478
			"setHeading (\""+sel->getHeading()+"\")", 
insilmaril@721
  1479
			sel,
insilmaril@721
  1480
			"setHeading (\""+s+"\")", 
insilmaril@721
  1481
			QString("Set heading of %1 to \"%2\"").arg(getObjectName(sel)).arg(s) );
insilmaril@721
  1482
		sel->setHeading(s );
insilmaril@721
  1483
		reposition();
insilmaril@721
  1484
		selection.update();
insilmaril@721
  1485
		ensureSelectionVisible();
insilmaril@721
  1486
	}
insilmaril@721
  1487
}
insilmaril@721
  1488
insilmaril@721
  1489
QString VymModel::getHeading(bool &ok, QPoint &p)
insilmaril@721
  1490
{
insilmaril@721
  1491
	BranchObj *bo=selection.getBranch();
insilmaril@721
  1492
	if (bo)
insilmaril@721
  1493
	{
insilmaril@721
  1494
		ok=true;
insilmaril@721
  1495
		//p=scene->mapFromScene(bo->getAbsPos());	// FIXME this is view-dependant!!!
insilmaril@721
  1496
		return bo->getHeading();
insilmaril@721
  1497
	}
insilmaril@721
  1498
	ok=false;
insilmaril@721
  1499
	return QString();
insilmaril@721
  1500
}
insilmaril@721
  1501
insilmaril@721
  1502
insilmaril@721
  1503
void VymModel::setHeadingInt(const QString &s)
insilmaril@721
  1504
{
insilmaril@721
  1505
	BranchObj *bo=selection.getBranch();
insilmaril@721
  1506
	if (bo)
insilmaril@721
  1507
	{
insilmaril@721
  1508
		bo->setHeading(s);
insilmaril@721
  1509
		reposition();
insilmaril@721
  1510
		selection.update();
insilmaril@721
  1511
		ensureSelectionVisible();
insilmaril@721
  1512
	}
insilmaril@721
  1513
}
insilmaril@721
  1514
insilmaril@721
  1515
BranchObj* VymModel::findText (QString s, bool cs)
insilmaril@721
  1516
{
insilmaril@721
  1517
	QTextDocument::FindFlags flags=0;
insilmaril@721
  1518
	if (cs) flags=QTextDocument::FindCaseSensitively;
insilmaril@721
  1519
insilmaril@721
  1520
	if (!itFind) 
insilmaril@721
  1521
	{	// Nothing found or new find process
insilmaril@721
  1522
		if (EOFind)
insilmaril@721
  1523
			// nothing found, start again
insilmaril@721
  1524
			EOFind=false;
insilmaril@721
  1525
		itFind=first();
insilmaril@721
  1526
	}	
insilmaril@721
  1527
	bool searching=true;
insilmaril@721
  1528
	bool foundNote=false;
insilmaril@721
  1529
	while (searching && !EOFind)
insilmaril@721
  1530
	{
insilmaril@721
  1531
		if (itFind)
insilmaril@721
  1532
		{
insilmaril@721
  1533
			// Searching in Note
insilmaril@721
  1534
			if (itFind->getNote().contains(s,cs))
insilmaril@721
  1535
			{
insilmaril@721
  1536
				if (selection.single()!=itFind) 
insilmaril@721
  1537
				{
insilmaril@721
  1538
					selection.select(itFind);
insilmaril@721
  1539
					ensureSelectionVisible();
insilmaril@721
  1540
				}
insilmaril@721
  1541
				if (textEditor->findText(s,flags)) 
insilmaril@721
  1542
				{
insilmaril@721
  1543
					searching=false;
insilmaril@721
  1544
					foundNote=true;
insilmaril@721
  1545
				}	
insilmaril@721
  1546
			}
insilmaril@721
  1547
			// Searching in Heading
insilmaril@721
  1548
			if (searching && itFind->getHeading().contains (s,cs) ) 
insilmaril@721
  1549
			{
insilmaril@721
  1550
				selection.select(itFind);
insilmaril@721
  1551
				ensureSelectionVisible();
insilmaril@721
  1552
				searching=false;
insilmaril@721
  1553
			}
insilmaril@721
  1554
		}	
insilmaril@721
  1555
		if (!foundNote)
insilmaril@721
  1556
		{
insilmaril@721
  1557
			itFind=next(itFind);
insilmaril@721
  1558
			if (!itFind) EOFind=true;
insilmaril@721
  1559
		}
insilmaril@721
  1560
	//cout <<"still searching...  "<<qPrintable( itFind->getHeading())<<endl;
insilmaril@721
  1561
	}	
insilmaril@721
  1562
	if (!searching)
insilmaril@721
  1563
		return selection.getBranch();
insilmaril@721
  1564
	else
insilmaril@721
  1565
		return NULL;
insilmaril@721
  1566
}
insilmaril@721
  1567
insilmaril@721
  1568
void VymModel::findReset()
insilmaril@721
  1569
{	// Necessary if text to find changes during a find process
insilmaril@721
  1570
	itFind=NULL;
insilmaril@721
  1571
	EOFind=false;
insilmaril@721
  1572
}
insilmaril@721
  1573
insilmaril@721
  1574
insilmaril@721
  1575
insilmaril@721
  1576
void VymModel::setScene (QGraphicsScene *s)
insilmaril@721
  1577
{
insilmaril@721
  1578
	mapScene=s;
insilmaril@721
  1579
    init();	// Here we have a mapScene set, 
insilmaril@721
  1580
			// which is (still) needed to create MapCenters
insilmaril@721
  1581
}
insilmaril@721
  1582
insilmaril@721
  1583
void VymModel::setURL(const QString &url)
insilmaril@721
  1584
{
insilmaril@721
  1585
	BranchObj *bo=selection.getBranch();
insilmaril@721
  1586
	if (bo)
insilmaril@721
  1587
	{
insilmaril@721
  1588
		QString oldurl=bo->getURL();
insilmaril@721
  1589
		bo->setURL (url);
insilmaril@721
  1590
		saveState (
insilmaril@721
  1591
			bo,
insilmaril@721
  1592
			QString ("setURL (\"%1\")").arg(oldurl),
insilmaril@721
  1593
			bo,
insilmaril@721
  1594
			QString ("setURL (\"%1\")").arg(url),
insilmaril@721
  1595
			QString ("set URL of %1 to %2").arg(getObjectName(bo)).arg(url)
insilmaril@721
  1596
		);
insilmaril@721
  1597
		updateActions();
insilmaril@721
  1598
		reposition();
insilmaril@721
  1599
		selection.update();
insilmaril@721
  1600
		ensureSelectionVisible();
insilmaril@721
  1601
	}
insilmaril@721
  1602
}	
insilmaril@721
  1603
insilmaril@721
  1604
QString VymModel::getURL()
insilmaril@721
  1605
{
insilmaril@721
  1606
	BranchObj *bo=selection.getBranch();
insilmaril@721
  1607
	if (bo)
insilmaril@721
  1608
		return bo->getURL();
insilmaril@721
  1609
	else
insilmaril@721
  1610
		return "";
insilmaril@721
  1611
}
insilmaril@721
  1612
insilmaril@721
  1613
QStringList VymModel::getURLs()
insilmaril@721
  1614
{
insilmaril@721
  1615
	QStringList urls;
insilmaril@721
  1616
	BranchObj *bo=selection.getBranch();
insilmaril@721
  1617
	if (bo)
insilmaril@721
  1618
	{		
insilmaril@721
  1619
		bo=bo->first();	
insilmaril@721
  1620
		while (bo) 
insilmaril@721
  1621
		{
insilmaril@721
  1622
			if (!bo->getURL().isEmpty()) urls.append( bo->getURL());
insilmaril@721
  1623
			bo=bo->next();
insilmaril@721
  1624
		}	
insilmaril@721
  1625
	}	
insilmaril@721
  1626
	return urls;
insilmaril@721
  1627
}
insilmaril@721
  1628
insilmaril@721
  1629
void VymModel::linkFloatImageTo(const QString &dstString)	
insilmaril@721
  1630
{
insilmaril@721
  1631
	FloatImageObj *fio=selection.getFloatImage();
insilmaril@721
  1632
	if (fio)
insilmaril@721
  1633
	{
insilmaril@721
  1634
		BranchObj *dst=(BranchObj*)findObjBySelect(dstString);
insilmaril@721
  1635
		if (dst && (typeid(*dst)==typeid (BranchObj) || 
insilmaril@721
  1636
					typeid(*dst)==typeid (MapCenterObj)))
insilmaril@721
  1637
		{			
insilmaril@721
  1638
			LinkableMapObj *dstPar=dst->getParObj();
insilmaril@721
  1639
			QString parString=getSelectString(dstPar);
insilmaril@721
  1640
			QString fioPreSelectString=getSelectString(fio);
insilmaril@721
  1641
			QString fioPreParentSelectString=getSelectString (fio->getParObj());
insilmaril@721
  1642
			((BranchObj*)dst)->addFloatImage (fio);
insilmaril@721
  1643
			selection.unselect();
insilmaril@721
  1644
			((BranchObj*)(fio->getParObj()))->removeFloatImage (fio);
insilmaril@721
  1645
			fio=((BranchObj*)dst)->getLastFloatImage();
insilmaril@721
  1646
			fio->setRelPos();
insilmaril@721
  1647
			fio->reposition();
insilmaril@721
  1648
			selection.select(fio);
insilmaril@721
  1649
			saveState(
insilmaril@721
  1650
				getSelectString(fio),
insilmaril@721
  1651
				QString("linkTo (\"%1\")").arg(fioPreParentSelectString), 
insilmaril@721
  1652
				fioPreSelectString, 
insilmaril@721
  1653
				QString ("linkTo (\"%1\")").arg(dstString),
insilmaril@721
  1654
				QString ("Link floatimage to %1").arg(getObjectName(dst)));
insilmaril@721
  1655
		}
insilmaril@721
  1656
	}
insilmaril@721
  1657
}
insilmaril@721
  1658
insilmaril@721
  1659
insilmaril@721
  1660
void VymModel::setFrameType(const FrameObj::FrameType &t)
insilmaril@721
  1661
{
insilmaril@721
  1662
	BranchObj *bo=selection.getBranch();
insilmaril@721
  1663
	if (bo)
insilmaril@721
  1664
	{
insilmaril@721
  1665
		QString s=bo->getFrameTypeName();
insilmaril@721
  1666
		bo->setFrameType (t);
insilmaril@721
  1667
		saveState (bo, QString("setFrameType (\"%1\")").arg(s),
insilmaril@721
  1668
			bo, QString ("setFrameType (\"%1\")").arg(bo->getFrameTypeName()),QString ("set type of frame to %1").arg(s));
insilmaril@721
  1669
		reposition();
insilmaril@721
  1670
		bo->updateLink();
insilmaril@721
  1671
	}
insilmaril@721
  1672
}
insilmaril@721
  1673
insilmaril@721
  1674
void VymModel::setFrameType(const QString &s)	
insilmaril@721
  1675
{
insilmaril@721
  1676
	BranchObj *bo=selection.getBranch();
insilmaril@721
  1677
	if (bo)
insilmaril@721
  1678
	{
insilmaril@721
  1679
		saveState (bo, QString("setFrameType (\"%1\")").arg(bo->getFrameTypeName()),
insilmaril@721
  1680
			bo, QString ("setFrameType (\"%1\")").arg(s),QString ("set type of frame to %1").arg(s));
insilmaril@721
  1681
		bo->setFrameType (s);
insilmaril@721
  1682
		reposition();
insilmaril@721
  1683
		bo->updateLink();
insilmaril@721
  1684
	}
insilmaril@721
  1685
}
insilmaril@721
  1686
insilmaril@721
  1687
void VymModel::setFramePenColor(const QColor &c)	
insilmaril@721
  1688
{
insilmaril@721
  1689
	BranchObj *bo=selection.getBranch();
insilmaril@721
  1690
	if (bo)
insilmaril@721
  1691
	{
insilmaril@721
  1692
		saveState (bo, QString("setFramePenColor (\"%1\")").arg(bo->getFramePenColor().name() ),
insilmaril@721
  1693
			bo, QString ("setFramePenColor (\"%1\")").arg(c.name() ),QString ("set pen color of frame to %1").arg(c.name() ));
insilmaril@721
  1694
		bo->setFramePenColor (c);
insilmaril@721
  1695
	}	
insilmaril@721
  1696
}
insilmaril@721
  1697
insilmaril@721
  1698
void VymModel::setFrameBrushColor(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("setFrameBrushColor (\"%1\")").arg(bo->getFrameBrushColor().name() ),
insilmaril@721
  1704
			bo, QString ("setFrameBrushColor (\"%1\")").arg(c.name() ),QString ("set brush color of frame to %1").arg(c.name() ));
insilmaril@721
  1705
		bo->setFrameBrushColor (c);
insilmaril@721
  1706
	}	
insilmaril@721
  1707
}
insilmaril@721
  1708
insilmaril@721
  1709
void VymModel::setFramePadding (const int &i)
insilmaril@721
  1710
{
insilmaril@721
  1711
	BranchObj *bo=selection.getBranch();
insilmaril@721
  1712
	if (bo)
insilmaril@721
  1713
	{
insilmaril@721
  1714
		saveState (bo, QString("setFramePadding (\"%1\")").arg(bo->getFramePadding() ),
insilmaril@721
  1715
			bo, QString ("setFramePadding (\"%1\")").arg(i),QString ("set brush color of frame to %1").arg(i));
insilmaril@721
  1716
		bo->setFramePadding (i);
insilmaril@721
  1717
		reposition();
insilmaril@721
  1718
		bo->updateLink();
insilmaril@721
  1719
	}	
insilmaril@721
  1720
}
insilmaril@721
  1721
insilmaril@721
  1722
void VymModel::setFrameBorderWidth(const int &i)
insilmaril@721
  1723
{
insilmaril@721
  1724
	BranchObj *bo=selection.getBranch();
insilmaril@721
  1725
	if (bo)
insilmaril@721
  1726
	{
insilmaril@721
  1727
		saveState (bo, QString("setFrameBorderWidth (\"%1\")").arg(bo->getFrameBorderWidth() ),
insilmaril@721
  1728
			bo, QString ("setFrameBorderWidth (\"%1\")").arg(i),QString ("set border width of frame to %1").arg(i));
insilmaril@721
  1729
		bo->setFrameBorderWidth (i);
insilmaril@721
  1730
		reposition();
insilmaril@721
  1731
		bo->updateLink();
insilmaril@721
  1732
	}	
insilmaril@721
  1733
}
insilmaril@721
  1734
insilmaril@721
  1735
void VymModel::setIncludeImagesVer(bool b)	
insilmaril@721
  1736
{
insilmaril@721
  1737
	BranchObj *bo=selection.getBranch();
insilmaril@721
  1738
	if (bo)
insilmaril@721
  1739
	{
insilmaril@721
  1740
		QString u= b ? "false" : "true";
insilmaril@721
  1741
		QString r=!b ? "false" : "true";
insilmaril@721
  1742
		
insilmaril@721
  1743
		saveState(
insilmaril@721
  1744
			bo,
insilmaril@721
  1745
			QString("setIncludeImagesVertically (%1)").arg(u),
insilmaril@721
  1746
			bo, 
insilmaril@721
  1747
			QString("setIncludeImagesVertically (%1)").arg(r),
insilmaril@721
  1748
			QString("Include images vertically in %1").arg(getObjectName(bo))
insilmaril@721
  1749
		);	
insilmaril@721
  1750
		bo->setIncludeImagesVer(b);
insilmaril@721
  1751
		reposition();
insilmaril@721
  1752
	}	
insilmaril@721
  1753
}
insilmaril@721
  1754
insilmaril@721
  1755
void VymModel::setIncludeImagesHor(bool b)	
insilmaril@721
  1756
{
insilmaril@721
  1757
	BranchObj *bo=selection.getBranch();
insilmaril@721
  1758
	if (bo)
insilmaril@721
  1759
	{
insilmaril@721
  1760
		QString u= b ? "false" : "true";
insilmaril@721
  1761
		QString r=!b ? "false" : "true";
insilmaril@721
  1762
		
insilmaril@721
  1763
		saveState(
insilmaril@721
  1764
			bo,
insilmaril@721
  1765
			QString("setIncludeImagesHorizontally (%1)").arg(u),
insilmaril@721
  1766
			bo, 
insilmaril@721
  1767
			QString("setIncludeImagesHorizontally (%1)").arg(r),
insilmaril@721
  1768
			QString("Include images horizontally in %1").arg(getObjectName(bo))
insilmaril@721
  1769
		);	
insilmaril@721
  1770
		bo->setIncludeImagesHor(b);
insilmaril@721
  1771
		reposition();
insilmaril@721
  1772
	}	
insilmaril@721
  1773
}
insilmaril@721
  1774
insilmaril@721
  1775
void VymModel::setHideLinkUnselected (bool b)
insilmaril@721
  1776
{
insilmaril@721
  1777
	LinkableMapObj *sel=selection.single();
insilmaril@721
  1778
	if (sel &&
insilmaril@721
  1779
		(selection.type() == Selection::Branch || 
insilmaril@721
  1780
		selection.type() == Selection::MapCenter  ||
insilmaril@721
  1781
		selection.type() == Selection::FloatImage ))
insilmaril@721
  1782
	{
insilmaril@721
  1783
		QString u= b ? "false" : "true";
insilmaril@721
  1784
		QString r=!b ? "false" : "true";
insilmaril@721
  1785
		
insilmaril@721
  1786
		saveState(
insilmaril@721
  1787
			sel,
insilmaril@721
  1788
			QString("setHideLinkUnselected (%1)").arg(u),
insilmaril@721
  1789
			sel, 
insilmaril@721
  1790
			QString("setHideLinkUnselected (%1)").arg(r),
insilmaril@721
  1791
			QString("Hide link of %1 if unselected").arg(getObjectName(sel))
insilmaril@721
  1792
		);	
insilmaril@721
  1793
		sel->setHideLinkUnselected(b);
insilmaril@721
  1794
	}
insilmaril@721
  1795
}
insilmaril@721
  1796
insilmaril@721
  1797
void VymModel::setHideExport(bool b)
insilmaril@721
  1798
{
insilmaril@721
  1799
	BranchObj *bo=selection.getBranch();
insilmaril@721
  1800
	if (bo)
insilmaril@721
  1801
	{
insilmaril@721
  1802
		bo->setHideInExport (b);
insilmaril@721
  1803
		QString u= b ? "false" : "true";
insilmaril@721
  1804
		QString r=!b ? "false" : "true";
insilmaril@721
  1805
		
insilmaril@721
  1806
		saveState(
insilmaril@721
  1807
			bo,
insilmaril@721
  1808
			QString ("setHideExport (%1)").arg(u),
insilmaril@721
  1809
			bo,
insilmaril@721
  1810
			QString ("setHideExport (%1)").arg(r),
insilmaril@721
  1811
			QString ("Set HideExport flag of %1 to %2").arg(getObjectName(bo)).arg (r)
insilmaril@721
  1812
		);	
insilmaril@721
  1813
		updateActions();
insilmaril@721
  1814
		reposition();
insilmaril@721
  1815
		selection.update();
insilmaril@721
  1816
		// FIXME needed? scene()->update();
insilmaril@721
  1817
	}
insilmaril@721
  1818
}
insilmaril@721
  1819
insilmaril@721
  1820
void VymModel::toggleHideExport()
insilmaril@721
  1821
{
insilmaril@721
  1822
	BranchObj *bo=selection.getBranch();
insilmaril@721
  1823
	if (bo)
insilmaril@721
  1824
		setHideExport ( !bo->hideInExport() );
insilmaril@721
  1825
}
insilmaril@721
  1826
insilmaril@721
  1827
insilmaril@721
  1828
void VymModel::copy()
insilmaril@721
  1829
{
insilmaril@721
  1830
	LinkableMapObj *sel=selection.single();
insilmaril@721
  1831
	if (sel)
insilmaril@721
  1832
	{
insilmaril@721
  1833
		if (redosAvail == 0)
insilmaril@721
  1834
		{
insilmaril@721
  1835
			// Copy to history
insilmaril@721
  1836
			QString s=getSelectString(sel);
insilmaril@721
  1837
			saveState (PartOfMap, s, "nop ()", s, "copy ()","Copy selection to clipboard",sel  );
insilmaril@721
  1838
			curClipboard=curStep;
insilmaril@721
  1839
		}
insilmaril@721
  1840
insilmaril@721
  1841
		// Copy also to global clipboard, because we are at last step in history
insilmaril@721
  1842
		QString bakMapName(QString("history-%1").arg(curStep));
insilmaril@721
  1843
		QString bakMapDir(tmpMapDir +"/"+bakMapName);
insilmaril@721
  1844
		copyDir (bakMapDir,clipboardDir );
insilmaril@721
  1845
insilmaril@721
  1846
		clipboardEmpty=false;
insilmaril@721
  1847
		updateActions();
insilmaril@721
  1848
	}	    
insilmaril@721
  1849
}
insilmaril@721
  1850
insilmaril@721
  1851
insilmaril@721
  1852
void VymModel::pasteNoSave(const int &n)
insilmaril@721
  1853
{
insilmaril@721
  1854
	bool old=blockSaveState;
insilmaril@721
  1855
	blockSaveState=true;
insilmaril@721
  1856
	bool zippedOrg=zipped;
insilmaril@721
  1857
	if (redosAvail > 0 || n!=0)
insilmaril@721
  1858
	{
insilmaril@721
  1859
		// Use the "historical" buffer
insilmaril@721
  1860
		QString bakMapName(QString("history-%1").arg(n));
insilmaril@721
  1861
		QString bakMapDir(tmpMapDir +"/"+bakMapName);
insilmaril@721
  1862
		load (bakMapDir+"/"+clipboardFile,ImportAdd, VymMap);
insilmaril@721
  1863
	} else
insilmaril@721
  1864
		// Use the global buffer
insilmaril@721
  1865
		load (clipboardDir+"/"+clipboardFile,ImportAdd, VymMap);
insilmaril@721
  1866
	zipped=zippedOrg;
insilmaril@721
  1867
	blockSaveState=old;
insilmaril@721
  1868
}
insilmaril@721
  1869
insilmaril@721
  1870
void VymModel::paste()		
insilmaril@721
  1871
{   
insilmaril@721
  1872
	BranchObj *sel=selection.getBranch();
insilmaril@721
  1873
	if (sel)
insilmaril@721
  1874
	{
insilmaril@721
  1875
		saveStateChangingPart(
insilmaril@721
  1876
			sel,
insilmaril@721
  1877
			sel,
insilmaril@721
  1878
			QString ("paste (%1)").arg(curClipboard),
insilmaril@721
  1879
			QString("Paste to %1").arg( getObjectName(sel))
insilmaril@721
  1880
		);
insilmaril@721
  1881
		pasteNoSave(0);
insilmaril@721
  1882
		reposition();
insilmaril@721
  1883
	}
insilmaril@721
  1884
}
insilmaril@721
  1885
insilmaril@721
  1886
void VymModel::cut()
insilmaril@721
  1887
{
insilmaril@721
  1888
	LinkableMapObj *sel=selection.single();
insilmaril@721
  1889
	if ( sel && (selection.type() == Selection::Branch ||
insilmaril@721
  1890
		selection.type()==Selection::MapCenter ||
insilmaril@721
  1891
		selection.type()==Selection::FloatImage))
insilmaril@721
  1892
	{
insilmaril@721
  1893
	/* No savestate! savestate is called in cutNoSave
insilmaril@721
  1894
		saveStateChangingPart(
insilmaril@721
  1895
			sel->getParObj(),
insilmaril@721
  1896
			sel,
insilmaril@721
  1897
			"cut ()",
insilmaril@721
  1898
			QString("Cut %1").arg(getObjectName(sel ))
insilmaril@721
  1899
		);
insilmaril@721
  1900
	*/	
insilmaril@721
  1901
		copy();
insilmaril@721
  1902
		deleteSelection();
insilmaril@721
  1903
		reposition();
insilmaril@721
  1904
	}
insilmaril@721
  1905
}
insilmaril@721
  1906
insilmaril@721
  1907
void VymModel::moveBranchUp()
insilmaril@721
  1908
{
insilmaril@721
  1909
	BranchObj* bo=selection.getBranch();
insilmaril@721
  1910
	BranchObj* par;
insilmaril@721
  1911
	if (bo)
insilmaril@721
  1912
	{
insilmaril@721
  1913
		if (!bo->canMoveBranchUp()) return;
insilmaril@721
  1914
		par=(BranchObj*)(bo->getParObj());
insilmaril@721
  1915
		BranchObj *obo=par->moveBranchUp (bo);	// bo will be the one below selection
insilmaril@721
  1916
		saveState (getSelectString(bo),"moveBranchDown ()",getSelectString(obo),"moveBranchUp ()",QString("Move up %1").arg(getObjectName(bo)));
insilmaril@721
  1917
		reposition();
insilmaril@721
  1918
		//FIXME needed? scene()->update();
insilmaril@721
  1919
		selection.update();
insilmaril@721
  1920
		ensureSelectionVisible();
insilmaril@721
  1921
	}
insilmaril@721
  1922
}
insilmaril@721
  1923
insilmaril@721
  1924
void VymModel::moveBranchDown()
insilmaril@721
  1925
{
insilmaril@721
  1926
	BranchObj* bo=selection.getBranch();
insilmaril@721
  1927
	BranchObj* par;
insilmaril@721
  1928
	if (bo)
insilmaril@721
  1929
	{
insilmaril@721
  1930
		if (!bo->canMoveBranchDown()) return;
insilmaril@721
  1931
		par=(BranchObj*)(bo->getParObj());
insilmaril@721
  1932
		BranchObj *obo=par->moveBranchDown(bo);	// bo will be the one above selection
insilmaril@721
  1933
		saveState(getSelectString(bo),"moveBranchUp ()",getSelectString(obo),"moveBranchDown ()",QString("Move down %1").arg(getObjectName(bo)));
insilmaril@721
  1934
		reposition();
insilmaril@721
  1935
		//FIXME needed? scene()->update();
insilmaril@721
  1936
		selection.update();
insilmaril@721
  1937
		ensureSelectionVisible();
insilmaril@721
  1938
	}	
insilmaril@721
  1939
}
insilmaril@721
  1940
insilmaril@721
  1941
void VymModel::sortChildren()
insilmaril@721
  1942
{
insilmaril@721
  1943
	BranchObj* bo=selection.getBranch();
insilmaril@721
  1944
	if (bo)
insilmaril@721
  1945
	{
insilmaril@721
  1946
		if(bo->countBranches()>1)
insilmaril@721
  1947
		{
insilmaril@721
  1948
			saveStateChangingPart(bo,bo, "sortChildren ()",QString("Sort children of %1").arg(getObjectName(bo)));
insilmaril@721
  1949
			bo->sortChildren();
insilmaril@721
  1950
			reposition();
insilmaril@721
  1951
			ensureSelectionVisible();
insilmaril@721
  1952
		}
insilmaril@721
  1953
	}
insilmaril@721
  1954
}
insilmaril@721
  1955
insilmaril@721
  1956
MapCenterObj* VymModel::addMapCenter ()
insilmaril@721
  1957
{
insilmaril@723
  1958
	MapCenterObj *mco=addMapCenter (contextPos);
insilmaril@721
  1959
	selection.select (mco);
insilmaril@721
  1960
	updateActions();
insilmaril@721
  1961
	ensureSelectionVisible();
insilmaril@721
  1962
	saveState (
insilmaril@721
  1963
		mco,
insilmaril@721
  1964
		"delete()",
insilmaril@721
  1965
		NULL,
insilmaril@723
  1966
		QString ("addMapCenter (%1,%2)").arg (contextPos.x()).arg(contextPos.y()),
insilmaril@723
  1967
		QString ("Adding MapCenter to (%1,%2)").arg (contextPos.x()).arg(contextPos.y())
insilmaril@721
  1968
	);	
insilmaril@721
  1969
	return mco;	
insilmaril@721
  1970
}
insilmaril@721
  1971
insilmaril@721
  1972
MapCenterObj* VymModel::addMapCenter(QPointF absPos)
insilmaril@721
  1973
{
insilmaril@721
  1974
	MapCenterObj *mapCenter = new MapCenterObj(mapScene);
insilmaril@721
  1975
	mapCenter->move (absPos);
insilmaril@721
  1976
    mapCenter->setVisibility (true);
insilmaril@721
  1977
	mapCenter->setHeading (QApplication::translate("Heading of mapcenter in new map", "New map"));
insilmaril@721
  1978
	mapCenter->setMapEditor(mapEditor);		//FIXME needed to get defLinkStyle, mapLinkColorHint ... for later added objects
insilmaril@721
  1979
	mapCenters.append(mapCenter);
insilmaril@721
  1980
	return mapCenter;
insilmaril@721
  1981
}
insilmaril@721
  1982
insilmaril@723
  1983
MapCenterObj* VymModel::removeMapCenter(MapCenterObj* mco)
insilmaril@721
  1984
{
insilmaril@721
  1985
	int i=mapCenters.indexOf (mco);
insilmaril@721
  1986
	if (i>=0)
insilmaril@721
  1987
	{
insilmaril@721
  1988
		mapCenters.removeAt (i);
insilmaril@721
  1989
		delete (mco);
insilmaril@721
  1990
		if (i>0) return mapCenters.at(i-1);	// Return previous MCO
insilmaril@721
  1991
	}
insilmaril@721
  1992
	return NULL;
insilmaril@721
  1993
}
insilmaril@721
  1994
insilmaril@723
  1995
MapCenterObj* VymModel::getLastMapCenter()
insilmaril@723
  1996
{
insilmaril@723
  1997
	if (mapCenters.size()>0)
insilmaril@723
  1998
		return mapCenters.last();
insilmaril@723
  1999
	else
insilmaril@723
  2000
		return NULL;
insilmaril@723
  2001
insilmaril@723
  2002
}
insilmaril@723
  2003
insilmaril@721
  2004
insilmaril@721
  2005
BranchObj* VymModel::addNewBranchInt(int num)
insilmaril@721
  2006
{
insilmaril@721
  2007
	// Depending on pos:
insilmaril@721
  2008
	// -3		insert in children of parent  above selection 
insilmaril@721
  2009
	// -2		add branch to selection 
insilmaril@721
  2010
	// -1		insert in children of parent below selection 
insilmaril@721
  2011
	// 0..n		insert in children of parent at pos
insilmaril@721
  2012
	BranchObj *newbo=NULL;
insilmaril@721
  2013
	BranchObj *bo=selection.getBranch();
insilmaril@721
  2014
	if (bo)
insilmaril@721
  2015
	{
insilmaril@721
  2016
		if (num==-2)
insilmaril@721
  2017
		{
insilmaril@721
  2018
			// save scroll state. If scrolled, automatically select
insilmaril@721
  2019
			// new branch in order to tmp unscroll parent...
insilmaril@721
  2020
			newbo=bo->addBranch();
insilmaril@721
  2021
			
insilmaril@721
  2022
		}else if (num==-1)
insilmaril@721
  2023
		{
insilmaril@721
  2024
			num=bo->getNum()+1;
insilmaril@721
  2025
			bo=(BranchObj*)bo->getParObj();
insilmaril@721
  2026
			if (bo) newbo=bo->insertBranch(num);
insilmaril@721
  2027
		}else if (num==-3)
insilmaril@721
  2028
		{
insilmaril@721
  2029
			num=bo->getNum();
insilmaril@721
  2030
			bo=(BranchObj*)bo->getParObj();
insilmaril@721
  2031
			if (bo) newbo=bo->insertBranch(num);
insilmaril@721
  2032
		}
insilmaril@721
  2033
		if (!newbo) return NULL;
insilmaril@721
  2034
	}	
insilmaril@721
  2035
	return newbo;
insilmaril@721
  2036
}	
insilmaril@721
  2037
insilmaril@721
  2038
BranchObj* VymModel::addNewBranch(int pos)
insilmaril@721
  2039
{
insilmaril@721
  2040
	// Different meaning than num in addNewBranchInt!
insilmaril@721
  2041
	// -1	add above
insilmaril@721
  2042
	//  0	add as child
insilmaril@721
  2043
	// +1	add below
insilmaril@721
  2044
	BranchObj *bo = selection.getBranch();
insilmaril@721
  2045
	BranchObj *newbo=NULL;
insilmaril@721
  2046
insilmaril@721
  2047
	if (bo)
insilmaril@721
  2048
	{
insilmaril@721
  2049
		// FIXME  do we still need this in model? setCursor (Qt::ArrowCursor);
insilmaril@721
  2050
insilmaril@721
  2051
		newbo=addNewBranchInt (pos-2);
insilmaril@721
  2052
insilmaril@721
  2053
		if (newbo)
insilmaril@721
  2054
		{
insilmaril@721
  2055
			saveState(
insilmaril@721
  2056
				newbo,		
insilmaril@721
  2057
				"delete ()",
insilmaril@721
  2058
				bo,
insilmaril@721
  2059
				QString ("addBranch (%1)").arg(pos),
insilmaril@721
  2060
				QString ("Add new branch to %1").arg(getObjectName(bo)));	
insilmaril@721
  2061
insilmaril@721
  2062
			reposition();
insilmaril@721
  2063
			selection.update();
insilmaril@721
  2064
			latestSelectionString=getSelectString(newbo);
insilmaril@721
  2065
			// In Network mode, the client needs to know where the new branch is,
insilmaril@721
  2066
			// so we have to pass on this information via saveState.
insilmaril@721
  2067
			// TODO: Get rid of this positioning workaround
insilmaril@721
  2068
			QString ps=qpointfToString (newbo->getAbsPos());
insilmaril@721
  2069
			sendData ("selectLatestAdded ()");
insilmaril@721
  2070
			sendData (QString("move %1").arg(ps));
insilmaril@721
  2071
			sendSelection();
insilmaril@721
  2072
		}
insilmaril@721
  2073
	}	
insilmaril@721
  2074
	return newbo;
insilmaril@721
  2075
}
insilmaril@721
  2076
insilmaril@721
  2077
insilmaril@721
  2078
BranchObj* VymModel::addNewBranchBefore()
insilmaril@721
  2079
{
insilmaril@721
  2080
	BranchObj *newbo=NULL;
insilmaril@721
  2081
	BranchObj *bo = selection.getBranch();
insilmaril@721
  2082
	if (bo && selection.type()==Selection::Branch)
insilmaril@721
  2083
		 // We accept no MapCenterObj here, so we _have_ a parent
insilmaril@721
  2084
	{
insilmaril@721
  2085
		QPointF p=bo->getRelPos();
insilmaril@721
  2086
insilmaril@721
  2087
insilmaril@721
  2088
		BranchObj *parbo=(BranchObj*)(bo->getParObj());
insilmaril@721
  2089
insilmaril@721
  2090
		// add below selection
insilmaril@721
  2091
		newbo=parbo->insertBranch(bo->getNum()+1);
insilmaril@721
  2092
		if (newbo)
insilmaril@721
  2093
		{
insilmaril@721
  2094
			newbo->move2RelPos (p);
insilmaril@721
  2095
insilmaril@721
  2096
			// Move selection to new branch
insilmaril@721
  2097
			bo->linkTo (newbo,-1);
insilmaril@721
  2098
insilmaril@721
  2099
			saveState (newbo, "deleteKeepChildren ()", newbo, "addBranchBefore ()", 
insilmaril@721
  2100
				QString ("Add branch before %1").arg(getObjectName(bo)));
insilmaril@721
  2101
insilmaril@721
  2102
			reposition();
insilmaril@721
  2103
			selection.update();
insilmaril@721
  2104
		}
insilmaril@721
  2105
	}	
insilmaril@721
  2106
	latestSelectionString=selection.getSelectString();
insilmaril@721
  2107
	return newbo;
insilmaril@721
  2108
}
insilmaril@721
  2109
insilmaril@721
  2110
void VymModel::deleteSelection()
insilmaril@721
  2111
{
insilmaril@721
  2112
	BranchObj *bo = selection.getBranch();
insilmaril@721
  2113
	if (bo && selection.type()==Selection::MapCenter)
insilmaril@721
  2114
	{
insilmaril@721
  2115
	//	BranchObj* par=(BranchObj*)(bo->getParObj());
insilmaril@721
  2116
		selection.unselect();
insilmaril@721
  2117
	/* FIXME Note:  does saveStateRemovingPart work for MCO? (No parent!)
insilmaril@721
  2118
		saveStateRemovingPart (bo, QString ("Delete %1").arg(getObjectName(bo)));
insilmaril@721
  2119
		*/
insilmaril@721
  2120
		bo=removeMapCenter ((MapCenterObj*)bo);
insilmaril@721
  2121
		if (bo) 
insilmaril@721
  2122
		{
insilmaril@721
  2123
			selection.select (bo);
insilmaril@721
  2124
			ensureSelectionVisible();
insilmaril@721
  2125
			selection.update();
insilmaril@721
  2126
		}	
insilmaril@721
  2127
		reposition();
insilmaril@721
  2128
		return;
insilmaril@721
  2129
	}
insilmaril@721
  2130
	if (bo && selection.type()==Selection::Branch)
insilmaril@721
  2131
	{
insilmaril@722
  2132
		//FIXME need to check if any animObj is part of Branch, remove from animated obj list then
insilmaril@722
  2133
insilmaril@722
  2134
		// missing!!!!
insilmaril@722
  2135
insilmaril@722
  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
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  2772
	} else if (com=="exportASCII")
insilmaril@721
  2773
	{
insilmaril@721
  2774
		QString fname="";
insilmaril@721
  2775
		ok=true;
insilmaril@721
  2776
		if (parser.parCount()>=1)
insilmaril@721
  2777
			// Hey, we even have a filename
insilmaril@721
  2778
			fname=parser.parString(ok,0); 
insilmaril@721
  2779
		if (!ok)
insilmaril@721
  2780
		{
insilmaril@721
  2781
			parser.setError (Aborted,"Could not read filename");
insilmaril@721
  2782
		} else
insilmaril@721
  2783
		{
insilmaril@721
  2784
				exportASCII (fname,false);
insilmaril@721
  2785
		}
insilmaril@721
  2786
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  2787
	} else if (com=="exportImage")
insilmaril@721
  2788
	{
insilmaril@721
  2789
		QString fname="";
insilmaril@721
  2790
		ok=true;
insilmaril@721
  2791
		if (parser.parCount()>=2)
insilmaril@721
  2792
			// Hey, we even have a filename
insilmaril@721
  2793
			fname=parser.parString(ok,0); 
insilmaril@721
  2794
		if (!ok)
insilmaril@721
  2795
		{
insilmaril@721
  2796
			parser.setError (Aborted,"Could not read filename");
insilmaril@721
  2797
		} else
insilmaril@721
  2798
		{
insilmaril@721
  2799
			QString format="PNG";
insilmaril@721
  2800
			if (parser.parCount()>=2)
insilmaril@721
  2801
			{
insilmaril@721
  2802
				format=parser.parString(ok,1);
insilmaril@721
  2803
			}
insilmaril@721
  2804
			exportImage (fname,false,format);
insilmaril@721
  2805
		}
insilmaril@721
  2806
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  2807
	} else if (com=="exportXHTML")
insilmaril@721
  2808
	{
insilmaril@721
  2809
		QString fname="";
insilmaril@721
  2810
		ok=true;
insilmaril@721
  2811
		if (parser.parCount()>=2)
insilmaril@721
  2812
			// Hey, we even have a filename
insilmaril@721
  2813
			fname=parser.parString(ok,1); 
insilmaril@721
  2814
		if (!ok)
insilmaril@721
  2815
		{
insilmaril@721
  2816
			parser.setError (Aborted,"Could not read filename");
insilmaril@721
  2817
		} else
insilmaril@721
  2818
		{
insilmaril@721
  2819
			exportXHTML (fname,false);
insilmaril@721
  2820
		}
insilmaril@721
  2821
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  2822
	} else if (com=="exportXML")
insilmaril@721
  2823
	{
insilmaril@721
  2824
		QString fname="";
insilmaril@721
  2825
		ok=true;
insilmaril@721
  2826
		if (parser.parCount()>=2)
insilmaril@721
  2827
			// Hey, we even have a filename
insilmaril@721
  2828
			fname=parser.parString(ok,1); 
insilmaril@721
  2829
		if (!ok)
insilmaril@721
  2830
		{
insilmaril@721
  2831
			parser.setError (Aborted,"Could not read filename");
insilmaril@721
  2832
		} else
insilmaril@721
  2833
		{
insilmaril@721
  2834
			exportXML (fname,false);
insilmaril@721
  2835
		}
insilmaril@721
  2836
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  2837
	} else if (com=="importDir")
insilmaril@721
  2838
	{
insilmaril@721
  2839
		if (selection.isEmpty())
insilmaril@721
  2840
		{
insilmaril@721
  2841
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  2842
		} else if (! selb )
insilmaril@721
  2843
		{				  
insilmaril@721
  2844
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  2845
		} else if (parser.checkParCount(1))
insilmaril@721
  2846
		{
insilmaril@721
  2847
			s=parser.parString(ok,0);
insilmaril@721
  2848
			if (ok) importDirInt(s);
insilmaril@721
  2849
		}	
insilmaril@721
  2850
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  2851
	} else if (com=="linkTo")
insilmaril@721
  2852
	{
insilmaril@721
  2853
		if (selection.isEmpty())
insilmaril@721
  2854
		{
insilmaril@721
  2855
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  2856
		} else if ( selb)
insilmaril@721
  2857
		{
insilmaril@721
  2858
			if (parser.checkParCount(4))
insilmaril@721
  2859
			{
insilmaril@721
  2860
				// 0	selectstring of parent
insilmaril@721
  2861
				// 1	num in parent (for branches)
insilmaril@721
  2862
				// 2,3	x,y of mainbranch or mapcenter
insilmaril@721
  2863
				s=parser.parString(ok,0);
insilmaril@721
  2864
				LinkableMapObj *dst=findObjBySelect (s);
insilmaril@721
  2865
				if (dst)
insilmaril@721
  2866
				{	
insilmaril@721
  2867
					if (typeid(*dst) == typeid(BranchObj) ) 
insilmaril@721
  2868
					{
insilmaril@721
  2869
						// Get number in parent
insilmaril@721
  2870
						n=parser.parInt (ok,1);
insilmaril@721
  2871
						if (ok)
insilmaril@721
  2872
						{
insilmaril@721
  2873
							selb->linkTo ((BranchObj*)(dst),n);
insilmaril@721
  2874
							selection.update();
insilmaril@721
  2875
						}	
insilmaril@721
  2876
					} else if (typeid(*dst) == typeid(MapCenterObj) ) 
insilmaril@721
  2877
					{
insilmaril@721
  2878
						selb->linkTo ((BranchObj*)(dst),-1);
insilmaril@721
  2879
						// Get coordinates of mainbranch
insilmaril@721
  2880
						x=parser.parDouble(ok,2);
insilmaril@721
  2881
						if (ok)
insilmaril@721
  2882
						{
insilmaril@721
  2883
							y=parser.parDouble(ok,3);
insilmaril@721
  2884
							if (ok) 
insilmaril@721
  2885
							{
insilmaril@721
  2886
								selb->move (x,y);
insilmaril@721
  2887
								selection.update();
insilmaril@721
  2888
							}
insilmaril@721
  2889
						}
insilmaril@721
  2890
					}	
insilmaril@721
  2891
				}	
insilmaril@721
  2892
			}	
insilmaril@721
  2893
		} else if ( selection.type() == Selection::FloatImage) 
insilmaril@721
  2894
		{
insilmaril@721
  2895
			if (parser.checkParCount(1))
insilmaril@721
  2896
			{
insilmaril@721
  2897
				// 0	selectstring of parent
insilmaril@721
  2898
				s=parser.parString(ok,0);
insilmaril@721
  2899
				LinkableMapObj *dst=findObjBySelect (s);
insilmaril@721
  2900
				if (dst)
insilmaril@721
  2901
				{	
insilmaril@721
  2902
					if (typeid(*dst) == typeid(BranchObj) ||
insilmaril@721
  2903
						typeid(*dst) == typeid(MapCenterObj)) 
insilmaril@721
  2904
						linkFloatImageTo (getSelectString(dst));
insilmaril@721
  2905
				} else	
insilmaril@721
  2906
					parser.setError (Aborted,"Destination is not a branch");
insilmaril@721
  2907
			}		
insilmaril@721
  2908
		} else
insilmaril@721
  2909
			parser.setError (Aborted,"Type of selection is not a floatimage or branch");
insilmaril@721
  2910
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  2911
	} else if (com=="loadImage")
insilmaril@721
  2912
	{
insilmaril@721
  2913
		if (selection.isEmpty())
insilmaril@721
  2914
		{
insilmaril@721
  2915
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  2916
		} else if (! selb )
insilmaril@721
  2917
		{				  
insilmaril@721
  2918
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  2919
		} else if (parser.checkParCount(1))
insilmaril@721
  2920
		{
insilmaril@721
  2921
			s=parser.parString(ok,0);
insilmaril@721
  2922
			if (ok) loadFloatImageInt (s);
insilmaril@721
  2923
		}	
insilmaril@721
  2924
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  2925
	} else if (com=="moveBranchUp")
insilmaril@721
  2926
	{
insilmaril@721
  2927
		if (selection.isEmpty() )
insilmaril@721
  2928
		{
insilmaril@721
  2929
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  2930
		} else if (! selb )
insilmaril@721
  2931
		{				  
insilmaril@721
  2932
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  2933
		} else if (parser.checkParCount(0))
insilmaril@721
  2934
		{
insilmaril@721
  2935
			moveBranchUp();
insilmaril@721
  2936
		}	
insilmaril@721
  2937
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  2938
	} else if (com=="moveBranchDown")
insilmaril@721
  2939
	{
insilmaril@721
  2940
		if (selection.isEmpty() )
insilmaril@721
  2941
		{
insilmaril@721
  2942
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  2943
		} else if (! selb )
insilmaril@721
  2944
		{				  
insilmaril@721
  2945
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  2946
		} else if (parser.checkParCount(0))
insilmaril@721
  2947
		{
insilmaril@721
  2948
			moveBranchDown();
insilmaril@721
  2949
		}	
insilmaril@721
  2950
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  2951
	} else if (com=="move")
insilmaril@721
  2952
	{
insilmaril@721
  2953
		if (selection.isEmpty() )
insilmaril@721
  2954
		{
insilmaril@721
  2955
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  2956
		} else if ( selection.type()!=Selection::Branch  && 
insilmaril@721
  2957
					selection.type()!=Selection::MapCenter  &&
insilmaril@721
  2958
					selection.type()!=Selection::FloatImage )
insilmaril@721
  2959
		{				  
insilmaril@721
  2960
			parser.setError (Aborted,"Type of selection is not a branch or floatimage");
insilmaril@721
  2961
		} else if (parser.checkParCount(2))
insilmaril@721
  2962
		{	
insilmaril@721
  2963
			x=parser.parDouble (ok,0);
insilmaril@721
  2964
			if (ok)
insilmaril@721
  2965
			{
insilmaril@721
  2966
				y=parser.parDouble (ok,1);
insilmaril@721
  2967
				if (ok) move (x,y);
insilmaril@721
  2968
			}
insilmaril@721
  2969
		}	
insilmaril@721
  2970
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  2971
	} else if (com=="moveRel")
insilmaril@721
  2972
	{
insilmaril@721
  2973
		if (selection.isEmpty() )
insilmaril@721
  2974
		{
insilmaril@721
  2975
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  2976
		} else if ( selection.type()!=Selection::Branch  && 
insilmaril@721
  2977
					selection.type()!=Selection::MapCenter  &&
insilmaril@721
  2978
					selection.type()!=Selection::FloatImage )
insilmaril@721
  2979
		{				  
insilmaril@721
  2980
			parser.setError (Aborted,"Type of selection is not a branch or floatimage");
insilmaril@721
  2981
		} else if (parser.checkParCount(2))
insilmaril@721
  2982
		{	
insilmaril@721
  2983
			x=parser.parDouble (ok,0);
insilmaril@721
  2984
			if (ok)
insilmaril@721
  2985
			{
insilmaril@721
  2986
				y=parser.parDouble (ok,1);
insilmaril@721
  2987
				if (ok) moveRel (x,y);
insilmaril@721
  2988
			}
insilmaril@721
  2989
		}	
insilmaril@721
  2990
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  2991
	} else if (com=="nop")
insilmaril@721
  2992
	{
insilmaril@721
  2993
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  2994
	} else if (com=="paste")
insilmaril@721
  2995
	{
insilmaril@721
  2996
		if (selection.isEmpty() )
insilmaril@721
  2997
		{
insilmaril@721
  2998
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  2999
		} else if (! selb )
insilmaril@721
  3000
		{				  
insilmaril@721
  3001
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3002
		} else if (parser.checkParCount(1))
insilmaril@721
  3003
		{	
insilmaril@721
  3004
			n=parser.parInt (ok,0);
insilmaril@721
  3005
			if (ok) pasteNoSave(n);
insilmaril@721
  3006
		}	
insilmaril@721
  3007
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3008
	} else if (com=="qa")
insilmaril@721
  3009
	{
insilmaril@721
  3010
		if (selection.isEmpty() )
insilmaril@721
  3011
		{
insilmaril@721
  3012
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  3013
		} else if (! selb )
insilmaril@721
  3014
		{				  
insilmaril@721
  3015
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3016
		} else if (parser.checkParCount(4))
insilmaril@721
  3017
		{	
insilmaril@721
  3018
			QString c,u;
insilmaril@721
  3019
			c=parser.parString (ok,0);
insilmaril@721
  3020
			if (!ok)
insilmaril@721
  3021
			{
insilmaril@721
  3022
				parser.setError (Aborted,"No comment given");
insilmaril@721
  3023
			} else
insilmaril@721
  3024
			{
insilmaril@721
  3025
				s=parser.parString (ok,1);
insilmaril@721
  3026
				if (!ok)
insilmaril@721
  3027
				{
insilmaril@721
  3028
					parser.setError (Aborted,"First parameter is not a string");
insilmaril@721
  3029
				} else
insilmaril@721
  3030
				{
insilmaril@721
  3031
					t=parser.parString (ok,2);
insilmaril@721
  3032
					if (!ok)
insilmaril@721
  3033
					{
insilmaril@721
  3034
						parser.setError (Aborted,"Condition is not a string");
insilmaril@721
  3035
					} else
insilmaril@721
  3036
					{
insilmaril@721
  3037
						u=parser.parString (ok,3);
insilmaril@721
  3038
						if (!ok)
insilmaril@721
  3039
						{
insilmaril@721
  3040
							parser.setError (Aborted,"Third parameter is not a string");
insilmaril@721
  3041
						} else
insilmaril@721
  3042
						{
insilmaril@721
  3043
							if (s!="heading")
insilmaril@721
  3044
							{
insilmaril@721
  3045
								parser.setError (Aborted,"Unknown type: "+s);
insilmaril@721
  3046
							} else
insilmaril@721
  3047
							{
insilmaril@721
  3048
								if (! (t=="eq") ) 
insilmaril@721
  3049
								{
insilmaril@721
  3050
									parser.setError (Aborted,"Unknown operator: "+t);
insilmaril@721
  3051
								} else
insilmaril@721
  3052
								{
insilmaril@721
  3053
									if (! selb    )
insilmaril@721
  3054
									{
insilmaril@721
  3055
										parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3056
									} else
insilmaril@721
  3057
									{
insilmaril@721
  3058
										if (selb->getHeading() == u)
insilmaril@721
  3059
										{
insilmaril@721
  3060
											cout << "PASSED: " << qPrintable (c)  << endl;
insilmaril@721
  3061
										} else
insilmaril@721
  3062
										{
insilmaril@721
  3063
											cout << "FAILED: " << qPrintable (c)  << endl;
insilmaril@721
  3064
										}
insilmaril@721
  3065
									}
insilmaril@721
  3066
								}
insilmaril@721
  3067
							}
insilmaril@721
  3068
						} 
insilmaril@721
  3069
					} 
insilmaril@721
  3070
				} 
insilmaril@721
  3071
			}
insilmaril@721
  3072
		}	
insilmaril@721
  3073
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3074
	} else if (com=="saveImage")
insilmaril@721
  3075
	{
insilmaril@721
  3076
		FloatImageObj *fio=selection.getFloatImage();
insilmaril@721
  3077
		if (!fio)
insilmaril@721
  3078
		{
insilmaril@721
  3079
			parser.setError (Aborted,"Type of selection is not an image");
insilmaril@721
  3080
		} else if (parser.checkParCount(2))
insilmaril@721
  3081
		{
insilmaril@721
  3082
			s=parser.parString(ok,0);
insilmaril@721
  3083
			if (ok)
insilmaril@721
  3084
			{
insilmaril@721
  3085
				t=parser.parString(ok,1);
insilmaril@721
  3086
				if (ok) saveFloatImageInt (fio,t,s);
insilmaril@721
  3087
			}
insilmaril@721
  3088
		}	
insilmaril@721
  3089
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3090
	} else if (com=="scroll")
insilmaril@721
  3091
	{
insilmaril@721
  3092
		if (selection.isEmpty() )
insilmaril@721
  3093
		{
insilmaril@721
  3094
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  3095
		} else if (! selb )
insilmaril@721
  3096
		{				  
insilmaril@721
  3097
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3098
		} else if (parser.checkParCount(0))
insilmaril@721
  3099
		{	
insilmaril@721
  3100
			if (!scrollBranch (selb))	
insilmaril@721
  3101
				parser.setError (Aborted,"Could not scroll branch");
insilmaril@721
  3102
		}	
insilmaril@721
  3103
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3104
	} else if (com=="select")
insilmaril@721
  3105
	{
insilmaril@721
  3106
		if (parser.checkParCount(1))
insilmaril@721
  3107
		{
insilmaril@721
  3108
			s=parser.parString(ok,0);
insilmaril@721
  3109
			if (ok) select (s);
insilmaril@721
  3110
		}	
insilmaril@721
  3111
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3112
	} else if (com=="selectLastBranch")
insilmaril@721
  3113
	{
insilmaril@721
  3114
		if (selection.isEmpty() )
insilmaril@721
  3115
		{
insilmaril@721
  3116
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  3117
		} else if (! selb )
insilmaril@721
  3118
		{				  
insilmaril@721
  3119
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3120
		} else if (parser.checkParCount(0))
insilmaril@721
  3121
		{	
insilmaril@721
  3122
			BranchObj *bo=selb->getLastBranch();
insilmaril@721
  3123
			if (!bo)
insilmaril@721
  3124
				parser.setError (Aborted,"Could not select last branch");
insilmaril@721
  3125
			selectInt (bo);	
insilmaril@721
  3126
				
insilmaril@721
  3127
		}	
insilmaril@721
  3128
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3129
	} else if (com=="selectLastImage")
insilmaril@721
  3130
	{
insilmaril@721
  3131
		if (selection.isEmpty() )
insilmaril@721
  3132
		{
insilmaril@721
  3133
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  3134
		} else if (! selb )
insilmaril@721
  3135
		{				  
insilmaril@721
  3136
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3137
		} else if (parser.checkParCount(0))
insilmaril@721
  3138
		{	
insilmaril@721
  3139
			FloatImageObj *fio=selb->getLastFloatImage();
insilmaril@721
  3140
			if (!fio)
insilmaril@721
  3141
				parser.setError (Aborted,"Could not select last image");
insilmaril@721
  3142
			selectInt (fio);	
insilmaril@721
  3143
				
insilmaril@721
  3144
		}	
insilmaril@721
  3145
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3146
	} else if (com=="selectLatestAdded")
insilmaril@721
  3147
	{
insilmaril@721
  3148
		if (latestSelectionString.isEmpty() )
insilmaril@721
  3149
		{
insilmaril@721
  3150
			parser.setError (Aborted,"No latest added object");
insilmaril@721
  3151
		} else
insilmaril@721
  3152
		{	
insilmaril@721
  3153
			if (!select (latestSelectionString))
insilmaril@721
  3154
				parser.setError (Aborted,"Could not select latest added object "+latestSelectionString);
insilmaril@721
  3155
		}	
insilmaril@721
  3156
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3157
	} else if (com=="setFrameType")
insilmaril@721
  3158
	{
insilmaril@721
  3159
		if ( selection.type()!=Selection::Branch && selection.type()!= Selection::MapCenter && selection.type()!=Selection::FloatImage)
insilmaril@721
  3160
		{
insilmaril@721
  3161
			parser.setError (Aborted,"Type of selection does not allow setting frame type");
insilmaril@721
  3162
		}
insilmaril@721
  3163
		else if (parser.checkParCount(1))
insilmaril@721
  3164
		{
insilmaril@721
  3165
			s=parser.parString(ok,0);
insilmaril@721
  3166
			if (ok) setFrameType (s);
insilmaril@721
  3167
		}	
insilmaril@721
  3168
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3169
	} else if (com=="setFramePenColor")
insilmaril@721
  3170
	{
insilmaril@721
  3171
		if ( selection.type()!=Selection::Branch && selection.type()!= Selection::MapCenter && selection.type()!=Selection::FloatImage)
insilmaril@721
  3172
		{
insilmaril@721
  3173
			parser.setError (Aborted,"Type of selection does not allow setting of pen color");
insilmaril@721
  3174
		}
insilmaril@721
  3175
		else if (parser.checkParCount(1))
insilmaril@721
  3176
		{
insilmaril@721
  3177
			QColor c=parser.parColor(ok,0);
insilmaril@721
  3178
			if (ok) setFramePenColor (c);
insilmaril@721
  3179
		}	
insilmaril@721
  3180
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3181
	} else if (com=="setFrameBrushColor")
insilmaril@721
  3182
	{
insilmaril@721
  3183
		if ( selection.type()!=Selection::Branch && selection.type()!= Selection::MapCenter && selection.type()!=Selection::FloatImage)
insilmaril@721
  3184
		{
insilmaril@721
  3185
			parser.setError (Aborted,"Type of selection does not allow setting brush color");
insilmaril@721
  3186
		}
insilmaril@721
  3187
		else if (parser.checkParCount(1))
insilmaril@721
  3188
		{
insilmaril@721
  3189
			QColor c=parser.parColor(ok,0);
insilmaril@721
  3190
			if (ok) setFrameBrushColor (c);
insilmaril@721
  3191
		}	
insilmaril@721
  3192
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3193
	} else if (com=="setFramePadding")
insilmaril@721
  3194
	{
insilmaril@721
  3195
		if ( selection.type()!=Selection::Branch && selection.type()!= Selection::MapCenter && selection.type()!=Selection::FloatImage)
insilmaril@721
  3196
		{
insilmaril@721
  3197
			parser.setError (Aborted,"Type of selection does not allow setting frame padding");
insilmaril@721
  3198
		}
insilmaril@721
  3199
		else if (parser.checkParCount(1))
insilmaril@721
  3200
		{
insilmaril@721
  3201
			n=parser.parInt(ok,0);
insilmaril@721
  3202
			if (ok) setFramePadding(n);
insilmaril@721
  3203
		}	
insilmaril@721
  3204
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3205
	} else if (com=="setFrameBorderWidth")
insilmaril@721
  3206
	{
insilmaril@721
  3207
		if ( selection.type()!=Selection::Branch && selection.type()!= Selection::MapCenter && selection.type()!=Selection::FloatImage)
insilmaril@721
  3208
		{
insilmaril@721
  3209
			parser.setError (Aborted,"Type of selection does not allow setting frame border width");
insilmaril@721
  3210
		}
insilmaril@721
  3211
		else if (parser.checkParCount(1))
insilmaril@721
  3212
		{
insilmaril@721
  3213
			n=parser.parInt(ok,0);
insilmaril@721
  3214
			if (ok) setFrameBorderWidth (n);
insilmaril@721
  3215
		}	
insilmaril@721
  3216
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3217
	} else if (com=="setMapAuthor")
insilmaril@721
  3218
	{
insilmaril@721
  3219
		if (parser.checkParCount(1))
insilmaril@721
  3220
		{
insilmaril@721
  3221
			s=parser.parString(ok,0);
insilmaril@721
  3222
			if (ok) setAuthor (s);
insilmaril@721
  3223
		}	
insilmaril@721
  3224
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3225
	} else if (com=="setMapComment")
insilmaril@721
  3226
	{
insilmaril@721
  3227
		if (parser.checkParCount(1))
insilmaril@721
  3228
		{
insilmaril@721
  3229
			s=parser.parString(ok,0);
insilmaril@721
  3230
			if (ok) setComment(s);
insilmaril@721
  3231
		}	
insilmaril@721
  3232
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3233
	} else if (com=="setMapBackgroundColor")
insilmaril@721
  3234
	{
insilmaril@721
  3235
		if (selection.isEmpty() )
insilmaril@721
  3236
		{
insilmaril@721
  3237
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  3238
		} else if (! selection.getBranch() )
insilmaril@721
  3239
		{				  
insilmaril@721
  3240
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3241
		} else if (parser.checkParCount(1))
insilmaril@721
  3242
		{
insilmaril@721
  3243
			QColor c=parser.parColor (ok,0);
insilmaril@721
  3244
			if (ok) setMapBackgroundColor (c);
insilmaril@721
  3245
		}	
insilmaril@721
  3246
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3247
	} else if (com=="setMapDefLinkColor")
insilmaril@721
  3248
	{
insilmaril@721
  3249
		if (selection.isEmpty() )
insilmaril@721
  3250
		{
insilmaril@721
  3251
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  3252
		} else if (! selb )
insilmaril@721
  3253
		{				  
insilmaril@721
  3254
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3255
		} else if (parser.checkParCount(1))
insilmaril@721
  3256
		{
insilmaril@721
  3257
			QColor c=parser.parColor (ok,0);
insilmaril@721
  3258
			if (ok) setMapDefLinkColor (c);
insilmaril@721
  3259
		}	
insilmaril@721
  3260
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3261
	} else if (com=="setMapLinkStyle")
insilmaril@721
  3262
	{
insilmaril@721
  3263
		if (parser.checkParCount(1))
insilmaril@721
  3264
		{
insilmaril@721
  3265
			s=parser.parString (ok,0);
insilmaril@721
  3266
			if (ok) setMapLinkStyle(s);
insilmaril@721
  3267
		}	
insilmaril@721
  3268
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3269
	} else if (com=="setHeading")
insilmaril@721
  3270
	{
insilmaril@721
  3271
		if (selection.isEmpty() )
insilmaril@721
  3272
		{
insilmaril@721
  3273
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  3274
		} else if (! selb )
insilmaril@721
  3275
		{				  
insilmaril@721
  3276
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3277
		} else if (parser.checkParCount(1))
insilmaril@721
  3278
		{
insilmaril@721
  3279
			s=parser.parString (ok,0);
insilmaril@721
  3280
			if (ok) 
insilmaril@721
  3281
				setHeading (s);
insilmaril@721
  3282
		}	
insilmaril@721
  3283
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3284
	} else if (com=="setHideExport")
insilmaril@721
  3285
	{
insilmaril@721
  3286
		if (selection.isEmpty() )
insilmaril@721
  3287
		{
insilmaril@721
  3288
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  3289
		} else if (selection.type()!=Selection::Branch && selection.type() != Selection::MapCenter &&selection.type()!=Selection::FloatImage)
insilmaril@721
  3290
		{				  
insilmaril@721
  3291
			parser.setError (Aborted,"Type of selection is not a branch or floatimage");
insilmaril@721
  3292
		} else if (parser.checkParCount(1))
insilmaril@721
  3293
		{
insilmaril@721
  3294
			b=parser.parBool(ok,0);
insilmaril@721
  3295
			if (ok) setHideExport (b);
insilmaril@721
  3296
		}
insilmaril@721
  3297
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3298
	} else if (com=="setIncludeImagesHorizontally")
insilmaril@721
  3299
	{ 
insilmaril@721
  3300
		if (selection.isEmpty() )
insilmaril@721
  3301
		{
insilmaril@721
  3302
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  3303
		} else if (! selb)
insilmaril@721
  3304
		{				  
insilmaril@721
  3305
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3306
		} else if (parser.checkParCount(1))
insilmaril@721
  3307
		{
insilmaril@721
  3308
			b=parser.parBool(ok,0);
insilmaril@721
  3309
			if (ok) setIncludeImagesHor(b);
insilmaril@721
  3310
		}
insilmaril@721
  3311
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3312
	} else if (com=="setIncludeImagesVertically")
insilmaril@721
  3313
	{
insilmaril@721
  3314
		if (selection.isEmpty() )
insilmaril@721
  3315
		{
insilmaril@721
  3316
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  3317
		} else if (! selb)
insilmaril@721
  3318
		{				  
insilmaril@721
  3319
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3320
		} else if (parser.checkParCount(1))
insilmaril@721
  3321
		{
insilmaril@721
  3322
			b=parser.parBool(ok,0);
insilmaril@721
  3323
			if (ok) setIncludeImagesVer(b);
insilmaril@721
  3324
		}
insilmaril@721
  3325
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3326
	} else if (com=="setHideLinkUnselected")
insilmaril@721
  3327
	{
insilmaril@721
  3328
		if (selection.isEmpty() )
insilmaril@721
  3329
		{
insilmaril@721
  3330
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  3331
		} else if ( selection.type()!=Selection::Branch && selection.type()!= Selection::MapCenter && selection.type()!=Selection::FloatImage)
insilmaril@721
  3332
		{				  
insilmaril@721
  3333
			parser.setError (Aborted,"Type of selection does not allow hiding the link");
insilmaril@721
  3334
		} else if (parser.checkParCount(1))
insilmaril@721
  3335
		{
insilmaril@721
  3336
			b=parser.parBool(ok,0);
insilmaril@721
  3337
			if (ok) setHideLinkUnselected(b);
insilmaril@721
  3338
		}
insilmaril@721
  3339
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3340
	} else if (com=="setSelectionColor")
insilmaril@721
  3341
	{
insilmaril@721
  3342
		if (parser.checkParCount(1))
insilmaril@721
  3343
		{
insilmaril@721
  3344
			QColor c=parser.parColor (ok,0);
insilmaril@721
  3345
			if (ok) setSelectionColorInt (c);
insilmaril@721
  3346
		}	
insilmaril@721
  3347
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3348
	} else if (com=="setURL")
insilmaril@721
  3349
	{
insilmaril@721
  3350
		if (selection.isEmpty() )
insilmaril@721
  3351
		{
insilmaril@721
  3352
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  3353
		} else if (! selb )
insilmaril@721
  3354
		{				  
insilmaril@721
  3355
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3356
		} else if (parser.checkParCount(1))
insilmaril@721
  3357
		{
insilmaril@721
  3358
			s=parser.parString (ok,0);
insilmaril@721
  3359
			if (ok) setURL(s);
insilmaril@721
  3360
		}	
insilmaril@721
  3361
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3362
	} else if (com=="setVymLink")
insilmaril@721
  3363
	{
insilmaril@721
  3364
		if (selection.isEmpty() )
insilmaril@721
  3365
		{
insilmaril@721
  3366
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  3367
		} else if (! selb )
insilmaril@721
  3368
		{				  
insilmaril@721
  3369
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3370
		} else if (parser.checkParCount(1))
insilmaril@721
  3371
		{
insilmaril@721
  3372
			s=parser.parString (ok,0);
insilmaril@721
  3373
			if (ok) setVymLink(s);
insilmaril@721
  3374
		}	
insilmaril@721
  3375
	}
insilmaril@721
  3376
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3377
	else if (com=="setFlag")
insilmaril@721
  3378
	{
insilmaril@721
  3379
		if (selection.isEmpty() )
insilmaril@721
  3380
		{
insilmaril@721
  3381
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  3382
		} else if (! selb )
insilmaril@721
  3383
		{				  
insilmaril@721
  3384
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3385
		} else if (parser.checkParCount(1))
insilmaril@721
  3386
		{
insilmaril@721
  3387
			s=parser.parString(ok,0);
insilmaril@721
  3388
			if (ok) 
insilmaril@721
  3389
			{
insilmaril@721
  3390
				selb->activateStandardFlag(s);
insilmaril@721
  3391
				selb->updateFlagsToolbar();
insilmaril@721
  3392
			}	
insilmaril@721
  3393
		}
insilmaril@721
  3394
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3395
	} else if (com=="setFrameType")
insilmaril@721
  3396
	{
insilmaril@721
  3397
		if (selection.isEmpty() )
insilmaril@721
  3398
		{
insilmaril@721
  3399
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  3400
		} else if (! selb )
insilmaril@721
  3401
		{				  
insilmaril@721
  3402
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3403
		} else if (parser.checkParCount(1))
insilmaril@721
  3404
		{
insilmaril@721
  3405
			s=parser.parString(ok,0);
insilmaril@721
  3406
			if (ok) 
insilmaril@721
  3407
				setFrameType (s);
insilmaril@721
  3408
		}
insilmaril@721
  3409
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3410
	} else if (com=="sortChildren")
insilmaril@721
  3411
	{
insilmaril@721
  3412
		if (selection.isEmpty() )
insilmaril@721
  3413
		{
insilmaril@721
  3414
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  3415
		} else if (! selb )
insilmaril@721
  3416
		{				  
insilmaril@721
  3417
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3418
		} else if (parser.checkParCount(0))
insilmaril@721
  3419
		{
insilmaril@721
  3420
			sortChildren();
insilmaril@721
  3421
		}
insilmaril@721
  3422
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3423
	} else if (com=="toggleFlag")
insilmaril@721
  3424
	{
insilmaril@721
  3425
		if (selection.isEmpty() )
insilmaril@721
  3426
		{
insilmaril@721
  3427
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  3428
		} else if (! selb )
insilmaril@721
  3429
		{				  
insilmaril@721
  3430
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3431
		} else if (parser.checkParCount(1))
insilmaril@721
  3432
		{
insilmaril@721
  3433
			s=parser.parString(ok,0);
insilmaril@721
  3434
			if (ok) 
insilmaril@721
  3435
			{
insilmaril@721
  3436
				selb->toggleStandardFlag(s);	
insilmaril@721
  3437
				selb->updateFlagsToolbar();
insilmaril@721
  3438
			}	
insilmaril@721
  3439
		}
insilmaril@721
  3440
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3441
	} else if (com=="unscroll")
insilmaril@721
  3442
	{
insilmaril@721
  3443
		if (selection.isEmpty() )
insilmaril@721
  3444
		{
insilmaril@721
  3445
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  3446
		} else if (! selb )
insilmaril@721
  3447
		{				  
insilmaril@721
  3448
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3449
		} else if (parser.checkParCount(0))
insilmaril@721
  3450
		{	
insilmaril@721
  3451
			if (!unscrollBranch (selb))	
insilmaril@721
  3452
				parser.setError (Aborted,"Could not unscroll branch");
insilmaril@721
  3453
		}	
insilmaril@721
  3454
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3455
	} else if (com=="unscrollChildren")
insilmaril@721
  3456
	{
insilmaril@721
  3457
		if (selection.isEmpty() )
insilmaril@721
  3458
		{
insilmaril@721
  3459
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  3460
		} else if (! selb )
insilmaril@721
  3461
		{				  
insilmaril@721
  3462
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3463
		} else if (parser.checkParCount(0))
insilmaril@721
  3464
		{	
insilmaril@721
  3465
			unscrollChildren ();
insilmaril@721
  3466
		}	
insilmaril@721
  3467
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3468
	} else if (com=="unsetFlag")
insilmaril@721
  3469
	{
insilmaril@721
  3470
		if (selection.isEmpty() )
insilmaril@721
  3471
		{
insilmaril@721
  3472
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  3473
		} else if (! selb )
insilmaril@721
  3474
		{				  
insilmaril@721
  3475
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3476
		} else if (parser.checkParCount(1))
insilmaril@721
  3477
		{
insilmaril@721
  3478
			s=parser.parString(ok,0);
insilmaril@721
  3479
			if (ok) 
insilmaril@721
  3480
			{
insilmaril@721
  3481
				selb->deactivateStandardFlag(s);
insilmaril@721
  3482
				selb->updateFlagsToolbar();
insilmaril@721
  3483
			}	
insilmaril@721
  3484
		}
insilmaril@721
  3485
	} else
insilmaril@721
  3486
		parser.setError (Aborted,"Unknown command");
insilmaril@721
  3487
insilmaril@721
  3488
	// Any errors?
insilmaril@721
  3489
	if (parser.errorLevel()==NoError)
insilmaril@721
  3490
	{
insilmaril@721
  3491
		// setChanged();  FIXME should not be called e.g. for export?!
insilmaril@721
  3492
		reposition();
insilmaril@721
  3493
	}	
insilmaril@721
  3494
	else	
insilmaril@721
  3495
	{
insilmaril@721
  3496
		// TODO Error handling
insilmaril@721
  3497
		qWarning("VymModel::parseAtom: Error!");
insilmaril@721
  3498
		qWarning(parser.errorMessage());
insilmaril@721
  3499
	} 
insilmaril@721
  3500
}
insilmaril@721
  3501
insilmaril@721
  3502
void VymModel::runScript (QString script)
insilmaril@721
  3503
{
insilmaril@721
  3504
	parser.setScript (script);
insilmaril@721
  3505
	parser.runScript();
insilmaril@721
  3506
	while (parser.next() ) 
insilmaril@721
  3507
		parseAtom(parser.getAtom());
insilmaril@721
  3508
}
insilmaril@721
  3509
insilmaril@721
  3510
void VymModel::setExportMode (bool b)
insilmaril@721
  3511
{
insilmaril@721
  3512
	// should be called before and after exports
insilmaril@721
  3513
	// depending on the settings
insilmaril@721
  3514
	if (b && settings.value("/export/useHideExport","true")=="true")
insilmaril@721
  3515
		setHideTmpMode (HideExport);
insilmaril@721
  3516
	else	
insilmaril@721
  3517
		setHideTmpMode (HideNone);
insilmaril@721
  3518
}
insilmaril@721
  3519
insilmaril@721
  3520
void VymModel::exportImage(QString fname, bool askName, QString format)
insilmaril@721
  3521
{
insilmaril@721
  3522
	if (fname=="")
insilmaril@721
  3523
	{
insilmaril@721
  3524
		fname=getMapName()+".png";
insilmaril@721
  3525
		format="PNG";
insilmaril@721
  3526
	} 	
insilmaril@721
  3527
insilmaril@721
  3528
	if (askName)
insilmaril@721
  3529
	{
insilmaril@721
  3530
		QStringList fl;
insilmaril@721
  3531
		QFileDialog *fd=new QFileDialog (NULL);
insilmaril@721
  3532
		fd->setCaption (tr("Export map as image"));
insilmaril@721
  3533
		fd->setDirectory (lastImageDir);
insilmaril@721
  3534
		fd->setFileMode(QFileDialog::AnyFile);
insilmaril@721
  3535
		fd->setFilters  (imageIO.getFilters() );
insilmaril@721
  3536
		if (fd->exec())
insilmaril@721
  3537
		{
insilmaril@721
  3538
			fl=fd->selectedFiles();
insilmaril@721
  3539
			fname=fl.first();
insilmaril@721
  3540
			format=imageIO.getType(fd->selectedFilter());
insilmaril@721
  3541
		} 
insilmaril@721
  3542
	}
insilmaril@721
  3543
insilmaril@721
  3544
	setExportMode (true);
insilmaril@721
  3545
	QPixmap pix (getPixmap());
insilmaril@721
  3546
	pix.save(fname, format);
insilmaril@721
  3547
	setExportMode (false);
insilmaril@721
  3548
}
insilmaril@721
  3549
insilmaril@721
  3550
insilmaril@721
  3551
void VymModel::exportXML(QString dir, bool askForName)
insilmaril@721
  3552
{
insilmaril@721
  3553
	if (askForName)
insilmaril@721
  3554
	{
insilmaril@721
  3555
		dir=browseDirectory(NULL,tr("Export XML to directory"));
insilmaril@721
  3556
		if (dir =="" && !reallyWriteDirectory(dir) )
insilmaril@721
  3557
		return;
insilmaril@721
  3558
	}
insilmaril@721
  3559
insilmaril@721
  3560
	// Hide stuff during export, if settings want this
insilmaril@721
  3561
	setExportMode (true);
insilmaril@721
  3562
insilmaril@721
  3563
	// Create subdirectories
insilmaril@721
  3564
	makeSubDirs (dir);
insilmaril@721
  3565
insilmaril@721
  3566
	// write to directory
insilmaril@721
  3567
	QString saveFile=saveToDir (dir,mapName+"-",true,getTotalBBox().topLeft() ,NULL);
insilmaril@721
  3568
	QFile file;
insilmaril@721
  3569
insilmaril@721
  3570
	file.setName ( dir + "/"+mapName+".xml");
insilmaril@721
  3571
	if ( !file.open( QIODevice::WriteOnly ) )
insilmaril@721
  3572
	{
insilmaril@721
  3573
		// This should neverever happen
insilmaril@721
  3574
		QMessageBox::critical (0,tr("Critical Export Error"),tr("VymModel::exportXML couldn't open %1").arg(file.name()));
insilmaril@721
  3575
		return;
insilmaril@721
  3576
	}	
insilmaril@721
  3577
insilmaril@721
  3578
	// Write it finally, and write in UTF8, no matter what 
insilmaril@721
  3579
	QTextStream ts( &file );
insilmaril@721
  3580
	ts.setEncoding (QTextStream::UnicodeUTF8);
insilmaril@721
  3581
	ts << saveFile;
insilmaril@721
  3582
	file.close();
insilmaril@721
  3583
insilmaril@721
  3584
	// Now write image, too
insilmaril@721
  3585
	exportImage (dir+"/images/"+mapName+".png",false,"PNG");
insilmaril@721
  3586
insilmaril@721
  3587
	setExportMode (false);
insilmaril@721
  3588
}
insilmaril@721
  3589
insilmaril@721
  3590
void VymModel::exportASCII(QString fname,bool askName)
insilmaril@721
  3591
{
insilmaril@721
  3592
	ExportASCII ex;
insilmaril@721
  3593
	ex.setModel (this);
insilmaril@721
  3594
	if (fname=="") 
insilmaril@721
  3595
		ex.setFile (mapName+".txt");	
insilmaril@721
  3596
	else
insilmaril@721
  3597
		ex.setFile (fname);
insilmaril@721
  3598
insilmaril@721
  3599
	if (askName)
insilmaril@721
  3600
	{
insilmaril@721
  3601
		//ex.addFilter ("TXT (*.txt)");
insilmaril@721
  3602
		ex.setDir(lastImageDir);
insilmaril@721
  3603
		//ex.setCaption(vymName+ " -" +tr("Export as ASCII")+" "+tr("(still experimental)"));
insilmaril@721
  3604
		ex.execDialog() ; 
insilmaril@721
  3605
	} 
insilmaril@721
  3606
	if (!ex.canceled())
insilmaril@721
  3607
	{
insilmaril@721
  3608
		setExportMode(true);
insilmaril@721
  3609
		ex.doExport();
insilmaril@721
  3610
		setExportMode(false);
insilmaril@721
  3611
	}
insilmaril@721
  3612
}
insilmaril@721
  3613
insilmaril@721
  3614
void VymModel::exportXHTML (const QString &dir, bool askForName)
insilmaril@721
  3615
{
insilmaril@721
  3616
			ExportXHTMLDialog dia(NULL);
insilmaril@721
  3617
			dia.setFilePath (filePath );
insilmaril@721
  3618
			dia.setMapName (mapName );
insilmaril@721
  3619
			dia.readSettings();
insilmaril@721
  3620
			if (dir!="") dia.setDir (dir);
insilmaril@721
  3621
insilmaril@721
  3622
			bool ok=true;
insilmaril@721
  3623
			
insilmaril@721
  3624
			if (askForName)
insilmaril@721
  3625
			{
insilmaril@721
  3626
				if (dia.exec()!=QDialog::Accepted) 
insilmaril@721
  3627
					ok=false;
insilmaril@721
  3628
				else	
insilmaril@721
  3629
				{
insilmaril@721
  3630
					QDir d (dia.getDir());
insilmaril@721
  3631
					// Check, if warnings should be used before overwriting
insilmaril@721
  3632
					// the output directory
insilmaril@721
  3633
					if (d.exists() && d.count()>0)
insilmaril@721
  3634
					{
insilmaril@721
  3635
						WarningDialog warn;
insilmaril@721
  3636
						warn.showCancelButton (true);
insilmaril@721
  3637
						warn.setText(QString(
insilmaril@721
  3638
							"The directory %1 is not empty.\n"
insilmaril@721
  3639
							"Do you risk to overwrite some of its contents?").arg(d.path() ));
insilmaril@721
  3640
						warn.setCaption("Warning: Directory not empty");
insilmaril@721
  3641
						warn.setShowAgainName("mainwindow/overwrite-dir-xhtml");
insilmaril@721
  3642
insilmaril@721
  3643
						if (warn.exec()!=QDialog::Accepted) ok=false;
insilmaril@721
  3644
					}
insilmaril@721
  3645
				}	
insilmaril@721
  3646
			}
insilmaril@721
  3647
insilmaril@721
  3648
			if (ok)
insilmaril@721
  3649
			{
insilmaril@721
  3650
				exportXML (dia.getDir(),false );
insilmaril@721
  3651
				dia.doExport(mapName );
insilmaril@721
  3652
				//if (dia.hasChanged()) setChanged();
insilmaril@721
  3653
			}
insilmaril@721
  3654
}
insilmaril@721
  3655
insilmaril@721
  3656
void VymModel::exportOOPresentation(const QString &fn, const QString &cf)
insilmaril@721
  3657
{
insilmaril@721
  3658
	ExportOO ex;
insilmaril@721
  3659
	ex.setFile (fn);
insilmaril@721
  3660
	ex.setModel (this);
insilmaril@721
  3661
	if (ex.setConfigFile(cf)) 
insilmaril@721
  3662
	{
insilmaril@721
  3663
		setExportMode (true);
insilmaril@721
  3664
		ex.exportPresentation();
insilmaril@721
  3665
		setExportMode (false);
insilmaril@721
  3666
	}
insilmaril@721
  3667
}
insilmaril@721
  3668
insilmaril@721
  3669
insilmaril@721
  3670
insilmaril@650
  3671
insilmaril@652
  3672
//////////////////////////////////////////////
insilmaril@652
  3673
// View related
insilmaril@652
  3674
//////////////////////////////////////////////
insilmaril@652
  3675
insilmaril@723
  3676
void VymModel::registerEditor(QWidget *me)
insilmaril@721
  3677
{
insilmaril@723
  3678
	mapEditor=(MapEditor*)me;
insilmaril@723
  3679
	for (int i=0; i<mapCenters.count(); i++)
insilmaril@723
  3680
		mapCenters.at(i)->setMapEditor(mapEditor);
insilmaril@721
  3681
}
insilmaril@721
  3682
insilmaril@721
  3683
void VymModel::unregisterEditor(QWidget *)
insilmaril@721
  3684
{
insilmaril@723
  3685
	mapEditor=NULL;
insilmaril@723
  3686
}
insilmaril@723
  3687
insilmaril@723
  3688
void VymModel::setContextPos(QPointF p)
insilmaril@723
  3689
{
insilmaril@723
  3690
	contextPos=p;
insilmaril@723
  3691
}
insilmaril@723
  3692
insilmaril@723
  3693
void VymModel::unsetContextPos()
insilmaril@723
  3694
{
insilmaril@723
  3695
	contextPos=QPointF();
insilmaril@721
  3696
}
insilmaril@721
  3697
insilmaril@721
  3698
void VymModel::updateNoteFlag()
insilmaril@721
  3699
{
insilmaril@721
  3700
	setChanged();
insilmaril@721
  3701
	BranchObj *bo=selection.getBranch();
insilmaril@721
  3702
	if (bo) 
insilmaril@721
  3703
	{
insilmaril@721
  3704
		bo->updateNoteFlag();
insilmaril@721
  3705
		mainWindow->updateActions();
insilmaril@721
  3706
	}	
insilmaril@721
  3707
}
insilmaril@721
  3708
insilmaril@652
  3709
void VymModel::updateRelPositions()
insilmaril@652
  3710
{
insilmaril@652
  3711
	for (int i=0; i<mapCenters.count(); i++)
insilmaril@652
  3712
		mapCenters.at(i)->updateRelPositions();
insilmaril@652
  3713
}
insilmaril@652
  3714
insilmaril@652
  3715
void VymModel::reposition()
insilmaril@652
  3716
{
insilmaril@652
  3717
	for (int i=0;i<mapCenters.count(); i++)
insilmaril@652
  3718
		mapCenters.at(i)->reposition();	//	for positioning heading
insilmaril@652
  3719
}
insilmaril@652
  3720
insilmaril@665
  3721
QPolygonF VymModel::shape(BranchObj *bo)
insilmaril@665
  3722
{
insilmaril@665
  3723
	// Creating (arbitrary) shapes
insilmaril@665
  3724
insilmaril@665
  3725
	QPolygonF p;
insilmaril@665
  3726
	QRectF rb=bo->getBBox();
insilmaril@665
  3727
	if (bo->getDepth()==0)
insilmaril@665
  3728
	{
insilmaril@665
  3729
		// Just take BBox of this mapCenter
insilmaril@665
  3730
		p<<rb.topLeft()<<rb.topRight()<<rb.bottomRight()<<rb.bottomLeft();
insilmaril@665
  3731
		return p;
insilmaril@665
  3732
	}
insilmaril@665
  3733
insilmaril@665
  3734
	// Take union of BBox and TotalBBox 
insilmaril@665
  3735
insilmaril@665
  3736
	QRectF ra=bo->getTotalBBox();
insilmaril@665
  3737
	if (bo->getOrientation()==LinkableMapObj::LeftOfCenter)
insilmaril@665
  3738
		p   <<ra.bottomLeft()
insilmaril@665
  3739
			<<ra.topLeft()
insilmaril@665
  3740
			<<QPointF (rb.topLeft().x(), ra.topLeft().y() )
insilmaril@665
  3741
			<<rb.topRight()
insilmaril@665
  3742
			<<rb.bottomRight()
insilmaril@665
  3743
			<<QPointF (rb.bottomLeft().x(), ra.bottomLeft().y() ) ;
insilmaril@665
  3744
	else		
insilmaril@665
  3745
		p   <<ra.bottomRight()
insilmaril@665
  3746
			<<ra.topRight()
insilmaril@665
  3747
			<<QPointF (rb.topRight().x(), ra.topRight().y() )
insilmaril@665
  3748
			<<rb.topLeft()
insilmaril@665
  3749
			<<rb.bottomLeft()
insilmaril@665
  3750
			<<QPointF (rb.bottomRight().x(), ra.bottomRight().y() ) ;
insilmaril@665
  3751
	return p;		
insilmaril@665
  3752
insilmaril@665
  3753
}
insilmaril@665
  3754
insilmaril@665
  3755
void VymModel::moveAway(LinkableMapObj *lmo)
insilmaril@665
  3756
{
insilmaril@665
  3757
	// Autolayout:
insilmaril@665
  3758
	//
insilmaril@665
  3759
	// Move all branches and MapCenters away from lmo 
insilmaril@665
  3760
	// to avoid collisions 
insilmaril@665
  3761
insilmaril@665
  3762
	QPolygonF pA;
insilmaril@665
  3763
	QPolygonF pB;
insilmaril@665
  3764
insilmaril@665
  3765
	BranchObj *boA=(BranchObj*)lmo;
insilmaril@665
  3766
	BranchObj *boB;
insilmaril@665
  3767
	for (int i=0; i<mapCenters.count(); i++)
insilmaril@665
  3768
	{
insilmaril@665
  3769
		boB=mapCenters.at(i);
insilmaril@665
  3770
		pA=shape (boA);
insilmaril@665
  3771
		pB=shape (boB);
insilmaril@665
  3772
		PolygonCollisionResult r = PolygonCollision(pA, pB, QPoint(0,0));
insilmaril@665
  3773
		cout <<"------->"
insilmaril@665
  3774
			<<"="<<r.intersect
insilmaril@665
  3775
			<<"  ("<<qPrintable(boA->getHeading() )<<")"
insilmaril@665
  3776
			<<"  with ("<< qPrintable (boB->getHeading() )
insilmaril@665
  3777
			<<")  willIntersect"
insilmaril@665
  3778
			<<r.willIntersect 
insilmaril@665
  3779
			<<"  minT="<<r.minTranslation<<endl<<endl;
insilmaril@665
  3780
	}
insilmaril@665
  3781
}
insilmaril@652
  3782
insilmaril@721
  3783
QPixmap VymModel::getPixmap()
insilmaril@721
  3784
{
insilmaril@721
  3785
	QRectF mapRect=getTotalBBox();
insilmaril@721
  3786
	QPixmap pix((int)mapRect.width()+2,(int)mapRect.height()+1);
insilmaril@721
  3787
	QPainter pp (&pix);
insilmaril@721
  3788
	
insilmaril@721
  3789
	pp.setRenderHints(mapEditor->renderHints());
insilmaril@721
  3790
insilmaril@721
  3791
	// Don't print the visualisation of selection
insilmaril@721
  3792
	selection.unselect();
insilmaril@721
  3793
insilmaril@721
  3794
	mapScene->render (	&pp, 
insilmaril@721
  3795
		QRectF(0,0,mapRect.width()+2,mapRect.height()+2),
insilmaril@721
  3796
		QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height() ));
insilmaril@721
  3797
insilmaril@721
  3798
	// Restore selection
insilmaril@721
  3799
	selection.reselect();
insilmaril@721
  3800
	
insilmaril@721
  3801
	return pix;
insilmaril@721
  3802
}
insilmaril@721
  3803
insilmaril@721
  3804
insilmaril@721
  3805
void VymModel::setMapLinkStyle (const QString & s)
insilmaril@721
  3806
{
insilmaril@721
  3807
	QString snow;
insilmaril@721
  3808
	if (linkstyle==LinkableMapObj::Line)
insilmaril@721
  3809
		snow="StyleLine";
insilmaril@721
  3810
	else if (linkstyle==LinkableMapObj::Parabel)
insilmaril@721
  3811
		snow="StyleParabel";
insilmaril@721
  3812
	else if (linkstyle==LinkableMapObj::PolyLine)
insilmaril@721
  3813
		snow="StylePolyLine";
insilmaril@721
  3814
	else if (linkstyle==LinkableMapObj::PolyParabel)
insilmaril@721
  3815
		snow="StyleParabel";
insilmaril@721
  3816
insilmaril@721
  3817
	saveState (
insilmaril@721
  3818
		QString("setMapLinkStyle (\"%1\")").arg(s),
insilmaril@721
  3819
		QString("setMapLinkStyle (\"%1\")").arg(snow),
insilmaril@721
  3820
		QString("Set map link style (\"%1\")").arg(s)
insilmaril@721
  3821
	);	
insilmaril@721
  3822
insilmaril@721
  3823
	if (s=="StyleLine")
insilmaril@721
  3824
		linkstyle=LinkableMapObj::Line;
insilmaril@721
  3825
	else if (s=="StyleParabel")
insilmaril@721
  3826
		linkstyle=LinkableMapObj::Parabel;
insilmaril@721
  3827
	else if (s=="StylePolyLine")
insilmaril@721
  3828
		linkstyle=LinkableMapObj::PolyLine;
insilmaril@721
  3829
	else	
insilmaril@721
  3830
		linkstyle=LinkableMapObj::PolyParabel;
insilmaril@721
  3831
insilmaril@721
  3832
	BranchObj *bo;
insilmaril@721
  3833
	bo=first();
insilmaril@721
  3834
	bo=next(bo);
insilmaril@721
  3835
	while (bo) 
insilmaril@721
  3836
	{
insilmaril@721
  3837
		bo->setLinkStyle(bo->getDefLinkStyle());
insilmaril@721
  3838
		bo=next(bo);
insilmaril@721
  3839
	}
insilmaril@721
  3840
	reposition();
insilmaril@721
  3841
}
insilmaril@721
  3842
insilmaril@721
  3843
LinkableMapObj::Style VymModel::getMapLinkStyle ()
insilmaril@721
  3844
{
insilmaril@721
  3845
	return linkstyle;
insilmaril@721
  3846
}	
insilmaril@721
  3847
insilmaril@721
  3848
void VymModel::setMapDefLinkColor(QColor col)
insilmaril@721
  3849
{
insilmaril@721
  3850
	if ( !col.isValid() ) return;
insilmaril@721
  3851
	saveState (
insilmaril@721
  3852
		QString("setMapDefLinkColor (\"%1\")").arg(getMapDefLinkColor().name()),
insilmaril@721
  3853
		QString("setMapDefLinkColor (\"%1\")").arg(col.name()),
insilmaril@721
  3854
		QString("Set map link color to %1").arg(col.name())
insilmaril@721
  3855
	);
insilmaril@721
  3856
insilmaril@721
  3857
	defLinkColor=col;
insilmaril@721
  3858
	BranchObj *bo;
insilmaril@721
  3859
	bo=first();
insilmaril@721
  3860
	while (bo) 
insilmaril@721
  3861
	{
insilmaril@721
  3862
		bo->setLinkColor();
insilmaril@721
  3863
		bo=next(bo);
insilmaril@721
  3864
	}
insilmaril@721
  3865
	updateActions();
insilmaril@721
  3866
}
insilmaril@721
  3867
insilmaril@721
  3868
void VymModel::setMapLinkColorHintInt()
insilmaril@721
  3869
{
insilmaril@721
  3870
	// called from setMapLinkColorHint(lch) or at end of parse
insilmaril@721
  3871
	BranchObj *bo;
insilmaril@721
  3872
	bo=first();
insilmaril@721
  3873
	while (bo) 
insilmaril@721
  3874
	{
insilmaril@721
  3875
		bo->setLinkColor();
insilmaril@721
  3876
		bo=next(bo);
insilmaril@721
  3877
	}
insilmaril@721
  3878
}
insilmaril@721
  3879
insilmaril@721
  3880
void VymModel::setMapLinkColorHint(LinkableMapObj::ColorHint lch)
insilmaril@721
  3881
{
insilmaril@721
  3882
	linkcolorhint=lch;
insilmaril@721
  3883
	setMapLinkColorHintInt();
insilmaril@721
  3884
}
insilmaril@721
  3885
insilmaril@721
  3886
void VymModel::toggleMapLinkColorHint()
insilmaril@721
  3887
{
insilmaril@721
  3888
	if (linkcolorhint==LinkableMapObj::HeadingColor)
insilmaril@721
  3889
		linkcolorhint=LinkableMapObj::DefaultColor;
insilmaril@721
  3890
	else	
insilmaril@721
  3891
		linkcolorhint=LinkableMapObj::HeadingColor;
insilmaril@721
  3892
	BranchObj *bo;
insilmaril@721
  3893
	bo=first();
insilmaril@721
  3894
	while (bo) 
insilmaril@721
  3895
	{
insilmaril@721
  3896
		bo->setLinkColor();
insilmaril@721
  3897
		bo=next(bo);
insilmaril@721
  3898
	}
insilmaril@721
  3899
}
insilmaril@721
  3900
insilmaril@721
  3901
void VymModel::selectMapBackgroundImage ()
insilmaril@721
  3902
{
insilmaril@721
  3903
	Q3FileDialog *fd=new Q3FileDialog( NULL);
insilmaril@721
  3904
	fd->setMode (Q3FileDialog::ExistingFile);
insilmaril@721
  3905
	fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
insilmaril@721
  3906
	ImagePreview *p =new ImagePreview (fd);
insilmaril@721
  3907
	fd->setContentsPreviewEnabled( TRUE );
insilmaril@721
  3908
	fd->setContentsPreview( p, p );
insilmaril@721
  3909
	fd->setPreviewMode( Q3FileDialog::Contents );
insilmaril@721
  3910
	fd->setCaption(vymName+" - " +tr("Load background image"));
insilmaril@721
  3911
	fd->setDir (lastImageDir);
insilmaril@721
  3912
	fd->show();
insilmaril@721
  3913
insilmaril@721
  3914
	if ( fd->exec() == QDialog::Accepted )
insilmaril@721
  3915
	{
insilmaril@721
  3916
		// TODO selectMapBackgroundImg in QT4 use:	lastImageDir=fd->directory();
insilmaril@721
  3917
		lastImageDir=QDir (fd->dirPath());
insilmaril@721
  3918
		setMapBackgroundImage (fd->selectedFile());
insilmaril@721
  3919
	}
insilmaril@721
  3920
}	
insilmaril@721
  3921
insilmaril@721
  3922
void VymModel::setMapBackgroundImage (const QString &fn)	//FIXME missing savestate
insilmaril@721
  3923
{
insilmaril@721
  3924
	QColor oldcol=mapScene->backgroundBrush().color();
insilmaril@721
  3925
	/*
insilmaril@721
  3926
	saveState(
insilmaril@721
  3927
		selection,
insilmaril@721
  3928
		QString ("setMapBackgroundImage (%1)").arg(oldcol.name()),
insilmaril@721
  3929
		selection,
insilmaril@721
  3930
		QString ("setMapBackgroundImage (%1)").arg(col.name()),
insilmaril@721
  3931
		QString("Set background color of map to %1").arg(col.name()));
insilmaril@721
  3932
	*/	
insilmaril@721
  3933
	QBrush brush;
insilmaril@721
  3934
	brush.setTextureImage (QPixmap (fn));
insilmaril@721
  3935
	mapScene->setBackgroundBrush(brush);
insilmaril@721
  3936
}
insilmaril@721
  3937
insilmaril@721
  3938
void VymModel::selectMapBackgroundColor()
insilmaril@721
  3939
{
insilmaril@721
  3940
	QColor col = QColorDialog::getColor( mapScene->backgroundBrush().color(), NULL);
insilmaril@721
  3941
	if ( !col.isValid() ) return;
insilmaril@721
  3942
	setMapBackgroundColor( col );
insilmaril@721
  3943
}
insilmaril@721
  3944
insilmaril@721
  3945
insilmaril@721
  3946
void VymModel::setMapBackgroundColor(QColor col)
insilmaril@721
  3947
{
insilmaril@721
  3948
	QColor oldcol=mapScene->backgroundBrush().color();
insilmaril@721
  3949
	saveState(
insilmaril@721
  3950
		QString ("setMapBackgroundColor (\"%1\")").arg(oldcol.name()),
insilmaril@721
  3951
		QString ("setMapBackgroundColor (\"%1\")").arg(col.name()),
insilmaril@721
  3952
		QString("Set background color of map to %1").arg(col.name()));
insilmaril@721
  3953
	mapScene->setBackgroundBrush(col);
insilmaril@721
  3954
}
insilmaril@721
  3955
insilmaril@721
  3956
QColor VymModel::getMapBackgroundColor()
insilmaril@721
  3957
{
insilmaril@721
  3958
    return mapScene->backgroundBrush().color();
insilmaril@721
  3959
}
insilmaril@721
  3960
insilmaril@721
  3961
insilmaril@721
  3962
LinkableMapObj::ColorHint VymModel::getMapLinkColorHint()
insilmaril@721
  3963
{
insilmaril@721
  3964
	return linkcolorhint;
insilmaril@721
  3965
}
insilmaril@721
  3966
insilmaril@721
  3967
QColor VymModel::getMapDefLinkColor()
insilmaril@721
  3968
{
insilmaril@721
  3969
	return defLinkColor;
insilmaril@721
  3970
}
insilmaril@721
  3971
insilmaril@721
  3972
void VymModel::setMapDefXLinkColor(QColor col)
insilmaril@721
  3973
{
insilmaril@721
  3974
	defXLinkColor=col;
insilmaril@721
  3975
}
insilmaril@721
  3976
insilmaril@721
  3977
QColor VymModel::getMapDefXLinkColor()
insilmaril@721
  3978
{
insilmaril@721
  3979
	return defXLinkColor;
insilmaril@721
  3980
}
insilmaril@721
  3981
insilmaril@721
  3982
void VymModel::setMapDefXLinkWidth (int w)
insilmaril@721
  3983
{
insilmaril@721
  3984
	defXLinkWidth=w;
insilmaril@721
  3985
}
insilmaril@721
  3986
insilmaril@721
  3987
int VymModel::getMapDefXLinkWidth()
insilmaril@721
  3988
{
insilmaril@721
  3989
	return defXLinkWidth;
insilmaril@721
  3990
}
insilmaril@721
  3991
insilmaril@721
  3992
void VymModel::move(const double &x, const double &y)
insilmaril@721
  3993
{
insilmaril@721
  3994
	LinkableMapObj *sel=selection.single();
insilmaril@721
  3995
	if (sel)
insilmaril@721
  3996
	{
insilmaril@721
  3997
        QPointF ap(sel->getAbsPos());
insilmaril@721
  3998
        QPointF to(x, y);
insilmaril@721
  3999
        if (ap != to)
insilmaril@721
  4000
        {
insilmaril@721
  4001
            QString ps=qpointfToString(ap);
insilmaril@721
  4002
            QString s=selection.getSelectString();
insilmaril@721
  4003
            saveState(
insilmaril@721
  4004
                s, "move "+ps, 
insilmaril@721
  4005
                s, "move "+qpointfToString(to), 
insilmaril@721
  4006
                QString("Move %1 to %2").arg(getObjectName(sel)).arg(ps));
insilmaril@721
  4007
            sel->move(x,y);
insilmaril@721
  4008
            reposition();
insilmaril@721
  4009
            selection.update();
insilmaril@721
  4010
        }
insilmaril@721
  4011
	}
insilmaril@721
  4012
}
insilmaril@721
  4013
insilmaril@721
  4014
void VymModel::moveRel (const double &x, const double &y)
insilmaril@721
  4015
{
insilmaril@721
  4016
	LinkableMapObj *sel=selection.single();
insilmaril@721
  4017
	if (sel)
insilmaril@721
  4018
	{
insilmaril@721
  4019
        QPointF rp(sel->getRelPos());
insilmaril@721
  4020
        QPointF to(x, y);
insilmaril@721
  4021
        if (rp != to)
insilmaril@721
  4022
        {
insilmaril@721
  4023
            QString ps=qpointfToString (sel->getRelPos());
insilmaril@721
  4024
            QString s=getSelectString(sel);
insilmaril@721
  4025
            saveState(
insilmaril@721
  4026
                s, "moveRel "+ps, 
insilmaril@721
  4027
                s, "moveRel "+qpointfToString(to), 
insilmaril@721
  4028
                QString("Move %1 to relative position %2").arg(getObjectName(sel)).arg(ps));
insilmaril@721
  4029
            ((OrnamentedObj*)sel)->move2RelPos (x,y);
insilmaril@721
  4030
            reposition();
insilmaril@721
  4031
            sel->updateLink();
insilmaril@721
  4032
            selection.update();
insilmaril@721
  4033
        }
insilmaril@721
  4034
	}
insilmaril@721
  4035
}
insilmaril@721
  4036
insilmaril@721
  4037
insilmaril@696
  4038
void VymModel::animate()
insilmaril@696
  4039
{
insilmaril@696
  4040
	animationTimer->stop();
insilmaril@696
  4041
	BranchObj *bo;
insilmaril@696
  4042
	int i=0;
insilmaril@696
  4043
	while (i<animObjList.size() )
insilmaril@696
  4044
	{
insilmaril@696
  4045
		bo=(BranchObj*)animObjList.at(i);
insilmaril@696
  4046
		if (!bo->animate())
insilmaril@696
  4047
		{
insilmaril@721
  4048
			if (i>=0) 
insilmaril@721
  4049
			{	
insilmaril@721
  4050
				animObjList.removeAt(i);
insilmaril@721
  4051
				i--;
insilmaril@721
  4052
			}
insilmaril@696
  4053
		}
insilmaril@696
  4054
		bo->reposition();
insilmaril@696
  4055
		i++;
insilmaril@696
  4056
	} 
insilmaril@696
  4057
	mapEditor->updateSelection();
insilmaril@696
  4058
	mapScene->update();
insilmaril@723
  4059
	if (!animObjList.isEmpty()) animationTimer->start();
insilmaril@696
  4060
}
insilmaril@696
  4061
insilmaril@696
  4062
insilmaril@723
  4063
void VymModel::startAnimation(BranchObj *bo, const QPointF &start, const QPointF &dest)
insilmaril@696
  4064
{
insilmaril@696
  4065
	if (bo && bo->getDepth()>0) 
insilmaril@696
  4066
	{
insilmaril@696
  4067
		AnimPoint ap;
insilmaril@696
  4068
		ap.setStart (start);
insilmaril@696
  4069
		ap.setDest  (dest);
insilmaril@696
  4070
		ap.setTicks (animationTicks);
insilmaril@696
  4071
		ap.setAnimated (true);
insilmaril@696
  4072
		bo->setAnimation (ap);
insilmaril@696
  4073
		animObjList.append( bo );
insilmaril@696
  4074
		animationTimer->setSingleShot (true);
insilmaril@696
  4075
		animationTimer->start(animationInterval);
insilmaril@696
  4076
	}
insilmaril@696
  4077
}
insilmaril@696
  4078
insilmaril@722
  4079
void VymModel::stopAnimation (MapObj *mo)
insilmaril@722
  4080
{
insilmaril@722
  4081
	int i=animObjList.indexOf(mo);
insilmaril@722
  4082
    if (i>=0)
insilmaril@722
  4083
		animObjList.removeAt (i);
insilmaril@722
  4084
}
insilmaril@722
  4085
insilmaril@721
  4086
void VymModel::sendSelection()
insilmaril@721
  4087
{
insilmaril@721
  4088
	if (netstate!=Server) return;
insilmaril@721
  4089
	sendData (QString("select (\"%1\")").arg(selection.getSelectString()) );
insilmaril@721
  4090
}
insilmaril@721
  4091
insilmaril@721
  4092
void VymModel::newServer()
insilmaril@721
  4093
{
insilmaril@721
  4094
	port=54321;
insilmaril@721
  4095
	sendCounter=0;
insilmaril@721
  4096
    tcpServer = new QTcpServer(this);
insilmaril@721
  4097
    if (!tcpServer->listen(QHostAddress::Any,port)) {
insilmaril@721
  4098
        QMessageBox::critical(NULL, "vym server",
insilmaril@721
  4099
                              QString("Unable to start the server: %1.").arg(tcpServer->errorString()));
insilmaril@721
  4100
        //FIXME needed? we are no widget any longer... close();
insilmaril@721
  4101
        return;
insilmaril@721
  4102
    }
insilmaril@721
  4103
	connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newClient()));
insilmaril@721
  4104
	netstate=Server;
insilmaril@721
  4105
	cout<<"Server is running on port "<<tcpServer->serverPort()<<endl;
insilmaril@721
  4106
}
insilmaril@721
  4107
insilmaril@721
  4108
void VymModel::connectToServer()
insilmaril@721
  4109
{
insilmaril@721
  4110
	port=54321;
insilmaril@721
  4111
	server="salam.suse.de";
insilmaril@721
  4112
	server="localhost";
insilmaril@721
  4113
	clientSocket = new QTcpSocket (this);
insilmaril@721
  4114
	clientSocket->abort();
insilmaril@721
  4115
    clientSocket->connectToHost(server ,port);
insilmaril@721
  4116
	connect(clientSocket, SIGNAL(readyRead()), this, SLOT(readData()));
insilmaril@721
  4117
    connect(clientSocket, SIGNAL(error(QAbstractSocket::SocketError)),
insilmaril@721
  4118
            this, SLOT(displayNetworkError(QAbstractSocket::SocketError)));
insilmaril@721
  4119
	netstate=Client;		
insilmaril@721
  4120
	cout<<"connected to "<<qPrintable (server)<<" port "<<port<<endl;
insilmaril@721
  4121
insilmaril@721
  4122
	
insilmaril@721
  4123
}
insilmaril@721
  4124
insilmaril@721
  4125
void VymModel::newClient()
insilmaril@721
  4126
{
insilmaril@721
  4127
    QTcpSocket *newClient = tcpServer->nextPendingConnection();
insilmaril@721
  4128
    connect(newClient, SIGNAL(disconnected()),
insilmaril@721
  4129
            newClient, SLOT(deleteLater()));
insilmaril@721
  4130
insilmaril@721
  4131
	cout <<"ME::newClient  at "<<qPrintable( newClient->peerAddress().toString() )<<endl;
insilmaril@721
  4132
insilmaril@721
  4133
	clientList.append (newClient);
insilmaril@721
  4134
}
insilmaril@721
  4135
insilmaril@721
  4136
insilmaril@721
  4137
void VymModel::sendData(const QString &s)
insilmaril@721
  4138
{
insilmaril@721
  4139
	if (clientList.size()==0) return;
insilmaril@721
  4140
insilmaril@721
  4141
	// Create bytearray to send
insilmaril@721
  4142
	QByteArray block;
insilmaril@721
  4143
    QDataStream out(&block, QIODevice::WriteOnly);
insilmaril@721
  4144
    out.setVersion(QDataStream::Qt_4_0);
insilmaril@721
  4145
insilmaril@721
  4146
	// Reserve some space for blocksize
insilmaril@721
  4147
    out << (quint16)0;
insilmaril@721
  4148
insilmaril@721
  4149
	// Write sendCounter
insilmaril@721
  4150
    out << sendCounter++;
insilmaril@721
  4151
insilmaril@721
  4152
	// Write data
insilmaril@721
  4153
    out << s;
insilmaril@721
  4154
insilmaril@721
  4155
	// Go back and write blocksize so far
insilmaril@721
  4156
    out.device()->seek(0);
insilmaril@721
  4157
    quint16 bs=(quint16)(block.size() - 2*sizeof(quint16));
insilmaril@721
  4158
	out << bs;
insilmaril@721
  4159
insilmaril@721
  4160
	if (debug)
insilmaril@721
  4161
		cout << "ME::sendData  bs="<<bs<<"  counter="<<sendCounter<<"  s="<<qPrintable(s)<<endl;
insilmaril@721
  4162
insilmaril@721
  4163
	for (int i=0; i<clientList.size(); ++i)
insilmaril@721
  4164
	{
insilmaril@721
  4165
		//cout << "Sending \""<<qPrintable (s)<<"\" to "<<qPrintable (clientList.at(i)->peerAddress().toString())<<endl;
insilmaril@721
  4166
		clientList.at(i)->write (block);
insilmaril@721
  4167
	}
insilmaril@721
  4168
}
insilmaril@721
  4169
insilmaril@721
  4170
void VymModel::readData ()
insilmaril@721
  4171
{
insilmaril@721
  4172
	while (clientSocket->bytesAvailable() >=(int)sizeof(quint16) )
insilmaril@721
  4173
	{
insilmaril@721
  4174
		if (debug)
insilmaril@721
  4175
			cout <<"readData  bytesAvail="<<clientSocket->bytesAvailable();
insilmaril@721
  4176
		quint16 recCounter;
insilmaril@721
  4177
		quint16 blockSize;
insilmaril@721
  4178
insilmaril@721
  4179
		QDataStream in(clientSocket);
insilmaril@721
  4180
		in.setVersion(QDataStream::Qt_4_0);
insilmaril@721
  4181
insilmaril@721
  4182
		in >> blockSize;
insilmaril@721
  4183
		in >> recCounter;
insilmaril@721
  4184
		
insilmaril@721
  4185
		QString t;
insilmaril@721
  4186
		in >>t;
insilmaril@721
  4187
		if (debug)
insilmaril@721
  4188
			cout << "  t="<<qPrintable (t)<<endl;
insilmaril@721
  4189
		parseAtom (t);
insilmaril@721
  4190
	}
insilmaril@721
  4191
	return;
insilmaril@721
  4192
}
insilmaril@721
  4193
insilmaril@721
  4194
void VymModel::displayNetworkError(QAbstractSocket::SocketError socketError)
insilmaril@721
  4195
{
insilmaril@721
  4196
    switch (socketError) {
insilmaril@721
  4197
    case QAbstractSocket::RemoteHostClosedError:
insilmaril@721
  4198
        break;
insilmaril@721
  4199
    case QAbstractSocket::HostNotFoundError:
insilmaril@721
  4200
        QMessageBox::information(NULL, vymName +" Network client",
insilmaril@721
  4201
                                 "The host was not found. Please check the "
insilmaril@721
  4202
                                    "host name and port settings.");
insilmaril@721
  4203
        break;
insilmaril@721
  4204
    case QAbstractSocket::ConnectionRefusedError:
insilmaril@721
  4205
        QMessageBox::information(NULL, vymName + " Network client",
insilmaril@721
  4206
                                 "The connection was refused by the peer. "
insilmaril@721
  4207
                                    "Make sure the fortune server is running, "
insilmaril@721
  4208
                                    "and check that the host name and port "
insilmaril@721
  4209
                                    "settings are correct.");
insilmaril@721
  4210
        break;
insilmaril@721
  4211
    default:
insilmaril@721
  4212
        QMessageBox::information(NULL, vymName + " Network client",
insilmaril@721
  4213
                                 QString("The following error occurred: %1.")
insilmaril@721
  4214
                                 .arg(clientSocket->errorString()));
insilmaril@721
  4215
    }
insilmaril@721
  4216
}
insilmaril@721
  4217
insilmaril@721
  4218
insilmaril@721
  4219
insilmaril@721
  4220
insilmaril@721
  4221
void VymModel::selectMapSelectionColor()
insilmaril@721
  4222
{
insilmaril@721
  4223
	QColor col = QColorDialog::getColor( defLinkColor, NULL);
insilmaril@721
  4224
	setSelectionColor (col);
insilmaril@721
  4225
}
insilmaril@721
  4226
insilmaril@721
  4227
void VymModel::setSelectionColorInt (QColor col)
insilmaril@721
  4228
{
insilmaril@721
  4229
	if ( !col.isValid() ) return;
insilmaril@721
  4230
	saveState (
insilmaril@721
  4231
		QString("setSelectionColor (%1)").arg(selection.getColor().name()),
insilmaril@721
  4232
		QString("setSelectionColor (%1)").arg(col.name()),
insilmaril@721
  4233
		QString("Set color of selection box to %1").arg(col.name())
insilmaril@721
  4234
	);
insilmaril@721
  4235
insilmaril@721
  4236
	selection.setColor (col);
insilmaril@721
  4237
}
insilmaril@721
  4238
insilmaril@721
  4239
void VymModel::updateSelection()
insilmaril@721
  4240
{
insilmaril@721
  4241
	selection.update();
insilmaril@721
  4242
}
insilmaril@721
  4243
insilmaril@721
  4244
void VymModel::setSelectionColor(QColor col)
insilmaril@721
  4245
{
insilmaril@721
  4246
	if ( !col.isValid() ) return;
insilmaril@721
  4247
	saveState (
insilmaril@721
  4248
		QString("setSelectionColor (%1)").arg(selection.getColor().name()),
insilmaril@721
  4249
		QString("setSelectionColor (%1)").arg(col.name()),
insilmaril@721
  4250
		QString("Set color of selection box to %1").arg(col.name())
insilmaril@721
  4251
	);
insilmaril@721
  4252
	setSelectionColorInt (col);
insilmaril@721
  4253
}
insilmaril@721
  4254
insilmaril@721
  4255
QColor VymModel::getSelectionColor()
insilmaril@721
  4256
{
insilmaril@721
  4257
	return selection.getColor();
insilmaril@721
  4258
}
insilmaril@721
  4259
insilmaril@721
  4260
void VymModel::setHideTmpMode (HideTmpMode mode)
insilmaril@721
  4261
{
insilmaril@721
  4262
	hidemode=mode;
insilmaril@721
  4263
	for (int i=0;i<mapCenters.count(); i++)
insilmaril@721
  4264
		mapCenters.at(i)->setHideTmp (mode);	
insilmaril@721
  4265
	reposition();
insilmaril@721
  4266
	// FIXME needed? scene()->update();
insilmaril@721
  4267
}
insilmaril@721
  4268
insilmaril@721
  4269
insilmaril@721
  4270
QRectF VymModel::getTotalBBox()
insilmaril@721
  4271
{
insilmaril@721
  4272
	QRectF r;
insilmaril@721
  4273
	for (int i=0;i<mapCenters.count(); i++)
insilmaril@721
  4274
		r=addBBox (mapCenters.at(i)->getTotalBBox(), r);
insilmaril@721
  4275
	return r;	
insilmaril@721
  4276
}
insilmaril@652
  4277
insilmaril@652
  4278
//////////////////////////////////////////////
insilmaril@652
  4279
// Selection related
insilmaril@652
  4280
//////////////////////////////////////////////
insilmaril@652
  4281
insilmaril@721
  4282
void VymModel::setSelectionBlocked (bool b)
insilmaril@721
  4283
{
insilmaril@721
  4284
	if (b)
insilmaril@721
  4285
		selection.block();
insilmaril@721
  4286
	else	
insilmaril@721
  4287
		selection.unblock();
insilmaril@721
  4288
}
insilmaril@721
  4289
insilmaril@721
  4290
bool VymModel::isSelectionBlocked()
insilmaril@721
  4291
{
insilmaril@721
  4292
	return selection.isBlocked();
insilmaril@721
  4293
}
insilmaril@721
  4294
insilmaril@721
  4295
bool VymModel::select (const QString &s)
insilmaril@721
  4296
{
insilmaril@721
  4297
	if (selection.select(s))
insilmaril@721
  4298
	{
insilmaril@721
  4299
		selection.update();
insilmaril@721
  4300
		ensureSelectionVisible();
insilmaril@721
  4301
		sendSelection ();
insilmaril@721
  4302
		return true;
insilmaril@721
  4303
	}
insilmaril@721
  4304
	return false;
insilmaril@721
  4305
insilmaril@721
  4306
}
insilmaril@721
  4307
insilmaril@721
  4308
bool VymModel::select (LinkableMapObj *lmo)
insilmaril@721
  4309
{
insilmaril@721
  4310
	if (selection.select(lmo))
insilmaril@721
  4311
	{
insilmaril@721
  4312
		selection.update();
insilmaril@721
  4313
		ensureSelectionVisible();
insilmaril@721
  4314
		sendSelection ();
insilmaril@721
  4315
		return true;
insilmaril@721
  4316
	}
insilmaril@721
  4317
	return false;
insilmaril@721
  4318
}
insilmaril@721
  4319
insilmaril@721
  4320
void VymModel::unselect()
insilmaril@721
  4321
{
insilmaril@721
  4322
	selection.unselect();
insilmaril@721
  4323
}	
insilmaril@721
  4324
insilmaril@721
  4325
void VymModel::reselect()
insilmaril@721
  4326
{
insilmaril@721
  4327
	selection.reselect();
insilmaril@721
  4328
}	
insilmaril@721
  4329
insilmaril@721
  4330
void VymModel::ensureSelectionVisible()
insilmaril@721
  4331
{
insilmaril@721
  4332
	LinkableMapObj *lmo=selection.single();
insilmaril@721
  4333
	if (lmo &&mapEditor) mapEditor->ensureVisible (lmo->getBBox() );
insilmaril@721
  4334
	
insilmaril@721
  4335
}
insilmaril@721
  4336
insilmaril@721
  4337
void VymModel::selectInt (LinkableMapObj *lmo)
insilmaril@721
  4338
{
insilmaril@721
  4339
	if (selection.select(lmo))
insilmaril@721
  4340
	{
insilmaril@721
  4341
		selection.update();
insilmaril@721
  4342
		sendSelection ();
insilmaril@721
  4343
	}
insilmaril@721
  4344
}
insilmaril@721
  4345
insilmaril@721
  4346
insilmaril@721
  4347
void VymModel::selectNextBranchInt()
insilmaril@721
  4348
{
insilmaril@721
  4349
	// Increase number of branch
insilmaril@721
  4350
	LinkableMapObj *sel=selection.single();
insilmaril@721
  4351
	if (sel)
insilmaril@721
  4352
	{
insilmaril@721
  4353
		QString s=selection.getSelectString();
insilmaril@721
  4354
		QString part;
insilmaril@721
  4355
		QString typ;
insilmaril@721
  4356
		QString num;
insilmaril@721
  4357
insilmaril@721
  4358
		// Where am I? 
insilmaril@721
  4359
		part=s.section(",",-1);
insilmaril@721
  4360
		typ=part.left (3);
insilmaril@721
  4361
		num=part.right(part.length() - 3);
insilmaril@721
  4362
insilmaril@721
  4363
		s=s.left (s.length() -num.length());
insilmaril@721
  4364
insilmaril@721
  4365
		// Go to next lmo
insilmaril@721
  4366
		num=QString ("%1").arg(num.toUInt()+1);
insilmaril@721
  4367
insilmaril@721
  4368
		s=s+num;
insilmaril@721
  4369
		
insilmaril@721
  4370
		// Try to select this one
insilmaril@721
  4371
		if (select (s)) return;
insilmaril@721
  4372
insilmaril@721
  4373
		// We have no direct successor, 
insilmaril@721
  4374
		// try to increase the parental number in order to
insilmaril@721
  4375
		// find a successor with same depth
insilmaril@721
  4376
insilmaril@721
  4377
		int d=selection.single()->getDepth();
insilmaril@721
  4378
		int oldDepth=d;
insilmaril@721
  4379
		int i;
insilmaril@721
  4380
		bool found=false;
insilmaril@721
  4381
		bool b;
insilmaril@721
  4382
		while (!found && d>0)
insilmaril@721
  4383
		{
insilmaril@721
  4384
			s=s.section (",",0,d-1);
insilmaril@721
  4385
			// replace substring of current depth in s with "1"
insilmaril@721
  4386
			part=s.section(",",-1);
insilmaril@721
  4387
			typ=part.left (3);
insilmaril@721
  4388
			num=part.right(part.length() - 3);
insilmaril@721
  4389
insilmaril@721
  4390
			if (d>1)
insilmaril@721
  4391
			{	
insilmaril@721
  4392
				// increase number of parent
insilmaril@721
  4393
				num=QString ("%1").arg(num.toUInt()+1);
insilmaril@721
  4394
				s=s.section (",",0,d-2) + ","+ typ+num;
insilmaril@721
  4395
			} else
insilmaril@721
  4396
			{
insilmaril@721
  4397
				// Special case, look at orientation
insilmaril@721
  4398
				if (selection.single()->getOrientation()==LinkableMapObj::RightOfCenter)
insilmaril@721
  4399
					num=QString ("%1").arg(num.toUInt()+1);
insilmaril@721
  4400
				else	
insilmaril@721
  4401
					num=QString ("%1").arg(num.toUInt()-1);
insilmaril@721
  4402
				s=typ+num;
insilmaril@721
  4403
			}	
insilmaril@721
  4404
insilmaril@721
  4405
			if (select (s))
insilmaril@721
  4406
				// pad to oldDepth, select the first branch for each depth
insilmaril@721
  4407
				for (i=d;i<oldDepth;i++)
insilmaril@721
  4408
				{
insilmaril@721
  4409
					b=select (s);
insilmaril@721
  4410
					if (b)
insilmaril@721
  4411
					{	
insilmaril@721
  4412
						if ( selection.getBranch()->countBranches()>0)
insilmaril@721
  4413
							s+=",bo:0";
insilmaril@721
  4414
						else	
insilmaril@721
  4415
							break;
insilmaril@721
  4416
					} else
insilmaril@721
  4417
						break;
insilmaril@721
  4418
				}	
insilmaril@721
  4419
insilmaril@721
  4420
			// try to select the freshly built string
insilmaril@721
  4421
			found=select(s);
insilmaril@721
  4422
			d--;
insilmaril@721
  4423
		}
insilmaril@721
  4424
		return;
insilmaril@721
  4425
	}	
insilmaril@721
  4426
}
insilmaril@721
  4427
insilmaril@721
  4428
void VymModel::selectPrevBranchInt()
insilmaril@721
  4429
{
insilmaril@721
  4430
	// Decrease number of branch
insilmaril@721
  4431
	BranchObj *bo=selection.getBranch();
insilmaril@721
  4432
	if (bo)
insilmaril@721
  4433
	{
insilmaril@721
  4434
		QString s=selection.getSelectString();
insilmaril@721
  4435
		QString part;
insilmaril@721
  4436
		QString typ;
insilmaril@721
  4437
		QString num;
insilmaril@721
  4438
insilmaril@721
  4439
		// Where am I? 
insilmaril@721
  4440
		part=s.section(",",-1);
insilmaril@721
  4441
		typ=part.left (3);
insilmaril@721
  4442
		num=part.right(part.length() - 3);
insilmaril@721
  4443
insilmaril@721
  4444
		s=s.left (s.length() -num.length());
insilmaril@721
  4445
insilmaril@721
  4446
		int n=num.toInt()-1;
insilmaril@721
  4447
		
insilmaril@721
  4448
		// Go to next lmo
insilmaril@721
  4449
		num=QString ("%1").arg(n);
insilmaril@721
  4450
		s=s+num;
insilmaril@721
  4451
		
insilmaril@721
  4452
		// Try to select this one
insilmaril@721
  4453
		if (n>=0 && select (s)) return;
insilmaril@721
  4454
insilmaril@721
  4455
		// We have no direct precessor, 
insilmaril@721
  4456
		// try to decrease the parental number in order to
insilmaril@721
  4457
		// find a precessor with same depth
insilmaril@721
  4458
insilmaril@721
  4459
		int d=selection.single()->getDepth();
insilmaril@721
  4460
		int oldDepth=d;
insilmaril@721
  4461
		int i;
insilmaril@721
  4462
		bool found=false;
insilmaril@721
  4463
		bool b;
insilmaril@721
  4464
		while (!found && d>0)
insilmaril@721
  4465
		{
insilmaril@721
  4466
			s=s.section (",",0,d-1);
insilmaril@721
  4467
			// replace substring of current depth in s with "1"
insilmaril@721
  4468
			part=s.section(",",-1);
insilmaril@721
  4469
			typ=part.left (3);
insilmaril@721
  4470
			num=part.right(part.length() - 3);
insilmaril@721
  4471
insilmaril@721
  4472
			if (d>1)
insilmaril@721
  4473
			{
insilmaril@721
  4474
				// decrease number of parent
insilmaril@721
  4475
				num=QString ("%1").arg(num.toInt()-1);
insilmaril@721
  4476
				s=s.section (",",0,d-2) + ","+ typ+num;
insilmaril@721
  4477
			} else
insilmaril@721
  4478
			{
insilmaril@721
  4479
				// Special case, look at orientation
insilmaril@721
  4480
				if (selection.single()->getOrientation()==LinkableMapObj::RightOfCenter)
insilmaril@721
  4481
					num=QString ("%1").arg(num.toInt()-1);
insilmaril@721
  4482
				else	
insilmaril@721
  4483
					num=QString ("%1").arg(num.toInt()+1);
insilmaril@721
  4484
				s=typ+num;
insilmaril@721
  4485
			}	
insilmaril@721
  4486
insilmaril@721
  4487
			if (select(s))
insilmaril@721
  4488
				// pad to oldDepth, select the last branch for each depth
insilmaril@721
  4489
				for (i=d;i<oldDepth;i++)
insilmaril@721
  4490
				{
insilmaril@721
  4491
					b=select (s);
insilmaril@721
  4492
					if (b)
insilmaril@721
  4493
						if ( selection.getBranch()->countBranches()>0)
insilmaril@721
  4494
							s+=",bo:"+ QString ("%1").arg( selection.getBranch()->countBranches()-1 );
insilmaril@721
  4495
						else	
insilmaril@721
  4496
							break;
insilmaril@721
  4497
					else
insilmaril@721
  4498
						break;
insilmaril@721
  4499
				}	
insilmaril@721
  4500
			
insilmaril@721
  4501
			// try to select the freshly built string
insilmaril@721
  4502
			found=select(s);
insilmaril@721
  4503
			d--;
insilmaril@721
  4504
		}
insilmaril@721
  4505
		return;
insilmaril@721
  4506
	}	
insilmaril@721
  4507
}
insilmaril@721
  4508
insilmaril@721
  4509
void VymModel::selectUpperBranch()
insilmaril@721
  4510
{
insilmaril@721
  4511
	if (selection.isBlocked() ) return;
insilmaril@721
  4512
insilmaril@721
  4513
	BranchObj *bo=selection.getBranch();
insilmaril@721
  4514
	if (bo && selection.type()==Selection::Branch)
insilmaril@721
  4515
	{
insilmaril@721
  4516
		if (bo->getOrientation()==LinkableMapObj::RightOfCenter)
insilmaril@721
  4517
			selectPrevBranchInt();
insilmaril@721
  4518
		else
insilmaril@721
  4519
			if (bo->getDepth()==1)
insilmaril@721
  4520
				selectNextBranchInt();
insilmaril@721
  4521
			else
insilmaril@721
  4522
				selectPrevBranchInt();
insilmaril@721
  4523
	}
insilmaril@721
  4524
}
insilmaril@721
  4525
insilmaril@721
  4526
void VymModel::selectLowerBranch()
insilmaril@721
  4527
{
insilmaril@721
  4528
	if (selection.isBlocked() ) return;
insilmaril@721
  4529
insilmaril@721
  4530
	BranchObj *bo=selection.getBranch();
insilmaril@721
  4531
	if (bo && selection.type()==Selection::Branch)
insilmaril@721
  4532
	{
insilmaril@721
  4533
		if (bo->getOrientation()==LinkableMapObj::RightOfCenter)
insilmaril@721
  4534
			selectNextBranchInt();
insilmaril@721
  4535
		else
insilmaril@721
  4536
			if (bo->getDepth()==1)
insilmaril@721
  4537
				selectPrevBranchInt();
insilmaril@721
  4538
			else
insilmaril@721
  4539
				selectNextBranchInt();
insilmaril@721
  4540
	}			
insilmaril@721
  4541
}
insilmaril@721
  4542
insilmaril@721
  4543
insilmaril@721
  4544
void VymModel::selectLeftBranch()
insilmaril@721
  4545
{
insilmaril@721
  4546
	if (selection.isBlocked() ) return;
insilmaril@721
  4547
insilmaril@721
  4548
	BranchObj* bo;
insilmaril@721
  4549
	BranchObj* par;
insilmaril@721
  4550
	LinkableMapObj *sel=selection.single();
insilmaril@721
  4551
	if (sel)
insilmaril@721
  4552
	{
insilmaril@721
  4553
		if (selection.type()== Selection::MapCenter)
insilmaril@721
  4554
		{
insilmaril@721
  4555
			par=selection.getBranch();
insilmaril@721
  4556
			bo=par->getLastSelectedBranch();
insilmaril@721
  4557
			if (bo)
insilmaril@721
  4558
			{
insilmaril@721
  4559
				// Workaround for reselecting on left and right side
insilmaril@721
  4560
				if (bo->getOrientation()==LinkableMapObj::RightOfCenter)
insilmaril@721
  4561
					bo=par->getLastBranch();
insilmaril@721
  4562
				if (bo)
insilmaril@721
  4563
				{
insilmaril@721
  4564
					bo=par->getLastBranch();
insilmaril@721
  4565
					selection.select(bo);
insilmaril@721
  4566
					selection.update();
insilmaril@721
  4567
					ensureSelectionVisible();
insilmaril@721
  4568
					sendSelection();
insilmaril@721
  4569
				}
insilmaril@721
  4570
			}	
insilmaril@721
  4571
		} else
insilmaril@721
  4572
		{
insilmaril@721
  4573
			par=(BranchObj*)(sel->getParObj());
insilmaril@721
  4574
			if (sel->getOrientation()==LinkableMapObj::RightOfCenter)
insilmaril@721
  4575
			{
insilmaril@721
  4576
				if (selection.type() == Selection::Branch ||
insilmaril@721
  4577
					selection.type() == Selection::FloatImage)
insilmaril@721
  4578
				{
insilmaril@721
  4579
					selection.select(par);
insilmaril@721
  4580
					selection.update();
insilmaril@721
  4581
					ensureSelectionVisible();
insilmaril@721
  4582
					sendSelection();
insilmaril@721
  4583
				}
insilmaril@721
  4584
			} else
insilmaril@721
  4585
			{
insilmaril@721
  4586
				if (selection.type() == Selection::Branch )
insilmaril@721
  4587
				{
insilmaril@721
  4588
					bo=selection.getBranch()->getLastSelectedBranch();
insilmaril@721
  4589
					if (bo) 
insilmaril@721
  4590
					{
insilmaril@721
  4591
						selection.select(bo);
insilmaril@721
  4592
						selection.update();
insilmaril@721
  4593
						ensureSelectionVisible();
insilmaril@721
  4594
					sendSelection();
insilmaril@721
  4595
					}
insilmaril@721
  4596
				}
insilmaril@721
  4597
			}
insilmaril@721
  4598
		}	
insilmaril@721
  4599
	}
insilmaril@721
  4600
}
insilmaril@721
  4601
insilmaril@721
  4602
void VymModel::selectRightBranch()
insilmaril@721
  4603
{
insilmaril@721
  4604
	if (selection.isBlocked() ) return;
insilmaril@721
  4605
insilmaril@721
  4606
	BranchObj* bo;
insilmaril@721
  4607
	BranchObj* par;
insilmaril@721
  4608
	LinkableMapObj *sel=selection.single();
insilmaril@721
  4609
	if (sel)
insilmaril@721
  4610
	{
insilmaril@721
  4611
		if (selection.type()==Selection::MapCenter) 
insilmaril@721
  4612
		{
insilmaril@721
  4613
			par=selection.getBranch();
insilmaril@721
  4614
			bo=par->getLastSelectedBranch();
insilmaril@721
  4615
			if (bo)
insilmaril@721
  4616
			{
insilmaril@721
  4617
				// Workaround for reselecting on left and right side
insilmaril@721
  4618
				if (bo->getOrientation()==LinkableMapObj::LeftOfCenter)
insilmaril@721
  4619
					bo=par->getFirstBranch();
insilmaril@721
  4620
				if (bo)
insilmaril@721
  4621
				{
insilmaril@721
  4622
					selection.select(bo);
insilmaril@721
  4623
					selection.update();
insilmaril@721
  4624
					ensureSelectionVisible();
insilmaril@721
  4625
					sendSelection();
insilmaril@721
  4626
				}
insilmaril@721
  4627
			}
insilmaril@721
  4628
		} else
insilmaril@721
  4629
		{
insilmaril@721
  4630
			par=(BranchObj*)(selection.single()->getParObj());
insilmaril@721
  4631
			if (selection.single()->getOrientation()==LinkableMapObj::LeftOfCenter)
insilmaril@721
  4632
			{
insilmaril@721
  4633
				if (selection.type() == Selection::Branch ||
insilmaril@721
  4634
					selection.type() == Selection::FloatImage)
insilmaril@721
  4635
				{
insilmaril@721
  4636
					selection.select(par);
insilmaril@721
  4637
					selection.update();
insilmaril@721
  4638
					ensureSelectionVisible();
insilmaril@721
  4639
					sendSelection();
insilmaril@721
  4640
				}
insilmaril@721
  4641
			} else
insilmaril@721
  4642
			{
insilmaril@721
  4643
				if (selection.type()  == Selection::Branch) 
insilmaril@721
  4644
				{
insilmaril@721
  4645
					bo=selection.getBranch()->getLastSelectedBranch();
insilmaril@721
  4646
					if (bo) 
insilmaril@721
  4647
					{
insilmaril@721
  4648
						selection.select(bo);
insilmaril@721
  4649
						selection.update();
insilmaril@721
  4650
						ensureSelectionVisible();
insilmaril@721
  4651
					sendSelection();
insilmaril@721
  4652
					}
insilmaril@721
  4653
				}
insilmaril@721
  4654
			}
insilmaril@721
  4655
		}
insilmaril@721
  4656
	}
insilmaril@721
  4657
}
insilmaril@721
  4658
insilmaril@721
  4659
void VymModel::selectFirstBranch()
insilmaril@721
  4660
{
insilmaril@721
  4661
	BranchObj *bo1=selection.getBranch();
insilmaril@721
  4662
	BranchObj *bo2;
insilmaril@721
  4663
	BranchObj* par;
insilmaril@721
  4664
	if (bo1)
insilmaril@721
  4665
	{
insilmaril@721
  4666
		par=(BranchObj*)(bo1->getParObj());
insilmaril@721
  4667
		if (!par) return;
insilmaril@721
  4668
		bo2=par->getFirstBranch();
insilmaril@721
  4669
		if (bo2) {
insilmaril@721
  4670
			selection.select(bo2);
insilmaril@721
  4671
			selection.update();
insilmaril@721
  4672
			ensureSelectionVisible();
insilmaril@721
  4673
			sendSelection();
insilmaril@721
  4674
		}
insilmaril@721
  4675
	}		
insilmaril@721
  4676
}
insilmaril@721
  4677
insilmaril@721
  4678
void VymModel::selectLastBranch()
insilmaril@721
  4679
{
insilmaril@721
  4680
	BranchObj *bo1=selection.getBranch();
insilmaril@721
  4681
	BranchObj *bo2;
insilmaril@721
  4682
	BranchObj* par;
insilmaril@721
  4683
	if (bo1)
insilmaril@721
  4684
	{
insilmaril@721
  4685
		par=(BranchObj*)(bo1->getParObj());
insilmaril@721
  4686
		if (!par) return;
insilmaril@721
  4687
		bo2=par->getLastBranch();
insilmaril@721
  4688
		if (bo2) 
insilmaril@721
  4689
		{
insilmaril@721
  4690
			selection.select(bo2);
insilmaril@721
  4691
			selection.update();
insilmaril@721
  4692
			ensureSelectionVisible();
insilmaril@721
  4693
			sendSelection();
insilmaril@721
  4694
		}
insilmaril@721
  4695
	}		
insilmaril@721
  4696
}
insilmaril@721
  4697
insilmaril@721
  4698
Selection::Type VymModel::selectionType()
insilmaril@721
  4699
{
insilmaril@721
  4700
	return selection.type();
insilmaril@721
  4701
}
insilmaril@721
  4702
insilmaril@652
  4703
LinkableMapObj* VymModel::getSelection()
insilmaril@652
  4704
{
insilmaril@721
  4705
	return selection.single();	
insilmaril@652
  4706
}
insilmaril@652
  4707
BranchObj* VymModel::getSelectedBranch()
insilmaril@652
  4708
{
insilmaril@721
  4709
	return selection.getBranch();	
insilmaril@652
  4710
}
insilmaril@652
  4711
insilmaril@721
  4712
FloatImageObj* VymModel::getSelectedFloatImage()
insilmaril@652
  4713
{
insilmaril@721
  4714
	return selection.getFloatImage();	
insilmaril@721
  4715
}
insilmaril@721
  4716
insilmaril@721
  4717
QString VymModel::getSelectString ()
insilmaril@721
  4718
{
insilmaril@721
  4719
	return selection.getSelectString();
insilmaril@652
  4720
}
insilmaril@652
  4721
insilmaril@652
  4722
QString VymModel::getSelectString (LinkableMapObj *lmo)
insilmaril@652
  4723
{
insilmaril@652
  4724
	QString s;
insilmaril@652
  4725
	if (!lmo) return s;
insilmaril@652
  4726
	if (typeid(*lmo)==typeid(BranchObj) ||
insilmaril@652
  4727
		typeid(*lmo)==typeid(MapCenterObj) )
insilmaril@652
  4728
	{	
insilmaril@652
  4729
		LinkableMapObj *par=lmo->getParObj();
insilmaril@652
  4730
		if (par)
insilmaril@652
  4731
		{
insilmaril@652
  4732
			if (lmo->getDepth() ==1)
insilmaril@652
  4733
				// Mainbranch, return 
insilmaril@652
  4734
				s= "bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
insilmaril@652
  4735
			else	
insilmaril@652
  4736
				// Branch, call myself recursively
insilmaril@652
  4737
				s= getSelectString(par) + ",bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
insilmaril@652
  4738
		} else
insilmaril@652
  4739
		{
insilmaril@652
  4740
			// MapCenter
insilmaril@652
  4741
			int i=mapCenters.indexOf ((MapCenterObj*)lmo);
insilmaril@652
  4742
			if (i>=0) s=QString("mc:%1").arg(i);
insilmaril@652
  4743
		}	
insilmaril@652
  4744
	}	
insilmaril@652
  4745
	return s;
insilmaril@652
  4746
}
insilmaril@652
  4747