vymmodel.cpp
author insilmaril
Fri, 02 Oct 2009 14:31:03 +0000
changeset 803 338ebdc9b947
parent 801 16a8ef1d82b2
child 804 14f2b1b15242
permissions -rw-r--r--
Re-added support for Bugzilla URL and fixed missing autocolor of new branch
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@795
   643
void VymModel::addMapReplaceInt(const QString &undoSel, const QString &path)	
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
insilmaril@721
  1081
	blockSaveState=blockSaveStateOrg;
insilmaril@721
  1082
insilmaril@721
  1083
	undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
insilmaril@721
  1084
	undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
insilmaril@721
  1085
	undoSet.setEntry ("/history/curStep",QString::number(curStep));
insilmaril@721
  1086
	undoSet.writeSettings(histPath);
insilmaril@721
  1087
insilmaril@721
  1088
	mainWindow->updateHistory (undoSet);
insilmaril@721
  1089
	updateActions();
insilmaril@721
  1090
insilmaril@721
  1091
	/* TODO remove testing
insilmaril@721
  1092
	cout << "ME::redo() end\n";
insilmaril@721
  1093
	cout << "    undosAvail="<<undosAvail<<endl;
insilmaril@721
  1094
	cout << "    redosAvail="<<redosAvail<<endl;
insilmaril@721
  1095
	cout << "       curStep="<<curStep<<endl;
insilmaril@721
  1096
	cout << "    ---------------------------"<<endl<<endl;
insilmaril@721
  1097
	*/
insilmaril@721
  1098
insilmaril@721
  1099
insilmaril@721
  1100
}
insilmaril@721
  1101
insilmaril@721
  1102
bool VymModel::isRedoAvailable()
insilmaril@721
  1103
{
insilmaril@721
  1104
	if (undoSet.readNumEntry("/history/redosAvail",0)>0)
insilmaril@721
  1105
		return true;
insilmaril@721
  1106
	else	
insilmaril@721
  1107
		return false;
insilmaril@721
  1108
}
insilmaril@721
  1109
insilmaril@721
  1110
void VymModel::undo()
insilmaril@721
  1111
{
insilmaril@721
  1112
	// Can we undo at all?
insilmaril@721
  1113
	if (undosAvail<1) return;
insilmaril@721
  1114
insilmaril@721
  1115
	mainWindow->statusMessage (tr("Autosave disabled during undo."));
insilmaril@721
  1116
insilmaril@721
  1117
	bool blockSaveStateOrg=blockSaveState;
insilmaril@721
  1118
	blockSaveState=true;
insilmaril@721
  1119
	
insilmaril@721
  1120
	QString undoCommand=  undoSet.readEntry (QString("/history/step-%1/undoCommand").arg(curStep));
insilmaril@721
  1121
	QString undoSelection=undoSet.readEntry (QString("/history/step-%1/undoSelection").arg(curStep));
insilmaril@721
  1122
	QString redoCommand=  undoSet.readEntry (QString("/history/step-%1/redoCommand").arg(curStep));
insilmaril@721
  1123
	QString redoSelection=undoSet.readEntry (QString("/history/step-%1/redoSelection").arg(curStep));
insilmaril@721
  1124
	QString comment=undoSet.readEntry (QString("/history/step-%1/comment").arg(curStep));
insilmaril@721
  1125
	QString version=undoSet.readEntry ("/history/version");
insilmaril@721
  1126
insilmaril@721
  1127
	/* TODO Maybe check for version, if we save the history
insilmaril@721
  1128
	if (!checkVersion(version))
insilmaril@721
  1129
		QMessageBox::warning(0,tr("Warning"),
insilmaril@721
  1130
			tr("Version %1 of saved undo/redo data\ndoes not match current vym version %2.").arg(version).arg(vymVersion));
insilmaril@721
  1131
	*/
insilmaril@721
  1132
insilmaril@721
  1133
	// Find out current undo directory
insilmaril@721
  1134
	QString bakMapDir(QString(tmpMapDir+"/undo-%1").arg(curStep));
insilmaril@721
  1135
insilmaril@721
  1136
	// select  object before undo
insilmaril@794
  1137
	if (!select (undoSelection))
insilmaril@794
  1138
	{
insilmaril@794
  1139
		qWarning ("VymModel::undo()  Could not select object for undo");
insilmaril@794
  1140
		return;
insilmaril@794
  1141
	}
insilmaril@721
  1142
insilmaril@721
  1143
	if (debug)
insilmaril@721
  1144
	{
insilmaril@721
  1145
		cout << "VymModel::undo() begin\n";
insilmaril@721
  1146
		cout << "    undosAvail="<<undosAvail<<endl;
insilmaril@721
  1147
		cout << "    redosAvail="<<redosAvail<<endl;
insilmaril@721
  1148
		cout << "       curStep="<<curStep<<endl;
insilmaril@721
  1149
		cout << "    ---------------------------"<<endl;
insilmaril@721
  1150
		cout << "    comment="<<comment.toStdString()<<endl;
insilmaril@721
  1151
		cout << "    undoCom="<<undoCommand.toStdString()<<endl;
insilmaril@721
  1152
		cout << "    undoSel="<<undoSelection.toStdString()<<endl;
insilmaril@721
  1153
		cout << "    redoCom="<<redoCommand.toStdString()<<endl;
insilmaril@721
  1154
		cout << "    redoSel="<<redoSelection.toStdString()<<endl;
insilmaril@721
  1155
		cout << "    ---------------------------"<<endl<<endl;
insilmaril@721
  1156
	}	
insilmaril@721
  1157
	parseAtom (undoCommand);
insilmaril@721
  1158
insilmaril@721
  1159
	undosAvail--;
insilmaril@721
  1160
	curStep--; 
insilmaril@721
  1161
	if (curStep<1) curStep=stepsTotal;
insilmaril@721
  1162
insilmaril@721
  1163
	redosAvail++;
insilmaril@721
  1164
insilmaril@721
  1165
	blockSaveState=blockSaveStateOrg;
insilmaril@788
  1166
/* testing only
insilmaril@721
  1167
	cout << "VymModel::undo() end\n";
insilmaril@721
  1168
	cout << "    undosAvail="<<undosAvail<<endl;
insilmaril@721
  1169
	cout << "    redosAvail="<<redosAvail<<endl;
insilmaril@721
  1170
	cout << "       curStep="<<curStep<<endl;
insilmaril@721
  1171
	cout << "    ---------------------------"<<endl<<endl;
insilmaril@721
  1172
*/
insilmaril@721
  1173
insilmaril@721
  1174
	undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
insilmaril@721
  1175
	undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
insilmaril@721
  1176
	undoSet.setEntry ("/history/curStep",QString::number(curStep));
insilmaril@721
  1177
	undoSet.writeSettings(histPath);
insilmaril@721
  1178
insilmaril@721
  1179
	mainWindow->updateHistory (undoSet);
insilmaril@721
  1180
	updateActions();
insilmaril@795
  1181
	//emitSelectionChanged();
insilmaril@721
  1182
}
insilmaril@721
  1183
insilmaril@721
  1184
bool VymModel::isUndoAvailable()
insilmaril@721
  1185
{
insilmaril@721
  1186
	if (undoSet.readNumEntry("/history/undosAvail",0)>0)
insilmaril@721
  1187
		return true;
insilmaril@721
  1188
	else	
insilmaril@721
  1189
		return false;
insilmaril@721
  1190
}
insilmaril@721
  1191
insilmaril@721
  1192
void VymModel::gotoHistoryStep (int i)
insilmaril@721
  1193
{
insilmaril@721
  1194
	// Restore variables
insilmaril@721
  1195
	int undosAvail=undoSet.readNumEntry (QString("/history/undosAvail"));
insilmaril@721
  1196
	int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
insilmaril@721
  1197
insilmaril@721
  1198
	if (i<0) i=undosAvail+redosAvail;
insilmaril@721
  1199
insilmaril@721
  1200
	// Clicking above current step makes us undo things
insilmaril@721
  1201
	if (i<undosAvail) 
insilmaril@721
  1202
	{	
insilmaril@721
  1203
		for (int j=0; j<undosAvail-i; j++) undo();
insilmaril@721
  1204
		return;
insilmaril@721
  1205
	}	
insilmaril@721
  1206
	// Clicking below current step makes us redo things
insilmaril@721
  1207
	if (i>undosAvail) 
insilmaril@721
  1208
		for (int j=undosAvail; j<i; j++) 
insilmaril@721
  1209
		{
insilmaril@721
  1210
			if (debug) cout << "VymModel::gotoHistoryStep redo "<<j<<"/"<<undosAvail<<" i="<<i<<endl;
insilmaril@721
  1211
			redo();
insilmaril@721
  1212
		}
insilmaril@721
  1213
insilmaril@721
  1214
	// And ignore clicking the current row ;-)	
insilmaril@721
  1215
}
insilmaril@721
  1216
insilmaril@721
  1217
insilmaril@721
  1218
QString VymModel::getHistoryPath()
insilmaril@721
  1219
{
insilmaril@721
  1220
	QString histName(QString("history-%1").arg(curStep));
insilmaril@721
  1221
	return (tmpMapDir+"/"+histName);
insilmaril@721
  1222
}
insilmaril@721
  1223
insilmaril@753
  1224
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
  1225
{
insilmaril@746
  1226
	sendData(redoCom);	//FIXME-3 testing
insilmaril@721
  1227
insilmaril@721
  1228
	// Main saveState
insilmaril@721
  1229
insilmaril@721
  1230
insilmaril@721
  1231
	if (blockSaveState) return;
insilmaril@721
  1232
insilmaril@760
  1233
	if (debug) cout << "VM::saveState() for  "<<qPrintable (mapName)<<endl;
insilmaril@721
  1234
	
insilmaril@721
  1235
	// Find out current undo directory
insilmaril@721
  1236
	if (undosAvail<stepsTotal) undosAvail++;
insilmaril@721
  1237
	curStep++;
insilmaril@721
  1238
	if (curStep>stepsTotal) curStep=1;
insilmaril@721
  1239
	
insilmaril@721
  1240
	QString backupXML="";
insilmaril@721
  1241
	QString histDir=getHistoryPath();
insilmaril@721
  1242
	QString bakMapPath=histDir+"/map.xml";
insilmaril@721
  1243
insilmaril@721
  1244
	// Create histDir if not available
insilmaril@721
  1245
	QDir d(histDir);
insilmaril@721
  1246
	if (!d.exists()) 
insilmaril@721
  1247
		makeSubDirs (histDir);
insilmaril@721
  1248
insilmaril@721
  1249
	// Save depending on how much needs to be saved	
insilmaril@721
  1250
	if (saveSel)
insilmaril@721
  1251
		backupXML=saveToDir (histDir,mapName+"-",false, QPointF (),saveSel);
insilmaril@721
  1252
		
insilmaril@721
  1253
	QString undoCommand="";
insilmaril@721
  1254
	if (savemode==UndoCommand)
insilmaril@721
  1255
	{
insilmaril@721
  1256
		undoCommand=undoCom;
insilmaril@721
  1257
	}	
insilmaril@721
  1258
	else if (savemode==PartOfMap )
insilmaril@721
  1259
	{
insilmaril@721
  1260
		undoCommand=undoCom;
insilmaril@721
  1261
		undoCommand.replace ("PATH",bakMapPath);
insilmaril@721
  1262
	}
insilmaril@721
  1263
insilmaril@760
  1264
insilmaril@721
  1265
	if (!backupXML.isEmpty())
insilmaril@721
  1266
		// Write XML Data to disk
insilmaril@721
  1267
		saveStringToDisk (bakMapPath,backupXML);
insilmaril@721
  1268
insilmaril@721
  1269
	// We would have to save all actions in a tree, to keep track of 
insilmaril@721
  1270
	// possible redos after a action. Possible, but we are too lazy: forget about redos.
insilmaril@721
  1271
	redosAvail=0;
insilmaril@721
  1272
insilmaril@721
  1273
	// Write the current state to disk
insilmaril@721
  1274
	undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
insilmaril@721
  1275
	undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
insilmaril@721
  1276
	undoSet.setEntry ("/history/curStep",QString::number(curStep));
insilmaril@721
  1277
	undoSet.setEntry (QString("/history/step-%1/undoCommand").arg(curStep),undoCommand);
insilmaril@721
  1278
	undoSet.setEntry (QString("/history/step-%1/undoSelection").arg(curStep),undoSelection);
insilmaril@721
  1279
	undoSet.setEntry (QString("/history/step-%1/redoCommand").arg(curStep),redoCom);
insilmaril@721
  1280
	undoSet.setEntry (QString("/history/step-%1/redoSelection").arg(curStep),redoSelection);
insilmaril@721
  1281
	undoSet.setEntry (QString("/history/step-%1/comment").arg(curStep),comment);
insilmaril@721
  1282
	undoSet.setEntry (QString("/history/version"),vymVersion);
insilmaril@721
  1283
	undoSet.writeSettings(histPath);
insilmaril@721
  1284
insilmaril@721
  1285
	if (debug)
insilmaril@721
  1286
	{
insilmaril@721
  1287
		// TODO remove after testing
insilmaril@721
  1288
		//cout << "          into="<< histPath.toStdString()<<endl;
insilmaril@721
  1289
		cout << "    stepsTotal="<<stepsTotal<<
insilmaril@721
  1290
		", undosAvail="<<undosAvail<<
insilmaril@721
  1291
		", redosAvail="<<redosAvail<<
insilmaril@721
  1292
		", curStep="<<curStep<<endl;
insilmaril@721
  1293
		cout << "    ---------------------------"<<endl;
insilmaril@721
  1294
		cout << "    comment="<<comment.toStdString()<<endl;
insilmaril@721
  1295
		cout << "    undoCom="<<undoCommand.toStdString()<<endl;
insilmaril@721
  1296
		cout << "    undoSel="<<undoSelection.toStdString()<<endl;
insilmaril@721
  1297
		cout << "    redoCom="<<redoCom.toStdString()<<endl;
insilmaril@721
  1298
		cout << "    redoSel="<<redoSelection.toStdString()<<endl;
insilmaril@721
  1299
		if (saveSel) cout << "    saveSel="<<qPrintable (getSelectString(saveSel))<<endl;
insilmaril@721
  1300
		cout << "    ---------------------------"<<endl;
insilmaril@721
  1301
	}
insilmaril@721
  1302
insilmaril@721
  1303
	mainWindow->updateHistory (undoSet);
insilmaril@721
  1304
	setChanged();
insilmaril@721
  1305
	updateActions();
insilmaril@721
  1306
}
insilmaril@721
  1307
insilmaril@721
  1308
insilmaril@753
  1309
void VymModel::saveStateChangingPart(TreeItem *undoSel, TreeItem* redoSel, const QString &rc, const QString &comment)
insilmaril@721
  1310
{
insilmaril@721
  1311
	// save the selected part of the map, Undo will replace part of map 
insilmaril@721
  1312
	QString undoSelection="";
insilmaril@721
  1313
	if (undoSel)
insilmaril@721
  1314
		undoSelection=getSelectString(undoSel);
insilmaril@721
  1315
	else
insilmaril@721
  1316
		qWarning ("VymModel::saveStateChangingPart  no undoSel given!");
insilmaril@721
  1317
	QString redoSelection="";
insilmaril@721
  1318
	if (redoSel)
insilmaril@721
  1319
		redoSelection=getSelectString(undoSel);
insilmaril@721
  1320
	else
insilmaril@721
  1321
		qWarning ("VymModel::saveStateChangingPart  no redoSel given!");
insilmaril@721
  1322
		
insilmaril@721
  1323
insilmaril@721
  1324
	saveState (PartOfMap,
insilmaril@721
  1325
		undoSelection, "addMapReplace (\"PATH\")",
insilmaril@721
  1326
		redoSelection, rc, 
insilmaril@721
  1327
		comment, 
insilmaril@721
  1328
		undoSel);
insilmaril@721
  1329
}
insilmaril@721
  1330
insilmaril@760
  1331
void VymModel::saveStateRemovingPart(TreeItem* redoSel, const QString &comment)
insilmaril@721
  1332
{
insilmaril@721
  1333
	if (!redoSel)
insilmaril@721
  1334
	{
insilmaril@721
  1335
		qWarning ("VymModel::saveStateRemovingPart  no redoSel given!");
insilmaril@721
  1336
		return;
insilmaril@721
  1337
	}
insilmaril@788
  1338
	QString undoSelection;
insilmaril@721
  1339
	QString redoSelection=getSelectString(redoSel);
insilmaril@753
  1340
	if (redoSel->getType()==TreeItem::Branch) 
insilmaril@721
  1341
	{
insilmaril@788
  1342
		undoSelection=getSelectString (redoSel->parent());
insilmaril@721
  1343
		// save the selected branch of the map, Undo will insert part of map 
insilmaril@721
  1344
		saveState (PartOfMap,
insilmaril@753
  1345
			undoSelection, QString("addMapInsert (\"PATH\",%1)").arg(redoSel->num()),
insilmaril@721
  1346
			redoSelection, "delete ()", 
insilmaril@721
  1347
			comment, 
insilmaril@721
  1348
			redoSel);
insilmaril@721
  1349
	}
insilmaril@788
  1350
	if (redoSel->getType()==TreeItem::MapCenter) 
insilmaril@788
  1351
	{
insilmaril@788
  1352
		// save the selected branch of the map, Undo will insert part of map 
insilmaril@788
  1353
		saveState (PartOfMap,
insilmaril@788
  1354
			undoSelection, QString("addMapInsert (\"PATH\")"),
insilmaril@788
  1355
			redoSelection, "delete ()", 
insilmaril@788
  1356
			comment, 
insilmaril@788
  1357
			redoSel);
insilmaril@788
  1358
	}
insilmaril@721
  1359
}
insilmaril@721
  1360
insilmaril@721
  1361
insilmaril@753
  1362
void VymModel::saveState(TreeItem *undoSel, const QString &uc, TreeItem *redoSel, const QString &rc, const QString &comment) 
insilmaril@721
  1363
{
insilmaril@721
  1364
	// "Normal" savestate: save commands, selections and comment
insilmaril@721
  1365
	// so just save commands for undo and redo
insilmaril@721
  1366
	// and use current selection
insilmaril@721
  1367
insilmaril@721
  1368
	QString redoSelection="";
insilmaril@721
  1369
	if (redoSel) redoSelection=getSelectString(redoSel);
insilmaril@721
  1370
	QString undoSelection="";
insilmaril@721
  1371
	if (undoSel) undoSelection=getSelectString(undoSel);
insilmaril@721
  1372
insilmaril@721
  1373
	saveState (UndoCommand,
insilmaril@721
  1374
		undoSelection, uc,
insilmaril@721
  1375
		redoSelection, rc, 
insilmaril@721
  1376
		comment, 
insilmaril@721
  1377
		NULL);
insilmaril@721
  1378
}
insilmaril@721
  1379
insilmaril@721
  1380
void VymModel::saveState(const QString &undoSel, const QString &uc, const QString &redoSel, const QString &rc, const QString &comment) 
insilmaril@721
  1381
{
insilmaril@721
  1382
	// "Normal" savestate: save commands, selections and comment
insilmaril@721
  1383
	// so just save commands for undo and redo
insilmaril@721
  1384
	// and use current selection
insilmaril@721
  1385
	saveState (UndoCommand,
insilmaril@721
  1386
		undoSel, uc,
insilmaril@721
  1387
		redoSel, rc, 
insilmaril@721
  1388
		comment, 
insilmaril@721
  1389
		NULL);
insilmaril@721
  1390
}
insilmaril@721
  1391
insilmaril@721
  1392
void VymModel::saveState(const QString &uc, const QString &rc, const QString &comment) 
insilmaril@721
  1393
{
insilmaril@721
  1394
	// "Normal" savestate applied to model (no selection needed): 
insilmaril@721
  1395
	// save commands  and comment
insilmaril@721
  1396
	saveState (UndoCommand,
insilmaril@721
  1397
		NULL, uc,
insilmaril@721
  1398
		NULL, rc, 
insilmaril@721
  1399
		comment, 
insilmaril@721
  1400
		NULL);
insilmaril@721
  1401
}
insilmaril@721
  1402
insilmaril@721
  1403
insilmaril@650
  1404
QGraphicsScene* VymModel::getScene ()
insilmaril@650
  1405
{
insilmaril@650
  1406
	return mapScene;
insilmaril@650
  1407
}
insilmaril@650
  1408
insilmaril@761
  1409
TreeItem* VymModel::findBySelectString(QString s)
insilmaril@761
  1410
{
insilmaril@761
  1411
	if (s.isEmpty() ) return NULL;
insilmaril@761
  1412
insilmaril@761
  1413
	// Old maps don't have multiple mapcenters and don't save full path
insilmaril@761
  1414
	if (s.left(2) !="mc")
insilmaril@761
  1415
		s="mc:0,"+s;
insilmaril@761
  1416
insilmaril@761
  1417
	QStringList parts=s.split (",");
insilmaril@761
  1418
	QString typ;
insilmaril@761
  1419
	int n;
insilmaril@761
  1420
	TreeItem *ti=rootItem;
insilmaril@761
  1421
insilmaril@761
  1422
	while (!parts.isEmpty() )
insilmaril@650
  1423
	{
insilmaril@761
  1424
		typ=parts.first().left(2);
insilmaril@761
  1425
		n=parts.first().right(parts.first().length() - 3).toInt();
insilmaril@761
  1426
		parts.removeFirst();
insilmaril@761
  1427
		if (typ=="mc" || typ=="bo")
insilmaril@761
  1428
			ti=ti->getBranchNum (n);
insilmaril@794
  1429
		else if (typ=="fi")
insilmaril@794
  1430
			ti=ti->getImageNum (n);
insilmaril@794
  1431
		else if (typ=="ai")
insilmaril@794
  1432
			ti=ti->getAttributeNum (n);
insilmaril@794
  1433
		else if (typ=="xl")
insilmaril@794
  1434
			ti=ti->getXLinkNum (n);
insilmaril@791
  1435
		if(!ti) return NULL;		
insilmaril@761
  1436
	}
insilmaril@761
  1437
	return  ti;
insilmaril@650
  1438
}
insilmaril@650
  1439
insilmaril@795
  1440
TreeItem* VymModel::findID (const QString &s)	//FIXME-4 Search also other types...
insilmaril@754
  1441
{
insilmaril@790
  1442
	BranchItem *cur=NULL;
insilmaril@790
  1443
	BranchItem *prev=NULL;
insilmaril@790
  1444
	next(cur,prev);
insilmaril@790
  1445
	while (cur) 
insilmaril@650
  1446
	{
insilmaril@790
  1447
		if (s==cur->getID() ) return cur;
insilmaril@790
  1448
		next(cur,prev);
insilmaril@790
  1449
	}
insilmaril@650
  1450
	return NULL;
insilmaril@650
  1451
}
insilmaril@650
  1452
insilmaril@721
  1453
//////////////////////////////////////////////
insilmaril@721
  1454
// Interface 
insilmaril@721
  1455
//////////////////////////////////////////////
insilmaril@721
  1456
void VymModel::setVersion (const QString &s)
insilmaril@721
  1457
{
insilmaril@721
  1458
	version=s;
insilmaril@721
  1459
}
insilmaril@721
  1460
insilmaril@721
  1461
void VymModel::setAuthor (const QString &s)
insilmaril@721
  1462
{
insilmaril@721
  1463
	saveState (
insilmaril@721
  1464
		QString ("setMapAuthor (\"%1\")").arg(author),
insilmaril@721
  1465
		QString ("setMapAuthor (\"%1\")").arg(s),
insilmaril@721
  1466
		QString ("Set author of map to \"%1\"").arg(s)
insilmaril@721
  1467
	);
insilmaril@721
  1468
	author=s;
insilmaril@721
  1469
}
insilmaril@721
  1470
insilmaril@721
  1471
QString VymModel::getAuthor()
insilmaril@721
  1472
{
insilmaril@721
  1473
	return author;
insilmaril@721
  1474
}
insilmaril@721
  1475
insilmaril@721
  1476
void VymModel::setComment (const QString &s)
insilmaril@721
  1477
{
insilmaril@721
  1478
	saveState (
insilmaril@721
  1479
		QString ("setMapComment (\"%1\")").arg(comment),
insilmaril@721
  1480
		QString ("setMapComment (\"%1\")").arg(s),
insilmaril@721
  1481
		QString ("Set comment of map")
insilmaril@721
  1482
	);
insilmaril@721
  1483
	comment=s;
insilmaril@721
  1484
}
insilmaril@721
  1485
insilmaril@721
  1486
QString VymModel::getComment ()
insilmaril@721
  1487
{
insilmaril@721
  1488
	return comment;
insilmaril@721
  1489
}
insilmaril@721
  1490
insilmaril@721
  1491
QString VymModel::getDate ()
insilmaril@721
  1492
{
insilmaril@721
  1493
	return QDate::currentDate().toString ("yyyy-MM-dd");
insilmaril@721
  1494
}
insilmaril@721
  1495
insilmaril@776
  1496
int VymModel::branchCount()	// FIXME-4 Optimize this: use internal counter instead of going through whole map each time...
insilmaril@742
  1497
{
insilmaril@742
  1498
	int c=0;
insilmaril@750
  1499
	BranchItem *cur=NULL;
insilmaril@750
  1500
	BranchItem *prev=NULL;
insilmaril@773
  1501
	next(cur,prev);
insilmaril@742
  1502
	while (cur) 
insilmaril@742
  1503
	{
insilmaril@742
  1504
		c++;
insilmaril@773
  1505
		next(cur,prev);
insilmaril@742
  1506
	}
insilmaril@742
  1507
	return c;
insilmaril@742
  1508
}
insilmaril@742
  1509
insilmaril@800
  1510
void VymModel::setSortFilter (const QString &s)
insilmaril@800
  1511
{
insilmaril@800
  1512
	sortFilter=s;
insilmaril@800
  1513
	emit (sortFilterChanged (sortFilter));
insilmaril@800
  1514
}
insilmaril@800
  1515
insilmaril@800
  1516
QString VymModel::getSortFilter ()
insilmaril@800
  1517
{
insilmaril@800
  1518
	return sortFilter;
insilmaril@800
  1519
}
insilmaril@800
  1520
insilmaril@721
  1521
