mapeditor.cpp
author insilmaril
Tue, 23 Jan 2007 11:50:53 +0000
changeset 421 5522d1da7e37
parent 420 b7447adddc9a
child 424 d886c77ac0fe
permissions -rw-r--r--
1.8.65 Various fixes
insilmaril@0
     1
#include "mapeditor.h"
insilmaril@0
     2
insilmaril@374
     3
#include <q3filedialog.h>
insilmaril@0
     4
insilmaril@0
     5
#include <iostream>
insilmaril@95
     6
#include <cstdlib>
insilmaril@0
     7
#include <typeinfo>
insilmaril@0
     8
insilmaril@0
     9
#include "version.h"
insilmaril@0
    10
insilmaril@105
    11
#include "api.h"
insilmaril@175
    12
#include "editxlinkdialog.h"
insilmaril@175
    13
#include "exports.h"
insilmaril@175
    14
#include "extrainfodialog.h"
insilmaril@236
    15
#include "file.h"
insilmaril@175
    16
#include "linkablemapobj.h"
insilmaril@175
    17
#include "mainwindow.h"
insilmaril@175
    18
#include "misc.h"
insilmaril@175
    19
#include "texteditor.h"
insilmaril@314
    20
#include "warningdialog.h"
insilmaril@0
    21
#include "xml.h"
insilmaril@0
    22
insilmaril@0
    23
insilmaril@0
    24
extern TextEditor *textEditor;
insilmaril@0
    25
extern int statusbarTime;
insilmaril@0
    26
extern Main *mainWindow;
insilmaril@164
    27
extern QString tmpVymDir;
insilmaril@166
    28
extern QString clipboardDir;
insilmaril@166
    29
extern bool clipboardEmpty;
insilmaril@0
    30
extern FlagRowObj *standardFlagsDefault;
insilmaril@0
    31
insilmaril@366
    32
extern QMenu* branchContextMenu;
insilmaril@366
    33
extern QMenu* branchAddContextMenu;
insilmaril@366
    34
extern QMenu* branchRemoveContextMenu;
insilmaril@366
    35
extern QMenu* branchLinksContextMenu;
insilmaril@366
    36
extern QMenu* branchXLinksContextMenuEdit;
insilmaril@366
    37
extern QMenu* branchXLinksContextMenuFollow;
insilmaril@366
    38
extern QMenu* floatimageContextMenu;
insilmaril@366
    39
extern QMenu* canvasContextMenu;
insilmaril@366
    40
insilmaril@0
    41
insilmaril@0
    42
extern Settings settings;
insilmaril@366
    43
extern ImageIO imageIO;
insilmaril@0
    44
insilmaril@404
    45
extern QString vymName;
insilmaril@404
    46
extern QString vymVersion;
insilmaril@404
    47
insilmaril@269
    48
extern QString iconPath;
insilmaril@301
    49
extern QDir vymBaseDir;
insilmaril@366
    50
extern QDir lastImageDir;
insilmaril@421
    51
extern QDir lastFileDir;
insilmaril@269
    52
insilmaril@164
    53
int MapEditor::mapNum=0;	// make instance
insilmaril@0
    54
insilmaril@0
    55
///////////////////////////////////////////////////////////////////////
insilmaril@0
    56
///////////////////////////////////////////////////////////////////////
insilmaril@408
    57
MapEditor::MapEditor( QWidget* parent) :
insilmaril@421
    58
  QGraphicsView(parent)
insilmaril@0
    59
{
insilmaril@0
    60
	//cout << "Constructor ME "<<this<<endl;
insilmaril@164
    61
	mapNum++;
insilmaril@0
    62
insilmaril@119
    63
insilmaril@408
    64
	mapScene= new QGraphicsScene(parent);
insilmaril@408
    65
	//mapScene= new QGraphicsScene(QRectF(0,0,width(),height()), parent);
insilmaril@408
    66
	mapScene->setBackgroundBrush (QBrush(Qt::white, Qt::SolidPattern));
insilmaril@408
    67
insilmaril@408
    68
insilmaril@408
    69
    setScene (mapScene);
insilmaril@0
    70
	
insilmaril@408
    71
    mapCenter = new MapCenterObj(mapScene);
insilmaril@0
    72
    mapCenter->setVisibility (true);
insilmaril@0
    73
	mapCenter->setMapEditor (this);
insilmaril@93
    74
	mapCenter->setHeading (tr("New Map","Heading of mapcenter in new map"));
insilmaril@408
    75
	//mapCenter->move(mapScene->width()/2-mapCenter->width()/2,mapScene->height()/2-mapCenter->height()/2);
insilmaril@408
    76
	mapCenter->move(0,0);
insilmaril@0
    77
insilmaril@0
    78
    printer=NULL;
insilmaril@0
    79
insilmaril@97
    80
	defLinkColor=QColor (0,0,255);
insilmaril@97
    81
	defXLinkColor=QColor (180,180,180);
insilmaril@0
    82
	linkcolorhint=DefaultColor;
insilmaril@0
    83
	linkstyle=StylePolyParabel;
insilmaril@0
    84
insilmaril@366
    85
	// Create bitmap cursors, platform dependant
insilmaril@404
    86
	HandOpenCursor=QCursor (QPixmap(iconPath+"cursorhandopen.png"),1,1);		
insilmaril@404
    87
	PickColorCursor=QCursor ( QPixmap(iconPath+"cursorcolorpicker.png"), 5,27 ); 
insilmaril@404
    88
	CopyCursor=QCursor ( QPixmap(iconPath+"cursorcopy.png"), 1,1 ); 
insilmaril@404
    89
	XLinkCursor=QCursor ( QPixmap(iconPath+"cursorxlink.png"), 1,7 ); 
insilmaril@398
    90
insilmaril@398
    91
	setFocusPolicy (Qt::StrongFocus);
insilmaril@0
    92
insilmaril@0
    93
	pickingColor=false;
insilmaril@91
    94
	drawingLink=false;
insilmaril@93
    95
	copyingObj=false;
insilmaril@0
    96
insilmaril@0
    97
    editingBO=NULL;
insilmaril@0
    98
    selection=NULL;
insilmaril@0
    99
    selectionLast=NULL;
insilmaril@0
   100
    movingObj=NULL;
insilmaril@0
   101
insilmaril@366
   102
	xelection.setMapCenter (mapCenter);
insilmaril@421
   103
	xelection.clear();
insilmaril@366
   104
insilmaril@102
   105
	defXLinkWidth=1;
insilmaril@102
   106
	defXLinkColor=QColor (230,230,230);
insilmaril@102
   107
insilmaril@0
   108
    mapChanged=false;
insilmaril@0
   109
	mapDefault=true;
insilmaril@0
   110
	mapUnsaved=false;
insilmaril@0
   111
	
insilmaril@0
   112
	zipped=true;
insilmaril@0
   113
	filePath="";
insilmaril@294
   114
	fileName=tr("unnamed");
insilmaril@0
   115
	mapName="";
insilmaril@0
   116
insilmaril@398
   117
	stepsTotal=settings.readNumEntry("/mapeditor/stepsTotal",100);
insilmaril@381
   118
	undoSet.setEntry ("/history/stepsTotal",QString::number(stepsTotal));
insilmaril@163
   119
	
insilmaril@0
   120
	// Initialize find routine
insilmaril@0
   121
	itFind=NULL;				
insilmaril@0
   122
	EOFind=false;
insilmaril@0
   123
insilmaril@0
   124
	printFrame=true;
insilmaril@0
   125
	printFooter=true;
insilmaril@0
   126
insilmaril@139
   127
	blockReposition=false;
insilmaril@139
   128
	blockSaveState=false;
insilmaril@260
   129
insilmaril@260
   130
	hidemode=HideNone;
insilmaril@260
   131
insilmaril@377
   132
	// Create temporary files
insilmaril@377
   133
	makeTmpDirs();
insilmaril@0
   134
insilmaril@417
   135
	setAcceptDrops (true);	
insilmaril@0
   136
insilmaril@0
   137
	mapCenter->reposition();	//	for positioning heading
insilmaril@381
   138
insilmaril@0
   139
}
insilmaril@0
   140
insilmaril@0
   141
MapEditor::~MapEditor()
insilmaril@0
   142
{
insilmaril@0
   143
	//cout <<"Destructor MapEditor\n";
insilmaril@0
   144
}
insilmaril@0
   145
insilmaril@0
   146
MapCenterObj* MapEditor::getMapCenter()
insilmaril@0
   147
{
insilmaril@0
   148
    return mapCenter;
insilmaril@0
   149
}
insilmaril@0
   150
insilmaril@408
   151
QGraphicsScene * MapEditor::getScene()
insilmaril@0
   152
{
insilmaril@408
   153
    return mapScene;
insilmaril@0
   154
}
insilmaril@0
   155
insilmaril@139
   156
bool MapEditor::isRepositionBlocked()
insilmaril@0
   157
{
insilmaril@139
   158
	return blockReposition;
insilmaril@0
   159
}
insilmaril@0
   160
insilmaril@420
   161
QString MapEditor::getName (const LinkableMapObj *lmo)
insilmaril@294
   162
{
insilmaril@314
   163
	QString s;
insilmaril@314
   164
	if (!lmo) return QString("Error: NULL has no name!");
insilmaril@294
   165
insilmaril@294
   166
	if ((typeid(*lmo) == typeid(BranchObj) ||
insilmaril@294
   167
				      typeid(*lmo) == typeid(MapCenterObj))) 
insilmaril@314
   168
	{
insilmaril@314
   169
		
insilmaril@314
   170
		s=(((BranchObj*)lmo)->getHeading());
insilmaril@314
   171
		if (s=="") s="unnamed";
insilmaril@377
   172
		return QString("branch (%1)").arg(s);
insilmaril@377
   173
		//return QString("branch (<font color=\"#0000ff\">%1</font>)").arg(s);
insilmaril@314
   174
	}	
insilmaril@294
   175
	if ((typeid(*lmo) == typeid(FloatImageObj) ))
insilmaril@377
   176
		return QString ("floatimage [%1]").arg(((FloatImageObj*)lmo)->getOriginalFilename());
insilmaril@377
   177
		//return QString ("floatimage [<font color=\"#0000ff\">%1</font>]").arg(((FloatImageObj*)lmo)->getOriginalFilename());
insilmaril@294
   178
	return QString("Unknown type has no name!");
insilmaril@294
   179
}
insilmaril@294
   180
insilmaril@0
   181
void MapEditor::makeTmpDirs()
insilmaril@0
   182
{
insilmaril@0
   183
	// Create unique temporary directories
insilmaril@366
   184
	tmpMapDir=QDir::convertSeparators (tmpVymDir+QString("/mapeditor-%1").arg(mapNum));
insilmaril@366
   185
	histPath=QDir::convertSeparators (tmpMapDir+"/history");
insilmaril@164
   186
	QDir d;
insilmaril@377
   187
	d.mkdir (tmpMapDir);
insilmaril@0
   188
}
insilmaril@0
   189
insilmaril@408
   190
QString MapEditor::saveToDir(const QString &tmpdir, const QString &prefix, bool writeflags, const QPointF &offset, LinkableMapObj *saveSel)
insilmaril@0
   191
{
insilmaril@163
   192
	// tmpdir		temporary directory to which data will be written
insilmaril@0
   193
	// prefix		mapname, which will be appended to images etc.
insilmaril@0
   194
	// writeflags	Only write flags for "real" save of map, not undo
insilmaril@408
   195
	// offset		offset of bbox of whole map in scene. 
insilmaril@0
   196
	//				Needed for XML export
insilmaril@0
   197
	
insilmaril@0
   198
	// Save Header
insilmaril@0
   199
	QString ls;
insilmaril@0
   200
	switch (linkstyle)
insilmaril@0
   201
	{
insilmaril@0
   202
		case StyleLine: 
insilmaril@0
   203
			ls="StyleLine";
insilmaril@0
   204
			break;
insilmaril@0
   205
		case StyleParabel:
insilmaril@0
   206
			ls="StyleParabel";
insilmaril@0
   207
			break;
insilmaril@0
   208
		case StylePolyLine:	
insilmaril@0
   209
			ls="StylePolyLine";
insilmaril@0
   210
			break;
insilmaril@0
   211
		default:
insilmaril@0
   212
			ls="StylePolyParabel";
insilmaril@0
   213
			break;
insilmaril@0
   214
	}	
insilmaril@0
   215
insilmaril@0
   216
	QString s="<?xml version=\"1.0\" encoding=\"utf-8\"?><!DOCTYPE vymmap>\n";
insilmaril@0
   217
	QString colhint="";
insilmaril@0
   218
	if (linkcolorhint==HeadingColor) 
insilmaril@0
   219
		colhint=attribut("linkColorHint","HeadingColor");
insilmaril@0
   220
insilmaril@404
   221
	QString mapAttr=attribut("version",vymVersion);
insilmaril@398
   222
	if (!saveSel || saveSel==mapCenter)
insilmaril@0
   223
		mapAttr+= attribut("author",mapCenter->getAuthor()) +
insilmaril@0
   224
				  attribut("comment",mapCenter->getComment()) +
insilmaril@0
   225
			      attribut("date",mapCenter->getDate()) +
insilmaril@408
   226
		          attribut("backgroundColor", mapScene->backgroundBrush().color().name() ) +
insilmaril@0
   227
		          attribut("linkStyle", ls ) +
insilmaril@97
   228
		          attribut("linkColor", defLinkColor.name() ) +
insilmaril@102
   229
		          attribut("defXLinkColor", defXLinkColor.name() ) +
insilmaril@102
   230
		          attribut("defXLinkWidth", QString().setNum(defXLinkWidth,10) ) +
insilmaril@0
   231
		          colhint; 
insilmaril@0
   232
	s+=beginElement("vymmap",mapAttr);
insilmaril@0
   233
	incIndent();
insilmaril@0
   234
insilmaril@0
   235
	// Find the used flags while traversing the tree
insilmaril@0
   236
	standardFlagsDefault->resetUsedCounter();
insilmaril@0
   237
	
insilmaril@166
   238
	// Reset the counters before saving
insilmaril@406
   239
	// TODO constr. of FIO creates lots of objects, better do this in some other way...
insilmaril@408
   240
	FloatImageObj (mapScene).resetSaveCounter();
insilmaril@166
   241
insilmaril@0
   242
	// Build xml recursivly
insilmaril@398
   243
	if (!saveSel || typeid (*saveSel) == typeid (MapCenterObj))
insilmaril@366
   244
		// Save complete map, if saveSel not set
insilmaril@0
   245
		s+=mapCenter->saveToDir(tmpdir,prefix,writeflags,offset);
insilmaril@0
   246
	else
insilmaril@0
   247
	{
insilmaril@366
   248
		if ( typeid(*saveSel) == typeid(BranchObj) )
insilmaril@366
   249
			// Save Subtree
insilmaril@366
   250
			s+=((BranchObj*)(saveSel))->saveToDir(tmpdir,prefix,offset);
insilmaril@366
   251
		else if ( typeid(*saveSel) == typeid(FloatImageObj) )
insilmaril@366
   252
			// Save image
insilmaril@377
   253
			s+=((FloatImageObj*)(saveSel))->saveToDir(tmpdir,prefix);
insilmaril@0
   254
	}
insilmaril@0
   255
insilmaril@0
   256
	// Save local settings
insilmaril@0
   257
	s+=settings.getXMLData (destPath);
insilmaril@0
   258
insilmaril@0
   259
	// Save selection
insilmaril@366
   260
	if (selection && !saveSel ) 
insilmaril@0
   261
		s+=valueElement("select",selection->getSelectString());
insilmaril@0
   262
insilmaril@0
   263
	decIndent();
insilmaril@0
   264
	s+=endElement("vymmap");
insilmaril@0
   265
insilmaril@0
   266
	if (writeflags)
insilmaril@0
   267
		standardFlagsDefault->saveToDir (tmpdir+"/flags/","",writeflags);
insilmaril@0
   268
	return s;
insilmaril@0
   269
}
insilmaril@0
   270
insilmaril@395
   271
void MapEditor::saveStateChangingPart(LinkableMapObj *undoSel, LinkableMapObj* redoSel, const QString &rc, const QString &comment)
insilmaril@0
   272
{
insilmaril@366
   273
	// save the selected part of the map, Undo will replace part of map 
insilmaril@366
   274
	QString undoSelection="";
insilmaril@395
   275
	if (undoSel)
insilmaril@395
   276
		undoSelection=undoSel->getSelectString();
insilmaril@395
   277
	else
insilmaril@395
   278
		qWarning ("MapEditor::saveStateChangingPart  no undoSel given!");
insilmaril@395
   279
	QString redoSelection="";
insilmaril@395
   280
	if (redoSel)
insilmaril@395
   281
		redoSelection=undoSel->getSelectString();
insilmaril@395
   282
	else
insilmaril@395
   283
		qWarning ("MapEditor::saveStateChangingPart  no redoSel given!");
insilmaril@395
   284
		
insilmaril@366
   285
insilmaril@366
   286
	saveState (PartOfMap,
insilmaril@398
   287
		undoSelection, "addMapReplace (\"PATH\")",
insilmaril@395
   288
		redoSelection, rc, 
insilmaril@366
   289
		comment, 
insilmaril@366
   290
		undoSel);
insilmaril@105
   291
}
insilmaril@105
   292
insilmaril@393
   293
void MapEditor::saveStateRemovingPart(LinkableMapObj *redoSel, const QString &comment)
insilmaril@393
   294
{
insilmaril@394
   295
	if (!redoSel)
insilmaril@393
   296
	{
insilmaril@394
   297
		qWarning ("MapEditor::saveStateRemovingPart  no redoSel given!");
insilmaril@393
   298
		return;
insilmaril@393
   299
	}
insilmaril@393
   300
	QString undoSelection=redoSel->getParObj()->getSelectString();
insilmaril@393
   301
	QString redoSelection=redoSel->getSelectString();
insilmaril@398
   302
	if (typeid(*redoSel) == typeid(BranchObj)  ) 
insilmaril@394
   303
	{
insilmaril@394
   304
		// save the selected branch of the map, Undo will insert part of map 
insilmaril@394
   305
		saveState (PartOfMap,
insilmaril@394
   306
			undoSelection, QString("addMapInsert (\"PATH\",%1)").arg(((BranchObj*)redoSel)->getNum()),
insilmaril@394
   307
			redoSelection, "delete ()", 
insilmaril@394
   308
			comment, 
insilmaril@394
   309
			redoSel);
insilmaril@394
   310
	}
insilmaril@393
   311
}
insilmaril@393
   312
insilmaril@106
   313
insilmaril@366
   314
void MapEditor::saveState(LinkableMapObj *undoSel, const QString &uc, LinkableMapObj *redoSel, const QString &rc, const QString &comment) 
insilmaril@366
   315
{
insilmaril@366
   316
	// "Normal" savestate: save commands, selections and comment
insilmaril@366
   317
	// so just save commands for undo and redo
insilmaril@366
   318
	// and use current selection
insilmaril@366
   319
insilmaril@366
   320
	QString redoSelection="";
insilmaril@366
   321
	if (redoSel) redoSelection=redoSel->getSelectString();
insilmaril@366
   322
	QString undoSelection="";
insilmaril@366
   323
	if (undoSel) undoSelection=undoSel->getSelectString();
insilmaril@366
   324
insilmaril@366
   325
	saveState (UndoCommand,
insilmaril@366
   326
		undoSelection, uc,
insilmaril@366
   327
		redoSelection, rc, 
insilmaril@366
   328
		comment, 
insilmaril@366
   329
		NULL);
insilmaril@366
   330
}
insilmaril@366
   331
insilmaril@366
   332
void MapEditor::saveState(const QString &undoSel, const QString &uc, const QString &redoSel, const QString &rc, const QString &comment) 
insilmaril@366
   333
{
insilmaril@366
   334
	// "Normal" savestate: save commands, selections and comment
insilmaril@366
   335
	// so just save commands for undo and redo
insilmaril@366
   336
	// and use current selection
insilmaril@366
   337
	saveState (UndoCommand,
insilmaril@366
   338
		undoSel, uc,
insilmaril@366
   339
		redoSel, rc, 
insilmaril@366
   340
		comment, 
insilmaril@366
   341
		NULL);
insilmaril@366
   342
}
insilmaril@366
   343
insilmaril@366
   344
		
insilmaril@366
   345
