vymmodel.cpp
author insilmaril
Wed, 01 Apr 2009 15:06:57 +0000
changeset 749 9ff332964015
parent 748 edb78a44240b
child 750 ff3b01ce0960
permissions -rw-r--r--
moved scroll functions from BranchObj to BranchItem
</
insilmaril@650
     1
#include <QApplication>
insilmaril@687
     2
#include <typeinfo>
insilmaril@650
     3
insilmaril@721
     4
#include "vymmodel.h"
insilmaril@721
     5
insilmaril@749
     6
#include "branchitem.h"
insilmaril@749
     7
#include "mapcenteritem.h"
insilmaril@721
     8
#include "editxlinkdialog.h"
insilmaril@721
     9
#include "exports.h"
insilmaril@721
    10
#include "exportxhtmldialog.h"
insilmaril@721
    11
#include "file.h"
insilmaril@650
    12
#include "geometry.h"		// for addBBox
insilmaril@721
    13
#include "mainwindow.h"
insilmaril@721
    14
#include "mapcenterobj.h"
insilmaril@721
    15
#include "misc.h"
insilmaril@721
    16
#include "parser.h"
insilmaril@721
    17
#include "selection.h"
insilmaril@721
    18
insilmaril@721
    19
insilmaril@721
    20
#include "warningdialog.h"
insilmaril@721
    21
#include "xml-freemind.h"
insilmaril@726
    22
#include "xmlobj.h"
insilmaril@721
    23
#include "xml-vym.h"
insilmaril@721
    24
insilmaril@721
    25
insilmaril@721
    26
extern bool debug;
insilmaril@721
    27
extern Main *mainWindow;
insilmaril@696
    28
extern Settings settings;
insilmaril@721
    29
extern QString tmpVymDir;
insilmaril@721
    30
insilmaril@721
    31
extern TextEditor *textEditor;
insilmaril@721
    32
insilmaril@721
    33
extern QString clipboardDir;
insilmaril@721
    34
extern QString clipboardFile;
insilmaril@721
    35
extern bool clipboardEmpty;
insilmaril@721
    36
insilmaril@721
    37
extern ImageIO imageIO;
insilmaril@721
    38
insilmaril@721
    39
extern QString vymName;
insilmaril@721
    40
extern QString vymVersion;
insilmaril@721
    41
extern QDir vymBaseDir;
insilmaril@721
    42
insilmaril@721
    43
extern QDir lastImageDir;
insilmaril@721
    44
extern QDir lastFileDir;
insilmaril@721
    45
insilmaril@721
    46
extern FlagRowObj *standardFlagsDefault;
insilmaril@721
    47
insilmaril@721
    48
extern Settings settings;
insilmaril@721
    49
insilmaril@721
    50
insilmaril@721
    51
insilmaril@721
    52
int VymModel::mapNum=0;	// make instance
insilmaril@696
    53
insilmaril@650
    54
VymModel::VymModel() 
insilmaril@650
    55
{
insilmaril@650
    56
//    cout << "Const VymModel\n";
insilmaril@721
    57
	init();
insilmaril@746
    58
	rootItem->setModel (this);
insilmaril@650
    59
}
insilmaril@650
    60
insilmaril@650
    61
insilmaril@650
    62
VymModel::~VymModel() 
insilmaril@650
    63
{
insilmaril@650
    64
//    cout << "Destr VymModel\n";
insilmaril@721
    65
	autosaveTimer->stop();
insilmaril@721
    66
	fileChangedTimer->stop();
insilmaril@721
    67
	clear();
insilmaril@650
    68
}	
insilmaril@650
    69
insilmaril@650
    70
void VymModel::clear() 
insilmaril@650
    71
{
insilmaril@735
    72
	selModel->clearSelection();
insilmaril@721
    73
insilmaril@727
    74
	// Remove stuff    
insilmaril@746
    75
	while (!mapCenters.isEmpty())			// FIXME-2 VM needs to be in treemodel only...
insilmaril@665
    76
		delete mapCenters.takeFirst();
insilmaril@727
    77
insilmaril@727
    78
	QModelIndex ri=index(rootItem);
insilmaril@746
    79
	//removeRows (0, rowCount(ri),ri);		// FIXME-2 here should be at least a beginRemoveRows...
insilmaril@650
    80
}
insilmaril@650
    81
insilmaril@650
    82
void VymModel::init () 
insilmaril@650
    83
{
insilmaril@721
    84
	// We should have at least one map center to start with
insilmaril@746
    85
	// addMapCenter();  FIXME-2 VM create this in MapEditor as long as model is part of that
insilmaril@727
    86
insilmaril@727
    87
	// No MapEditor yet
insilmaril@727
    88
	mapEditor=NULL;
insilmaril@721
    89
insilmaril@746
    90
	// Also no scene yet (should not be needed anyway)  FIXME-3 VM
insilmaril@728
    91
	mapScene=NULL;
insilmaril@728
    92
insilmaril@721
    93
	// History 
insilmaril@721
    94
	mapNum++;
insilmaril@721
    95
    mapChanged=false;
insilmaril@721
    96
	mapDefault=true;
insilmaril@721
    97
	mapUnsaved=false;
insilmaril@721
    98
insilmaril@721
    99
	curStep=0;
insilmaril@721
   100
	redosAvail=0;
insilmaril@721
   101
	undosAvail=0;
insilmaril@721
   102
insilmaril@721
   103
 	stepsTotal=settings.readNumEntry("/history/stepsTotal",100);
insilmaril@721
   104
	undoSet.setEntry ("/history/stepsTotal",QString::number(stepsTotal));
insilmaril@721
   105
	mainWindow->updateHistory (undoSet);
insilmaril@721
   106
insilmaril@721
   107
	// Create tmp dirs
insilmaril@721
   108
	makeTmpDirectories();
insilmaril@721
   109
	
insilmaril@721
   110
	// Files
insilmaril@721
   111
	zipped=true;
insilmaril@721
   112
	filePath="";
insilmaril@721
   113
	fileName=tr("unnamed");
insilmaril@721
   114
	mapName="";
insilmaril@721
   115
	blockReposition=false;
insilmaril@721
   116
	blockSaveState=false;
insilmaril@721
   117
insilmaril@721
   118
	autosaveTimer=new QTimer (this);
insilmaril@721
   119
	connect(autosaveTimer, SIGNAL(timeout()), this, SLOT(autosave()));
insilmaril@721
   120
insilmaril@721
   121
	fileChangedTimer=new QTimer (this);
insilmaril@721
   122
	fileChangedTimer->start(3000);
insilmaril@721
   123
	connect(fileChangedTimer, SIGNAL(timeout()), this, SLOT(fileChanged()));
insilmaril@721
   124
insilmaril@721
   125
insilmaril@721
   126
	// selections
insilmaril@728
   127
	selModel=NULL;
insilmaril@721
   128
insilmaril@721
   129
	// find routine
insilmaril@741
   130
	findCurrent=NULL;				
insilmaril@741
   131
	findPrevious=NULL;				
insilmaril@721
   132
	EOFind=false;
insilmaril@696
   133
insilmaril@696
   134
	// animations
insilmaril@696
   135
	animationUse=settings.readBoolEntry("/animation/use",false);
insilmaril@696
   136
	animationTicks=settings.readNumEntry("/animation/ticks",10);
insilmaril@696
   137
	animationInterval=settings.readNumEntry("/animation/interval",50);
insilmaril@696
   138
	animObjList.clear();
insilmaril@696
   139
	animationTimer=new QTimer (this);
insilmaril@696
   140
	connect(animationTimer, SIGNAL(timeout()), this, SLOT(animate()));
insilmaril@696
   141
insilmaril@721
   142
	// View - map
insilmaril@721
   143
	defLinkColor=QColor (0,0,255);
insilmaril@721
   144
	defXLinkColor=QColor (180,180,180);
insilmaril@721
   145
	linkcolorhint=LinkableMapObj::DefaultColor;
insilmaril@721
   146
	linkstyle=LinkableMapObj::PolyParabel;
insilmaril@721
   147
	defXLinkWidth=1;
insilmaril@721
   148
	defXLinkColor=QColor (230,230,230);
insilmaril@721
   149
insilmaril@721
   150
	hidemode=HideNone;
insilmaril@721
   151
insilmaril@721
   152
	// Network
insilmaril@721
   153
	netstate=Offline;
insilmaril@721
   154
insilmaril@721
   155
	// Create MapCenter
insilmaril@746
   156
	//  addMapCenter();  FIXME-2 VM create this in MapEditor until BO and MCO are independent of scene
insilmaril@721
   157
insilmaril@650
   158
}
insilmaril@650
   159
insilmaril@721
   160
void VymModel::makeTmpDirectories()
insilmaril@721
   161
{
insilmaril@721
   162
	// Create unique temporary directories
insilmaril@721
   163
	tmpMapDir = tmpVymDir+QString("/model-%1").arg(mapNum);
insilmaril@721
   164
	histPath = tmpMapDir+"/history";
insilmaril@721
   165
	QDir d;
insilmaril@721
   166
	d.mkdir (tmpMapDir);
insilmaril@721
   167
}
insilmaril@721
   168
insilmaril@721
   169
insilmaril@746
   170
MapEditor* VymModel::getMapEditor()	// FIXME-2 VM better return favourite editor here
insilmaril@650
   171
{
insilmaril@650
   172
	return mapEditor;
insilmaril@650
   173
}
insilmaril@650
   174
insilmaril@721
   175
bool VymModel::isRepositionBlocked()
insilmaril@650
   176
{
insilmaril@721
   177
	return blockReposition;
insilmaril@650
   178
}
insilmaril@650
   179
insilmaril@746
   180
void VymModel::updateActions()	// FIXME-2  maybe don't update if blockReposition is set
insilmaril@650
   181
{
insilmaril@742
   182
	cout << "VymModel::updateActions \n";
insilmaril@721
   183
	// Tell mainwindow to update states of actions
insilmaril@721
   184
	mainWindow->updateActions();
insilmaril@650
   185
}
insilmaril@650
   186
insilmaril@721
   187
insilmaril@721
   188
insilmaril@721
   189
QString VymModel::saveToDir(const QString &tmpdir, const QString &prefix, bool writeflags, const QPointF &offset, LinkableMapObj *saveSel)
insilmaril@650
   190
{
insilmaril@721
   191
	// tmpdir		temporary directory to which data will be written
insilmaril@721
   192
	// prefix		mapname, which will be appended to images etc.
insilmaril@721
   193
	// writeflags	Only write flags for "real" save of map, not undo
insilmaril@721
   194
	// offset		offset of bbox of whole map in scene. 
insilmaril@721
   195
	//				Needed for XML export
insilmaril@721
   196
	
insilmaril@726
   197
insilmaril@726
   198
	XMLObj xml;
insilmaril@726
   199
insilmaril@721
   200
	// Save Header
insilmaril@721
   201
	QString ls;
insilmaril@721
   202
	switch (linkstyle)
insilmaril@721
   203
	{
insilmaril@721
   204
		case LinkableMapObj::Line: 
insilmaril@721
   205
			ls="StyleLine";
insilmaril@721
   206
			break;
insilmaril@721
   207
		case LinkableMapObj::Parabel:
insilmaril@721
   208
			ls="StyleParabel";
insilmaril@721
   209
			break;
insilmaril@721
   210
		case LinkableMapObj::PolyLine:	
insilmaril@721
   211
			ls="StylePolyLine";
insilmaril@721
   212
			break;
insilmaril@721
   213
		default:
insilmaril@721
   214
			ls="StylePolyParabel";
insilmaril@721
   215
			break;
insilmaril@721
   216
	}	
insilmaril@721
   217
insilmaril@721
   218
	QString s="<?xml version=\"1.0\" encoding=\"utf-8\"?><!DOCTYPE vymmap>\n";
insilmaril@721
   219
	QString colhint="";
insilmaril@721
   220
	if (linkcolorhint==LinkableMapObj::HeadingColor) 
insilmaril@726
   221
		colhint=xml.attribut("linkColorHint","HeadingColor");
insilmaril@726
   222
insilmaril@726
   223
	QString mapAttr=xml.attribut("version",vymVersion);
insilmaril@721
   224
	if (!saveSel)
insilmaril@726
   225
		mapAttr+= xml.attribut("author",author) +
insilmaril@726
   226
				  xml.attribut("comment",comment) +
insilmaril@726
   227
			      xml.attribut("date",getDate()) +
insilmaril@749
   228
				  xml.attribut("branchCount", QString().number(branchCount())) +
insilmaril@726
   229
		          xml.attribut("backgroundColor", mapScene->backgroundBrush().color().name() ) +
insilmaril@728
   230
		          xml.attribut("selectionColor", mapEditor->getSelectionColor().name() ) +
insilmaril@726
   231
		          xml.attribut("linkStyle", ls ) +
insilmaril@726
   232
		          xml.attribut("linkColor", defLinkColor.name() ) +
insilmaril@726
   233
		          xml.attribut("defXLinkColor", defXLinkColor.name() ) +
insilmaril@726
   234
		          xml.attribut("defXLinkWidth", QString().setNum(defXLinkWidth,10) ) +
insilmaril@721
   235
		          colhint; 
insilmaril@726
   236
	s+=xml.beginElement("vymmap",mapAttr);
insilmaril@726
   237
	xml.incIndent();
insilmaril@721
   238
insilmaril@746
   239
	// Find the used flags while traversing the tree	// FIXME-2 this can be done local to vymmodel maybe...
insilmaril@721
   240
	standardFlagsDefault->resetUsedCounter();
insilmaril@721
   241
	
insilmaril@721
   242
	// Reset the counters before saving
insilmaril@721
   243
	// TODO constr. of FIO creates lots of objects, better do this in some other way...
insilmaril@746
   244
	FloatImageObj (mapScene).resetSaveCounter();// FIXME-2 this can be done local to vymmodel maybe...
insilmaril@721
   245
insilmaril@721
   246
	// Build xml recursivly
insilmaril@721
   247
	if (!saveSel || typeid (*saveSel) == typeid (MapCenterObj))
insilmaril@745
   248
		// Save all mapcenters as complete map, if saveSel not set
insilmaril@745
   249
		s+=saveTreeToDir(tmpdir,prefix,writeflags,offset);
insilmaril@721
   250
	else
insilmaril@721
   251
	{
insilmaril@721
   252
		if ( typeid(*saveSel) == typeid(BranchObj) )
insilmaril@721
   253
			// Save Subtree
insilmaril@721
   254
			s+=((BranchObj*)(saveSel))->saveToDir(tmpdir,prefix,offset);
insilmaril@721
   255
		else if ( typeid(*saveSel) == typeid(FloatImageObj) )
insilmaril@721
   256
			// Save image
insilmaril@721
   257
			s+=((FloatImageObj*)(saveSel))->saveToDir(tmpdir,prefix);
insilmaril@721
   258
	}
insilmaril@721
   259
insilmaril@721
   260
	// Save local settings
insilmaril@721
   261
	s+=settings.getDataXML (destPath);
insilmaril@721
   262
insilmaril@721
   263
	// Save selection
insilmaril@721
   264
	if (!selection.isEmpty() && !saveSel ) 
insilmaril@726
   265
		s+=xml.valueElement("select",selection.getSelectString());
insilmaril@726
   266
insilmaril@726
   267
	xml.decIndent();
insilmaril@726
   268
	s+=xml.endElement("vymmap");
insilmaril@721
   269
insilmaril@721
   270
	if (writeflags)
insilmaril@721
   271
		standardFlagsDefault->saveToDir (tmpdir+"/flags/","",writeflags);
insilmaril@721
   272
	return s;
insilmaril@650
   273
}
insilmaril@650
   274
insilmaril@745
   275
QString VymModel::saveTreeToDir (const QString &tmpdir,const QString &prefix, int verbose, const QPointF &offset)
insilmaril@745
   276
{
insilmaril@745
   277
    QString s;
insilmaril@745
   278
insilmaril@745
   279
	for (int i=0; i<mapCenters.count(); i++)
insilmaril@745
   280
		s+=mapCenters.at(i)->saveToDir (tmpdir,prefix,verbose,offset);
insilmaril@745
   281
    return s;
insilmaril@745
   282
}
insilmaril@745
   283
insilmaril@721
   284
void VymModel::setFilePath(QString fpath, QString destname)
insilmaril@650
   285
{
insilmaril@721
   286
	if (fpath.isEmpty() || fpath=="")
insilmaril@721
   287
	{
insilmaril@721
   288
		filePath="";
insilmaril@721
   289
		fileName="";
insilmaril@721
   290
		destPath="";
insilmaril@721
   291
	} else
insilmaril@721
   292
	{
insilmaril@721
   293
		filePath=fpath;		// becomes absolute path
insilmaril@721
   294
		fileName=fpath;		// gets stripped of path
insilmaril@721
   295
		destPath=destname;	// needed for vymlinks and during load to reset fileChangedTime
insilmaril@721
   296
insilmaril@721
   297
		// If fpath is not an absolute path, complete it
insilmaril@721
   298
		filePath=QDir(fpath).absPath();
insilmaril@721
   299
		fileDir=filePath.left (1+filePath.findRev ("/"));
insilmaril@721
   300
insilmaril@721
   301
		// Set short name, too. Search from behind:
insilmaril@721
   302
		int i=fileName.findRev("/");
insilmaril@721
   303
		if (i>=0) fileName=fileName.remove (0,i+1);
insilmaril@721
   304
insilmaril@721
   305
		// Forget the .vym (or .xml) for name of map
insilmaril@721
   306
		mapName=fileName.left(fileName.findRev(".",-1,true) );
insilmaril@721
   307
	}
insilmaril@650
   308
}
insilmaril@650
   309
insilmaril@721
   310
void VymModel::setFilePath(QString fpath)
insilmaril@650
   311
{
insilmaril@721
   312
	setFilePath (fpath,fpath);
insilmaril@650
   313
}
insilmaril@650
   314
insilmaril@721
   315
QString VymModel::getFilePath()
insilmaril@650
   316
{
insilmaril@721
   317
	return filePath;
insilmaril@650
   318
}
insilmaril@650
   319
insilmaril@721
   320
QString VymModel::getFileName()
insilmaril@650
   321
{
insilmaril@721
   322
	return fileName;
insilmaril@650
   323
}
insilmaril@650
   324
insilmaril@721
   325
QString VymModel::getMapName()
insilmaril@721
   326
{
insilmaril@721
   327
	return mapName;
insilmaril@721
   328
}
insilmaril@721
   329
insilmaril@721
   330
QString VymModel::getDestPath()
insilmaril@721
   331
{
insilmaril@721
   332
	return destPath;
insilmaril@721
   333
}
insilmaril@721
   334
insilmaril@721
   335