void VymModel::setHeading(const QString &s)
insilmaril@721
  1522
{
insilmaril@787
  1523
	BranchItem *selbi=getSelectedBranch();
insilmaril@753
  1524
	if (selbi)
insilmaril@721
  1525
	{
insilmaril@721
  1526
		saveState(
insilmaril@753
  1527
			selbi,
insilmaril@753
  1528
			"setHeading (\""+selbi->getHeading()+"\")", 
insilmaril@753
  1529
			selbi,
insilmaril@721
  1530
			"setHeading (\""+s+"\")", 
insilmaril@753
  1531
			QString("Set heading of %1 to \"%2\"").arg(getObjectName(selbi)).arg(s) );
insilmaril@753
  1532
		selbi->setHeading(s );
insilmaril@767
  1533
		emitDataHasChanged ( selbi);	//FIXME-3 maybe emit signal from TreeItem? 
insilmaril@721
  1534
		reposition();
insilmaril@775
  1535
		emitSelectionChanged();
insilmaril@721
  1536
	}
insilmaril@721
  1537
}
insilmaril@721
  1538
insilmaril@784
  1539
QString VymModel::getHeading()
insilmaril@784
  1540
{
insilmaril@784
  1541
	TreeItem *selti=getSelectedItem();
insilmaril@784
  1542
	if (selti)
insilmaril@784
  1543
		return selti->getHeading();
insilmaril@784
  1544
	else	
insilmaril@784
  1545
		return QString();
insilmaril@784
  1546
}
insilmaril@784
  1547
insilmaril@753
  1548
BranchItem* VymModel::findText (QString s, bool cs)   
insilmaril@721
  1549
{
insilmaril@721
  1550
	QTextDocument::FindFlags flags=0;
insilmaril@721
  1551
	if (cs) flags=QTextDocument::FindCaseSensitively;
insilmaril@721
  1552
insilmaril@741
  1553
	if (!findCurrent) 
insilmaril@721
  1554
	{	// Nothing found or new find process
insilmaril@721
  1555
		if (EOFind)
insilmaril@721
  1556
			// nothing found, start again
insilmaril@721
  1557
			EOFind=false;
insilmaril@741
  1558
		findCurrent=NULL;	
insilmaril@741
  1559
		findPrevious=NULL;	
insilmaril@773
  1560
		next (findCurrent,findPrevious);
insilmaril@721
  1561
	}	
insilmaril@721
  1562
	bool searching=true;
insilmaril@721
  1563
	bool foundNote=false;
insilmaril@721
  1564
	while (searching && !EOFind)
insilmaril@721
  1565
	{
insilmaril@741
  1566
		if (findCurrent)
insilmaril@721
  1567
		{
insilmaril@721
  1568
			// Searching in Note
insilmaril@741
  1569
			if (findCurrent->getNote().contains(s,cs))
insilmaril@721
  1570
			{
insilmaril@741
  1571
				select (findCurrent);
insilmaril@741
  1572
				/*
insilmaril@735
  1573
				if (getSelectedBranch()!=itFind) 
insilmaril@721
  1574
				{
insilmaril@735
  1575
					select(itFind);
insilmaril@763
  1576
					emitShowSelection();
insilmaril@721
  1577
				}
insilmaril@741
  1578
				*/
insilmaril@721
  1579
				if (textEditor->findText(s,flags)) 
insilmaril@721
  1580
				{
insilmaril@721
  1581
					searching=false;
insilmaril@721
  1582
					foundNote=true;
insilmaril@721
  1583
				}	
insilmaril@721
  1584
			}
insilmaril@721
  1585
			// Searching in Heading
insilmaril@741
  1586
			if (searching && findCurrent->getHeading().contains (s,cs) ) 
insilmaril@721
  1587
			{
insilmaril@741
  1588
				select(findCurrent);
insilmaril@721
  1589
				searching=false;
insilmaril@721
  1590
			}
insilmaril@721
  1591
		}	
insilmaril@721
  1592
		if (!foundNote)
insilmaril@721
  1593
		{
insilmaril@773
  1594
			if (!next(findCurrent,findPrevious) )
insilmaril@741
  1595
				EOFind=true;
insilmaril@721
  1596
		}
insilmaril@721
  1597
	//cout <<"still searching...  "<<qPrintable( itFind->getHeading())<<endl;
insilmaril@721
  1598
	}	
insilmaril@721
  1599
	if (!searching)
insilmaril@787
  1600
		return getSelectedBranch();
insilmaril@721
  1601
	else
insilmaril@721
  1602
		return NULL;
insilmaril@721
  1603
}
insilmaril@721
  1604
insilmaril@721
  1605
void VymModel::findReset()
insilmaril@721
  1606
{	// Necessary if text to find changes during a find process
insilmaril@741
  1607
	findCurrent=NULL;
insilmaril@741
  1608
	findPrevious=NULL;
insilmaril@721
  1609
	EOFind=false;
insilmaril@721
  1610
}
insilmaril@721
  1611
insilmaril@721
  1612
void VymModel::setScene (QGraphicsScene *s)
insilmaril@721
  1613
{
insilmaril@795
  1614
	mapScene=s;	
insilmaril@721
  1615
}
insilmaril@721
  1616
insilmaril@767
  1617
void VymModel::setURL(const QString &url) 
insilmaril@767
  1618
{
insilmaril@767
  1619
	TreeItem *selti=getSelectedItem();
insilmaril@767
  1620
	if (selti)
insilmaril@721
  1621
	{
insilmaril@767
  1622
		QString oldurl=selti->getURL();
insilmaril@767
  1623
		selti->setURL (url);
insilmaril@721
  1624
		saveState (
insilmaril@767
  1625
			selti,
insilmaril@721
  1626
			QString ("setURL (\"%1\")").arg(oldurl),
insilmaril@767
  1627
			selti,
insilmaril@721
  1628
			QString ("setURL (\"%1\")").arg(url),
insilmaril@767
  1629
			QString ("set URL of %1 to %2").arg(getObjectName(selti)).arg(url)
insilmaril@721
  1630
		);
insilmaril@721
  1631
		reposition();
insilmaril@767
  1632
		emitDataHasChanged (selti);
insilmaril@763
  1633
		emitShowSelection();
insilmaril@721
  1634
	}
insilmaril@721
  1635
}	
insilmaril@721
  1636
insilmaril@767
  1637
QString VymModel::getURL()	
insilmaril@767
  1638
{
insilmaril@767
  1639
	TreeItem *selti=getSelectedItem();
insilmaril@767
  1640
	if (selti)
insilmaril@767
  1641
		return selti->getURL();
insilmaril@767
  1642
	else	
insilmaril@767
  1643
		return QString();
insilmaril@721
  1644
}
insilmaril@721
  1645
insilmaril@771
  1646
QStringList VymModel::getURLs()	
insilmaril@771
  1647
{
insilmaril@721
  1648
	QStringList urls;
insilmaril@787
  1649
	BranchItem *selbi=getSelectedBranch();
insilmaril@773
  1650
	BranchItem *cur=selbi;
insilmaril@771
  1651
	BranchItem *prev=NULL;
insilmaril@771
  1652
	while (cur) 
insilmaril@771
  1653
	{
insilmaril@771
  1654
		if (!cur->getURL().isEmpty()) urls.append( cur->getURL());
insilmaril@773
  1655
		cur=next (cur,prev,selbi);
insilmaril@721
  1656
	}	
insilmaril@721
  1657
	return urls;
insilmaril@721
  1658
}
insilmaril@721
  1659
insilmaril@777
  1660
insilmaril@777
  1661
void VymModel::setFrameType(const FrameObj::FrameType &t)	//FIXME-4 not saved if there is no LMO
insilmaril@777
  1662
{
insilmaril@787
  1663
	BranchItem *bi=getSelectedBranch();
insilmaril@777
  1664
	if (bi)
insilmaril@721
  1665
	{
insilmaril@777
  1666
		BranchObj *bo=(BranchObj*)(bi->getLMO());
insilmaril@777
  1667
		if (bo)
insilmaril@777
  1668
		{
insilmaril@777
  1669
			QString s=bo->getFrameTypeName();
insilmaril@777
  1670
			bo->setFrameType (t);
insilmaril@777
  1671
			saveState (bi, QString("setFrameType (\"%1\")").arg(s),
insilmaril@777
  1672
				bi, QString ("setFrameType (\"%1\")").arg(bo->getFrameTypeName()),QString ("set type of frame to %1").arg(s));
insilmaril@777
  1673
			reposition();
insilmaril@779
  1674
			bo->updateLinkGeometry();
insilmaril@721
  1675
		}
insilmaril@721
  1676
	}
insilmaril@721
  1677
}
insilmaril@721
  1678
insilmaril@777
  1679
void VymModel::setFrameType(const QString &s)	//FIXME-4 not saved if there is no LMO
insilmaril@777
  1680
{
insilmaril@787
  1681
	BranchItem *bi=getSelectedBranch();
insilmaril@777
  1682
	if (bi)
insilmaril@777
  1683
	{
insilmaril@777
  1684
		BranchObj *bo=(BranchObj*)(bi->getLMO());
insilmaril@777
  1685
		if (bo)
insilmaril@777
  1686
		{
insilmaril@777
  1687
			saveState (bi, QString("setFrameType (\"%1\")").arg(bo->getFrameTypeName()),
insilmaril@777
  1688
				bi, QString ("setFrameType (\"%1\")").arg(s),QString ("set type of frame to %1").arg(s));
insilmaril@777
  1689
			bo->setFrameType (s);
insilmaril@777
  1690
			reposition();
insilmaril@779
  1691
			bo->updateLinkGeometry();
insilmaril@777
  1692
		}
insilmaril@777
  1693
	}
insilmaril@777
  1694
}
insilmaril@777
  1695
insilmaril@777
  1696
void VymModel::setFramePenColor(const QColor &c)	//FIXME-4 not saved if there is no LMO
insilmaril@777
  1697
insilmaril@777
  1698
{
insilmaril@787
  1699
	BranchItem *bi=getSelectedBranch();
insilmaril@777
  1700
	if (bi)
insilmaril@777
  1701
	{
insilmaril@777
  1702
		BranchObj *bo=(BranchObj*)(bi->getLMO());
insilmaril@777
  1703
		if (bo)
insilmaril@777
  1704
		{
insilmaril@777
  1705
			saveState (bi, QString("setFramePenColor (\"%1\")").arg(bo->getFramePenColor().name() ),
insilmaril@777
  1706
				bi, QString ("setFramePenColor (\"%1\")").arg(c.name() ),QString ("set pen color of frame to %1").arg(c.name() ));
insilmaril@777
  1707
			bo->setFramePenColor (c);
insilmaril@777
  1708
		}	
insilmaril@777
  1709
	}	
insilmaril@777
  1710
}
insilmaril@777
  1711
insilmaril@777
  1712
void VymModel::setFrameBrushColor(const QColor &c)	//FIXME-4 not saved if there is no LMO
insilmaril@777
  1713
{
insilmaril@787
  1714
	BranchItem *bi=getSelectedBranch();
insilmaril@777
  1715
	if (bi)
insilmaril@777
  1716
	{
insilmaril@777
  1717
		BranchObj *bo=(BranchObj*)(bi->getLMO());
insilmaril@777
  1718
		if (bo)
insilmaril@777
  1719
		{
insilmaril@777
  1720
			saveState (bi, QString("setFrameBrushColor (\"%1\")").arg(bo->getFrameBrushColor().name() ),
insilmaril@777
  1721
				bi, QString ("setFrameBrushColor (\"%1\")").arg(c.name() ),QString ("set brush color of frame to %1").arg(c.name() ));
insilmaril@777
  1722
			bo->setFrameBrushColor (c);
insilmaril@777
  1723
		}	
insilmaril@777
  1724
	}	
insilmaril@777
  1725
}
insilmaril@777
  1726
insilmaril@777
  1727
void VymModel::setFramePadding (const int &i) //FIXME-4 not saved if there is no LMO
insilmaril@777
  1728
{
insilmaril@787
  1729
	BranchItem *bi=getSelectedBranch();
insilmaril@777
  1730
	if (bi)
insilmaril@777
  1731
	{
insilmaril@777
  1732
		BranchObj *bo=(BranchObj*)(bi->getLMO());
insilmaril@777
  1733
		if (bo)
insilmaril@777
  1734
		{
insilmaril@777
  1735
			saveState (bi, QString("setFramePadding (\"%1\")").arg(bo->getFramePadding() ),
insilmaril@777
  1736
				bi, QString ("setFramePadding (\"%1\")").arg(i),QString ("set brush color of frame to %1").arg(i));
insilmaril@777
  1737
			bo->setFramePadding (i);
insilmaril@777
  1738
			reposition();
insilmaril@779
  1739
			bo->updateLinkGeometry();
insilmaril@777
  1740
		}	
insilmaril@777
  1741
	}	
insilmaril@777
  1742
}
insilmaril@777
  1743
insilmaril@777
  1744
void VymModel::setFrameBorderWidth(const int &i) //FIXME-4 not saved if there is no LMO
insilmaril@777
  1745
{
insilmaril@787
  1746
	BranchItem *bi=getSelectedBranch();
insilmaril@777
  1747
	if (bi)
insilmaril@777
  1748
	{
insilmaril@777
  1749
		BranchObj *bo=(BranchObj*)(bi->getLMO());
insilmaril@777
  1750
		if (bo)
insilmaril@777
  1751
		{
insilmaril@777
  1752
			saveState (bi, QString("setFrameBorderWidth (\"%1\")").arg(bo->getFrameBorderWidth() ),
insilmaril@777
  1753
				bi, QString ("setFrameBorderWidth (\"%1\")").arg(i),QString ("set border width of frame to %1").arg(i));
insilmaril@777
  1754
			bo->setFrameBorderWidth (i);
insilmaril@777
  1755
			reposition();
insilmaril@779
  1756
			bo->updateLinkGeometry();
insilmaril@777
  1757
		}	
insilmaril@777
  1758
	}	
insilmaril@777
  1759
}
insilmaril@777
  1760
insilmaril@785
  1761
void VymModel::setIncludeImagesVer(bool b)
insilmaril@785
  1762
{
insilmaril@787
  1763
	BranchItem *bi=getSelectedBranch();
insilmaril@785
  1764
	if (bi)
insilmaril@721
  1765
	{
insilmaril@785
  1766
		QString u= b ? "false" : "true";
insilmaril@785
  1767
		QString r=!b ? "false" : "true";
insilmaril@785
  1768
		
insilmaril@785
  1769
		saveState(
insilmaril@785
  1770
			bi,
insilmaril@785
  1771
			QString("setIncludeImagesVertically (%1)").arg(u),
insilmaril@785
  1772
			bi, 
insilmaril@785
  1773
			QString("setIncludeImagesVertically (%1)").arg(r),
insilmaril@785
  1774
			QString("Include images vertically in %1").arg(getObjectName(bi))
insilmaril@785
  1775
		);	
insilmaril@785
  1776
		bi->setIncludeImagesVer(b);
insilmaril@785
  1777
		emitDataHasChanged ( bi);	
insilmaril@785
  1778
		reposition();
insilmaril@721
  1779
	}	
insilmaril@785
  1780
}
insilmaril@785
  1781
insilmaril@785
  1782
void VymModel::setIncludeImagesHor(bool b)	
insilmaril@785
  1783
{
insilmaril@787
  1784
	BranchItem *bi=getSelectedBranch();
insilmaril@785
  1785
	if (bi)
insilmaril@721
  1786
	{
insilmaril@785
  1787
		QString u= b ? "false" : "true";
insilmaril@785
  1788
		QString r=!b ? "false" : "true";
insilmaril@785
  1789
		
insilmaril@785
  1790
		saveState(
insilmaril@785
  1791
			bi,
insilmaril@785
  1792
			QString("setIncludeImagesHorizontally (%1)").arg(u),
insilmaril@785
  1793
			bi, 
insilmaril@785
  1794
			QString("setIncludeImagesHorizontally (%1)").arg(r),
insilmaril@785
  1795
			QString("Include images horizontally in %1").arg(getObjectName(bi))
insilmaril@785
  1796
		);	
insilmaril@785
  1797
		bi->setIncludeImagesHor(b);
insilmaril@785
  1798
		emitDataHasChanged ( bi);
insilmaril@785
  1799
		reposition();
insilmaril@721
  1800
	}	
insilmaril@721
  1801
}
insilmaril@721
  1802
insilmaril@753
  1803
void VymModel::setHideLinkUnselected (bool b)//FIXME-2
insilmaril@753
  1804
{
insilmaril@779
  1805
	TreeItem *ti=getSelectedItem();
insilmaril@779
  1806
	if (ti && (ti->getType()==TreeItem::Image ||ti->isBranchLikeType()))
insilmaril@721
  1807
	{
insilmaril@721
  1808
		QString u= b ? "false" : "true";
insilmaril@721
  1809
		QString r=!b ? "false" : "true";
insilmaril@721
  1810
		
insilmaril@721
  1811
		saveState(
insilmaril@779
  1812
			ti,
insilmaril@721
  1813
			QString("setHideLinkUnselected (%1)").arg(u),
insilmaril@779
  1814
			ti, 
insilmaril@721
  1815
			QString("setHideLinkUnselected (%1)").arg(r),
insilmaril@779
  1816
			QString("Hide link of %1 if unselected").arg(getObjectName(ti))
insilmaril@721
  1817
		);	
insilmaril@779
  1818
		((MapItem*)ti)->setHideLinkUnselected(b);
insilmaril@721
  1819
	}
insilmaril@721
  1820
}
insilmaril@721
  1821
insilmaril@721
  1822
void VymModel::setHideExport(bool b)
insilmaril@721
  1823
{
insilmaril@777
  1824
	TreeItem *ti=getSelectedItem();
insilmaril@777
  1825
	if (ti && 
insilmaril@777
  1826
		(ti->getType()==TreeItem::Image ||ti->isBranchLikeType()))
insilmaril@721
  1827
	{
insilmaril@777
  1828
		ti->setHideInExport (b);
insilmaril@721
  1829
		QString u= b ? "false" : "true";
insilmaril@721
  1830
		QString r=!b ? "false" : "true";
insilmaril@721
  1831
		
insilmaril@721
  1832
		saveState(
insilmaril@777
  1833
			ti,
insilmaril@721
  1834
			QString ("setHideExport (%1)").arg(u),
insilmaril@777
  1835
			ti,
insilmaril@721
  1836
			QString ("setHideExport (%1)").arg(r),
insilmaril@777
  1837
			QString ("Set HideExport flag of %1 to %2").arg(getObjectName(ti)).arg (r)
insilmaril@721
  1838
		);	
insilmaril@777
  1839
			emitDataHasChanged(ti);
insilmaril@776
  1840
			emitSelectionChanged();
insilmaril@721
  1841
		updateActions();
insilmaril@721
  1842
		reposition();
insilmaril@775
  1843
		// emitSelectionChanged();
insilmaril@746
  1844
		// FIXME-3 VM needed? scene()->update();
insilmaril@721
  1845
	}
insilmaril@721
  1846
}
insilmaril@721
  1847
insilmaril@721
  1848
void VymModel::toggleHideExport()
insilmaril@721
  1849
{
insilmaril@776
  1850
	TreeItem *selti=getSelectedItem();
insilmaril@776
  1851
	if (selti)
insilmaril@776
  1852
		setHideExport ( !selti->hideInExport() );
insilmaril@721
  1853
}
insilmaril@721
  1854
insilmaril@721
  1855
insilmaril@767
  1856
void VymModel::copy()	
insilmaril@767
  1857
{
insilmaril@767
  1858
	TreeItem *selti=getSelectedItem();
insilmaril@767
  1859
	if (selti &&
insilmaril@767
  1860
		(selti->getType() == TreeItem::Branch || 
insilmaril@767
  1861
		selti->getType() == TreeItem::MapCenter  ||
insilmaril@767
  1862
		selti->getType() == TreeItem::Image ))
insilmaril@721
  1863
	{
insilmaril@721
  1864
		if (redosAvail == 0)
insilmaril@721
  1865
		{
insilmaril@721
  1866
			// Copy to history
insilmaril@767
  1867
			QString s=getSelectString(selti);
insilmaril@767
  1868
			saveState (PartOfMap, s, "nop ()", s, "copy ()","Copy selection to clipboard",selti  );
insilmaril@721
  1869
			curClipboard=curStep;
insilmaril@721
  1870
		}
insilmaril@721
  1871
insilmaril@721
  1872
		// Copy also to global clipboard, because we are at last step in history
insilmaril@721
  1873
		QString bakMapName(QString("history-%1").arg(curStep));
insilmaril@721
  1874
		QString bakMapDir(tmpMapDir +"/"+bakMapName);
insilmaril@721
  1875
		copyDir (bakMapDir,clipboardDir );
insilmaril@721
  1876
insilmaril@721
  1877
		clipboardEmpty=false;
insilmaril@721
  1878
		updateActions();
insilmaril@721
  1879
	}	    
insilmaril@721
  1880
}
insilmaril@721
  1881
insilmaril@721
  1882
insilmaril@767
  1883
void VymModel::pasteNoSave(const int &n)
insilmaril@767
  1884
{
insilmaril@721
  1885
	bool old=blockSaveState;
insilmaril@721
  1886
	blockSaveState=true;
insilmaril@721
  1887
	bool zippedOrg=zipped;
insilmaril@721
  1888
	if (redosAvail > 0 || n!=0)
insilmaril@721
  1889
	{
insilmaril@721
  1890
		// Use the "historical" buffer
insilmaril@721
  1891
		QString bakMapName(QString("history-%1").arg(n));
insilmaril@721
  1892
		QString bakMapDir(tmpMapDir +"/"+bakMapName);
insilmaril@721
  1893
		load (bakMapDir+"/"+clipboardFile,ImportAdd, VymMap);
insilmaril@721
  1894
	} else
insilmaril@721
  1895
		// Use the global buffer
insilmaril@721
  1896
		load (clipboardDir+"/"+clipboardFile,ImportAdd, VymMap);
insilmaril@721
  1897
	zipped=zippedOrg;
insilmaril@721
  1898
	blockSaveState=old;
insilmaril@721
  1899
}
insilmaril@721
  1900
insilmaril@767
  1901
void VymModel::paste()	
insilmaril@721
  1902
{   
insilmaril@787
  1903
	BranchItem *selbi=getSelectedBranch();
insilmaril@767
  1904
	if (selbi)
insilmaril@721
  1905
	{
insilmaril@721
  1906
		saveStateChangingPart(
insilmaril@767
  1907
			selbi,
insilmaril@767
  1908
			selbi,
insilmaril@721
  1909
			QString ("paste (%1)").arg(curClipboard),
insilmaril@767
  1910
			QString("Paste to %1").arg( getObjectName(selbi))
insilmaril@721
  1911
		);
insilmaril@721
  1912
		pasteNoSave(0);
insilmaril@721
  1913
		reposition();
insilmaril@721
  1914
	}
insilmaril@721
  1915
}
insilmaril@721
  1916
insilmaril@767
  1917
void VymModel::cut()	
insilmaril@767
  1918
{
insilmaril@767
  1919
	TreeItem *selti=getSelectedItem();
insilmaril@767
  1920
	if ( selti && (selti->isBranchLikeType() ||selti->getType()==TreeItem::Image))
insilmaril@721
  1921
	{
insilmaril@721
  1922
		copy();
insilmaril@721
  1923
		deleteSelection();
insilmaril@721
  1924
		reposition();
insilmaril@721
  1925
	}
insilmaril@767
  1926
}
insilmaril@767
  1927
insilmaril@795
  1928
bool VymModel::moveUp(BranchItem *bi)	//FIXME-2 crashes if trying to move MCO
insilmaril@795
  1929
{
insilmaril@795
  1930
	if (bi && bi->canMoveUp()) 
insilmaril@795
  1931
		return relinkBranch (bi,(BranchItem*)bi->parent(),bi->num()-1);
insilmaril@795
  1932
	else	
insilmaril@795
  1933
		return false;
insilmaril@795
  1934
}
insilmaril@795
  1935
insilmaril@795
  1936
void VymModel::moveUp()	
insilmaril@767
  1937
{
insilmaril@787
  1938
	BranchItem *selbi=getSelectedBranch();
insilmaril@767
  1939
	if (selbi)
insilmaril@721
  1940
	{
insilmaril@767
  1941
		QString oldsel=getSelectString();
insilmaril@795
  1942
		if (moveUp (selbi))
insilmaril@795
  1943
			saveState (
insilmaril@795
  1944
				getSelectString(),"moveDown ()",
insilmaril@795
  1945
				oldsel,"moveUp ()",
insilmaril@795
  1946
				QString("Move up %1").arg(getObjectName(selbi)));
insilmaril@721
  1947
	}
insilmaril@721
  1948
}
insilmaril@721
  1949
insilmaril@795
  1950
bool VymModel::moveDown(BranchItem *bi)	
insilmaril@795
  1951
{
insilmaril@795
  1952
	if (bi && bi->canMoveDown())
insilmaril@795
  1953
		return relinkBranch (bi,(BranchItem*)bi->parent(),bi->num()+1);
insilmaril@795
  1954
	else
insilmaril@795
  1955
		return false;
insilmaril@795
  1956
}
insilmaril@795
  1957
insilmaril@767
  1958
void VymModel::moveDown()	
insilmaril@767
  1959
{
insilmaril@787
  1960
	BranchItem *selbi=getSelectedBranch();
insilmaril@767
  1961
	if (selbi)
insilmaril@721
  1962
	{
insilmaril@767
  1963
		QString oldsel=getSelectString();
insilmaril@795
  1964
		if ( moveDown(selbi))
insilmaril@795
  1965
			saveState (
insilmaril@795
  1966
				getSelectString(),"moveUp ()",
insilmaril@795
  1967
				oldsel,"moveDown ()",
insilmaril@795
  1968
				QString("Move down %1").arg(getObjectName(selbi)));
insilmaril@767
  1969
	}
insilmaril@721
  1970
}
insilmaril@721
  1971
insilmaril@795
  1972
void VymModel::detach()	
insilmaril@795
  1973
{
insilmaril@795
  1974
	BranchItem *selbi=getSelectedBranch();
insilmaril@795
  1975
	if (selbi && selbi->depth()>0)
insilmaril@721
  1976
	{
insilmaril@798
  1977
		// if no relPos have been set before, try to use current rel positions   
insilmaril@798
  1978
		if (selbi->getLMO())
insilmaril@798
  1979
			for (int i=0; i<selbi->branchCount();++i)
insilmaril@798
  1980
				selbi->getBranchNum(i)->getBranchObj()->setRelPos();
insilmaril@798
  1981
		
insilmaril@795
  1982
		//QString oldsel=getSelectString();
insilmaril@795
  1983
		if ( relinkBranch (selbi,rootItem,-1) )
insilmaril@795
  1984
			saveState (
insilmaril@798
  1985
				selbi,QString("relink()"), //FIXME-1 add paramters when detaching
insilmaril@795
  1986
				selbi,"detach ()",
insilmaril@795
  1987
				QString("Detach %1").arg(getObjectName(selbi))
insilmaril@795
  1988
			);
insilmaril@795
  1989
	}
insilmaril@795
  1990
}
insilmaril@795
  1991