void MapEditor::saveState(const SaveMode &savemode, const QString &undoSelection, const QString &undoCom, const QString &redoSelection, const QString &redoCom, const QString &comment, LinkableMapObj *saveSel)
insilmaril@163
   346
{
insilmaril@163
   347
	// Main saveState
insilmaril@163
   348
insilmaril@139
   349
	if (blockSaveState) return;
insilmaril@139
   350
insilmaril@163
   351
	/* TODO remove after testing
insilmaril@163
   352
	*/
insilmaril@388
   353
	cout << "ME::saveState()  "<<endl;
insilmaril@294
   354
	
insilmaril@366
   355
	int undosAvail=undoSet.readNumEntry ("/history/undosAvail",0);
insilmaril@381
   356
	int redosAvail=undoSet.readNumEntry ("/history/redosAvail",0);
insilmaril@366
   357
	int curStep=undoSet.readNumEntry ("/history/curStep",0);
insilmaril@163
   358
	// Find out current undo directory
insilmaril@381
   359
	if (undosAvail<stepsTotal) undosAvail++;
insilmaril@366
   360
	curStep++;
insilmaril@381
   361
	if (curStep>stepsTotal) curStep=1;
insilmaril@163
   362
	
insilmaril@398
   363
	QString backupXML="";
insilmaril@388
   364
	QString bakMapName=QDir::convertSeparators (QString("history-%1").arg(curStep));
insilmaril@388
   365
	QString bakMapDir=QDir::convertSeparators (tmpMapDir +"/"+bakMapName);
insilmaril@164
   366
	QString bakMapPath=QDir::convertSeparators(bakMapDir+"/map.xml");
insilmaril@164
   367
insilmaril@164
   368
	// Create bakMapDir if not available
insilmaril@164
   369
	QDir d(bakMapDir);
insilmaril@164
   370
	if (!d.exists()) 
insilmaril@164
   371
		makeSubDirs (bakMapDir);
insilmaril@163
   372
insilmaril@366
   373
	// Save depending on how much needs to be saved	
insilmaril@398
   374
	if (saveSel)
insilmaril@408
   375
		backupXML=saveToDir (bakMapDir,mapName+"-",false, QPointF (),saveSel);
insilmaril@136
   376
		
insilmaril@163
   377
	QString undoCommand="";
insilmaril@106
   378
	if (savemode==UndoCommand)
insilmaril@0
   379
	{
insilmaril@105
   380
		undoCommand=undoCom;
insilmaril@106
   381
	}	
insilmaril@366
   382
	else if (savemode==PartOfMap )
insilmaril@0
   383
	{
insilmaril@393
   384
		undoCommand=undoCom;
insilmaril@393
   385
		undoCommand.replace ("PATH",bakMapPath);
insilmaril@106
   386
	}
insilmaril@398
   387
insilmaril@163
   388
	if (!backupXML.isEmpty())
insilmaril@163
   389
		// Write XML Data to disk
insilmaril@163
   390
		saveStringToDisk (QString(bakMapPath),backupXML);
insilmaril@163
   391
insilmaril@388
   392
	// We would have to save all actions in a tree, to keep track of 
insilmaril@393
   393
	// possible redos after a action. Possible, but we are too lazy: forget about redos.
insilmaril@388
   394
	redosAvail=0;
insilmaril@388
   395
insilmaril@388
   396
	// Write the current state to disk
insilmaril@366
   397
	undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
insilmaril@388
   398
	undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
insilmaril@366
   399
	undoSet.setEntry ("/history/curStep",QString::number(curStep));
insilmaril@366
   400
	undoSet.setEntry (QString("/history/step-%1/undoCommand").arg(curStep),undoCommand);
insilmaril@366
   401
	undoSet.setEntry (QString("/history/step-%1/undoSelection").arg(curStep),undoSelection);
insilmaril@366
   402
	undoSet.setEntry (QString("/history/step-%1/redoCommand").arg(curStep),redoCom);
insilmaril@366
   403
	undoSet.setEntry (QString("/history/step-%1/redoSelection").arg(curStep),redoSelection);
insilmaril@366
   404
	undoSet.setEntry (QString("/history/step-%1/comment").arg(curStep),comment);
insilmaril@404
   405
	undoSet.setEntry (QString("/history/version"),vymVersion);
insilmaril@366
   406
	undoSet.writeSettings(histPath);
insilmaril@163
   407
insilmaril@163
   408
	/* TODO remove after testing
insilmaril@366
   409
	*/
insilmaril@366
   410
	//cout << "          into="<< histPath.toStdString()<<endl;
insilmaril@381
   411
	cout << "    stepsTotal="<<stepsTotal<<
insilmaril@366
   412
	", undosAvail="<<undosAvail<<
insilmaril@381
   413
	", redosAvail="<<redosAvail<<
insilmaril@366
   414
	", curStep="<<curStep<<endl;
insilmaril@163
   415
	cout << "    ---------------------------"<<endl;
insilmaril@366
   416
	cout << "    comment="<<comment.toStdString()<<endl;
insilmaril@366
   417
	cout << "    undoCom="<<undoCommand.toStdString()<<endl;
insilmaril@366
   418
	cout << "    undoSel="<<undoSelection.toStdString()<<endl;
insilmaril@366
   419
	cout << "    redoCom="<<redoCom.toStdString()<<endl;
insilmaril@366
   420
	cout << "    redoSel="<<redoSelection.toStdString()<<endl;
insilmaril@398
   421
	if (saveSel) cout << "    saveSel="<<saveSel->getSelectString().ascii()<<endl;
insilmaril@294
   422
	cout << "    ---------------------------"<<endl;
insilmaril@377
   423
insilmaril@408
   424
	mainWindow->updateHistory (undoSet);
insilmaril@407
   425
	setChanged();
insilmaril@395
   426
	updateActions();
insilmaril@106
   427
}
insilmaril@106
   428
insilmaril@129
   429
void MapEditor::parseAtom(const QString &atom)
insilmaril@106
   430
{
insilmaril@106
   431
	API api;
insilmaril@163
   432
	QString s,t;
insilmaril@129
   433
	int x,y;
insilmaril@395
   434
	bool b,ok;
insilmaril@129
   435
insilmaril@129
   436
	// Split string s into command and parameters
insilmaril@394
   437
	api.parseInput (atom);
insilmaril@129
   438
	QString com=api.command();
insilmaril@129
   439
	
insilmaril@106
   440
	// External commands
insilmaril@393
   441
	if (com=="addBranch")
insilmaril@366
   442
	{
insilmaril@394
   443
		if (!selection)
insilmaril@394
   444
		{
insilmaril@394
   445
			api.setError (Aborted,"Nothing selected");
insilmaril@394
   446
		} else if ( (typeid(*selection) != typeid(BranchObj) && 
insilmaril@394
   447
					 typeid(*selection) != typeid(MapCenterObj)) )
insilmaril@394
   448
		{				  
insilmaril@394
   449
			api.setError (Aborted,"Type of selection is not a branch");
insilmaril@394
   450
		} else 
insilmaril@366
   451
		{	
insilmaril@394
   452
			QList <int> pl;
insilmaril@394
   453
			pl << 0 <<1;
insilmaril@394
   454
			if (api.checkParamCount(pl))
insilmaril@394
   455
			{
insilmaril@394
   456
				if (api.paramCount()==0)
insilmaril@394
   457
					addNewBranchInt (-2);
insilmaril@394
   458
				else
insilmaril@394
   459
				{
insilmaril@394
   460
					y=api.parInt (ok,0);
insilmaril@394
   461
					if (ok ) addNewBranchInt (y);
insilmaril@394
   462
				}
insilmaril@394
   463
			}
insilmaril@394
   464
		}
insilmaril@394
   465
	} else if (com=="addBranchBefore")
insilmaril@394
   466
	{
insilmaril@394
   467
		if (!selection)
insilmaril@394
   468
		{
insilmaril@394
   469
			api.setError (Aborted,"Nothing selected");
insilmaril@394
   470
		} else if ( (typeid(*selection) != typeid(BranchObj) && 
insilmaril@394
   471
					 typeid(*selection) != typeid(MapCenterObj)) )
insilmaril@394
   472
		{				  
insilmaril@394
   473
			api.setError (Aborted,"Type of selection is not a branch");
insilmaril@394
   474
		} else 
insilmaril@394
   475
		{	
insilmaril@394
   476
			if (api.paramCount()==0)
insilmaril@394
   477
			{
insilmaril@394
   478
				addNewBranchBefore ();
insilmaril@394
   479
			}	
insilmaril@394
   480
		}
insilmaril@393
   481
	} else if (com==QString("addMapReplace"))
insilmaril@393
   482
	{
insilmaril@394
   483
		if (!selection)
insilmaril@394
   484
		{
insilmaril@394
   485
			api.setError (Aborted,"Nothing selected");
insilmaril@394
   486
		} else if ( (typeid(*selection) != typeid(BranchObj) && 
insilmaril@394
   487
					 typeid(*selection) != typeid(MapCenterObj)) )
insilmaril@394
   488
		{				  
insilmaril@394
   489
			api.setError (Aborted,"Type of selection is not a branch");
insilmaril@398
   490
		} else if (api.checkParamCount(1))
insilmaril@393
   491
		{
insilmaril@398
   492
			//s=api.parString (ok,0);	// selection
insilmaril@398
   493
			t=api.parString (ok,0);	// path to map
insilmaril@393
   494
			if (QDir::isRelativePath(t)) t=QDir::convertSeparators (tmpMapDir + "/"+t);
insilmaril@398
   495
			addMapReplaceInt(selection->getSelectString(),t);	
insilmaril@393
   496
		}
insilmaril@393
   497
	} else if (com==QString("addMapInsert"))
insilmaril@393
   498
	{
insilmaril@394
   499
		if (!selection)
insilmaril@393
   500
		{
insilmaril@394
   501
			api.setError (Aborted,"Nothing selected");
insilmaril@394
   502
		} else if ( (typeid(*selection) != typeid(BranchObj) && 
insilmaril@394
   503
					 typeid(*selection) != typeid(MapCenterObj)) )
insilmaril@394
   504
		{				  
insilmaril@394
   505
			api.setError (Aborted,"Type of selection is not a branch");
insilmaril@394
   506
		} else 
insilmaril@394
   507
		{	
insilmaril@394
   508
			if (api.checkParamCount(2))
insilmaril@394
   509
			{
insilmaril@394
   510
				t=api.parString (ok,0);	// path to map
insilmaril@394
   511
				y=api.parInt(ok,1);		// position
insilmaril@394
   512
				if (QDir::isRelativePath(t)) t=QDir::convertSeparators (tmpMapDir + "/"+t);
insilmaril@394
   513
				addMapInsertInt(t,y);	
insilmaril@394
   514
			}
insilmaril@393
   515
		}
insilmaril@395
   516
	} else if (com=="colorBranch")
insilmaril@395
   517
	{
insilmaril@395
   518
		if (!selection)
insilmaril@395
   519
		{
insilmaril@395
   520
			api.setError (Aborted,"Nothing selected");
insilmaril@395
   521
		} else if ( typeid(*selection) != typeid(BranchObj) && 
insilmaril@395
   522
					typeid(*selection) != typeid(MapCenterObj))
insilmaril@395
   523
		{				  
insilmaril@395
   524
			api.setError (Aborted,"Type of selection is not a branch");
insilmaril@395
   525
		} else if (api.checkParamCount(1))
insilmaril@395
   526
		{	
insilmaril@395
   527
			QColor c=api.parColor (ok,0);
insilmaril@395
   528
			if (ok) colorBranch (c);
insilmaril@395
   529
		}	
insilmaril@417
   530
	} else if (com=="colorSubtree")
insilmaril@417
   531
	{
insilmaril@417
   532
		if (!selection)
insilmaril@417
   533
		{
insilmaril@417
   534
			api.setError (Aborted,"Nothing selected");
insilmaril@417
   535
		} else if ( typeid(*selection) != typeid(BranchObj) && 
insilmaril@417
   536
					typeid(*selection) != typeid(MapCenterObj))
insilmaril@417
   537
		{				  
insilmaril@417
   538
			api.setError (Aborted,"Type of selection is not a branch");
insilmaril@417
   539
		} else if (api.checkParamCount(1))
insilmaril@417
   540
		{	
insilmaril@417
   541
			QColor c=api.parColor (ok,0);
insilmaril@417
   542
			if (ok) colorSubtree (c);
insilmaril@417
   543
		}	
insilmaril@395
   544
	} else if (com=="cut")
insilmaril@395
   545
	{
insilmaril@395
   546
		if (!selection)
insilmaril@395
   547
		{
insilmaril@395
   548
			api.setError (Aborted,"Nothing selected");
insilmaril@395
   549
		} else if ( typeid(*selection) != typeid(BranchObj) && 
insilmaril@395
   550
					typeid(*selection) != typeid(MapCenterObj) &&
insilmaril@395
   551
					typeid(*selection) != typeid(FloatImageObj) )
insilmaril@395
   552
		{				  
insilmaril@395
   553
			api.setError (Aborted,"Type of selection is not a branch or floatimage");
insilmaril@395
   554
		} else if (api.checkParamCount(0))
insilmaril@395
   555
		{	
insilmaril@395
   556
			cut();
insilmaril@395
   557
		}	
insilmaril@393
   558
	} else if (com=="delete")
insilmaril@393
   559
	{
insilmaril@394
   560
		if (!selection)
insilmaril@394
   561
		{
insilmaril@394
   562
			api.setError (Aborted,"Nothing selected");
insilmaril@394
   563
		} else if ( (typeid(*selection) != typeid(BranchObj) && 
insilmaril@394
   564
					 typeid(*selection) != typeid(MapCenterObj)) )
insilmaril@394
   565
		{				  
insilmaril@394
   566
			api.setError (Aborted,"Type of selection is not a branch");
insilmaril@394
   567
		} else if (api.checkParamCount(0))
insilmaril@393
   568
		{	
insilmaril@393
   569
			deleteSelection();
insilmaril@393
   570
		}	
insilmaril@394
   571
	} else if (com=="deleteKeepChilds")
insilmaril@394
   572
	{
insilmaril@394
   573
		if (!selection)
insilmaril@394
   574
		{
insilmaril@394
   575
			api.setError (Aborted,"Nothing selected");
insilmaril@394
   576
		} else if ( (typeid(*selection) != typeid(BranchObj) && 
insilmaril@394
   577
					 typeid(*selection) != typeid(MapCenterObj)) )
insilmaril@394
   578
		{				  
insilmaril@394
   579
			api.setError (Aborted,"Type of selection is not a branch");
insilmaril@394
   580
		} else if (api.checkParamCount(0))
insilmaril@394
   581
		{	
insilmaril@395
   582
			deleteKeepChilds();
insilmaril@395
   583
		}	
insilmaril@395
   584
	} else if (com=="deleteChilds")
insilmaril@395
   585
	{
insilmaril@395
   586
		if (!selection)
insilmaril@395
   587
		{
insilmaril@395
   588
			api.setError (Aborted,"Nothing selected");
insilmaril@395
   589
		} else if ( (typeid(*selection) != typeid(BranchObj) && 
insilmaril@395
   590
					 typeid(*selection) != typeid(MapCenterObj)) )
insilmaril@395
   591
		{				  
insilmaril@395
   592
			api.setError (Aborted,"Type of selection is not a branch");
insilmaril@395
   593
		} else if (api.checkParamCount(0))
insilmaril@395
   594
		{	
insilmaril@395
   595
			deleteChilds();
insilmaril@394
   596
		}	
insilmaril@420
   597
	} else if (com=="linkTo")
insilmaril@420
   598
	{
insilmaril@420
   599
		if (!selection)
insilmaril@420
   600
		{
insilmaril@420
   601
			api.setError (Aborted,"Nothing selected");
insilmaril@421
   602
		} else if ( (typeid(*selection) == typeid(BranchObj) || 
insilmaril@421
   603
					 typeid(*selection) == typeid(MapCenterObj)) )
insilmaril@420
   604
		{
insilmaril@421
   605
			if (api.checkParamCount(4))
insilmaril@421
   606
			{
insilmaril@421
   607
				// 0	selectstring of parent
insilmaril@421
   608
				// 1	num in parent (for branches)
insilmaril@421
   609
				// 2,3	x,y of mainbranch or mapcenter
insilmaril@421
   610
				s=api.parString(ok,0);
insilmaril@421
   611
				LinkableMapObj *dst=mapCenter->findObjBySelect (s);
insilmaril@421
   612
				if (dst)
insilmaril@421
   613
				{	
insilmaril@421
   614
					if (typeid(*dst) == typeid(BranchObj) ) 
insilmaril@421
   615
					{
insilmaril@421
   616
						// Get number in parent
insilmaril@421
   617
						x=api.parInt (ok,1);
insilmaril@421
   618
						if (ok)
insilmaril@421
   619
							((BranchObj*)selection)->linkTo ((BranchObj*)(dst),x);
insilmaril@421
   620
					} else if (typeid(*dst) == typeid(MapCenterObj) ) 
insilmaril@421
   621
					{
insilmaril@421
   622
						((BranchObj*)selection)->linkTo ((BranchObj*)(dst),-1);
insilmaril@421
   623
						// Get coordinates of mainbranch
insilmaril@421
   624
						x=api.parInt (ok,2);
insilmaril@421
   625
						if (ok)
insilmaril@421
   626
						{
insilmaril@421
   627
							y=api.parInt (ok,3);
insilmaril@421
   628
							if (ok) ((BranchObj*)selection)->move (x,y);
insilmaril@421
   629
						}
insilmaril@421
   630
					}	
insilmaril@421
   631
				}	
insilmaril@421
   632
			}	
insilmaril@421
   633
		} else if ( typeid(*selection) != typeid(FloatImageObj)) 
insilmaril@421
   634
		{
insilmaril@421
   635
			if (api.checkParamCount(1))
insilmaril@421
   636
			{
insilmaril@421
   637
				// 0	selectstring of parent
insilmaril@421
   638
				s=api.parString(ok,0);
insilmaril@421
   639
				LinkableMapObj *dst=mapCenter->findObjBySelect (s);
insilmaril@421
   640
				if (dst)
insilmaril@421
   641
				{	
insilmaril@421
   642
					if (typeid(*dst) == typeid(BranchObj) ||
insilmaril@421
   643
						typeid(*dst) == typeid(MapCenterObj)) 
insilmaril@421
   644
						linkTo (dst->getSelectString());
insilmaril@421
   645
				} else	
insilmaril@421
   646
					api.setError (Aborted,"Destination is not a branch");
insilmaril@421
   647
			}		
insilmaril@421
   648
		} else
insilmaril@421
   649
			api.setError (Aborted,"Type of selection is not a floatimage or branch");
insilmaril@393
   650
	} else if (com=="moveBranchUp")
insilmaril@393
   651
	{
insilmaril@394
   652
		if (!selection)
insilmaril@394
   653
		{
insilmaril@394
   654
			api.setError (Aborted,"Nothing selected");
insilmaril@394
   655
		} else if ( (typeid(*selection) != typeid(BranchObj) && 
insilmaril@394
   656
					 typeid(*selection) != typeid(MapCenterObj)) )
insilmaril@394
   657
		{				  
insilmaril@394
   658
			api.setError (Aborted,"Type of selection is not a branch");
insilmaril@394
   659
		} else if (api.checkParamCount(0))
insilmaril@394
   660
		{
insilmaril@394
   661
			moveBranchUp();
insilmaril@394
   662
		}	
insilmaril@394
   663
	} else if (com=="moveBranchDown")
insilmaril@394
   664
	{
insilmaril@394
   665
		if (!selection)
insilmaril@394
   666
		{
insilmaril@394
   667
			api.setError (Aborted,"Nothing selected");
insilmaril@394
   668
		} else if ( (typeid(*selection) != typeid(BranchObj) && 
insilmaril@394
   669
					 typeid(*selection) != typeid(MapCenterObj)) )
insilmaril@394
   670
		{				  
insilmaril@394
   671
			api.setError (Aborted,"Type of selection is not a branch");
insilmaril@394
   672
		} else if (api.checkParamCount(0))
insilmaril@394
   673
		{
insilmaril@394
   674
			moveBranchDown();
insilmaril@394
   675
		}	
insilmaril@394
   676
	} else if (com=="move")
insilmaril@394
   677
	{
insilmaril@394
   678
		if (!selection)
insilmaril@394
   679
		{
insilmaril@394
   680
			api.setError (Aborted,"Nothing selected");
insilmaril@394
   681
		} else if ( typeid(*selection) != typeid(BranchObj) && 
insilmaril@394
   682
					typeid(*selection) != typeid(MapCenterObj) &&
insilmaril@394
   683
					typeid(*selection) != typeid(FloatImageObj) )
insilmaril@394
   684
		{				  
insilmaril@394
   685
			api.setError (Aborted,"Type of selection is not a branch or floatimage");
insilmaril@394
   686
		} else if (api.checkParamCount(2))
insilmaril@393
   687
		{	
insilmaril@393
   688
			x=api.parInt (ok,0);
insilmaril@393
   689
			if (ok)
insilmaril@393
   690
			{
insilmaril@393
   691
				y=api.parInt (ok,1);
insilmaril@393
   692
				if (ok) move (x,y);
insilmaril@393
   693
			}
insilmaril@393
   694
		}	
insilmaril@395
   695
	} else if (com=="moveRel")
insilmaril@393
   696
	{
insilmaril@394
   697
		if (!selection)
insilmaril@394
   698
		{
insilmaril@394
   699
			api.setError (Aborted,"Nothing selected");
insilmaril@394
   700
		} else if ( typeid(*selection) != typeid(BranchObj) && 
insilmaril@394
   701
					typeid(*selection) != typeid(MapCenterObj) &&
insilmaril@394
   702
					typeid(*selection) != typeid(FloatImageObj) )
insilmaril@394
   703
		{				  
insilmaril@394
   704
			api.setError (Aborted,"Type of selection is not a branch or floatimage");
insilmaril@394
   705
		} else if (api.checkParamCount(2))
insilmaril@393
   706
		{	
insilmaril@393
   707
			x=api.parInt (ok,0);
insilmaril@393
   708
			if (ok)
insilmaril@393
   709
			{
insilmaril@393
   710
				y=api.parInt (ok,1);
insilmaril@393
   711
				if (ok) moveRel (x,y);
insilmaril@393
   712
			}
insilmaril@393
   713
		}	
insilmaril@395
   714
	} else if (com=="paste")
insilmaril@395
   715
	{
insilmaril@395
   716
		if (!selection)
insilmaril@395
   717
		{
insilmaril@395
   718
			api.setError (Aborted,"Nothing selected");
insilmaril@395
   719
		} else if ( typeid(*selection) != typeid(BranchObj) && 
insilmaril@395
   720
					typeid(*selection) != typeid(MapCenterObj) )
insilmaril@395
   721
		{				  
insilmaril@395
   722
			api.setError (Aborted,"Type of selection is not a branch");
insilmaril@395
   723
		} else if (api.checkParamCount(0))
insilmaril@395
   724
		{	
insilmaril@395
   725
			paste();
insilmaril@395
   726
		}	
insilmaril@398
   727
	} else if (com=="select")
insilmaril@398
   728
	{
insilmaril@398
   729
		if (api.checkParamCount(1))
insilmaril@398
   730
		{
insilmaril@398
   731
			s=api.parString(ok,0);
insilmaril@398
   732
			if (ok) select (s);
insilmaril@398
   733
		}	
insilmaril@398
   734
	} else if (com=="setMapAuthor")
insilmaril@398
   735
	{
insilmaril@398
   736
		if (api.checkParamCount(1))
insilmaril@398
   737
		{
insilmaril@398
   738
			s=api.parString(ok,0);
insilmaril@398
   739
			if (ok) setMapAuthor (s);
insilmaril@398
   740
		}	
insilmaril@398
   741
	} else if (com=="setMapComment")
insilmaril@398
   742
	{
insilmaril@398
   743
		if (api.checkParamCount(1))
insilmaril@398
   744
		{
insilmaril@398
   745
			s=api.parString(ok,0);
insilmaril@398
   746
			if (ok) setMapComment(s);
insilmaril@398
   747
		}	
insilmaril@398
   748
	} else if (com=="setMapBackgroundColor")
insilmaril@398
   749
	{
insilmaril@398
   750
		if (!selection)
insilmaril@398
   751
		{
insilmaril@398
   752
			api.setError (Aborted,"Nothing selected");
insilmaril@398
   753
		} else if ( (typeid(*selection) != typeid(BranchObj) && 
insilmaril@398
   754
					 typeid(*selection) != typeid(MapCenterObj)) )
insilmaril@398
   755
		{				  
insilmaril@398
   756
			api.setError (Aborted,"Type of selection is not a branch");
insilmaril@398
   757
		} else if (api.checkParamCount(1))
insilmaril@398
   758
		{
insilmaril@398
   759
			QColor c=api.parColor (ok,0);
insilmaril@398
   760
			if (ok) setMapBackgroundColor (c);
insilmaril@398
   761
		}	
insilmaril@398
   762
	} else if (com=="setMapDefLinkColor")
insilmaril@398
   763
	{
insilmaril@398
   764
		if (!selection)
insilmaril@398
   765
		{
insilmaril@398
   766
			api.setError (Aborted,"Nothing selected");
insilmaril@398
   767
		} else if ( (typeid(*selection) != typeid(BranchObj) && 
insilmaril@398
   768
					 typeid(*selection) != typeid(MapCenterObj)) )
insilmaril@398
   769
		{				  
insilmaril@398
   770
			api.setError (Aborted,"Type of selection is not a branch");
insilmaril@398
   771
		} else if (api.checkParamCount(1))
insilmaril@398
   772
		{
insilmaril@398
   773
			QColor c=api.parColor (ok,0);
insilmaril@398
   774
			if (ok) setMapDefLinkColor (c);
insilmaril@398
   775
		}	
insilmaril@398
   776
	} else if (com=="setMapLinkStyle")
insilmaril@398
   777
	{
insilmaril@398
   778
		if (api.checkParamCount(1))
insilmaril@398
   779
		{
insilmaril@398
   780
			s=api.parString (ok,0);
insilmaril@398
   781
			if (ok) setMapLinkStyle(s);
insilmaril@398
   782
		}	
insilmaril@129
   783
	} else if (com=="setHeading")
insilmaril@129
   784
	{
insilmaril@394
   785
		if (!selection)
insilmaril@394
   786
		{
insilmaril@394
   787
			api.setError (Aborted,"Nothing selected");
insilmaril@394
   788
		} else if ( (typeid(*selection) != typeid(BranchObj) && 
insilmaril@394
   789
					 typeid(*selection) != typeid(MapCenterObj)) )
insilmaril@394
   790
		{				  
insilmaril@394
   791
			api.setError (Aborted,"Type of selection is not a branch");
insilmaril@394
   792
		} else if (api.checkParamCount(1))
insilmaril@129
   793
		{
insilmaril@129
   794
			s=api.parString (ok,0);
insilmaril@366
   795
			if (ok) 
insilmaril@366
   796
				setHeading (s);
insilmaril@129
   797
		}	
insilmaril@395
   798
	} else if (com=="setHideExport")
insilmaril@395
   799
	{
insilmaril@395
   800
		if (!selection)
insilmaril@395
   801
		{
insilmaril@395
   802
			api.setError (Aborted,"Nothing selected");
insilmaril@395
   803
		} else if ( typeid(*selection) != typeid(BranchObj) && 
insilmaril@395
   804
					typeid(*selection) != typeid(FloatImageObj) )
insilmaril@395
   805
		{				  
insilmaril@395
   806
			api.setError (Aborted,"Type of selection is not a branch or floatimage");
insilmaril@395
   807
		} else if (api.checkParamCount(1))
insilmaril@395
   808
		{
insilmaril@395
   809
			b=api.parBool(ok,0);
insilmaril@395
   810
			if (ok) setHideExport (b);
insilmaril@395
   811
		}
insilmaril@143
   812
	} else if (com=="setURL")
insilmaril@143
   813
	{
insilmaril@394
   814
		if (!selection)
insilmaril@394
   815
		{
insilmaril@394
   816
			api.setError (Aborted,"Nothing selected");
insilmaril@394
   817
		} else if ( (typeid(*selection) != typeid(BranchObj) && 
insilmaril@394
   818
					 typeid(*selection) != typeid(MapCenterObj)) )
insilmaril@394
   819
		{				  
insilmaril@394
   820
			api.setError (Aborted,"Type of selection is not a branch");
insilmaril@394
   821
		} else if (api.checkParamCount(1))
insilmaril@143
   822
		{
insilmaril@143
   823
			s=api.parString (ok,0);
insilmaril@366
   824
			if (ok) setURLInt(s);
insilmaril@143
   825
		}	
insilmaril@143
   826
	} else if (com=="setVymLink")
insilmaril@143
   827
	{
insilmaril@394
   828
		if (!selection)
insilmaril@394
   829
		{
insilmaril@394
   830
			api.setError (Aborted,"Nothing selected");
insilmaril@394
   831
		} else if ( (typeid(*selection) != typeid(BranchObj) && 
insilmaril@394
   832
					 typeid(*selection) != typeid(MapCenterObj)) )
insilmaril@394
   833
		{				  
insilmaril@394
   834
			api.setError (Aborted,"Type of selection is not a branch");
insilmaril@394
   835
		} else if (api.checkParamCount(1))
insilmaril@143
   836
		{
insilmaril@143
   837
			s=api.parString (ok,0);
insilmaril@366
   838
			if (ok) setVymLinkInt(s);
insilmaril@143
   839
		}	
insilmaril@143
   840
	}
insilmaril@366
   841
	else if (com=="setFlag")
insilmaril@366
   842
	{
insilmaril@394
   843
		if (!selection)
insilmaril@366
   844
		{
insilmaril@394
   845
			api.setError (Aborted,"Nothing selected");
insilmaril@394
   846
		} else if ( (typeid(*selection) != typeid(BranchObj) && 
insilmaril@394
   847
					 typeid(*selection) != typeid(MapCenterObj)) )
insilmaril@394
   848
		{				  
insilmaril@394
   849
			api.setError (Aborted,"Type of selection is not a branch");
insilmaril@394
   850
		} else if (api.checkParamCount(1))
insilmaril@394
   851
		{
insilmaril@394
   852
			s=api.parString(ok,0);
insilmaril@394
   853
			if (ok) 
insilmaril@394
   854
			{
insilmaril@394
   855
				BranchObj* bo=(BranchObj*)selection;
insilmaril@394
   856
				bo->activateStandardFlag(s);
insilmaril@394
   857
				bo->updateFlagsToolbar();
insilmaril@366
   858
			}	
insilmaril@366
   859
		}
insilmaril@366
   860
	}	
insilmaril@366
   861
	else if (com=="unsetFlag")
insilmaril@366
   862
	{
insilmaril@394
   863
		if (!selection)
insilmaril@366
   864
		{
insilmaril@394
   865
			api.setError (Aborted,"Nothing selected");
insilmaril@394
   866
		} else if ( (typeid(*selection) != typeid(BranchObj) && 
insilmaril@394
   867
					 typeid(*selection) != typeid(MapCenterObj)) )
insilmaril@394
   868
		{				  
insilmaril@394
   869
			api.setError (Aborted,"Type of selection is not a branch");
insilmaril@394
   870
		} else if (api.checkParamCount(1))
insilmaril@394
   871
		{
insilmaril@394
   872
			s=api.parString(ok,0);
insilmaril@394
   873
			if (ok) 
insilmaril@394
   874
			{
insilmaril@394
   875
				BranchObj* bo=(BranchObj*)selection;
insilmaril@394
   876
				bo->deactivateStandardFlag(s);
insilmaril@394
   877
				bo->updateFlagsToolbar();
insilmaril@366
   878
			}	
insilmaril@366
   879
		}
insilmaril@398
   880
	} else
insilmaril@394
   881
		api.setError (Aborted,"Unknown command");
insilmaril@129
   882
insilmaril@129
   883
	// Any errors?
insilmaril@394
   884
	if (api.errorLevel()==NoError)
insilmaril@394
   885
		setChanged();
insilmaril@394
   886
	else	
insilmaril@106
   887
	{
insilmaril@366
   888
		// TODO Error handling
insilmaril@366
   889
		qWarning("MapEditor::parseAtom: Error!");
insilmaril@394
   890
		qWarning(api.errorMessage());
insilmaril@394
   891
	} 
insilmaril@0
   892
}
insilmaril@0
   893
