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