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