insilmaril@0
   894
insilmaril@0
   895
bool MapEditor::isDefault()
insilmaril@0
   896
{
insilmaril@0
   897
    return mapDefault;
insilmaril@0
   898
}
insilmaril@0
   899
insilmaril@0
   900
bool MapEditor::isUnsaved()
insilmaril@0
   901
{
insilmaril@0
   902
    return mapUnsaved;
insilmaril@0
   903
}
insilmaril@0
   904
insilmaril@0
   905
bool MapEditor::hasChanged()
insilmaril@0
   906
{
insilmaril@0
   907
    return mapChanged;
insilmaril@0
   908
}
insilmaril@0
   909
insilmaril@0
   910
void MapEditor::setChanged()
insilmaril@0
   911
{
insilmaril@0
   912
	mapChanged=true;
insilmaril@0
   913
	mapDefault=false;
insilmaril@0
   914
	mapUnsaved=true;
insilmaril@0
   915
	findReset();
insilmaril@0
   916
}
insilmaril@0
   917
insilmaril@0
   918
void MapEditor::closeMap()
insilmaril@0
   919
{
insilmaril@0
   920
	// Unselect before disabling the toolbar actions
insilmaril@0
   921
	if (selection) selection->unselect();
insilmaril@0
   922
	selection=NULL;
insilmaril@0
   923
	updateActions();
insilmaril@0
   924
insilmaril@0
   925
    clear();
insilmaril@0
   926
	close();
insilmaril@0
   927
}
insilmaril@0
   928
insilmaril@0
   929
void MapEditor::setFilePath(QString fname)
insilmaril@0
   930
{
insilmaril@0
   931
	setFilePath (fname,fname);
insilmaril@0
   932
}
insilmaril@0
   933
insilmaril@0
   934
void MapEditor::setFilePath(QString fname, QString destname)
insilmaril@0
   935
{
insilmaril@205
   936
	if (fname.isEmpty() || fname=="")
insilmaril@106
   937
	{
insilmaril@106
   938
		filePath="";
insilmaril@106
   939
		fileName="";
insilmaril@106
   940
		destPath="";
insilmaril@106
   941
	} else
insilmaril@106
   942
	{
insilmaril@106
   943
		filePath=fname;		// becomes absolute path
insilmaril@106
   944
		fileName=fname;		// gets stripped of path
insilmaril@106
   945
		destPath=destname;	// needed for vymlinks
insilmaril@106
   946
insilmaril@106
   947
		// If fname is not an absolute path, complete it
insilmaril@106
   948
		filePath=QDir(fname).absPath();
insilmaril@106
   949
		fileDir=filePath.left (1+filePath.findRev ("/"));
insilmaril@106
   950
insilmaril@106
   951
		// Set short name, too. Search from behind:
insilmaril@106
   952
		int i=fileName.findRev("/");
insilmaril@106
   953
		if (i>=0) fileName=fileName.remove (0,i+1);
insilmaril@106
   954
insilmaril@106
   955
		// Forget the .vym (or .xml) for name of map
insilmaril@106
   956
		mapName=fileName.left(fileName.findRev(".",-1,true) );
insilmaril@106
   957
	}
insilmaril@0
   958
}
insilmaril@0
   959
insilmaril@0
   960
QString MapEditor::getFilePath()
insilmaril@0
   961
{
insilmaril@0
   962
	return filePath;
insilmaril@0
   963
}
insilmaril@0
   964
insilmaril@0
   965
QString MapEditor::getFileName()
insilmaril@0
   966
{
insilmaril@0
   967
	return fileName;
insilmaril@0
   968
}
insilmaril@0
   969
insilmaril@0
   970
QString MapEditor::getMapName()
insilmaril@0
   971
{
insilmaril@0
   972
	return mapName;
insilmaril@0
   973
}
insilmaril@0
   974
insilmaril@0
   975
QString MapEditor::getDestPath()
insilmaril@0
   976
{
insilmaril@0
   977
	return destPath;
insilmaril@0
   978
}
insilmaril@0
   979
insilmaril@205
   980
ErrorCode MapEditor::load (QString fname, LoadMode lmode)
insilmaril@0
   981
{
insilmaril@205
   982
	ErrorCode err=success;
insilmaril@0
   983
insilmaril@0
   984
	if (lmode==NewMap)
insilmaril@0
   985
	{
insilmaril@0
   986
		if (selection) selection->unselect();
insilmaril@0
   987
		selection=NULL;
insilmaril@0
   988
		mapCenter->clear();
insilmaril@0
   989
		mapCenter->setMapEditor(this);
insilmaril@0
   990
		// (map state is set later at end of load...)
insilmaril@0
   991
	} else
insilmaril@0
   992
	{
insilmaril@395
   993
		if (!selection || (typeid(*selection) != typeid(BranchObj) &&
insilmaril@395
   994
			typeid(*selection) != typeid (MapCenterObj)))
insilmaril@395
   995
			return aborted;
insilmaril@395
   996
		BranchObj *bo=(BranchObj*)selection;	
insilmaril@395
   997
		if (lmode==ImportAdd)
insilmaril@395
   998
			saveStateChangingPart(
insilmaril@395
   999
				selection,
insilmaril@395
  1000
				selection,
insilmaril@395
  1001
				QString("addMapInsert (%1)").arg(fname),
insilmaril@395
  1002
				QString("Add map %1 to %2").arg(fname).arg(getName(bo)));
insilmaril@395
  1003
		else	
insilmaril@395
  1004
			saveStateChangingPart(
insilmaril@395
  1005
				selection,
insilmaril@395
  1006
				selection,
insilmaril@395
  1007
				QString("addMapReplace(%1)").arg(fname),
insilmaril@395
  1008
				QString("Add map %1 to %2").arg(fname).arg(getName(bo)));
insilmaril@0
  1009
	}	
insilmaril@0
  1010
	
insilmaril@0
  1011
    
insilmaril@0
  1012
    mapBuilderHandler handler;
insilmaril@0
  1013
	QFile file( fname );
insilmaril@0
  1014
insilmaril@0
  1015
	// I am paranoid: file should exist anyway
insilmaril@0
  1016
	// according to check in mainwindow.
insilmaril@0
  1017
	if (!file.exists() )
insilmaril@0
  1018
	{
insilmaril@0
  1019
		QMessageBox::critical( 0, tr( "Critical Parse Error" ),
insilmaril@0
  1020
				   tr("Couldn't open map " +fname)+".");
insilmaril@205
  1021
		err=aborted;	
insilmaril@0
  1022
	} else
insilmaril@0
  1023
	{
insilmaril@139
  1024
		blockReposition=true;
insilmaril@0
  1025
		QXmlInputSource source( file);
insilmaril@0
  1026
		QXmlSimpleReader reader;
insilmaril@0
  1027
		reader.setContentHandler( &handler );
insilmaril@0
  1028
		reader.setErrorHandler( &handler );
insilmaril@0
  1029
		handler.setMapEditor( this );
insilmaril@0
  1030
		handler.setTmpDir (filePath.left(filePath.findRev("/",-1)));	// needed to load files with rel. path
insilmaril@243
  1031
		handler.setInputFile (file.name());
insilmaril@0
  1032
		handler.setLoadMode (lmode);
insilmaril@163
  1033
		blockSaveState=true;
insilmaril@0
  1034
		bool ok = reader.parse( source );
insilmaril@139
  1035
		blockReposition=false;
insilmaril@163
  1036
		blockSaveState=false;
insilmaril@0
  1037
		file.close();
insilmaril@0
  1038
		if ( ok ) 
insilmaril@0
  1039
		{
insilmaril@0
  1040
			mapCenter->reposition();
insilmaril@0
  1041
			if (lmode==NewMap)
insilmaril@0
  1042
			{
insilmaril@0
  1043
				mapDefault=false;
insilmaril@0
  1044
				mapChanged=false;
insilmaril@0
  1045
				mapUnsaved=false;
insilmaril@0
  1046
			}
insilmaril@0
  1047
		} else 
insilmaril@0
  1048
		{
insilmaril@0
  1049
			QMessageBox::critical( 0, tr( "Critical Parse Error" ),
insilmaril@0
  1050
					   tr( handler.errorProtocol() ) );
insilmaril@0
  1051
			// returnCode=1;	
insilmaril@0
  1052
			// Still return "success": the map maybe at least
insilmaril@0
  1053
			// partially read by the parser
insilmaril@0
  1054
		}	
insilmaril@0
  1055
	}	
insilmaril@0
  1056
	updateActions();
insilmaril@205
  1057
	return err;
insilmaril@0
  1058
}
insilmaril@0
  1059
