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