ErrorCode VymModel::load (QString fname, const LoadMode &lmode, const FileType &ftype)
insilmaril@721
   336
{
insilmaril@721
   337
	ErrorCode err=success;
insilmaril@721
   338
insilmaril@721
   339
	parseBaseHandler *handler;
insilmaril@721
   340
	fileType=ftype;
insilmaril@721
   341
	switch (fileType)
insilmaril@721
   342
	{
insilmaril@721
   343
		case VymMap: handler=new parseVYMHandler; break;
insilmaril@721
   344
		case FreemindMap : handler=new parseFreemindHandler; break;
insilmaril@721
   345
		default: 
insilmaril@721
   346
			QMessageBox::critical( 0, tr( "Critical Parse Error" ),
insilmaril@721
   347
				   "Unknown FileType in VymModel::load()");
insilmaril@721
   348
		return aborted;	
insilmaril@721
   349
	}
insilmaril@729
   350
insilmaril@729
   351
	bool zipped_org=zipped;
insilmaril@729
   352
insilmaril@721
   353
	if (lmode==NewMap)
insilmaril@721
   354
	{
insilmaril@735
   355
		selModel->clearSelection();
insilmaril@746
   356
		// FIXME-2 VM not needed??? model->setMapEditor(this);
insilmaril@721
   357
		// (map state is set later at end of load...)
insilmaril@721
   358
	} else
insilmaril@721
   359
	{
insilmaril@735
   360
		BranchObj *bo=getSelectedBranch();
insilmaril@721
   361
		if (!bo) return aborted;
insilmaril@721
   362
		if (lmode==ImportAdd)
insilmaril@721
   363
			saveStateChangingPart(
insilmaril@721
   364
				bo,
insilmaril@721
   365
				bo,
insilmaril@721
   366
				QString("addMapInsert (%1)").arg(fname),
insilmaril@721
   367
				QString("Add map %1 to %2").arg(fname).arg(getObjectName(bo)));
insilmaril@721
   368
		else	
insilmaril@721
   369
			saveStateChangingPart(
insilmaril@721
   370
				bo,
insilmaril@721
   371
				bo,
insilmaril@721
   372
				QString("addMapReplace(%1)").arg(fname),
insilmaril@721
   373
				QString("Add map %1 to %2").arg(fname).arg(getObjectName(bo)));
insilmaril@721
   374
	}	
insilmaril@721
   375
    
insilmaril@721
   376
insilmaril@721
   377
	// Create temporary directory for packing
insilmaril@721
   378
	bool ok;
insilmaril@721
   379
	QString tmpZipDir=makeTmpDir (ok,"vym-pack");
insilmaril@721
   380
	if (!ok)
insilmaril@721
   381
	{
insilmaril@721
   382
		QMessageBox::critical( 0, tr( "Critical Load Error" ),
insilmaril@721
   383
		   tr("Couldn't create temporary directory before load\n"));
insilmaril@721
   384
		return aborted; 
insilmaril@721
   385
	}
insilmaril@721
   386
insilmaril@721
   387
	// Try to unzip file
insilmaril@721
   388
	err=unzipDir (tmpZipDir,fname);
insilmaril@721
   389
	QString xmlfile;
insilmaril@721
   390
	if (err==nozip)
insilmaril@721
   391
	{
insilmaril@721
   392
		xmlfile=fname;
insilmaril@721
   393
		zipped=false;
insilmaril@721
   394
	} else
insilmaril@721
   395
	{
insilmaril@721
   396
		zipped=true;
insilmaril@721
   397
		
insilmaril@721
   398
		// Look for mapname.xml
insilmaril@721
   399
		xmlfile= fname.left(fname.findRev(".",-1,true));
insilmaril@721
   400
		xmlfile=xmlfile.section( '/', -1 );
insilmaril@721
   401
		QFile mfile( tmpZipDir + "/" + xmlfile + ".xml");
insilmaril@721
   402
		if (!mfile.exists() )
insilmaril@721
   403
		{
insilmaril@721
   404
			// mapname.xml does not exist, well, 
insilmaril@721
   405
			// maybe someone renamed the mapname.vym file...
insilmaril@721
   406
			// Try to find any .xml in the toplevel 
insilmaril@721
   407
			// directory of the .vym file
insilmaril@721
   408
			QStringList flist=QDir (tmpZipDir).entryList("*.xml");
insilmaril@721
   409
			if (flist.count()==1) 
insilmaril@721
   410
			{
insilmaril@721
   411
				// Only one entry, take this one
insilmaril@721
   412
				xmlfile=tmpZipDir + "/"+flist.first();
insilmaril@721
   413
			} else
insilmaril@721
   414
			{
insilmaril@721
   415
				for ( QStringList::Iterator it = flist.begin(); it != flist.end(); ++it ) 
insilmaril@721
   416
					*it=tmpZipDir + "/" + *it;
insilmaril@721
   417
				// TODO Multiple entries, load all (but only the first one into this ME)
insilmaril@721
   418
				//mainWindow->fileLoadFromTmp (flist);
insilmaril@721
   419
				//returnCode=1;	// Silently forget this attempt to load
insilmaril@721
   420
				qWarning ("MainWindow::load (fn)  multimap found...");
insilmaril@721
   421
			}	
insilmaril@721
   422
				
insilmaril@721
   423
			if (flist.isEmpty() )
insilmaril@721
   424
			{
insilmaril@721
   425
				QMessageBox::critical( 0, tr( "Critical Load Error" ),
insilmaril@721
   426
						   tr("Couldn't find a map (*.xml) in .vym archive.\n"));
insilmaril@721
   427
				err=aborted;				   
insilmaril@721
   428
			}	
insilmaril@721
   429
		} //file doesn't exist	
insilmaril@721
   430
		else
insilmaril@721
   431
			xmlfile=mfile.name();
insilmaril@721
   432
	}
insilmaril@721
   433
insilmaril@721
   434
	QFile file( xmlfile);
insilmaril@721
   435
insilmaril@721
   436
	// I am paranoid: file should exist anyway
insilmaril@721
   437
	// according to check in mainwindow.
insilmaril@721
   438
	if (!file.exists() )
insilmaril@721
   439
	{
insilmaril@721
   440
		QMessageBox::critical( 0, tr( "Critical Parse Error" ),
insilmaril@721
   441
				   tr(QString("Couldn't open map %1").arg(file.name())));
insilmaril@721
   442
		err=aborted;	
insilmaril@721
   443
	} else
insilmaril@721
   444
	{
insilmaril@721
   445
		bool blockSaveStateOrg=blockSaveState;
insilmaril@721
   446
		blockReposition=true;
insilmaril@721
   447
		blockSaveState=true;
insilmaril@721
   448
		QXmlInputSource source( file);
insilmaril@721
   449
		QXmlSimpleReader reader;
insilmaril@721
   450
		reader.setContentHandler( handler );
insilmaril@721
   451
		reader.setErrorHandler( handler );
insilmaril@721
   452
		handler->setModel ( this);
insilmaril@721
   453
insilmaril@721
   454
insilmaril@721
   455
		// We need to set the tmpDir in order  to load files with rel. path
insilmaril@721
   456
		QString tmpdir;
insilmaril@721
   457
		if (zipped)
insilmaril@721
   458
			tmpdir=tmpZipDir;
insilmaril@721
   459
		else
insilmaril@721
   460
			tmpdir=fname.left(fname.findRev("/",-1));	
insilmaril@721
   461
		handler->setTmpDir (tmpdir);
insilmaril@721
   462
		handler->setInputFile (file.name());
insilmaril@721
   463
		handler->setLoadMode (lmode);
insilmaril@721
   464
		bool ok = reader.parse( source );
insilmaril@721
   465
		blockReposition=false;
insilmaril@721
   466
		blockSaveState=blockSaveStateOrg;
insilmaril@721
   467
		file.close();
insilmaril@721
   468
		if ( ok ) 
insilmaril@721
   469
		{
insilmaril@746
   470
			reposition();	// FIXME-2 VM reposition the view instead...
insilmaril@721
   471
			selection.update();
insilmaril@721
   472
			if (lmode==NewMap)
insilmaril@721
   473
			{
insilmaril@721
   474
				mapDefault=false;
insilmaril@721
   475
				mapChanged=false;
insilmaril@721
   476
				mapUnsaved=false;
insilmaril@721
   477
				autosaveTimer->stop();
insilmaril@721
   478
			}
insilmaril@721
   479
insilmaril@721
   480
			// Reset timestamp to check for later updates of file
insilmaril@721
   481
			fileChangedTime=QFileInfo (destPath).lastModified();
insilmaril@721
   482
		} else 
insilmaril@721
   483
		{
insilmaril@721
   484
			QMessageBox::critical( 0, tr( "Critical Parse Error" ),
insilmaril@721
   485
					   tr( handler->errorProtocol() ) );
insilmaril@721
   486
			// returnCode=1;	
insilmaril@721
   487
			// Still return "success": the map maybe at least
insilmaril@721
   488
			// partially read by the parser
insilmaril@721
   489
		}	
insilmaril@721
   490
	}	
insilmaril@721
   491
insilmaril@721
   492
	// Delete tmpZipDir
insilmaril@721
   493
	removeDir (QDir(tmpZipDir));
insilmaril@721
   494
insilmaril@729
   495
	// Restore original zip state
insilmaril@729
   496
	zipped=zipped_org;
insilmaril@729
   497
insilmaril@721
   498
	updateActions();
insilmaril@721
   499
	return err;
insilmaril@721
   500
}
insilmaril@721
   501
insilmaril@721
   502
ErrorCode VymModel::save (const SaveMode &savemode)
insilmaril@721
   503
{
insilmaril@721
   504
	QString tmpZipDir;
insilmaril@721
   505
	QString mapFileName;
insilmaril@721
   506
	QString safeFilePath;
insilmaril@721
   507
insilmaril@721
   508
	ErrorCode err=success;
insilmaril@721
   509
insilmaril@721
   510
	if (zipped)
insilmaril@721
   511
		// save as .xml
insilmaril@721
   512
		mapFileName=mapName+".xml";
insilmaril@721
   513
	else
insilmaril@721
   514
		// use name given by user, even if he chooses .doc
insilmaril@721
   515
		mapFileName=fileName;
insilmaril@721
   516
insilmaril@721
   517
	// Look, if we should zip the data:
insilmaril@721
   518
	if (!zipped)
insilmaril@721
   519
	{
insilmaril@721
   520
		QMessageBox mb( vymName,
insilmaril@721
   521
			tr("The map %1\ndid not use the compressed "
insilmaril@721
   522
			"vym file format.\nWriting it uncompressed will also write images \n"
insilmaril@721
   523
			"and flags and thus may overwrite files in the "
insilmaril@721
   524
			"given directory\n\nDo you want to write the map").arg(filePath),
insilmaril@721
   525
			QMessageBox::Warning,
insilmaril@721
   526
			QMessageBox::Yes | QMessageBox::Default,
insilmaril@721
   527
			QMessageBox::No ,
insilmaril@721
   528
			QMessageBox::Cancel | QMessageBox::Escape);
insilmaril@721
   529
		mb.setButtonText( QMessageBox::Yes, tr("compressed (vym default)") );
insilmaril@721
   530
		mb.setButtonText( QMessageBox::No, tr("uncompressed") );
insilmaril@721
   531
		mb.setButtonText( QMessageBox::Cancel, tr("Cancel"));
insilmaril@721
   532
		switch( mb.exec() ) 
insilmaril@721
   533
		{
insilmaril@721
   534
			case QMessageBox::Yes:
insilmaril@721
   535
				// save compressed (default file format)
insilmaril@721
   536
				zipped=true;
insilmaril@721
   537
				break;
insilmaril@721
   538
			case QMessageBox::No:
insilmaril@721
   539
				// save uncompressed
insilmaril@721
   540
				zipped=false;
insilmaril@721
   541
				break;
insilmaril@721
   542
			case QMessageBox::Cancel:
insilmaril@721
   543
				// do nothing
insilmaril@721
   544
				return aborted;
insilmaril@721
   545
				break;
insilmaril@721
   546
		}
insilmaril@721
   547
	}
insilmaril@721
   548
insilmaril@721
   549
	// First backup existing file, we 
insilmaril@721
   550
	// don't want to add to old zip archives
insilmaril@721
   551
	QFile f(destPath);
insilmaril@721
   552
	if (f.exists())
insilmaril@721
   553
	{
insilmaril@721
   554
		if ( settings.value ("/mapeditor/writeBackupFile").toBool())
insilmaril@721
   555
		{
insilmaril@721
   556
			QString backupFileName(destPath + "~");
insilmaril@721
   557
			QFile backupFile(backupFileName);
insilmaril@721
   558
			if (backupFile.exists() && !backupFile.remove())
insilmaril@721
   559
			{
insilmaril@721
   560
				QMessageBox::warning(0, tr("Save Error"),
insilmaril@721
   561
									 tr("%1\ncould not be removed before saving").arg(backupFileName));
insilmaril@721
   562
			}
insilmaril@721
   563
			else if (!f.rename(backupFileName))
insilmaril@721
   564
			{
insilmaril@721
   565
				QMessageBox::warning(0, tr("Save Error"),
insilmaril@721
   566
									 tr("%1\ncould not be renamed before saving").arg(destPath));
insilmaril@721
   567
			}
insilmaril@721
   568
		}
insilmaril@721
   569
	}
insilmaril@721
   570
insilmaril@721
   571
	if (zipped)
insilmaril@721
   572
	{
insilmaril@721
   573
		// Create temporary directory for packing
insilmaril@721
   574
		bool ok;
insilmaril@721
   575
		tmpZipDir=makeTmpDir (ok,"vym-zip");
insilmaril@721
   576
		if (!ok)
insilmaril@721
   577
		{
insilmaril@721
   578
			QMessageBox::critical( 0, tr( "Critical Load Error" ),
insilmaril@721
   579
			   tr("Couldn't create temporary directory before save\n"));
insilmaril@721
   580
			return aborted; 
insilmaril@721
   581
		}
insilmaril@721
   582
insilmaril@721
   583
		safeFilePath=filePath;
insilmaril@721
   584
		setFilePath (tmpZipDir+"/"+ mapName+ ".xml", safeFilePath);
insilmaril@721
   585
	} // zipped
insilmaril@721
   586
insilmaril@721
   587
	// Create mapName and fileDir
insilmaril@721
   588
	makeSubDirs (fileDir);
insilmaril@721
   589
insilmaril@721
   590
	QString saveFile;
insilmaril@721
   591
	if (savemode==CompleteMap || selection.isEmpty())
insilmaril@721
   592
	{
insilmaril@721
   593
		// Save complete map
insilmaril@721
   594
		saveFile=saveToDir (fileDir,mapName+"-",true,QPointF(),NULL);
insilmaril@721
   595
		mapChanged=false;
insilmaril@721
   596
		mapUnsaved=false;
insilmaril@721
   597
		autosaveTimer->stop();
insilmaril@721
   598
	}
insilmaril@721
   599
	else	
insilmaril@721
   600
	{
insilmaril@721
   601
		// Save part of map
insilmaril@735
   602
		if (selectionType()==TreeItem::Image)
insilmaril@721
   603
			saveFloatImage();
insilmaril@721
   604
		else	
insilmaril@735
   605
			saveFile=saveToDir (fileDir,mapName+"-",true,QPointF(),getSelectedBranch());	
insilmaril@721
   606
		// TODO take care of multiselections
insilmaril@721
   607
	}	
insilmaril@721
   608
insilmaril@721
   609
	if (!saveStringToDisk(fileDir+mapFileName,saveFile))
insilmaril@721
   610
	{
insilmaril@721
   611
		err=aborted;
insilmaril@721
   612
		qWarning ("ME::saveStringToDisk failed!");
insilmaril@721
   613
	}
insilmaril@721
   614
insilmaril@721
   615
	if (zipped)
insilmaril@721
   616
	{
insilmaril@721
   617
		// zip
insilmaril@721
   618
		if (err==success) err=zipDir (tmpZipDir,destPath);
insilmaril@721
   619
insilmaril@721
   620
		// Delete tmpDir
insilmaril@721
   621
		removeDir (QDir(tmpZipDir));
insilmaril@721
   622
insilmaril@721
   623
		// Restore original filepath outside of tmp zip dir
insilmaril@721
   624
		setFilePath (safeFilePath);
insilmaril@721
   625
	}
insilmaril@721
   626
insilmaril@721
   627
	updateActions();
insilmaril@721
   628
	fileChangedTime=QFileInfo (destPath).lastModified();
insilmaril@721
   629
	return err;
insilmaril@721
   630
}
insilmaril@721
   631
insilmaril@721
   632
void VymModel::addMapReplaceInt(const QString &undoSel, const QString &path)
insilmaril@721
   633
{
insilmaril@721
   634
	QString pathDir=path.left(path.findRev("/"));
insilmaril@721
   635
	QDir d(pathDir);
insilmaril@721
   636
	QFile file (path);
insilmaril@721
   637
insilmaril@721
   638
	if (d.exists() )
insilmaril@721
   639
	{
insilmaril@721
   640
		// We need to parse saved XML data
insilmaril@721
   641
		parseVYMHandler handler;
insilmaril@721
   642
		QXmlInputSource source( file);
insilmaril@721
   643
		QXmlSimpleReader reader;
insilmaril@721
   644
		reader.setContentHandler( &handler );
insilmaril@721
   645
		reader.setErrorHandler( &handler );
insilmaril@721
   646
		handler.setModel ( this);
insilmaril@721
   647
		handler.setTmpDir ( pathDir );	// needed to load files with rel. path
insilmaril@721
   648
		if (undoSel.isEmpty())
insilmaril@721
   649
		{
insilmaril@721
   650
			unselect();
insilmaril@721
   651
			clear();
insilmaril@721
   652
			handler.setLoadMode (NewMap);
insilmaril@721
   653
		} else	
insilmaril@721
   654
		{
insilmaril@721
   655
			select (undoSel);
insilmaril@721
   656
			handler.setLoadMode (ImportReplace);
insilmaril@721
   657
		}	
insilmaril@721
   658
		blockReposition=true;
insilmaril@721
   659
		bool ok = reader.parse( source );
insilmaril@721
   660
		blockReposition=false;
insilmaril@721
   661
		if (! ok ) 
insilmaril@721
   662
		{	
insilmaril@721
   663
			// This should never ever happen
insilmaril@721
   664
			QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path),
insilmaril@721
   665
								    handler.errorProtocol());
insilmaril@721
   666
		}
insilmaril@721
   667
	} else	
insilmaril@721
   668
		QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
insilmaril@721
   669
}
insilmaril@721
   670
insilmaril@721
   671
void VymModel::addMapInsertInt (const QString &path, int pos)
insilmaril@721
   672
{
insilmaril@735
   673
	BranchObj *sel=getSelectedBranch();
insilmaril@721
   674
	if (sel)
insilmaril@721
   675
	{
insilmaril@721
   676
		QString pathDir=path.left(path.findRev("/"));
insilmaril@721
   677
		QDir d(pathDir);
insilmaril@721
   678
		QFile file (path);
insilmaril@721
   679
insilmaril@721
   680
		if (d.exists() )
insilmaril@721
   681
		{
insilmaril@721
   682
			// We need to parse saved XML data
insilmaril@721
   683
			parseVYMHandler handler;
insilmaril@721
   684
			QXmlInputSource source( file);
insilmaril@721
   685
			QXmlSimpleReader reader;
insilmaril@721
   686
			reader.setContentHandler( &handler );
insilmaril@721
   687
			reader.setErrorHandler( &handler );
insilmaril@721
   688
			handler.setModel (this);
insilmaril@721
   689
			handler.setTmpDir ( pathDir );	// needed to load files with rel. path
insilmaril@721
   690
			handler.setLoadMode (ImportAdd);
insilmaril@721
   691
			blockReposition=true;
insilmaril@721
   692
			bool ok = reader.parse( source );
insilmaril@721
   693
			blockReposition=false;
insilmaril@721
   694
			if (! ok ) 
insilmaril@721
   695
			{	
insilmaril@721
   696
				// This should never ever happen
insilmaril@721
   697
				QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path),
insilmaril@721
   698
										handler.errorProtocol());
insilmaril@721
   699
			}
insilmaril@721
   700
			if (sel->getDepth()>0)
insilmaril@721
   701
				sel->getLastBranch()->linkTo (sel,pos);
insilmaril@721
   702
		} else	
insilmaril@721
   703
			QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
insilmaril@721
   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@721
   720