insilmaril@0
  1060
int MapEditor::save (const SaveMode &savemode)
insilmaril@0
  1061
{
insilmaril@0
  1062
	int returnCode=0;
insilmaril@0
  1063
insilmaril@0
  1064
	// Create mapName and fileDir
insilmaril@0
  1065
	makeSubDirs (fileDir);
insilmaril@0
  1066
	QString fname;
insilmaril@0
  1067
	if (saveZipped())
insilmaril@0
  1068
		// save as .xml
insilmaril@0
  1069
		fname=mapName+".xml";
insilmaril@0
  1070
	else
insilmaril@0
  1071
		// use name given by user, even if he chooses .doc
insilmaril@0
  1072
		fname=fileName;
insilmaril@0
  1073
insilmaril@0
  1074
insilmaril@0
  1075
	QString saveFile;
insilmaril@106
  1076
	if (savemode==CompleteMap || selection==NULL)
insilmaril@408
  1077
		saveFile=saveToDir (fileDir,mapName+"-",true,QPointF(),NULL);
insilmaril@106
  1078
	else	
insilmaril@408
  1079
		saveFile=saveToDir (fileDir,mapName+"-",true,QPointF(),selection);
insilmaril@0
  1080
insilmaril@163
  1081
	if (!saveStringToDisk(fileDir+fname,saveFile))
insilmaril@0
  1082
		return 1;
insilmaril@0
  1083
insilmaril@0
  1084
	if (returnCode==0)
insilmaril@0
  1085
	{
insilmaril@0
  1086
		mapChanged=false;
insilmaril@0
  1087
		mapUnsaved=false;
insilmaril@390
  1088
		updateActions();
insilmaril@0
  1089
	}
insilmaril@0
  1090
insilmaril@0
  1091
	return returnCode;
insilmaril@0
  1092
}
insilmaril@0
  1093
insilmaril@0
  1094
void MapEditor::setZipped (bool z)
insilmaril@0
  1095
{
insilmaril@0
  1096
	zipped=z;
insilmaril@0
  1097
}
insilmaril@0
  1098
insilmaril@0
  1099
bool MapEditor::saveZipped ()
insilmaril@0
  1100
{
insilmaril@0
  1101
	return zipped;
insilmaril@0
  1102
}
insilmaril@0
  1103
insilmaril@0
  1104
void MapEditor::print()
insilmaril@0
  1105
{
insilmaril@0
  1106
	if ( !printer ) 
insilmaril@0
  1107
	{
insilmaril@0
  1108
		printer = new QPrinter;
insilmaril@0
  1109
		printer->setColorMode (QPrinter::Color);
insilmaril@366
  1110
		printer->setPrinterName (settings.value("/mainwindow/printerName",printer->printerName()).toString());
insilmaril@412
  1111
		printer->setOutputFormat((QPrinter::OutputFormat)settings.value("/mainwindow/printerFormat",printer->outputFormat()).toInt());
insilmaril@412
  1112
		printer->setOutputFileName(settings.value("/mainwindow/printerFileName",printer->outputFileName()).toString());
insilmaril@0
  1113
	}
insilmaril@0
  1114
insilmaril@421
  1115
	// FIXME testing cout << "ME::print  printProgram="<<printer->printProgram().ascii()<<endl;
insilmaril@421
  1116
insilmaril@408
  1117
	QRectF totalBBox=mapCenter->getTotalBBox();
insilmaril@0
  1118
insilmaril@0
  1119
	// Try to set orientation automagically
insilmaril@0
  1120
	// Note: Interpretation of generated postscript is amibiguous, if 
insilmaril@0
  1121
	// there are problems with landscape mode, see
insilmaril@0
  1122
	// http://sdb.suse.de/de/sdb/html/jsmeix_print-cups-landscape-81.html
insilmaril@0
  1123
insilmaril@0
  1124
	if (totalBBox.width()>totalBBox.height())
insilmaril@0
  1125
		// recommend landscape
insilmaril@0
  1126
		printer->setOrientation (QPrinter::Landscape);
insilmaril@0
  1127
	else	
insilmaril@0
  1128
		// recommend portrait
insilmaril@0
  1129
		printer->setOrientation (QPrinter::Portrait);
insilmaril@0
  1130
insilmaril@0
  1131
	if ( printer->setup(this) ) 
insilmaril@0
  1132
	// returns false, if printing is canceled
insilmaril@0
  1133
	{
insilmaril@0
  1134
		QPainter pp(printer);
insilmaril@0
  1135
insilmaril@412
  1136
		pp.setRenderHint(QPainter::Antialiasing,true);
insilmaril@412
  1137
insilmaril@0
  1138
		// Don't print the visualisation of selection
insilmaril@0
  1139
		LinkableMapObj *oldselection=NULL;
insilmaril@0
  1140
		if (selection) 
insilmaril@0
  1141
		{
insilmaril@0
  1142
			oldselection=selection;
insilmaril@0
  1143
			selection->unselect();
insilmaril@0
  1144
		}
insilmaril@0
  1145
insilmaril@408
  1146
		QRectF mapRect=totalBBox;
insilmaril@411
  1147
		QGraphicsRectItem *frame=NULL;
insilmaril@412
  1148
insilmaril@417
  1149
		if (printFrame) 
insilmaril@0
  1150
		{
insilmaril@417
  1151
			// Print frame around map
insilmaril@417
  1152
			mapRect.setRect (totalBBox.x()-10, totalBBox.y()-10, 
insilmaril@417
  1153
				totalBBox.width()+20, totalBBox.height()+20);
insilmaril@417
  1154
			frame=mapScene->addRect (mapRect, QPen(Qt::black),QBrush(Qt::NoBrush));
insilmaril@417
  1155
			frame->setZValue(0);
insilmaril@417
  1156
			frame->show();    
insilmaril@417
  1157
		}		
insilmaril@417
  1158
insilmaril@417
  1159
insilmaril@412
  1160
		double paperAspect = (double)printer->width()   / (double)printer->height();
insilmaril@412
  1161
		double   mapAspect = (double)mapRect.width() / (double)mapRect.height();
insilmaril@417
  1162
		int viewBottom;
insilmaril@0
  1163
		if (mapAspect>=paperAspect)
insilmaril@0
  1164
		{
insilmaril@0
  1165
			// Fit horizontally to paper width
insilmaril@417
  1166
			//pp.setViewport(0,0, printer->width(),(int)(printer->width()/mapAspect) );	
insilmaril@417
  1167
			viewBottom=(int)(printer->width()/mapAspect);	
insilmaril@0
  1168
		}	else
insilmaril@0
  1169
		{
insilmaril@0
  1170
			// Fit vertically to paper height
insilmaril@417
  1171
			//pp.setViewport(0,0,(int)(printer->height()*mapAspect),printer->height());	
insilmaril@417
  1172
			viewBottom=printer->height();	
insilmaril@0
  1173
		}	
insilmaril@417
  1174
		
insilmaril@417
  1175
		if (printFooter) 
insilmaril@417
  1176
		{
insilmaril@417
  1177
			// Print footer below map
insilmaril@417
  1178
			QFont font;		
insilmaril@417
  1179
			font.setPointSize(10);
insilmaril@417
  1180
			pp.setFont (font);
insilmaril@417
  1181
			QRectF footerBox(0,viewBottom,printer->width(),15);
insilmaril@417
  1182
			pp.drawText ( footerBox,Qt::AlignLeft,"VYM - " +fileName);
insilmaril@417
  1183
			pp.drawText ( footerBox, Qt::AlignRight, QDate::currentDate().toString(Qt::TextDate));
insilmaril@417
  1184
		}
insilmaril@412
  1185
		mapScene->render (
insilmaril@412
  1186
			&pp, 
insilmaril@412
  1187
			QRectF (0,0,printer->width(),printer->height()-15),
insilmaril@412
  1188
			QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height())
insilmaril@412
  1189
		);
insilmaril@412
  1190
		
insilmaril@412
  1191
		// Viewport has paper dimension
insilmaril@412
  1192
		if (frame)  delete (frame);
insilmaril@0
  1193
insilmaril@0
  1194
		// Restore selection
insilmaril@0
  1195
		if (oldselection) 
insilmaril@0
  1196
		{
insilmaril@0
  1197
			selection=oldselection;
insilmaril@0
  1198
			selection->select();
insilmaril@0
  1199
		}	
insilmaril@119
  1200
insilmaril@119
  1201
		// Save settings in vymrc
insilmaril@366
  1202
		settings.writeEntry("/mainwindow/printerName",printer->printerName());
insilmaril@412
  1203
		settings.writeEntry("/mainwindow/printerFormat",printer->outputFormat());
insilmaril@412
  1204
		settings.writeEntry("/mainwindow/printerFileName",printer->outputFileName());
insilmaril@0
  1205
	}
insilmaril@0
  1206
}
insilmaril@0
  1207
insilmaril@408
  1208
void MapEditor::setAntiAlias (bool b)
insilmaril@408
  1209
{
insilmaril@408
  1210
	setRenderHint(QPainter::Antialiasing,b);
insilmaril@408
  1211
}
insilmaril@408
  1212
insilmaril@408
  1213
void MapEditor::setSmoothPixmap(bool b)
insilmaril@408
  1214
{
insilmaril@408
  1215
	setRenderHint(QPainter::SmoothPixmapTransform,b);
insilmaril@408
  1216
}
insilmaril@408
  1217
insilmaril@0
  1218
QPixmap MapEditor::getPixmap()
insilmaril@0
  1219
{
insilmaril@408
  1220
	QRectF mapRect=mapCenter->getTotalBBox();
insilmaril@420
  1221
	QPixmap pix((int)mapRect.width(),(int)mapRect.height());
insilmaril@420
  1222
	cout << "w.h="<<(int)mapRect.width()<<","<<(int)mapRect.height()<<endl;
insilmaril@0
  1223
	QPainter pp (&pix);
insilmaril@420
  1224
	
insilmaril@420
  1225
	pp.setRenderHints(renderHints());
insilmaril@0
  1226
insilmaril@0
  1227
	// Don't print the visualisation of selection
insilmaril@0
  1228
	LinkableMapObj *oldselection=NULL;
insilmaril@0
  1229
	if (selection) 
insilmaril@0
  1230
	{
insilmaril@0
  1231
		oldselection=selection;
insilmaril@0
  1232
		selection->unselect();
insilmaril@0
  1233
	}
insilmaril@0
  1234
insilmaril@420
  1235
	mapScene->render (	&pp, 
insilmaril@420
  1236
		QRectF(0,0,mapRect.width(),mapRect.height()),
insilmaril@420
  1237
		QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height() ));
insilmaril@420
  1238
insilmaril@420
  1239
//	mapScene->render(&pp);	// draw scene to painter
insilmaril@417
  1240
insilmaril@0
  1241
insilmaril@0
  1242
insilmaril@0
  1243
	// Restore selection
insilmaril@0
  1244
	if (oldselection) 
insilmaril@0
  1245
	{
insilmaril@0
  1246
		selection=oldselection;
insilmaril@0
  1247
		selection->select();
insilmaril@0
  1248
	}	
insilmaril@0
  1249
	
insilmaril@0
  1250
	return pix;
insilmaril@0
  1251
}
insilmaril@0
  1252
insilmaril@260
  1253
void MapEditor::setHideTmpMode (HideTmpMode mode)
insilmaril@260
  1254
{
insilmaril@260
  1255
	hidemode=mode;
insilmaril@260
  1256
	mapCenter->setHideTmp (hidemode);
insilmaril@260
  1257
	mapCenter->reposition();
insilmaril@408
  1258
	scene()->update();
insilmaril@260
  1259
}
insilmaril@260
  1260
insilmaril@260
  1261
HideTmpMode MapEditor::getHideTmpMode()
insilmaril@260
  1262
{
insilmaril@260
  1263
	return hidemode;
insilmaril@260
  1264
}
insilmaril@260
  1265
insilmaril@0
  1266
void MapEditor::exportImage(QString fn)
insilmaril@0
  1267
{
insilmaril@260
  1268
	setExportMode (true);
insilmaril@0
  1269
	QPixmap pix (getPixmap());
insilmaril@0
  1270
	pix.save(fn, "PNG");
insilmaril@260
  1271
	setExportMode (false);
insilmaril@260
  1272
}
insilmaril@260
  1273
insilmaril@260
  1274
void MapEditor::setExportMode (bool b)
insilmaril@260
  1275
{
insilmaril@260
  1276
	// should be called before and after exports
insilmaril@260
  1277
	// depending on the settings
insilmaril@366
  1278
	if (b && settings.value("/export/useHideExport","yes")=="yes")
insilmaril@260
  1279
		setHideTmpMode (HideExport);
insilmaril@260
  1280
	else	
insilmaril@260
  1281
		setHideTmpMode (HideNone);
insilmaril@0
  1282
}
insilmaril@0
  1283
insilmaril@366
  1284
void MapEditor::exportImage(QString fn, QString format)
insilmaril@0
  1285
{
insilmaril@260
  1286
	setExportMode (true);
insilmaril@0
  1287
	QPixmap pix (getPixmap());
insilmaril@366
  1288
	pix.save(fn, format);
insilmaril@260
  1289
	setExportMode (false);
insilmaril@0
  1290
}
insilmaril@0
  1291
insilmaril@185
  1292
void MapEditor::exportOOPresentation(const QString &fn, const QString &cf)
insilmaril@163
  1293
{
insilmaril@185
  1294
	ExportOO ex;
insilmaril@199
  1295
	ex.setFile (fn);
insilmaril@185
  1296
	ex.setMapCenter(mapCenter);
insilmaril@260
  1297
	if (ex.setConfigFile(cf)) 
insilmaril@260
  1298
	{
insilmaril@260
  1299
		setExportMode (true);
insilmaril@260
  1300
		ex.exportPresentation();
insilmaril@260
  1301
		setExportMode (false);
insilmaril@260
  1302
	}
insilmaril@163
  1303
}
insilmaril@163
  1304
insilmaril@163
  1305
insilmaril@0
  1306
insilmaril@0
  1307
void MapEditor::exportXML(const QString &dir)
insilmaril@0
  1308
{
insilmaril@260
  1309
	// Hide stuff during export, if settings want this
insilmaril@260
  1310
	setExportMode (true);
insilmaril@260
  1311
insilmaril@0
  1312
	// Create subdirectories
insilmaril@0
  1313
	makeSubDirs (dir);
insilmaril@0
  1314
insilmaril@0
  1315
	// write to directory
insilmaril@106
  1316
	QString saveFile=saveToDir (dir,mapName+"-",true,mapCenter->getTotalBBox().topLeft() ,NULL);
insilmaril@0
  1317
	QFile file;
insilmaril@0
  1318
insilmaril@0
  1319
	file.setName ( dir + "/"+mapName+".xml");
insilmaril@366
  1320
	if ( !file.open( QIODevice::WriteOnly ) )
insilmaril@0
  1321
	{
insilmaril@0
  1322
		// This should neverever happen
insilmaril@159
  1323
		QMessageBox::critical (0,tr("Critical Export Error"),tr("MapEditor::exportXML couldn't open %1").arg(file.name()));
insilmaril@0
  1324
		return;
insilmaril@0
  1325
	}	
insilmaril@0
  1326
insilmaril@0
  1327
	// Write it finally, and write in UTF8, no matter what 
insilmaril@0
  1328
	QTextStream ts( &file );
insilmaril@0
  1329
	ts.setEncoding (QTextStream::UnicodeUTF8);
insilmaril@0
  1330
	ts << saveFile;
insilmaril@0
  1331
	file.close();
insilmaril@0
  1332
insilmaril@0
  1333
	// Now write image, too
insilmaril@0
  1334
	exportImage (dir+"/images/"+mapName+".png");
insilmaril@260
  1335
insilmaril@260
  1336
	setExportMode (false);
insilmaril@0
  1337
}
insilmaril@0
  1338
insilmaril@0
  1339
void MapEditor::clear()
insilmaril@0
  1340
{
insilmaril@0
  1341
	if (selection)
insilmaril@0
  1342
	{
insilmaril@0
  1343
		selection->unselect();
insilmaril@0
  1344
		selection=NULL;
insilmaril@0
  1345
	}	
insilmaril@0
  1346
insilmaril@0
  1347
	mapCenter->clear();
insilmaril@0
  1348
}
insilmaril@0
  1349
insilmaril@0
  1350
void MapEditor::copy()
insilmaril@0
  1351
{
insilmaril@0
  1352
	if (selection) 
insilmaril@0
  1353
	{
insilmaril@166
  1354
		// write to directory
insilmaril@166
  1355
		QString clipfile="part";
insilmaril@408
  1356
		QString saveFile=saveToDir (fileDir,clipfile+"-",true,QPointF(),selection);
insilmaril@166
  1357
		QFile file;
insilmaril@166
  1358
insilmaril@166
  1359
		file.setName ( clipboardDir + "/"+clipfile+".xml");
insilmaril@366
  1360
		if ( !file.open( QIODevice::WriteOnly ) )
insilmaril@0
  1361
		{
insilmaril@166
  1362
			// This should neverever happen
insilmaril@166
  1363
			QMessageBox::critical (0,tr("Critical Export Error"),tr("MapEditor::exportXML couldn't open %1").arg(file.name()));
insilmaril@166
  1364
			return;
insilmaril@166
  1365
		}	
insilmaril@166
  1366
insilmaril@166
  1367
		// Write it finally, and write in UTF8, no matter what 
insilmaril@166
  1368
		QTextStream ts( &file );
insilmaril@166
  1369
		ts.setEncoding (QTextStream::UnicodeUTF8);
insilmaril@166
  1370
		ts << saveFile;
insilmaril@166
  1371
		file.close();
insilmaril@166
  1372
insilmaril@166
  1373
		clipboardEmpty=false;
insilmaril@166
  1374
		updateActions();
insilmaril@0
  1375
	}	    
insilmaril@0
  1376
}
insilmaril@0
  1377
insilmaril@163
  1378
void MapEditor::redo()
insilmaril@163
  1379
{
insilmaril@163
  1380
	blockSaveState=true;
insilmaril@163
  1381
	
insilmaril@366
  1382
	// Restore variables
insilmaril@366
  1383
	int curStep=undoSet.readNumEntry (QString("/history/curStep"));
insilmaril@366
  1384
	int undosAvail=undoSet.readNumEntry (QString("/history/undosAvail"));
insilmaril@366
  1385
	int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
insilmaril@381
  1386
	// Can we undo at all?
insilmaril@381
  1387
	if (redosAvail<1) return;
insilmaril@381
  1388
	redosAvail--;
insilmaril@381
  1389
insilmaril@381
  1390
	if (undosAvail<stepsTotal) undosAvail++;
insilmaril@366
  1391
	curStep++;
insilmaril@381
  1392
	if (curStep>stepsTotal) curStep=1;
insilmaril@366
  1393
	QString undoCommand=  undoSet.readEntry (QString("/history/step-%1/undoCommand").arg(curStep));
insilmaril@366
  1394
	QString undoSelection=undoSet.readEntry (QString("/history/step-%1/undoSelection").arg(curStep));
insilmaril@366
  1395
	QString redoCommand=  undoSet.readEntry (QString("/history/step-%1/redoCommand").arg(curStep));
insilmaril@366
  1396
	QString redoSelection=undoSet.readEntry (QString("/history/step-%1/redoSelection").arg(curStep));
insilmaril@366
  1397
	QString comment=undoSet.readEntry (QString("/history/step-%1/comment").arg(curStep));
insilmaril@395
  1398
	QString version=undoSet.readEntry ("/history/version");
insilmaril@395
  1399
insilmaril@395
  1400
	if (!checkVersion(version))
insilmaril@395
  1401
		QMessageBox::warning(0,tr("Warning"),
insilmaril@404
  1402
			tr("Version %1 of saved undo/redo data\ndoes not match current vym version %2.").arg(version).arg(vymVersion));
insilmaril@395
  1403
insilmaril@366
  1404
insilmaril@163
  1405
	// Find out current undo directory
insilmaril@366
  1406
	QString bakMapDir=QDir::convertSeparators (QString(tmpMapDir+"/undo-%1").arg(curStep));
insilmaril@366
  1407
insilmaril@366
  1408
/* TODO remove testing
insilmaril@366
  1409
*/
insilmaril@366
  1410
	cout << "ME::redo() begin\n";
insilmaril@366
  1411
	cout << "    undosAvail="<<undosAvail<<endl;
insilmaril@366
  1412
	cout << "    redosAvail="<<redosAvail<<endl;
insilmaril@366
  1413
	cout << "       curStep="<<curStep<<endl;
insilmaril@366
  1414
	cout << "    ---------------------------"<<endl;
insilmaril@366
  1415
	cout << "    comment="<<comment.toStdString()<<endl;
insilmaril@366
  1416
	cout << "    undoCom="<<undoCommand.toStdString()<<endl;
insilmaril@366
  1417
	cout << "    undoSel="<<undoSelection.toStdString()<<endl;
insilmaril@366
  1418
	cout << "    redoCom="<<redoCommand.toStdString()<<endl;
insilmaril@366
  1419
	cout << "    redoSel="<<redoSelection.toStdString()<<endl;
insilmaril@366
  1420
	cout << "    ---------------------------"<<endl<<endl;
insilmaril@163
  1421
insilmaril@163
  1422
	// select  object before redo
insilmaril@163
  1423
	if (!redoSelection.isEmpty())
insilmaril@163
  1424
		select (redoSelection);
insilmaril@163
  1425
insilmaril@366
  1426
insilmaril@366
  1427
	parseAtom (redoCommand);
insilmaril@366
  1428
	mapCenter->reposition();
insilmaril@366
  1429
insilmaril@366
  1430
	blockSaveState=false;
insilmaril@366
  1431
insilmaril@366
  1432
	undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
insilmaril@366
  1433
	undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
insilmaril@366
  1434
	undoSet.setEntry ("/history/curStep",QString::number(curStep));
insilmaril@366
  1435
	undoSet.writeSettings(histPath);
insilmaril@366
  1436
insilmaril@408
  1437
	mainWindow->updateHistory (undoSet);
insilmaril@407
  1438
	updateActions();
insilmaril@366
  1439
insilmaril@366
  1440
	/* TODO remove testing
insilmaril@163
  1441
	cout << "ME::redo() end\n";
insilmaril@163
  1442
	cout << "    undosAvail="<<undosAvail<<endl;
insilmaril@366
  1443
	cout << "    redosAvail="<<redosAvail<<endl;
insilmaril@366
  1444
	cout << "       curStep="<<curStep<<endl;
insilmaril@163
  1445
	cout << "    ---------------------------"<<endl<<endl;
insilmaril@404
  1446
	*/
insilmaril@366
  1447
insilmaril@366
  1448
insilmaril@163
  1449
}
insilmaril@163
  1450
