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