void VymModel::loadFloatImage ()
insilmaril@721
   721
{
insilmaril@735
   722
	BranchObj *bo=getSelectedBranch();
insilmaril@721
   723
	if (bo)
insilmaril@721
   724
	{
insilmaril@721
   725
insilmaril@721
   726
		Q3FileDialog *fd=new Q3FileDialog( NULL);
insilmaril@721
   727
		fd->setMode (Q3FileDialog::ExistingFiles);
insilmaril@721
   728
		fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
insilmaril@721
   729
		ImagePreview *p =new ImagePreview (fd);
insilmaril@721
   730
		fd->setContentsPreviewEnabled( TRUE );
insilmaril@721
   731
		fd->setContentsPreview( p, p );
insilmaril@721
   732
		fd->setPreviewMode( Q3FileDialog::Contents );
insilmaril@721
   733
		fd->setCaption(vymName+" - " +tr("Load image"));
insilmaril@721
   734
		fd->setDir (lastImageDir);
insilmaril@721
   735
		fd->show();
insilmaril@721
   736
insilmaril@721
   737
		if ( fd->exec() == QDialog::Accepted )
insilmaril@721
   738
		{
insilmaril@721
   739
			// TODO loadFIO in QT4 use:	lastImageDir=fd->directory();
insilmaril@721
   740
			lastImageDir=QDir (fd->dirPath());
insilmaril@721
   741
			QString s;
insilmaril@721
   742
			FloatImageObj *fio;
insilmaril@721
   743
			for (int j=0; j<fd->selectedFiles().count(); j++)
insilmaril@721
   744
			{
insilmaril@721
   745
				s=fd->selectedFiles().at(j);
insilmaril@721
   746
				fio=loadFloatImageInt (s);
insilmaril@721
   747
				if (fio)
insilmaril@721
   748
					saveState(
insilmaril@721
   749
						(LinkableMapObj*)fio,
insilmaril@721
   750
						"delete ()",
insilmaril@721
   751
						bo, 
insilmaril@721
   752
						QString ("loadImage (%1)").arg(s ),
insilmaril@721
   753
						QString("Add image %1 to %2").arg(s).arg(getObjectName(bo))
insilmaril@721
   754
					);
insilmaril@721
   755
				else
insilmaril@721
   756
					// TODO loadFIO error handling
insilmaril@721
   757
					qWarning ("Failed to load "+s);
insilmaril@721
   758
			}
insilmaril@721
   759
		}
insilmaril@721
   760
		delete (p);
insilmaril@721
   761
		delete (fd);
insilmaril@721
   762
	}
insilmaril@721
   763
}
insilmaril@721
   764
insilmaril@721
   765
void VymModel::saveFloatImageInt  (FloatImageObj *fio, const QString &type, const QString &fn)
insilmaril@721
   766
{
insilmaril@721
   767
	fio->save (fn,type);
insilmaril@721
   768
}
insilmaril@721
   769
insilmaril@721
   770
void VymModel::saveFloatImage ()
insilmaril@721
   771
{
insilmaril@721
   772
	FloatImageObj *fio=selection.getFloatImage();
insilmaril@721
   773
	if (fio)
insilmaril@721
   774
	{
insilmaril@721
   775
		QFileDialog *fd=new QFileDialog( NULL);
insilmaril@721
   776
		fd->setFilters (imageIO.getFilters());
insilmaril@721
   777
		fd->setCaption(vymName+" - " +tr("Save image"));
insilmaril@721
   778
		fd->setFileMode( QFileDialog::AnyFile );
insilmaril@721
   779
		fd->setDirectory (lastImageDir);
insilmaril@721
   780
//		fd->setSelection (fio->getOriginalFilename());
insilmaril@721
   781
		fd->show();
insilmaril@721
   782
insilmaril@721
   783
		QString fn;
insilmaril@721
   784
		if ( fd->exec() == QDialog::Accepted && fd->selectedFiles().count()==1)
insilmaril@721
   785
		{
insilmaril@721
   786
			fn=fd->selectedFiles().at(0);
insilmaril@721
   787
			if (QFile (fn).exists() )
insilmaril@721
   788
			{
insilmaril@721
   789
				QMessageBox mb( vymName,
insilmaril@721
   790
					tr("The file %1 exists already.\n"
insilmaril@721
   791
					"Do you want to overwrite it?").arg(fn),
insilmaril@721
   792
				QMessageBox::Warning,
insilmaril@721
   793
				QMessageBox::Yes | QMessageBox::Default,
insilmaril@721
   794
				QMessageBox::Cancel | QMessageBox::Escape,
insilmaril@721
   795
				QMessageBox::NoButton );
insilmaril@721
   796
insilmaril@721
   797
				mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
insilmaril@721
   798
				mb.setButtonText( QMessageBox::No, tr("Cancel"));
insilmaril@721
   799
				switch( mb.exec() ) 
insilmaril@721
   800
				{
insilmaril@721
   801
					case QMessageBox::Yes:
insilmaril@721
   802
						// save 
insilmaril@721
   803
						break;
insilmaril@721
   804
					case QMessageBox::Cancel:
insilmaril@721
   805
						// do nothing
insilmaril@721
   806
						delete (fd);
insilmaril@721
   807
						return;
insilmaril@721
   808
						break;
insilmaril@721
   809
				}
insilmaril@721
   810
			}
insilmaril@721
   811
			saveFloatImageInt (fio,fd->selectedFilter(),fn );
insilmaril@721
   812
		}
insilmaril@721
   813
		delete (fd);
insilmaril@721
   814
	}
insilmaril@721
   815
}
insilmaril@721
   816
insilmaril@721
   817
insilmaril@721
   818
void VymModel::importDirInt(BranchObj *dst, QDir d)
insilmaril@721
   819
{
insilmaril@735
   820
	BranchObj *bo=getSelectedBranch();
insilmaril@721
   821
	if (bo)
insilmaril@721
   822
	{
insilmaril@721
   823
		// Traverse directories
insilmaril@721
   824
		d.setFilter( QDir::Dirs| QDir::Hidden | QDir::NoSymLinks );
insilmaril@721
   825
		QFileInfoList list = d.entryInfoList();
insilmaril@721
   826
		QFileInfo fi;
insilmaril@721
   827
insilmaril@721
   828
		for (int i = 0; i < list.size(); ++i) 
insilmaril@721
   829
		{
insilmaril@721
   830
			fi=list.at(i);
insilmaril@721
   831
			if (fi.fileName() != "." && fi.fileName() != ".." )
insilmaril@721
   832
			{
insilmaril@721
   833
				dst->addBranch();
insilmaril@721
   834
				bo=dst->getLastBranch();
insilmaril@749
   835
				BranchItem *bi=(BranchItem*)(bo->getTreeItem());
insilmaril@749
   836
				bi->setHeading (fi.fileName() );	// FIXME-3 check this
insilmaril@721
   837
				bo->setColor (QColor("blue"));
insilmaril@749
   838
				bi->toggleScroll();
insilmaril@721
   839
				if ( !d.cd(fi.fileName()) ) 
insilmaril@721
   840
					QMessageBox::critical (0,tr("Critical Import Error"),tr("Cannot find the directory %1").arg(fi.fileName()));
insilmaril@721
   841
				else 
insilmaril@721
   842
				{
insilmaril@721
   843
					// Recursively add subdirs
insilmaril@721
   844
					importDirInt (bo,d);
insilmaril@721
   845
					d.cdUp();
insilmaril@721
   846
				}
insilmaril@721
   847
			}	
insilmaril@721
   848
		}		
insilmaril@721
   849
		// Traverse files
insilmaril@721
   850
		d.setFilter( QDir::Files| QDir::Hidden | QDir::NoSymLinks );
insilmaril@721
   851
		list = d.entryInfoList();
insilmaril@721
   852
insilmaril@721
   853
		for (int i = 0; i < list.size(); ++i) 
insilmaril@721
   854
		{
insilmaril@721
   855
			fi=list.at(i);
insilmaril@721
   856
			dst->addBranch();
insilmaril@721
   857
			bo=dst->getLastBranch();
insilmaril@721
   858
			bo->setHeading (fi.fileName() );
insilmaril@721
   859
			bo->setColor (QColor("black"));
insilmaril@721
   860
			if (fi.fileName().right(4) == ".vym" )
insilmaril@721
   861
				bo->setVymLink (fi.filePath());
insilmaril@721
   862
		}	
insilmaril@721
   863
	}		
insilmaril@721
   864
}
insilmaril@721
   865
insilmaril@721
   866
void VymModel::importDirInt (const QString &s)
insilmaril@721
   867
{
insilmaril@735
   868
	BranchObj *bo=getSelectedBranch();
insilmaril@721
   869
	if (bo)
insilmaril@721
   870
	{
insilmaril@721
   871
		saveStateChangingPart (bo,bo,QString ("importDir (\"%1\")").arg(s),QString("Import directory structure from %1").arg(s));
insilmaril@721
   872
insilmaril@721
   873
		QDir d(s);
insilmaril@721
   874
		importDirInt (bo,d);
insilmaril@721
   875
	}
insilmaril@721
   876
}	
insilmaril@721
   877
insilmaril@721
   878
void VymModel::importDir()
insilmaril@721
   879
{
insilmaril@735
   880
	BranchObj *bo=getSelectedBranch();
insilmaril@721
   881
	if (bo)
insilmaril@721
   882
	{
insilmaril@721
   883
		QStringList filters;
insilmaril@721
   884
		filters <<"VYM map (*.vym)";
insilmaril@721
   885
		QFileDialog *fd=new QFileDialog( NULL,vymName+ " - " +tr("Choose directory structure to import"));
insilmaril@721
   886
		fd->setMode (QFileDialog::DirectoryOnly);
insilmaril@721
   887
		fd->setFilters (filters);
insilmaril@721
   888
		fd->setCaption(vymName+" - " +tr("Choose directory structure to import"));
insilmaril@721
   889
		fd->show();
insilmaril@721
   890
insilmaril@721
   891
		QString fn;
insilmaril@721
   892
		if ( fd->exec() == QDialog::Accepted )
insilmaril@721
   893
		{
insilmaril@721
   894
			importDirInt (fd->selectedFile() );
insilmaril@721
   895
			reposition();
insilmaril@746
   896
			//FIXME-3 VM needed? scene()->update();
insilmaril@721
   897
		}
insilmaril@721
   898
	}	
insilmaril@721
   899
}
insilmaril@721
   900
insilmaril@721
   901
insilmaril@721
   902
void VymModel::autosave()
insilmaril@721
   903
{
insilmaril@742
   904
	if (filePath=="") 
insilmaril@742
   905
	{
insilmaril@742
   906
		if (debug)
insilmaril@742
   907
			cout << "VymModel::autosave rejected due to missing filePath\n";
insilmaril@742
   908
	}
insilmaril@742
   909
insilmaril@721
   910
	QDateTime now=QDateTime().currentDateTime();
insilmaril@721
   911
insilmaril@721
   912
	// Disable autosave, while we have gone back in history
insilmaril@721
   913
	int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
insilmaril@721
   914
	if (redosAvail>0) return;
insilmaril@721
   915
insilmaril@721
   916
	// Also disable autosave for new map without filename
insilmaril@721
   917
	if (filePath.isEmpty()) return;
insilmaril@721
   918
insilmaril@721
   919
insilmaril@742
   920
	if (mapUnsaved &&mapChanged && settings.value ("/mainwindow/autosave/use",true).toBool() )
insilmaril@721
   921
	{
insilmaril@721
   922
		if (QFileInfo(filePath).lastModified()<=fileChangedTime) 
insilmaril@721
   923
			mainWindow->fileSave (this);
insilmaril@721
   924
		else
insilmaril@721
   925
			if (debug)
insilmaril@721
   926
				cout <<"  ME::autosave  rejected, file on disk is newer than last save.\n"; 
insilmaril@721
   927
insilmaril@721
   928
	}	
insilmaril@721
   929
}
insilmaril@721
   930
insilmaril@721
   931
void VymModel::fileChanged()
insilmaril@721
   932
{
insilmaril@721
   933
	// Check if file on disk has changed meanwhile
insilmaril@721
   934
	if (!filePath.isEmpty())
insilmaril@721
   935
	{
insilmaril@721
   936
		QDateTime tmod=QFileInfo (filePath).lastModified();
insilmaril@721
   937
		if (tmod>fileChangedTime)
insilmaril@721
   938
		{
insilmaril@746
   939
			// FIXME-2 VM switch to current mapeditor and finish lineedits...
insilmaril@721
   940
			QMessageBox mb( vymName,
insilmaril@721
   941
				tr("The file of the map  on disk has changed:\n\n"  
insilmaril@721
   942
				   "   %1\n\nDo you want to reload that map with the new file?").arg(filePath),
insilmaril@721
   943
				QMessageBox::Question,
insilmaril@721
   944
				QMessageBox::Yes ,
insilmaril@721
   945
				QMessageBox::Cancel | QMessageBox::Default,
insilmaril@721
   946
				QMessageBox::NoButton );
insilmaril@721
   947
insilmaril@721
   948
			mb.setButtonText( QMessageBox::Yes, tr("Reload"));
insilmaril@721
   949
			mb.setButtonText( QMessageBox::No, tr("Ignore"));
insilmaril@721
   950
			switch( mb.exec() ) 
insilmaril@721
   951
			{
insilmaril@721
   952
				case QMessageBox::Yes:
insilmaril@721
   953
					// Reload map
insilmaril@721
   954
					load (filePath,NewMap,fileType);
insilmaril@721
   955
		        case QMessageBox::Cancel:
insilmaril@721
   956
					fileChangedTime=tmod; // allow autosave to overwrite newer file!
insilmaril@721
   957
			}
insilmaril@721
   958
		}
insilmaril@721
   959
	}	
insilmaril@721
   960
}
insilmaril@721
   961
insilmaril@721
   962
bool VymModel::isDefault()
insilmaril@721
   963
{
insilmaril@721
   964
    return mapDefault;
insilmaril@721
   965
}
insilmaril@721
   966
insilmaril@721
   967
void VymModel::makeDefault()
insilmaril@721
   968
{
insilmaril@721
   969
	mapChanged=false;
insilmaril@721
   970
	mapDefault=true;
insilmaril@721
   971
}
insilmaril@721
   972
insilmaril@721
   973
bool VymModel::hasChanged()
insilmaril@721
   974
{
insilmaril@721
   975
    return mapChanged;
insilmaril@721
   976
}
insilmaril@721
   977
insilmaril@721
   978
void VymModel::setChanged()
insilmaril@721
   979
{
insilmaril@742
   980
	cout << "VM::setChanged()\n";
insilmaril@721
   981
	if (!mapChanged)
insilmaril@742
   982
	{
insilmaril@742
   983
		autosaveTimer->start(settings.value("/mainwindow/autosave/ms/",300000).toInt());
insilmaril@742
   984
		cout <<"   timer started with "<<settings.value("/mainwindow/autosave/ms/",300000).toInt()<<endl;
insilmaril@742
   985
	}	
insilmaril@721
   986
	mapChanged=true;
insilmaril@721
   987
	mapDefault=false;
insilmaril@721
   988
	mapUnsaved=true;
insilmaril@721
   989
	findReset();
insilmaril@721
   990
}
insilmaril@721
   991
insilmaril@721
   992
insilmaril@721
   993
QString VymModel::getObjectName (const LinkableMapObj *lmo)
insilmaril@721
   994
{
insilmaril@721
   995
	QString s;
insilmaril@721
   996
	if (!lmo) return QString("Error: NULL has no name!");
insilmaril@721
   997
insilmaril@721
   998
	if ((typeid(*lmo) == typeid(BranchObj) ||
insilmaril@721
   999
				      typeid(*lmo) == typeid(MapCenterObj))) 
insilmaril@721
  1000
	{
insilmaril@721
  1001
		
insilmaril@721
  1002
		s=(((BranchObj*)lmo)->getHeading());
insilmaril@721
  1003
		if (s=="") s="unnamed";
insilmaril@721
  1004
		return QString("branch (%1)").arg(s);
insilmaril@721
  1005
	}	
insilmaril@721
  1006
	if ((typeid(*lmo) == typeid(FloatImageObj) ))
insilmaril@721
  1007
		return QString ("floatimage [%1]").arg(((FloatImageObj*)lmo)->getOriginalFilename());
insilmaril@721
  1008
	return QString("Unknown type has no name!");
insilmaril@721
  1009
}
insilmaril@721
  1010
insilmaril@721
  1011
void VymModel::redo()
insilmaril@721
  1012
{
insilmaril@721
  1013
	// Can we undo at all?
insilmaril@721
  1014
	if (redosAvail<1) return;
insilmaril@721
  1015
insilmaril@721
  1016
	bool blockSaveStateOrg=blockSaveState;
insilmaril@721
  1017
	blockSaveState=true;
insilmaril@721
  1018
	
insilmaril@721
  1019
	redosAvail--;
insilmaril@721
  1020
insilmaril@721
  1021
	if (undosAvail<stepsTotal) undosAvail++;
insilmaril@721
  1022
	curStep++;
insilmaril@721
  1023
	if (curStep>stepsTotal) curStep=1;
insilmaril@721
  1024
	QString undoCommand=  undoSet.readEntry (QString("/history/step-%1/undoCommand").arg(curStep));
insilmaril@721
  1025
	QString undoSelection=undoSet.readEntry (QString("/history/step-%1/undoSelection").arg(curStep));
insilmaril@721
  1026
	QString redoCommand=  undoSet.readEntry (QString("/history/step-%1/redoCommand").arg(curStep));
insilmaril@721
  1027
	QString redoSelection=undoSet.readEntry (QString("/history/step-%1/redoSelection").arg(curStep));
insilmaril@721
  1028
	QString comment=undoSet.readEntry (QString("/history/step-%1/comment").arg(curStep));
insilmaril@721
  1029
	QString version=undoSet.readEntry ("/history/version");
insilmaril@721
  1030
insilmaril@721
  1031
	/* TODO Maybe check for version, if we save the history
insilmaril@721
  1032
	if (!checkVersion(version))
insilmaril@721
  1033
		QMessageBox::warning(0,tr("Warning"),
insilmaril@721
  1034
			tr("Version %1 of saved undo/redo data\ndoes not match current vym version %2.").arg(version).arg(vymVersion));
insilmaril@721
  1035
	*/ 
insilmaril@721
  1036
insilmaril@721
  1037
	// Find out current undo directory
insilmaril@721
  1038
	QString bakMapDir(QString(tmpMapDir+"/undo-%1").arg(curStep));
insilmaril@721
  1039
insilmaril@721
  1040
	if (debug)
insilmaril@721
  1041
	{
insilmaril@721
  1042
		cout << "VymModel::redo() begin\n";
insilmaril@721
  1043
		cout << "    undosAvail="<<undosAvail<<endl;
insilmaril@721
  1044
		cout << "    redosAvail="<<redosAvail<<endl;
insilmaril@721
  1045
		cout << "       curStep="<<curStep<<endl;
insilmaril@721
  1046
		cout << "    ---------------------------"<<endl;
insilmaril@721
  1047
		cout << "    comment="<<comment.toStdString()<<endl;
insilmaril@721
  1048
		cout << "    undoCom="<<undoCommand.toStdString()<<endl;
insilmaril@721
  1049
		cout << "    undoSel="<<undoSelection.toStdString()<<endl;
insilmaril@721
  1050
		cout << "    redoCom="<<redoCommand.toStdString()<<endl;
insilmaril@721
  1051
		cout << "    redoSel="<<redoSelection.toStdString()<<endl;
insilmaril@721
  1052
		cout << "    ---------------------------"<<endl<<endl;
insilmaril@721
  1053
	}
insilmaril@721
  1054
insilmaril@721
  1055
	// select  object before redo
insilmaril@721
  1056
	if (!redoSelection.isEmpty())
insilmaril@721
  1057
		select (redoSelection);
insilmaril@721
  1058
insilmaril@721
  1059
insilmaril@721
  1060
	parseAtom (redoCommand);
insilmaril@721
  1061
	reposition();
insilmaril@721
  1062
insilmaril@721
  1063
	blockSaveState=blockSaveStateOrg;
insilmaril@721
  1064
insilmaril@721
  1065
	undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
insilmaril@721
  1066
	undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
insilmaril@721
  1067
	undoSet.setEntry ("/history/curStep",QString::number(curStep));
insilmaril@721
  1068
	undoSet.writeSettings(histPath);
insilmaril@721
  1069
insilmaril@721
  1070
	mainWindow->updateHistory (undoSet);
insilmaril@721
  1071
	updateActions();
insilmaril@721
  1072
insilmaril@721
  1073
	/* TODO remove testing
insilmaril@721
  1074
	cout << "ME::redo() end\n";
insilmaril@721
  1075
	cout << "    undosAvail="<<undosAvail<<endl;
insilmaril@721
  1076
	cout << "    redosAvail="<<redosAvail<<endl;
insilmaril@721
  1077
	cout << "       curStep="<<curStep<<endl;
insilmaril@721
  1078
	cout << "    ---------------------------"<<endl<<endl;
insilmaril@721
  1079
	*/
insilmaril@721
  1080
insilmaril@721
  1081
insilmaril@721
  1082
}
insilmaril@721
  1083