insilmaril@390
  1451
bool MapEditor::isRedoAvailable()
insilmaril@390
  1452
{
insilmaril@393
  1453
	if (undoSet.readNumEntry("/history/redosAvail",0)>0)
insilmaril@390
  1454
		return true;
insilmaril@390
  1455
	else	
insilmaril@390
  1456
		return false;
insilmaril@390
  1457
}
insilmaril@390
  1458
insilmaril@106
  1459
void MapEditor::undo()
insilmaril@106
  1460
{
insilmaril@163
  1461
	blockSaveState=true;
insilmaril@163
  1462
	
insilmaril@366
  1463
	// Restore variables
insilmaril@366
  1464
	int curStep=undoSet.readNumEntry (QString("/history/curStep"));
insilmaril@366
  1465
	int undosAvail=undoSet.readNumEntry (QString("/history/undosAvail"));
insilmaril@366
  1466
	int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
insilmaril@381
  1467
insilmaril@381
  1468
	// Can we undo at all?
insilmaril@381
  1469
	if (undosAvail<1) return;
insilmaril@381
  1470
insilmaril@366
  1471
	QString undoCommand=  undoSet.readEntry (QString("/history/step-%1/undoCommand").arg(curStep));
insilmaril@366
  1472
	QString undoSelection=undoSet.readEntry (QString("/history/step-%1/undoSelection").arg(curStep));
insilmaril@366
  1473
	QString redoCommand=  undoSet.readEntry (QString("/history/step-%1/redoCommand").arg(curStep));
insilmaril@366
  1474
	QString redoSelection=undoSet.readEntry (QString("/history/step-%1/redoSelection").arg(curStep));
insilmaril@366
  1475
	QString comment=undoSet.readEntry (QString("/history/step-%1/comment").arg(curStep));
insilmaril@395
  1476
	QString version=undoSet.readEntry ("/history/version");
insilmaril@395
  1477
insilmaril@395
  1478
	if (!checkVersion(version))
insilmaril@395
  1479
		QMessageBox::warning(0,tr("Warning"),
insilmaril@404
  1480
			tr("Version %1 of saved undo/redo data\ndoes not match current vym version %2.").arg(version).arg(vymVersion));
insilmaril@366
  1481
insilmaril@163
  1482
	// Find out current undo directory
insilmaril@366
  1483
	QString bakMapDir=QDir::convertSeparators (QString(tmpMapDir+"/undo-%1").arg(curStep));
insilmaril@163
  1484
insilmaril@136
  1485
	// select  object before undo
insilmaril@136
  1486
	if (!undoSelection.isEmpty())
insilmaril@136
  1487
		select (undoSelection);
insilmaril@136
  1488
insilmaril@164
  1489
/* TODO testing
insilmaril@366
  1490
*/	
insilmaril@163
  1491
	cout << "ME::undo() begin\n";
insilmaril@163
  1492
	cout << "    undosAvail="<<undosAvail<<endl;
insilmaril@366
  1493
	cout << "    redosAvail="<<redosAvail<<endl;
insilmaril@366
  1494
	cout << "       curStep="<<curStep<<endl;
insilmaril@163
  1495
	cout << "    ---------------------------"<<endl;
insilmaril@366
  1496
	cout << "    comment="<<comment.toStdString()<<endl;
insilmaril@366
  1497
	cout << "    undoCom="<<undoCommand.toStdString()<<endl;
insilmaril@366
  1498
	cout << "    undoSel="<<undoSelection.toStdString()<<endl;
insilmaril@366
  1499
	cout << "    redoCom="<<redoCommand.toStdString()<<endl;
insilmaril@366
  1500
	cout << "    redoSel="<<redoSelection.toStdString()<<endl;
insilmaril@366
  1501
	cout << "    ---------------------------"<<endl<<endl;
insilmaril@106
  1502
	parseAtom (undoCommand);
insilmaril@106
  1503
	mapCenter->reposition();
insilmaril@106
  1504
insilmaril@163
  1505
	undosAvail--;
insilmaril@366
  1506
	curStep--; 
insilmaril@381
  1507
	if (curStep<1) curStep=stepsTotal;
insilmaril@366
  1508
insilmaril@366
  1509
	redosAvail++;
insilmaril@163
  1510
insilmaril@163
  1511
	blockSaveState=false;
insilmaril@163
  1512
/* TODO remove testing
insilmaril@163
  1513
	cout << "ME::undo() end\n";
insilmaril@163
  1514
	cout << "    undosAvail="<<undosAvail<<endl;
insilmaril@366
  1515
	cout << "    redosAvail="<<redosAvail<<endl;
insilmaril@366
  1516
	cout << "       curStep="<<curStep<<endl;
insilmaril@163
  1517
	cout << "    ---------------------------"<<endl<<endl;
insilmaril@404
  1518
*/
insilmaril@366
  1519
insilmaril@366
  1520
	undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
insilmaril@366
  1521
	undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
insilmaril@366
  1522
	undoSet.setEntry ("/history/curStep",QString::number(curStep));
insilmaril@366
  1523
	undoSet.writeSettings(histPath);
insilmaril@366
  1524
insilmaril@404
  1525
	mainWindow->updateHistory (undoSet);
insilmaril@366
  1526
	updateActions();
insilmaril@420
  1527
	ensureSelectionVisible();
insilmaril@106
  1528
}
insilmaril@106
  1529
insilmaril@390
  1530
bool MapEditor::isUndoAvailable()
insilmaril@390
  1531
{
insilmaril@390
  1532
	if (undoSet.readNumEntry("/history/undosAvail",0)>0)
insilmaril@390
  1533
		return true;
insilmaril@390
  1534
	else	
insilmaril@390
  1535
		return false;
insilmaril@390
  1536
}
insilmaril@390
  1537
insilmaril@404
  1538
void MapEditor::gotoHistoryStep (int i)
insilmaril@381
  1539
{
insilmaril@381
  1540
	// Restore variables
insilmaril@381
  1541
	int undosAvail=undoSet.readNumEntry (QString("/history/undosAvail"));
insilmaril@388
  1542
	int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
insilmaril@388
  1543
insilmaril@388
  1544
	if (i<0) i=undosAvail+redosAvail;
insilmaril@388
  1545
insilmaril@388
  1546
	// Clicking above current step makes us undo things
insilmaril@381
  1547
	if (i<undosAvail) 
insilmaril@381
  1548
	{	
insilmaril@381
  1549
		for (int j=0; j<undosAvail-i; j++) undo();
insilmaril@381
  1550
		return;
insilmaril@381
  1551
	}	
insilmaril@388
  1552
	// Clicking below current step makes us redo things
insilmaril@381
  1553
	if (i>undosAvail) 
insilmaril@381
  1554
		for (int j=undosAvail; j<i; j++) redo();
insilmaril@381
  1555
insilmaril@381
  1556
	// And ignore clicking the current row ;-)	
insilmaril@381
  1557
}
insilmaril@381
  1558
insilmaril@394
  1559
void MapEditor::addMapReplaceInt(const QString &undoSel, const QString &path)
insilmaril@106
  1560
{
insilmaril@393
  1561
	QString pathDir=path.left(path.findRev("/"));
insilmaril@393
  1562
	QDir d(pathDir);
insilmaril@393
  1563
	QFile file (path);
insilmaril@163
  1564
insilmaril@106
  1565
	if (d.exists() )
insilmaril@106
  1566
	{
insilmaril@106
  1567
		// We need to parse saved XML data
insilmaril@106
  1568
		mapBuilderHandler handler;
insilmaril@163
  1569
		QXmlInputSource source( file);
insilmaril@106
  1570
		QXmlSimpleReader reader;
insilmaril@106
  1571
		reader.setContentHandler( &handler );
insilmaril@106
  1572
		reader.setErrorHandler( &handler );
insilmaril@106
  1573
		handler.setMapEditor( this );
insilmaril@393
  1574
		handler.setTmpDir ( pathDir );	// needed to load files with rel. path
insilmaril@106
  1575
		if (undoSel.isEmpty())
insilmaril@106
  1576
		{
insilmaril@139
  1577
			unselect();
insilmaril@106
  1578
			mapCenter->clear();
insilmaril@106
  1579
			handler.setLoadMode (NewMap);
insilmaril@106
  1580
		} else	
insilmaril@106
  1581
		{
insilmaril@106
  1582
			select (undoSel);
insilmaril@106
  1583
			handler.setLoadMode (ImportReplace);
insilmaril@106
  1584
		}	
insilmaril@139
  1585
		blockReposition=true;
insilmaril@106
  1586
		bool ok = reader.parse( source );
insilmaril@139
  1587
		blockReposition=false;
insilmaril@106
  1588
		if (! ok ) 
insilmaril@106
  1589
		{	
insilmaril@106
  1590
			// This should never ever happen
insilmaril@393
  1591
			QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path),
insilmaril@393
  1592
								    handler.errorProtocol());
insilmaril@106
  1593
		}
insilmaril@106
  1594
	} else	
insilmaril@393
  1595
		QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
insilmaril@393
  1596
}
insilmaril@393
  1597
insilmaril@394
  1598
void MapEditor::addMapInsertInt (const QString &path, int pos)
insilmaril@393
  1599
{
insilmaril@393
  1600
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@393
  1601
				      typeid(*selection) == typeid(MapCenterObj))) 
insilmaril@106
  1602
	{
insilmaril@393
  1603
		QString pathDir=path.left(path.findRev("/"));
insilmaril@393
  1604
		QDir d(pathDir);
insilmaril@393
  1605
		QFile file (path);
insilmaril@393
  1606
insilmaril@393
  1607
		if (d.exists() )
insilmaril@393
  1608
		{
insilmaril@393
  1609
			// We need to parse saved XML data
insilmaril@393
  1610
			mapBuilderHandler handler;
insilmaril@393
  1611
			QXmlInputSource source( file);
insilmaril@393
  1612
			QXmlSimpleReader reader;
insilmaril@393
  1613
			reader.setContentHandler( &handler );
insilmaril@393
  1614
			reader.setErrorHandler( &handler );
insilmaril@393
  1615
			handler.setMapEditor( this );
insilmaril@393
  1616
			handler.setTmpDir ( pathDir );	// needed to load files with rel. path
insilmaril@394
  1617
			handler.setLoadMode (ImportAdd);
insilmaril@393
  1618
			blockReposition=true;
insilmaril@393
  1619
			bool ok = reader.parse( source );
insilmaril@393
  1620
			blockReposition=false;
insilmaril@393
  1621
			if (! ok ) 
insilmaril@393
  1622
			{	
insilmaril@393
  1623
				// This should never ever happen
insilmaril@393
  1624
				QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path),
insilmaril@393
  1625
										handler.errorProtocol());
insilmaril@393
  1626
			}
insilmaril@394
  1627
			if (selection!=mapCenter)
insilmaril@421
  1628
				((BranchObj*)selection)->getLastBranch()->linkTo ((BranchObj*)(selection),pos);
insilmaril@393
  1629
		} else	
insilmaril@393
  1630
			QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
insilmaril@393
  1631
	}		
insilmaril@106
  1632
}
insilmaril@106
  1633
insilmaril@166
  1634
void MapEditor::pasteNoSave()
insilmaril@0
  1635
{
insilmaril@166
  1636
	load (clipboardDir+"/part.xml",ImportAdd);
insilmaril@0
  1637
}
insilmaril@0
  1638
insilmaril@0
  1639
void MapEditor::cutNoSave()
insilmaril@0
  1640
{
insilmaril@166
  1641
	copy();
insilmaril@166
  1642
	deleteSelection();
insilmaril@0
  1643
}
insilmaril@0
  1644
insilmaril@0
  1645
void MapEditor::paste()
insilmaril@0
  1646
{   
insilmaril@166
  1647
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@166
  1648
				      typeid(*selection) == typeid(MapCenterObj))) 
insilmaril@166
  1649
	{
insilmaril@166
  1650
		pasteNoSave();
insilmaril@395
  1651
		saveStateChangingPart(
insilmaril@395
  1652
			selection,
insilmaril@395
  1653
			selection,
insilmaril@395
  1654
			"paste ()",
insilmaril@395
  1655
			QString("Paste to %1").arg( getName(selection))
insilmaril@395
  1656
		);
insilmaril@166
  1657
		mapCenter->reposition();
insilmaril@166
  1658
	}
insilmaril@0
  1659
}
insilmaril@0
  1660
insilmaril@0
  1661
void MapEditor::cut()
insilmaril@0
  1662
{
insilmaril@395
  1663
	saveStateChangingPart(
insilmaril@395
  1664
		selection->getParObj(),
insilmaril@395
  1665
		selection,
insilmaril@395
  1666
		"cut ()",
insilmaril@395
  1667
		QString("Cut %1").arg(getName(selection))
insilmaril@395
  1668
	);
insilmaril@0
  1669
	copy();
insilmaril@0
  1670
	cutNoSave();
insilmaril@0
  1671
	mapCenter->reposition();
insilmaril@0
  1672
}
insilmaril@0
  1673
insilmaril@130
  1674
void MapEditor::move(const int &x, const int &y)
insilmaril@130
  1675
{
insilmaril@366
  1676
	if (selection)
insilmaril@366
  1677
	{
insilmaril@408
  1678
		QString ps=qpointfToString (selection->getAbsPos());
insilmaril@366
  1679
		QString s=selection->getSelectString();
insilmaril@366
  1680
		saveState(
insilmaril@366
  1681
			s, "move "+ps, 
insilmaril@408
  1682
			s, "move "+qpointfToString (QPointF (x,y)), 
insilmaril@366
  1683
			QString("Move %1 to  %2").arg(getName(selection)).arg(ps));
insilmaril@366
  1684
		selection->move(x,y);
insilmaril@366
  1685
		mapCenter->reposition();
insilmaril@366
  1686
	}
insilmaril@366
  1687
insilmaril@366
  1688
}
insilmaril@366
  1689
insilmaril@366
  1690
void MapEditor::moveRel (const int &x, const int &y)
insilmaril@366
  1691
{
insilmaril@388
  1692
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@388
  1693
				      typeid(*selection) == typeid(MapCenterObj) ||
insilmaril@388
  1694
					  typeid(*selection) == typeid (FloatImageObj))) 
insilmaril@366
  1695
	{
insilmaril@408
  1696
		QString ps=qpointfToString (selection->getRelPos());
insilmaril@366
  1697
		QString s=selection->getSelectString();
insilmaril@366
  1698
		saveState(
insilmaril@366
  1699
			s, "moveRel "+ps, 
insilmaril@408
  1700
			s, "moveRel "+qpointfToString (QPointF (x,y)), 
insilmaril@366
  1701
			QString("Move %1 to relativ position %2").arg(getName(selection)).arg(ps));
insilmaril@388
  1702
		((OrnamentedObj*)selection)->move2RelPos (x,y);
insilmaril@366
  1703
		mapCenter->reposition();
insilmaril@420
  1704
		selection->updateLink();
insilmaril@366
  1705
	}
insilmaril@130
  1706
}
insilmaril@130
  1707
insilmaril@0
  1708
void MapEditor::moveBranchUp()
insilmaril@0
  1709
{
insilmaril@0
  1710
	BranchObj* bo;
insilmaril@0
  1711
	BranchObj* par;
insilmaril@0
  1712
	if (typeid(*selection) == typeid(BranchObj)  ) 
insilmaril@0
  1713
	{
insilmaril@163
  1714
		bo=(BranchObj*)selection;
insilmaril@314
  1715
		if (!bo->canMoveBranchUp()) return;
insilmaril@0
  1716
		par=(BranchObj*)(bo->getParObj());
insilmaril@0
  1717
		selection->unselect();
insilmaril@366
  1718
		bo=par->moveBranchUp (bo);	// bo will be the one below selection
insilmaril@0
  1719
		selection->select();
insilmaril@366
  1720
		saveState (selection,"moveBranchDown ()",bo,"moveBranchUp ()",QString("Move up %1").arg(getName(bo)));
insilmaril@0
  1721
		mapCenter->reposition();
insilmaril@420
  1722
		scene()->update();
insilmaril@0
  1723
		ensureSelectionVisible();
insilmaril@0
  1724
	}
insilmaril@0
  1725
}
insilmaril@0
  1726
insilmaril@0
  1727
void MapEditor::moveBranchDown()
insilmaril@0
  1728
{
insilmaril@0
  1729
	BranchObj* bo;
insilmaril@0
  1730
	BranchObj* par;
insilmaril@0
  1731
	if (typeid(*selection) == typeid(BranchObj)  ) 
insilmaril@0
  1732
	{
insilmaril@163
  1733
		bo=(BranchObj*)selection;
insilmaril@314
  1734
		if (!bo->canMoveBranchDown()) return;
insilmaril@0
  1735
		par=(BranchObj*)(bo->getParObj());
insilmaril@0
  1736
		selection->unselect(); 
insilmaril@366
  1737
		bo=par->moveBranchDown(bo);	// bo will be the one above selection
insilmaril@0
  1738
		selection->select();
insilmaril@366
  1739
		saveState(selection,"moveBranchUp ()",bo,"moveBranchDown ()",QString("Move down %1").arg(getName(bo)));
insilmaril@0
  1740
		mapCenter->reposition();
insilmaril@420
  1741
		scene()->update();
insilmaril@0
  1742
		ensureSelectionVisible();
insilmaril@0
  1743
	}	
insilmaril@0
  1744
}
insilmaril@0
  1745
insilmaril@420
  1746