insilmaril@795
  1992
void VymModel::sortChildren()
insilmaril@795
  1993
{
insilmaril@795
  1994
	BranchItem* selbi=getSelectedBranch();
insilmaril@795
  1995
	if (selbi)
insilmaril@795
  1996
	{
insilmaril@795
  1997
		if(selbi->branchCount()>1)
insilmaril@721
  1998
		{
insilmaril@795
  1999
			saveStateChangingPart(
insilmaril@795
  2000
				selbi,selbi, "sortChildren ()",
insilmaril@795
  2001
				QString("Sort children of %1").arg(getObjectName(selbi)));
insilmaril@795
  2002
			selbi->sortChildren();
insilmaril@721
  2003
			reposition();
insilmaril@763
  2004
			emitShowSelection();
insilmaril@721
  2005
		}
insilmaril@721
  2006
	}
insilmaril@721
  2007
}
insilmaril@721
  2008
insilmaril@773
  2009
BranchItem* VymModel::createMapCenter()
insilmaril@773
  2010
{
insilmaril@773
  2011
	BranchItem *newbi=addMapCenter (QPointF (0,0) );
insilmaril@773
  2012
	return newbi;
insilmaril@726
  2013
}
insilmaril@726
  2014
insilmaril@775
  2015
BranchItem* VymModel::createBranch(BranchItem *dst)	
insilmaril@775
  2016
{
insilmaril@775
  2017
	if (dst)
insilmaril@775
  2018
		return addNewBranchInt (dst,-2);
insilmaril@771
  2019
	else
insilmaril@771
  2020
		return NULL;
insilmaril@739
  2021
}
insilmaril@739
  2022
insilmaril@776
  2023
ImageItem* VymModel::createImage(BranchItem *dst)
insilmaril@776
  2024
{
insilmaril@775
  2025
	if (dst)
insilmaril@726
  2026
	{
insilmaril@776
  2027
		QModelIndex parix;
insilmaril@776
  2028
		int n;
insilmaril@777
  2029
insilmaril@726
  2030
		QList<QVariant> cData;
insilmaril@790
  2031
		cData << "new" << "undef";
insilmaril@777
  2032
insilmaril@777
  2033
		ImageItem *newii=new ImageItem(cData) ;	
insilmaril@777
  2034
		//newii->setHeading (QApplication::translate("Heading of new image in map", "new image"));
insilmaril@776
  2035
insilmaril@776
  2036
		emit (layoutAboutToBeChanged() );
insilmaril@776
  2037
insilmaril@776
  2038
			parix=index(dst);
insilmaril@791
  2039
			if (!parix.isValid()) cout << "VM::createII invalid index\n";
insilmaril@776
  2040
			n=dst->getRowNumAppend(newii);
insilmaril@791
  2041
			beginInsertRows (parix,n,n);
insilmaril@776
  2042
			dst->appendChild (newii);	
insilmaril@776
  2043
			endInsertRows ();
insilmaril@776
  2044
insilmaril@776
  2045
		emit (layoutChanged() );
insilmaril@776
  2046
insilmaril@776
  2047
		// save scroll state. If scrolled, automatically select
insilmaril@776
  2048
		// new branch in order to tmp unscroll parent...
insilmaril@776
  2049
		newii->createMapObj(mapScene);
insilmaril@776
  2050
		reposition();
insilmaril@776
  2051
		return newii;
insilmaril@776
  2052
	} 
insilmaril@776
  2053
	return NULL;
insilmaril@726
  2054
}
insilmaril@726
  2055
insilmaril@790
  2056
XLinkItem* VymModel::createXLink(BranchItem *bi,bool createMO)
insilmaril@790
  2057
{
insilmaril@790
  2058
	if (bi)
insilmaril@790
  2059
	{
insilmaril@790
  2060
		QModelIndex parix;
insilmaril@790
  2061
		int n;
insilmaril@790
  2062
insilmaril@790
  2063
		QList<QVariant> cData;
insilmaril@790
  2064
		cData << "new xLink"<<"undef";
insilmaril@790
  2065
insilmaril@790
  2066
		XLinkItem *newxli=new XLinkItem(cData) ;	
insilmaril@791
  2067
		newxli->setBegin (bi);
insilmaril@790
  2068
insilmaril@790
  2069
		emit (layoutAboutToBeChanged() );
insilmaril@790
  2070
insilmaril@790
  2071
			parix=index(bi);
insilmaril@790
  2072
			n=bi->getRowNumAppend(newxli);
insilmaril@791
  2073
			beginInsertRows (parix,n,n);
insilmaril@790
  2074
			bi->appendChild (newxli);	
insilmaril@790
  2075
			endInsertRows ();
insilmaril@790
  2076
insilmaril@790
  2077
		emit (layoutChanged() );
insilmaril@790
  2078
insilmaril@790
  2079
		// save scroll state. If scrolled, automatically select
insilmaril@790
  2080
		// new branch in order to tmp unscroll parent...
insilmaril@790
  2081
		if (createMO) 
insilmaril@790
  2082
		{
insilmaril@790
  2083
			newxli->createMapObj(mapScene);
insilmaril@790
  2084
			reposition();
insilmaril@790
  2085
		}
insilmaril@790
  2086
		return newxli;
insilmaril@790
  2087
	} 
insilmaril@790
  2088
	return NULL;
insilmaril@790
  2089
}
insilmaril@790
  2090
insilmaril@788
  2091
AttributeItem* VymModel::addAttribute()	// FIXME-2 savestate missing
insilmaril@788
  2092
{
insilmaril@788
  2093
	BranchItem *selbi=getSelectedBranch();
insilmaril@788
  2094
	if (selbi)
insilmaril@786
  2095
	{
insilmaril@786
  2096
		QList<QVariant> cData;
insilmaril@790
  2097
		cData << "new attribute" << "undef";
insilmaril@786
  2098
		AttributeItem *a=new AttributeItem (cData);
insilmaril@786
  2099
insilmaril@786
  2100
		emit (layoutAboutToBeChanged() );
insilmaril@786
  2101
insilmaril@788
  2102
		QModelIndex parix=index(selbi);
insilmaril@788
  2103
		int n=selbi->getRowNumAppend (a);
insilmaril@791
  2104
		beginInsertRows (parix,n,n);	
insilmaril@788
  2105
		selbi->appendChild (a);	
insilmaril@786
  2106
		endInsertRows ();
insilmaril@786
  2107
insilmaril@786
  2108
		emit (layoutChanged() );
insilmaril@786
  2109
insilmaril@786
  2110
		reposition();
insilmaril@786
  2111
		return a;
insilmaril@786
  2112
	}
insilmaril@786
  2113
	return NULL;
insilmaril@786
  2114
}
insilmaril@786
  2115
insilmaril@773
  2116
BranchItem* VymModel::addMapCenter ()
insilmaril@773
  2117
{
insilmaril@773
  2118
	BranchItem *bi=addMapCenter (contextPos);
insilmaril@721
  2119
	updateActions();
insilmaril@763
  2120
	emitShowSelection();
insilmaril@721
  2121
	saveState (
insilmaril@773
  2122
		bi,
insilmaril@721
  2123
		"delete()",
insilmaril@721
  2124
		NULL,
insilmaril@723
  2125
		QString ("addMapCenter (%1,%2)").arg (contextPos.x()).arg(contextPos.y()),
insilmaril@723
  2126
		QString ("Adding MapCenter to (%1,%2)").arg (contextPos.x()).arg(contextPos.y())
insilmaril@721
  2127
	);	
insilmaril@773
  2128
	return bi;	
insilmaril@721
  2129
}
insilmaril@721
  2130
insilmaril@791
  2131
BranchItem* VymModel::addMapCenter(QPointF absPos)	
insilmaril@775
  2132
// createMapCenter could then probably be merged with createBranch
insilmaril@721
  2133
{
insilmaril@749
  2134
insilmaril@749
  2135
	// Create TreeItem
insilmaril@749
  2136
	QModelIndex parix=index(rootItem);
insilmaril@749
  2137
insilmaril@749
  2138
	QList<QVariant> cData;
insilmaril@790
  2139
	cData << "VM:addMapCenter" << "undef";
insilmaril@788
  2140
	BranchItem *newbi=new BranchItem (cData,rootItem);
insilmaril@773
  2141
	newbi->setHeading (QApplication::translate("Heading of mapcenter in new map", "New map"));
insilmaril@791
  2142
	int n=rootItem->getRowNumAppend (newbi);
insilmaril@791
  2143
insilmaril@791
  2144
	emit (layoutAboutToBeChanged() );
insilmaril@791
  2145
	beginInsertRows (parix,n,n);
insilmaril@791
  2146
insilmaril@773
  2147
	rootItem->appendChild (newbi);
insilmaril@749
  2148
insilmaril@749
  2149
	endInsertRows();
insilmaril@749
  2150
	emit (layoutChanged() );
insilmaril@749
  2151
insilmaril@749
  2152
	// Create MapObj
insilmaril@775
  2153
	newbi->setPositionMode (MapItem::Absolute);
insilmaril@791
  2154
	BranchObj *bo=newbi->createMapObj(mapScene);
insilmaril@791
  2155
	if (bo) bo->move (absPos);
insilmaril@760
  2156
		
insilmaril@773
  2157
	return newbi;
insilmaril@721
  2158
}
insilmaril@721
  2159
insilmaril@791
  2160
BranchItem* VymModel::addNewBranchInt(BranchItem *dst,int num)
insilmaril@721
  2161
{
insilmaril@721
  2162
	// Depending on pos:
insilmaril@721
  2163
	// -3		insert in children of parent  above selection 
insilmaril@721
  2164
	// -2		add branch to selection 
insilmaril@721
  2165
	// -1		insert in children of parent below selection 
insilmaril@721
  2166
	// 0..n		insert in children of parent at pos
insilmaril@771
  2167
insilmaril@771
  2168
	// Create TreeItem
insilmaril@771
  2169
	QList<QVariant> cData;
insilmaril@803
  2170
	cData << "" << "undef";
insilmaril@771
  2171
insilmaril@771
  2172
	BranchItem *parbi;
insilmaril@771
  2173
	QModelIndex parix;
insilmaril@771
  2174
	int n;
insilmaril@771
  2175
	BranchItem *newbi=new BranchItem (cData);	
insilmaril@803
  2176
	//newbi->setHeading (QApplication::translate("Heading of new branch in map", "new"));
insilmaril@771
  2177
insilmaril@771
  2178
	emit (layoutAboutToBeChanged() );
insilmaril@771
  2179
insilmaril@771
  2180
	if (num==-2)
insilmaril@721
  2181
	{
insilmaril@771
  2182
		parbi=dst;
insilmaril@771
  2183
		parix=index(parbi);
insilmaril@776
  2184
		n=parbi->getRowNumAppend (newbi);
insilmaril@791
  2185
		beginInsertRows (parix,n,n);	
insilmaril@771
  2186
		parbi->appendChild (newbi);	
insilmaril@771
  2187
		endInsertRows ();
insilmaril@772
  2188
	}else if (num==-1 || num==-3)
insilmaril@771
  2189
	{
insilmaril@771
  2190
		// insert below selection
insilmaril@771
  2191
		parbi=(BranchItem*)dst->parent();
insilmaril@776
  2192
		parix=index(parbi);  
insilmaril@776
  2193
		
insilmaril@776
  2194
		n=dst->childNumber() + (3+num)/2;	//-1 |-> 1;-3 |-> 0
insilmaril@771
  2195
		beginInsertRows (parix,n,n);	
insilmaril@771
  2196
		parbi->insertBranch(n,newbi);	
insilmaril@771
  2197
		endInsertRows ();
insilmaril@772
  2198
	}
insilmaril@771
  2199
	emit (layoutChanged() );
insilmaril@771
  2200
insilmaril@803
  2201
	// Set color of heading to that of parent
insilmaril@803
  2202
	newbi->setHeadingColor (parbi->getHeadingColor());
insilmaril@803
  2203
insilmaril@771
  2204
	// save scroll state. If scrolled, automatically select
insilmaril@771
  2205
	// new branch in order to tmp unscroll parent...
insilmaril@771
  2206
	newbi->createMapObj(mapScene);
insilmaril@772
  2207
	reposition();
insilmaril@771
  2208
	return newbi;
insilmaril@721
  2209
}	
insilmaril@721
  2210
insilmaril@755
  2211
BranchItem* VymModel::addNewBranch(int pos)
insilmaril@721
  2212
{
insilmaril@721
  2213
	// Different meaning than num in addNewBranchInt!
insilmaril@721
  2214
	// -1	add above
insilmaril@721
  2215
	//  0	add as child
insilmaril@721
  2216
	// +1	add below
insilmaril@755
  2217
	BranchItem *newbi=NULL;
insilmaril@787
  2218
	BranchItem *selbi=getSelectedBranch();
insilmaril@755
  2219
insilmaril@755
  2220
	if (selbi)
insilmaril@721
  2221
	{
insilmaril@771
  2222
		// FIXME-3 setCursor (Qt::ArrowCursor);  //Still needed?
insilmaril@771
  2223
insilmaril@771
  2224
		newbi=addNewBranchInt (selbi,pos-2);
insilmaril@755
  2225
insilmaril@755
  2226
		if (newbi)
insilmaril@721
  2227
		{
insilmaril@721
  2228
			saveState(
insilmaril@755
  2229
				newbi,		
insilmaril@721
  2230
				"delete ()",
insilmaril@755
  2231
				selbi,
insilmaril@721
  2232
				QString ("addBranch (%1)").arg(pos),
insilmaril@755
  2233
				QString ("Add new branch to %1").arg(getObjectName(selbi)));	
insilmaril@721
  2234
insilmaril@721
  2235
			reposition();
insilmaril@775
  2236
			// emitSelectionChanged(); FIXME-3
insilmaril@761
  2237
			latestAddedItem=newbi;
insilmaril@721
  2238
			// In Network mode, the client needs to know where the new branch is,
insilmaril@721
  2239
			// so we have to pass on this information via saveState.
insilmaril@721
  2240
			// TODO: Get rid of this positioning workaround
insilmaril@755
  2241
			/* FIXME-4  network problem:  QString ps=qpointfToString (newbo->getAbsPos());
insilmaril@721
  2242
			sendData ("selectLatestAdded ()");
insilmaril@721
  2243
			sendData (QString("move %1").arg(ps));
insilmaril@721
  2244
			sendSelection();
insilmaril@755
  2245
			*/
insilmaril@721
  2246
		}
insilmaril@721
  2247
	}	
insilmaril@755
  2248
	return newbi;
insilmaril@721
  2249
}
insilmaril@721
  2250
insilmaril@721
  2251
insilmaril@772
  2252
BranchItem* VymModel::addNewBranchBefore()	
insilmaril@771
  2253
{
insilmaril@771
  2254
	BranchItem *newbi=NULL;
insilmaril@787
  2255
	BranchItem *selbi=getSelectedBranch();
insilmaril@771
  2256
	if (selbi && selbi->getType()==TreeItem::Branch)
insilmaril@771
  2257
		 // We accept no MapCenter here, so we _have_ a parent
insilmaril@721
  2258
	{
insilmaril@771
  2259
		//QPointF p=bo->getRelPos();
insilmaril@771
  2260
insilmaril@721
  2261
insilmaril@721
  2262
		// add below selection
insilmaril@771
  2263
		newbi=addNewBranchInt (selbi,-1);
insilmaril@771
  2264
insilmaril@771
  2265
		if (newbi)
insilmaril@721
  2266
		{
insilmaril@771
  2267
			//newbi->move2RelPos (p);
insilmaril@721
  2268
insilmaril@721
  2269
			// Move selection to new branch
insilmaril@771
  2270
			relinkBranch (selbi,newbi,0);
insilmaril@771
  2271
insilmaril@771
  2272
			saveState (newbi, "deleteKeepChildren ()", newbi, "addBranchBefore ()", 
insilmaril@771
  2273
				QString ("Add branch before %1").arg(getObjectName(selbi)));
insilmaril@771
  2274
insilmaril@771
  2275
			// FIXME-3 needed? reposition();
insilmaril@775
  2276
			// emitSelectionChanged(); FIXME-3 
insilmaril@721
  2277
		}
insilmaril@721
  2278
	}	
insilmaril@771
  2279
	return newbi;
insilmaril@721
  2280
}
insilmaril@721
  2281
insilmaril@767
  2282
bool VymModel::relinkBranch (BranchItem *branch, BranchItem *dst, int pos)
insilmaril@755
  2283
{
insilmaril@755
  2284
	if (branch && dst)
insilmaril@755
  2285
	{
insilmaril@756
  2286
		emit (layoutAboutToBeChanged() );
insilmaril@755
  2287
		BranchItem *branchpi=(BranchItem*)branch->parent();
insilmaril@756
  2288
		// Remove at current position
insilmaril@756
  2289
		int n=branch->childNum();
insilmaril@756
  2290
		beginRemoveRows (index(branchpi),n,n);
insilmaril@756
  2291
		branchpi->removeChild (n);
insilmaril@756
  2292
		endRemoveRows();
insilmaril@756
  2293
insilmaril@756
  2294
		if (pos<0 ||pos>dst->branchCount() ) pos=dst->branchCount();
insilmaril@756
  2295
insilmaril@756
  2296
		// Append as last branch to dst
insilmaril@756
  2297
		if (dst->branchCount()==0)
insilmaril@756
  2298
			n=0;
insilmaril@756
  2299
		else	
insilmaril@756
  2300
			n=dst->getFirstBranch()->childNumber(); 
insilmaril@756
  2301
		beginInsertRows (index(dst),n+pos,n+pos);
insilmaril@756
  2302
		dst->insertBranch (pos,branch);
insilmaril@756
  2303
		endInsertRows();
insilmaril@756
  2304
insilmaril@788
  2305
		// Correct type if necessesary
insilmaril@788
  2306
		if (branch->getType()==TreeItem::MapCenter) 
insilmaril@788
  2307
			branch->setType(TreeItem::Branch);
insilmaril@788
  2308
insilmaril@773
  2309
		// reset parObj, fonts, frame, etc in related LMO or other view-objects
insilmaril@773
  2310
		branch->updateStyles();
insilmaril@773
  2311
insilmaril@756
  2312
		emit (layoutChanged() );
insilmaril@767
  2313
		reposition();	// both for moveUp/Down and relinking
insilmaril@781
  2314
		if (dst->isScrolled() )
insilmaril@781
  2315
		{
insilmaril@781
  2316
			select (dst);	
insilmaril@781
  2317
			branch->updateVisibility();
insilmaril@781
  2318
		}
insilmaril@781
  2319
		else	
insilmaril@781
  2320
			select (branch);
insilmaril@767
  2321
		return true;
insilmaril@755
  2322
	}
insilmaril@767
  2323
	return false;
insilmaril@755
  2324
}
insilmaril@755
  2325
insilmaril@777
  2326
bool VymModel::relinkImage (ImageItem *image, BranchItem *dst)
insilmaril@777
  2327
{
insilmaril@777
  2328
	if (image && dst)
insilmaril@777
  2329
	{
insilmaril@777
  2330
		emit (layoutAboutToBeChanged() );
insilmaril@777
  2331
insilmaril@777
  2332
		BranchItem *pi=(BranchItem*)(image->parent());
insilmaril@777
  2333
		QString oldParString=getSelectString (pi);
insilmaril@777
  2334
		// Remove at current position
insilmaril@777
  2335
		int n=image->childNum();
insilmaril@777
  2336
		beginRemoveRows (index(pi),n,n);
insilmaril@777
  2337
		pi->removeChild (n);
insilmaril@777
  2338
		endRemoveRows();
insilmaril@777
  2339
insilmaril@777
  2340
		// Add at dst
insilmaril@777
  2341
		QModelIndex dstix=index(dst);
insilmaril@777
  2342
		n=dst->getRowNumAppend (image);
insilmaril@777
  2343
		beginInsertRows (dstix,n,n+1);	
insilmaril@777
  2344
		dst->appendChild (image);	
insilmaril@777
  2345
		endInsertRows ();
insilmaril@777
  2346
insilmaril@777
  2347
		// Set new parent also for lmo
insilmaril@777
  2348
		if (image->getLMO() && dst->getLMO() )
insilmaril@777
  2349
			image->getLMO()->setParObj (dst->getLMO() );
insilmaril@777
  2350
insilmaril@777
  2351
		emit (layoutChanged() );
insilmaril@777
  2352
		saveState(
insilmaril@777
  2353
			image,
insilmaril@777
  2354
			QString("relinkTo (\"%1\")").arg(oldParString), 
insilmaril@777
  2355
			image,
insilmaril@777
  2356
			QString ("relinkTo (\"%1\")").arg(getSelectString (dst)),
insilmaril@777
  2357
			QString ("Relink floatimage to %1").arg(getObjectName(dst)));
insilmaril@777
  2358
		return true;	
insilmaril@777
  2359
	}
insilmaril@777
  2360
	return false;
insilmaril@777
  2361
}
insilmaril@777
  2362
insilmaril@788
  2363
void VymModel::deleteSelection()	
insilmaril@721
  2364
{
insilmaril@787
  2365
	BranchItem *selbi=getSelectedBranch();
insilmaril@753
  2366
insilmaril@788
  2367
	if (selbi)
insilmaril@721
  2368
	{
insilmaril@735
  2369
		unselect();
insilmaril@760
  2370
		saveStateRemovingPart (selbi, QString ("Delete %1").arg(getObjectName(selbi)));
insilmaril@745
  2371
insilmaril@776
  2372
		TreeItem *pi=deleteItem (selbi);
insilmaril@776
  2373
		if (pi)
insilmaril@776
  2374
		{
insilmaril@776
  2375
			select (pi);
insilmaril@776
  2376
			emitShowSelection();
insilmaril@776
  2377
		}
insilmaril@721
  2378
		return;
insilmaril@721
  2379
	}
insilmaril@788
  2380
	TreeItem *ti=getSelectedItem();
insilmaril@790
  2381
	if (ti)
insilmaril@721
  2382
	{
insilmaril@787
  2383
		TreeItem *pi=ti->parent();
insilmaril@790
  2384
		if (!pi) return;
insilmaril@790
  2385
		if (ti->getType()==TreeItem::Image || ti->getType()==TreeItem::Attribute)
insilmaril@790
  2386
		{
insilmaril@790
  2387
			saveStateChangingPart(
insilmaril@790
  2388
				pi, 
insilmaril@790
  2389
				ti,
insilmaril@790
  2390
				"delete ()",
insilmaril@790
  2391
				QString("Delete %1").arg(getObjectName(ti))
insilmaril@790
  2392
			);
insilmaril@792
  2393
			unselect();
insilmaril@792
  2394
			deleteItem (ti);
insilmaril@792
  2395
			emitDataHasChanged (pi);
insilmaril@792
  2396
			select (pi);
insilmaril@792
  2397
			reposition();
insilmaril@792
  2398
			emitShowSelection();
insilmaril@794
  2399
		} else if (ti->getType()==TreeItem::XLink)
insilmaril@794
  2400
		{
insilmaril@794
  2401
			//FIXME-2 savestate missing
insilmaril@794
  2402
			deleteItem (ti);
insilmaril@794
  2403
		} else
insilmaril@794
  2404
			qWarning ("VymmModel::deleteSelection()  unknown type?!");
insilmaril@721
  2405
	}
insilmaril@721
  2406
}
insilmaril@721
  2407
insilmaril@787
  2408
void VymModel::deleteKeepChildren()	//FIXME-3 does not work yet for mapcenters
insilmaril@787
  2409
insilmaril@787
  2410
{
insilmaril@787
  2411
	BranchItem *selbi=getSelectedBranch();
insilmaril@775
  2412
	BranchItem *pi;
insilmaril@775
  2413
	if (selbi)
insilmaril@721
  2414
	{
insilmaril@726
  2415
		// Don't use this on mapcenter
insilmaril@775
  2416
		if (selbi->depth()<2) return;
insilmaril@775
  2417
insilmaril@775
  2418
		pi=(BranchItem*)(selbi->parent());
insilmaril@726
  2419
		// Check if we have childs at all to keep
insilmaril@775
  2420
		if (selbi->branchCount()==0) 
insilmaril@726
  2421
		{
insilmaril@726
  2422
			deleteSelection();
insilmaril@726
  2423
			return;
insilmaril@726
  2424
		}
insilmaril@726
  2425
insilmaril@775
  2426
		QPointF p;
insilmaril@775
  2427
		if (selbi->getLMO()) p=selbi->getLMO()->getRelPos();
insilmaril@721
  2428
		saveStateChangingPart(
insilmaril@775
  2429
			pi,
insilmaril@775
  2430
			selbi,
insilmaril@721
  2431
			"deleteKeepChildren ()",
insilmaril@775
  2432
			QString("Remove %1 and keep its children").arg(getObjectName(selbi))
insilmaril@721
  2433
		);
insilmaril@721
  2434
insilmaril@775
  2435
		QString sel=getSelectString(selbi);
insilmaril@721
  2436
		unselect();
insilmaril@776
  2437
		int pos=selbi->num();
insilmaril@776
  2438
		BranchItem *bi=selbi->getFirstBranch();
insilmaril@776
  2439
		while (bi)
insilmaril@776
  2440
		{
insilmaril@776
  2441
			relinkBranch (bi,pi,pos);
insilmaril@776
  2442
			bi=selbi->getFirstBranch();
insilmaril@776
  2443
			pos++;
insilmaril@776
  2444
		}
insilmaril@776
  2445
		deleteItem (selbi);
insilmaril@721
  2446
		reposition();
insilmaril@721
  2447
		select (sel);
insilmaril@775
  2448
		BranchObj *bo=getSelectedBranchObj();
insilmaril@775
  2449
		if (bo) 
insilmaril@775
  2450
		{
insilmaril@775
  2451
			bo->move2RelPos (p);
insilmaril@775
  2452
			reposition();
insilmaril@775
  2453
		}
insilmaril@721
  2454
	}	
insilmaril@775
  2455
}
insilmaril@753
  2456
insilmaril@772
  2457
void VymModel::deleteChildren()		
insilmaril@772
  2458
