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