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