insilmaril@721
  1084
bool VymModel::isRedoAvailable()
insilmaril@721
  1085
{
insilmaril@721
  1086
	if (undoSet.readNumEntry("/history/redosAvail",0)>0)
insilmaril@721
  1087
		return true;
insilmaril@721
  1088
	else	
insilmaril@721
  1089
		return false;
insilmaril@721
  1090
}
insilmaril@721
  1091
insilmaril@721
  1092
void VymModel::undo()
insilmaril@721
  1093
{
insilmaril@721
  1094
	// Can we undo at all?
insilmaril@721
  1095
	if (undosAvail<1) return;
insilmaril@721
  1096
insilmaril@721
  1097
	mainWindow->statusMessage (tr("Autosave disabled during undo."));
insilmaril@721
  1098
insilmaril@721
  1099
	bool blockSaveStateOrg=blockSaveState;
insilmaril@721
  1100
	blockSaveState=true;
insilmaril@721
  1101
	
insilmaril@721
  1102
	QString undoCommand=  undoSet.readEntry (QString("/history/step-%1/undoCommand").arg(curStep));
insilmaril@721
  1103
	QString undoSelection=undoSet.readEntry (QString("/history/step-%1/undoSelection").arg(curStep));
insilmaril@721
  1104
	QString redoCommand=  undoSet.readEntry (QString("/history/step-%1/redoCommand").arg(curStep));
insilmaril@721
  1105
	QString redoSelection=undoSet.readEntry (QString("/history/step-%1/redoSelection").arg(curStep));
insilmaril@721
  1106
	QString comment=undoSet.readEntry (QString("/history/step-%1/comment").arg(curStep));
insilmaril@721
  1107
	QString version=undoSet.readEntry ("/history/version");
insilmaril@721
  1108
insilmaril@721
  1109
	/* TODO Maybe check for version, if we save the history
insilmaril@721
  1110
	if (!checkVersion(version))
insilmaril@721
  1111
		QMessageBox::warning(0,tr("Warning"),
insilmaril@721
  1112
			tr("Version %1 of saved undo/redo data\ndoes not match current vym version %2.").arg(version).arg(vymVersion));
insilmaril@721
  1113
	*/
insilmaril@721
  1114
insilmaril@721
  1115
	// Find out current undo directory
insilmaril@721
  1116
	QString bakMapDir(QString(tmpMapDir+"/undo-%1").arg(curStep));
insilmaril@721
  1117
insilmaril@721
  1118
	// select  object before undo
insilmaril@721
  1119
	if (!undoSelection.isEmpty())
insilmaril@721
  1120
		select (undoSelection);
insilmaril@721
  1121
insilmaril@721
  1122
	if (debug)
insilmaril@721
  1123
	{
insilmaril@721
  1124
		cout << "VymModel::undo() begin\n";
insilmaril@721
  1125
		cout << "    undosAvail="<<undosAvail<<endl;
insilmaril@721
  1126
		cout << "    redosAvail="<<redosAvail<<endl;
insilmaril@721
  1127
		cout << "       curStep="<<curStep<<endl;
insilmaril@721
  1128
		cout << "    ---------------------------"<<endl;
insilmaril@721
  1129
		cout << "    comment="<<comment.toStdString()<<endl;
insilmaril@721
  1130
		cout << "    undoCom="<<undoCommand.toStdString()<<endl;
insilmaril@721
  1131
		cout << "    undoSel="<<undoSelection.toStdString()<<endl;
insilmaril@721
  1132
		cout << "    redoCom="<<redoCommand.toStdString()<<endl;
insilmaril@721
  1133
		cout << "    redoSel="<<redoSelection.toStdString()<<endl;
insilmaril@721
  1134
		cout << "    ---------------------------"<<endl<<endl;
insilmaril@721
  1135
	}	
insilmaril@721
  1136
	parseAtom (undoCommand);
insilmaril@721
  1137
	reposition();
insilmaril@721
  1138
insilmaril@721
  1139
	undosAvail--;
insilmaril@721
  1140
	curStep--; 
insilmaril@721
  1141
	if (curStep<1) curStep=stepsTotal;
insilmaril@721
  1142
insilmaril@721
  1143
	redosAvail++;
insilmaril@721
  1144
insilmaril@721
  1145
	blockSaveState=blockSaveStateOrg;
insilmaril@721
  1146
/* TODO remove testing
insilmaril@721
  1147
	cout << "VymModel::undo() end\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<<endl;
insilmaril@721
  1152
*/
insilmaril@721
  1153
insilmaril@721
  1154
	undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
insilmaril@721
  1155
	undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
insilmaril@721
  1156
	undoSet.setEntry ("/history/curStep",QString::number(curStep));
insilmaril@721
  1157
	undoSet.writeSettings(histPath);
insilmaril@721
  1158
insilmaril@721
  1159
	mainWindow->updateHistory (undoSet);
insilmaril@721
  1160
	updateActions();
insilmaril@721
  1161
	selection.update();
insilmaril@721
  1162
	ensureSelectionVisible();
insilmaril@721
  1163
}
insilmaril@721
  1164
insilmaril@721
  1165
bool VymModel::isUndoAvailable()
insilmaril@721
  1166
{
insilmaril@721
  1167
	if (undoSet.readNumEntry("/history/undosAvail",0)>0)
insilmaril@721
  1168
		return true;
insilmaril@721
  1169
	else	
insilmaril@721
  1170
		return false;
insilmaril@721
  1171
}
insilmaril@721
  1172
insilmaril@721
  1173
void VymModel::gotoHistoryStep (int i)
insilmaril@721
  1174
{
insilmaril@721
  1175
	// Restore variables
insilmaril@721
  1176
	int undosAvail=undoSet.readNumEntry (QString("/history/undosAvail"));
insilmaril@721
  1177
	int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
insilmaril@721
  1178
insilmaril@721
  1179
	if (i<0) i=undosAvail+redosAvail;
insilmaril@721
  1180
insilmaril@721
  1181
	// Clicking above current step makes us undo things
insilmaril@721
  1182
	if (i<undosAvail) 
insilmaril@721
  1183
	{	
insilmaril@721
  1184
		for (int j=0; j<undosAvail-i; j++) undo();
insilmaril@721
  1185
		return;
insilmaril@721
  1186
	}	
insilmaril@721
  1187
	// Clicking below current step makes us redo things
insilmaril@721
  1188
	if (i>undosAvail) 
insilmaril@721
  1189
		for (int j=undosAvail; j<i; j++) 
insilmaril@721
  1190
		{
insilmaril@721
  1191
			if (debug) cout << "VymModel::gotoHistoryStep redo "<<j<<"/"<<undosAvail<<" i="<<i<<endl;
insilmaril@721
  1192
			redo();
insilmaril@721
  1193
		}
insilmaril@721
  1194
insilmaril@721
  1195
	// And ignore clicking the current row ;-)	
insilmaril@721
  1196
}
insilmaril@721
  1197
insilmaril@721
  1198
insilmaril@721
  1199
QString VymModel::getHistoryPath()
insilmaril@721
  1200
{
insilmaril@721
  1201
	QString histName(QString("history-%1").arg(curStep));
insilmaril@721
  1202
	return (tmpMapDir+"/"+histName);
insilmaril@721
  1203
}
insilmaril@721
  1204
insilmaril@721
  1205
void VymModel::saveState(const SaveMode &savemode, const QString &undoSelection, const QString &undoCom, const QString &redoSelection, const QString &redoCom, const QString &comment, LinkableMapObj *saveSel)
insilmaril@721
  1206
{
insilmaril@746
  1207
	sendData(redoCom);	//FIXME-3 testing
insilmaril@721
  1208
insilmaril@721
  1209
	// Main saveState
insilmaril@721
  1210
insilmaril@721
  1211
insilmaril@721
  1212
	if (blockSaveState) return;
insilmaril@721
  1213
insilmaril@721
  1214
	if (debug) cout << "ME::saveState() for  "<<qPrintable (mapName)<<endl;
insilmaril@721
  1215
	
insilmaril@721
  1216
	// Find out current undo directory
insilmaril@721
  1217
	if (undosAvail<stepsTotal) undosAvail++;
insilmaril@721
  1218
	curStep++;
insilmaril@721
  1219
	if (curStep>stepsTotal) curStep=1;
insilmaril@721
  1220
	
insilmaril@721
  1221
	QString backupXML="";
insilmaril@721
  1222
	QString histDir=getHistoryPath();
insilmaril@721
  1223
	QString bakMapPath=histDir+"/map.xml";
insilmaril@721
  1224
insilmaril@721
  1225
	// Create histDir if not available
insilmaril@721
  1226
	QDir d(histDir);
insilmaril@721
  1227
	if (!d.exists()) 
insilmaril@721
  1228
		makeSubDirs (histDir);
insilmaril@721
  1229
insilmaril@721
  1230
	// Save depending on how much needs to be saved	
insilmaril@721
  1231
	if (saveSel)
insilmaril@721
  1232
		backupXML=saveToDir (histDir,mapName+"-",false, QPointF (),saveSel);
insilmaril@721
  1233
		
insilmaril@721
  1234
	QString undoCommand="";
insilmaril@721
  1235
	if (savemode==UndoCommand)
insilmaril@721
  1236
	{
insilmaril@721
  1237
		undoCommand=undoCom;
insilmaril@721
  1238
	}	
insilmaril@721
  1239
	else if (savemode==PartOfMap )
insilmaril@721
  1240
	{
insilmaril@721
  1241
		undoCommand=undoCom;
insilmaril@721
  1242
		undoCommand.replace ("PATH",bakMapPath);
insilmaril@721
  1243
	}
insilmaril@721
  1244
insilmaril@721
  1245
	if (!backupXML.isEmpty())
insilmaril@721
  1246
		// Write XML Data to disk
insilmaril@721
  1247
		saveStringToDisk (bakMapPath,backupXML);
insilmaril@721
  1248
insilmaril@721
  1249
	// We would have to save all actions in a tree, to keep track of 
insilmaril@721
  1250
	// possible redos after a action. Possible, but we are too lazy: forget about redos.
insilmaril@721
  1251
	redosAvail=0;
insilmaril@721
  1252
insilmaril@721
  1253
	// Write the current state to disk
insilmaril@721
  1254
	undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
insilmaril@721
  1255
	undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
insilmaril@721
  1256
	undoSet.setEntry ("/history/curStep",QString::number(curStep));
insilmaril@721
  1257
	undoSet.setEntry (QString("/history/step-%1/undoCommand").arg(curStep),undoCommand);
insilmaril@721
  1258
	undoSet.setEntry (QString("/history/step-%1/undoSelection").arg(curStep),undoSelection);
insilmaril@721
  1259
	undoSet.setEntry (QString("/history/step-%1/redoCommand").arg(curStep),redoCom);
insilmaril@721
  1260
	undoSet.setEntry (QString("/history/step-%1/redoSelection").arg(curStep),redoSelection);
insilmaril@721
  1261
	undoSet.setEntry (QString("/history/step-%1/comment").arg(curStep),comment);
insilmaril@721
  1262
	undoSet.setEntry (QString("/history/version"),vymVersion);
insilmaril@721
  1263
	undoSet.writeSettings(histPath);
insilmaril@721
  1264
insilmaril@721
  1265
	if (debug)
insilmaril@721
  1266
	{
insilmaril@721
  1267
		// TODO remove after testing
insilmaril@721
  1268
		//cout << "          into="<< histPath.toStdString()<<endl;
insilmaril@721
  1269
		cout << "    stepsTotal="<<stepsTotal<<
insilmaril@721
  1270
		", undosAvail="<<undosAvail<<
insilmaril@721
  1271
		", redosAvail="<<redosAvail<<
insilmaril@721
  1272
		", curStep="<<curStep<<endl;
insilmaril@721
  1273
		cout << "    ---------------------------"<<endl;
insilmaril@721
  1274
		cout << "    comment="<<comment.toStdString()<<endl;
insilmaril@721
  1275
		cout << "    undoCom="<<undoCommand.toStdString()<<endl;
insilmaril@721
  1276
		cout << "    undoSel="<<undoSelection.toStdString()<<endl;
insilmaril@721
  1277
		cout << "    redoCom="<<redoCom.toStdString()<<endl;
insilmaril@721
  1278
		cout << "    redoSel="<<redoSelection.toStdString()<<endl;
insilmaril@721
  1279
		if (saveSel) cout << "    saveSel="<<qPrintable (getSelectString(saveSel))<<endl;
insilmaril@721
  1280
		cout << "    ---------------------------"<<endl;
insilmaril@721
  1281
	}
insilmaril@721
  1282
insilmaril@721
  1283
	mainWindow->updateHistory (undoSet);
insilmaril@721
  1284
	setChanged();
insilmaril@721
  1285
	updateActions();
insilmaril@721
  1286
}
insilmaril@721
  1287
insilmaril@721
  1288
insilmaril@721
  1289
void VymModel::saveStateChangingPart(LinkableMapObj *undoSel, LinkableMapObj* redoSel, const QString &rc, const QString &comment)
insilmaril@721
  1290
{
insilmaril@721
  1291
	// save the selected part of the map, Undo will replace part of map 
insilmaril@721
  1292
	QString undoSelection="";
insilmaril@721
  1293
	if (undoSel)
insilmaril@721
  1294
		undoSelection=getSelectString(undoSel);
insilmaril@721
  1295
	else
insilmaril@721
  1296
		qWarning ("VymModel::saveStateChangingPart  no undoSel given!");
insilmaril@721
  1297
	QString redoSelection="";
insilmaril@721
  1298
	if (redoSel)
insilmaril@721
  1299
		redoSelection=getSelectString(undoSel);
insilmaril@721
  1300
	else
insilmaril@721
  1301
		qWarning ("VymModel::saveStateChangingPart  no redoSel given!");
insilmaril@721
  1302
		
insilmaril@721
  1303
insilmaril@721
  1304
	saveState (PartOfMap,
insilmaril@721
  1305
		undoSelection, "addMapReplace (\"PATH\")",
insilmaril@721
  1306
		redoSelection, rc, 
insilmaril@721
  1307
		comment, 
insilmaril@721
  1308
		undoSel);
insilmaril@721
  1309
}
insilmaril@721
  1310
insilmaril@721
  1311
void VymModel::saveStateRemovingPart(LinkableMapObj *redoSel, const QString &comment)
insilmaril@721
  1312
{
insilmaril@721
  1313
	if (!redoSel)
insilmaril@721
  1314
	{
insilmaril@721
  1315
		qWarning ("VymModel::saveStateRemovingPart  no redoSel given!");
insilmaril@721
  1316
		return;
insilmaril@721
  1317
	}
insilmaril@721
  1318
	QString undoSelection=getSelectString (redoSel->getParObj());
insilmaril@721
  1319
	QString redoSelection=getSelectString(redoSel);
insilmaril@721
  1320
	if (typeid(*redoSel) == typeid(BranchObj)  ) 
insilmaril@721
  1321
	{
insilmaril@721
  1322
		// save the selected branch of the map, Undo will insert part of map 
insilmaril@721
  1323
		saveState (PartOfMap,
insilmaril@749
  1324
			undoSelection, QString("addMapInsert (\"PATH\",%1)").arg(redoSel->getTreeItem()->num()),
insilmaril@721
  1325
			redoSelection, "delete ()", 
insilmaril@721
  1326
			comment, 
insilmaril@721
  1327
			redoSel);
insilmaril@721
  1328
	}
insilmaril@721
  1329
}
insilmaril@721
  1330
insilmaril@721
  1331
insilmaril@721
  1332
void VymModel::saveState(LinkableMapObj *undoSel, const QString &uc, LinkableMapObj *redoSel, const QString &rc, const QString &comment) 
insilmaril@721
  1333
{
insilmaril@721
  1334
	// "Normal" savestate: save commands, selections and comment
insilmaril@721
  1335
	// so just save commands for undo and redo
insilmaril@721
  1336
	// and use current selection
insilmaril@721
  1337
insilmaril@721
  1338
	QString redoSelection="";
insilmaril@721
  1339
	if (redoSel) redoSelection=getSelectString(redoSel);
insilmaril@721
  1340
	QString undoSelection="";
insilmaril@721
  1341
	if (undoSel) undoSelection=getSelectString(undoSel);
insilmaril@721
  1342
insilmaril@721
  1343
	saveState (UndoCommand,
insilmaril@721
  1344
		undoSelection, uc,
insilmaril@721
  1345
		redoSelection, rc, 
insilmaril@721
  1346
		comment, 
insilmaril@721
  1347
		NULL);
insilmaril@721
  1348
}
insilmaril@721
  1349
insilmaril@721
  1350
void VymModel::saveState(const QString &undoSel, const QString &uc, const QString &redoSel, const QString &rc, const QString &comment) 
insilmaril@721
  1351
{
insilmaril@721
  1352
	// "Normal" savestate: save commands, selections and comment
insilmaril@721
  1353
	// so just save commands for undo and redo
insilmaril@721
  1354
	// and use current selection
insilmaril@721
  1355
	saveState (UndoCommand,
insilmaril@721
  1356
		undoSel, uc,
insilmaril@721
  1357
		redoSel, rc, 
insilmaril@721
  1358
		comment, 
insilmaril@721
  1359
		NULL);
insilmaril@721
  1360
}
insilmaril@721
  1361
insilmaril@721
  1362
void VymModel::saveState(const QString &uc, const QString &rc, const QString &comment) 
insilmaril@721
  1363
{
insilmaril@721
  1364
	// "Normal" savestate applied to model (no selection needed): 
insilmaril@721
  1365
	// save commands  and comment
insilmaril@721
  1366
	saveState (UndoCommand,
insilmaril@721
  1367
		NULL, uc,
insilmaril@721
  1368
		NULL, rc, 
insilmaril@721
  1369
		comment, 
insilmaril@721
  1370
		NULL);
insilmaril@721
  1371
}
insilmaril@721
  1372
insilmaril@721
  1373
insilmaril@650
  1374
QGraphicsScene* VymModel::getScene ()
insilmaril@650
  1375
{
insilmaril@650
  1376
	return mapScene;
insilmaril@650
  1377
}
insilmaril@650
  1378
insilmaril@650
  1379
LinkableMapObj* VymModel::findMapObj(QPointF p, LinkableMapObj *excludeLMO)
insilmaril@650
  1380
{
insilmaril@650
  1381
	LinkableMapObj *lmo;
insilmaril@650
  1382
insilmaril@650
  1383
	for (int i=0;i<mapCenters.count(); i++)
insilmaril@650
  1384
	{
insilmaril@650
  1385
		lmo=mapCenters.at(i)->findMapObj (p,excludeLMO);
insilmaril@650
  1386
		if (lmo) return lmo;
insilmaril@650
  1387
	}
insilmaril@650
  1388
	return NULL;
insilmaril@650
  1389
}
insilmaril@650
  1390