void MapEditor::linkTo(const QString &dstString)
insilmaril@420
  1747
{
insilmaril@420
  1748
	BranchObj *dst=(BranchObj*)(mapCenter->findObjBySelect(dstString));
insilmaril@420
  1749
	if (dst && (typeid(*dst)==typeid (BranchObj) || 
insilmaril@420
  1750
	            typeid(*dst)==typeid (MapCenterObj)))
insilmaril@420
  1751
	{			
insilmaril@420
  1752
		LinkableMapObj *dstPar=dst->getParObj();
insilmaril@420
  1753
		QString parString=dstPar->getSelectString();
insilmaril@420
  1754
		if (typeid(*selection)==typeid(FloatImageObj)) 
insilmaril@420
  1755
		{
insilmaril@420
  1756
			FloatImageObj *fio=(FloatImageObj*)selection;
insilmaril@420
  1757
			QString fioPreSelectString=fio->getSelectString();
insilmaril@420
  1758
			QString fioPreParentSelectString=fio->getParObj()->getSelectString();
insilmaril@420
  1759
			((BranchObj*)(dst))->addFloatImage (fio);
insilmaril@420
  1760
			fio->unselect();
insilmaril@420
  1761
			((BranchObj*)(fio->getParObj()))->removeFloatImage (fio);
insilmaril@420
  1762
			fio=((BranchObj*)(dst))->getLastFloatImage();
insilmaril@420
  1763
			fio->setRelPos();
insilmaril@420
  1764
			fio->reposition();
insilmaril@420
  1765
			selection=(LinkableMapObj*)fio;
insilmaril@420
  1766
			selection->select();
insilmaril@420
  1767
			saveState(
insilmaril@420
  1768
				fio->getSelectString(),
insilmaril@420
  1769
				QString("linkTo (\"%1\")").arg(fioPreParentSelectString), 
insilmaril@420
  1770
				fioPreSelectString, 
insilmaril@420
  1771
				QString ("linkTo (\"%1\")").arg(dstString),
insilmaril@420
  1772
				QString ("Link floatimage to %1").arg(getName(dst)));
insilmaril@420
  1773
		}	
insilmaril@420
  1774
	}
insilmaril@420
  1775
}
insilmaril@420
  1776
insilmaril@366
  1777
QString MapEditor::getHeading(bool &ok, QPoint &p)
insilmaril@0
  1778
{
insilmaril@366
  1779
	if (selection  &&  
insilmaril@366
  1780
		 (typeid(*selection) == typeid(BranchObj) || 
insilmaril@366
  1781
		  typeid(*selection) == typeid(MapCenterObj) ) ) 
insilmaril@366
  1782
	{
insilmaril@366
  1783
		ok=true;
insilmaril@408
  1784
		p=mapFromScene(selection->getAbsPos());
insilmaril@366
  1785
		return ((BranchObj*)selection)->getHeading();
insilmaril@366
  1786
	}
insilmaril@366
  1787
	ok=false;
insilmaril@366
  1788
	return QString();
insilmaril@366
  1789
}
insilmaril@366
  1790
insilmaril@366
  1791
void MapEditor::setHeading(const QString &s)
insilmaril@366
  1792
{
insilmaril@0
  1793
	if (selection  &&  
insilmaril@0
  1794
		 (typeid(*selection) == typeid(BranchObj) || 
insilmaril@0
  1795
		  typeid(*selection) == typeid(MapCenterObj) ) ) 
insilmaril@0
  1796
	{
insilmaril@163
  1797
		editingBO=(BranchObj*)selection;
insilmaril@395
  1798
		saveState(
insilmaril@395
  1799
			selection,
insilmaril@366
  1800
			"setHeading (\""+editingBO->getHeading()+"\")", 
insilmaril@395
  1801
			selection,
insilmaril@366
  1802
			"setHeading (\""+s+"\")", 
insilmaril@366
  1803
			QString("Set heading of %1 to \"%2\"").arg(getName(editingBO)).arg(s) );
insilmaril@366
  1804
		editingBO->setHeading(s );
insilmaril@366
  1805
		editingBO=NULL;
insilmaril@106
  1806
		mapCenter->reposition();
insilmaril@106
  1807
		ensureSelectionVisible();
insilmaril@106
  1808
	}
insilmaril@106
  1809
}
insilmaril@106
  1810
insilmaril@366
  1811
void MapEditor::setURLInt (const QString &s)
insilmaril@143
  1812
{
insilmaril@143
  1813
	// Internal function, no saveState needed
insilmaril@143
  1814
	if (selection  &&  
insilmaril@143
  1815
		 (typeid(*selection) == typeid(BranchObj) || 
insilmaril@143
  1816
		  typeid(*selection) == typeid(MapCenterObj) ) ) 
insilmaril@143
  1817
	{
insilmaril@163
  1818
		((BranchObj*)selection)->setURL(s);
insilmaril@143
  1819
		mapCenter->reposition();
insilmaril@143
  1820
		ensureSelectionVisible();
insilmaril@143
  1821
	}
insilmaril@143
  1822
}
insilmaril@143
  1823
insilmaril@366
  1824
void MapEditor::setHeadingInt(const QString &s)
insilmaril@366
  1825
{
insilmaril@366
  1826
	if (selection  &&  
insilmaril@366
  1827
		 (typeid(*selection) == typeid(BranchObj) || 
insilmaril@366
  1828
		  typeid(*selection) == typeid(MapCenterObj) ) ) 
insilmaril@366
  1829
	{
insilmaril@366
  1830
		((BranchObj*)selection)->setHeading(s);
insilmaril@366
  1831
		mapCenter->reposition();
insilmaril@366
  1832
		ensureSelectionVisible();
insilmaril@366
  1833
	}
insilmaril@366
  1834
}
insilmaril@366
  1835
insilmaril@366
  1836
void MapEditor::setVymLinkInt (const QString &s)
insilmaril@143
  1837
{
insilmaril@143
  1838
	// Internal function, no saveState needed
insilmaril@143
  1839
	if (selection  &&  
insilmaril@143
  1840
		 (typeid(*selection) == typeid(BranchObj) || 
insilmaril@143
  1841
		  typeid(*selection) == typeid(MapCenterObj) ) ) 
insilmaril@143
  1842
	{
insilmaril@163
  1843
		((BranchObj*)selection)->setVymLink(s);
insilmaril@143
  1844
		mapCenter->reposition();
insilmaril@143
  1845
		ensureSelectionVisible();
insilmaril@143
  1846
	}
insilmaril@143
  1847
}
insilmaril@143
  1848
insilmaril@366
  1849
BranchObj* MapEditor::addNewBranchInt(int num)
insilmaril@0
  1850
{
insilmaril@366
  1851
	// Depending on pos:
insilmaril@366
  1852
	// -3		insert in childs of parent  above selection 
insilmaril@366
  1853
	// -2		add branch to selection 
insilmaril@366
  1854
	// -1		insert in childs of parent below selection 
insilmaril@366
  1855
	// 0..n		insert in childs of parent at pos
insilmaril@366
  1856
	BranchObj *newbo=NULL;
insilmaril@0
  1857
	if (selection  &&  
insilmaril@0
  1858
		 (typeid(*selection) == typeid(BranchObj) || 
insilmaril@0
  1859
		  typeid(*selection) == typeid(MapCenterObj) ) ) 
insilmaril@0
  1860
	{
insilmaril@366
  1861
		BranchObj* bo = (BranchObj*) selection;
insilmaril@366
  1862
		if (num==-2)
insilmaril@0
  1863
		{
insilmaril@0
  1864
			// save scroll state. If scrolled, automatically select
insilmaril@0
  1865
			// new branch in order to tmp unscroll parent...
insilmaril@366
  1866
			return bo->addBranch();
insilmaril@366
  1867
			
insilmaril@366
  1868
		}else if (num==-1)
insilmaril@0
  1869
		{
insilmaril@366
  1870
			num=bo->getNum()+1;
insilmaril@366
  1871
			bo=(BranchObj*)bo->getParObj();
insilmaril@366
  1872
		}else if (num==-3)
insilmaril@366
  1873
		{
insilmaril@366
  1874
			num=bo->getNum();
insilmaril@366
  1875
			bo=(BranchObj*)bo->getParObj();
insilmaril@366
  1876
		}
insilmaril@366
  1877
		if (!bo) return bo;
insilmaril@366
  1878
		newbo=bo->insertBranch(num);
insilmaril@366
  1879
	}	
insilmaril@366
  1880
	return newbo;
insilmaril@366
  1881
}	
insilmaril@366
  1882
insilmaril@394
  1883
BranchObj* MapEditor::addNewBranch(int pos)
insilmaril@366
  1884
{
insilmaril@366
  1885
	// Different meaning than num in addNewBranchInt!
insilmaril@366
  1886
	// -1	add above
insilmaril@366
  1887
	//  0	add as child
insilmaril@366
  1888
	// +1	add below
insilmaril@394
  1889
	BranchObj *bo = (BranchObj*) selection;
insilmaril@394
  1890
	BranchObj *newbo=NULL;
insilmaril@394
  1891
insilmaril@366
  1892
	if (selection  &&  
insilmaril@366
  1893
		 (typeid(*selection) == typeid(BranchObj) || 
insilmaril@366
  1894
		  typeid(*selection) == typeid(MapCenterObj) ) ) 
insilmaril@366
  1895
	{
insilmaril@421
  1896
		setCursor (Qt::ArrowCursor);
insilmaril@421
  1897
insilmaril@366
  1898
		newbo=addNewBranchInt (pos-2);
insilmaril@366
  1899
insilmaril@366
  1900
		if (newbo)
insilmaril@366
  1901
		{
insilmaril@395
  1902
			saveState(
insilmaril@417
  1903
				newbo,		
insilmaril@393
  1904
				"delete ()",
insilmaril@395
  1905
				selection,
insilmaril@366
  1906
				QString ("addBranch (%1)").arg(pos-2),
insilmaril@395
  1907
				QString ("Add new branch to %1").arg(getName(bo)));	
insilmaril@366
  1908
insilmaril@366
  1909
			mapCenter->reposition();
insilmaril@366
  1910
		}
insilmaril@366
  1911
	}	
insilmaril@394
  1912
	return newbo;
insilmaril@366
  1913
}
insilmaril@366
  1914
insilmaril@366
  1915
insilmaril@394
  1916
BranchObj* MapEditor::addNewBranchBefore()
insilmaril@366
  1917
{
insilmaril@394
  1918
	BranchObj *newbo=NULL;
insilmaril@366
  1919
	if (selection  &&  
insilmaril@366
  1920
		 (typeid(*selection) == typeid(BranchObj) ) )
insilmaril@394
  1921
		 // We accept no MapCenterObj here, so we _have_ a parent
insilmaril@366
  1922
	{
insilmaril@394
  1923
		BranchObj* bo = (BranchObj*) selection;
insilmaril@408
  1924
		QPointF p=bo->getRelPos();
insilmaril@394
  1925
insilmaril@394
  1926
insilmaril@366
  1927
		BranchObj *parbo=(BranchObj*)(selection->getParObj());
insilmaril@394
  1928
insilmaril@394
  1929
		// add below selection
insilmaril@394
  1930
		newbo=parbo->insertBranch(bo->getNum()+1);
insilmaril@394
  1931
		if (newbo)
insilmaril@366
  1932
		{
insilmaril@394
  1933
			newbo->move2RelPos (p);
insilmaril@394
  1934
insilmaril@394
  1935
			// Move selection to new branch
insilmaril@421
  1936
			((BranchObj*)selection)->linkTo (newbo,-1);
insilmaril@394
  1937
insilmaril@394
  1938
			saveState (newbo, "deleteKeepChilds ()", newbo, "addBranchBefore ()", 
insilmaril@394
  1939
				QString ("Add branch before %1").arg(getName(bo)));
insilmaril@394
  1940
insilmaril@394
  1941
			mapCenter->reposition();
insilmaril@394
  1942
		}
insilmaril@0
  1943
	}	
insilmaril@394
  1944
	return newbo;
insilmaril@0
  1945
}
insilmaril@0
  1946
insilmaril@0
  1947
void MapEditor::deleteSelection()
insilmaril@0
  1948
{
insilmaril@0
  1949
	if (selection  && typeid(*selection) ==typeid(BranchObj) ) 
insilmaril@0
  1950
	{
insilmaril@366
  1951
		BranchObj* bo=(BranchObj*)selection;
insilmaril@294
  1952
		BranchObj* par=(BranchObj*)(bo->getParObj());
insilmaril@294
  1953
		bo->unselect();
insilmaril@394
  1954
		saveStateRemovingPart (bo, QString ("Delete %1").arg(getName(bo)));
insilmaril@0
  1955
		selection=NULL;
insilmaril@0
  1956
		par->removeBranch(bo);
insilmaril@0
  1957
		selection=par;
insilmaril@0
  1958
		selection->select();
insilmaril@0
  1959
		ensureSelectionVisible();
insilmaril@0
  1960
		mapCenter->reposition();
insilmaril@0
  1961
	}
insilmaril@0
  1962
	if (selection  && typeid(*selection) ==typeid(FloatImageObj) ) 
insilmaril@0
  1963
	{
insilmaril@366
  1964
		FloatImageObj* fio=(FloatImageObj*)selection;
insilmaril@0
  1965
		BranchObj* par=(BranchObj*)(fio->getParObj());
insilmaril@395
  1966
		saveStateChangingPart(
insilmaril@395
  1967
			par, 
insilmaril@395
  1968
			fio,
insilmaril@395
  1969
			"delete ()",
insilmaril@395
  1970
			QString("Delete %1").arg(getName(fio))
insilmaril@395
  1971
		);
insilmaril@0
  1972
		fio->unselect();
insilmaril@0
  1973
		selection=NULL;
insilmaril@0
  1974
		par->removeFloatImage(fio);
insilmaril@0
  1975
		selection=par;
insilmaril@0
  1976
		selection->select();
insilmaril@0
  1977
		ensureSelectionVisible();
insilmaril@0
  1978
		mapCenter->reposition();
insilmaril@0
  1979
	}
insilmaril@421
  1980
	mapScene->update();
insilmaril@0
  1981
}
insilmaril@0
  1982
insilmaril@0
  1983
LinkableMapObj* MapEditor::getSelection()
insilmaril@0
  1984
{
insilmaril@0
  1985
	return selection;
insilmaril@0
  1986
}
insilmaril@0
  1987
insilmaril@267
  1988
void MapEditor::unselect()
insilmaril@267
  1989
{
insilmaril@267
  1990
	if (selection) 
insilmaril@267
  1991
	{
insilmaril@267
  1992
		selectionLast=selection;
insilmaril@267
  1993
		selection->unselect();
insilmaril@267
  1994
		selection=NULL;
insilmaril@267
  1995
	}
insilmaril@267
  1996
}	
insilmaril@267
  1997
insilmaril@267
  1998
void MapEditor::reselect()
insilmaril@267
  1999
{
insilmaril@267
  2000
	if (selectionLast)
insilmaril@267
  2001
	{
insilmaril@267
  2002
		selection=selectionLast;
insilmaril@267
  2003
		selection->select();
insilmaril@267
  2004
		selectionLast=NULL;
insilmaril@267
  2005
	}
insilmaril@267
  2006
}	
insilmaril@267
  2007
insilmaril@83
  2008
bool MapEditor::select (const QString &s)
insilmaril@83
  2009
{
insilmaril@106
  2010
	LinkableMapObj *lmo=mapCenter->findObjBySelect(s);
insilmaril@0
  2011
insilmaril@0
  2012
	// Finally select the found object
insilmaril@0
  2013
	if (lmo)
insilmaril@0
  2014
	{
insilmaril@98
  2015
		if (selection) unselect();
insilmaril@0
  2016
		selection=lmo;
insilmaril@0
  2017
		selection->select();
insilmaril@0
  2018
		ensureSelectionVisible();
insilmaril@0
  2019
		return true;
insilmaril@139
  2020
	} 
insilmaril@139
  2021
	return false;
insilmaril@0
  2022
}
insilmaril@0
  2023
insilmaril@366
  2024
QString MapEditor::getSelectString()
insilmaril@366
  2025
{
insilmaril@366
  2026
	if (selection) return selection->getSelectString();
insilmaril@366
  2027
	return QString();
insilmaril@366
  2028
}
insilmaril@366
  2029
insilmaril@366
  2030
void MapEditor::selectInt (LinkableMapObj *lmo)
insilmaril@0
  2031
{
insilmaril@267
  2032
	if (lmo && selection != lmo)
insilmaril@0
  2033
	{
insilmaril@267
  2034
		// select the MapObj
insilmaril@267
  2035
		if (selection) selection->unselect();
insilmaril@267
  2036
		selection=lmo;
insilmaril@267
  2037
		selection->select();
insilmaril@0
  2038
	}
insilmaril@267
  2039
}
insilmaril@0
  2040
insilmaril@366
  2041
void MapEditor::selectNextBranchInt()
insilmaril@0
  2042
{
insilmaril@0
  2043
	// Increase number of branch
insilmaril@0
  2044
	if (selection)
insilmaril@0
  2045
	{
insilmaril@0
  2046
		QString s=selection->getSelectString();
insilmaril@0
  2047
		QString part;
insilmaril@0
  2048
		QString typ;
insilmaril@0
  2049
		QString num;
insilmaril@0
  2050
insilmaril@0
  2051
		// Where am I? 
insilmaril@0
  2052
		part=s.section(",",-1);
insilmaril@0
  2053
		typ=part.left (3);
insilmaril@0
  2054
		num=part.right(part.length() - 3);
insilmaril@0
  2055
insilmaril@0
  2056
		s=s.left (s.length() -num.length());
insilmaril@0
  2057
insilmaril@0
  2058
		// Go to next lmo
insilmaril@0
  2059
		num=QString ("%1").arg(num.toUInt()+1);
insilmaril@0
  2060
insilmaril@0
  2061
		s=s+num;
insilmaril@0
  2062
		
insilmaril@0
  2063
		// Try to select this one
insilmaril@0
  2064
		if (select (s)) return;
insilmaril@0
  2065
insilmaril@0
  2066
		// We have no direct successor, 
insilmaril@0
  2067
		// try to increase the parental number in order to
insilmaril@0
  2068
		// find a successor with same depth
insilmaril@0
  2069
insilmaril@0
  2070
		int d=selection->getDepth();
insilmaril@0
  2071
		int oldDepth=d;
insilmaril@0
  2072
		int i;
insilmaril@0
  2073
		bool found=false;
insilmaril@0
  2074
		bool b;
insilmaril@0
  2075
		while (!found && d>0)
insilmaril@0
  2076
		{
insilmaril@0
  2077
			s=s.section (",",0,d-1);
insilmaril@0
  2078
			// replace substring of current depth in s with "1"
insilmaril@0
  2079
			part=s.section(",",-1);
insilmaril@0
  2080
			typ=part.left (3);
insilmaril@0
  2081
			num=part.right(part.length() - 3);
insilmaril@0
  2082
insilmaril@0
  2083
			if (d>1)
insilmaril@0
  2084
			{	
insilmaril@0
  2085
				// increase number of parent
insilmaril@0
  2086
				num=QString ("%1").arg(num.toUInt()+1);
insilmaril@0
  2087
				s=s.section (",",0,d-2) + ","+ typ+num;
insilmaril@0
  2088
			} else
insilmaril@0
  2089
			{
insilmaril@0
  2090
				// Special case, look at orientation
insilmaril@0
  2091
				if (selection->getOrientation()==OrientRightOfCenter)
insilmaril@0
  2092
					num=QString ("%1").arg(num.toUInt()+1);
insilmaril@0
  2093
				else	
insilmaril@0
  2094
					num=QString ("%1").arg(num.toUInt()-1);
insilmaril@0
  2095
				s=typ+num;
insilmaril@0
  2096
			}	
insilmaril@0
  2097
insilmaril@0
  2098
			if (select (s))
insilmaril@0
  2099
				// pad to oldDepth, select the first branch for each depth
insilmaril@0
  2100
				for (i=d;i<oldDepth;i++)
insilmaril@0
  2101
				{
insilmaril@0
  2102
					b=select (s);
insilmaril@0
  2103
					if (b)
insilmaril@0
  2104
					{	
insilmaril@163
  2105
						if ( ((BranchObj*)selection)->countBranches()>0)
insilmaril@0
  2106
							s+=",bo:0";
insilmaril@0
  2107
						else	
insilmaril@0
  2108
							break;
insilmaril@0
  2109
					} else
insilmaril@0
  2110
						break;
insilmaril@0
  2111
				}	
insilmaril@0
  2112
insilmaril@0
  2113
			// try to select the freshly built string
insilmaril@0
  2114
			found=select(s);
insilmaril@0
  2115
			d--;
insilmaril@0
  2116
		}
insilmaril@0
  2117
		return;
insilmaril@0
  2118
	}	
insilmaril@0
  2119
}
insilmaril@0
  2120
insilmaril@366
  2121
