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