insilmaril@772
  2459
{
insilmaril@787
  2460
	BranchItem *selbi=getSelectedBranch();
insilmaril@772
  2461
	if (selbi)
insilmaril@721
  2462
	{		
insilmaril@721
  2463
		saveStateChangingPart(
insilmaril@772
  2464
			selbi, 
insilmaril@772
  2465
			selbi,
insilmaril@721
  2466
			"deleteChildren ()",
insilmaril@772
  2467
			QString( "Remove children of branch %1").arg(getObjectName(selbi))
insilmaril@721
  2468
		);
insilmaril@772
  2469
		emit (layoutAboutToBeChanged() );
insilmaril@772
  2470
insilmaril@772
  2471
		QModelIndex ix=index (selbi);
insilmaril@772
  2472
		int n=selbi->branchCount()-1;
insilmaril@772
  2473
		beginRemoveRows (ix,0,n);
insilmaril@772
  2474
		removeRows (0,n+1,ix);
insilmaril@772
  2475
		endRemoveRows();
insilmaril@781
  2476
		if (selbi->isScrolled()) selbi->unScroll();
insilmaril@772
  2477
		emit (layoutChanged() );
insilmaril@721
  2478
		reposition();
insilmaril@721
  2479
	}	
insilmaril@772
  2480
}
insilmaril@721
  2481
insilmaril@776
  2482
TreeItem* VymModel::deleteItem (TreeItem *ti)
insilmaril@776
  2483
{
insilmaril@776
  2484
	if (ti)
insilmaril@776
  2485
	{
insilmaril@776
  2486
		TreeItem *pi=ti->parent();
insilmaril@776
  2487
		QModelIndex parentIndex=index(pi);
insilmaril@776
  2488
insilmaril@776
  2489
		emit (layoutAboutToBeChanged() );
insilmaril@776
  2490
insilmaril@776
  2491
		int n=ti->childNum();
insilmaril@776
  2492
		beginRemoveRows (parentIndex,n,n);
insilmaril@776
  2493
		removeRows (n,1,parentIndex);
insilmaril@776
  2494
		endRemoveRows();
insilmaril@776
  2495
		reposition();
insilmaril@776
  2496
insilmaril@776
  2497
		emit (layoutChanged() );
insilmaril@788
  2498
		if (pi->depth()>=0) return pi;
insilmaril@776
  2499
	}	
insilmaril@776
  2500
	return NULL;
insilmaril@776
  2501
}
insilmaril@776
  2502
insilmaril@795
  2503
void VymModel::clearItem (TreeItem *ti)
insilmaril@795
  2504
{
insilmaril@795
  2505
	if (ti)
insilmaril@795
  2506
	{
insilmaril@795
  2507
		QModelIndex parentIndex=index(ti);
insilmaril@795
  2508
		if (!parentIndex.isValid()) return;
insilmaril@795
  2509
insilmaril@795
  2510
		int n=ti->childCount();
insilmaril@795
  2511
		if (n==0) return;
insilmaril@795
  2512
insilmaril@795
  2513
		emit (layoutAboutToBeChanged() );
insilmaril@795
  2514
insilmaril@795
  2515
		beginRemoveRows (parentIndex,0,n-1);
insilmaril@795
  2516
		removeRows (0,n,parentIndex);
insilmaril@795
  2517
		endRemoveRows();
insilmaril@795
  2518
		reposition();
insilmaril@795
  2519
insilmaril@795
  2520
		emit (layoutChanged() );
insilmaril@795
  2521
insilmaril@795
  2522
	}	
insilmaril@795
  2523
	return ;
insilmaril@795
  2524
}
insilmaril@795
  2525
insilmaril@749
  2526
bool VymModel::scrollBranch(BranchItem *bi)
insilmaril@749
  2527
{
insilmaril@749
  2528
	if (bi)	
insilmaril@721
  2529
	{
insilmaril@749
  2530
		if (bi->isScrolled()) return false;
insilmaril@749
  2531
		if (bi->branchCount()==0) return false;
insilmaril@749
  2532
		if (bi->depth()==0) return false;
insilmaril@767
  2533
		if (bi->toggleScroll())
insilmaril@767
  2534
		{
insilmaril@798
  2535
			reposition();
insilmaril@767
  2536
			QString u,r;
insilmaril@767
  2537
			r="scroll";
insilmaril@767
  2538
			u="unscroll";
insilmaril@767
  2539
			saveState(
insilmaril@767
  2540
				bi,
insilmaril@767
  2541
				QString ("%1 ()").arg(u),
insilmaril@767
  2542
				bi,
insilmaril@767
  2543
				QString ("%1 ()").arg(r),
insilmaril@767
  2544
				QString ("%1 %2").arg(r).arg(getObjectName(bi))
insilmaril@767
  2545
			);
insilmaril@767
  2546
			emitDataHasChanged(bi);
insilmaril@775
  2547
			emitSelectionChanged();
insilmaril@795
  2548
			mapScene->update(); //Needed for _quick_ update,  even in 1.13.x 
insilmaril@767
  2549
			return true;
insilmaril@767
  2550
		}
insilmaril@721
  2551
	}	
insilmaril@721
  2552
	return false;
insilmaril@721
  2553
}
insilmaril@721
  2554
insilmaril@749
  2555
bool VymModel::unscrollBranch(BranchItem *bi)
insilmaril@749
  2556
{
insilmaril@749
  2557
	if (bi)
insilmaril@721
  2558
	{
insilmaril@763
  2559
		if (!bi->isScrolled()) return false;
insilmaril@749
  2560
		if (bi->branchCount()==0) return false;
insilmaril@749
  2561
		if (bi->depth()==0) return false;
insilmaril@795
  2562
		if (bi->toggleScroll())
insilmaril@795
  2563
		{
insilmaril@795
  2564
			reposition();
insilmaril@798
  2565
			QString u,r;
insilmaril@798
  2566
			u="scroll";
insilmaril@798
  2567
			r="unscroll";
insilmaril@798
  2568
			saveState(
insilmaril@798
  2569
				bi,
insilmaril@798
  2570
				QString ("%1 ()").arg(u),
insilmaril@798
  2571
				bi,
insilmaril@798
  2572
				QString ("%1 ()").arg(r),
insilmaril@798
  2573
				QString ("%1 %2").arg(r).arg(getObjectName(bi))
insilmaril@798
  2574
			);
insilmaril@798
  2575
			emitDataHasChanged(bi);
insilmaril@798
  2576
			emitSelectionChanged();
insilmaril@795
  2577
			mapScene->update(); //Needed for _quick_ update,  even in 1.13.x 
insilmaril@798
  2578
			return true;
insilmaril@798
  2579
		}	
insilmaril@795
  2580
	}	
insilmaril@721
  2581
	return false;
insilmaril@721
  2582
}
insilmaril@721
  2583
insilmaril@767
  2584
void VymModel::toggleScroll()	
insilmaril@721
  2585
{
insilmaril@787
  2586
	BranchItem *bi=(BranchItem*)getSelectedBranch();
insilmaril@767
  2587
	if (bi && bi->isBranchLikeType() )
insilmaril@721
  2588
	{
insilmaril@749
  2589
		if (bi->isScrolled())
insilmaril@749
  2590
			unscrollBranch (bi);
insilmaril@721
  2591
		else
insilmaril@749
  2592
			scrollBranch (bi);
insilmaril@721
  2593
	}
insilmaril@795
  2594
	// saveState & reposition are called in above functions
insilmaril@721
  2595
}
insilmaril@721
  2596
insilmaril@795
  2597
void VymModel::unscrollChildren() 	//FIXME-2 does not update flag yet, possible segfault
insilmaril@795
  2598
{
insilmaril@795
  2599
	BranchItem *selbi=getSelectedBranch();
insilmaril@795
  2600
	BranchItem *prev=NULL;
insilmaril@795
  2601
	BranchItem *cur=selbi;
insilmaril@795
  2602
	if (selbi)
insilmaril@721
  2603
	{
insilmaril@795
  2604
		saveStateChangingPart(
insilmaril@795
  2605
			selbi,
insilmaril@795
  2606
			selbi,
insilmaril@795
  2607
			QString ("unscrollChildren ()"),
insilmaril@795
  2608
			QString ("unscroll all children of %1").arg(getObjectName(selbi))
insilmaril@795
  2609
		);	
insilmaril@795
  2610
		while (cur) 
insilmaril@721
  2611
		{
insilmaril@795
  2612
			if (cur->isScrolled())
insilmaril@795
  2613
			{
insilmaril@795
  2614
				cur->toggleScroll(); 
insilmaril@795
  2615
				emitDataHasChanged (cur);
insilmaril@721
  2616
		}
insilmaril@795
  2617
			cur=next (cur,prev,selbi);
insilmaril@795
  2618
		}	
insilmaril@795
  2619
		updateActions();
insilmaril@795
  2620
		reposition();
insilmaril@721
  2621
	}	
insilmaril@721
  2622
}
insilmaril@746
  2623
insilmaril@763
  2624
void VymModel::emitExpandAll()
insilmaril@763
  2625
{
insilmaril@763
  2626
	emit (expandAll() );
insilmaril@763
  2627
}
insilmaril@763
  2628
insilmaril@767
  2629
void VymModel::toggleStandardFlag (const QString &name, FlagRow *master)
insilmaril@767
  2630
{
insilmaril@787
  2631
	BranchItem *bi=getSelectedBranch();
insilmaril@766
  2632
	if (bi) 
insilmaril@766
  2633
	{
insilmaril@766
  2634
		QString u,r;
insilmaril@766
  2635
		if (bi->isActiveStandardFlag(name))
insilmaril@766
  2636
		{
insilmaril@766
  2637
			r="unsetFlag";
insilmaril@766
  2638
			u="setFlag";
insilmaril@766
  2639
		}	
insilmaril@766
  2640
		else
insilmaril@766
  2641
		{
insilmaril@766
  2642
			u="unsetFlag";
insilmaril@766
  2643
			r="setFlag";
insilmaril@766
  2644
		}	
insilmaril@766
  2645
		saveState(
insilmaril@766
  2646
			bi,
insilmaril@766
  2647
			QString("%1 (\"%2\")").arg(u).arg(name), 
insilmaril@766
  2648
			bi,
insilmaril@766
  2649
			QString("%1 (\"%2\")").arg(r).arg(name),
insilmaril@766
  2650
			QString("Toggling standard flag \"%1\" of %2").arg(name).arg(getObjectName(bi)));
insilmaril@767
  2651
			bi->toggleStandardFlag (name, master);
insilmaril@767
  2652
		reposition();
insilmaril@775
  2653
		emitSelectionChanged();	
insilmaril@766
  2654
	}
insilmaril@766
  2655
}
insilmaril@766
  2656
insilmaril@791
  2657
void VymModel::addFloatImage (const QPixmap &img) 
insilmaril@791
  2658
{
insilmaril@791
  2659
	BranchItem *selbi=getSelectedBranch();
insilmaril@791
  2660
	if (selbi)
insilmaril@791
  2661
	{
insilmaril@791
  2662
		ImageItem *ii=createImage (selbi);
insilmaril@791
  2663
		ii->load(img);
insilmaril@791
  2664
		ii->setOriginalFilename("No original filename (image added by dropevent)");	
insilmaril@791
  2665
		QString s=getSelectString(selbi);
insilmaril@791
  2666
		saveState (PartOfMap, s, "nop ()", s, "copy ()","Copy dropped image to clipboard",ii  );
insilmaril@791
  2667
		saveState (ii,"delete ()", selbi,QString("paste(%1)").arg(curStep),"Pasting dropped image");
insilmaril@791
  2668
		reposition();
insilmaril@791
  2669
		// FIXME-3 VM needed? scene()->update();
insilmaril@791
  2670
	}
insilmaril@721
  2671
}
insilmaril@721
  2672
insilmaril@721
  2673
insilmaril@773
  2674
void VymModel::colorBranch (QColor c)	
insilmaril@773
  2675
{
insilmaril@798
  2676
cout << "VM::colBranch\n";
insilmaril@787
  2677
	BranchItem *selbi=getSelectedBranch();
insilmaril@773
  2678
	if (selbi)
insilmaril@721
  2679
	{
insilmaril@721
  2680
		saveState(
insilmaril@773
  2681
			selbi, 
insilmaril@773
  2682
			QString ("colorBranch (\"%1\")").arg(selbi->getHeadingColor().name()),
insilmaril@773
  2683
			selbi,
insilmaril@721
  2684
			QString ("colorBranch (\"%1\")").arg(c.name()),
insilmaril@773
  2685
			QString("Set color of %1 to %2").arg(getObjectName(selbi)).arg(c.name())
insilmaril@721
  2686
		);	
insilmaril@773
  2687
		selbi->setHeadingColor(c); // color branch
insilmaril@795
  2688
		mapScene->update();
insilmaril@721
  2689
	}
insilmaril@721
  2690
}
insilmaril@721
  2691
insilmaril@773
  2692
void VymModel::colorSubtree (QColor c) 
insilmaril@773
  2693
{
insilmaril@787
  2694
	BranchItem *selbi=getSelectedBranch();
insilmaril@773
  2695
	if (selbi)
insilmaril@721
  2696
	{
insilmaril@721
  2697
		saveStateChangingPart(
insilmaril@773
  2698
			selbi,
insilmaril@773
  2699
			selbi,
insilmaril@721
  2700
			QString ("colorSubtree (\"%1\")").arg(c.name()),
insilmaril@773
  2701
			QString ("Set color of %1 and children to %2").arg(getObjectName(selbi)).arg(c.name())
insilmaril@721
  2702
		);	
insilmaril@773
  2703
		BranchItem *prev=NULL;
insilmaril@773
  2704
		BranchItem *cur=selbi;
insilmaril@773
  2705
		while (cur) 
insilmaril@773
  2706
		{
insilmaril@773
  2707
			cur->setHeadingColor(c); // color links, color children
insilmaril@773
  2708
			cur=next (cur,prev,selbi);
insilmaril@773
  2709
		}	
insilmaril@795
  2710
	mapScene->update();
insilmaril@721
  2711
	}
insilmaril@721
  2712
}
insilmaril@721
  2713
insilmaril@773
  2714
QColor VymModel::getCurrentHeadingColor()	
insilmaril@773
  2715
{
insilmaril@787
  2716
	BranchItem *selbi=getSelectedBranch();
insilmaril@773
  2717
	if (selbi)	return selbi->getHeadingColor();
insilmaril@773
  2718
		
insilmaril@721
  2719
	QMessageBox::warning(0,"Warning","Can't get color of heading,\nthere's no branch selected");
insilmaril@721
  2720
	return Qt::black;
insilmaril@721
  2721
}
insilmaril@721
  2722
insilmaril@721
  2723
insilmaril@721
  2724
insilmaril@767
  2725
void VymModel::editURL()	
insilmaril@767
  2726
{
insilmaril@767
  2727
	TreeItem *selti=getSelectedItem();
insilmaril@767
  2728
	if (selti)
insilmaril@721
  2729
	{		
insilmaril@721
  2730
		bool ok;
insilmaril@721
  2731
		QString text = QInputDialog::getText(
insilmaril@721
  2732
				"VYM", tr("Enter URL:"), QLineEdit::Normal,
insilmaril@767
  2733
				selti->getURL(), &ok, NULL);
insilmaril@721
  2734
		if ( ok) 
insilmaril@721
  2735
			// user entered something and pressed OK
insilmaril@721
  2736
			setURL(text);
insilmaril@721
  2737
	}
insilmaril@721
  2738
}
insilmaril@721
  2739
insilmaril@767
  2740
void VymModel::editLocalURL()
insilmaril@767
  2741
{
insilmaril@767
  2742
	TreeItem *selti=getSelectedItem();
insilmaril@767
  2743
	if (selti)
insilmaril@721
  2744
	{		
insilmaril@721
  2745
		QStringList filters;
insilmaril@721
  2746
		filters <<"All files (*)";
insilmaril@721
  2747
		filters << tr("Text","Filedialog") + " (*.txt)";
insilmaril@721
  2748
		filters << tr("Spreadsheet","Filedialog") + " (*.odp,*.sxc)";
insilmaril@721
  2749
		filters << tr("Textdocument","Filedialog") +" (*.odw,*.sxw)";
insilmaril@721
  2750
		filters << tr("Images","Filedialog") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)";
insilmaril@721
  2751
		QFileDialog *fd=new QFileDialog( NULL,vymName+" - " +tr("Set URL to a local file"));
insilmaril@721
  2752
		fd->setFilters (filters);
insilmaril@721
  2753
		fd->setCaption(vymName+" - " +tr("Set URL to a local file"));
insilmaril@721
  2754
		fd->setDirectory (lastFileDir);
insilmaril@767
  2755
		if (! selti->getVymLink().isEmpty() )
insilmaril@767
  2756
			fd->selectFile( selti->getURL() );
insilmaril@721
  2757
		fd->show();
insilmaril@721
  2758
insilmaril@721
  2759
		if ( fd->exec() == QDialog::Accepted )
insilmaril@721
  2760
		{
insilmaril@721
  2761
			lastFileDir=QDir (fd->directory().path());
insilmaril@721
  2762
			setURL (fd->selectedFile() );
insilmaril@721
  2763
		}
insilmaril@721
  2764
	}
insilmaril@721
  2765
}
insilmaril@721
  2766
insilmaril@721
  2767
insilmaril@767
  2768
void VymModel::editHeading2URL() 
insilmaril@767
  2769
{
insilmaril@767
  2770
	TreeItem *selti=getSelectedItem();
insilmaril@767
  2771
	if (selti)
insilmaril@767
  2772
		setURL (selti->getHeading());
insilmaril@721
  2773
}	
insilmaril@721
  2774
insilmaril@767
  2775
void VymModel::editBugzilla2URL()	
insilmaril@767
  2776
{
insilmaril@767
  2777
	TreeItem *selti=getSelectedItem();
insilmaril@767
  2778
	if (selti)
insilmaril@721
  2779
	{		
insilmaril@803
  2780
		QString h=selti->getHeading();
insilmaril@803
  2781
		QRegExp rx("^(\\d+)");
insilmaril@803
  2782
		if (rx.indexIn(h) !=-1)
insilmaril@803
  2783
			setURL ("https://bugzilla.novell.com/show_bug.cgi?id="+rx.cap(1) );
insilmaril@721
  2784
	}
insilmaril@721
  2785
}	
insilmaril@721
  2786
insilmaril@767
  2787
void VymModel::editFATE2URL()
insilmaril@767
  2788
{
insilmaril@767
  2789
	TreeItem *selti=getSelectedItem();
insilmaril@767
  2790
	if (selti)
insilmaril@721
  2791
	{		
insilmaril@767
  2792
		QString url= "http://keeper.suse.de:8080/webfate/match/id?value=ID"+selti->getHeading();
insilmaril@721
  2793
		saveState(
insilmaril@767
  2794
			selti,
insilmaril@767
  2795
			"setURL (\""+selti->getURL()+"\")",
insilmaril@767
  2796
			selti,
insilmaril@721
  2797
			"setURL (\""+url+"\")",
insilmaril@767
  2798
			QString("Use heading of %1 as link to FATE").arg(getObjectName(selti))
insilmaril@721
  2799
		);	
insilmaril@767
  2800
		selti->setURL (url);
insilmaril@767
  2801
		// FIXME-4 updateActions();
insilmaril@721
  2802
	}
insilmaril@721
  2803
}	
insilmaril@721
  2804
insilmaril@721
  2805
void VymModel::editVymLink()
insilmaril@721
  2806
{
insilmaril@787
  2807
	BranchItem *bi=getSelectedBranch();
insilmaril@753
  2808
	if (bi)
insilmaril@721
  2809
	{		
insilmaril@721
  2810
		QStringList filters;
insilmaril@721
  2811
		filters <<"VYM map (*.vym)";
insilmaril@721
  2812
		QFileDialog *fd=new QFileDialog( NULL,vymName+" - " +tr("Link to another map"));
insilmaril@721
  2813
		fd->setFilters (filters);
insilmaril@721
  2814
		fd->setCaption(vymName+" - " +tr("Link to another map"));
insilmaril@721
  2815
		fd->setDirectory (lastFileDir);
insilmaril@753
  2816
		if (! bi->getVymLink().isEmpty() )
insilmaril@753
  2817
			fd->selectFile( bi->getVymLink() );
insilmaril@721
  2818
		fd->show();
insilmaril@721
  2819
insilmaril@721
  2820
		QString fn;
insilmaril@721
  2821
		if ( fd->exec() == QDialog::Accepted )
insilmaril@721
  2822
		{
insilmaril@721
  2823
			lastFileDir=QDir (fd->directory().path());
insilmaril@721
  2824
			saveState(
insilmaril@753
  2825
				bi,
insilmaril@753
  2826
				"setVymLink (\""+bi->getVymLink()+"\")",
insilmaril@753
  2827
				bi,
insilmaril@721
  2828
				"setVymLink (\""+fd->selectedFile()+"\")",
insilmaril@753
  2829
				QString("Set vymlink of %1 to %2").arg(getObjectName(bi)).arg(fd->selectedFile())
insilmaril@721
  2830
			);	
insilmaril@746
  2831
			setVymLink (fd->selectedFile() );	// FIXME-2 ok?
insilmaril@721
  2832
		}
insilmaril@721
  2833
	}
insilmaril@721
  2834
}
insilmaril@721
  2835
insilmaril@753
  2836
void VymModel::setVymLink (const QString &s)	// FIXME-3 no savestate?
insilmaril@721
  2837
{
insilmaril@721
  2838
	// Internal function, no saveState needed
insilmaril@780
  2839
	TreeItem *selti=getSelectedItem();
insilmaril@780
  2840
	if (selti)
insilmaril@721
  2841
	{
insilmaril@780
  2842
		selti->setVymLink(s);
insilmaril@721
  2843
		reposition();
insilmaril@780
  2844
		emitDataHasChanged (selti);
insilmaril@780
  2845
		emitShowSelection();
insilmaril@721
  2846
	}
insilmaril@721
  2847
}
insilmaril@721
  2848
insilmaril@721
  2849
void VymModel::deleteVymLink()
insilmaril@721
  2850
{
insilmaril@787
  2851
	BranchItem *bi=getSelectedBranch();
insilmaril@753
  2852
	if (bi)
insilmaril@721
  2853
	{		
insilmaril@721
  2854
		saveState(
insilmaril@753
  2855
			bi,
insilmaril@767
  2856
			"setVymLink (\""+bi->getVymLink()+"\")", 
insilmaril@753
  2857
			bi,
insilmaril@721
  2858
			"setVymLink (\"\")",
insilmaril@753
  2859
			QString("Unset vymlink of %1").arg(getObjectName(bi))
insilmaril@721
  2860
		);	
insilmaril@767
  2861
		bi->setVymLink ("" );
insilmaril@721
  2862
		updateActions();
insilmaril@721
  2863
		reposition();
insilmaril@721
  2864
	}
insilmaril@721
  2865
}
insilmaril@721
  2866
insilmaril@721
  2867
QString VymModel::getVymLink()
insilmaril@721
  2868
{
insilmaril@787
  2869
	BranchItem *bi=getSelectedBranch();
insilmaril@753
  2870
	if (bi)
insilmaril@767
  2871
		return bi->getVymLink();
insilmaril@721
  2872
	else	
insilmaril@721
  2873
		return "";
insilmaril@721
  2874
	
insilmaril@721
  2875
}
insilmaril@721
  2876
insilmaril@771
  2877
QStringList VymModel::getVymLinks()	
insilmaril@771
  2878
{
insilmaril@721
  2879
	QStringList links;
insilmaril@787
  2880
	BranchItem *selbi=getSelectedBranch();
insilmaril@773
  2881
	BranchItem *cur=selbi;
insilmaril@771
  2882
	BranchItem *prev=NULL;
insilmaril@771
  2883
	while (cur) 
insilmaril@771
  2884
	{
insilmaril@771
  2885
		if (!cur->getVymLink().isEmpty()) links.append( cur->getVymLink());
insilmaril@773
  2886
		cur=next (cur,prev,selbi);
insilmaril@721
  2887
	}	
insilmaril@721
  2888
	return links;
insilmaril@721
  2889
}
insilmaril@721
  2890
insilmaril@721
  2891
insilmaril@790
  2892
void VymModel::followXLink(int i)	
insilmaril@753
  2893
{
insilmaril@753
  2894
	i=0;
insilmaril@790
  2895
	BranchItem *selbi=getSelectedBranch();
insilmaril@790
  2896
	if (selbi)
insilmaril@721
  2897
	{
insilmaril@790
  2898
		selbi=selbi->getXLinkNum(i)->getPartnerBranch();
insilmaril@790
  2899
		if (selbi) select (selbi);
insilmaril@721
  2900
	}
insilmaril@721
  2901
}
insilmaril@721
  2902
insilmaril@790
  2903
void VymModel::editXLink(int i)	
insilmaril@753
  2904
{
insilmaril@753
  2905
	i=0;
insilmaril@790
  2906
	BranchItem *selbi=getSelectedBranch();
insilmaril@790
  2907
	if (selbi)
insilmaril@721
  2908
	{
insilmaril@790
  2909
		XLinkItem *xli=selbi->getXLinkNum(i);
insilmaril@790
  2910
		if (xli) 
insilmaril@721
  2911
		{
insilmaril@721
  2912
			EditXLinkDialog dia;
insilmaril@790
  2913
			dia.setXLink (xli);
insilmaril@790
  2914
			dia.setSelection(selbi);
insilmaril@721
  2915
			if (dia.exec() == QDialog::Accepted)
insilmaril@721
  2916
			{
insilmaril@721
  2917
				if (dia.useSettingsGlobal() )
insilmaril@721
  2918
				{
insilmaril@790
  2919
					setMapDefXLinkColor (xli->getColor() );
insilmaril@790
  2920
					setMapDefXLinkWidth (xli->getWidth() );
insilmaril@721
  2921
				}
insilmaril@790
  2922
				if (dia.deleteXLink()) deleteItem (xli);
insilmaril@721
  2923
			}
insilmaril@721
  2924
		}	
insilmaril@721
  2925
	}
insilmaril@721
  2926
}
insilmaril@721
  2927
insilmaril@721
  2928
insilmaril@721
  2929
insilmaril@721
  2930
insilmaril@721
  2931
insilmaril@721
  2932
//////////////////////////////////////////////
insilmaril@721
  2933
// Scripting
insilmaril@721
  2934
//////////////////////////////////////////////
insilmaril@721
  2935
insilmaril@721
  2936