void MapEditor::selectPrevBranchInt()
insilmaril@0
  2122
{
insilmaril@0
  2123
	// Decrease number of branch
insilmaril@0
  2124
	if (selection)
insilmaril@0
  2125
	{
insilmaril@0
  2126
		QString s=selection->getSelectString();
insilmaril@0
  2127
		QString part;
insilmaril@0
  2128
		QString typ;
insilmaril@0
  2129
		QString num;
insilmaril@0
  2130
insilmaril@0
  2131
		// Where am I? 
insilmaril@0
  2132
		part=s.section(",",-1);
insilmaril@0
  2133
		typ=part.left (3);
insilmaril@0
  2134
		num=part.right(part.length() - 3);
insilmaril@0
  2135
insilmaril@0
  2136
		s=s.left (s.length() -num.length());
insilmaril@0
  2137
insilmaril@411
  2138
		int n=num.toInt()-1;
insilmaril@411
  2139
		
insilmaril@0
  2140
		// Go to next lmo
insilmaril@411
  2141
		num=QString ("%1").arg(n);
insilmaril@0
  2142
		s=s+num;
insilmaril@0
  2143
		
insilmaril@0
  2144
		// Try to select this one
insilmaril@411
  2145
		if (n>=0 && select (s)) return;
insilmaril@0
  2146
insilmaril@0
  2147
		// We have no direct precessor, 
insilmaril@0
  2148
		// try to decrease the parental number in order to
insilmaril@0
  2149
		// find a precessor with same depth
insilmaril@0
  2150
insilmaril@0
  2151
		int d=selection->getDepth();
insilmaril@0
  2152
		int oldDepth=d;
insilmaril@0
  2153
		int i;
insilmaril@0
  2154
		bool found=false;
insilmaril@0
  2155
		bool b;
insilmaril@0
  2156
		while (!found && d>0)
insilmaril@0
  2157
		{
insilmaril@0
  2158
			s=s.section (",",0,d-1);
insilmaril@0
  2159
			// replace substring of current depth in s with "1"
insilmaril@0
  2160
			part=s.section(",",-1);
insilmaril@0
  2161
			typ=part.left (3);
insilmaril@0
  2162
			num=part.right(part.length() - 3);
insilmaril@0
  2163
insilmaril@0
  2164
			if (d>1)
insilmaril@0
  2165
			{
insilmaril@0
  2166
				// decrease number of parent
insilmaril@411
  2167
				num=QString ("%1").arg(num.toInt()-1);
insilmaril@0
  2168
				s=s.section (",",0,d-2) + ","+ typ+num;
insilmaril@0
  2169
			} else
insilmaril@0
  2170
			{
insilmaril@0
  2171
				// Special case, look at orientation
insilmaril@0
  2172
				if (selection->getOrientation()==OrientRightOfCenter)
insilmaril@411
  2173
					num=QString ("%1").arg(num.toInt()-1);
insilmaril@0
  2174
				else	
insilmaril@411
  2175
					num=QString ("%1").arg(num.toInt()+1);
insilmaril@0
  2176
				s=typ+num;
insilmaril@0
  2177
			}	
insilmaril@0
  2178
insilmaril@0
  2179
			if (select(s))
insilmaril@0
  2180
				// pad to oldDepth, select the last branch for each depth
insilmaril@0
  2181
				for (i=d;i<oldDepth;i++)
insilmaril@0
  2182
				{
insilmaril@0
  2183
					b=select (s);
insilmaril@0
  2184
					if (b)
insilmaril@163
  2185
						if ( ((BranchObj*)selection)->countBranches()>0)
insilmaril@163
  2186
							s+=",bo:"+ QString ("%1").arg( ((BranchObj*)selection)->countBranches()-1 );
insilmaril@0
  2187
						else	
insilmaril@0
  2188
							break;
insilmaril@0
  2189
					else
insilmaril@0
  2190
						break;
insilmaril@0
  2191
				}	
insilmaril@0
  2192
			
insilmaril@0
  2193
			// try to select the freshly built string
insilmaril@0
  2194
			found=select(s);
insilmaril@0
  2195
			d--;
insilmaril@0
  2196
		}
insilmaril@0
  2197
		return;
insilmaril@0
  2198
	}	
insilmaril@0
  2199
}
insilmaril@0
  2200
insilmaril@0
  2201
void MapEditor::selectUpperBranch()
insilmaril@0
  2202
{
insilmaril@0
  2203
	if (selection) 
insilmaril@0
  2204
	{
insilmaril@0
  2205
		if (typeid(*selection) == typeid(BranchObj))
insilmaril@0
  2206
		{
insilmaril@0
  2207
			if (selection->getOrientation()==OrientRightOfCenter)
insilmaril@366
  2208
				selectPrevBranchInt();
insilmaril@0
  2209
			else
insilmaril@0
  2210
				if (selection->getDepth()==1)
insilmaril@366
  2211
					selectNextBranchInt();
insilmaril@0
  2212
				else
insilmaril@366
  2213
					selectPrevBranchInt();
insilmaril@0
  2214
		}		
insilmaril@0
  2215
	}
insilmaril@0
  2216
}
insilmaril@0
  2217
insilmaril@0
  2218
void MapEditor::selectLowerBranch()
insilmaril@0
  2219
{
insilmaril@0
  2220
	if (selection) 
insilmaril@0
  2221
	{
insilmaril@0
  2222
		if (typeid(*selection) == typeid(BranchObj))
insilmaril@0
  2223
		{
insilmaril@0
  2224
			if (selection->getOrientation()==OrientRightOfCenter)
insilmaril@366
  2225
				selectNextBranchInt();
insilmaril@0
  2226
			else
insilmaril@0
  2227
				if (selection->getDepth()==1)
insilmaril@366
  2228
					selectPrevBranchInt();
insilmaril@0
  2229
				else
insilmaril@366
  2230
					selectNextBranchInt();
insilmaril@0
  2231
		}		
insilmaril@0
  2232
	}
insilmaril@0
  2233
}
insilmaril@0
  2234
insilmaril@0
  2235
insilmaril@0
  2236
void MapEditor::selectLeftBranch()
insilmaril@0
  2237
{
insilmaril@0
  2238
	BranchObj* bo;
insilmaril@0
  2239
	BranchObj* par;
insilmaril@0
  2240
	if (selection) 
insilmaril@0
  2241
	{
insilmaril@0
  2242
		if (typeid(*selection) == typeid(MapCenterObj))
insilmaril@0
  2243
		{
insilmaril@163
  2244
			par=  (BranchObj*) selection;
insilmaril@0
  2245
			bo=par->getLastSelectedBranch();
insilmaril@0
  2246
			if (bo)
insilmaril@0
  2247
			{
insilmaril@0
  2248
				// Workaround for reselecting on left and right side
insilmaril@0
  2249
				if (bo->getOrientation()==OrientRightOfCenter)
insilmaril@0
  2250
				{
insilmaril@0
  2251
					bo=par->getLastBranch();
insilmaril@0
  2252
				}	
insilmaril@0
  2253
				if (bo)
insilmaril@0
  2254
				{
insilmaril@0
  2255
					par->unselect();
insilmaril@0
  2256
					selection=bo;
insilmaril@0
  2257
					selection->select();
insilmaril@0
  2258
					ensureSelectionVisible();
insilmaril@0
  2259
				}
insilmaril@0
  2260
			}	
insilmaril@0
  2261
		} else
insilmaril@0
  2262
		{
insilmaril@0
  2263
			par=(BranchObj*)(selection->getParObj());
insilmaril@0
  2264
			if (selection->getOrientation()==OrientRightOfCenter)
insilmaril@0
  2265
			{
insilmaril@0
  2266
				if (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2267
					typeid(*selection) == typeid(FloatImageObj))
insilmaril@0
  2268
				{
insilmaril@0
  2269
					selection->unselect();
insilmaril@0
  2270
					selection=par;
insilmaril@0
  2271
					selection->select();
insilmaril@0
  2272
					ensureSelectionVisible();
insilmaril@0
  2273
				}
insilmaril@0
  2274
			} else
insilmaril@0
  2275
			{
insilmaril@0
  2276
				if (typeid(*selection) == typeid(BranchObj) )
insilmaril@0
  2277
				{
insilmaril@163
  2278
					bo=((BranchObj*)selection)->getLastSelectedBranch();
insilmaril@0
  2279
					if (bo) 
insilmaril@0
  2280
					{
insilmaril@0
  2281
						selection->unselect();
insilmaril@0
  2282
						selection=bo;
insilmaril@0
  2283
						selection->select();
insilmaril@0
  2284
						ensureSelectionVisible();
insilmaril@0
  2285
					}
insilmaril@0
  2286
				}
insilmaril@0
  2287
			}
insilmaril@0
  2288
		}	
insilmaril@0
  2289
	}
insilmaril@0
  2290
}
insilmaril@0
  2291
insilmaril@0
  2292
void MapEditor::selectRightBranch()
insilmaril@0
  2293
{
insilmaril@0
  2294
	BranchObj* bo;
insilmaril@0
  2295
	BranchObj* par;
insilmaril@0
  2296
insilmaril@0
  2297
	if (selection) 
insilmaril@0
  2298
	{
insilmaril@0
  2299
		if (typeid(*selection) == typeid(MapCenterObj))
insilmaril@0
  2300
		{
insilmaril@163
  2301
			par=  (BranchObj*) selection;
insilmaril@0
  2302
			bo=par->getLastSelectedBranch();
insilmaril@0
  2303
			if (bo)
insilmaril@0
  2304
			{
insilmaril@366
  2305
				// Workaround for reselecting on left and right side
insilmaril@0
  2306
				if (bo->getOrientation()==OrientLeftOfCenter)
insilmaril@0
  2307
					bo=par->getFirstBranch();
insilmaril@0
  2308
				if (bo)
insilmaril@0
  2309
				{
insilmaril@0
  2310
					par->unselect();
insilmaril@0
  2311
					selection=bo;
insilmaril@0
  2312
					selection->select();
insilmaril@0
  2313
					ensureSelectionVisible();
insilmaril@0
  2314
				}
insilmaril@0
  2315
			}
insilmaril@0
  2316
		} else
insilmaril@0
  2317
		{
insilmaril@0
  2318
			par=(BranchObj*)(selection->getParObj());
insilmaril@0
  2319
			if (selection->getOrientation()==OrientLeftOfCenter)
insilmaril@0
  2320
			{
insilmaril@0
  2321
				if (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2322
					typeid(*selection) == typeid(FloatImageObj))
insilmaril@0
  2323
				{
insilmaril@0
  2324
					selection->unselect();
insilmaril@0
  2325
					selection=par;
insilmaril@0
  2326
					selection->select();
insilmaril@0
  2327
					ensureSelectionVisible();
insilmaril@0
  2328
				}
insilmaril@0
  2329
			} else
insilmaril@0
  2330
			{
insilmaril@0
  2331
				if (typeid(*selection) == typeid(BranchObj) )
insilmaril@0
  2332
				{
insilmaril@163
  2333
					bo=((BranchObj*)selection)->getLastSelectedBranch();
insilmaril@0
  2334
					if (bo) 
insilmaril@0
  2335
					{
insilmaril@0
  2336
						selection->unselect();
insilmaril@0
  2337
						selection=bo;
insilmaril@0
  2338
						selection->select();
insilmaril@0
  2339
						ensureSelectionVisible();
insilmaril@0
  2340
					}
insilmaril@0
  2341
				}
insilmaril@0
  2342
			}
insilmaril@0
  2343
		}
insilmaril@0
  2344
	}
insilmaril@0
  2345
}
insilmaril@0
  2346
insilmaril@0
  2347
void MapEditor::selectFirstBranch()
insilmaril@0
  2348
{
insilmaril@0
  2349
	BranchObj *bo1;
insilmaril@0
  2350
	BranchObj *bo2;
insilmaril@0
  2351
	BranchObj* par;
insilmaril@0
  2352
	if (selection) {
insilmaril@0
  2353
		if (typeid(*selection) == typeid(BranchObj))
insilmaril@0
  2354
		{
insilmaril@163
  2355
			bo1=  (BranchObj*) selection;
insilmaril@0
  2356
			par=(BranchObj*)(bo1->getParObj());
insilmaril@0
  2357
			bo2=par->getFirstBranch();
insilmaril@0
  2358
			if (bo2) {
insilmaril@0
  2359
				bo1->unselect();
insilmaril@0
  2360
				selection=bo2;
insilmaril@0
  2361
				selection->select();
insilmaril@0
  2362
				ensureSelectionVisible();
insilmaril@0
  2363
			}
insilmaril@0
  2364
		}		
insilmaril@0
  2365
	}
insilmaril@0
  2366
}
insilmaril@0
  2367
insilmaril@0
  2368
void MapEditor::selectLastBranch()
insilmaril@0
  2369
{
insilmaril@0
  2370
	BranchObj *bo1;
insilmaril@0
  2371
	BranchObj *bo2;
insilmaril@0
  2372
	BranchObj* par;
insilmaril@0
  2373
	if (selection) {
insilmaril@0
  2374
		if (typeid(*selection) == typeid(BranchObj))
insilmaril@0
  2375
		{
insilmaril@163
  2376
			bo1=  (BranchObj*) selection;
insilmaril@0
  2377
			par=(BranchObj*)(bo1->getParObj());
insilmaril@0
  2378
			bo2=par->getLastBranch();
insilmaril@0
  2379
			if (bo2) {
insilmaril@0
  2380
				bo1->unselect();
insilmaril@0
  2381
				selection=bo2;
insilmaril@0
  2382
				selection->select();
insilmaril@0
  2383
				ensureSelectionVisible();
insilmaril@0
  2384
			}
insilmaril@0
  2385
		}		
insilmaril@0
  2386
	}
insilmaril@0
  2387
}
insilmaril@0
  2388
insilmaril@408
  2389
void MapEditor::selectMapBackgroundImage ()
insilmaril@408
  2390
{
insilmaril@408
  2391
	Q3FileDialog *fd=new Q3FileDialog( this);
insilmaril@408
  2392
	fd->setMode (Q3FileDialog::ExistingFile);
insilmaril@408
  2393
	fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
insilmaril@408
  2394
	ImagePreview *p =new ImagePreview (fd);
insilmaril@408
  2395
	fd->setContentsPreviewEnabled( TRUE );
insilmaril@408
  2396
	fd->setContentsPreview( p, p );
insilmaril@408
  2397
	fd->setPreviewMode( Q3FileDialog::Contents );
insilmaril@408
  2398
	fd->setCaption(vymName+" - " +tr("Load background image"));
insilmaril@408
  2399
	fd->setDir (lastImageDir);
insilmaril@408
  2400
	fd->show();
insilmaril@408
  2401
insilmaril@408
  2402
	if ( fd->exec() == QDialog::Accepted )
insilmaril@408
  2403
	{
insilmaril@412
  2404
		// TODO selectMapBackgroundImg in QT4 use:	lastImageDir=fd->directory();
insilmaril@408
  2405
		lastImageDir=QDir (fd->dirPath());
insilmaril@408
  2406
		setMapBackgroundImage (fd->selectedFile());
insilmaril@408
  2407
	}
insilmaril@408
  2408
}	
insilmaril@408
  2409
insilmaril@408
  2410
void MapEditor::setMapBackgroundImage (const QString &fn)
insilmaril@408
  2411
{
insilmaril@408
  2412
	QColor oldcol=mapScene->backgroundBrush().color();
insilmaril@408
  2413
	/*
insilmaril@408
  2414
	saveState(
insilmaril@408
  2415
		selection,
insilmaril@408
  2416
		QString ("setMapBackgroundImage (%1)").arg(oldcol.name()),
insilmaril@408
  2417
		selection,
insilmaril@408
  2418
		QString ("setMapBackgroundImage (%1)").arg(col.name()),
insilmaril@408
  2419
		QString("Set background color of map to %1").arg(col.name()));
insilmaril@408
  2420
	*/	
insilmaril@408
  2421
	cout << "Trying to load "<<fn.ascii()<<endl;
insilmaril@408
  2422
	QBrush brush;
insilmaril@408
  2423
	brush.setTextureImage (QPixmap (fn));
insilmaril@408
  2424
	mapScene->setBackgroundBrush(brush);
insilmaril@408
  2425
}
insilmaril@408
  2426
insilmaril@398
  2427
void MapEditor::selectMapBackgroundColor()
insilmaril@0
  2428
{
insilmaril@408
  2429
	QColor col = QColorDialog::getColor( mapScene->backgroundBrush().color(), this );
insilmaril@0
  2430
	if ( !col.isValid() ) return;
insilmaril@408
  2431
	setMapBackgroundColor( col );
insilmaril@0
  2432
}
insilmaril@0
  2433
insilmaril@398
  2434
insilmaril@398
  2435
void MapEditor::setMapBackgroundColor(QColor col)
insilmaril@0
  2436
{
insilmaril@408
  2437
	QColor oldcol=mapScene->backgroundBrush().color();
insilmaril@398
  2438
	saveState(
insilmaril@398
  2439
		selection,
insilmaril@398
  2440
		QString ("setMapBackgroundColor (%1)").arg(oldcol.name()),
insilmaril@398
  2441
		selection,
insilmaril@398
  2442
		QString ("setMapBackgroundColor (%1)").arg(col.name()),
insilmaril@398
  2443
		QString("Set background color of map to %1").arg(col.name()));
insilmaril@408
  2444
	mapScene->setBackgroundBrush(col);
insilmaril@398
  2445
}
insilmaril@398
  2446
insilmaril@398
  2447
QColor MapEditor::getMapBackgroundColor()
insilmaril@398
  2448
{
insilmaril@408
  2449
    return mapScene->backgroundBrush().color();
insilmaril@0
  2450
}
insilmaril@0
  2451
insilmaril@395
  2452
QColor MapEditor::getCurrentHeadingColor()
insilmaril@0
  2453
{
insilmaril@0
  2454
	if (selection) 
insilmaril@0
  2455
	{
insilmaril@0
  2456
		if (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2457
			typeid(*selection) == typeid(MapCenterObj))
insilmaril@0
  2458
		{
insilmaril@163
  2459
			BranchObj *bo=(BranchObj*)selection;
insilmaril@395
  2460
			return bo->getColor(); 
insilmaril@0
  2461
		}    
insilmaril@0
  2462
	}
insilmaril@395
  2463
	
insilmaril@395
  2464
	QMessageBox::warning(0,tr("Warning"),tr("Can't get color of heading,\nthere's no branch selected"));
insilmaril@395
  2465
	return Qt::black;
insilmaril@0
  2466
}
insilmaril@0
  2467
insilmaril@417
  2468
void MapEditor::colorBranch (QColor c)
insilmaril@0
  2469
{
insilmaril@0
  2470
	if (selection) 
insilmaril@0
  2471
	{
insilmaril@0
  2472
		if (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2473
			typeid(*selection) == typeid(MapCenterObj))
insilmaril@0
  2474
		{
insilmaril@163
  2475
			BranchObj *bo=(BranchObj*)selection;
insilmaril@398
  2476
			saveState(
insilmaril@395
  2477
				selection, 
insilmaril@417
  2478
				QString ("colorBranch (%1)").arg(bo->getColor().name()),
insilmaril@395
  2479
				selection,
insilmaril@417
  2480
				QString ("colorBranch (%1)").arg(c.name()),
insilmaril@395
  2481
				QString("Set color of %1 to %2").arg(getName(bo)).arg(c.name())
insilmaril@398
  2482
			);	
insilmaril@395
  2483
			bo->setColor(c); // color branch
insilmaril@0
  2484
		}    
insilmaril@0
  2485
	}
insilmaril@0
  2486
}
insilmaril@0
  2487
insilmaril@417
  2488
void MapEditor::colorSubtree (QColor c)
insilmaril@0
  2489
{
insilmaril@0
  2490
	if (selection) 
insilmaril@0
  2491
	{
insilmaril@0
  2492
		if (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2493
			typeid(*selection) == typeid(MapCenterObj))
insilmaril@0
  2494
		{
insilmaril@163
  2495
			BranchObj *bo=(BranchObj*)selection;
insilmaril@395
  2496
			saveStateChangingPart(
insilmaril@395
  2497
				selection, 
insilmaril@395
  2498
				selection,
insilmaril@417
  2499
				QString ("colorSubtree (%1)").arg(c.name()),
insilmaril@395
  2500
				QString ("Set color of %1 and childs to %2").arg(getName(bo)).arg(c.name())
insilmaril@395
  2501
			);	
insilmaril@417
  2502
			bo->setColorSubtree (c); // color links, color childs
insilmaril@0
  2503
		}    
insilmaril@0
  2504
	}
insilmaril@0
  2505
}
insilmaril@0
  2506
insilmaril@0
  2507
