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