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