insilmaril@650
  1391
LinkableMapObj* VymModel::findObjBySelect(const QString &s)
insilmaril@650
  1392
{
insilmaril@650
  1393
	LinkableMapObj *lmo;
insilmaril@650
  1394
	if (!s.isEmpty() )
insilmaril@650
  1395
	{
insilmaril@650
  1396
		QString part;
insilmaril@650
  1397
		QString typ;
insilmaril@650
  1398
		QString num;
insilmaril@650
  1399
		part=s.section(",",0,0);
insilmaril@652
  1400
		typ=part.left (2);
insilmaril@650
  1401
		num=part.right(part.length() - 3);
insilmaril@650
  1402
		if (typ=="mc" && num.toInt()>=0 && num.toInt() <mapCenters.count() )
insilmaril@650
  1403
			return mapCenters.at(num.toInt() );
insilmaril@650
  1404
	}		
insilmaril@650
  1405
insilmaril@650
  1406
	for (int i=0; i<mapCenters.count(); i++)
insilmaril@650
  1407
	{
insilmaril@650
  1408
		lmo=mapCenters.at(i)->findObjBySelect(s);
insilmaril@650
  1409
		if (lmo) return lmo;
insilmaril@650
  1410
	}	
insilmaril@650
  1411
	return NULL;
insilmaril@650
  1412
}
insilmaril@650
  1413
insilmaril@650
  1414
LinkableMapObj* VymModel::findID (const QString &s)
insilmaril@650
  1415
{
insilmaril@650
  1416
	LinkableMapObj *lmo;
insilmaril@650
  1417
	for (int i=0; i<mapCenters.count(); i++)
insilmaril@650
  1418
	{
insilmaril@650
  1419
		lmo=mapCenters.at(i)->findID (s);
insilmaril@650
  1420
		if (lmo) return lmo;
insilmaril@650
  1421
	}	
insilmaril@650
  1422
	return NULL;
insilmaril@650
  1423
}
insilmaril@650
  1424
insilmaril@721
  1425
//////////////////////////////////////////////
insilmaril@721
  1426
// Interface 
insilmaril@721
  1427
//////////////////////////////////////////////
insilmaril@721
  1428
void VymModel::setVersion (const QString &s)
insilmaril@721
  1429
{
insilmaril@721
  1430
	version=s;
insilmaril@721
  1431
}
insilmaril@721
  1432
insilmaril@721
  1433
void VymModel::setAuthor (const QString &s)
insilmaril@721
  1434
{
insilmaril@721
  1435
	saveState (
insilmaril@721
  1436
		QString ("setMapAuthor (\"%1\")").arg(author),
insilmaril@721
  1437
		QString ("setMapAuthor (\"%1\")").arg(s),
insilmaril@721
  1438
		QString ("Set author of map to \"%1\"").arg(s)
insilmaril@721
  1439
	);
insilmaril@721
  1440
insilmaril@721
  1441
	author=s;
insilmaril@721
  1442
}
insilmaril@721
  1443
insilmaril@721
  1444
QString VymModel::getAuthor()
insilmaril@721
  1445
{
insilmaril@721
  1446
	return author;
insilmaril@721
  1447
}
insilmaril@721
  1448
insilmaril@721
  1449
void VymModel::setComment (const QString &s)
insilmaril@721
  1450
{
insilmaril@721
  1451
	saveState (
insilmaril@721
  1452
		QString ("setMapComment (\"%1\")").arg(comment),
insilmaril@721
  1453
		QString ("setMapComment (\"%1\")").arg(s),
insilmaril@721
  1454
		QString ("Set comment of map")
insilmaril@721
  1455
	);
insilmaril@721
  1456
insilmaril@721
  1457
	comment=s;
insilmaril@721
  1458
}
insilmaril@721
  1459
insilmaril@721
  1460
QString VymModel::getComment ()
insilmaril@721
  1461
{
insilmaril@721
  1462
	return comment;
insilmaril@721
  1463
}
insilmaril@721
  1464
insilmaril@721
  1465
QString VymModel::getDate ()
insilmaril@721
  1466
{
insilmaril@721
  1467
	return QDate::currentDate().toString ("yyyy-MM-dd");
insilmaril@721
  1468
}
insilmaril@721
  1469
insilmaril@749
  1470
int VymModel::branchCount()	// FIXME-2 Optimize this: use internal counter instead of going through whole map each time...
insilmaril@742
  1471
{
insilmaril@742
  1472
	int c=0;
insilmaril@742
  1473
	TreeItem *cur=NULL;
insilmaril@742
  1474
	TreeItem *prev=NULL;
insilmaril@742
  1475
	int d;
insilmaril@742
  1476
	next(cur,prev,d);
insilmaril@742
  1477
	while (cur) 
insilmaril@742
  1478
	{
insilmaril@742
  1479
		c++;
insilmaril@742
  1480
		next(cur,prev,d);
insilmaril@742
  1481
	}
insilmaril@742
  1482
	return c;
insilmaril@742
  1483
insilmaril@742
  1484
}
insilmaril@742
  1485
insilmaril@721
  1486
void VymModel::setHeading(const QString &s)
insilmaril@721
  1487
{
insilmaril@735
  1488
	BranchObj *sel=getSelectedBranch();
insilmaril@721
  1489
	if (sel)
insilmaril@721
  1490
	{
insilmaril@721
  1491
		saveState(
insilmaril@721
  1492
			sel,
insilmaril@721
  1493
			"setHeading (\""+sel->getHeading()+"\")", 
insilmaril@721
  1494
			sel,
insilmaril@721
  1495
			"setHeading (\""+s+"\")", 
insilmaril@721
  1496
			QString("Set heading of %1 to \"%2\"").arg(getObjectName(sel)).arg(s) );
insilmaril@721
  1497
		sel->setHeading(s );
insilmaril@746
  1498
		/* FIXME-3 testing only
insilmaril@736
  1499
		*/
insilmaril@738
  1500
		TreeItem *ti=getSelectedItem();
insilmaril@727
  1501
		if (ti)
insilmaril@726
  1502
		{
insilmaril@727
  1503
			ti->setHeading (s);
insilmaril@746
  1504
			//FIXME-3 VM ix is wrong ModelIndex below, ix2 is (hopefully) correct:
insilmaril@736
  1505
			//QModelIndex ix=index( ti->row(), ti->column(), index (0,0,QModelIndex()) );
insilmaril@746
  1506
			//FIXME-3 VM testing only cout <<"VM::setHeading  s="<<s.toStdString()<<"  ti="<<ti<<"  r,c=("<<ti->row()<<","<<ti->column()<<")"<<endl;
insilmaril@727
  1507
			QModelIndex ix2=index (ti);
insilmaril@727
  1508
			emit (dataChanged ( ix2,ix2));
insilmaril@726
  1509
		}
insilmaril@727
  1510
		else
insilmaril@727
  1511
		{
insilmaril@727
  1512
			cout << "Warning: VM::setHeading ti==NULL\n";
insilmaril@727
  1513
		}
insilmaril@727
  1514
insilmaril@727
  1515
		//cout <<"                (r,c)=("<<ix2.row()<<","<<ix2.column()<<")"<<endl;
insilmaril@726
  1516
insilmaril@721
  1517
		reposition();
insilmaril@721
  1518
		selection.update();
insilmaril@721
  1519
		ensureSelectionVisible();
insilmaril@721
  1520
	}
insilmaril@721
  1521
}
insilmaril@721
  1522
insilmaril@741
  1523
BranchObj* VymModel::findText (QString s, bool cs)   
insilmaril@721
  1524
{
insilmaril@741
  1525
	int d=0;
insilmaril@721
  1526
	QTextDocument::FindFlags flags=0;
insilmaril@721
  1527
	if (cs) flags=QTextDocument::FindCaseSensitively;
insilmaril@721
  1528
insilmaril@741
  1529
	if (!findCurrent) 
insilmaril@721
  1530
	{	// Nothing found or new find process
insilmaril@721
  1531
		if (EOFind)
insilmaril@721
  1532
			// nothing found, start again
insilmaril@721
  1533
			EOFind=false;
insilmaril@741
  1534
		findCurrent=NULL;	
insilmaril@741
  1535
		findPrevious=NULL;	
insilmaril@741
  1536
		next (findCurrent,findPrevious,d);
insilmaril@721
  1537
	}	
insilmaril@721
  1538
	bool searching=true;
insilmaril@721
  1539
	bool foundNote=false;
insilmaril@721
  1540
	while (searching && !EOFind)
insilmaril@721
  1541
	{
insilmaril@741
  1542
		if (findCurrent)
insilmaril@721
  1543
		{
insilmaril@721
  1544
			// Searching in Note
insilmaril@741
  1545
			if (findCurrent->getNote().contains(s,cs))
insilmaril@721
  1546
			{
insilmaril@741
  1547
				select (findCurrent);
insilmaril@741
  1548
				/*
insilmaril@735
  1549
				if (getSelectedBranch()!=itFind) 
insilmaril@721
  1550
				{
insilmaril@735
  1551
					select(itFind);
insilmaril@721
  1552
					ensureSelectionVisible();
insilmaril@721
  1553
				}
insilmaril@741
  1554
				*/
insilmaril@721
  1555
				if (textEditor->findText(s,flags)) 
insilmaril@721
  1556
				{
insilmaril@721
  1557
					searching=false;
insilmaril@721
  1558
					foundNote=true;
insilmaril@721
  1559
				}	
insilmaril@721
  1560
			}
insilmaril@721
  1561
			// Searching in Heading
insilmaril@741
  1562
			if (searching && findCurrent->getHeading().contains (s,cs) ) 
insilmaril@721
  1563
			{
insilmaril@741
  1564
				select(findCurrent);
insilmaril@721
  1565
				searching=false;
insilmaril@721
  1566
			}
insilmaril@721
  1567
		}	
insilmaril@721
  1568
		if (!foundNote)
insilmaril@721
  1569
		{
insilmaril@741
  1570
			if (!next(findCurrent,findPrevious,d) )
insilmaril@741
  1571
				EOFind=true;
insilmaril@721
  1572
		}
insilmaril@721
  1573
	//cout <<"still searching...  "<<qPrintable( itFind->getHeading())<<endl;
insilmaril@721
  1574
	}	
insilmaril@721
  1575
	if (!searching)
insilmaril@735
  1576
		return getSelectedBranch();
insilmaril@721
  1577
	else
insilmaril@721
  1578
		return NULL;
insilmaril@721
  1579
}
insilmaril@721
  1580
insilmaril@721
  1581
void VymModel::findReset()
insilmaril@721
  1582
{	// Necessary if text to find changes during a find process
insilmaril@741
  1583
	findCurrent=NULL;
insilmaril@741
  1584
	findPrevious=NULL;
insilmaril@721
  1585
	EOFind=false;
insilmaril@721
  1586
}
insilmaril@721
  1587
insilmaril@721
  1588
insilmaril@721
  1589
insilmaril@721
  1590
void VymModel::setScene (QGraphicsScene *s)
insilmaril@721
  1591
{
insilmaril@746
  1592
	mapScene=s;	// FIXME-2 VM should not be necessary anymore, move all occurences to MapEditor
insilmaril@728
  1593
    //init();	// Here we have a mapScene set, 
insilmaril@721
  1594
			// which is (still) needed to create MapCenters
insilmaril@721
  1595
}
insilmaril@721
  1596
insilmaril@721
  1597
void VymModel::setURL(const QString &url)
insilmaril@721
  1598
{
insilmaril@735
  1599
	BranchObj *bo=getSelectedBranch();
insilmaril@721
  1600
	if (bo)
insilmaril@721
  1601
	{
insilmaril@721
  1602
		QString oldurl=bo->getURL();
insilmaril@721
  1603
		bo->setURL (url);
insilmaril@721
  1604
		saveState (
insilmaril@721
  1605
			bo,
insilmaril@721
  1606
			QString ("setURL (\"%1\")").arg(oldurl),
insilmaril@721
  1607
			bo,
insilmaril@721
  1608
			QString ("setURL (\"%1\")").arg(url),
insilmaril@721
  1609
			QString ("set URL of %1 to %2").arg(getObjectName(bo)).arg(url)
insilmaril@721
  1610
		);
insilmaril@721
  1611
		updateActions();
insilmaril@721
  1612
		reposition();
insilmaril@721
  1613
		selection.update();
insilmaril@721
  1614
		ensureSelectionVisible();
insilmaril@721
  1615
	}
insilmaril@721
  1616
}	
insilmaril@721
  1617
insilmaril@721
  1618
QString VymModel::getURL()
insilmaril@721
  1619
{
insilmaril@735
  1620
	BranchObj *bo=getSelectedBranch();
insilmaril@721
  1621
	if (bo)
insilmaril@721
  1622
		return bo->getURL();
insilmaril@721
  1623
	else
insilmaril@721
  1624
		return "";
insilmaril@721
  1625
}
insilmaril@721
  1626
insilmaril@746
  1627
QStringList VymModel::getURLs()	// FIXME-1	first, next moved to vymmodel
insilmaril@746
  1628
{
insilmaril@746
  1629
	return QStringList();
insilmaril@746
  1630
	/*
insilmaril@721
  1631
	QStringList urls;
insilmaril@735
  1632
	BranchObj *bo=getSelectedBranch();
insilmaril@721
  1633
	if (bo)
insilmaril@721
  1634
	{		
insilmaril@721
  1635
		bo=bo->first();	
insilmaril@721
  1636
		while (bo) 
insilmaril@721
  1637
		{
insilmaril@721
  1638
			if (!bo->getURL().isEmpty()) urls.append( bo->getURL());
insilmaril@721
  1639
			bo=bo->next();
insilmaril@721
  1640
		}	
insilmaril@721
  1641
	}	
insilmaril@721
  1642
	return urls;
insilmaril@746
  1643
	*/
insilmaril@721
  1644
}
insilmaril@721
  1645
insilmaril@721
  1646
void VymModel::linkFloatImageTo(const QString &dstString)	
insilmaril@721
  1647
{
insilmaril@721
  1648
	FloatImageObj *fio=selection.getFloatImage();
insilmaril@721
  1649
	if (fio)
insilmaril@721
  1650
	{
insilmaril@721
  1651
		BranchObj *dst=(BranchObj*)findObjBySelect(dstString);
insilmaril@721
  1652
		if (dst && (typeid(*dst)==typeid (BranchObj) || 
insilmaril@721
  1653
					typeid(*dst)==typeid (MapCenterObj)))
insilmaril@721
  1654
		{			
insilmaril@721
  1655
			LinkableMapObj *dstPar=dst->getParObj();
insilmaril@721
  1656
			QString parString=getSelectString(dstPar);
insilmaril@721
  1657
			QString fioPreSelectString=getSelectString(fio);
insilmaril@721
  1658
			QString fioPreParentSelectString=getSelectString (fio->getParObj());
insilmaril@721
  1659
			((BranchObj*)dst)->addFloatImage (fio);
insilmaril@721
  1660
			selection.unselect();
insilmaril@721
  1661
			((BranchObj*)(fio->getParObj()))->removeFloatImage (fio);
insilmaril@721
  1662
			fio=((BranchObj*)dst)->getLastFloatImage();
insilmaril@721
  1663
			fio->setRelPos();
insilmaril@721
  1664
			fio->reposition();
insilmaril@721
  1665
			selection.select(fio);
insilmaril@721
  1666
			saveState(
insilmaril@721
  1667
				getSelectString(fio),
insilmaril@721
  1668
				QString("linkTo (\"%1\")").arg(fioPreParentSelectString), 
insilmaril@721
  1669
				fioPreSelectString, 
insilmaril@721
  1670
				QString ("linkTo (\"%1\")").arg(dstString),
insilmaril@721
  1671
				QString ("Link floatimage to %1").arg(getObjectName(dst)));
insilmaril@721
  1672
		}
insilmaril@721
  1673
	}
insilmaril@721
  1674
}
insilmaril@721
  1675
insilmaril@721
  1676
insilmaril@721
  1677
void VymModel::setFrameType(const FrameObj::FrameType &t)
insilmaril@721
  1678
{
insilmaril@735
  1679
	BranchObj *bo=getSelectedBranch();
insilmaril@721
  1680
	if (bo)
insilmaril@721
  1681
	{
insilmaril@721
  1682
		QString s=bo->getFrameTypeName();
insilmaril@721
  1683
		bo->setFrameType (t);
insilmaril@721
  1684
		saveState (bo, QString("setFrameType (\"%1\")").arg(s),
insilmaril@721
  1685
			bo, QString ("setFrameType (\"%1\")").arg(bo->getFrameTypeName()),QString ("set type of frame to %1").arg(s));
insilmaril@721
  1686
		reposition();
insilmaril@721
  1687
		bo->updateLink();
insilmaril@721
  1688
	}
insilmaril@721
  1689
}
insilmaril@721
  1690
insilmaril@721
  1691
void VymModel::setFrameType(const QString &s)	
insilmaril@721
  1692
{
insilmaril@735
  1693
	BranchObj *bo=getSelectedBranch();
insilmaril@721
  1694
	if (bo)
insilmaril@721
  1695
	{
insilmaril@721
  1696
		saveState (bo, QString("setFrameType (\"%1\")").arg(bo->getFrameTypeName()),
insilmaril@721
  1697
			bo, QString ("setFrameType (\"%1\")").arg(s),QString ("set type of frame to %1").arg(s));
insilmaril@721
  1698
		bo->setFrameType (s);
insilmaril@721
  1699
		reposition();
insilmaril@721
  1700
		bo->updateLink();
insilmaril@721
  1701
	}
insilmaril@721
  1702
}
insilmaril@721
  1703
insilmaril@721
  1704
void VymModel::setFramePenColor(const QColor &c)	
insilmaril@721
  1705
{
insilmaril@735
  1706
	BranchObj *bo=getSelectedBranch();
insilmaril@721
  1707
	if (bo)
insilmaril@721
  1708
	{
insilmaril@721
  1709
		saveState (bo, QString("setFramePenColor (\"%1\")").arg(bo->getFramePenColor().name() ),
insilmaril@721
  1710
			bo, QString ("setFramePenColor (\"%1\")").arg(c.name() ),QString ("set pen color of frame to %1").arg(c.name() ));
insilmaril@721
  1711
		bo->setFramePenColor (c);
insilmaril@721
  1712
	}	
insilmaril@721
  1713
}
insilmaril@721
  1714
insilmaril@721
  1715
void VymModel::setFrameBrushColor(const QColor &c)	
insilmaril@721
  1716
{
insilmaril@735
  1717
	BranchObj *bo=getSelectedBranch();
insilmaril@721
  1718
	if (bo)
insilmaril@721
  1719
	{
insilmaril@721
  1720
		saveState (bo, QString("setFrameBrushColor (\"%1\")").arg(bo->getFrameBrushColor().name() ),
insilmaril@721
  1721
			bo, QString ("setFrameBrushColor (\"%1\")").arg(c.name() ),QString ("set brush color of frame to %1").arg(c.name() ));
insilmaril@721
  1722
		bo->setFrameBrushColor (c);
insilmaril@721
  1723
	}	
insilmaril@721
  1724
}
insilmaril@721
  1725
insilmaril@721
  1726
void VymModel::setFramePadding (const int &i)
insilmaril@721
  1727
{
insilmaril@735
  1728
	BranchObj *bo=getSelectedBranch();
insilmaril@721
  1729
	if (bo)
insilmaril@721
  1730
	{
insilmaril@721
  1731
		saveState (bo, QString("setFramePadding (\"%1\")").arg(bo->getFramePadding() ),
insilmaril@721
  1732
			bo, QString ("setFramePadding (\"%1\")").arg(i),QString ("set brush color of frame to %1").arg(i));
insilmaril@721
  1733
		bo->setFramePadding (i);
insilmaril@721
  1734
		reposition();
insilmaril@721
  1735
		bo->updateLink();
insilmaril@721
  1736
	}	
insilmaril@721
  1737
}
insilmaril@721
  1738