void VymModel::parseAtom(const QString &atom)
insilmaril@721
  2937
{
insilmaril@750
  2938
	TreeItem* selti=getSelectedItem();
insilmaril@787
  2939
	BranchItem *selbi=getSelectedBranch();
insilmaril@721
  2940
	QString s,t;
insilmaril@721
  2941
	double x,y;
insilmaril@721
  2942
	int n;
insilmaril@721
  2943
	bool b,ok;
insilmaril@721
  2944
insilmaril@721
  2945
	// Split string s into command and parameters
insilmaril@721
  2946
	parser.parseAtom (atom);
insilmaril@721
  2947
	QString com=parser.getCommand();
insilmaril@721
  2948
	
insilmaril@721
  2949
	// External commands
insilmaril@721
  2950
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  2951
	if (com=="addBranch")
insilmaril@721
  2952
	{
insilmaril@750
  2953
		if (!selti)
insilmaril@721
  2954
		{
insilmaril@721
  2955
			parser.setError (Aborted,"Nothing selected");
insilmaril@753
  2956
		} else if (! selbi )
insilmaril@721
  2957
		{				  
insilmaril@721
  2958
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  2959
		} else 
insilmaril@721
  2960
		{	
insilmaril@721
  2961
			QList <int> pl;
insilmaril@721
  2962
			pl << 0 <<1;
insilmaril@721
  2963
			if (parser.checkParCount(pl))
insilmaril@721
  2964
			{
insilmaril@721
  2965
				if (parser.parCount()==0)
insilmaril@721
  2966
					addNewBranch (0);
insilmaril@721
  2967
				else
insilmaril@721
  2968
				{
insilmaril@721
  2969
					n=parser.parInt (ok,0);
insilmaril@721
  2970
					if (ok ) addNewBranch (n);
insilmaril@721
  2971
				}
insilmaril@721
  2972
			}
insilmaril@721
  2973
		}
insilmaril@721
  2974
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  2975
	} else if (com=="addBranchBefore")
insilmaril@721
  2976
	{
insilmaril@750
  2977
		if (!selti)
insilmaril@721
  2978
		{
insilmaril@721
  2979
			parser.setError (Aborted,"Nothing selected");
insilmaril@753
  2980
		} else if (! selbi )
insilmaril@721
  2981
		{				  
insilmaril@721
  2982
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  2983
		} else 
insilmaril@721
  2984
		{	
insilmaril@721
  2985
			if (parser.parCount()==0)
insilmaril@721
  2986
			{
insilmaril@721
  2987
				addNewBranchBefore ();
insilmaril@721
  2988
			}	
insilmaril@721
  2989
		}
insilmaril@721
  2990
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  2991
	} else if (com==QString("addMapCenter"))
insilmaril@721
  2992
	{
insilmaril@721
  2993
		if (parser.checkParCount(2))
insilmaril@721
  2994
		{
insilmaril@721
  2995
			x=parser.parDouble (ok,0);
insilmaril@721
  2996
			if (ok)
insilmaril@721
  2997
			{
insilmaril@721
  2998
				y=parser.parDouble (ok,1);
insilmaril@721
  2999
				if (ok) addMapCenter (QPointF(x,y));
insilmaril@721
  3000
			}
insilmaril@721
  3001
		}	
insilmaril@721
  3002
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3003
	} else if (com==QString("addMapReplace"))
insilmaril@721
  3004
	{
insilmaril@750
  3005
		if (!selti)
insilmaril@721
  3006
		{
insilmaril@721
  3007
			parser.setError (Aborted,"Nothing selected");
insilmaril@753
  3008
		} else if (! selbi )
insilmaril@721
  3009
		{				  
insilmaril@721
  3010
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3011
		} else if (parser.checkParCount(1))
insilmaril@721
  3012
		{
insilmaril@721
  3013
			//s=parser.parString (ok,0);	// selection
insilmaril@721
  3014
			t=parser.parString (ok,0);	// path to map
insilmaril@721
  3015
			if (QDir::isRelativePath(t)) t=(tmpMapDir + "/"+t);
insilmaril@753
  3016
			addMapReplaceInt(getSelectString(selbi),t);	
insilmaril@721
  3017
		}
insilmaril@721
  3018
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3019
	} else if (com==QString("addMapInsert"))
insilmaril@721
  3020
	{
insilmaril@788
  3021
		if (parser.parCount()==2)
insilmaril@721
  3022
		{
insilmaril@788
  3023
insilmaril@788
  3024
			if (!selti)
insilmaril@721
  3025
			{
insilmaril@788
  3026
				parser.setError (Aborted,"Nothing selected");
insilmaril@788
  3027
			} else if (! selbi )
insilmaril@788
  3028
			{				  
insilmaril@788
  3029
				parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@788
  3030
			} else 
insilmaril@788
  3031
			{	
insilmaril@721
  3032
				t=parser.parString (ok,0);	// path to map
insilmaril@721
  3033
				n=parser.parInt(ok,1);		// position
insilmaril@721
  3034
				if (QDir::isRelativePath(t)) t=(tmpMapDir + "/"+t);
insilmaril@721
  3035
				addMapInsertInt(t,n);	
insilmaril@721
  3036
			}
insilmaril@788
  3037
		} else if (parser.parCount()==1)
insilmaril@788
  3038
		{
insilmaril@788
  3039
			t=parser.parString (ok,0);	// path to map
insilmaril@788
  3040
			if (QDir::isRelativePath(t)) t=(tmpMapDir + "/"+t);
insilmaril@788
  3041
			addMapInsertInt(t);	
insilmaril@788
  3042
		} else
insilmaril@788
  3043
			parser.setError (Aborted,"Wrong number of parameters");
insilmaril@721
  3044
	/////////////////////////////////////////////////////////////////////
insilmaril@794
  3045
	} else if (com==QString("addXLink"))
insilmaril@794
  3046
	{
insilmaril@794
  3047
		if (parser.parCount()>1)
insilmaril@794
  3048
		{
insilmaril@794
  3049
			s=parser.parString (ok,0);	// begin
insilmaril@794
  3050
			t=parser.parString (ok,1);	// end
insilmaril@794
  3051
			BranchItem *begin=(BranchItem*)findBySelectString(s);
insilmaril@794
  3052
			BranchItem *end=(BranchItem*)findBySelectString(t);
insilmaril@794
  3053
			if (begin && end)
insilmaril@794
  3054
			{
insilmaril@794
  3055
				if (begin->isBranchLikeType() && end->isBranchLikeType())
insilmaril@794
  3056
				{
insilmaril@794
  3057
					XLinkItem *xl=createXLink (begin,true);
insilmaril@794
  3058
					if (xl)
insilmaril@794
  3059
					{
insilmaril@794
  3060
						xl->setEnd (end);
insilmaril@794
  3061
						xl->activate();
insilmaril@794
  3062
					} else
insilmaril@794
  3063
						parser.setError (Aborted,"Failed to create xLink");
insilmaril@794
  3064
				}
insilmaril@794
  3065
				else
insilmaril@794
  3066
					parser.setError (Aborted,"begin or end of xLink are not branch or mapcenter");
insilmaril@794
  3067
				
insilmaril@794
  3068
			} else
insilmaril@794
  3069
				parser.setError (Aborted,"Couldn't select begin or end of xLink");
insilmaril@794
  3070
		} else
insilmaril@794
  3071
			parser.setError (Aborted,"Need at least 2 parameters for begin and end");
insilmaril@794
  3072
	/////////////////////////////////////////////////////////////////////
insilmaril@767
  3073
	} else if (com=="clearFlags")	
insilmaril@721
  3074
	{
insilmaril@750
  3075
		if (!selti )
insilmaril@721
  3076
		{
insilmaril@721
  3077
			parser.setError (Aborted,"Nothing selected");
insilmaril@753
  3078
		} else if (! selbi )
insilmaril@721
  3079
		{				  
insilmaril@721
  3080
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3081
		} else if (parser.checkParCount(0))
insilmaril@721
  3082
		{
insilmaril@767
  3083
			selbi->deactivateAllStandardFlags();	
insilmaril@721
  3084
		}
insilmaril@721
  3085
	/////////////////////////////////////////////////////////////////////
insilmaril@767
  3086
	} else if (com=="colorBranch")
insilmaril@721
  3087
	{
insilmaril@750
  3088
		if (!selti)
insilmaril@721
  3089
		{
insilmaril@721
  3090
			parser.setError (Aborted,"Nothing selected");
insilmaril@753
  3091
		} else if (! selbi )
insilmaril@721
  3092
		{				  
insilmaril@721
  3093
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3094
		} else if (parser.checkParCount(1))
insilmaril@721
  3095
		{	
insilmaril@721
  3096
			QColor c=parser.parColor (ok,0);
insilmaril@721
  3097
			if (ok) colorBranch (c);
insilmaril@721
  3098
		}	
insilmaril@721
  3099
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3100
	} else if (com=="colorSubtree")
insilmaril@721
  3101
	{
insilmaril@750
  3102
		if (!selti)
insilmaril@721
  3103
		{
insilmaril@721
  3104
			parser.setError (Aborted,"Nothing selected");
insilmaril@753
  3105
		} else if (! selbi )
insilmaril@721
  3106
		{				  
insilmaril@721
  3107
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3108
		} else if (parser.checkParCount(1))
insilmaril@721
  3109
		{	
insilmaril@721
  3110
			QColor c=parser.parColor (ok,0);
insilmaril@721
  3111
			if (ok) colorSubtree (c);
insilmaril@721
  3112
		}	
insilmaril@721
  3113
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3114
	} else if (com=="copy")
insilmaril@721
  3115
	{
insilmaril@750
  3116
		if (!selti)
insilmaril@721
  3117
		{
insilmaril@721
  3118
			parser.setError (Aborted,"Nothing selected");
insilmaril@771
  3119
		} else if ( selectionType()!=TreeItem::Branch  && 
insilmaril@771
  3120
					selectionType()!=TreeItem::MapCenter  &&
insilmaril@771
  3121
					selectionType()!=TreeItem::Image )
insilmaril@721
  3122
		{				  
insilmaril@771
  3123
			parser.setError (Aborted,"Type of selection is not a branch or floatimage");
insilmaril@721
  3124
		} else if (parser.checkParCount(0))
insilmaril@721
  3125
		{	
insilmaril@771
  3126
			copy();
insilmaril@721
  3127
		}	
insilmaril@721
  3128
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3129
	} else if (com=="cut")
insilmaril@721
  3130
	{
insilmaril@750
  3131
		if (!selti)
insilmaril@721
  3132
		{
insilmaril@721
  3133
			parser.setError (Aborted,"Nothing selected");
insilmaril@735
  3134
		} else if ( selectionType()!=TreeItem::Branch  && 
insilmaril@735
  3135
					selectionType()!=TreeItem::MapCenter  &&
insilmaril@735
  3136
					selectionType()!=TreeItem::Image )
insilmaril@721
  3137
		{				  
insilmaril@721
  3138
			parser.setError (Aborted,"Type of selection is not a branch or floatimage");
insilmaril@721
  3139
		} else if (parser.checkParCount(0))
insilmaril@721
  3140
		{	
insilmaril@721
  3141
			cut();
insilmaril@721
  3142
		}	
insilmaril@721
  3143
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3144
	} else if (com=="delete")
insilmaril@721
  3145
	{
insilmaril@750
  3146
		if (!selti)
insilmaril@721
  3147
		{
insilmaril@721
  3148
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  3149
		} 
insilmaril@735
  3150
		/*else if (selectionType() != TreeItem::Branch && selectionType() != TreeItem::Image )
insilmaril@721
  3151
		{
insilmaril@721
  3152
			parser.setError (Aborted,"Type of selection is wrong.");
insilmaril@721
  3153
		} 
insilmaril@721
  3154
		*/
insilmaril@721
  3155
		else if (parser.checkParCount(0))
insilmaril@721
  3156
		{	
insilmaril@721
  3157
			deleteSelection();
insilmaril@721
  3158
		}	
insilmaril@721
  3159
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3160
	} else if (com=="deleteKeepChildren")
insilmaril@721
  3161
	{
insilmaril@750
  3162
		if (!selti)
insilmaril@721
  3163
		{
insilmaril@721
  3164
			parser.setError (Aborted,"Nothing selected");
insilmaril@753
  3165
		} else if (! selbi )
insilmaril@721
  3166
		{
insilmaril@721
  3167
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3168
		} else if (parser.checkParCount(0))
insilmaril@721
  3169
		{	
insilmaril@721
  3170
			deleteKeepChildren();
insilmaril@721
  3171
		}	
insilmaril@721
  3172
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3173
	} else if (com=="deleteChildren")
insilmaril@721
  3174
	{
insilmaril@750
  3175
		if (!selti)
insilmaril@721
  3176
		{
insilmaril@721
  3177
			parser.setError (Aborted,"Nothing selected");
insilmaril@753
  3178
		} else if (! selbi)
insilmaril@721
  3179
		{
insilmaril@721
  3180
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3181
		} else if (parser.checkParCount(0))
insilmaril@721
  3182
		{	
insilmaril@721
  3183
			deleteChildren();
insilmaril@721
  3184
		}	
insilmaril@721
  3185
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3186
	} else if (com=="exportASCII")
insilmaril@721
  3187
	{
insilmaril@721
  3188
		QString fname="";
insilmaril@721
  3189
		ok=true;
insilmaril@721
  3190
		if (parser.parCount()>=1)
insilmaril@721
  3191
			// Hey, we even have a filename
insilmaril@721
  3192
			fname=parser.parString(ok,0); 
insilmaril@721
  3193
		if (!ok)
insilmaril@721
  3194
		{
insilmaril@721
  3195
			parser.setError (Aborted,"Could not read filename");
insilmaril@721
  3196
		} else
insilmaril@721
  3197
		{
insilmaril@721
  3198
				exportASCII (fname,false);
insilmaril@721
  3199
		}
insilmaril@721
  3200
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3201
	} else if (com=="exportImage")
insilmaril@721
  3202
	{
insilmaril@721
  3203
		QString fname="";
insilmaril@721
  3204
		ok=true;
insilmaril@721
  3205
		if (parser.parCount()>=2)
insilmaril@721
  3206
			// Hey, we even have a filename
insilmaril@721
  3207
			fname=parser.parString(ok,0); 
insilmaril@721
  3208
		if (!ok)
insilmaril@721
  3209
		{
insilmaril@721
  3210
			parser.setError (Aborted,"Could not read filename");
insilmaril@721
  3211
		} else
insilmaril@721
  3212
		{
insilmaril@721
  3213
			QString format="PNG";
insilmaril@721
  3214
			if (parser.parCount()>=2)
insilmaril@721
  3215
			{
insilmaril@721
  3216
				format=parser.parString(ok,1);
insilmaril@721
  3217
			}
insilmaril@721
  3218
			exportImage (fname,false,format);
insilmaril@721
  3219
		}
insilmaril@721
  3220
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3221
	} else if (com=="exportXHTML")
insilmaril@721
  3222
	{
insilmaril@721
  3223
		QString fname="";
insilmaril@721
  3224
		ok=true;
insilmaril@721
  3225
		if (parser.parCount()>=2)
insilmaril@721
  3226
			// Hey, we even have a filename
insilmaril@721
  3227
			fname=parser.parString(ok,1); 
insilmaril@721
  3228
		if (!ok)
insilmaril@721
  3229
		{
insilmaril@721
  3230
			parser.setError (Aborted,"Could not read filename");
insilmaril@721
  3231
		} else
insilmaril@721
  3232
		{
insilmaril@721
  3233
			exportXHTML (fname,false);
insilmaril@721
  3234
		}
insilmaril@721
  3235
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3236
	} else if (com=="exportXML")
insilmaril@721
  3237
	{
insilmaril@721
  3238
		QString fname="";
insilmaril@721
  3239
		ok=true;
insilmaril@721
  3240
		if (parser.parCount()>=2)
insilmaril@721
  3241
			// Hey, we even have a filename
insilmaril@721
  3242
			fname=parser.parString(ok,1); 
insilmaril@721
  3243
		if (!ok)
insilmaril@721
  3244
		{
insilmaril@721
  3245
			parser.setError (Aborted,"Could not read filename");
insilmaril@721
  3246
		} else
insilmaril@721
  3247
		{
insilmaril@721
  3248
			exportXML (fname,false);
insilmaril@721
  3249
		}
insilmaril@721
  3250
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3251
	} else if (com=="importDir")
insilmaril@721
  3252
	{
insilmaril@750
  3253
		if (!selti)
insilmaril@721
  3254
		{
insilmaril@721
  3255
			parser.setError (Aborted,"Nothing selected");
insilmaril@753
  3256
		} else if (! selbi )
insilmaril@721
  3257
		{				  
insilmaril@721
  3258
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3259
		} else if (parser.checkParCount(1))
insilmaril@721
  3260
		{
insilmaril@721
  3261
			s=parser.parString(ok,0);
insilmaril@721
  3262
			if (ok) importDirInt(s);
insilmaril@721
  3263
		}	
insilmaril@721
  3264
	/////////////////////////////////////////////////////////////////////
insilmaril@776
  3265
	} else if (com=="relinkTo")
insilmaril@721
  3266
	{
insilmaril@750
  3267
		if (!selti)
insilmaril@721
  3268
		{
insilmaril@721
  3269
			parser.setError (Aborted,"Nothing selected");
insilmaril@753
  3270
		} else if ( selbi)
insilmaril@721
  3271
		{
insilmaril@721
  3272
			if (parser.checkParCount(4))
insilmaril@721
  3273
			{
insilmaril@721
  3274
				// 0	selectstring of parent
insilmaril@721
  3275
				// 1	num in parent (for branches)
insilmaril@721
  3276
				// 2,3	x,y of mainbranch or mapcenter
insilmaril@721
  3277
				s=parser.parString(ok,0);
insilmaril@776
  3278
				TreeItem *dst=findBySelectString (s);
insilmaril@721
  3279
				if (dst)
insilmaril@721
  3280
				{	
insilmaril@776
  3281
					if (dst->getType()==TreeItem::Branch) 
insilmaril@721
  3282
					{
insilmaril@721
  3283
						// Get number in parent
insilmaril@721
  3284
						n=parser.parInt (ok,1);
insilmaril@721
  3285
						if (ok)
insilmaril@721
  3286
						{
insilmaril@776
  3287
							relinkBranch (selbi,(BranchItem*)dst,n);
insilmaril@775
  3288
							emitSelectionChanged();
insilmaril@721
  3289
						}	
insilmaril@776
  3290
					} else if (dst->getType()==TreeItem::MapCenter) 
insilmaril@721
  3291
					{
insilmaril@776
  3292
						relinkBranch (selbi,(BranchItem*)dst);
insilmaril@721
  3293
						// Get coordinates of mainbranch
insilmaril@721
  3294
						x=parser.parDouble(ok,2);
insilmaril@721
  3295
						if (ok)
insilmaril@721
  3296
						{
insilmaril@721
  3297
							y=parser.parDouble(ok,3);
insilmaril@721
  3298
							if (ok) 
insilmaril@721
  3299
							{
insilmaril@776
  3300
								if (selbi->getLMO()) selbi->getLMO()->move (x,y);
insilmaril@775
  3301
								emitSelectionChanged();
insilmaril@721
  3302
							}
insilmaril@721
  3303
						}
insilmaril@721
  3304
					}	
insilmaril@721
  3305
				}	
insilmaril@721
  3306
			}	
insilmaril@776
  3307
		} else if ( selti->getType() == TreeItem::Image) 
insilmaril@721
  3308
		{
insilmaril@721
  3309
			if (parser.checkParCount(1))
insilmaril@721
  3310
			{
insilmaril@721
  3311
				// 0	selectstring of parent
insilmaril@721
  3312
				s=parser.parString(ok,0);
insilmaril@776
  3313
				TreeItem *dst=findBySelectString (s);
insilmaril@721
  3314
				if (dst)
insilmaril@721
  3315
				{	
insilmaril@776
  3316
					if (dst->isBranchLikeType())
insilmaril@777
  3317
						relinkImage ( ((ImageItem*)selti),(BranchItem*)dst);
insilmaril@721
  3318
				} else	
insilmaril@721
  3319
					parser.setError (Aborted,"Destination is not a branch");
insilmaril@721
  3320
			}		
insilmaril@721
  3321
		} else
insilmaril@721
  3322
			parser.setError (Aborted,"Type of selection is not a floatimage or branch");
insilmaril@721
  3323
	/////////////////////////////////////////////////////////////////////
insilmaril@776
  3324
	} else if (com=="loadImage")
insilmaril@721
  3325
	{
insilmaril@750
  3326
		if (!selti)
insilmaril@721
  3327
		{
insilmaril@721
  3328
			parser.setError (Aborted,"Nothing selected");
insilmaril@753
  3329
		} else if (! selbi )
insilmaril@721
  3330
		{				  
insilmaril@721
  3331
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3332
		} else if (parser.checkParCount(1))
insilmaril@721
  3333
		{
insilmaril@721
  3334
			s=parser.parString(ok,0);
insilmaril@775
  3335
			if (ok) loadFloatImageInt (selbi,s);
insilmaril@721
  3336
		}	
insilmaril@721
  3337
	/////////////////////////////////////////////////////////////////////
insilmaril@767
  3338
	} else if (com=="moveUp")
insilmaril@721
  3339
	{
insilmaril@750
  3340
		if (!selti )
insilmaril@721
  3341
		{
insilmaril@721
  3342
			parser.setError (Aborted,"Nothing selected");
insilmaril@753
  3343
		} else if (! selbi )
insilmaril@721
  3344
		{				  
insilmaril@721
  3345
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3346
		} else if (parser.checkParCount(0))
insilmaril@721
  3347
		{
insilmaril@767
  3348
			moveUp();
insilmaril@721
  3349
		}	
insilmaril@721
  3350
	/////////////////////////////////////////////////////////////////////
insilmaril@767
  3351
	} else if (com=="moveDown")
insilmaril@721
  3352
	{
insilmaril@750
  3353
		if (!selti )
insilmaril@721
  3354
		{
insilmaril@721
  3355
			parser.setError (Aborted,"Nothing selected");
insilmaril@753
  3356
		} else if (! selbi )
insilmaril@721
  3357
		{				  
insilmaril@721
  3358
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3359
		} else if (parser.checkParCount(0))
insilmaril@721
  3360
		{
insilmaril@767
  3361
			moveDown();
insilmaril@721
  3362
		}	
insilmaril@721
  3363
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3364
	} else if (com=="move")
insilmaril@721
  3365
	{
insilmaril@750
  3366
		if (!selti )
insilmaril@721
  3367
		{
insilmaril@721
  3368
			parser.setError (Aborted,"Nothing selected");
insilmaril@735
  3369
		} else if ( selectionType()!=TreeItem::Branch  && 
insilmaril@735
  3370
					selectionType()!=TreeItem::MapCenter  &&
insilmaril@735
  3371
					selectionType()!=TreeItem::Image )
insilmaril@721
  3372
		{				  
insilmaril@721
  3373
			parser.setError (Aborted,"Type of selection is not a branch or floatimage");
insilmaril@721
  3374
		} else if (parser.checkParCount(2))
insilmaril@721
  3375
		{	
insilmaril@721
  3376
			x=parser.parDouble (ok,0);
insilmaril@721
  3377
			if (ok)
insilmaril@721
  3378
			{
insilmaril@721
  3379
				y=parser.parDouble (ok,1);
insilmaril@721
  3380
				if (ok) move (x,y);
insilmaril@721
  3381
			}
insilmaril@721
  3382
		}	
insilmaril@721
  3383
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3384
	} else if (com=="moveRel")
insilmaril@721
  3385
	{
insilmaril@750
  3386
		if (!selti )
insilmaril@721
  3387
		{
insilmaril@721
  3388
			parser.setError (Aborted,"Nothing selected");
insilmaril@735
  3389
		} else if ( selectionType()!=TreeItem::Branch  && 
insilmaril@735
  3390
					selectionType()!=TreeItem::MapCenter  &&
insilmaril@735
  3391
					selectionType()!=TreeItem::Image )
insilmaril@721
  3392
		{				  
insilmaril@721
  3393
			parser.setError (Aborted,"Type of selection is not a branch or floatimage");
insilmaril@721
  3394
		} else if (parser.checkParCount(2))
insilmaril@721
  3395
		{	
insilmaril@721
  3396
			x=parser.parDouble (ok,0);
insilmaril@721
  3397
			if (ok)
insilmaril@721
  3398
			{
insilmaril@721
  3399
				y=parser.parDouble (ok,1);
insilmaril@721
  3400
				if (ok) moveRel (x,y);
insilmaril@721
  3401
			}
insilmaril@721
  3402
		}	
insilmaril@721
  3403
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3404
	} else if (com=="nop")
insilmaril@721
  3405
	{
insilmaril@721
  3406
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3407
	} else if (com=="paste")
insilmaril@721
  3408
	{
insilmaril@750
  3409
		if (!selti )
insilmaril@721
  3410
		{
insilmaril@721
  3411
			parser.setError (Aborted,"Nothing selected");
insilmaril@753
  3412
		} else if (! selbi )
insilmaril@721
  3413
		{				  
insilmaril@721
  3414
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3415
		} else if (parser.checkParCount(1))
insilmaril@721
  3416
		{	
insilmaril@721
  3417
			n=parser.parInt (ok,0);
insilmaril@721
  3418
			if (ok) pasteNoSave(n);
insilmaril@721
  3419
		}	
insilmaril@721
  3420
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3421
	} else if (com=="qa")
insilmaril@721
  3422
	{
insilmaril@750
  3423
		if (!selti )
insilmaril@721
  3424
		{
insilmaril@721
  3425
			parser.setError (Aborted,"Nothing selected");
insilmaril@753
  3426
		} else if (! selbi )
insilmaril@721
  3427
		{				  
insilmaril@721
  3428
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3429
		} else if (parser.checkParCount(4))
insilmaril@721
  3430
		{	
insilmaril@721
  3431
			QString c,u;
insilmaril@721
  3432
			c=parser.parString (ok,0);
insilmaril@721
  3433
			if (!ok)
insilmaril@721
  3434
			{
insilmaril@721
  3435
				parser.setError (Aborted,"No comment given");
insilmaril@721
  3436
			} else
insilmaril@721
  3437
			{
insilmaril@721
  3438
				s=parser.parString (ok,1);
insilmaril@721
  3439
				if (!ok)
insilmaril@721
  3440
				{
insilmaril@721
  3441
					parser.setError (Aborted,"First parameter is not a string");
insilmaril@721
  3442
				} else
insilmaril@721
  3443
				{
insilmaril@721
  3444
					t=parser.parString (ok,2);
insilmaril@721
  3445
					if (!ok)
insilmaril@721
  3446
					{
insilmaril@721
  3447
						parser.setError (Aborted,"Condition is not a string");
insilmaril@721
  3448
					} else
insilmaril@721
  3449
					{
insilmaril@721
  3450
						u=parser.parString (ok,3);
insilmaril@721
  3451
						if (!ok)
insilmaril@721
  3452
						{
insilmaril@721
  3453
							parser.setError (Aborted,"Third parameter is not a string");
insilmaril@721
  3454
						} else
insilmaril@721
  3455
						{
insilmaril@721
  3456
							if (s!="heading")
insilmaril@721
  3457
							{
insilmaril@721
  3458
								parser.setError (Aborted,"Unknown type: "+s);
insilmaril@721
  3459
							} else
insilmaril@721
  3460
							{
insilmaril@721
  3461
								if (! (t=="eq") ) 
insilmaril@721
  3462
								{
insilmaril@721
  3463
									parser.setError (Aborted,"Unknown operator: "+t);
insilmaril@721
  3464
								} else
insilmaril@721
  3465
								{
insilmaril@753
  3466
									if (! selbi    )
insilmaril@721
  3467
									{
insilmaril@721
  3468
										parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3469
									} else
insilmaril@721
  3470
									{
insilmaril@753
  3471
										if (selbi->getHeading() == u)
insilmaril@721
  3472
										{
insilmaril@721
  3473
											cout << "PASSED: " << qPrintable (c)  << endl;
insilmaril@721
  3474
										} else
insilmaril@721
  3475
										{
insilmaril@721
  3476
											cout << "FAILED: " << qPrintable (c)  << endl;
insilmaril@721
  3477
										}
insilmaril@721
  3478
									}
insilmaril@721
  3479
								}
insilmaril@721
  3480
							}
insilmaril@721
  3481
						} 
insilmaril@721
  3482
					} 
insilmaril@721
  3483
				} 
insilmaril@721
  3484
			}
insilmaril@721
  3485
		}	
insilmaril@721
  3486
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3487
	} else if (com=="saveImage")
insilmaril@721
  3488
	{
insilmaril@787
  3489
		ImageItem *ii=getSelectedImage();
insilmaril@776
  3490
		if (!ii )
insilmaril@721
  3491
		{
insilmaril@776
  3492
			parser.setError (Aborted,"No image selected");
insilmaril@721
  3493
		} else if (parser.checkParCount(2))
insilmaril@721
  3494
		{
insilmaril@721
  3495
			s=parser.parString(ok,0);
insilmaril@721
  3496
			if (ok)
insilmaril@721
  3497
			{
insilmaril@721
  3498
				t=parser.parString(ok,1);
insilmaril@776
  3499
				if (ok) saveFloatImageInt (ii,t,s);
insilmaril@721
  3500
			}
insilmaril@721
  3501
		}	
insilmaril@721
  3502
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3503
	} else if (com=="scroll")
insilmaril@721
  3504
	{
insilmaril@750
  3505
		if (!selti)
insilmaril@721
  3506
		{
insilmaril@721
  3507
			parser.setError (Aborted,"Nothing selected");
insilmaril@750
  3508
		} else if (! selbi )
insilmaril@721
  3509
		{				  
insilmaril@721
  3510
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3511
		} else if (parser.checkParCount(0))
insilmaril@721
  3512
		{	
insilmaril@750
  3513
			if (!scrollBranch (selbi))	
insilmaril@721
  3514
				parser.setError (Aborted,"Could not scroll branch");
insilmaril@721
  3515
		}	
insilmaril@721
  3516
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3517
	} else if (com=="select")
insilmaril@721
  3518
	{
insilmaril@721
  3519
		if (parser.checkParCount(1))
insilmaril@721
  3520
		{
insilmaril@721
  3521
			s=parser.parString(ok,0);
insilmaril@721
  3522
			if (ok) select (s);
insilmaril@721
  3523
		}	
insilmaril@721
  3524
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3525
	} else if (com=="selectLastBranch")
insilmaril@721
  3526
	{
insilmaril@750
  3527
		if (!selti )
insilmaril@721
  3528
		{
insilmaril@721
  3529
			parser.setError (Aborted,"Nothing selected");
insilmaril@753
  3530
		} else if (! selbi )
insilmaril@721
  3531
		{				  
insilmaril@721
  3532
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3533
		} else if (parser.checkParCount(0))
insilmaril@721
  3534
		{	
insilmaril@750
  3535
			BranchItem *bi=selbi->getLastBranch();
insilmaril@750
  3536
			if (!bi)
insilmaril@721
  3537
				parser.setError (Aborted,"Could not select last branch");
insilmaril@750
  3538
			select (bi);		// FIXME-3 was selectInt
insilmaril@721
  3539
				
insilmaril@721
  3540
		}	
insilmaril@721
  3541
	/////////////////////////////////////////////////////////////////////
insilmaril@753
  3542
	} else /* FIXME-2 if (com=="selectLastImage")
insilmaril@721
  3543
	{
insilmaril@750
  3544
		if (!selti )
insilmaril@721
  3545
		{
insilmaril@721
  3546
			parser.setError (Aborted,"Nothing selected");
insilmaril@753
  3547
		} else if (! selbi )
insilmaril@721
  3548
		{				  
insilmaril@721
  3549
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3550
		} else if (parser.checkParCount(0))
insilmaril@721
  3551
		{	
insilmaril@721
  3552
			FloatImageObj *fio=selb->getLastFloatImage();
insilmaril@721
  3553
			if (!fio)
insilmaril@721
  3554
				parser.setError (Aborted,"Could not select last image");
insilmaril@750
  3555
			select (fio);		// FIXME-3 was selectInt
insilmaril@721
  3556
				
insilmaril@721
  3557
		}	
insilmaril@721
  3558
	/////////////////////////////////////////////////////////////////////
insilmaril@753
  3559
	} else */ if (com=="selectLatestAdded")
insilmaril@721
  3560
	{
insilmaril@761
  3561
		if (!latestAddedItem)
insilmaril@721
  3562
		{
insilmaril@721
  3563
			parser.setError (Aborted,"No latest added object");
insilmaril@721
  3564
		} else
insilmaril@721
  3565
		{	
insilmaril@761
  3566
			if (!select (latestAddedItem))
insilmaril@761
  3567
				parser.setError (Aborted,"Could not select latest added object ");
insilmaril@721
  3568
		}	
insilmaril@721
  3569
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3570
	} else if (com=="setFrameType")
insilmaril@721
  3571
	{
insilmaril@735
  3572
		if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
insilmaril@721
  3573
		{
insilmaril@721
  3574
			parser.setError (Aborted,"Type of selection does not allow setting frame type");
insilmaril@721
  3575
		}
insilmaril@721
  3576
		else if (parser.checkParCount(1))
insilmaril@721
  3577
		{
insilmaril@721
  3578
			s=parser.parString(ok,0);
insilmaril@721
  3579
			if (ok) setFrameType (s);
insilmaril@721
  3580
		}	
insilmaril@721
  3581
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3582
	} else if (com=="setFramePenColor")
insilmaril@721
  3583
	{
insilmaril@735
  3584
		if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
insilmaril@721
  3585
		{
insilmaril@721
  3586
			parser.setError (Aborted,"Type of selection does not allow setting of pen color");
insilmaril@721
  3587
		}
insilmaril@721
  3588
		else if (parser.checkParCount(1))
insilmaril@721
  3589
		{
insilmaril@721
  3590
			QColor c=parser.parColor(ok,0);
insilmaril@721
  3591
			if (ok) setFramePenColor (c);
insilmaril@721
  3592
		}	
insilmaril@721
  3593
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3594
	} else if (com=="setFrameBrushColor")
insilmaril@721
  3595
	{
insilmaril@735
  3596
		if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
insilmaril@721
  3597
		{
insilmaril@721
  3598
			parser.setError (Aborted,"Type of selection does not allow setting brush color");
insilmaril@721
  3599
		}
insilmaril@721
  3600
		else if (parser.checkParCount(1))
insilmaril@721
  3601
		{
insilmaril@721
  3602
			QColor c=parser.parColor(ok,0);
insilmaril@721
  3603
			if (ok) setFrameBrushColor (c);
insilmaril@721
  3604
		}	
insilmaril@721
  3605
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3606
	} else if (com=="setFramePadding")
insilmaril@721
  3607
	{
insilmaril@735
  3608
		if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
insilmaril@721
  3609
		{
insilmaril@721
  3610
			parser.setError (Aborted,"Type of selection does not allow setting frame padding");
insilmaril@721
  3611
		}
insilmaril@721
  3612
		else if (parser.checkParCount(1))
insilmaril@721
  3613
		{
insilmaril@721
  3614
			n=parser.parInt(ok,0);
insilmaril@721
  3615
			if (ok) setFramePadding(n);
insilmaril@721
  3616
		}	
insilmaril@721
  3617
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3618
	} else if (com=="setFrameBorderWidth")
insilmaril@721
  3619
	{
insilmaril@735
  3620
		if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
insilmaril@721
  3621
		{
insilmaril@721
  3622
			parser.setError (Aborted,"Type of selection does not allow setting frame border width");
insilmaril@721
  3623
		}
insilmaril@721
  3624
		else if (parser.checkParCount(1))
insilmaril@721
  3625
		{
insilmaril@721
  3626
			n=parser.parInt(ok,0);
insilmaril@721
  3627
			if (ok) setFrameBorderWidth (n);
insilmaril@721
  3628
		}	
insilmaril@721
  3629
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3630
	} else if (com=="setMapAuthor")
insilmaril@721
  3631
	{
insilmaril@721
  3632
		if (parser.checkParCount(1))
insilmaril@721
  3633
		{
insilmaril@721
  3634
			s=parser.parString(ok,0);
insilmaril@721
  3635
			if (ok) setAuthor (s);
insilmaril@721
  3636
		}	
insilmaril@721
  3637
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3638
	} else if (com=="setMapComment")
insilmaril@721
  3639
	{
insilmaril@721
  3640
		if (parser.checkParCount(1))
insilmaril@721
  3641
		{
insilmaril@721
  3642
			s=parser.parString(ok,0);
insilmaril@721
  3643
			if (ok) setComment(s);
insilmaril@721
  3644
		}	
insilmaril@721
  3645
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3646
	} else if (com=="setMapBackgroundColor")
insilmaril@721
  3647
	{
insilmaril@750
  3648
		if (!selti )
insilmaril@721
  3649
		{
insilmaril@721
  3650
			parser.setError (Aborted,"Nothing selected");
insilmaril@753
  3651
		} else if (! selbi )
insilmaril@721
  3652
		{				  
insilmaril@721
  3653
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3654
		} else if (parser.checkParCount(1))
insilmaril@721
  3655
		{
insilmaril@721
  3656
			QColor c=parser.parColor (ok,0);
insilmaril@721
  3657
			if (ok) setMapBackgroundColor (c);
insilmaril@721
  3658
		}	
insilmaril@721
  3659
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3660
	} else if (com=="setMapDefLinkColor")
insilmaril@721
  3661
	{
insilmaril@750
  3662
		if (!selti )
insilmaril@721
  3663
		{
insilmaril@721
  3664
			parser.setError (Aborted,"Nothing selected");
insilmaril@753
  3665
		} else if (! selbi )
insilmaril@721
  3666
		{				  
insilmaril@721
  3667
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3668
		} else if (parser.checkParCount(1))
insilmaril@721
  3669
		{
insilmaril@721
  3670
			QColor c=parser.parColor (ok,0);
insilmaril@721
  3671
			if (ok) setMapDefLinkColor (c);
insilmaril@721
  3672
		}	
insilmaril@721
  3673
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3674
	} else if (com=="setMapLinkStyle")
insilmaril@721
  3675
	{
insilmaril@721
  3676
		if (parser.checkParCount(1))
insilmaril@721
  3677
		{
insilmaril@721
  3678
			s=parser.parString (ok,0);
insilmaril@721
  3679
			if (ok) setMapLinkStyle(s);
insilmaril@721
  3680
		}	
insilmaril@721
  3681
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3682
	} else if (com=="setHeading")
insilmaril@721
  3683
	{
insilmaril@750
  3684
		if (!selti )
insilmaril@721
  3685
		{
insilmaril@721
  3686
			parser.setError (Aborted,"Nothing selected");
insilmaril@753
  3687
		} else if (! selbi )
insilmaril@721
  3688
		{				  
insilmaril@721
  3689
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3690
		} else if (parser.checkParCount(1))
insilmaril@721
  3691
		{
insilmaril@721
  3692
			s=parser.parString (ok,0);
insilmaril@721
  3693
			if (ok) 
insilmaril@721
  3694
				setHeading (s);
insilmaril@721
  3695
		}	
insilmaril@721
  3696
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3697
	} else if (com=="setHideExport")
insilmaril@721
  3698
	{
insilmaril@750
  3699
		if (!selti )
insilmaril@721
  3700
		{
insilmaril@721
  3701
			parser.setError (Aborted,"Nothing selected");
insilmaril@735
  3702
		} else if (selectionType()!=TreeItem::Branch && selectionType() != TreeItem::MapCenter &&selectionType()!=TreeItem::Image)
insilmaril@721
  3703
		{				  
insilmaril@721
  3704
			parser.setError (Aborted,"Type of selection is not a branch or floatimage");
insilmaril@721
  3705
		} else if (parser.checkParCount(1))
insilmaril@721
  3706
		{
insilmaril@721
  3707
			b=parser.parBool(ok,0);
insilmaril@721
  3708
			if (ok) setHideExport (b);
insilmaril@721
  3709
		}
insilmaril@721
  3710
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3711
	} else if (com=="setIncludeImagesHorizontally")
insilmaril@721
  3712
	{ 
insilmaril@750
  3713
		if (!selti )
insilmaril@721
  3714
		{
insilmaril@721
  3715
			parser.setError (Aborted,"Nothing selected");
insilmaril@753
  3716
		} else if (! selbi)
insilmaril@721
  3717
		{				  
insilmaril@721
  3718
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3719
		} else if (parser.checkParCount(1))
insilmaril@721
  3720
		{
insilmaril@721
  3721
			b=parser.parBool(ok,0);
insilmaril@721
  3722
			if (ok) setIncludeImagesHor(b);
insilmaril@721
  3723
		}
insilmaril@721
  3724
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3725
	} else if (com=="setIncludeImagesVertically")
insilmaril@721
  3726
	{
insilmaril@750
  3727
		if (!selti )
insilmaril@721
  3728
		{
insilmaril@721
  3729
			parser.setError (Aborted,"Nothing selected");
insilmaril@753
  3730
		} else if (! selbi)
insilmaril@721
  3731
		{				  
insilmaril@721
  3732
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3733
		} else if (parser.checkParCount(1))
insilmaril@721
  3734
		{
insilmaril@721
  3735
			b=parser.parBool(ok,0);
insilmaril@721
  3736
			if (ok) setIncludeImagesVer(b);
insilmaril@721
  3737
		}
insilmaril@721
  3738
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3739
	} else if (com=="setHideLinkUnselected")
insilmaril@721
  3740
	{
insilmaril@750
  3741
		if (!selti )
insilmaril@721
  3742
		{
insilmaril@721
  3743
			parser.setError (Aborted,"Nothing selected");
insilmaril@735
  3744
		} else if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
insilmaril@721
  3745
		{				  
insilmaril@721
  3746
			parser.setError (Aborted,"Type of selection does not allow hiding the link");
insilmaril@721
  3747
		} else if (parser.checkParCount(1))
insilmaril@721
  3748
		{
insilmaril@721
  3749
			b=parser.parBool(ok,0);
insilmaril@721
  3750
			if (ok) setHideLinkUnselected(b);
insilmaril@721
  3751
		}
insilmaril@721
  3752
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3753
	} else if (com=="setSelectionColor")
insilmaril@721
  3754
	{
insilmaril@721
  3755
		if (parser.checkParCount(1))
insilmaril@721
  3756
		{
insilmaril@721
  3757
			QColor c=parser.parColor (ok,0);
insilmaril@721
  3758
			if (ok) setSelectionColorInt (c);
insilmaril@721
  3759
		}	
insilmaril@721
  3760
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3761
	} else if (com=="setURL")
insilmaril@721
  3762
	{
insilmaril@750
  3763
		if (!selti )
insilmaril@721
  3764
		{
insilmaril@721
  3765
			parser.setError (Aborted,"Nothing selected");
insilmaril@753
  3766
		} else if (! selbi )
insilmaril@721
  3767
		{				  
insilmaril@721
  3768
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3769
		} else if (parser.checkParCount(1))
insilmaril@721
  3770
		{
insilmaril@721
  3771
			s=parser.parString (ok,0);
insilmaril@721
  3772
			if (ok) setURL(s);
insilmaril@721
  3773
		}	
insilmaril@721
  3774
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3775
	} else if (com=="setVymLink")
insilmaril@721
  3776
	{
insilmaril@750
  3777
		if (!selti )
insilmaril@721
  3778
		{
insilmaril@721
  3779
			parser.setError (Aborted,"Nothing selected");
insilmaril@753
  3780
		} else if (! selbi )
insilmaril@721
  3781
		{				  
insilmaril@721
  3782
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3783
		} else if (parser.checkParCount(1))
insilmaril@721
  3784
		{
insilmaril@721
  3785
			s=parser.parString (ok,0);
insilmaril@721
  3786
			if (ok) setVymLink(s);
insilmaril@721
  3787
		}	
insilmaril@721
  3788
	}
insilmaril@721
  3789
	/////////////////////////////////////////////////////////////////////
insilmaril@767
  3790
	else if (com=="setFlag")
insilmaril@721
  3791
	{
insilmaril@750
  3792
		if (!selti )
insilmaril@721
  3793
		{
insilmaril@721
  3794
			parser.setError (Aborted,"Nothing selected");
insilmaril@753
  3795
		} else if (! selbi )
insilmaril@721
  3796
		{				  
insilmaril@721
  3797
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3798
		} else if (parser.checkParCount(1))
insilmaril@721
  3799
		{
insilmaril@721
  3800
			s=parser.parString(ok,0);
insilmaril@721
  3801
			if (ok) 
insilmaril@767
  3802
				selbi->activateStandardFlag(s);
insilmaril@721
  3803
		}
insilmaril@721
  3804
	/////////////////////////////////////////////////////////////////////
insilmaril@767
  3805
	} else /* FIXME-2 if (com=="setFrameType")
insilmaril@721
  3806
	{
insilmaril@750
  3807
		if (!selti )
insilmaril@721
  3808
		{
insilmaril@721
  3809
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  3810
		} else if (! selb )
insilmaril@721
  3811
		{				  
insilmaril@721
  3812
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3813
		} else if (parser.checkParCount(1))
insilmaril@721
  3814
		{
insilmaril@721
  3815
			s=parser.parString(ok,0);
insilmaril@721
  3816
			if (ok) 
insilmaril@721
  3817
				setFrameType (s);
insilmaril@721
  3818
		}
insilmaril@721
  3819
	/////////////////////////////////////////////////////////////////////
insilmaril@753
  3820
	} else*/ if (com=="sortChildren")
insilmaril@721
  3821
	{
insilmaril@750
  3822
		if (!selti )
insilmaril@721
  3823
		{
insilmaril@721
  3824
			parser.setError (Aborted,"Nothing selected");
insilmaril@753
  3825
		} else if (! selbi )
insilmaril@721
  3826
		{				  
insilmaril@721
  3827
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3828
		} else if (parser.checkParCount(0))
insilmaril@721
  3829
		{
insilmaril@721
  3830
			sortChildren();
insilmaril@721
  3831
		}
insilmaril@721
  3832
	/////////////////////////////////////////////////////////////////////
insilmaril@767
  3833
	} else if (com=="toggleFlag")
insilmaril@721
  3834
	{
insilmaril@750
  3835
		if (!selti )
insilmaril@721
  3836
		{
insilmaril@721
  3837
			parser.setError (Aborted,"Nothing selected");
insilmaril@753
  3838
		} else if (! selbi )
insilmaril@721
  3839
		{				  
insilmaril@721
  3840
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3841
		} else if (parser.checkParCount(1))
insilmaril@721
  3842
		{
insilmaril@721
  3843
			s=parser.parString(ok,0);
insilmaril@721
  3844
			if (ok) 
insilmaril@753
  3845
				selbi->toggleStandardFlag(s);	
insilmaril@721
  3846
		}
insilmaril@721
  3847
	/////////////////////////////////////////////////////////////////////
insilmaril@767
  3848
	} else  if (com=="unscroll")
insilmaril@721
  3849
	{
insilmaril@750
  3850
		if (!selti)
insilmaril@721
  3851
		{
insilmaril@721
  3852
			parser.setError (Aborted,"Nothing selected");
insilmaril@753
  3853
		} else if (! selbi )
insilmaril@721
  3854
		{				  
insilmaril@721
  3855
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3856
		} else if (parser.checkParCount(0))
insilmaril@721
  3857
		{	
insilmaril@750
  3858
			if (!unscrollBranch (selbi))	
insilmaril@721
  3859
				parser.setError (Aborted,"Could not unscroll branch");
insilmaril@721
  3860
		}	
insilmaril@721
  3861
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  3862
	} else if (com=="unscrollChildren")
insilmaril@721
  3863
	{
insilmaril@750
  3864
		if (!selti)
insilmaril@721
  3865
		{
insilmaril@721
  3866
			parser.setError (Aborted,"Nothing selected");
insilmaril@753
  3867
		} else if (! selbi )
insilmaril@721
  3868
		{				  
insilmaril@721
  3869
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3870
		} else if (parser.checkParCount(0))
insilmaril@721
  3871
		{	
insilmaril@721
  3872
			unscrollChildren ();
insilmaril@721
  3873
		}	
insilmaril@721
  3874
	/////////////////////////////////////////////////////////////////////
insilmaril@767
  3875
	} else if (com=="unsetFlag")
insilmaril@721
  3876
	{
insilmaril@767
  3877
		if (!selti)
insilmaril@721
  3878
		{
insilmaril@721
  3879
			parser.setError (Aborted,"Nothing selected");
insilmaril@753
  3880
		} else if (! selbi )
insilmaril@721
  3881
		{				  
insilmaril@721
  3882
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  3883
		} else if (parser.checkParCount(1))
insilmaril@721
  3884
		{
insilmaril@721
  3885
			s=parser.parString(ok,0);
insilmaril@721
  3886
			if (ok) 
insilmaril@767
  3887
				selbi->deactivateStandardFlag(s);
insilmaril@721
  3888
		}
insilmaril@767
  3889
	} else 
insilmaril@721
  3890
		parser.setError (Aborted,"Unknown command");
insilmaril@721
  3891
insilmaril@721
  3892
	// Any errors?
insilmaril@721
  3893
	if (parser.errorLevel()==NoError)
insilmaril@721
  3894
	{
insilmaril@746
  3895
		// setChanged();  FIXME-2 should not be called e.g. for export?!
insilmaril@721
  3896
		reposition();
insilmaril@721
  3897
	}	
insilmaril@721
  3898
	else	
insilmaril@721
  3899
	{
insilmaril@721
  3900
		// TODO Error handling
insilmaril@721
  3901
		qWarning("VymModel::parseAtom: Error!");
insilmaril@721
  3902
		qWarning(parser.errorMessage());
insilmaril@721
  3903
	} 
insilmaril@721
  3904
}
insilmaril@721
  3905
