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