insilmaril@721
  1739
void VymModel::setFrameBorderWidth(const int &i)
insilmaril@721
  1740
{
insilmaril@735
  1741
	BranchObj *bo=getSelectedBranch();
insilmaril@721
  1742
	if (bo)
insilmaril@721
  1743
	{
insilmaril@721
  1744
		saveState (bo, QString("setFrameBorderWidth (\"%1\")").arg(bo->getFrameBorderWidth() ),
insilmaril@721
  1745
			bo, QString ("setFrameBorderWidth (\"%1\")").arg(i),QString ("set border width of frame to %1").arg(i));
insilmaril@721
  1746
		bo->setFrameBorderWidth (i);
insilmaril@721
  1747
		reposition();
insilmaril@721
  1748
		bo->updateLink();
insilmaril@721
  1749
	}	
insilmaril@721
  1750
}
insilmaril@721
  1751
insilmaril@721
  1752
void VymModel::setIncludeImagesVer(bool b)	
insilmaril@721
  1753
{
insilmaril@735
  1754
	BranchObj *bo=getSelectedBranch();
insilmaril@721
  1755
	if (bo)
insilmaril@721
  1756
	{
insilmaril@721
  1757
		QString u= b ? "false" : "true";
insilmaril@721
  1758
		QString r=!b ? "false" : "true";
insilmaril@721
  1759
		
insilmaril@721
  1760
		saveState(
insilmaril@721
  1761
			bo,
insilmaril@721
  1762
			QString("setIncludeImagesVertically (%1)").arg(u),
insilmaril@721
  1763
			bo, 
insilmaril@721
  1764
			QString("setIncludeImagesVertically (%1)").arg(r),
insilmaril@721
  1765
			QString("Include images vertically in %1").arg(getObjectName(bo))
insilmaril@721
  1766
		);	
insilmaril@721
  1767
		bo->setIncludeImagesVer(b);
insilmaril@721
  1768
		reposition();
insilmaril@721
  1769
	}	
insilmaril@721
  1770
}
insilmaril@721
  1771
insilmaril@721
  1772
void VymModel::setIncludeImagesHor(bool b)	
insilmaril@721
  1773
{
insilmaril@735
  1774
	BranchObj *bo=getSelectedBranch();
insilmaril@721
  1775
	if (bo)
insilmaril@721
  1776
	{
insilmaril@721
  1777
		QString u= b ? "false" : "true";
insilmaril@721
  1778
		QString r=!b ? "false" : "true";
insilmaril@721
  1779
		
insilmaril@721
  1780
		saveState(
insilmaril@721
  1781
			bo,
insilmaril@721
  1782
			QString("setIncludeImagesHorizontally (%1)").arg(u),
insilmaril@721
  1783
			bo, 
insilmaril@721
  1784
			QString("setIncludeImagesHorizontally (%1)").arg(r),
insilmaril@721
  1785
			QString("Include images horizontally in %1").arg(getObjectName(bo))
insilmaril@721
  1786
		);	
insilmaril@721
  1787
		bo->setIncludeImagesHor(b);
insilmaril@721
  1788
		reposition();
insilmaril@721
  1789
	}	
insilmaril@721
  1790
}
insilmaril@721
  1791
insilmaril@721
  1792
void VymModel::setHideLinkUnselected (bool b)
insilmaril@721
  1793
{
insilmaril@735
  1794
	LinkableMapObj *sel=getSelectedLMO();
insilmaril@721
  1795
	if (sel &&
insilmaril@735
  1796
		(selectionType() == TreeItem::Branch || 
insilmaril@735
  1797
		selectionType() == TreeItem::MapCenter  ||
insilmaril@735
  1798
		selectionType() == TreeItem::Image ))
insilmaril@721
  1799
	{
insilmaril@721
  1800
		QString u= b ? "false" : "true";
insilmaril@721
  1801
		QString r=!b ? "false" : "true";
insilmaril@721
  1802
		
insilmaril@721
  1803
		saveState(
insilmaril@721
  1804
			sel,
insilmaril@721
  1805
			QString("setHideLinkUnselected (%1)").arg(u),
insilmaril@721
  1806
			sel, 
insilmaril@721
  1807
			QString("setHideLinkUnselected (%1)").arg(r),
insilmaril@721
  1808
			QString("Hide link of %1 if unselected").arg(getObjectName(sel))
insilmaril@721
  1809
		);	
insilmaril@721
  1810
		sel->setHideLinkUnselected(b);
insilmaril@721
  1811
	}
insilmaril@721
  1812
}
insilmaril@721
  1813
insilmaril@721
  1814
void VymModel::setHideExport(bool b)
insilmaril@721
  1815
{
insilmaril@735
  1816
	BranchObj *bo=getSelectedBranch();
insilmaril@721
  1817
	if (bo)
insilmaril@721
  1818
	{
insilmaril@721
  1819
		bo->setHideInExport (b);
insilmaril@721
  1820
		QString u= b ? "false" : "true";
insilmaril@721
  1821
		QString r=!b ? "false" : "true";
insilmaril@721
  1822
		
insilmaril@721
  1823
		saveState(
insilmaril@721
  1824
			bo,
insilmaril@721
  1825
			QString ("setHideExport (%1)").arg(u),
insilmaril@721
  1826
			bo,
insilmaril@721
  1827
			QString ("setHideExport (%1)").arg(r),
insilmaril@721
  1828
			QString ("Set HideExport flag of %1 to %2").arg(getObjectName(bo)).arg (r)
insilmaril@721
  1829
		);	
insilmaril@721
  1830
		updateActions();
insilmaril@721
  1831
		reposition();
insilmaril@721
  1832
		selection.update();
insilmaril@746
  1833
		// FIXME-3 VM needed? scene()->update();
insilmaril@721
  1834
	}
insilmaril@721
  1835
}
insilmaril@721
  1836
insilmaril@721
  1837
void VymModel::toggleHideExport()
insilmaril@721
  1838
{
insilmaril@735
  1839
	BranchObj *bo=getSelectedBranch();
insilmaril@721
  1840
	if (bo)
insilmaril@721
  1841
		setHideExport ( !bo->hideInExport() );
insilmaril@721
  1842
}
insilmaril@721
  1843
insilmaril@721
  1844
insilmaril@721
  1845
void VymModel::copy()
insilmaril@721
  1846
{
insilmaril@735
  1847
	LinkableMapObj *sel=getSelectedLMO();
insilmaril@735
  1848
	if (sel &&
insilmaril@735
  1849
		(selectionType() == TreeItem::Branch || 
insilmaril@735
  1850
		selectionType() == TreeItem::MapCenter  ||
insilmaril@735
  1851
		selectionType() == TreeItem::Image ))
insilmaril@721
  1852
	{
insilmaril@721
  1853
		if (redosAvail == 0)
insilmaril@721
  1854
		{
insilmaril@721
  1855
			// Copy to history
insilmaril@721
  1856
			QString s=getSelectString(sel);
insilmaril@721
  1857
			saveState (PartOfMap, s, "nop ()", s, "copy ()","Copy selection to clipboard",sel  );
insilmaril@721
  1858
			curClipboard=curStep;
insilmaril@721
  1859
		}
insilmaril@721
  1860
insilmaril@721
  1861
		// Copy also to global clipboard, because we are at last step in history
insilmaril@721
  1862
		QString bakMapName(QString("history-%1").arg(curStep));
insilmaril@721
  1863
		QString bakMapDir(tmpMapDir +"/"+bakMapName);
insilmaril@721
  1864
		copyDir (bakMapDir,clipboardDir );
insilmaril@721
  1865
insilmaril@721
  1866
		clipboardEmpty=false;
insilmaril@721
  1867
		updateActions();
insilmaril@721
  1868
	}	    
insilmaril@721
  1869
}
insilmaril@721
  1870
insilmaril@721
  1871
insilmaril@721
  1872
void VymModel::pasteNoSave(const int &n)
insilmaril@721
  1873
{
insilmaril@721
  1874
	bool old=blockSaveState;
insilmaril@721
  1875
	blockSaveState=true;
insilmaril@721
  1876
	bool zippedOrg=zipped;
insilmaril@721
  1877
	if (redosAvail > 0 || n!=0)
insilmaril@721
  1878
	{
insilmaril@721
  1879
		// Use the "historical" buffer
insilmaril@721
  1880
		QString bakMapName(QString("history-%1").arg(n));
insilmaril@721
  1881
		QString bakMapDir(tmpMapDir +"/"+bakMapName);
insilmaril@721
  1882
		load (bakMapDir+"/"+clipboardFile,ImportAdd, VymMap);
insilmaril@721
  1883
	} else
insilmaril@721
  1884
		// Use the global buffer
insilmaril@721
  1885
		load (clipboardDir+"/"+clipboardFile,ImportAdd, VymMap);
insilmaril@721
  1886
	zipped=zippedOrg;
insilmaril@721
  1887
	blockSaveState=old;
insilmaril@721
  1888
}
insilmaril@721
  1889
insilmaril@721
  1890
void VymModel::paste()		
insilmaril@721
  1891
{   
insilmaril@735
  1892
	BranchObj *sel=getSelectedBranch();
insilmaril@721
  1893
	if (sel)
insilmaril@721
  1894
	{
insilmaril@721
  1895
		saveStateChangingPart(
insilmaril@721
  1896
			sel,
insilmaril@721
  1897
			sel,
insilmaril@721
  1898
			QString ("paste (%1)").arg(curClipboard),
insilmaril@721
  1899
			QString("Paste to %1").arg( getObjectName(sel))
insilmaril@721
  1900
		);
insilmaril@721
  1901
		pasteNoSave(0);
insilmaril@721
  1902
		reposition();
insilmaril@721
  1903
	}
insilmaril@721
  1904
}
insilmaril@721
  1905
insilmaril@721
  1906
void VymModel::cut()
insilmaril@721
  1907
{
insilmaril@735
  1908
	LinkableMapObj *sel=getSelectedLMO();
insilmaril@735
  1909
	if ( sel && (selectionType() == TreeItem::Branch ||
insilmaril@735
  1910
		selectionType()==TreeItem::MapCenter ||
insilmaril@735
  1911
		selectionType()==TreeItem::Image))
insilmaril@721
  1912
	{
insilmaril@721
  1913
	/* No savestate! savestate is called in cutNoSave
insilmaril@721
  1914
		saveStateChangingPart(
insilmaril@721
  1915
			sel->getParObj(),
insilmaril@721
  1916
			sel,
insilmaril@721
  1917
			"cut ()",
insilmaril@721
  1918
			QString("Cut %1").arg(getObjectName(sel ))
insilmaril@721
  1919
		);
insilmaril@721
  1920
	*/	
insilmaril@721
  1921
		copy();
insilmaril@721
  1922
		deleteSelection();
insilmaril@721
  1923
		reposition();
insilmaril@721
  1924
	}
insilmaril@721
  1925
}
insilmaril@721
  1926
insilmaril@721
  1927
void VymModel::moveBranchUp()
insilmaril@721
  1928
{
insilmaril@735
  1929
	BranchObj* bo=getSelectedBranch();
insilmaril@721
  1930
	BranchObj* par;
insilmaril@721
  1931
	if (bo)
insilmaril@721
  1932
	{
insilmaril@721
  1933
		if (!bo->canMoveBranchUp()) return;
insilmaril@721
  1934
		par=(BranchObj*)(bo->getParObj());
insilmaril@721
  1935
		BranchObj *obo=par->moveBranchUp (bo);	// bo will be the one below selection
insilmaril@721
  1936
		saveState (getSelectString(bo),"moveBranchDown ()",getSelectString(obo),"moveBranchUp ()",QString("Move up %1").arg(getObjectName(bo)));
insilmaril@721
  1937
		reposition();
insilmaril@746
  1938
		//FIXME-3 VM needed? scene()->update();
insilmaril@721
  1939
		selection.update();
insilmaril@721
  1940
		ensureSelectionVisible();
insilmaril@721
  1941
	}
insilmaril@721
  1942
}
insilmaril@721
  1943
insilmaril@721
  1944
void VymModel::moveBranchDown()
insilmaril@721
  1945
{
insilmaril@735
  1946
	BranchObj* bo=getSelectedBranch();
insilmaril@721
  1947
	BranchObj* par;
insilmaril@721
  1948
	if (bo)
insilmaril@721
  1949
	{
insilmaril@721
  1950
		if (!bo->canMoveBranchDown()) return;
insilmaril@721
  1951
		par=(BranchObj*)(bo->getParObj());
insilmaril@721
  1952
		BranchObj *obo=par->moveBranchDown(bo);	// bo will be the one above selection
insilmaril@721
  1953
		saveState(getSelectString(bo),"moveBranchUp ()",getSelectString(obo),"moveBranchDown ()",QString("Move down %1").arg(getObjectName(bo)));
insilmaril@721
  1954
		reposition();
insilmaril@746
  1955
		//FIXME-3 VM needed? scene()->update();
insilmaril@721
  1956
		selection.update();
insilmaril@721
  1957
		ensureSelectionVisible();
insilmaril@721
  1958
	}	
insilmaril@721
  1959
}
insilmaril@721
  1960
insilmaril@749
  1961
void VymModel::sortChildren()	// FIXME-1 not implemented yet
insilmaril@749
  1962
{
insilmaril@749
  1963
/*
insilmaril@735
  1964
	BranchObj* bo=getSelectedBranch();
insilmaril@721
  1965
	if (bo)
insilmaril@721
  1966
	{
insilmaril@749
  1967
		if(treeItem->branchCount()>1)
insilmaril@721
  1968
		{
insilmaril@721
  1969
			saveStateChangingPart(bo,bo, "sortChildren ()",QString("Sort children of %1").arg(getObjectName(bo)));
insilmaril@721
  1970
			bo->sortChildren();
insilmaril@721
  1971
			reposition();
insilmaril@721
  1972
			ensureSelectionVisible();
insilmaril@721
  1973
		}
insilmaril@721
  1974
	}
insilmaril@749
  1975
*/
insilmaril@721
  1976
}
insilmaril@721
  1977
insilmaril@726
  1978
void VymModel::createMapCenter()
insilmaril@726
  1979
{
insilmaril@726
  1980
	MapCenterObj *mco=addMapCenter (QPointF (0,0) );
insilmaril@735
  1981
	select (mco);
insilmaril@726
  1982
}
insilmaril@726
  1983
insilmaril@726
  1984
void VymModel::createBranch()
insilmaril@726
  1985
{
insilmaril@739
  1986
	addNewBranchInt (-2);
insilmaril@739
  1987
	return;
insilmaril@739
  1988
}
insilmaril@739
  1989
insilmaril@739
  1990
TreeItem* VymModel::createImage()
insilmaril@739
  1991
{
insilmaril@735
  1992
	BranchObj *bo=getSelectedBranch();
insilmaril@726
  1993
	if (bo)
insilmaril@726
  1994
	{
insilmaril@746
  1995
		FloatImageObj *newfio=bo->addFloatImage(); // FIXME-1 VM Old model, merge with below
insilmaril@726
  1996
insilmaril@726
  1997
		// Create TreeItem
insilmaril@726
  1998
		QList<QVariant> cData;
insilmaril@739
  1999
		cData << "VM:createImage" << "undef"<<"undef";
insilmaril@726
  2000
		TreeItem *parti=bo->getTreeItem();
insilmaril@726
  2001
		TreeItem *ti=new TreeItem (cData,parti);
insilmaril@739
  2002
		ti->setLMO (newfio);
insilmaril@739
  2003
		ti->setType (TreeItem::Image);
insilmaril@726
  2004
		parti->appendChild (ti);
insilmaril@726
  2005
insilmaril@739
  2006
		if (newfio)
insilmaril@726
  2007
		{
insilmaril@739
  2008
			newfio->setTreeItem (ti);
insilmaril@746
  2009
			select (newfio); // FIXME-2 VM really needed here?
insilmaril@739
  2010
			return ti;
insilmaril@726
  2011
		}
insilmaril@726
  2012
	}
insilmaril@739
  2013
	return NULL;
insilmaril@726
  2014
}
insilmaril@726
  2015
insilmaril@721
  2016
MapCenterObj* VymModel::addMapCenter ()
insilmaril@721
  2017
{
insilmaril@723
  2018
	MapCenterObj *mco=addMapCenter (contextPos);
insilmaril@746
  2019
	//FIXME-3 selection.select (mco);
insilmaril@721
  2020
	updateActions();
insilmaril@721
  2021
	ensureSelectionVisible();
insilmaril@721
  2022
	saveState (
insilmaril@721
  2023
		mco,
insilmaril@721
  2024
		"delete()",
insilmaril@721
  2025
		NULL,
insilmaril@723
  2026
		QString ("addMapCenter (%1,%2)").arg (contextPos.x()).arg(contextPos.y()),
insilmaril@723
  2027
		QString ("Adding MapCenter to (%1,%2)").arg (contextPos.x()).arg(contextPos.y())
insilmaril@721
  2028
	);	
insilmaril@721
  2029
	return mco;	
insilmaril@721
  2030
}
insilmaril@721
  2031
insilmaril@721
  2032
MapCenterObj* VymModel::addMapCenter(QPointF absPos)
insilmaril@721
  2033
{
insilmaril@749
  2034
insilmaril@749
  2035
	// Create TreeItem
insilmaril@749
  2036
	QModelIndex parix=index(rootItem);
insilmaril@749
  2037
insilmaril@749
  2038
	int n=rootItem->branchCount();
insilmaril@749
  2039
insilmaril@749
  2040
	emit (layoutAboutToBeChanged() );
insilmaril@749
  2041
	beginInsertRows (parix,n,n+1);
insilmaril@749
  2042
insilmaril@749
  2043
	QList<QVariant> cData;
insilmaril@749
  2044
	cData << "VM:addMapCenter" << "undef"<<"undef";
insilmaril@749
  2045
	MapCenterItem *ti=new MapCenterItem (cData,rootItem);
insilmaril@749
  2046
	cout << "VM::addMapCenter  ti="<<ti<<endl;
insilmaril@749
  2047
	ti->setType (TreeItem::MapCenter);
insilmaril@749
  2048
	rootItem->appendChild (ti);
insilmaril@749
  2049
insilmaril@749
  2050
	endInsertRows();
insilmaril@749
  2051
	emit (newChildObject (parix));
insilmaril@749
  2052
	emit (layoutChanged() );
insilmaril@749
  2053
insilmaril@749
  2054
	// Create MapObj
insilmaril@727
  2055
	MapCenterObj *mapCenter = new MapCenterObj(mapScene,this);
insilmaril@746
  2056
	mapCenter->setMapEditor(mapEditor);		//FIXME-3 VM needed to get defLinkStyle, mapLinkColorHint ... for later added objects
insilmaril@749
  2057
	mapCenter->setTreeItem (ti);	// TreeItem needs to exist before setVisibility
insilmaril@749
  2058
	ti->setLMO (mapCenter);
insilmaril@721
  2059
	mapCenter->move (absPos);
insilmaril@721
  2060
    mapCenter->setVisibility (true);
insilmaril@721
  2061
	mapCenter->setHeading (QApplication::translate("Heading of mapcenter in new map", "New map"));
insilmaril@721
  2062
	mapCenters.append(mapCenter);
insilmaril@745
  2063
	// Testing
insilmaril@745
  2064
/*
insilmaril@745
  2065
	qWarning ("MW::insertRow a");
insilmaril@745
  2066
    if (!insertRow(0, parix))
insilmaril@745
  2067
	{
insilmaril@745
  2068
		std::cout << "  war nix...\n";
insilmaril@745
  2069
	}	
insilmaril@745
  2070
	qWarning ("MW::insertRow b");
insilmaril@745
  2071
*/
insilmaril@745
  2072
insilmaril@721
  2073
	return mapCenter;
insilmaril@721
  2074
}
insilmaril@721
  2075