insilmaril@721
  3906
void VymModel::runScript (QString script)
insilmaril@721
  3907
{
insilmaril@721
  3908
	parser.setScript (script);
insilmaril@721
  3909
	parser.runScript();
insilmaril@721
  3910
	while (parser.next() ) 
insilmaril@721
  3911
		parseAtom(parser.getAtom());
insilmaril@721
  3912
}
insilmaril@721
  3913
insilmaril@721
  3914
void VymModel::setExportMode (bool b)
insilmaril@721
  3915
{
insilmaril@721
  3916
	// should be called before and after exports
insilmaril@721
  3917
	// depending on the settings
insilmaril@721
  3918
	if (b && settings.value("/export/useHideExport","true")=="true")
insilmaril@753
  3919
		setHideTmpMode (TreeItem::HideExport);
insilmaril@721
  3920
	else	
insilmaril@753
  3921
		setHideTmpMode (TreeItem::HideNone);
insilmaril@721
  3922
}
insilmaril@721
  3923
insilmaril@721
  3924
void VymModel::exportImage(QString fname, bool askName, QString format)
insilmaril@721
  3925
{
insilmaril@721
  3926
	if (fname=="")
insilmaril@721
  3927
	{
insilmaril@721
  3928
		fname=getMapName()+".png";
insilmaril@721
  3929
		format="PNG";
insilmaril@721
  3930
	} 	
insilmaril@721
  3931
insilmaril@721
  3932
	if (askName)
insilmaril@721
  3933
	{
insilmaril@721
  3934
		QStringList fl;
insilmaril@721
  3935
		QFileDialog *fd=new QFileDialog (NULL);
insilmaril@721
  3936
		fd->setCaption (tr("Export map as image"));
insilmaril@721
  3937
		fd->setDirectory (lastImageDir);
insilmaril@721
  3938
		fd->setFileMode(QFileDialog::AnyFile);
insilmaril@721
  3939
		fd->setFilters  (imageIO.getFilters() );
insilmaril@721
  3940
		if (fd->exec())
insilmaril@721
  3941
		{
insilmaril@721
  3942
			fl=fd->selectedFiles();
insilmaril@721
  3943
			fname=fl.first();
insilmaril@721
  3944
			format=imageIO.getType(fd->selectedFilter());
insilmaril@721
  3945
		} 
insilmaril@721
  3946
	}
insilmaril@721
  3947
insilmaril@721
  3948
	setExportMode (true);
insilmaril@784
  3949
	QPixmap pix (mapEditor->getPixmap());
insilmaril@721
  3950
	pix.save(fname, format);
insilmaril@721
  3951
	setExportMode (false);
insilmaril@721
  3952
}
insilmaril@721
  3953
insilmaril@721
  3954
insilmaril@721
  3955
