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