insilmaril@0
  2508
void MapEditor::toggleStandardFlag(QString f)
insilmaril@0
  2509
{
insilmaril@0
  2510
	if (selection)
insilmaril@0
  2511
	{
insilmaril@366
  2512
		if (typeid(*selection) == typeid(BranchObj) ||
insilmaril@366
  2513
			typeid(*selection) == typeid(MapCenterObj))
insilmaril@366
  2514
		{
insilmaril@366
  2515
			BranchObj *bo=(BranchObj*)selection;
insilmaril@366
  2516
			QString u,r;
insilmaril@366
  2517
			if (bo->isSetStandardFlag(f))
insilmaril@366
  2518
			{
insilmaril@366
  2519
				r="unsetFlag";
insilmaril@366
  2520
				u="setFlag";
insilmaril@366
  2521
			}	
insilmaril@366
  2522
			else
insilmaril@366
  2523
			{
insilmaril@366
  2524
				u="unsetFlag";
insilmaril@366
  2525
				r="setFlag";
insilmaril@366
  2526
			}	
insilmaril@395
  2527
			saveState(
insilmaril@395
  2528
				selection,
insilmaril@395
  2529
				QString("%1 (\"%2\")").arg(u).arg(f), 
insilmaril@395
  2530
				selection,
insilmaril@366
  2531
				QString("%1 (\"%2\")").arg(r).arg(f),
insilmaril@366
  2532
				QString("Toggling standard flag \"%1\" of %2").arg(f).arg(getName(bo)));
insilmaril@390
  2533
			bo->toggleStandardFlag (f,mainWindow->useFlagGroups());
insilmaril@366
  2534
		}
insilmaril@0
  2535
	}	
insilmaril@0
  2536
}
insilmaril@0
  2537
insilmaril@0
  2538
insilmaril@0
  2539
BranchObj* MapEditor::findText (QString s, bool cs)
insilmaril@0
  2540
{
insilmaril@366
  2541
	QTextDocument::FindFlags flags=0;
insilmaril@366
  2542
	if (cs) flags=QTextDocument::FindCaseSensitively;
insilmaril@366
  2543
insilmaril@0
  2544
	if (!itFind) 
insilmaril@0
  2545
	{	// Nothing found or new find process
insilmaril@0
  2546
		if (EOFind)
insilmaril@0
  2547
			// nothing found, start again
insilmaril@0
  2548
			EOFind=false;
insilmaril@0
  2549
		itFind=mapCenter->first();
insilmaril@0
  2550
	}	
insilmaril@0
  2551
	bool searching=true;
insilmaril@0
  2552
	bool foundNote=false;
insilmaril@0
  2553
	while (searching && !EOFind)
insilmaril@0
  2554
	{
insilmaril@0
  2555
		if (itFind)
insilmaril@0
  2556
		{
insilmaril@0
  2557
			// Searching in Note
insilmaril@0
  2558
			if (itFind->getNote().contains(s,cs))
insilmaril@0
  2559
			{
insilmaril@0
  2560
				if (selection!=itFind) 
insilmaril@0
  2561
				{
insilmaril@163
  2562
					if (selection) ((BranchObj*)selection)->unselect();
insilmaril@0
  2563
					selection=itFind;
insilmaril@0
  2564
					selection->select();
insilmaril@0
  2565
					ensureSelectionVisible();
insilmaril@0
  2566
				}
insilmaril@366
  2567
				if (textEditor->findText(s,flags)) 
insilmaril@0
  2568
				{
insilmaril@0
  2569
					searching=false;
insilmaril@0
  2570
					foundNote=true;
insilmaril@0
  2571
				}	
insilmaril@0
  2572
			}
insilmaril@0
  2573
			// Searching in Heading
insilmaril@0
  2574
			if (searching && itFind->getHeading().contains (s,cs) ) 
insilmaril@0
  2575
			{
insilmaril@163
  2576
				if (selection) ((BranchObj*)selection)->unselect();
insilmaril@0
  2577
				selection=itFind;
insilmaril@0
  2578
				selection->select();
insilmaril@0
  2579
				ensureSelectionVisible();
insilmaril@0
  2580
				searching=false;
insilmaril@0
  2581
			}
insilmaril@0
  2582
		}	
insilmaril@0
  2583
		if (!foundNote)
insilmaril@0
  2584
		{
insilmaril@0
  2585
			itFind=itFind->next();
insilmaril@0
  2586
			if (!itFind) EOFind=true;
insilmaril@0
  2587
		}
insilmaril@0
  2588
	}	
insilmaril@0
  2589
	if (!searching)
insilmaril@0
  2590
	{
insilmaril@163
  2591
		return (BranchObj*)selection;
insilmaril@0
  2592
	}	else
insilmaril@0
  2593
		return NULL;
insilmaril@0
  2594
}
insilmaril@0
  2595
insilmaril@0
  2596
void MapEditor::findReset()
insilmaril@0
  2597
{	// Necessary if text to find changes during a find process
insilmaril@0
  2598
	itFind=NULL;
insilmaril@0
  2599
	EOFind=false;
insilmaril@0
  2600
}
insilmaril@395
  2601
void MapEditor::setURL(const QString &url)
insilmaril@395
  2602
{
insilmaril@395
  2603
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@395
  2604
			typeid(*selection) == typeid(MapCenterObj)) )
insilmaril@395
  2605
	{		
insilmaril@395
  2606
		BranchObj *bo=(BranchObj*)selection;
insilmaril@395
  2607
		QString oldurl=bo->getURL();
insilmaril@395
  2608
		bo->setURL (url);
insilmaril@395
  2609
		saveState (
insilmaril@395
  2610
			selection,
insilmaril@395
  2611
			QString ("setURL (\"%1\")").arg(oldurl),
insilmaril@395
  2612
			selection,
insilmaril@395
  2613
			QString ("setURL (\"%1\")").arg(url),
insilmaril@395
  2614
			QString ("set URL of %1 to %2").arg(getName(bo)).arg(url)
insilmaril@395
  2615
		);
insilmaril@395
  2616
		updateActions();
insilmaril@395
  2617
	}
insilmaril@395
  2618
}	
insilmaril@0
  2619
insilmaril@0
  2620
void MapEditor::editURL()
insilmaril@0
  2621
{
insilmaril@0
  2622
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2623
			typeid(*selection) == typeid(MapCenterObj)) )
insilmaril@0
  2624
	{		
insilmaril@0
  2625
		bool ok;
insilmaril@163
  2626
		BranchObj *bo=(BranchObj*)selection;
insilmaril@0
  2627
		QString text = QInputDialog::getText(
insilmaril@0
  2628
				"VYM", tr("Enter URL:"), QLineEdit::Normal,
insilmaril@143
  2629
				bo->getURL(), &ok, this );
insilmaril@0
  2630
		if ( ok) 
insilmaril@0
  2631
			// user entered something and pressed OK
insilmaril@395
  2632
			setURL (text);
insilmaril@0
  2633
	}
insilmaril@0
  2634
}
insilmaril@0
  2635
insilmaril@266
  2636
QString MapEditor::getURL()
insilmaril@266
  2637
{
insilmaril@266
  2638
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@266
  2639
			typeid(*selection) == typeid(MapCenterObj)) )
insilmaril@266
  2640
		return ((BranchObj*)selection)->getURL();
insilmaril@266
  2641
	else
insilmaril@266
  2642
		return "";
insilmaril@266
  2643
}
insilmaril@266
  2644
insilmaril@366
  2645
QStringList MapEditor::getURLs()
insilmaril@366
  2646
{
insilmaril@366
  2647
	QStringList urls;
insilmaril@366
  2648
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@366
  2649
			typeid(*selection) == typeid(MapCenterObj)) )
insilmaril@366
  2650
	{		
insilmaril@366
  2651
		BranchObj *bo=(BranchObj*)selection;
insilmaril@366
  2652
		bo=bo->first();	
insilmaril@366
  2653
		while (bo) 
insilmaril@366
  2654
		{
insilmaril@366
  2655
			if (!bo->getURL().isEmpty()) urls.append( bo->getURL());
insilmaril@366
  2656
			bo=bo->next();
insilmaril@366
  2657
		}	
insilmaril@366
  2658
	}	
insilmaril@366
  2659
	return urls;
insilmaril@366
  2660
}
insilmaril@366
  2661
insilmaril@366
  2662
insilmaril@0
  2663
void MapEditor::editHeading2URL()
insilmaril@0
  2664
{
insilmaril@0
  2665
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2666
			typeid(*selection) == typeid(MapCenterObj)) )
insilmaril@395
  2667
		setURL (((BranchObj*)selection)->getHeading());
insilmaril@0
  2668
}	
insilmaril@0
  2669
insilmaril@0
  2670
void MapEditor::editBugzilla2URL()
insilmaril@0
  2671
{
insilmaril@0
  2672
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2673
			typeid(*selection) == typeid(MapCenterObj)) )
insilmaril@0
  2674
	{		
insilmaril@163
  2675
		BranchObj *bo=(BranchObj*)selection;
insilmaril@143
  2676
		QString url= "https://bugzilla.novell.com/show_bug.cgi?id="+bo->getHeading();
insilmaril@395
  2677
		setURL (url);
insilmaril@0
  2678
	}
insilmaril@0
  2679
}	
insilmaril@0
  2680
insilmaril@175
  2681
void MapEditor::editFATE2URL()
insilmaril@175
  2682
{
insilmaril@175
  2683
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@175
  2684
			typeid(*selection) == typeid(MapCenterObj)) )
insilmaril@175
  2685
	{		
insilmaril@175
  2686
		BranchObj *bo=(BranchObj*)selection;
insilmaril@175
  2687
		QString url= "http://keeper.suse.de:8080/webfate/match/id?value=ID"+bo->getHeading();
insilmaril@395
  2688
		saveState(
insilmaril@395
  2689
			selection,
insilmaril@395
  2690
			"setURL (\""+bo->getURL()+"\")",
insilmaril@395
  2691
			selection,
insilmaril@395
  2692
			"setURL (\""+url+"\")",
insilmaril@395
  2693
			QString("Use heading of %1 as link to FATE").arg(getName(bo))
insilmaril@395
  2694
		);	
insilmaril@175
  2695
		bo->setURL (url);
insilmaril@175
  2696
		updateActions();
insilmaril@175
  2697
	}
insilmaril@175
  2698
}	
insilmaril@175
  2699
insilmaril@0
  2700
void MapEditor::editVymLink()
insilmaril@0
  2701
{
insilmaril@0
  2702
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2703
			typeid(*selection) == typeid(MapCenterObj)) )
insilmaril@0
  2704
	{		
insilmaril@163
  2705
		BranchObj *bo=(BranchObj*)selection;
insilmaril@404
  2706
		QStringList filters;
insilmaril@404
  2707
		filters <<"VYM map (*.vym)";
insilmaril@404
  2708
		QFileDialog *fd=new QFileDialog( this,vymName+" - " +tr("Link to another map"));
insilmaril@404
  2709
		fd->setFilters (filters);
insilmaril@404
  2710
		fd->setCaption(vymName+" - " +tr("Link to another map"));
insilmaril@143
  2711
		if (! bo->getVymLink().isEmpty() )
insilmaril@404
  2712
			fd->selectFile( bo->getVymLink() );
insilmaril@0
  2713
		fd->show();
insilmaril@0
  2714
insilmaril@0
  2715
		QString fn;
insilmaril@0
  2716
		if ( fd->exec() == QDialog::Accepted )
insilmaril@143
  2717
		{
insilmaril@395
  2718
			saveState(
insilmaril@395
  2719
				selection,
insilmaril@395
  2720
				"setVymLink (\""+bo->getVymLink()+"\")",
insilmaril@395
  2721
				selection,
insilmaril@395
  2722
				"setVymLink (\""+fd->selectedFile()+"\")",
insilmaril@395
  2723
				QString("Set vymlink of %1 to %2").arg(getName(bo)).arg(fd->selectedFile())
insilmaril@395
  2724
			);	
insilmaril@143
  2725
			bo->setVymLink (fd->selectedFile() );
insilmaril@143
  2726
			updateActions();
insilmaril@143
  2727
			mapCenter->reposition();
insilmaril@408
  2728
			scene()->update();
insilmaril@143
  2729
		}
insilmaril@0
  2730
	}
insilmaril@0
  2731
}
insilmaril@0
  2732
insilmaril@0
  2733
void MapEditor::deleteVymLink()
insilmaril@0
  2734
{
insilmaril@0
  2735
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2736
			typeid(*selection) == typeid(MapCenterObj)) )
insilmaril@0
  2737
	{		
insilmaril@163
  2738
		BranchObj *bo=(BranchObj*)selection;
insilmaril@395
  2739
		saveState(
insilmaril@395
  2740
			selection,
insilmaril@395
  2741
			"setVymLink (\""+bo->getVymLink()+"\")",
insilmaril@395
  2742
			selection,
insilmaril@395
  2743
			"setVymLink (\"\")",
insilmaril@395
  2744
			QString("Unset vymlink of %1").arg(getName(bo))
insilmaril@395
  2745
		);	
insilmaril@143
  2746
		bo->setVymLink ("" );
insilmaril@0
  2747
		updateActions();
insilmaril@0
  2748
		mapCenter->reposition();
insilmaril@408
  2749
		scene()->update();
insilmaril@0
  2750
	}
insilmaril@0
  2751
}
insilmaril@0
  2752
insilmaril@395
  2753
void MapEditor::setHideExport(bool b)
insilmaril@243
  2754
{
insilmaril@243
  2755
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@395
  2756
			typeid(*selection)==typeid(FloatImageObj)))
insilmaril@395
  2757
	{
insilmaril@250
  2758
		OrnamentedObj *oo=(OrnamentedObj*)selection;
insilmaril@395
  2759
		oo->setHideInExport (b);
insilmaril@395
  2760
		QString u= b ? "false" : "true";
insilmaril@395
  2761
		QString r=!b ? "false" : "true";
insilmaril@395
  2762
		
insilmaril@395
  2763
		saveState(
insilmaril@395
  2764
			selection,
insilmaril@395
  2765
			QString ("setHideExport (%1)").arg(u),
insilmaril@395
  2766
			selection,
insilmaril@395
  2767
			QString ("setHideExport (%1)").arg(r),
insilmaril@395
  2768
			QString ("Set HideExport flag of %1 to %2").arg(getName(oo)).arg (r)
insilmaril@395
  2769
		);	
insilmaril@243
  2770
		updateActions();
insilmaril@243
  2771
		mapCenter->reposition();
insilmaril@408
  2772
		scene()->update();
insilmaril@243
  2773
	}
insilmaril@243
  2774
}
insilmaril@243
  2775
insilmaril@395
  2776
void MapEditor::toggleHideExport()
insilmaril@395
  2777
{
insilmaril@395
  2778
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@395
  2779
			typeid(*selection)==typeid(FloatImageObj)))
insilmaril@395
  2780
		setHideExport ( !((OrnamentedObj*)selection)->hideInExport() );
insilmaril@395
  2781
}
insilmaril@395
  2782
insilmaril@0
  2783
QString MapEditor::getVymLink()
insilmaril@0
  2784
{
insilmaril@0
  2785
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2786
			typeid(*selection) == typeid(MapCenterObj)) )
insilmaril@0
  2787
	{		
insilmaril@163
  2788
		return ((BranchObj*)selection)->getVymLink();
insilmaril@0
  2789
	}
insilmaril@0
  2790
	return "";
insilmaril@0
  2791
	
insilmaril@0
  2792
}
insilmaril@0
  2793
insilmaril@366
  2794
QStringList MapEditor::getVymLinks()
insilmaril@366
  2795
{
insilmaril@366
  2796
	QStringList links;
insilmaril@366
  2797
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@366
  2798
			typeid(*selection) == typeid(MapCenterObj)) )
insilmaril@366
  2799
	{		
insilmaril@366
  2800
		BranchObj *bo=(BranchObj*)selection;
insilmaril@366
  2801
		bo=bo->first();	
insilmaril@366
  2802
		while (bo) 
insilmaril@366
  2803
		{
insilmaril@366
  2804
			if (!bo->getVymLink().isEmpty()) links.append( bo->getVymLink());
insilmaril@366
  2805
			bo=bo->next();
insilmaril@366
  2806
		}	
insilmaril@366
  2807
	}	
insilmaril@366
  2808
	return links;
insilmaril@366
  2809
}
insilmaril@366
  2810
insilmaril@366
  2811
insilmaril@395
  2812
void MapEditor::deleteKeepChilds()
insilmaril@98
  2813
{
insilmaril@98
  2814
	if (selection && (typeid(*selection) == typeid(BranchObj) ))
insilmaril@98
  2815
	{		
insilmaril@163
  2816
		BranchObj* bo=(BranchObj*)selection;
insilmaril@98
  2817
		BranchObj* par=(BranchObj*)(bo->getParObj());
insilmaril@408
  2818
		QPointF p=bo->getRelPos();
insilmaril@395
  2819
		saveStateChangingPart(
insilmaril@395
  2820
			selection->getParObj(),
insilmaril@395
  2821
			selection,
insilmaril@395
  2822
			"deleteKeepChilds ()",
insilmaril@395
  2823
			QString("Remove %1 and keep its childs").arg(getName(bo))
insilmaril@395
  2824
		);
insilmaril@394
  2825
insilmaril@139
  2826
		QString sel=selection->getSelectString();
insilmaril@98
  2827
		unselect();
insilmaril@98
  2828
		par->removeBranchHere(bo);
insilmaril@98
  2829
		mapCenter->reposition();
insilmaril@98
  2830
		select (sel);
insilmaril@394
  2831
		((BranchObj*)selection)->move2RelPos (p);
insilmaril@394
  2832
		mapCenter->reposition();
insilmaril@98
  2833
	}	
insilmaril@98
  2834
}
insilmaril@98
  2835
insilmaril@395
  2836
void MapEditor::deleteChilds()
insilmaril@98
  2837
{
insilmaril@395
  2838
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@395
  2839
		typeid(*selection)==typeid(MapCenterObj)))
insilmaril@98
  2840
	{		
insilmaril@395
  2841
		saveStateChangingPart(
insilmaril@395
  2842
			selection->getParObj(), 
insilmaril@395
  2843
			selection,
insilmaril@395
  2844
			"deleteChilds ()",
insilmaril@395
  2845
			QString( "Remove childs of branch %1").arg(getName(selection))
insilmaril@395
  2846
		);
insilmaril@163
  2847
		((BranchObj*)selection)->removeChilds();
insilmaril@98
  2848
		mapCenter->reposition();
insilmaril@98
  2849
	}	
insilmaril@98
  2850
}
insilmaril@98
  2851
insilmaril@0
  2852
void MapEditor::editMapInfo()
insilmaril@0
  2853
{
insilmaril@0
  2854
	ExtraInfoDialog dia;
insilmaril@0
  2855
	dia.setMapName (getFileName() );
insilmaril@0
  2856
	dia.setAuthor (mapCenter->getAuthor() );
insilmaril@0
  2857
	dia.setComment(mapCenter->getComment() );
insilmaril@0
  2858
insilmaril@0
  2859
	// Calc some stats
insilmaril@0
  2860
	QString stats;
insilmaril@408
  2861
    stats+=tr("%1 items on map\n","Info about map").arg (mapScene->items().size(),6);
insilmaril@0
  2862
insilmaril@0
  2863
	uint b=0;
insilmaril@0
  2864
	uint f=0;
insilmaril@0
  2865
	uint n=0;
insilmaril@103
  2866
	uint xl=0;
insilmaril@0
  2867
	BranchObj *bo;
insilmaril@0
  2868
	bo=mapCenter->first();
insilmaril@0
  2869
	while (bo) 
insilmaril@0
  2870
	{
insilmaril@0
  2871
		if (!bo->getNote().isEmpty() ) n++;
insilmaril@0
  2872
		f+= bo->countFloatImages();
insilmaril@0
  2873
		b++;
insilmaril@103
  2874
		xl+=bo->countXLinks();
insilmaril@0
  2875
		bo=bo->next();
insilmaril@0
  2876
	}
insilmaril@0
  2877
    stats+=QString ("%1 branches\n").arg (b-1,6);
insilmaril@104
  2878
    stats+=QString ("%1 xLinks \n").arg (xl,6);
insilmaril@0
  2879
    stats+=QString ("%1 notes\n").arg (n,6);
insilmaril@0
  2880
    stats+=QString ("%1 images\n").arg (f,6);
insilmaril@0
  2881
	dia.setStats (stats);