insilmaril@723
  2076
MapCenterObj* VymModel::removeMapCenter(MapCenterObj* mco)
insilmaril@721
  2077
{
insilmaril@721
  2078
	int i=mapCenters.indexOf (mco);
insilmaril@721
  2079
	if (i>=0)
insilmaril@721
  2080
	{
insilmaril@721
  2081
		mapCenters.removeAt (i);
insilmaril@721
  2082
		delete (mco);
insilmaril@721
  2083
		if (i>0) return mapCenters.at(i-1);	// Return previous MCO
insilmaril@721
  2084
	}
insilmaril@721
  2085
	return NULL;
insilmaril@721
  2086
}
insilmaril@721
  2087
insilmaril@723
  2088
MapCenterObj* VymModel::getLastMapCenter()
insilmaril@723
  2089
{
insilmaril@723
  2090
	if (mapCenters.size()>0)
insilmaril@723
  2091
		return mapCenters.last();
insilmaril@723
  2092
	else
insilmaril@723
  2093
		return NULL;
insilmaril@723
  2094
insilmaril@723
  2095
}
insilmaril@723
  2096
insilmaril@721
  2097
insilmaril@721
  2098
BranchObj* VymModel::addNewBranchInt(int num)
insilmaril@721
  2099
{
insilmaril@721
  2100
	// Depending on pos:
insilmaril@721
  2101
	// -3		insert in children of parent  above selection 
insilmaril@721
  2102
	// -2		add branch to selection 
insilmaril@721
  2103
	// -1		insert in children of parent below selection 
insilmaril@721
  2104
	// 0..n		insert in children of parent at pos
insilmaril@721
  2105
	BranchObj *newbo=NULL;
insilmaril@735
  2106
	BranchObj *bo=getSelectedBranch();
insilmaril@749
  2107
	BranchItem *bi=getSelectedBranchItem();
insilmaril@721
  2108
	if (bo)
insilmaril@721
  2109
	{
insilmaril@721
  2110
		if (num==-2)
insilmaril@721
  2111
		{
insilmaril@749
  2112
			// Create TreeItem
insilmaril@749
  2113
			QList<QVariant> cData;
insilmaril@749
  2114
			cData << "new" << "undef"<<"undef";
insilmaril@749
  2115
insilmaril@749
  2116
			BranchItem *parbi=bi;
insilmaril@749
  2117
			QModelIndex parix=index(parbi);
insilmaril@749
  2118
			int n=parbi->branchCount();
insilmaril@749
  2119
insilmaril@749
  2120
			emit (layoutAboutToBeChanged() );
insilmaril@749
  2121
			beginInsertRows (parix,n,n+1);
insilmaril@749
  2122
			bi=new BranchItem (cData,parbi);
insilmaril@749
  2123
			bi->setType (TreeItem::Branch);
insilmaril@749
  2124
insilmaril@749
  2125
			parbi->appendChild (bi);
insilmaril@749
  2126
			endInsertRows ();
insilmaril@749
  2127
			emit (newChildObject (parix));
insilmaril@749
  2128
			emit (layoutChanged() );
insilmaril@749
  2129
insilmaril@721
  2130
			// save scroll state. If scrolled, automatically select
insilmaril@721
  2131
			// new branch in order to tmp unscroll parent...
insilmaril@721
  2132
			newbo=bo->addBranch();
insilmaril@739
  2133
insilmaril@739
  2134
			if (newbo)
insilmaril@739
  2135
			{
insilmaril@749
  2136
				bi->setLMO (newbo);
insilmaril@749
  2137
				newbo->setTreeItem (bi);
insilmaril@746
  2138
				select (newbo); // FIXME-2 VM really needed here?
insilmaril@739
  2139
			}
insilmaril@721
  2140
			
insilmaril@721
  2141
		}else if (num==-1)
insilmaril@721
  2142
		{
insilmaril@749
  2143
			num=bi->num()+1;
insilmaril@721
  2144
			bo=(BranchObj*)bo->getParObj();
insilmaril@746
  2145
			if (bo) newbo=bo->insertBranch(num);	//FIXME-1 VM still missing 
insilmaril@721
  2146
		}else if (num==-3)
insilmaril@721
  2147
		{
insilmaril@749
  2148
			num=bi->num();
insilmaril@721
  2149
			bo=(BranchObj*)bo->getParObj();
insilmaril@746
  2150
			if (bo) newbo=bo->insertBranch(num);	//FIXME-1 VM still missing
insilmaril@721
  2151
		}
insilmaril@721
  2152
	}	
insilmaril@721
  2153
	return newbo;
insilmaril@721
  2154
}	
insilmaril@721
  2155
insilmaril@721
  2156
BranchObj* VymModel::addNewBranch(int pos)
insilmaril@721
  2157
{
insilmaril@721
  2158
	// Different meaning than num in addNewBranchInt!
insilmaril@721
  2159
	// -1	add above
insilmaril@721
  2160
	//  0	add as child
insilmaril@721
  2161
	// +1	add below
insilmaril@735
  2162
	BranchObj *bo = getSelectedBranch();
insilmaril@721
  2163
	BranchObj *newbo=NULL;
insilmaril@721
  2164
insilmaril@721
  2165
	if (bo)
insilmaril@721
  2166
	{
insilmaril@749
  2167
		// FIXME-2 VM  do we still need this in model? setCursor (Qt::ArrowCursor);
insilmaril@721
  2168
insilmaril@721
  2169
		newbo=addNewBranchInt (pos-2);
insilmaril@721
  2170
insilmaril@721
  2171
		if (newbo)
insilmaril@721
  2172
		{
insilmaril@721
  2173
			saveState(
insilmaril@721
  2174
				newbo,		
insilmaril@721
  2175
				"delete ()",
insilmaril@721
  2176
				bo,
insilmaril@721
  2177
				QString ("addBranch (%1)").arg(pos),
insilmaril@721
  2178
				QString ("Add new branch to %1").arg(getObjectName(bo)));	
insilmaril@721
  2179
insilmaril@721
  2180
			reposition();
insilmaril@746
  2181
			// selection.update(); FIXME-3
insilmaril@721
  2182
			latestSelectionString=getSelectString(newbo);
insilmaril@721
  2183
			// In Network mode, the client needs to know where the new branch is,
insilmaril@721
  2184
			// so we have to pass on this information via saveState.
insilmaril@721
  2185
			// TODO: Get rid of this positioning workaround
insilmaril@721
  2186
			QString ps=qpointfToString (newbo->getAbsPos());
insilmaril@721
  2187
			sendData ("selectLatestAdded ()");
insilmaril@721
  2188
			sendData (QString("move %1").arg(ps));
insilmaril@721
  2189
			sendSelection();
insilmaril@721
  2190
		}
insilmaril@721
  2191
	}	
insilmaril@721
  2192
	return newbo;
insilmaril@721
  2193
}
insilmaril@721
  2194
insilmaril@721
  2195
insilmaril@721
  2196
BranchObj* VymModel::addNewBranchBefore()
insilmaril@721
  2197
{
insilmaril@721
  2198
	BranchObj *newbo=NULL;
insilmaril@735
  2199
	BranchObj *bo = getSelectedBranch();
insilmaril@735
  2200
	if (bo && selectionType()==TreeItem::Branch)
insilmaril@721
  2201
		 // We accept no MapCenterObj here, so we _have_ a parent
insilmaril@721
  2202
	{
insilmaril@721
  2203
		QPointF p=bo->getRelPos();
insilmaril@721
  2204
insilmaril@721
  2205
insilmaril@721
  2206
		BranchObj *parbo=(BranchObj*)(bo->getParObj());
insilmaril@721
  2207
insilmaril@721
  2208
		// add below selection
insilmaril@749
  2209
		newbo=parbo->insertBranch(bo->getTreeItem()->num()+1);		//FIXME-1 VM still missing
insilmaril@745
  2210
insilmaril@721
  2211
		if (newbo)
insilmaril@721
  2212
		{
insilmaril@721
  2213
			newbo->move2RelPos (p);
insilmaril@721
  2214
insilmaril@721
  2215
			// Move selection to new branch
insilmaril@721
  2216
			bo->linkTo (newbo,-1);
insilmaril@721
  2217
insilmaril@721
  2218
			saveState (newbo, "deleteKeepChildren ()", newbo, "addBranchBefore ()", 
insilmaril@721
  2219
				QString ("Add branch before %1").arg(getObjectName(bo)));
insilmaril@721
  2220
insilmaril@721
  2221
			reposition();
insilmaril@746
  2222
			// selection.update(); FIXME-3 
insilmaril@721
  2223
		}
insilmaril@721
  2224
	}	
insilmaril@721
  2225
	latestSelectionString=selection.getSelectString();
insilmaril@721
  2226
	return newbo;
insilmaril@721
  2227
}
insilmaril@721
  2228
insilmaril@721
  2229
void VymModel::deleteSelection()
insilmaril@721
  2230
{
insilmaril@746
  2231
	BranchObj *bo = getSelectedBranch();	// FIXME-2 VM should not be necessary
insilmaril@745
  2232
	if (!bo) return;
insilmaril@745
  2233
insilmaril@745
  2234
	QModelIndex ix=getSelectedIndex();
insilmaril@745
  2235
	if (!ix.isValid() ) return;
insilmaril@745
  2236
insilmaril@745
  2237
	QModelIndex parentIndex=parent(ix);
insilmaril@748
  2238
/*
insilmaril@748
  2239
	if (!parentIndex.isValid()) 
insilmaril@748
  2240
	{
insilmaril@748
  2241
	cout << "VM::delete no valid parent ix\n";
insilmaril@748
  2242
	return;
insilmaril@748
  2243
	}
insilmaril@748
  2244
*/	
insilmaril@745
  2245
	TreeItem *ti=bo->getTreeItem();
insilmaril@728
  2246
	
insilmaril@748
  2247
	if (selectionType()==TreeItem::MapCenter)	
insilmaril@721
  2248
	{
insilmaril@721
  2249
	//	BranchObj* par=(BranchObj*)(bo->getParObj());
insilmaril@745
  2250
		//selection.unselect();
insilmaril@746
  2251
		/* FIXME-1 VM Note:  does saveStateRemovingPart work for MCO? (No parent!)
insilmaril@721
  2252
		saveStateRemovingPart (bo, QString ("Delete %1").arg(getObjectName(bo)));
insilmaril@748
  2253
		*/	
insilmaril@748
  2254
		emit (layoutAboutToBeChanged() );
insilmaril@748
  2255
insilmaril@748
  2256
		parentIndex=parent(index(ti));
insilmaril@748
  2257
		cout << "VM::delete  ti="<<ti<<"  row="<<ix.row()<<"  parIndex.valid="<<parentIndex.isValid()<<endl;
insilmaril@748
  2258
		int n=ix.row();
insilmaril@748
  2259
		beginRemoveRows (parentIndex,n,n);
insilmaril@748
  2260
		removeRows (n,1,parentIndex);
insilmaril@721
  2261
		bo=removeMapCenter ((MapCenterObj*)bo);
insilmaril@748
  2262
		endRemoveRows();
insilmaril@721
  2263
		if (bo) 
insilmaril@721
  2264
		{
insilmaril@748
  2265
			select (bo);
insilmaril@721
  2266
			ensureSelectionVisible();
insilmaril@748
  2267
			ensureSelectionVisible();
insilmaril@748
  2268
			reposition();
insilmaril@721
  2269
		}	
insilmaril@748
  2270
insilmaril@748
  2271
		emit (layoutChanged() );
insilmaril@721
  2272
	}
insilmaril@745
  2273
	if (selectionType()==TreeItem::Branch)
insilmaril@721
  2274
	{
insilmaril@722
  2275
insilmaril@721
  2276
		BranchObj* par=(BranchObj*)bo->getParObj();
insilmaril@735
  2277
		unselect();
insilmaril@721
  2278
		saveStateRemovingPart (bo, QString ("Delete %1").arg(getObjectName(bo)));
insilmaril@745
  2279
insilmaril@745
  2280
		emit (layoutAboutToBeChanged() );
insilmaril@745
  2281
insilmaril@745
  2282
		cout << "VM::delete  ti="<<ti<<"  row="<<ix.row()<<endl;
insilmaril@745
  2283
		parentIndex=parent(index(ti));
insilmaril@748
  2284
		int n=ix.row();
insilmaril@745
  2285
		beginRemoveRows (parentIndex,n,n);
insilmaril@748
  2286
		removeRows (n,1,parentIndex);
insilmaril@749
  2287
		endRemoveRows();
insilmaril@748
  2288
		par->removeBranch(bo);	// remove from BranchObj lists...
insilmaril@735
  2289
		select (par);
insilmaril@721
  2290
		ensureSelectionVisible();
insilmaril@721
  2291
		reposition();
insilmaril@745
  2292
insilmaril@745
  2293
		emit (layoutChanged() );
insilmaril@721
  2294
		return;
insilmaril@721
  2295
	}
insilmaril@749
  2296
	//FloatImageObj *fio=selection.getFloatImage(); 	//FIXME-1 VM still missing
insilmaril@745
  2297
insilmaril@748
  2298
/*
insilmaril@721
  2299
	if (fio)
insilmaril@721
  2300
	{
insilmaril@721
  2301
		BranchObj* par=(BranchObj*)fio->getParObj();
insilmaril@721
  2302
		saveStateChangingPart(
insilmaril@721
  2303
			par, 
insilmaril@721
  2304
			fio,
insilmaril@721
  2305
			"delete ()",
insilmaril@721
  2306
			QString("Delete %1").arg(getObjectName(fio))
insilmaril@721
  2307
		);
insilmaril@735
  2308
		unselect();
insilmaril@721
  2309
		par->removeFloatImage(fio);
insilmaril@735
  2310
		select (par);
insilmaril@721
  2311
		reposition();
insilmaril@721
  2312
		ensureSelectionVisible();
insilmaril@721
  2313
		return;
insilmaril@721
  2314
	}
insilmaril@748
  2315
	*/
insilmaril@721
  2316
}
insilmaril@721
  2317
insilmaril@746
  2318
void VymModel::deleteKeepChildren()		//FIXME-1 VM still missing
insilmaril@745
  2319
insilmaril@721
  2320
{
insilmaril@735
  2321
	BranchObj *bo=getSelectedBranch();
insilmaril@721
  2322
	BranchObj *par;
insilmaril@721
  2323
	if (bo)
insilmaril@721
  2324
	{
insilmaril@721
  2325
		par=(BranchObj*)(bo->getParObj());
insilmaril@726
  2326
insilmaril@726
  2327
		// Don't use this on mapcenter
insilmaril@726
  2328
		if (!par) return;
insilmaril@726
  2329
insilmaril@726
  2330
		// Check if we have childs at all to keep
insilmaril@749
  2331
		if (bo->getTreeItem()->branchCount()==0) 
insilmaril@726
  2332
		{
insilmaril@726
  2333
			deleteSelection();
insilmaril@726
  2334
			return;
insilmaril@726
  2335
		}
insilmaril@726
  2336
insilmaril@721
  2337
		QPointF p=bo->getRelPos();
insilmaril@721
  2338
		saveStateChangingPart(
insilmaril@721
  2339
			bo->getParObj(),
insilmaril@721
  2340
			bo,
insilmaril@721
  2341
			"deleteKeepChildren ()",
insilmaril@721
  2342
			QString("Remove %1 and keep its children").arg(getObjectName(bo))
insilmaril@721
  2343
		);
insilmaril@721
  2344
insilmaril@721
  2345
		QString sel=getSelectString(bo);
insilmaril@721
  2346
		unselect();
insilmaril@721
  2347
		par->removeBranchHere(bo);
insilmaril@721
  2348
		reposition();
insilmaril@721
  2349
		select (sel);
insilmaril@735
  2350
		getSelectedBranch()->move2RelPos (p);
insilmaril@721
  2351
		reposition();
insilmaril@721
  2352
	}	
insilmaril@721
  2353
}
insilmaril@721
  2354
insilmaril@746
  2355
void VymModel::deleteChildren()		//FIXME-1 VM still missing
insilmaril@745
  2356
insilmaril@721
  2357
{
insilmaril@735
  2358
	BranchObj *bo=getSelectedBranch();
insilmaril@721
  2359
	if (bo)
insilmaril@721
  2360
	{		
insilmaril@721
  2361
		saveStateChangingPart(
insilmaril@721
  2362
			bo, 
insilmaril@721
  2363
			bo,
insilmaril@721
  2364
			"deleteChildren ()",
insilmaril@721
  2365
			QString( "Remove children of branch %1").arg(getObjectName(bo))
insilmaril@721
  2366
		);
insilmaril@721
  2367
		bo->removeChildren();
insilmaril@721
  2368
		reposition();
insilmaril@721
  2369
	}	
insilmaril@721
  2370
}
insilmaril@721
  2371
insilmaril@721
  2372
insilmaril@749
  2373
bool VymModel::scrollBranch(BranchItem *bi)
insilmaril@749
  2374
{
insilmaril@749
  2375
	if (bi)	
insilmaril@721
  2376
	{
insilmaril@749
  2377
		if (bi->isScrolled()) return false;
insilmaril@749
  2378
		if (bi->branchCount()==0) return false;
insilmaril@749
  2379
		if (bi->depth()==0) return false;
insilmaril@721
  2380
		QString u,r;
insilmaril@721
  2381
		r="scroll";
insilmaril@721
  2382
		u="unscroll";
insilmaril@749
  2383
		/* FIXME-1 no savestate yet
insilmaril@721
  2384
		saveState(
insilmaril@721
  2385
			bo,
insilmaril@721
  2386
			QString ("%1 ()").arg(u),
insilmaril@721
  2387
			bo,
insilmaril@721
  2388
			QString ("%1 ()").arg(r),
insilmaril@721
  2389
			QString ("%1 %2").arg(r).arg(getObjectName(bo))
insilmaril@721
  2390
		);
insilmaril@749
  2391
		*/
insilmaril@749
  2392
		bi->toggleScroll();
insilmaril@749
  2393
		//selection.update();
insilmaril@746
  2394
		// FIXME-3 VM needed? scene()->update();
insilmaril@721
  2395
		return true;
insilmaril@721
  2396
	}	
insilmaril@721
  2397
	return false;
insilmaril@721
  2398
}
insilmaril@721
  2399
insilmaril@749
  2400
bool VymModel::unscrollBranch(BranchItem *bi)
insilmaril@749
  2401
{
insilmaril@749
  2402
	if (bi)
insilmaril@721
  2403
	{
insilmaril@749
  2404
		if (bi->isScrolled()) return false;
insilmaril@749
  2405
		if (bi->branchCount()==0) return false;
insilmaril@749
  2406
		if (bi->depth()==0) return false;
insilmaril@749
  2407
insilmaril@721
  2408
		QString u,r;
insilmaril@721
  2409
		u="scroll";
insilmaril@721
  2410
		r="unscroll";
insilmaril@749
  2411
		/* FIXME-1 no savestate yet
insilmaril@721
  2412
		saveState(
insilmaril@721
  2413
			bo,
insilmaril@721
  2414
			QString ("%1 ()").arg(u),
insilmaril@721
  2415
			bo,
insilmaril@721
  2416
			QString ("%1 ()").arg(r),
insilmaril@721
  2417
			QString ("%1 %2").arg(r).arg(getObjectName(bo))
insilmaril@721
  2418
		);
insilmaril@749
  2419
		*/
insilmaril@749
  2420
		bi->toggleScroll();
insilmaril@749
  2421
		// selection.update();
insilmaril@746
  2422
		// FIXME-3 VM needed? scene()->update();
insilmaril@721
  2423
		return true;
insilmaril@721
  2424
	}	
insilmaril@721
  2425
	return false;
insilmaril@721
  2426
}
insilmaril@721
  2427