void VymModel::exportXML(QString dir, bool askForName)
insilmaril@721
  3956
{
insilmaril@721
  3957
	if (askForName)
insilmaril@721
  3958
	{
insilmaril@721
  3959
		dir=browseDirectory(NULL,tr("Export XML to directory"));
insilmaril@721
  3960
		if (dir =="" && !reallyWriteDirectory(dir) )
insilmaril@721
  3961
		return;
insilmaril@721
  3962
	}
insilmaril@721
  3963
insilmaril@721
  3964
	// Hide stuff during export, if settings want this
insilmaril@721
  3965
	setExportMode (true);
insilmaril@721
  3966
insilmaril@721
  3967
	// Create subdirectories
insilmaril@721
  3968
	makeSubDirs (dir);
insilmaril@721
  3969
insilmaril@784
  3970
	// write to directory	//FIXME-4 check totalBBox here...
insilmaril@784
  3971
	QString saveFile=saveToDir (dir,mapName+"-",true,mapEditor->getTotalBBox().topLeft() ,NULL); 
insilmaril@721
  3972
	QFile file;
insilmaril@721
  3973
insilmaril@721
  3974
	file.setName ( dir + "/"+mapName+".xml");
insilmaril@721
  3975
	if ( !file.open( QIODevice::WriteOnly ) )
insilmaril@721
  3976
	{
insilmaril@721
  3977
		// This should neverever happen
insilmaril@721
  3978
		QMessageBox::critical (0,tr("Critical Export Error"),tr("VymModel::exportXML couldn't open %1").arg(file.name()));
insilmaril@721
  3979
		return;
insilmaril@721
  3980
	}	
insilmaril@721
  3981
insilmaril@721
  3982
	// Write it finally, and write in UTF8, no matter what 
insilmaril@721
  3983
	QTextStream ts( &file );
insilmaril@721
  3984
	ts.setEncoding (QTextStream::UnicodeUTF8);
insilmaril@721
  3985
	ts << saveFile;
insilmaril@721
  3986
	file.close();
insilmaril@721
  3987
insilmaril@721
  3988
	// Now write image, too
insilmaril@721
  3989
	exportImage (dir+"/images/"+mapName+".png",false,"PNG");
insilmaril@721
  3990
insilmaril@721
  3991
	setExportMode (false);
insilmaril@721
  3992
}
insilmaril@721
  3993
insilmaril@721
  3994
void VymModel::exportASCII(QString fname,bool askName)
insilmaril@721
  3995
{
insilmaril@721
  3996
	ExportASCII ex;
insilmaril@721
  3997
	ex.setModel (this);
insilmaril@721
  3998
	if (fname=="") 
insilmaril@721
  3999
		ex.setFile (mapName+".txt");	
insilmaril@721
  4000
	else
insilmaril@721
  4001
		ex.setFile (fname);
insilmaril@721
  4002
insilmaril@721
  4003
	if (askName)
insilmaril@721
  4004
	{
insilmaril@721
  4005
		//ex.addFilter ("TXT (*.txt)");
insilmaril@721
  4006
		ex.setDir(lastImageDir);
insilmaril@721
  4007
		//ex.setCaption(vymName+ " -" +tr("Export as ASCII")+" "+tr("(still experimental)"));
insilmaril@721
  4008
		ex.execDialog() ; 
insilmaril@721
  4009
	} 
insilmaril@721
  4010
	if (!ex.canceled())
insilmaril@721
  4011
	{
insilmaril@721
  4012
		setExportMode(true);
insilmaril@721
  4013
		ex.doExport();
insilmaril@721
  4014
		setExportMode(false);
insilmaril@721
  4015
	}
insilmaril@721
  4016
}
insilmaril@721
  4017
insilmaril@721
  4018
void VymModel::exportXHTML (const QString &dir, bool askForName)
insilmaril@721
  4019
{
insilmaril@721
  4020
			ExportXHTMLDialog dia(NULL);
insilmaril@721
  4021
			dia.setFilePath (filePath );
insilmaril@721
  4022
			dia.setMapName (mapName );
insilmaril@721
  4023
			dia.readSettings();
insilmaril@721
  4024
			if (dir!="") dia.setDir (dir);
insilmaril@721
  4025
insilmaril@721
  4026
			bool ok=true;
insilmaril@721
  4027
			
insilmaril@721
  4028
			if (askForName)
insilmaril@721
  4029
			{
insilmaril@721
  4030
				if (dia.exec()!=QDialog::Accepted) 
insilmaril@721
  4031
					ok=false;
insilmaril@721
  4032
				else	
insilmaril@721
  4033
				{
insilmaril@721
  4034
					QDir d (dia.getDir());
insilmaril@721
  4035
					// Check, if warnings should be used before overwriting
insilmaril@721
  4036
					// the output directory
insilmaril@721
  4037
					if (d.exists() && d.count()>0)
insilmaril@721
  4038
					{
insilmaril@721
  4039
						WarningDialog warn;
insilmaril@721
  4040
						warn.showCancelButton (true);
insilmaril@721
  4041
						warn.setText(QString(
insilmaril@721
  4042
							"The directory %1 is not empty.\n"
insilmaril@721
  4043
							"Do you risk to overwrite some of its contents?").arg(d.path() ));
insilmaril@721
  4044
						warn.setCaption("Warning: Directory not empty");
insilmaril@721
  4045
						warn.setShowAgainName("mainwindow/overwrite-dir-xhtml");
insilmaril@721
  4046
insilmaril@721
  4047
						if (warn.exec()!=QDialog::Accepted) ok=false;
insilmaril@721
  4048
					}
insilmaril@721
  4049
				}	
insilmaril@721
  4050
			}
insilmaril@721
  4051
insilmaril@721
  4052
			if (ok)
insilmaril@721
  4053
			{
insilmaril@721
  4054
				exportXML (dia.getDir(),false );
insilmaril@721
  4055
				dia.doExport(mapName );
insilmaril@721
  4056
				//if (dia.hasChanged()) setChanged();
insilmaril@721
  4057
			}
insilmaril@721
  4058
}
insilmaril@721
  4059
insilmaril@721
  4060
void VymModel::exportOOPresentation(const QString &fn, const QString &cf)
insilmaril@721
  4061
{
insilmaril@721
  4062
	ExportOO ex;
insilmaril@721
  4063
	ex.setFile (fn);
insilmaril@721
  4064
	ex.setModel (this);
insilmaril@721
  4065
	if (ex.setConfigFile(cf)) 
insilmaril@721
  4066
	{
insilmaril@721
  4067
		setExportMode (true);
insilmaril@721
  4068
		ex.exportPresentation();
insilmaril@721
  4069
		setExportMode (false);
insilmaril@721
  4070
	}
insilmaril@721
  4071
}
insilmaril@721
  4072
insilmaril@721
  4073
insilmaril@721
  4074
insilmaril@650
  4075
insilmaril@652
  4076
//////////////////////////////////////////////
insilmaril@652
  4077
// View related
insilmaril@652
  4078
//////////////////////////////////////////////
insilmaril@652
  4079
insilmaril@723
  4080
void VymModel::registerEditor(QWidget *me)
insilmaril@721
  4081
{
insilmaril@723
  4082
	mapEditor=(MapEditor*)me;
insilmaril@721
  4083
}
insilmaril@721
  4084
insilmaril@721
  4085
void VymModel::unregisterEditor(QWidget *)
insilmaril@721
  4086
{
insilmaril@723
  4087
	mapEditor=NULL;
insilmaril@723
  4088
}
insilmaril@723
  4089
insilmaril@723
  4090
void VymModel::setContextPos(QPointF p)
insilmaril@723
  4091
{
insilmaril@723
  4092
	contextPos=p;
insilmaril@723
  4093
}
insilmaril@723
  4094
insilmaril@723
  4095
void VymModel::unsetContextPos()
insilmaril@723
  4096
{
insilmaril@723
  4097
	contextPos=QPointF();
insilmaril@721
  4098
}
insilmaril@721
  4099
insilmaril@721
  4100
void VymModel::updateNoteFlag()
insilmaril@721
  4101
{
insilmaril@721
  4102
	setChanged();
insilmaril@767
  4103
	TreeItem *selti=getSelectedItem();
insilmaril@767
  4104
	if (selti)
insilmaril@721
  4105
	{
insilmaril@767
  4106
		if (textEditor->isEmpty()) 
insilmaril@767
  4107
			selti->clearNote();
insilmaril@767
  4108
		else
insilmaril@767
  4109
			selti->setNote (textEditor->getText());
insilmaril@767
  4110
		emitDataHasChanged(selti);		
insilmaril@775
  4111
		emitSelectionChanged();
insilmaril@767
  4112
insilmaril@767
  4113
	}
insilmaril@721
  4114
}
insilmaril@721
  4115
insilmaril@795
  4116
void VymModel::reposition()	//FIXME-4 VM should have no need to reposition, but the views...
insilmaril@761
  4117
{
insilmaril@761
  4118
	//cout << "VM::reposition blocked="<<blockReposition<<endl;
insilmaril@761
  4119
	if (blockReposition) return;
insilmaril@761
  4120
insilmaril@754
  4121
	for (int i=0;i<rootItem->branchCount(); i++)
insilmaril@754
  4122
		rootItem->getBranchObjNum(i)->reposition();	//	for positioning heading
insilmaril@795
  4123
	//emitSelectionChanged();	
insilmaril@652
  4124
}
insilmaril@652
  4125
insilmaril@721
  4126
insilmaril@721
  4127
void VymModel::setMapLinkStyle (const QString & s)
insilmaril@721
  4128
{
insilmaril@721
  4129
	QString snow;
insilmaril@727
  4130
	switch (linkstyle)
insilmaril@727
  4131
	{
insilmaril@727
  4132
		case LinkableMapObj::Line :
insilmaril@727
  4133
			snow="StyleLine";
insilmaril@727
  4134
			break;
insilmaril@727
  4135
		case LinkableMapObj::Parabel:
insilmaril@727
  4136
			snow="StyleParabel";
insilmaril@727
  4137
			break;
insilmaril@727
  4138
		case LinkableMapObj::PolyLine:
insilmaril@727
  4139
			snow="StylePolyLine";
insilmaril@727
  4140
			break;
insilmaril@727
  4141
		case LinkableMapObj::PolyParabel:
insilmaril@727
  4142
			snow="StylePolyParabel";
insilmaril@727
  4143
			break;
insilmaril@727
  4144
		default:	
insilmaril@727
  4145
			snow="UndefinedStyle";
insilmaril@727
  4146
			break;
insilmaril@727
  4147
	}
insilmaril@721
  4148
insilmaril@721
  4149
	saveState (
insilmaril@721
  4150
		QString("setMapLinkStyle (\"%1\")").arg(s),
insilmaril@721
  4151
		QString("setMapLinkStyle (\"%1\")").arg(snow),
insilmaril@721
  4152
		QString("Set map link style (\"%1\")").arg(s)
insilmaril@721
  4153
	);	
insilmaril@721
  4154
insilmaril@721
  4155
	if (s=="StyleLine")
insilmaril@721
  4156
		linkstyle=LinkableMapObj::Line;
insilmaril@721
  4157
	else if (s=="StyleParabel")
insilmaril@721
  4158
		linkstyle=LinkableMapObj::Parabel;
insilmaril@721
  4159
	else if (s=="StylePolyLine")
insilmaril@721
  4160
		linkstyle=LinkableMapObj::PolyLine;
insilmaril@727
  4161
	else if (s=="StylePolyParabel")	
insilmaril@721
  4162
		linkstyle=LinkableMapObj::PolyParabel;
insilmaril@727
  4163
	else
insilmaril@727
  4164
		linkstyle=LinkableMapObj::UndefinedStyle;
insilmaril@721
  4165
insilmaril@750
  4166
	BranchItem *cur=NULL;
insilmaril@750
  4167
	BranchItem *prev=NULL;
insilmaril@721
  4168
	BranchObj *bo;
insilmaril@773
  4169
	next (cur,prev);
insilmaril@741
  4170
	while (cur) 
insilmaril@721
  4171
	{
insilmaril@741
  4172
		bo=(BranchObj*)(cur->getLMO() );
insilmaril@786
  4173
		bo->setLinkStyle(bo->getDefLinkStyle(cur->parent() ));	//FIXME-3 better emit dataCHanged and leave the changes to View
insilmaril@773
  4174
		cur=next(cur,prev);
insilmaril@721
  4175
	}
insilmaril@721
  4176
	reposition();
insilmaril@721
  4177
}
insilmaril@721
  4178
insilmaril@721
  4179
LinkableMapObj::Style VymModel::getMapLinkStyle ()
insilmaril@721
  4180
{
insilmaril@721
  4181
	return linkstyle;
insilmaril@721
  4182
}	
insilmaril@721
  4183
insilmaril@721
  4184
void VymModel::setMapDefLinkColor(QColor col)
insilmaril@721
  4185
{
insilmaril@721
  4186
	if ( !col.isValid() ) return;
insilmaril@721
  4187
	saveState (
insilmaril@721
  4188
		QString("setMapDefLinkColor (\"%1\")").arg(getMapDefLinkColor().name()),
insilmaril@721
  4189
		QString("setMapDefLinkColor (\"%1\")").arg(col.name()),
insilmaril@721
  4190
		QString("Set map link color to %1").arg(col.name())
insilmaril@721
  4191
	);
insilmaril@721
  4192
insilmaril@721
  4193
	defLinkColor=col;
insilmaril@750
  4194
	BranchItem *cur=NULL;
insilmaril@750
  4195
	BranchItem *prev=NULL;
insilmaril@721
  4196
	BranchObj *bo;
insilmaril@773
  4197
	cur=next(cur,prev);
insilmaril@741
  4198
	while (cur) 
insilmaril@721
  4199
	{
insilmaril@741
  4200
		bo=(BranchObj*)(cur->getLMO() );
insilmaril@721
  4201
		bo->setLinkColor();
insilmaril@773
  4202
		next(cur,prev);
insilmaril@721
  4203
	}
insilmaril@721
  4204
	updateActions();
insilmaril@721
  4205
}
insilmaril@721
  4206
insilmaril@721
  4207
void VymModel::setMapLinkColorHintInt()
insilmaril@721
  4208
{
insilmaril@721
  4209
	// called from setMapLinkColorHint(lch) or at end of parse
insilmaril@750
  4210
	BranchItem *cur=NULL;
insilmaril@750
  4211
	BranchItem *prev=NULL;
insilmaril@721
  4212
	BranchObj *bo;
insilmaril@773
  4213
	cur=next(cur,prev);
insilmaril@741
  4214
	while (cur) 
insilmaril@721
  4215
	{
insilmaril@741
  4216
		bo=(BranchObj*)(cur->getLMO() );
insilmaril@721
  4217
		bo->setLinkColor();
insilmaril@773
  4218
		cur=next(cur,prev);
insilmaril@721
  4219
	}
insilmaril@721
  4220
}
insilmaril@721
  4221
insilmaril@721
  4222
void VymModel::setMapLinkColorHint(LinkableMapObj::ColorHint lch)
insilmaril@721
  4223
{
insilmaril@721
  4224
	linkcolorhint=lch;
insilmaril@721
  4225
	setMapLinkColorHintInt();
insilmaril@721
  4226
}
insilmaril@721
  4227
insilmaril@721
  4228
void VymModel::toggleMapLinkColorHint()
insilmaril@721
  4229
{
insilmaril@721
  4230
	if (linkcolorhint==LinkableMapObj::HeadingColor)
insilmaril@721
  4231
		linkcolorhint=LinkableMapObj::DefaultColor;
insilmaril@721
  4232
	else	
insilmaril@721
  4233
		linkcolorhint=LinkableMapObj::HeadingColor;
insilmaril@750
  4234
	BranchItem *cur=NULL;
insilmaril@750
  4235
	BranchItem *prev=NULL;
insilmaril@721
  4236
	BranchObj *bo;
insilmaril@773
  4237
	cur=next(cur,prev);
insilmaril@741
  4238
	while (cur) 
insilmaril@721
  4239
	{
insilmaril@741
  4240
		bo=(BranchObj*)(cur->getLMO() );
insilmaril@721
  4241
		bo->setLinkColor();
insilmaril@773
  4242
		next(cur,prev);
insilmaril@721
  4243
	}
insilmaril@721
  4244
}
insilmaril@721
  4245
insilmaril@746
  4246
void VymModel::selectMapBackgroundImage ()	// FIXME-2 move to ME
insilmaril@798
  4247
// FIXME-4 for using background image: view.setCacheMode(QGraphicsView::CacheBackground);
insilmaril@721
  4248
{
insilmaril@721
  4249
	Q3FileDialog *fd=new Q3FileDialog( NULL);
insilmaril@721
  4250
	fd->setMode (Q3FileDialog::ExistingFile);
insilmaril@721
  4251
	fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
insilmaril@721
  4252
	ImagePreview *p =new ImagePreview (fd);
insilmaril@721
  4253
	fd->setContentsPreviewEnabled( TRUE );
insilmaril@721
  4254
	fd->setContentsPreview( p, p );
insilmaril@721
  4255
	fd->setPreviewMode( Q3FileDialog::Contents );
insilmaril@721
  4256
	fd->setCaption(vymName+" - " +tr("Load background image"));
insilmaril@721
  4257
	fd->setDir (lastImageDir);
insilmaril@721
  4258
	fd->show();
insilmaril@721
  4259
insilmaril@721
  4260
	if ( fd->exec() == QDialog::Accepted )
insilmaril@721
  4261
	{
insilmaril@721
  4262
		// TODO selectMapBackgroundImg in QT4 use:	lastImageDir=fd->directory();
insilmaril@721
  4263
		lastImageDir=QDir (fd->dirPath());
insilmaril@721
  4264
		setMapBackgroundImage (fd->selectedFile());
insilmaril@721
  4265
	}
insilmaril@721
  4266
}	
insilmaril@721
  4267
insilmaril@795
  4268
void VymModel::setMapBackgroundImage (const QString &fn)	//FIXME-3 missing savestate, move to ME
insilmaril@721
  4269
{
insilmaril@721
  4270
	QColor oldcol=mapScene->backgroundBrush().color();
insilmaril@721
  4271
	/*
insilmaril@721
  4272
	saveState(
insilmaril@721
  4273
		selection,
insilmaril@721
  4274
		QString ("setMapBackgroundImage (%1)").arg(oldcol.name()),
insilmaril@721
  4275
		selection,
insilmaril@721
  4276
		QString ("setMapBackgroundImage (%1)").arg(col.name()),
insilmaril@721
  4277
		QString("Set background color of map to %1").arg(col.name()));
insilmaril@721
  4278
	*/	
insilmaril@721
  4279
	QBrush brush;
insilmaril@721
  4280
	brush.setTextureImage (QPixmap (fn));
insilmaril@721
  4281
	mapScene->setBackgroundBrush(brush);
insilmaril@721
  4282
}
insilmaril@721
  4283
insilmaril@753
  4284
void VymModel::selectMapBackgroundColor()	// FIXME-3 move to ME
insilmaril@721
  4285
{
insilmaril@721
  4286
	QColor col = QColorDialog::getColor( mapScene->backgroundBrush().color(), NULL);
insilmaril@721
  4287
	if ( !col.isValid() ) return;
insilmaril@721
  4288
	setMapBackgroundColor( col );
insilmaril@721
  4289
}
insilmaril@721
  4290
insilmaril@721
  4291
insilmaril@753
  4292
void VymModel::setMapBackgroundColor(QColor col)	// FIXME-3 move to ME
insilmaril@721
  4293
{
insilmaril@721
  4294
	QColor oldcol=mapScene->backgroundBrush().color();
insilmaril@721
  4295
	saveState(
insilmaril@721
  4296
		QString ("setMapBackgroundColor (\"%1\")").arg(oldcol.name()),
insilmaril@721
  4297
		QString ("setMapBackgroundColor (\"%1\")").arg(col.name()),
insilmaril@721
  4298
		QString("Set background color of map to %1").arg(col.name()));
insilmaril@721
  4299
	mapScene->setBackgroundBrush(col);
insilmaril@721
  4300
}
insilmaril@721
  4301
insilmaril@753
  4302
QColor VymModel::getMapBackgroundColor()	// FIXME-3 move to ME
insilmaril@721
  4303
{
insilmaril@721
  4304
    return mapScene->backgroundBrush().color();
insilmaril@721
  4305
}
insilmaril@721
  4306
insilmaril@721
  4307
insilmaril@753
  4308
LinkableMapObj::ColorHint VymModel::getMapLinkColorHint()	// FIXME-3 move to ME
insilmaril@721
  4309
{
insilmaril@721
  4310
	return linkcolorhint;
insilmaril@721
  4311
}
insilmaril@721
  4312
insilmaril@753
  4313
QColor VymModel::getMapDefLinkColor()	// FIXME-3 move to ME
insilmaril@721
  4314
{
insilmaril@721
  4315
	return defLinkColor;
insilmaril@721
  4316
}
insilmaril@721
  4317
insilmaril@753
  4318
void VymModel::setMapDefXLinkColor(QColor col)	// FIXME-3 move to ME
insilmaril@721
  4319
{
insilmaril@721
  4320
	defXLinkColor=col;
insilmaril@721
  4321
}
insilmaril@721
  4322
insilmaril@753
  4323
QColor VymModel::getMapDefXLinkColor()	// FIXME-3 move to ME
insilmaril@721
  4324
{
insilmaril@721
  4325
	return defXLinkColor;
insilmaril@721
  4326
}
insilmaril@721
  4327
insilmaril@753
  4328
void VymModel::setMapDefXLinkWidth (int w)	// FIXME-3 move to ME
insilmaril@721
  4329
{
insilmaril@721
  4330
	defXLinkWidth=w;
insilmaril@721
  4331
}
insilmaril@721
  4332
insilmaril@753
  4333
int VymModel::getMapDefXLinkWidth()	// FIXME-3 move to ME
insilmaril@721
  4334
{
insilmaril@721
  4335
	return defXLinkWidth;
insilmaril@721
  4336
}
insilmaril@721
  4337
insilmaril@794
  4338
void VymModel::move(const double &x, const double &y)
insilmaril@753
  4339
{
insilmaril@753
  4340
	int i=x; i=y;
insilmaril@794
  4341
	MapItem *seli = (MapItem*)getSelectedItem();
insilmaril@794
  4342
	if (seli && (seli->isBranchLikeType() || seli->getType()==TreeItem::Image))
insilmaril@721
  4343
	{
insilmaril@794
  4344
		LinkableMapObj *lmo=seli->getLMO();
insilmaril@794
  4345
		if (lmo)
insilmaril@794
  4346
		{
insilmaril@794
  4347
			QPointF ap(lmo->getAbsPos());
insilmaril@794
  4348
			QPointF to(x, y);
insilmaril@794
  4349
			if (ap != to)
insilmaril@794
  4350
			{
insilmaril@794
  4351
				QString ps=qpointFToString(ap);
insilmaril@794
  4352
				QString s=getSelectString(seli);
insilmaril@794
  4353
				saveState(
insilmaril@794
  4354
					s, "move "+ps, 
insilmaril@794
  4355
					s, "move "+qpointFToString(to), 
insilmaril@794
  4356
					QString("Move %1 to %2").arg(getObjectName(seli)).arg(ps));
insilmaril@794
  4357
				lmo->move(x,y);
insilmaril@794
  4358
				reposition();
insilmaril@794
  4359
				emitSelectionChanged();
insilmaril@794
  4360
			}
insilmaril@794
  4361
		}
insilmaril@721
  4362
	}
insilmaril@721
  4363
}
insilmaril@721
  4364
insilmaril@794
  4365
void VymModel::moveRel (const double &x, const double &y)	
insilmaril@753
  4366
{
insilmaril@753
  4367
	int i=x; i=y;
insilmaril@794
  4368
	MapItem *seli = (MapItem*)getSelectedItem();
insilmaril@794
  4369
	if (seli && (seli->isBranchLikeType() || seli->getType()==TreeItem::Image))
insilmaril@721
  4370
	{
insilmaril@794
  4371
		LinkableMapObj *lmo=seli->getLMO();
insilmaril@794
  4372
		if (lmo)
insilmaril@794
  4373
		{
insilmaril@794
  4374
			QPointF rp(lmo->getRelPos());
insilmaril@794
  4375
			QPointF to(x, y);
insilmaril@794
  4376
			if (rp != to)
insilmaril@794
  4377
			{
insilmaril@794
  4378
				QString ps=qpointFToString (lmo->getRelPos());
insilmaril@794
  4379
				QString s=getSelectString(seli);
insilmaril@794
  4380
				saveState(
insilmaril@794
  4381
					s, "moveRel "+ps, 
insilmaril@794
  4382
					s, "moveRel "+qpointFToString(to), 
insilmaril@794
  4383
					QString("Move %1 to relative position %2").arg(getObjectName(seli)).arg(ps));
insilmaril@794
  4384
				((OrnamentedObj*)lmo)->move2RelPos (x,y);
insilmaril@794
  4385
				reposition();
insilmaril@794
  4386
				lmo->updateLinkGeometry();
insilmaril@794
  4387
				emitSelectionChanged();
insilmaril@794
  4388
			}
insilmaril@794
  4389
		}	
insilmaril@721
  4390
	}
insilmaril@721
  4391
}
insilmaril@721
  4392
insilmaril@721
  4393
insilmaril@696
  4394
void VymModel::animate()
insilmaril@696
  4395
{
insilmaril@696
  4396
	animationTimer->stop();
insilmaril@696
  4397
	BranchObj *bo;
insilmaril@696
  4398
	int i=0;
insilmaril@696
  4399
	while (i<animObjList.size() )
insilmaril@696
  4400
	{
insilmaril@696
  4401
		bo=(BranchObj*)animObjList.at(i);
insilmaril@696
  4402
		if (!bo->animate())
insilmaril@696
  4403
		{
insilmaril@721
  4404
			if (i>=0) 
insilmaril@721
  4405
			{	
insilmaril@721
  4406
				animObjList.removeAt(i);
insilmaril@721
  4407
				i--;
insilmaril@721
  4408
			}
insilmaril@696
  4409
		}
insilmaril@696
  4410
		bo->reposition();
insilmaril@696
  4411
		i++;
insilmaril@696
  4412
	} 
insilmaril@735
  4413
	QItemSelection sel=selModel->selection();
insilmaril@735
  4414
	emit (selectionChanged(sel,sel));
insilmaril@735
  4415
insilmaril@696
  4416
	mapScene->update();
insilmaril@723
  4417
	if (!animObjList.isEmpty()) animationTimer->start();
insilmaril@696
  4418
}
insilmaril@696
  4419
insilmaril@696
  4420
insilmaril@723
  4421
void VymModel::startAnimation(BranchObj *bo, const QPointF &start, const QPointF &dest)
insilmaril@696
  4422
{
insilmaril@798
  4423
	if (start==dest) return;
insilmaril@755
  4424
	if (bo && bo->getTreeItem()->depth()>0) 
insilmaril@696
  4425
	{
insilmaril@696
  4426
		AnimPoint ap;
insilmaril@696
  4427
		ap.setStart (start);
insilmaril@696
  4428
		ap.setDest  (dest);
insilmaril@696
  4429
		ap.setTicks (animationTicks);
insilmaril@696
  4430
		ap.setAnimated (true);
insilmaril@696
  4431
		bo->setAnimation (ap);
insilmaril@696
  4432
		animObjList.append( bo );
insilmaril@696
  4433
		animationTimer->setSingleShot (true);
insilmaril@696
  4434
		animationTimer->start(animationInterval);
insilmaril@696
  4435
	}
insilmaril@696
  4436
}
insilmaril@696
  4437
