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