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