insilmaril@721
  2428
void VymModel::toggleScroll()
insilmaril@721
  2429
{
insilmaril@749
  2430
	BranchItem *bi=(BranchItem*)getSelectedBranchItem();
insilmaril@749
  2431
	if (bi && bi->getType()==TreeItem::Branch )
insilmaril@721
  2432
	{
insilmaril@749
  2433
		if (bi->isScrolled())
insilmaril@749
  2434
			unscrollBranch (bi);
insilmaril@721
  2435
		else
insilmaril@749
  2436
			scrollBranch (bi);
insilmaril@721
  2437
	}
insilmaril@721
  2438
}
insilmaril@721
  2439
insilmaril@746
  2440
void VymModel::unscrollChildren() 	// FIXME-1	first, next moved to vymmodel
insilmaril@746
  2441
insilmaril@746
  2442
{
insilmaril@746
  2443
/*
insilmaril@735
  2444
	BranchObj *bo=getSelectedBranch();
insilmaril@721
  2445
	if (bo)
insilmaril@721
  2446
	{
insilmaril@721
  2447
		bo->first();
insilmaril@721
  2448
		while (bo) 
insilmaril@721
  2449
		{
insilmaril@721
  2450
			if (bo->isScrolled()) unscrollBranch (bo);
insilmaril@721
  2451
			bo=bo->next();
insilmaril@721
  2452
		}
insilmaril@721
  2453
	}	
insilmaril@746
  2454
*/	
insilmaril@721
  2455
}
insilmaril@746
  2456
insilmaril@721
  2457
void VymModel::addFloatImage (const QPixmap &img) 
insilmaril@721
  2458
{
insilmaril@735
  2459
	BranchObj *bo=getSelectedBranch();
insilmaril@721
  2460
	if (bo)
insilmaril@721
  2461
  {
insilmaril@721
  2462
	FloatImageObj *fio=bo->addFloatImage();
insilmaril@721
  2463
    fio->load(img);
insilmaril@721
  2464
    fio->setOriginalFilename("No original filename (image added by dropevent)");	
insilmaril@721
  2465
	QString s=getSelectString(bo);
insilmaril@721
  2466
	saveState (PartOfMap, s, "nop ()", s, "copy ()","Copy dropped image to clipboard",fio  );
insilmaril@721
  2467
	saveState (fio,"delete ()", bo,QString("paste(%1)").arg(curStep),"Pasting dropped image");
insilmaril@721
  2468
    reposition();
insilmaril@746
  2469
    // FIXME-3 VM needed? scene()->update();
insilmaril@721
  2470
  }
insilmaril@721
  2471
}
insilmaril@721
  2472
insilmaril@721
  2473
insilmaril@721
  2474
void VymModel::colorBranch (QColor c)
insilmaril@721
  2475
{
insilmaril@735
  2476
	BranchObj *bo=getSelectedBranch();
insilmaril@721
  2477
	if (bo)
insilmaril@721
  2478
	{
insilmaril@721
  2479
		saveState(
insilmaril@721
  2480
			bo, 
insilmaril@721
  2481
			QString ("colorBranch (\"%1\")").arg(bo->getColor().name()),
insilmaril@721
  2482
			bo,
insilmaril@721
  2483
			QString ("colorBranch (\"%1\")").arg(c.name()),
insilmaril@721
  2484
			QString("Set color of %1 to %2").arg(getObjectName(bo)).arg(c.name())
insilmaril@721
  2485
		);	
insilmaril@721
  2486
		bo->setColor(c); // color branch
insilmaril@721
  2487
	}
insilmaril@721
  2488
}
insilmaril@721
  2489
insilmaril@721
  2490
void VymModel::colorSubtree (QColor c)
insilmaril@721
  2491
{
insilmaril@735
  2492
	BranchObj *bo=getSelectedBranch();
insilmaril@721
  2493
	if (bo) 
insilmaril@721
  2494
	{
insilmaril@721
  2495
		saveStateChangingPart(
insilmaril@721
  2496
			bo, 
insilmaril@721
  2497
			bo,
insilmaril@721
  2498
			QString ("colorSubtree (\"%1\")").arg(c.name()),
insilmaril@721
  2499
			QString ("Set color of %1 and children to %2").arg(getObjectName(bo)).arg(c.name())
insilmaril@721
  2500
		);	
insilmaril@721
  2501
		bo->setColorSubtree (c); // color links, color children
insilmaril@721
  2502
	}
insilmaril@721
  2503
}
insilmaril@721
  2504
insilmaril@721
  2505
QColor VymModel::getCurrentHeadingColor()
insilmaril@721
  2506
{
insilmaril@735
  2507
	BranchObj *bo=getSelectedBranch();
insilmaril@721
  2508
	if (bo) return bo->getColor(); 
insilmaril@721
  2509
	
insilmaril@721
  2510
	QMessageBox::warning(0,"Warning","Can't get color of heading,\nthere's no branch selected");
insilmaril@721
  2511
	return Qt::black;
insilmaril@721
  2512
}
insilmaril@721
  2513
insilmaril@721
  2514
insilmaril@721
  2515
insilmaril@721
  2516
void VymModel::editURL()
insilmaril@721
  2517
{
insilmaril@735
  2518
	BranchObj *bo=getSelectedBranch();
insilmaril@721
  2519
	if (bo)
insilmaril@721
  2520
	{		
insilmaril@721
  2521
		bool ok;
insilmaril@721
  2522
		QString text = QInputDialog::getText(
insilmaril@721
  2523
				"VYM", tr("Enter URL:"), QLineEdit::Normal,
insilmaril@721
  2524
				bo->getURL(), &ok, NULL);
insilmaril@721
  2525
		if ( ok) 
insilmaril@721
  2526
			// user entered something and pressed OK
insilmaril@721
  2527
			setURL(text);
insilmaril@721
  2528
	}
insilmaril@721
  2529
}
insilmaril@721
  2530
insilmaril@721
  2531
void VymModel::editLocalURL()
insilmaril@721
  2532
{
insilmaril@735
  2533
	BranchObj *bo=getSelectedBranch();
insilmaril@721
  2534
	if (bo)
insilmaril@721
  2535
	{		
insilmaril@721
  2536
		QStringList filters;
insilmaril@721
  2537
		filters <<"All files (*)";
insilmaril@721
  2538
		filters << tr("Text","Filedialog") + " (*.txt)";
insilmaril@721
  2539
		filters << tr("Spreadsheet","Filedialog") + " (*.odp,*.sxc)";
insilmaril@721
  2540
		filters << tr("Textdocument","Filedialog") +" (*.odw,*.sxw)";
insilmaril@721
  2541
		filters << tr("Images","Filedialog") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)";
insilmaril@721
  2542
		QFileDialog *fd=new QFileDialog( NULL,vymName+" - " +tr("Set URL to a local file"));
insilmaril@721
  2543
		fd->setFilters (filters);
insilmaril@721
  2544
		fd->setCaption(vymName+" - " +tr("Set URL to a local file"));
insilmaril@721
  2545
		fd->setDirectory (lastFileDir);
insilmaril@721
  2546
		if (! bo->getVymLink().isEmpty() )
insilmaril@721
  2547
			fd->selectFile( bo->getURL() );
insilmaril@721
  2548
		fd->show();
insilmaril@721
  2549
insilmaril@721
  2550
		if ( fd->exec() == QDialog::Accepted )
insilmaril@721
  2551
		{
insilmaril@721
  2552
			lastFileDir=QDir (fd->directory().path());
insilmaril@721
  2553
			setURL (fd->selectedFile() );
insilmaril@721
  2554
		}
insilmaril@721
  2555
	}
insilmaril@721
  2556
}
insilmaril@721
  2557
insilmaril@721
  2558
insilmaril@721
  2559
void VymModel::editHeading2URL()
insilmaril@721
  2560
{
insilmaril@735
  2561
	BranchObj *bo=getSelectedBranch();
insilmaril@721
  2562
	if (bo)
insilmaril@721
  2563
		setURL (bo->getHeading());
insilmaril@721
  2564
}	
insilmaril@721
  2565
insilmaril@721
  2566
void VymModel::editBugzilla2URL()
insilmaril@721
  2567
{
insilmaril@735
  2568
	BranchObj *bo=getSelectedBranch();
insilmaril@721
  2569
	if (bo)
insilmaril@721
  2570
	{		
insilmaril@721
  2571
		QString url= "https://bugzilla.novell.com/show_bug.cgi?id="+bo->getHeading();
insilmaril@721
  2572
		setURL (url);
insilmaril@721
  2573
	}
insilmaril@721
  2574
}	
insilmaril@721
  2575
insilmaril@721
  2576
void VymModel::editFATE2URL()
insilmaril@721
  2577
{
insilmaril@735
  2578
	BranchObj *bo=getSelectedBranch();
insilmaril@721
  2579
	if (bo)
insilmaril@721
  2580
	{		
insilmaril@721
  2581
		QString url= "http://keeper.suse.de:8080/webfate/match/id?value=ID"+bo->getHeading();
insilmaril@721
  2582
		saveState(
insilmaril@721
  2583
			bo,
insilmaril@721
  2584
			"setURL (\""+bo->getURL()+"\")",
insilmaril@721
  2585
			bo,
insilmaril@721
  2586
			"setURL (\""+url+"\")",
insilmaril@721
  2587
			QString("Use heading of %1 as link to FATE").arg(getObjectName(bo))
insilmaril@721
  2588
		);	
insilmaril@721
  2589
		bo->setURL (url);
insilmaril@721
  2590
		updateActions();
insilmaril@721
  2591
	}
insilmaril@721
  2592
}	
insilmaril@721
  2593
insilmaril@721
  2594
void VymModel::editVymLink()
insilmaril@721
  2595
{
insilmaril@735
  2596
	BranchObj *bo=getSelectedBranch();
insilmaril@721
  2597
	if (bo)
insilmaril@721
  2598
	{		
insilmaril@721
  2599
		QStringList filters;
insilmaril@721
  2600
		filters <<"VYM map (*.vym)";
insilmaril@721
  2601
		QFileDialog *fd=new QFileDialog( NULL,vymName+" - " +tr("Link to another map"));
insilmaril@721
  2602
		fd->setFilters (filters);
insilmaril@721
  2603
		fd->setCaption(vymName+" - " +tr("Link to another map"));
insilmaril@721
  2604
		fd->setDirectory (lastFileDir);
insilmaril@721
  2605
		if (! bo->getVymLink().isEmpty() )
insilmaril@721
  2606
			fd->selectFile( bo->getVymLink() );
insilmaril@721
  2607
		fd->show();
insilmaril@721
  2608
insilmaril@721
  2609
		QString fn;
insilmaril@721
  2610
		if ( fd->exec() == QDialog::Accepted )
insilmaril@721
  2611
		{
insilmaril@721
  2612
			lastFileDir=QDir (fd->directory().path());
insilmaril@721
  2613
			saveState(
insilmaril@721
  2614
				bo,
insilmaril@721
  2615
				"setVymLink (\""+bo->getVymLink()+"\")",
insilmaril@721
  2616
				bo,
insilmaril@721
  2617
				"setVymLink (\""+fd->selectedFile()+"\")",
insilmaril@721
  2618
				QString("Set vymlink of %1 to %2").arg(getObjectName(bo)).arg(fd->selectedFile())
insilmaril@721
  2619
			);	
insilmaril@746
  2620
			setVymLink (fd->selectedFile() );	// FIXME-2 ok?
insilmaril@721
  2621
		}
insilmaril@721
  2622
	}
insilmaril@721
  2623
}
insilmaril@721
  2624
insilmaril@721
  2625
void VymModel::setVymLink (const QString &s)
insilmaril@721
  2626
{
insilmaril@721
  2627
	// Internal function, no saveState needed
insilmaril@735
  2628
	BranchObj *bo=getSelectedBranch();
insilmaril@721
  2629
	if (bo)
insilmaril@721
  2630
	{
insilmaril@721
  2631
		bo->setVymLink(s);
insilmaril@721
  2632
		reposition();
insilmaril@721
  2633
		updateActions();
insilmaril@721
  2634
		selection.update();
insilmaril@721
  2635
		ensureSelectionVisible();
insilmaril@721
  2636
	}
insilmaril@721
  2637
}
insilmaril@721
  2638
insilmaril@721
  2639
void VymModel::deleteVymLink()
insilmaril@721
  2640
{
insilmaril@735
  2641
	BranchObj *bo=getSelectedBranch();
insilmaril@721
  2642
	if (bo)
insilmaril@721
  2643
	{		
insilmaril@721
  2644
		saveState(
insilmaril@721
  2645
			bo,
insilmaril@721
  2646
			"setVymLink (\""+bo->getVymLink()+"\")",
insilmaril@721
  2647
			bo,
insilmaril@721
  2648
			"setVymLink (\"\")",
insilmaril@721
  2649
			QString("Unset vymlink of %1").arg(getObjectName(bo))
insilmaril@721
  2650
		);	
insilmaril@721
  2651
		bo->setVymLink ("" );
insilmaril@721
  2652
		updateActions();
insilmaril@721
  2653
		reposition();
insilmaril@746
  2654
		// FIXME-3 VM needed? scene()->update();
insilmaril@721
  2655
	}
insilmaril@721
  2656
}
insilmaril@721
  2657
insilmaril@721
  2658
QString VymModel::getVymLink()
insilmaril@721
  2659
{
insilmaril@735
  2660
	BranchObj *bo=getSelectedBranch();
insilmaril@721
  2661
	if (bo)
insilmaril@721
  2662
		return bo->getVymLink();
insilmaril@721
  2663
	else	
insilmaril@721
  2664
		return "";
insilmaril@721
  2665
	
insilmaril@721
  2666
}
insilmaril@721
  2667
insilmaril@746
  2668
QStringList VymModel::getVymLinks()	// FIXME-1	first, next moved to vymmodel
insilmaril@746
  2669
{
insilmaril@746
  2670
	return QStringList();
insilmaril@746
  2671
/*
insilmaril@721
  2672
	QStringList links;
insilmaril@735
  2673
	BranchObj *bo=getSelectedBranch();
insilmaril@721
  2674
	if (bo)
insilmaril@721
  2675
	{		
insilmaril@721
  2676
		bo=bo->first();	
insilmaril@721
  2677
		while (bo) 
insilmaril@721
  2678
		{
insilmaril@721
  2679
			if (!bo->getVymLink().isEmpty()) links.append( bo->getVymLink());
insilmaril@721
  2680
			bo=bo->next();
insilmaril@721
  2681
		}	
insilmaril@721
  2682
	}	
insilmaril@721
  2683
	return links;
insilmaril@746
  2684
*/	
insilmaril@721
  2685
}
insilmaril@721
  2686
insilmaril@721
  2687
insilmaril@721
  2688
void VymModel::followXLink(int i)
insilmaril@721
  2689
{
insilmaril@735
  2690
	BranchObj *bo=getSelectedBranch();
insilmaril@721
  2691
	if (bo)
insilmaril@721
  2692
	{
insilmaril@721
  2693
		bo=bo->XLinkTargetAt(i);
insilmaril@721
  2694
		if (bo) 
insilmaril@721
  2695
		{
insilmaril@721
  2696
			selection.select(bo);
insilmaril@721
  2697
			ensureSelectionVisible();
insilmaril@721
  2698
		}
insilmaril@721
  2699
	}
insilmaril@721
  2700
}
insilmaril@721
  2701
insilmaril@746
  2702
void VymModel::editXLink(int i)	// FIXME-1 VM missing saveState
insilmaril@721
  2703
{
insilmaril@735
  2704
	BranchObj *bo=getSelectedBranch();
insilmaril@721
  2705
	if (bo)
insilmaril@721
  2706
	{
insilmaril@721
  2707
		XLinkObj *xlo=bo->XLinkAt(i);
insilmaril@721
  2708
		if (xlo) 
insilmaril@721
  2709
		{
insilmaril@721
  2710
			EditXLinkDialog dia;
insilmaril@721
  2711
			dia.setXLink (xlo);
insilmaril@721
  2712
			dia.setSelection(bo);
insilmaril@721
  2713
			if (dia.exec() == QDialog::Accepted)
insilmaril@721
  2714
			{
insilmaril@721
  2715
				if (dia.useSettingsGlobal() )
insilmaril@721
  2716
				{
insilmaril@721
  2717
					setMapDefXLinkColor (xlo->getColor() );
insilmaril@721
  2718
					setMapDefXLinkWidth (xlo->getWidth() );
insilmaril@721
  2719
				}
insilmaril@721
  2720
				if (dia.deleteXLink())
insilmaril@721
  2721
					bo->deleteXLinkAt(i);
insilmaril@721
  2722
			}
insilmaril@721
  2723
		}	
insilmaril@721
  2724
	}
insilmaril@721
  2725
}
insilmaril@721
  2726
insilmaril@721
  2727
insilmaril@721
  2728
insilmaril@721
  2729
insilmaril@721
  2730
insilmaril@721
  2731
//////////////////////////////////////////////
insilmaril@721
  2732
// Scripting
insilmaril@721
  2733
//////////////////////////////////////////////
insilmaril@721
  2734
insilmaril@721
  2735
void VymModel::parseAtom(const QString &atom)
insilmaril@721
  2736
{
insilmaril@735
  2737
	BranchObj *selb=getSelectedBranch();
insilmaril@749
  2738
	BranchItem *bi=getSelectedBranchItem();
insilmaril@721
  2739
	QString s,t;
insilmaril@721
  2740
	double x,y;
insilmaril@721
  2741
	int n;
insilmaril@721
  2742
	bool b,ok;
insilmaril@721
  2743
insilmaril@721
  2744
	// Split string s into command and parameters
insilmaril@721
  2745
	parser.parseAtom (atom);
insilmaril@721
  2746
	QString com=parser.getCommand();
insilmaril@721
  2747
	
insilmaril@721
  2748
	// External commands
insilmaril@721
  2749
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  2750
	if (com=="addBranch")
insilmaril@721
  2751
	{
insilmaril@721
  2752
		if (selection.isEmpty())
insilmaril@721
  2753
		{
insilmaril@721
  2754
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  2755
		} else if (! selb )
insilmaril@721
  2756
		{				  
insilmaril@721
  2757
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  2758
		} else 
insilmaril@721
  2759
		{	
insilmaril@721
  2760
			QList <int> pl;
insilmaril@721
  2761
			pl << 0 <<1;
insilmaril@721
  2762
			if (parser.checkParCount(pl))
insilmaril@721
  2763
			{
insilmaril@721
  2764
				if (parser.parCount()==0)
insilmaril@721
  2765
					addNewBranch (0);
insilmaril@721
  2766
				else
insilmaril@721
  2767
				{
insilmaril@721
  2768
					n=parser.parInt (ok,0);
insilmaril@721
  2769
					if (ok ) addNewBranch (n);
insilmaril@721
  2770
				}
insilmaril@721
  2771
			}
insilmaril@721
  2772
		}
insilmaril@721
  2773
	/////////////////////////////////////////////////////////////////////
insilmaril@721
  2774
	} else if (com=="addBranchBefore")
insilmaril@721
  2775
	{
insilmaril@721
  2776
		if (selection.isEmpty())
insilmaril@721
  2777
		{
insilmaril@721
  2778
			parser.setError (Aborted,"Nothing selected");
insilmaril@721
  2779
		} else if (! selb )
insilmaril@721
  2780
		{				  
insilmaril@721
  2781
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@721
  2782
		} else