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