insilmaril@722
  4438
void VymModel::stopAnimation (MapObj *mo)
insilmaril@722
  4439
{
insilmaril@722
  4440
	int i=animObjList.indexOf(mo);
insilmaril@722
  4441
    if (i>=0)
insilmaril@722
  4442
		animObjList.removeAt (i);
insilmaril@722
  4443
}
insilmaril@722
  4444
insilmaril@721
  4445
void VymModel::sendSelection()
insilmaril@721
  4446
{
insilmaril@721
  4447
	if (netstate!=Server) return;
insilmaril@773
  4448
	sendData (QString("select (\"%1\")").arg(getSelectString()) );
insilmaril@721
  4449
}
insilmaril@721
  4450
insilmaril@721
  4451
void VymModel::newServer()
insilmaril@721
  4452
{
insilmaril@721
  4453
	port=54321;
insilmaril@721
  4454
	sendCounter=0;
insilmaril@721
  4455
    tcpServer = new QTcpServer(this);
insilmaril@721
  4456
    if (!tcpServer->listen(QHostAddress::Any,port)) {
insilmaril@721
  4457
        QMessageBox::critical(NULL, "vym server",
insilmaril@721
  4458
                              QString("Unable to start the server: %1.").arg(tcpServer->errorString()));
insilmaril@746
  4459
        //FIXME-3 needed? we are no widget any longer... close();
insilmaril@721
  4460
        return;
insilmaril@721
  4461
    }
insilmaril@721
  4462
	connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newClient()));
insilmaril@721
  4463
	netstate=Server;
insilmaril@721
  4464
	cout<<"Server is running on port "<<tcpServer->serverPort()<<endl;
insilmaril@721
  4465
}
insilmaril@721
  4466
insilmaril@721
  4467
void VymModel::connectToServer()
insilmaril@721
  4468
{
insilmaril@721
  4469
	port=54321;
insilmaril@721
  4470
	server="salam.suse.de";
insilmaril@721
  4471
	server="localhost";
insilmaril@721
  4472
	clientSocket = new QTcpSocket (this);
insilmaril@721
  4473
	clientSocket->abort();
insilmaril@721
  4474
    clientSocket->connectToHost(server ,port);
insilmaril@721
  4475
	connect(clientSocket, SIGNAL(readyRead()), this, SLOT(readData()));
insilmaril@721
  4476
    connect(clientSocket, SIGNAL(error(QAbstractSocket::SocketError)),
insilmaril@721
  4477
            this, SLOT(displayNetworkError(QAbstractSocket::SocketError)));
insilmaril@721
  4478
	netstate=Client;		
insilmaril@721
  4479
	cout<<"connected to "<<qPrintable (server)<<" port "<<port<<endl;
insilmaril@721
  4480
insilmaril@721
  4481
	
insilmaril@721
  4482
}
insilmaril@721
  4483
insilmaril@721
  4484
void VymModel::newClient()
insilmaril@721
  4485
{
insilmaril@721
  4486
    QTcpSocket *newClient = tcpServer->nextPendingConnection();
insilmaril@721
  4487
    connect(newClient, SIGNAL(disconnected()),
insilmaril@721
  4488
            newClient, SLOT(deleteLater()));
insilmaril@721
  4489
insilmaril@721
  4490
	cout <<"ME::newClient  at "<<qPrintable( newClient->peerAddress().toString() )<<endl;
insilmaril@721
  4491
insilmaril@721
  4492
	clientList.append (newClient);
insilmaril@721
  4493
}
insilmaril@721
  4494
insilmaril@721
  4495
insilmaril@721
  4496
void VymModel::sendData(const QString &s)
insilmaril@721
  4497
{
insilmaril@721
  4498
	if (clientList.size()==0) return;
insilmaril@721
  4499
insilmaril@721
  4500
	// Create bytearray to send
insilmaril@721
  4501
	QByteArray block;
insilmaril@721
  4502
    QDataStream out(&block, QIODevice::WriteOnly);
insilmaril@721
  4503
    out.setVersion(QDataStream::Qt_4_0);
insilmaril@721
  4504
insilmaril@721
  4505
	// Reserve some space for blocksize
insilmaril@721
  4506
    out << (quint16)0;
insilmaril@721
  4507
insilmaril@721
  4508
	// Write sendCounter
insilmaril@721
  4509
    out << sendCounter++;
insilmaril@721
  4510
insilmaril@721
  4511
	// Write data
insilmaril@721
  4512
    out << s;
insilmaril@721
  4513
insilmaril@721
  4514
	// Go back and write blocksize so far
insilmaril@721
  4515
    out.device()->seek(0);
insilmaril@721
  4516
    quint16 bs=(quint16)(block.size() - 2*sizeof(quint16));
insilmaril@721
  4517
	out << bs;
insilmaril@721
  4518
insilmaril@721
  4519
	if (debug)
insilmaril@721
  4520
		cout << "ME::sendData  bs="<<bs<<"  counter="<<sendCounter<<"  s="<<qPrintable(s)<<endl;
insilmaril@721
  4521
insilmaril@721
  4522
	for (int i=0; i<clientList.size(); ++i)
insilmaril@721
  4523
	{
insilmaril@721
  4524
		//cout << "Sending \""<<qPrintable (s)<<"\" to "<<qPrintable (clientList.at(i)->peerAddress().toString())<<endl;
insilmaril@721
  4525
		clientList.at(i)->write (block);
insilmaril@721
  4526
	}
insilmaril@721
  4527
}
insilmaril@721
  4528
insilmaril@721
  4529
void VymModel::readData ()
insilmaril@721
  4530
{
insilmaril@721
  4531
	while (clientSocket->bytesAvailable() >=(int)sizeof(quint16) )
insilmaril@721
  4532
	{
insilmaril@721
  4533
		if (debug)
insilmaril@721
  4534
			cout <<"readData  bytesAvail="<<clientSocket->bytesAvailable();
insilmaril@721
  4535
		quint16 recCounter;
insilmaril@721
  4536
		quint16 blockSize;
insilmaril@721
  4537
insilmaril@721
  4538
		QDataStream in(clientSocket);
insilmaril@721
  4539
		in.setVersion(QDataStream::Qt_4_0);
insilmaril@721
  4540
insilmaril@721
  4541
		in >> blockSize;
insilmaril@721
  4542
		in >> recCounter;
insilmaril@721
  4543
		
insilmaril@721
  4544
		QString t;
insilmaril@721
  4545
		in >>t;
insilmaril@721
  4546
		if (debug)
insilmaril@721
  4547
			cout << "  t="<<qPrintable (t)<<endl;
insilmaril@721
  4548
		parseAtom (t);
insilmaril@721
  4549
	}
insilmaril@721
  4550
	return;
insilmaril@721
  4551
}
insilmaril@721
  4552
insilmaril@721
  4553
void VymModel::displayNetworkError(QAbstractSocket::SocketError socketError)
insilmaril@721
  4554
{
insilmaril@721
  4555
    switch (socketError) {
insilmaril@721
  4556
    case QAbstractSocket::RemoteHostClosedError:
insilmaril@721
  4557
        break;
insilmaril@721
  4558
    case QAbstractSocket::HostNotFoundError:
insilmaril@721
  4559
        QMessageBox::information(NULL, vymName +" Network client",
insilmaril@721
  4560
                                 "The host was not found. Please check the "
insilmaril@721
  4561
                                    "host name and port settings.");
insilmaril@721
  4562
        break;
insilmaril@721
  4563
    case QAbstractSocket::ConnectionRefusedError:
insilmaril@721
  4564
        QMessageBox::information(NULL, vymName + " Network client",
insilmaril@721
  4565
                                 "The connection was refused by the peer. "
insilmaril@721
  4566
                                    "Make sure the fortune server is running, "
insilmaril@721
  4567
                                    "and check that the host name and port "
insilmaril@721
  4568
                                    "settings are correct.");
insilmaril@721
  4569
        break;
insilmaril@721
  4570
    default:
insilmaril@721
  4571
        QMessageBox::information(NULL, vymName + " Network client",
insilmaril@721
  4572
                                 QString("The following error occurred: %1.")
insilmaril@721
  4573
                                 .arg(clientSocket->errorString()));
insilmaril@721
  4574
    }
insilmaril@721
  4575
}
insilmaril@721
  4576
insilmaril@785
  4577
/* FIXME-3 Playing with DBUS...
insilmaril@784
  4578
QDBusVariant VymModel::query (const QString &query)
insilmaril@784
  4579
{
insilmaril@784
  4580
	TreeItem *selti=getSelectedItem();
insilmaril@784
  4581
	if (selti)
insilmaril@784
  4582
		return QDBusVariant (selti->getHeading());
insilmaril@784
  4583
	else
insilmaril@784
  4584
		return QDBusVariant ("Nothing selected.");
insilmaril@784
  4585
}
insilmaril@784
  4586
*/
insilmaril@721
  4587
insilmaril@801
  4588
void VymModel::testslot()	//FIXME-3 Playing with DBUS
insilmaril@785
  4589
{
insilmaril@785
  4590
	cout << "VM::testslot called\n";
insilmaril@785
  4591
}
insilmaril@785
  4592
insilmaril@721
  4593
void VymModel::selectMapSelectionColor()
insilmaril@721
  4594
{
insilmaril@721
  4595
	QColor col = QColorDialog::getColor( defLinkColor, NULL);
insilmaril@721
  4596
	setSelectionColor (col);
insilmaril@721
  4597
}
insilmaril@721
  4598
insilmaril@721
  4599
void VymModel::setSelectionColorInt (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
insilmaril@728
  4608
	mapEditor->setSelectionColor (col);
insilmaril@721
  4609
}
insilmaril@721
  4610
insilmaril@775
  4611
void VymModel::emitSelectionChanged(const QItemSelection &newsel)
insilmaril@735
  4612
{
insilmaril@742
  4613
	emit (selectionChanged(newsel,newsel));	// needed e.g. to update geometry in editor
insilmaril@798
  4614
	//FIXME-3 emitShowSelection();
insilmaril@735
  4615
	sendSelection();
insilmaril@721
  4616
}
insilmaril@721
  4617
insilmaril@775
  4618
void VymModel::emitSelectionChanged()
insilmaril@746
  4619
{
insilmaril@746
  4620
	QItemSelection newsel=selModel->selection();
insilmaril@775
  4621
	emitSelectionChanged (newsel);
insilmaril@746
  4622
}
insilmaril@746
  4623
insilmaril@721
  4624
void VymModel::setSelectionColor(QColor col)
insilmaril@721
  4625
{
insilmaril@721
  4626
	if ( !col.isValid() ) return;
insilmaril@721
  4627
	saveState (
insilmaril@728
  4628
		QString("setSelectionColor (%1)").arg(mapEditor->getSelectionColor().name()),
insilmaril@721
  4629
		QString("setSelectionColor (%1)").arg(col.name()),
insilmaril@721
  4630
		QString("Set color of selection box to %1").arg(col.name())
insilmaril@721
  4631
	);
insilmaril@721
  4632
	setSelectionColorInt (col);
insilmaril@721
  4633
}
insilmaril@721
  4634
insilmaril@721
  4635
QColor VymModel::getSelectionColor()
insilmaril@721
  4636
{
insilmaril@728
  4637
	return mapEditor->getSelectionColor();
insilmaril@721
  4638
}
insilmaril@721
  4639
insilmaril@753
  4640
void VymModel::setHideTmpMode (TreeItem::HideTmpMode mode)
insilmaril@721
  4641
{
insilmaril@721
  4642
	hidemode=mode;
insilmaril@753
  4643
	for (int i=0;i<rootItem->childCount();i++)
insilmaril@753
  4644
		rootItem->child(i)->setHideTmp (mode);	
insilmaril@721
  4645
	reposition();
insilmaril@746
  4646
	// FIXME-3 needed? scene()->update();
insilmaril@721
  4647
}
insilmaril@721
  4648
insilmaril@652
  4649
//////////////////////////////////////////////
insilmaril@652
  4650
// Selection related
insilmaril@652
  4651
//////////////////////////////////////////////
insilmaril@652
  4652
insilmaril@727
  4653
void VymModel::setSelectionModel (QItemSelectionModel *sm)
insilmaril@727
  4654
{
insilmaril@727
  4655
	selModel=sm;
insilmaril@728
  4656
}
insilmaril@728
  4657
insilmaril@728
  4658
QItemSelectionModel* VymModel::getSelectionModel()
insilmaril@728
  4659
{
insilmaril@728
  4660
	return selModel;
insilmaril@727
  4661
}
insilmaril@727
  4662
insilmaril@721
  4663
void VymModel::setSelectionBlocked (bool b)
insilmaril@721
  4664
{
insilmaril@773
  4665
	selectionBlocked=b;
insilmaril@721
  4666
}
insilmaril@721
  4667
insilmaril@721
  4668
bool VymModel::isSelectionBlocked()
insilmaril@721
  4669
{
insilmaril@773
  4670
	return selectionBlocked;
insilmaril@721
  4671
}
insilmaril@721
  4672
insilmaril@727
  4673
bool VymModel::select ()
insilmaril@727
  4674
{
insilmaril@776
  4675
	return select (selModel->selectedIndexes().first());	// TODO no multiselections yet
insilmaril@727
  4676
}
insilmaril@727
  4677
insilmaril@721
  4678
bool VymModel::select (const QString &s)
insilmaril@721
  4679
{
insilmaril@788
  4680
	if (s.isEmpty())
insilmaril@788
  4681
	{
insilmaril@788
  4682
		unselect();
insilmaril@788
  4683
		return true;
insilmaril@788
  4684
	}
insilmaril@754
  4685
	TreeItem *ti=findBySelectString(s);
insilmaril@787
  4686
	if (ti) return select (index(ti));
insilmaril@721
  4687
	return false;
insilmaril@721
  4688
}
insilmaril@721
  4689
insilmaril@721
  4690
bool VymModel::select (LinkableMapObj *lmo)
insilmaril@721
  4691
{
insilmaril@735
  4692
	QItemSelection oldsel=selModel->selection();
insilmaril@735
  4693
insilmaril@735
  4694
	if (lmo)
insilmaril@787
  4695
		return select (index (lmo->getTreeItem()) );
insilmaril@738
  4696
	else	
insilmaril@738
  4697
		return false;
insilmaril@738
  4698
}
insilmaril@738
  4699
insilmaril@738
  4700
bool VymModel::select (TreeItem *ti)
insilmaril@738
  4701
{
insilmaril@769
  4702
	if (ti) return select (index(ti));
insilmaril@769
  4703
	return false;
insilmaril@769
  4704
}
insilmaril@769
  4705
insilmaril@769
  4706
bool VymModel::select (const QModelIndex &index)
insilmaril@769
  4707
{
insilmaril@769
  4708
	if (index.isValid() )
insilmaril@721
  4709
	{
insilmaril@769
  4710
		selModel->select (index,QItemSelectionModel::ClearAndSelect  );
insilmaril@787
  4711
		BranchItem *bi=getSelectedBranch();
insilmaril@787
  4712
		if (bi) bi->setLastSelectedBranch();
insilmaril@721
  4713
		return true;
insilmaril@721
  4714
	}
insilmaril@721
  4715
	return false;
insilmaril@721
  4716
}
insilmaril@721
  4717
insilmaril@721
  4718
void VymModel::unselect()
insilmaril@721
  4719
{
insilmaril@773
  4720
	lastSelectString=getSelectString();
insilmaril@735
  4721
	selModel->clearSelection();
insilmaril@721
  4722
}	
insilmaril@721
  4723
insilmaril@787
  4724
bool VymModel::reselect()
insilmaril@787
  4725
{
insilmaril@787
  4726
	return select (lastSelectString);
insilmaril@721
  4727
}	
insilmaril@721
  4728
insilmaril@763
  4729
void VymModel::emitShowSelection()	
insilmaril@763
  4730
{
insilmaril@763
  4731
	if (!blockReposition)
insilmaril@763
  4732
		emit (showSelection() );
insilmaril@746
  4733
}
insilmaril@746
  4734
insilmaril@753
  4735
void VymModel::emitNoteHasChanged (TreeItem *ti)
insilmaril@746
  4736
{
insilmaril@746
  4737
	QModelIndex ix=index(ti);
insilmaril@753
  4738
	emit (noteHasChanged (ix) );
insilmaril@753
  4739
}
insilmaril@753
  4740
insilmaril@753
  4741
void VymModel::emitDataHasChanged (TreeItem *ti)
insilmaril@753
  4742
{
insilmaril@753
  4743
	QModelIndex ix=index(ti);
insilmaril@767
  4744
	emit (dataChanged (ix,ix) );
insilmaril@721
  4745
}
insilmaril@721
  4746
insilmaril@750
  4747
insilmaril@787
  4748
bool VymModel::selectFirstBranch()
insilmaril@787
  4749
{
insilmaril@787
  4750
	TreeItem *ti=getSelectedBranch();
insilmaril@738
  4751
	if (ti)
insilmaril@721
  4752
	{
insilmaril@738
  4753
		TreeItem *par=ti->parent();
insilmaril@787
  4754
		if (par) 
insilmaril@787
  4755
		{
insilmaril@787
  4756
			TreeItem *ti2=par->getFirstBranch();
insilmaril@787
  4757
			if (ti2) return  select(ti2);
insilmaril@721
  4758
		}
insilmaril@721
  4759
	}		
insilmaril@787
  4760
	return false;
insilmaril@721
  4761
}
insilmaril@721
  4762
insilmaril@787
  4763
bool VymModel::selectLastBranch()
insilmaril@787
  4764
{
insilmaril@787
  4765
	TreeItem *ti=getSelectedBranch();
insilmaril@738
  4766
	if (ti)
insilmaril@721
  4767
	{
insilmaril@738
  4768
		TreeItem *par=ti->parent();
insilmaril@787
  4769
		if (par) 
insilmaril@787
  4770
		{
insilmaril@787
  4771
			TreeItem *ti2=par->getLastBranch();
insilmaril@787
  4772
			if (ti2) return select(ti2);
insilmaril@721
  4773
		}
insilmaril@721
  4774
	}		
insilmaril@787
  4775
	return false;
insilmaril@721
  4776
}
insilmaril@721
  4777
insilmaril@787
  4778
bool VymModel::selectLastSelectedBranch()
insilmaril@787
  4779
{
insilmaril@787
  4780
	BranchItem *bi=getSelectedBranch();
insilmaril@787
  4781
	if (bi)
insilmaril@735
  4782
	{
insilmaril@787
  4783
		bi=bi->getLastSelectedBranch();
insilmaril@787
  4784
		if (bi) return select (bi);
insilmaril@735
  4785
	}		
insilmaril@787
  4786
	return false;
insilmaril@735
  4787
}
insilmaril@735
  4788
insilmaril@787
  4789
bool VymModel::selectParent()
insilmaril@726
  4790
{
insilmaril@738
  4791
	TreeItem *ti=getSelectedItem();
insilmaril@738
  4792
	TreeItem *par;
insilmaril@738
  4793
	if (ti)
insilmaril@726
  4794
	{
insilmaril@738
  4795
		par=ti->parent();
insilmaril@787
  4796
		if (par) 
insilmaril@787
  4797
			return select(par);
insilmaril@726
  4798
	}		
insilmaril@787
  4799
	return false;
insilmaril@726
  4800
}
insilmaril@726
  4801
insilmaril@735
  4802
TreeItem::Type VymModel::selectionType()
insilmaril@721
  4803
{
insilmaril@735
  4804
	QModelIndexList list=selModel->selectedIndexes();
insilmaril@735
  4805
	if (list.isEmpty()) return TreeItem::Undefined;	
insilmaril@745
  4806
	TreeItem *ti = getItem (list.first() );
insilmaril@735
  4807
	return ti->getType();
insilmaril@735
  4808
insilmaril@721
  4809
}
insilmaril@721
  4810
insilmaril@735
  4811
LinkableMapObj* VymModel::getSelectedLMO()
insilmaril@652
  4812
{
insilmaril@735
  4813
	QModelIndexList list=selModel->selectedIndexes();
insilmaril@735
  4814
	if (!list.isEmpty() )
insilmaril@735
  4815
	{
insilmaril@745
  4816
		TreeItem *ti = getItem (list.first() );
insilmaril@735
  4817
		TreeItem::Type type=ti->getType();
insilmaril@735
  4818
		if (type ==TreeItem::Branch || type==TreeItem::MapCenter || type==TreeItem::Image)
insilmaril@779
  4819
			return ((MapItem*)ti)->getLMO();
insilmaril@735
  4820
	}
insilmaril@735
  4821
	return NULL;
insilmaril@652
  4822
}
insilmaril@735
  4823
insilmaril@753
  4824
BranchObj* VymModel::getSelectedBranchObj()	// FIXME-3 this should not be needed in the end!!!
insilmaril@652
  4825
{
insilmaril@787
  4826
	TreeItem *ti = getSelectedBranch();
insilmaril@738
  4827
	if (ti)
insilmaril@779
  4828
		return (BranchObj*)(  ((MapItem*)ti)->getLMO());
insilmaril@738
  4829
	else	
insilmaril@738
  4830
		return NULL;
insilmaril@738
  4831
}
insilmaril@738
  4832
insilmaril@787
  4833
BranchItem* VymModel::getSelectedBranch()
insilmaril@738
  4834
{
insilmaril@735
  4835
	QModelIndexList list=selModel->selectedIndexes();
insilmaril@735
  4836
	if (!list.isEmpty() )
insilmaril@735
  4837
	{
insilmaril@745
  4838
		TreeItem *ti = getItem (list.first() );
insilmaril@735
  4839
		TreeItem::Type type=ti->getType();
insilmaril@735
  4840
		if (type ==TreeItem::Branch || type==TreeItem::MapCenter)
insilmaril@749
  4841
			return (BranchItem*)ti;
insilmaril@735
  4842
	}
insilmaril@735
  4843
	return NULL;
insilmaril@735
  4844
}
insilmaril@735
  4845
insilmaril@787
  4846
ImageItem* VymModel::getSelectedImage()
insilmaril@776
  4847
{
insilmaril@776
  4848
	QModelIndexList list=selModel->selectedIndexes();
insilmaril@776
  4849
	if (!list.isEmpty())
insilmaril@776
  4850
	{
insilmaril@776
  4851
		TreeItem *ti=getItem (list.first());
insilmaril@776
  4852
		if (ti && ti->getType()==TreeItem::Image)
insilmaril@776
  4853
			return (ImageItem*)ti;
insilmaril@776
  4854
	}
insilmaril@773
  4855
	return NULL;
insilmaril@721
  4856
}
insilmaril@721
  4857
insilmaril@787
  4858
AttributeItem* VymModel::getSelectedAttribute()	
insilmaril@787
  4859
{
insilmaril@787
  4860
	QModelIndexList list=selModel->selectedIndexes();
insilmaril@787
  4861
	if (!list.isEmpty() )
insilmaril@787
  4862
	{
insilmaril@787
  4863
		TreeItem *ti = getItem (list.first() );
insilmaril@787
  4864
		TreeItem::Type type=ti->getType();
insilmaril@787
  4865
		if (type ==TreeItem::Attribute)
insilmaril@787
  4866
			return (AttributeItem*)ti;
insilmaril@787
  4867
	} 
insilmaril@787
  4868
	return NULL;
insilmaril@787
  4869
}
insilmaril@787
  4870
insilmaril@787
  4871
TreeItem* VymModel::getSelectedItem()	
insilmaril@787
  4872
{
insilmaril@787
  4873
	QModelIndexList list=selModel->selectedIndexes();
insilmaril@787
  4874
	if (!list.isEmpty() )
insilmaril@787
  4875
		return getItem (list.first() );
insilmaril@787
  4876
	else	
insilmaril@787
  4877
		return NULL;
insilmaril@787
  4878
}
insilmaril@787
  4879
insilmaril@787
  4880
QModelIndex VymModel::getSelectedIndex()
insilmaril@787
  4881
{
insilmaril@787
  4882
	QModelIndexList list=selModel->selectedIndexes();
insilmaril@787
  4883
	if (list.isEmpty() )
insilmaril@787
  4884
		return QModelIndex();
insilmaril@787
  4885
	else
insilmaril@787
  4886
		return list.first();
insilmaril@787
  4887
}
insilmaril@787
  4888
insilmaril@721
  4889
QString VymModel::getSelectString ()
insilmaril@721
  4890
{
insilmaril@775
  4891
	return getSelectString (getSelectedItem());
insilmaril@652
  4892
}
insilmaril@652
  4893
insilmaril@791
  4894
QString VymModel::getSelectString (LinkableMapObj *lmo)	// only for convenience. Used in MapEditor
insilmaril@749
  4895
{
insilmaril@749
  4896
	if (!lmo) return QString();
insilmaril@749
  4897
	return getSelectString (lmo->getTreeItem() );
insilmaril@749
  4898
}
insilmaril@749
  4899
insilmaril@791
  4900
QString VymModel::getSelectString (TreeItem *ti) 
insilmaril@652
  4901
{
insilmaril@652
  4902
	QString s;
insilmaril@749
  4903
	if (!ti) return s;
insilmaril@777
  4904
	switch (ti->getType())
insilmaril@777
  4905
	{
insilmaril@777
  4906
		case TreeItem::MapCenter: s="mc:"; break;
insilmaril@777
  4907
		case TreeItem::Branch: s="bo:";break;
insilmaril@777
  4908
		case TreeItem::Image: s="fi:";break;
insilmaril@787
  4909
		case TreeItem::Attribute: s="ai:";break;
insilmaril@794
  4910
		case TreeItem::XLink: s="xl:";break;
insilmaril@794
  4911
		default:
insilmaril@794
  4912
			s="unknown type in VymModel::getSelectString()";
insilmaril@794
  4913
			break;
insilmaril@777
  4914
	}
insilmaril@777
  4915
	s=  s + QString("%1").arg(ti->num());
insilmaril@777
  4916
	if (ti->depth() >0)
insilmaril@777
  4917
		// call myself recursively
insilmaril@777
  4918
		s= getSelectString(ti->parent()) +","+s;
insilmaril@777
  4919
			
insilmaril@652
  4920
	return s;
insilmaril@652
  4921
}
insilmaril@652
  4922