mapeditor.cpp
author insilmaril
Thu, 14 Jun 2007 10:21:41 +0000
changeset 502 f3465a5f0dc4
parent 500 d8c245d582a3
child 505 244e309b4047
permissions -rw-r--r--
Fixed undo/redo of copy/paste
insilmaril@0
     1
#include "mapeditor.h"
insilmaril@0
     2
insilmaril@374
     3
#include <q3filedialog.h>
insilmaril@0
     4
insilmaril@0
     5
#include <iostream>
insilmaril@95
     6
#include <cstdlib>
insilmaril@0
     7
#include <typeinfo>
insilmaril@0
     8
insilmaril@0
     9
#include "version.h"
insilmaril@0
    10
insilmaril@432
    11
#include "parser.h"
insilmaril@175
    12
#include "editxlinkdialog.h"
insilmaril@175
    13
#include "exports.h"
insilmaril@175
    14
#include "extrainfodialog.h"
insilmaril@236
    15
#include "file.h"
insilmaril@175
    16
#include "linkablemapobj.h"
insilmaril@175
    17
#include "mainwindow.h"
insilmaril@175
    18
#include "misc.h"
insilmaril@175
    19
#include "texteditor.h"
insilmaril@314
    20
#include "warningdialog.h"
insilmaril@0
    21
#include "xml.h"
insilmaril@0
    22
insilmaril@0
    23
insilmaril@0
    24
extern TextEditor *textEditor;
insilmaril@0
    25
extern int statusbarTime;
insilmaril@0
    26
extern Main *mainWindow;
insilmaril@164
    27
extern QString tmpVymDir;
insilmaril@166
    28
extern QString clipboardDir;
insilmaril@491
    29
extern QString clipboardFile;
insilmaril@166
    30
extern bool clipboardEmpty;
insilmaril@473
    31
extern bool debug;
insilmaril@0
    32
extern FlagRowObj *standardFlagsDefault;
insilmaril@0
    33
insilmaril@366
    34
extern QMenu* branchContextMenu;
insilmaril@366
    35
extern QMenu* branchAddContextMenu;
insilmaril@366
    36
extern QMenu* branchRemoveContextMenu;
insilmaril@366
    37
extern QMenu* branchLinksContextMenu;
insilmaril@366
    38
extern QMenu* branchXLinksContextMenuEdit;
insilmaril@366
    39
extern QMenu* branchXLinksContextMenuFollow;
insilmaril@366
    40
extern QMenu* floatimageContextMenu;
insilmaril@366
    41
extern QMenu* canvasContextMenu;
insilmaril@366
    42
insilmaril@0
    43
insilmaril@0
    44
extern Settings settings;
insilmaril@366
    45
extern ImageIO imageIO;
insilmaril@0
    46
insilmaril@404
    47
extern QString vymName;
insilmaril@404
    48
extern QString vymVersion;
insilmaril@404
    49
insilmaril@269
    50
extern QString iconPath;
insilmaril@301
    51
extern QDir vymBaseDir;
insilmaril@366
    52
extern QDir lastImageDir;
insilmaril@421
    53
extern QDir lastFileDir;
insilmaril@269
    54
insilmaril@164
    55
int MapEditor::mapNum=0;	// make instance
insilmaril@0
    56
insilmaril@0
    57
///////////////////////////////////////////////////////////////////////
insilmaril@0
    58
///////////////////////////////////////////////////////////////////////
insilmaril@408
    59
MapEditor::MapEditor( QWidget* parent) :
insilmaril@473
    60
  QGraphicsView(parent)  
insilmaril@0
    61
{
insilmaril@0
    62
	//cout << "Constructor ME "<<this<<endl;
insilmaril@164
    63
	mapNum++;
insilmaril@0
    64
insilmaril@119
    65
insilmaril@408
    66
	mapScene= new QGraphicsScene(parent);
insilmaril@408
    67
	//mapScene= new QGraphicsScene(QRectF(0,0,width(),height()), parent);
insilmaril@408
    68
	mapScene->setBackgroundBrush (QBrush(Qt::white, Qt::SolidPattern));
insilmaril@408
    69
insilmaril@408
    70
insilmaril@408
    71
    setScene (mapScene);
insilmaril@0
    72
	
insilmaril@408
    73
    mapCenter = new MapCenterObj(mapScene);
insilmaril@0
    74
    mapCenter->setVisibility (true);
insilmaril@0
    75
	mapCenter->setMapEditor (this);
insilmaril@93
    76
	mapCenter->setHeading (tr("New Map","Heading of mapcenter in new map"));
insilmaril@408
    77
	//mapCenter->move(mapScene->width()/2-mapCenter->width()/2,mapScene->height()/2-mapCenter->height()/2);
insilmaril@408
    78
	mapCenter->move(0,0);
insilmaril@0
    79
insilmaril@0
    80
    printer=NULL;
insilmaril@0
    81
insilmaril@97
    82
	defLinkColor=QColor (0,0,255);
insilmaril@97
    83
	defXLinkColor=QColor (180,180,180);
insilmaril@473
    84
	linkcolorhint=LinkableMapObj::DefaultColor;
insilmaril@473
    85
	linkstyle=LinkableMapObj::PolyParabel;
insilmaril@0
    86
insilmaril@366
    87
	// Create bitmap cursors, platform dependant
insilmaril@404
    88
	HandOpenCursor=QCursor (QPixmap(iconPath+"cursorhandopen.png"),1,1);		
insilmaril@404
    89
	PickColorCursor=QCursor ( QPixmap(iconPath+"cursorcolorpicker.png"), 5,27 ); 
insilmaril@404
    90
	CopyCursor=QCursor ( QPixmap(iconPath+"cursorcopy.png"), 1,1 ); 
insilmaril@404
    91
	XLinkCursor=QCursor ( QPixmap(iconPath+"cursorxlink.png"), 1,7 ); 
insilmaril@398
    92
insilmaril@398
    93
	setFocusPolicy (Qt::StrongFocus);
insilmaril@0
    94
insilmaril@0
    95
	pickingColor=false;
insilmaril@91
    96
	drawingLink=false;
insilmaril@93
    97
	copyingObj=false;
insilmaril@0
    98
insilmaril@0
    99
    editingBO=NULL;
insilmaril@0
   100
    movingObj=NULL;
insilmaril@0
   101
insilmaril@442
   102
	xelection.setMapEditor (this);
insilmaril@424
   103
	xelection.unselect();
insilmaril@366
   104
insilmaril@102
   105
	defXLinkWidth=1;
insilmaril@102
   106
	defXLinkColor=QColor (230,230,230);
insilmaril@102
   107
insilmaril@0
   108
    mapChanged=false;
insilmaril@0
   109
	mapDefault=true;
insilmaril@0
   110
	mapUnsaved=false;
insilmaril@0
   111
	
insilmaril@0
   112
	zipped=true;
insilmaril@0
   113
	filePath="";
insilmaril@294
   114
	fileName=tr("unnamed");
insilmaril@0
   115
	mapName="";
insilmaril@0
   116
insilmaril@398
   117
	stepsTotal=settings.readNumEntry("/mapeditor/stepsTotal",100);
insilmaril@381
   118
	undoSet.setEntry ("/history/stepsTotal",QString::number(stepsTotal));
insilmaril@491
   119
	mainWindow->updateHistory (undoSet);
insilmaril@163
   120
	
insilmaril@0
   121
	// Initialize find routine
insilmaril@0
   122
	itFind=NULL;				
insilmaril@0
   123
	EOFind=false;
insilmaril@0
   124
insilmaril@0
   125
	printFrame=true;
insilmaril@0
   126
	printFooter=true;
insilmaril@0
   127
insilmaril@139
   128
	blockReposition=false;
insilmaril@139
   129
	blockSaveState=false;
insilmaril@260
   130
insilmaril@473
   131
	hidemode=BranchObj::HideNone;
insilmaril@260
   132
insilmaril@377
   133
	// Create temporary files
insilmaril@377
   134
	makeTmpDirs();
insilmaril@0
   135
insilmaril@502
   136
	curStep=0;
insilmaril@502
   137
	redosAvail=0;
insilmaril@502
   138
	undosAvail=0;
insilmaril@502
   139
insilmaril@417
   140
	setAcceptDrops (true);	
insilmaril@0
   141
insilmaril@0
   142
	mapCenter->reposition();	//	for positioning heading
insilmaril@381
   143
insilmaril@473
   144
	// TODO animations
insilmaril@473
   145
	animObjList.clear();
insilmaril@473
   146
	//timerId = startTimer(100);  
insilmaril@473
   147
insilmaril@473
   148
	// autosave
insilmaril@473
   149
	autosaveTimer=new QTimer (this);
insilmaril@473
   150
	connect(autosaveTimer, SIGNAL(timeout()), this, SLOT(autosave()));
insilmaril@0
   151
}
insilmaril@0
   152
insilmaril@0
   153
MapEditor::~MapEditor()
insilmaril@0
   154
{
insilmaril@0
   155
	//cout <<"Destructor MapEditor\n";
insilmaril@473
   156
	autosaveTimer->stop();
insilmaril@0
   157
}
insilmaril@0
   158
insilmaril@0
   159
MapCenterObj* MapEditor::getMapCenter()
insilmaril@0
   160
{
insilmaril@0
   161
    return mapCenter;
insilmaril@0
   162
}
insilmaril@0
   163
insilmaril@408
   164
QGraphicsScene * MapEditor::getScene()
insilmaril@0
   165
{
insilmaril@408
   166
    return mapScene;
insilmaril@0
   167
}
insilmaril@0
   168
insilmaril@491
   169
MapEditor::State MapEditor::getState()
insilmaril@491
   170
{
insilmaril@491
   171
    return state;
insilmaril@491
   172
}
insilmaril@491
   173
insilmaril@491
   174
void MapEditor::setStateEditHeading(bool s)
insilmaril@491
   175
{
insilmaril@491
   176
	if (s)
insilmaril@491
   177
	{
insilmaril@491
   178
		if (state==Idle) state=EditHeading;
insilmaril@491
   179
	}
insilmaril@491
   180
	else	
insilmaril@491
   181
		state=Idle;
insilmaril@491
   182
}
insilmaril@491
   183
insilmaril@139
   184
bool MapEditor::isRepositionBlocked()
insilmaril@0
   185
{
insilmaril@139
   186
	return blockReposition;
insilmaril@0
   187
}
insilmaril@0
   188
insilmaril@473
   189
void MapEditor::setSaveStateBlocked(bool b)
insilmaril@473
   190
{
insilmaril@473
   191
	blockSaveState=b;
insilmaril@473
   192
}
insilmaril@473
   193
insilmaril@491
   194
bool MapEditor::isSelectBlocked()
insilmaril@491
   195
{
insilmaril@491
   196
	if (state==EditHeading)
insilmaril@491
   197
		return true;
insilmaril@491
   198
	else
insilmaril@491
   199
		return false; 
insilmaril@491
   200
}
insilmaril@491
   201
insilmaril@420
   202
QString MapEditor::getName (const LinkableMapObj *lmo)
insilmaril@294
   203
{
insilmaril@314
   204
	QString s;
insilmaril@314
   205
	if (!lmo) return QString("Error: NULL has no name!");
insilmaril@294
   206
insilmaril@294
   207
	if ((typeid(*lmo) == typeid(BranchObj) ||
insilmaril@294
   208
				      typeid(*lmo) == typeid(MapCenterObj))) 
insilmaril@314
   209
	{
insilmaril@314
   210
		
insilmaril@314
   211
		s=(((BranchObj*)lmo)->getHeading());
insilmaril@314
   212
		if (s=="") s="unnamed";
insilmaril@377
   213
		return QString("branch (%1)").arg(s);
insilmaril@377
   214
		//return QString("branch (<font color=\"#0000ff\">%1</font>)").arg(s);
insilmaril@314
   215
	}	
insilmaril@294
   216
	if ((typeid(*lmo) == typeid(FloatImageObj) ))
insilmaril@377
   217
		return QString ("floatimage [%1]").arg(((FloatImageObj*)lmo)->getOriginalFilename());
insilmaril@377
   218
		//return QString ("floatimage [<font color=\"#0000ff\">%1</font>]").arg(((FloatImageObj*)lmo)->getOriginalFilename());
insilmaril@294
   219
	return QString("Unknown type has no name!");
insilmaril@294
   220
}
insilmaril@294
   221
insilmaril@0
   222
void MapEditor::makeTmpDirs()
insilmaril@0
   223
{
insilmaril@0
   224
	// Create unique temporary directories
insilmaril@366
   225
	tmpMapDir=QDir::convertSeparators (tmpVymDir+QString("/mapeditor-%1").arg(mapNum));
insilmaril@366
   226
	histPath=QDir::convertSeparators (tmpMapDir+"/history");
insilmaril@164
   227
	QDir d;
insilmaril@377
   228
	d.mkdir (tmpMapDir);
insilmaril@0
   229
}
insilmaril@0
   230
insilmaril@408
   231
QString MapEditor::saveToDir(const QString &tmpdir, const QString &prefix, bool writeflags, const QPointF &offset, LinkableMapObj *saveSel)
insilmaril@0
   232
{
insilmaril@163
   233
	// tmpdir		temporary directory to which data will be written
insilmaril@0
   234
	// prefix		mapname, which will be appended to images etc.
insilmaril@0
   235
	// writeflags	Only write flags for "real" save of map, not undo
insilmaril@408
   236
	// offset		offset of bbox of whole map in scene. 
insilmaril@0
   237
	//				Needed for XML export
insilmaril@0
   238
	
insilmaril@0
   239
	// Save Header
insilmaril@0
   240
	QString ls;
insilmaril@0
   241
	switch (linkstyle)
insilmaril@0
   242
	{
insilmaril@473
   243
		case LinkableMapObj::Line: 
insilmaril@0
   244
			ls="StyleLine";
insilmaril@0
   245
			break;
insilmaril@473
   246
		case LinkableMapObj::Parabel:
insilmaril@0
   247
			ls="StyleParabel";
insilmaril@0
   248
			break;
insilmaril@473
   249
		case LinkableMapObj::PolyLine:	
insilmaril@0
   250
			ls="StylePolyLine";
insilmaril@0
   251
			break;
insilmaril@0
   252
		default:
insilmaril@0
   253
			ls="StylePolyParabel";
insilmaril@0
   254
			break;
insilmaril@0
   255
	}	
insilmaril@0
   256
insilmaril@0
   257
	QString s="<?xml version=\"1.0\" encoding=\"utf-8\"?><!DOCTYPE vymmap>\n";
insilmaril@0
   258
	QString colhint="";
insilmaril@473
   259
	if (linkcolorhint==LinkableMapObj::HeadingColor) 
insilmaril@0
   260
		colhint=attribut("linkColorHint","HeadingColor");
insilmaril@0
   261
insilmaril@404
   262
	QString mapAttr=attribut("version",vymVersion);
insilmaril@398
   263
	if (!saveSel || saveSel==mapCenter)
insilmaril@0
   264
		mapAttr+= attribut("author",mapCenter->getAuthor()) +
insilmaril@0
   265
				  attribut("comment",mapCenter->getComment()) +
insilmaril@0
   266
			      attribut("date",mapCenter->getDate()) +
insilmaril@408
   267
		          attribut("backgroundColor", mapScene->backgroundBrush().color().name() ) +
insilmaril@442
   268
		          attribut("selectionColor", xelection.getColor().name() ) +
insilmaril@0
   269
		          attribut("linkStyle", ls ) +
insilmaril@97
   270
		          attribut("linkColor", defLinkColor.name() ) +
insilmaril@102
   271
		          attribut("defXLinkColor", defXLinkColor.name() ) +
insilmaril@102
   272
		          attribut("defXLinkWidth", QString().setNum(defXLinkWidth,10) ) +
insilmaril@0
   273
		          colhint; 
insilmaril@0
   274
	s+=beginElement("vymmap",mapAttr);
insilmaril@0
   275
	incIndent();
insilmaril@0
   276
insilmaril@0
   277
	// Find the used flags while traversing the tree
insilmaril@0
   278
	standardFlagsDefault->resetUsedCounter();
insilmaril@0
   279
	
insilmaril@166
   280
	// Reset the counters before saving
insilmaril@406
   281
	// TODO constr. of FIO creates lots of objects, better do this in some other way...
insilmaril@408
   282
	FloatImageObj (mapScene).resetSaveCounter();
insilmaril@166
   283
insilmaril@0
   284
	// Build xml recursivly
insilmaril@398
   285
	if (!saveSel || typeid (*saveSel) == typeid (MapCenterObj))
insilmaril@366
   286
		// Save complete map, if saveSel not set
insilmaril@0
   287
		s+=mapCenter->saveToDir(tmpdir,prefix,writeflags,offset);
insilmaril@0
   288
	else
insilmaril@0
   289
	{
insilmaril@366
   290
		if ( typeid(*saveSel) == typeid(BranchObj) )
insilmaril@366
   291
			// Save Subtree
insilmaril@366
   292
			s+=((BranchObj*)(saveSel))->saveToDir(tmpdir,prefix,offset);
insilmaril@366
   293
		else if ( typeid(*saveSel) == typeid(FloatImageObj) )
insilmaril@366
   294
			// Save image
insilmaril@377
   295
			s+=((FloatImageObj*)(saveSel))->saveToDir(tmpdir,prefix);
insilmaril@0
   296
	}
insilmaril@0
   297
insilmaril@0
   298
	// Save local settings
insilmaril@0
   299
	s+=settings.getXMLData (destPath);
insilmaril@0
   300
insilmaril@0
   301
	// Save selection
insilmaril@424
   302
	if (!xelection.isEmpty() && !saveSel ) 
insilmaril@424
   303
		s+=valueElement("select",xelection.getSelectString());
insilmaril@0
   304
insilmaril@0
   305
	decIndent();
insilmaril@0
   306
	s+=endElement("vymmap");
insilmaril@0
   307
insilmaril@0
   308
	if (writeflags)
insilmaril@0
   309
		standardFlagsDefault->saveToDir (tmpdir+"/flags/","",writeflags);
insilmaril@0
   310
	return s;
insilmaril@0
   311
}
insilmaril@0
   312
insilmaril@486
   313
void MapEditor::saveState(const SaveMode &savemode, const QString &undoSelection, const QString &undoCom, const QString &redoSelection, const QString &redoCom, const QString &comment, LinkableMapObj *saveSel)
insilmaril@486
   314
{
insilmaril@486
   315
	// Main saveState
insilmaril@486
   316
insilmaril@486
   317
	if (blockSaveState) return;
insilmaril@486
   318
insilmaril@486
   319
	if (debug) cout << "ME::saveState() for  "<<mapName.ascii()<<endl;
insilmaril@486
   320
	
insilmaril@486
   321
	// Find out current undo directory
insilmaril@486
   322
	if (undosAvail<stepsTotal) undosAvail++;
insilmaril@486
   323
	curStep++;
insilmaril@486
   324
	if (curStep>stepsTotal) curStep=1;
insilmaril@486
   325
	
insilmaril@486
   326
	QString backupXML="";
insilmaril@486
   327
	QString bakMapName=QDir::convertSeparators (QString("history-%1").arg(curStep));
insilmaril@486
   328
	QString bakMapDir=QDir::convertSeparators (tmpMapDir +"/"+bakMapName);
insilmaril@486
   329
	QString bakMapPath=QDir::convertSeparators(bakMapDir+"/map.xml");
insilmaril@486
   330
insilmaril@486
   331
	// Create bakMapDir if not available
insilmaril@486
   332
	QDir d(bakMapDir);
insilmaril@486
   333
	if (!d.exists()) 
insilmaril@486
   334
		makeSubDirs (bakMapDir);
insilmaril@486
   335
insilmaril@486
   336
	// Save depending on how much needs to be saved	
insilmaril@486
   337
	if (saveSel)
insilmaril@486
   338
		backupXML=saveToDir (bakMapDir,mapName+"-",false, QPointF (),saveSel);
insilmaril@486
   339
		
insilmaril@486
   340
	QString undoCommand="";
insilmaril@486
   341
	if (savemode==UndoCommand)
insilmaril@486
   342
	{
insilmaril@486
   343
		undoCommand=undoCom;
insilmaril@486
   344
	}	
insilmaril@486
   345
	else if (savemode==PartOfMap )
insilmaril@486
   346
	{
insilmaril@486
   347
		undoCommand=undoCom;
insilmaril@486
   348
		undoCommand.replace ("PATH",bakMapPath);
insilmaril@486
   349
	}
insilmaril@486
   350
insilmaril@486
   351
	if (!backupXML.isEmpty())
insilmaril@486
   352
		// Write XML Data to disk
insilmaril@486
   353
		saveStringToDisk (QString(bakMapPath),backupXML);
insilmaril@486
   354
insilmaril@486
   355
	// We would have to save all actions in a tree, to keep track of 
insilmaril@486
   356
	// possible redos after a action. Possible, but we are too lazy: forget about redos.
insilmaril@486
   357
	redosAvail=0;
insilmaril@486
   358
insilmaril@486
   359
	// Write the current state to disk
insilmaril@486
   360
	undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
insilmaril@486
   361
	undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
insilmaril@486
   362
	undoSet.setEntry ("/history/curStep",QString::number(curStep));
insilmaril@486
   363
	undoSet.setEntry (QString("/history/step-%1/undoCommand").arg(curStep),undoCommand);
insilmaril@486
   364
	undoSet.setEntry (QString("/history/step-%1/undoSelection").arg(curStep),undoSelection);
insilmaril@486
   365
	undoSet.setEntry (QString("/history/step-%1/redoCommand").arg(curStep),redoCom);
insilmaril@486
   366
	undoSet.setEntry (QString("/history/step-%1/redoSelection").arg(curStep),redoSelection);
insilmaril@486
   367
	undoSet.setEntry (QString("/history/step-%1/comment").arg(curStep),comment);
insilmaril@486
   368
	undoSet.setEntry (QString("/history/version"),vymVersion);
insilmaril@486
   369
	undoSet.writeSettings(histPath);
insilmaril@486
   370
insilmaril@486
   371
	if (debug)
insilmaril@486
   372
	{
insilmaril@486
   373
		// TODO remove after testing
insilmaril@486
   374
		//cout << "          into="<< histPath.toStdString()<<endl;
insilmaril@486
   375
		cout << "    stepsTotal="<<stepsTotal<<
insilmaril@486
   376
		", undosAvail="<<undosAvail<<
insilmaril@486
   377
		", redosAvail="<<redosAvail<<
insilmaril@486
   378
		", curStep="<<curStep<<endl;
insilmaril@486
   379
		cout << "    ---------------------------"<<endl;
insilmaril@486
   380
		cout << "    comment="<<comment.toStdString()<<endl;
insilmaril@486
   381
		cout << "    undoCom="<<undoCommand.toStdString()<<endl;
insilmaril@486
   382
		cout << "    undoSel="<<undoSelection.toStdString()<<endl;
insilmaril@486
   383
		cout << "    redoCom="<<redoCom.toStdString()<<endl;
insilmaril@486
   384
		cout << "    redoSel="<<redoSelection.toStdString()<<endl;
insilmaril@486
   385
		if (saveSel) cout << "    saveSel="<<saveSel->getSelectString().ascii()<<endl;
insilmaril@486
   386
		cout << "    ---------------------------"<<endl;
insilmaril@486
   387
	}
insilmaril@486
   388
insilmaril@486
   389
	mainWindow->updateHistory (undoSet);
insilmaril@486
   390
	setChanged();
insilmaril@486
   391
	updateActions();
insilmaril@486
   392
}
insilmaril@486
   393
insilmaril@486
   394
insilmaril@395
   395
void MapEditor::saveStateChangingPart(LinkableMapObj *undoSel, LinkableMapObj* redoSel, const QString &rc, const QString &comment)
insilmaril@0
   396
{
insilmaril@366
   397
	// save the selected part of the map, Undo will replace part of map 
insilmaril@366
   398
	QString undoSelection="";
insilmaril@395
   399
	if (undoSel)
insilmaril@395
   400
		undoSelection=undoSel->getSelectString();
insilmaril@395
   401
	else
insilmaril@395
   402
		qWarning ("MapEditor::saveStateChangingPart  no undoSel given!");
insilmaril@395
   403
	QString redoSelection="";
insilmaril@395
   404
	if (redoSel)
insilmaril@395
   405
		redoSelection=undoSel->getSelectString();
insilmaril@395
   406
	else
insilmaril@395
   407
		qWarning ("MapEditor::saveStateChangingPart  no redoSel given!");
insilmaril@395
   408
		
insilmaril@366
   409
insilmaril@366
   410
	saveState (PartOfMap,
insilmaril@398
   411
		undoSelection, "addMapReplace (\"PATH\")",
insilmaril@395
   412
		redoSelection, rc, 
insilmaril@366
   413
		comment, 
insilmaril@366
   414
		undoSel);
insilmaril@105
   415
}
insilmaril@105
   416
insilmaril@393
   417
void MapEditor::saveStateRemovingPart(LinkableMapObj *redoSel, const QString &comment)
insilmaril@393
   418
{
insilmaril@394
   419
	if (!redoSel)
insilmaril@393
   420
	{
insilmaril@394
   421
		qWarning ("MapEditor::saveStateRemovingPart  no redoSel given!");
insilmaril@393
   422
		return;
insilmaril@393
   423
	}
insilmaril@393
   424
	QString undoSelection=redoSel->getParObj()->getSelectString();
insilmaril@393
   425
	QString redoSelection=redoSel->getSelectString();
insilmaril@398
   426
	if (typeid(*redoSel) == typeid(BranchObj)  ) 
insilmaril@394
   427
	{
insilmaril@394
   428
		// save the selected branch of the map, Undo will insert part of map 
insilmaril@394
   429
		saveState (PartOfMap,
insilmaril@394
   430
			undoSelection, QString("addMapInsert (\"PATH\",%1)").arg(((BranchObj*)redoSel)->getNum()),
insilmaril@394
   431
			redoSelection, "delete ()", 
insilmaril@394
   432
			comment, 
insilmaril@394
   433
			redoSel);
insilmaril@394
   434
	}
insilmaril@393
   435
}
insilmaril@393
   436
insilmaril@106
   437
insilmaril@366
   438
void MapEditor::saveState(LinkableMapObj *undoSel, const QString &uc, LinkableMapObj *redoSel, const QString &rc, const QString &comment) 
insilmaril@366
   439
{
insilmaril@366
   440
	// "Normal" savestate: save commands, selections and comment
insilmaril@366
   441
	// so just save commands for undo and redo
insilmaril@366
   442
	// and use current selection
insilmaril@366
   443
insilmaril@366
   444
	QString redoSelection="";
insilmaril@366
   445
	if (redoSel) redoSelection=redoSel->getSelectString();
insilmaril@366
   446
	QString undoSelection="";
insilmaril@366
   447
	if (undoSel) undoSelection=undoSel->getSelectString();
insilmaril@366
   448
insilmaril@366
   449
	saveState (UndoCommand,
insilmaril@366
   450
		undoSelection, uc,
insilmaril@366
   451
		redoSelection, rc, 
insilmaril@366
   452
		comment, 
insilmaril@366
   453
		NULL);
insilmaril@366
   454
}
insilmaril@366
   455
insilmaril@366
   456
void MapEditor::saveState(const QString &undoSel, const QString &uc, const QString &redoSel, const QString &rc, const QString &comment) 
insilmaril@366
   457
{
insilmaril@366
   458
	// "Normal" savestate: save commands, selections and comment
insilmaril@366
   459
	// so just save commands for undo and redo
insilmaril@366
   460
	// and use current selection
insilmaril@366
   461
	saveState (UndoCommand,
insilmaril@366
   462
		undoSel, uc,
insilmaril@366
   463
		redoSel, rc, 
insilmaril@366
   464
		comment, 
insilmaril@366
   465
		NULL);
insilmaril@366
   466
}
insilmaril@366
   467
insilmaril@366
   468
		
insilmaril@129
   469
void MapEditor::parseAtom(const QString &atom)
insilmaril@106
   470
{
insilmaril@424
   471
	BranchObj *selb=xelection.getBranch();
insilmaril@163
   472
	QString s,t;
insilmaril@129
   473
	int x,y;
insilmaril@395
   474
	bool b,ok;
insilmaril@129
   475
insilmaril@129
   476
	// Split string s into command and parameters
insilmaril@432
   477
	parser.parseAtom (atom);
insilmaril@447
   478
	QString com=parser.getCommand();
insilmaril@129
   479
	
insilmaril@106
   480
	// External commands
insilmaril@393
   481
	if (com=="addBranch")
insilmaril@366
   482
	{
insilmaril@424
   483
		if (xelection.isEmpty())
insilmaril@394
   484
		{
insilmaril@432
   485
			parser.setError (Aborted,"Nothing selected");
insilmaril@424
   486
		} else if (! selb )
insilmaril@394
   487
		{				  
insilmaril@432
   488
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@394
   489
		} else 
insilmaril@366
   490
		{	
insilmaril@394
   491
			QList <int> pl;
insilmaril@394
   492
			pl << 0 <<1;
insilmaril@432
   493
			if (parser.checkParamCount(pl))
insilmaril@394
   494
			{
insilmaril@432
   495
				if (parser.paramCount()==0)
insilmaril@394
   496
					addNewBranchInt (-2);
insilmaril@394
   497
				else
insilmaril@394
   498
				{
insilmaril@432
   499
					y=parser.parInt (ok,0);
insilmaril@394
   500
					if (ok ) addNewBranchInt (y);
insilmaril@394
   501
				}
insilmaril@394
   502
			}
insilmaril@394
   503
		}
insilmaril@394
   504
	} else if (com=="addBranchBefore")
insilmaril@394
   505
	{
insilmaril@424
   506
		if (xelection.isEmpty())
insilmaril@394
   507
		{
insilmaril@432
   508
			parser.setError (Aborted,"Nothing selected");
insilmaril@424
   509
		} else if (! selb )
insilmaril@394
   510
		{				  
insilmaril@432
   511
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@394
   512
		} else 
insilmaril@394
   513
		{	
insilmaril@432
   514
			if (parser.paramCount()==0)
insilmaril@394
   515
			{
insilmaril@394
   516
				addNewBranchBefore ();
insilmaril@394
   517
			}	
insilmaril@394
   518
		}
insilmaril@393
   519
	} else if (com==QString("addMapReplace"))
insilmaril@393
   520
	{
insilmaril@424
   521
		if (xelection.isEmpty())
insilmaril@394
   522
		{
insilmaril@432
   523
			parser.setError (Aborted,"Nothing selected");
insilmaril@424
   524
		} else if (! selb )
insilmaril@394
   525
		{				  
insilmaril@432
   526
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@432
   527
		} else if (parser.checkParamCount(1))
insilmaril@393
   528
		{
insilmaril@432
   529
			//s=parser.parString (ok,0);	// selection
insilmaril@432
   530
			t=parser.parString (ok,0);	// path to map
insilmaril@393
   531
			if (QDir::isRelativePath(t)) t=QDir::convertSeparators (tmpMapDir + "/"+t);
insilmaril@424
   532
			addMapReplaceInt(selb->getSelectString(),t);	
insilmaril@393
   533
		}
insilmaril@434
   534
	} else if (com==QString("addMapInsert"))
insilmaril@393
   535
	{
insilmaril@424
   536
		if (xelection.isEmpty())
insilmaril@393
   537
		{
insilmaril@432
   538
			parser.setError (Aborted,"Nothing selected");
insilmaril@424
   539
		} else if (! selb )
insilmaril@394
   540
		{				  
insilmaril@432
   541
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@394
   542
		} else 
insilmaril@394
   543
		{	
insilmaril@432
   544
			if (parser.checkParamCount(2))
insilmaril@394
   545
			{
insilmaril@432
   546
				t=parser.parString (ok,0);	// path to map
insilmaril@432
   547
				y=parser.parInt(ok,1);		// position
insilmaril@394
   548
				if (QDir::isRelativePath(t)) t=QDir::convertSeparators (tmpMapDir + "/"+t);
insilmaril@394
   549
				addMapInsertInt(t,y);	
insilmaril@394
   550
			}
insilmaril@393
   551
		}
insilmaril@447
   552
	} else if (com=="clearFlags")
insilmaril@447
   553
	{
insilmaril@447
   554
		if (xelection.isEmpty() )
insilmaril@447
   555
		{
insilmaril@447
   556
			parser.setError (Aborted,"Nothing selected");
insilmaril@447
   557
		} else if (! selb )
insilmaril@447
   558
		{				  
insilmaril@447
   559
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@447
   560
		} else if (parser.checkParamCount(0))
insilmaril@447
   561
		{
insilmaril@447
   562
			selb->clearStandardFlags();	
insilmaril@447
   563
			selb->updateFlagsToolbar();
insilmaril@447
   564
		}
insilmaril@395
   565
	} else if (com=="colorBranch")
insilmaril@395
   566
	{
insilmaril@424
   567
		if (xelection.isEmpty())
insilmaril@395
   568
		{
insilmaril@432
   569
			parser.setError (Aborted,"Nothing selected");
insilmaril@424
   570
		} else if (! selb )
insilmaril@395
   571
		{				  
insilmaril@432
   572
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@432
   573
		} else if (parser.checkParamCount(1))
insilmaril@395
   574
		{	
insilmaril@432
   575
			QColor c=parser.parColor (ok,0);
insilmaril@395
   576
			if (ok) colorBranch (c);
insilmaril@395
   577
		}	
insilmaril@417
   578
	} else if (com=="colorSubtree")
insilmaril@417
   579
	{
insilmaril@424
   580
		if (xelection.isEmpty())
insilmaril@417
   581
		{
insilmaril@432
   582
			parser.setError (Aborted,"Nothing selected");
insilmaril@424
   583
		} else if (! selb )
insilmaril@417
   584
		{				  
insilmaril@432
   585
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@432
   586
		} else if (parser.checkParamCount(1))
insilmaril@417
   587
		{	
insilmaril@432
   588
			QColor c=parser.parColor (ok,0);
insilmaril@417
   589
			if (ok) colorSubtree (c);
insilmaril@417
   590
		}	
insilmaril@502
   591
	} else if (com=="copy")
insilmaril@502
   592
	{
insilmaril@502
   593
		if (xelection.isEmpty())
insilmaril@502
   594
		{
insilmaril@502
   595
			parser.setError (Aborted,"Nothing selected");
insilmaril@502
   596
		} else if (! selb )
insilmaril@502
   597
		{				  
insilmaril@502
   598
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@502
   599
		} else if (parser.checkParamCount(0))
insilmaril@502
   600
		{	
insilmaril@502
   601
			//FIXME missing action for copy
insilmaril@502
   602
		}	
insilmaril@395
   603
	} else if (com=="cut")
insilmaril@395
   604
	{
insilmaril@424
   605
		if (xelection.isEmpty())
insilmaril@395
   606
		{
insilmaril@432
   607
			parser.setError (Aborted,"Nothing selected");
insilmaril@491
   608
		} else if ( xelection.type()!=Selection::Branch  && 
insilmaril@491
   609
					xelection.type()!=Selection::MapCenter  &&
insilmaril@491
   610
					xelection.type()!=Selection::FloatImage )
insilmaril@395
   611
		{				  
insilmaril@432
   612
			parser.setError (Aborted,"Type of selection is not a branch or floatimage");
insilmaril@432
   613
		} else if (parser.checkParamCount(0))
insilmaril@395
   614
		{	
insilmaril@395
   615
			cut();
insilmaril@395
   616
		}	
insilmaril@393
   617
	} else if (com=="delete")
insilmaril@393
   618
	{
insilmaril@424
   619
		if (xelection.isEmpty())
insilmaril@394
   620
		{
insilmaril@432
   621
			parser.setError (Aborted,"Nothing selected");
insilmaril@491
   622
		} else if (xelection.type() != Selection::Branch && xelection.type() != Selection::FloatImage )
insilmaril@424
   623
		{
insilmaril@434
   624
			parser.setError (Aborted,"Type of selection is wrong.");
insilmaril@432
   625
		} else if (parser.checkParamCount(0))
insilmaril@393
   626
		{	
insilmaril@393
   627
			deleteSelection();
insilmaril@393
   628
		}	
insilmaril@394
   629
	} else if (com=="deleteKeepChilds")
insilmaril@394
   630
	{
insilmaril@424
   631
		if (xelection.isEmpty())
insilmaril@394
   632
		{
insilmaril@432
   633
			parser.setError (Aborted,"Nothing selected");
insilmaril@424
   634
		} else if (! selb )
insilmaril@424
   635
		{
insilmaril@432
   636
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@432
   637
		} else if (parser.checkParamCount(0))
insilmaril@394
   638
		{	
insilmaril@395
   639
			deleteKeepChilds();
insilmaril@395
   640
		}	
insilmaril@395
   641
	} else if (com=="deleteChilds")
insilmaril@395
   642
	{
insilmaril@424
   643
		if (xelection.isEmpty())
insilmaril@395
   644
		{
insilmaril@432
   645
			parser.setError (Aborted,"Nothing selected");
insilmaril@424
   646
		} else if (! selb)
insilmaril@424
   647
		{
insilmaril@432
   648
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@432
   649
		} else if (parser.checkParamCount(0))
insilmaril@395
   650
		{	
insilmaril@395
   651
			deleteChilds();
insilmaril@394
   652
		}	
insilmaril@491
   653
	} else if (com=="importDir")
insilmaril@491
   654
	{
insilmaril@491
   655
		if (xelection.isEmpty())
insilmaril@491
   656
		{
insilmaril@491
   657
			parser.setError (Aborted,"Nothing selected");
insilmaril@491
   658
		} else if (! selb )
insilmaril@491
   659
		{				  
insilmaril@491
   660
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@491
   661
		} else if (parser.checkParamCount(1))
insilmaril@491
   662
		{
insilmaril@491
   663
			s=parser.parString(ok,0);
insilmaril@491
   664
			if (ok) importDirInt(s);
insilmaril@491
   665
		}	
insilmaril@420
   666
	} else if (com=="linkTo")
insilmaril@420
   667
	{
insilmaril@424
   668
		if (xelection.isEmpty())
insilmaril@420
   669
		{
insilmaril@432
   670
			parser.setError (Aborted,"Nothing selected");
insilmaril@424
   671
		} else if ( selb)
insilmaril@420
   672
		{
insilmaril@432
   673
			if (parser.checkParamCount(4))
insilmaril@421
   674
			{
insilmaril@421
   675
				// 0	selectstring of parent
insilmaril@421
   676
				// 1	num in parent (for branches)
insilmaril@421
   677
				// 2,3	x,y of mainbranch or mapcenter
insilmaril@432
   678
				s=parser.parString(ok,0);
insilmaril@421
   679
				LinkableMapObj *dst=mapCenter->findObjBySelect (s);
insilmaril@421
   680
				if (dst)
insilmaril@421
   681
				{	
insilmaril@421
   682
					if (typeid(*dst) == typeid(BranchObj) ) 
insilmaril@421
   683
					{
insilmaril@421
   684
						// Get number in parent
insilmaril@432
   685
						x=parser.parInt (ok,1);
insilmaril@421
   686
						if (ok)
insilmaril@424
   687
							selb->linkTo ((BranchObj*)(dst),x);
insilmaril@421
   688
					} else if (typeid(*dst) == typeid(MapCenterObj) ) 
insilmaril@421
   689
					{
insilmaril@424
   690
						selb->linkTo ((BranchObj*)(dst),-1);
insilmaril@421
   691
						// Get coordinates of mainbranch
insilmaril@432
   692
						x=parser.parInt (ok,2);
insilmaril@421
   693
						if (ok)
insilmaril@421
   694
						{
insilmaril@432
   695
							y=parser.parInt (ok,3);
insilmaril@424
   696
							if (ok) selb->move (x,y);
insilmaril@421
   697
						}
insilmaril@421
   698
					}	
insilmaril@421
   699
				}	
insilmaril@421
   700
			}	
insilmaril@491
   701
		} else if ( xelection.type() == Selection::FloatImage) 
insilmaril@421
   702
		{
insilmaril@432
   703
			if (parser.checkParamCount(1))
insilmaril@421
   704
			{
insilmaril@421
   705
				// 0	selectstring of parent
insilmaril@432
   706
				s=parser.parString(ok,0);
insilmaril@421
   707
				LinkableMapObj *dst=mapCenter->findObjBySelect (s);
insilmaril@421
   708
				if (dst)
insilmaril@421
   709
				{	
insilmaril@421
   710
					if (typeid(*dst) == typeid(BranchObj) ||
insilmaril@421
   711
						typeid(*dst) == typeid(MapCenterObj)) 
insilmaril@421
   712
						linkTo (dst->getSelectString());
insilmaril@421
   713
				} else	
insilmaril@432
   714
					parser.setError (Aborted,"Destination is not a branch");
insilmaril@421
   715
			}		
insilmaril@421
   716
		} else
insilmaril@432
   717
			parser.setError (Aborted,"Type of selection is not a floatimage or branch");
insilmaril@432
   718
	} else if (com=="loadImage")
insilmaril@432
   719
	{
insilmaril@432
   720
		if (xelection.isEmpty())
insilmaril@432
   721
		{
insilmaril@432
   722
			parser.setError (Aborted,"Nothing selected");
insilmaril@432
   723
		} else if (! selb )
insilmaril@432
   724
		{				  
insilmaril@432
   725
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@432
   726
		} else if (parser.checkParamCount(1))
insilmaril@432
   727
		{
insilmaril@432
   728
			s=parser.parString(ok,0);
insilmaril@432
   729
			if (ok) loadFloatImageInt (s);
insilmaril@432
   730
		}	
insilmaril@393
   731
	} else if (com=="moveBranchUp")
insilmaril@393
   732
	{
insilmaril@424
   733
		if (xelection.isEmpty() )
insilmaril@394
   734
		{
insilmaril@432
   735
			parser.setError (Aborted,"Nothing selected");
insilmaril@424
   736
		} else if (! selb )
insilmaril@394
   737
		{				  
insilmaril@432
   738
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@432
   739
		} else if (parser.checkParamCount(0))
insilmaril@394
   740
		{
insilmaril@394
   741
			moveBranchUp();
insilmaril@394
   742
		}	
insilmaril@394
   743
	} else if (com=="moveBranchDown")
insilmaril@394
   744
	{
insilmaril@424
   745
		if (xelection.isEmpty() )
insilmaril@394
   746
		{
insilmaril@432
   747
			parser.setError (Aborted,"Nothing selected");
insilmaril@424
   748
		} else if (! selb )
insilmaril@394
   749
		{				  
insilmaril@432
   750
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@432
   751
		} else if (parser.checkParamCount(0))
insilmaril@394
   752
		{
insilmaril@394
   753
			moveBranchDown();
insilmaril@394
   754
		}	
insilmaril@394
   755
	} else if (com=="move")
insilmaril@394
   756
	{
insilmaril@424
   757
		if (xelection.isEmpty() )
insilmaril@394
   758
		{
insilmaril@432
   759
			parser.setError (Aborted,"Nothing selected");
insilmaril@491
   760
		} else if ( xelection.type()!=Selection::Branch  && 
insilmaril@491
   761
					xelection.type()!=Selection::MapCenter  &&
insilmaril@491
   762
					xelection.type()!=Selection::FloatImage )
insilmaril@394
   763
		{				  
insilmaril@432
   764
			parser.setError (Aborted,"Type of selection is not a branch or floatimage");
insilmaril@432
   765
		} else if (parser.checkParamCount(2))
insilmaril@393
   766
		{	
insilmaril@432
   767
			x=parser.parInt (ok,0);
insilmaril@393
   768
			if (ok)
insilmaril@393
   769
			{
insilmaril@432
   770
				y=parser.parInt (ok,1);
insilmaril@393
   771
				if (ok) move (x,y);
insilmaril@393
   772
			}
insilmaril@393
   773
		}	
insilmaril@395
   774
	} else if (com=="moveRel")
insilmaril@393
   775
	{
insilmaril@424
   776
		if (xelection.isEmpty() )
insilmaril@394
   777
		{
insilmaril@432
   778
			parser.setError (Aborted,"Nothing selected");
insilmaril@491
   779
		} else if ( xelection.type()!=Selection::Selection::Branch  && 
insilmaril@491
   780
					xelection.type()!=Selection::Selection::MapCenter  &&
insilmaril@491
   781
					xelection.type()!=Selection::Selection::FloatImage )
insilmaril@394
   782
		{				  
insilmaril@432
   783
			parser.setError (Aborted,"Type of selection is not a branch or floatimage");
insilmaril@432
   784
		} else if (parser.checkParamCount(2))
insilmaril@393
   785
		{	
insilmaril@432
   786
			x=parser.parInt (ok,0);
insilmaril@393
   787
			if (ok)
insilmaril@393
   788
			{
insilmaril@432
   789
				y=parser.parInt (ok,1);
insilmaril@393
   790
				if (ok) moveRel (x,y);
insilmaril@393
   791
			}
insilmaril@393
   792
		}	
insilmaril@502
   793
	} else if (com=="nop")
insilmaril@502
   794
	{
insilmaril@395
   795
	} else if (com=="paste")
insilmaril@395
   796
	{
insilmaril@424
   797
		if (xelection.isEmpty() )
insilmaril@395
   798
		{
insilmaril@432
   799
			parser.setError (Aborted,"Nothing selected");
insilmaril@424
   800
		} else if (! selb )
insilmaril@395
   801
		{				  
insilmaril@432
   802
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@502
   803
		} else if (parser.checkParamCount(1))
insilmaril@395
   804
		{	
insilmaril@502
   805
			x=parser.parInt (ok,0);
insilmaril@502
   806
			if (ok) pasteNoSave(x);
insilmaril@502
   807
		}	
insilmaril@502
   808
	/////////////////////////////////////////////////////////////////////
insilmaril@502
   809
	} else if (com=="qa")
insilmaril@502
   810
	{
insilmaril@502
   811
		if (xelection.isEmpty() )
insilmaril@502
   812
		{
insilmaril@502
   813
			parser.setError (Aborted,"Nothing selected");
insilmaril@502
   814
		} else if (! selb )
insilmaril@502
   815
		{				  
insilmaril@502
   816
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@502
   817
		} else if (parser.checkParamCount(4))
insilmaril@502
   818
		{	
insilmaril@502
   819
			QString c,u;
insilmaril@502
   820
			c=parser.parString (ok,0);
insilmaril@502
   821
			if (!ok)
insilmaril@502
   822
			{
insilmaril@502
   823
				parser.setError (Aborted,"No comment given");
insilmaril@502
   824
			} else
insilmaril@502
   825
			{
insilmaril@502
   826
				s=parser.parString (ok,1);
insilmaril@502
   827
				if (!ok)
insilmaril@502
   828
				{
insilmaril@502
   829
					parser.setError (Aborted,"First parameter is not a string");
insilmaril@502
   830
				} else
insilmaril@502
   831
				{
insilmaril@502
   832
					t=parser.parString (ok,2);
insilmaril@502
   833
					if (!ok)
insilmaril@502
   834
					{
insilmaril@502
   835
						parser.setError (Aborted,"Condition is not a string");
insilmaril@502
   836
					} else
insilmaril@502
   837
					{
insilmaril@502
   838
						u=parser.parString (ok,3);
insilmaril@502
   839
						if (!ok)
insilmaril@502
   840
						{
insilmaril@502
   841
							parser.setError (Aborted,"Third parameter is not a string");
insilmaril@502
   842
						} else
insilmaril@502
   843
						{
insilmaril@502
   844
							if (s!="heading")
insilmaril@502
   845
							{
insilmaril@502
   846
								parser.setError (Aborted,"Unknown type: "+s);
insilmaril@502
   847
							} else
insilmaril@502
   848
							{
insilmaril@502
   849
								if (! (t=="eq") ) 
insilmaril@502
   850
								{
insilmaril@502
   851
									parser.setError (Aborted,"Unknown operator: "+t);
insilmaril@502
   852
								} else
insilmaril@502
   853
								{
insilmaril@502
   854
									if (! selb    )
insilmaril@502
   855
									{
insilmaril@502
   856
										parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@502
   857
									} else
insilmaril@502
   858
									{
insilmaril@502
   859
										if (selb->getHeading() == u)
insilmaril@502
   860
										{
insilmaril@502
   861
											cout << "PASSED: " << c.ascii() << endl;
insilmaril@502
   862
										} else
insilmaril@502
   863
										{
insilmaril@502
   864
											cout << "FAILED: " << c.ascii() << endl;
insilmaril@502
   865
										}
insilmaril@502
   866
									}
insilmaril@502
   867
								}
insilmaril@502
   868
							}
insilmaril@502
   869
						} 
insilmaril@502
   870
					} 
insilmaril@502
   871
				} 
insilmaril@502
   872
			}
insilmaril@395
   873
		}	
insilmaril@434
   874
	} else if (com=="saveImage")
insilmaril@434
   875
	{
insilmaril@434
   876
		FloatImageObj *fio=xelection.getFloatImage();
insilmaril@434
   877
		if (!fio)
insilmaril@434
   878
		{
insilmaril@434
   879
			parser.setError (Aborted,"Type of selection is not an image");
insilmaril@434
   880
		} else if (parser.checkParamCount(2))
insilmaril@434
   881
		{
insilmaril@434
   882
			s=parser.parString(ok,0);
insilmaril@434
   883
			if (ok)
insilmaril@434
   884
			{
insilmaril@434
   885
				t=parser.parString(ok,1);
insilmaril@434
   886
				if (ok) saveFloatImageInt (fio,t,s);
insilmaril@434
   887
			}
insilmaril@434
   888
		}	
insilmaril@427
   889
	} else if (com=="scroll")
insilmaril@427
   890
	{
insilmaril@427
   891
		if (xelection.isEmpty() )
insilmaril@427
   892
		{
insilmaril@432
   893
			parser.setError (Aborted,"Nothing selected");
insilmaril@427
   894
		} else if (! selb )
insilmaril@427
   895
		{				  
insilmaril@432
   896
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@432
   897
		} else if (parser.checkParamCount(0))
insilmaril@427
   898
		{	
insilmaril@473
   899
			if (!scrollBranch (selb))	
insilmaril@432
   900
				parser.setError (Aborted,"Could not scroll branch");
insilmaril@427
   901
		}	
insilmaril@398
   902
	} else if (com=="select")
insilmaril@398
   903
	{
insilmaril@432
   904
		if (parser.checkParamCount(1))
insilmaril@398
   905
		{
insilmaril@432
   906
			s=parser.parString(ok,0);
insilmaril@398
   907
			if (ok) select (s);
insilmaril@398
   908
		}	
insilmaril@432
   909
	} else if (com=="selectLastBranch")
insilmaril@432
   910
	{
insilmaril@432
   911
		if (xelection.isEmpty() )
insilmaril@432
   912
		{
insilmaril@432
   913
			parser.setError (Aborted,"Nothing selected");
insilmaril@432
   914
		} else if (! selb )
insilmaril@432
   915
		{				  
insilmaril@432
   916
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@432
   917
		} else if (parser.checkParamCount(0))
insilmaril@432
   918
		{	
insilmaril@432
   919
			BranchObj *bo=selb->getLastBranch();
insilmaril@432
   920
			if (!bo)
insilmaril@432
   921
				parser.setError (Aborted,"Could not select last branch");
insilmaril@432
   922
			selectInt (bo);	
insilmaril@432
   923
				
insilmaril@432
   924
		}	
insilmaril@434
   925
	} else if (com=="selectLastImage")
insilmaril@434
   926
	{
insilmaril@434
   927
		if (xelection.isEmpty() )
insilmaril@434
   928
		{
insilmaril@434
   929
			parser.setError (Aborted,"Nothing selected");
insilmaril@434
   930
		} else if (! selb )
insilmaril@434
   931
		{				  
insilmaril@434
   932
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@434
   933
		} else if (parser.checkParamCount(0))
insilmaril@434
   934
		{	
insilmaril@434
   935
			FloatImageObj *fio=selb->getLastFloatImage();
insilmaril@434
   936
			if (!fio)
insilmaril@434
   937
				parser.setError (Aborted,"Could not select last image");
insilmaril@434
   938
			selectInt (fio);	
insilmaril@434
   939
				
insilmaril@434
   940
		}	
insilmaril@473
   941
	} else if (com=="setFrameType")
insilmaril@473
   942
	{
insilmaril@491
   943
		if ( xelection.type()!=Selection::Branch && xelection.type()!= Selection::MapCenter && xelection.type()!=Selection::FloatImage)
insilmaril@473
   944
		{
insilmaril@473
   945
			parser.setError (Aborted,"Type of selection does not allow setting frame type");
insilmaril@473
   946
		}
insilmaril@473
   947
		else if (parser.checkParamCount(1))
insilmaril@473
   948
		{
insilmaril@473
   949
			s=parser.parString(ok,0);
insilmaril@473
   950
			if (ok) setFrameType (s);
insilmaril@473
   951
		}	
insilmaril@486
   952
	} else if (com=="setFramePenColor")
insilmaril@486
   953
	{
insilmaril@491
   954
		if ( xelection.type()!=Selection::Branch && xelection.type()!= Selection::MapCenter && xelection.type()!=Selection::FloatImage)
insilmaril@486
   955
		{
insilmaril@486
   956
			parser.setError (Aborted,"Type of selection does not allow setting of pen color");
insilmaril@486
   957
		}
insilmaril@486
   958
		else if (parser.checkParamCount(1))
insilmaril@486
   959
		{
insilmaril@486
   960
			QColor c=parser.parColor(ok,0);
insilmaril@486
   961
			if (ok) setFramePenColor (c);
insilmaril@486
   962
		}	
insilmaril@486
   963
	} else if (com=="setFrameBrushColor")
insilmaril@486
   964
	{
insilmaril@491
   965
		if ( xelection.type()!=Selection::Branch && xelection.type()!= Selection::MapCenter && xelection.type()!=Selection::FloatImage)
insilmaril@486
   966
		{
insilmaril@486
   967
			parser.setError (Aborted,"Type of selection does not allow setting brush color");
insilmaril@486
   968
		}
insilmaril@486
   969
		else if (parser.checkParamCount(1))
insilmaril@486
   970
		{
insilmaril@486
   971
			QColor c=parser.parColor(ok,0);
insilmaril@486
   972
			if (ok) setFrameBrushColor (c);
insilmaril@486
   973
		}	
insilmaril@486
   974
	} else if (com=="setFramePadding")
insilmaril@486
   975
	{
insilmaril@491
   976
		if ( xelection.type()!=Selection::Branch && xelection.type()!= Selection::MapCenter && xelection.type()!=Selection::FloatImage)
insilmaril@486
   977
		{
insilmaril@486
   978
			parser.setError (Aborted,"Type of selection does not allow setting frame padding");
insilmaril@486
   979
		}
insilmaril@486
   980
		else if (parser.checkParamCount(1))
insilmaril@486
   981
		{
insilmaril@486
   982
			x=parser.parInt(ok,0);
insilmaril@486
   983
			if (ok) setFramePadding(x);
insilmaril@486
   984
		}	
insilmaril@486
   985
	} else if (com=="setFrameBorderWidth")
insilmaril@486
   986
	{
insilmaril@491
   987
		if ( xelection.type()!=Selection::Branch && xelection.type()!= Selection::MapCenter && xelection.type()!=Selection::FloatImage)
insilmaril@486
   988
		{
insilmaril@486
   989
			parser.setError (Aborted,"Type of selection does not allow setting frame border width");
insilmaril@486
   990
		}
insilmaril@486
   991
		else if (parser.checkParamCount(1))
insilmaril@486
   992
		{
insilmaril@486
   993
			x=parser.parInt(ok,0);
insilmaril@486
   994
			if (ok) setFrameBorderWidth (x);
insilmaril@486
   995
		}	
insilmaril@398
   996
	} else if (com=="setMapAuthor")
insilmaril@398
   997
	{
insilmaril@432
   998
		if (parser.checkParamCount(1))
insilmaril@398
   999
		{
insilmaril@432
  1000
			s=parser.parString(ok,0);
insilmaril@398
  1001
			if (ok) setMapAuthor (s);
insilmaril@398
  1002
		}	
insilmaril@398
  1003
	} else if (com=="setMapComment")
insilmaril@398
  1004
	{
insilmaril@432
  1005
		if (parser.checkParamCount(1))
insilmaril@398
  1006
		{
insilmaril@432
  1007
			s=parser.parString(ok,0);
insilmaril@398
  1008
			if (ok) setMapComment(s);
insilmaril@398
  1009
		}	
insilmaril@398
  1010
	} else if (com=="setMapBackgroundColor")
insilmaril@398
  1011
	{
insilmaril@424
  1012
		if (xelection.isEmpty() )
insilmaril@398
  1013
		{
insilmaril@432
  1014
			parser.setError (Aborted,"Nothing selected");
insilmaril@424
  1015
		} else if (! xelection.getBranch() )
insilmaril@398
  1016
		{				  
insilmaril@432
  1017
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@432
  1018
		} else if (parser.checkParamCount(1))
insilmaril@398
  1019
		{
insilmaril@432
  1020
			QColor c=parser.parColor (ok,0);
insilmaril@398
  1021
			if (ok) setMapBackgroundColor (c);
insilmaril@398
  1022
		}	
insilmaril@398
  1023
	} else if (com=="setMapDefLinkColor")
insilmaril@398
  1024
	{
insilmaril@424
  1025
		if (xelection.isEmpty() )
insilmaril@398
  1026
		{
insilmaril@432
  1027
			parser.setError (Aborted,"Nothing selected");
insilmaril@424
  1028
		} else if (! selb )
insilmaril@398
  1029
		{				  
insilmaril@432
  1030
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@432
  1031
		} else if (parser.checkParamCount(1))
insilmaril@398
  1032
		{
insilmaril@432
  1033
			QColor c=parser.parColor (ok,0);
insilmaril@398
  1034
			if (ok) setMapDefLinkColor (c);
insilmaril@398
  1035
		}	
insilmaril@398
  1036
	} else if (com=="setMapLinkStyle")
insilmaril@398
  1037
	{
insilmaril@432
  1038
		if (parser.checkParamCount(1))
insilmaril@398
  1039
		{
insilmaril@432
  1040
			s=parser.parString (ok,0);
insilmaril@398
  1041
			if (ok) setMapLinkStyle(s);
insilmaril@398
  1042
		}	
insilmaril@129
  1043
	} else if (com=="setHeading")
insilmaril@129
  1044
	{
insilmaril@424
  1045
		if (xelection.isEmpty() )
insilmaril@394
  1046
		{
insilmaril@432
  1047
			parser.setError (Aborted,"Nothing selected");
insilmaril@424
  1048
		} else if (! selb )
insilmaril@394
  1049
		{				  
insilmaril@432
  1050
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@432
  1051
		} else if (parser.checkParamCount(1))
insilmaril@129
  1052
		{
insilmaril@432
  1053
			s=parser.parString (ok,0);
insilmaril@366
  1054
			if (ok) 
insilmaril@366
  1055
				setHeading (s);
insilmaril@129
  1056
		}	
insilmaril@395
  1057
	} else if (com=="setHideExport")
insilmaril@395
  1058
	{
insilmaril@424
  1059
		if (xelection.isEmpty() )
insilmaril@395
  1060
		{
insilmaril@432
  1061
			parser.setError (Aborted,"Nothing selected");
insilmaril@491
  1062
		} else if (xelection.type()!=Selection::Branch && xelection.type() != Selection::MapCenter &&xelection.type()!=Selection::FloatImage)
insilmaril@395
  1063
		{				  
insilmaril@432
  1064
			parser.setError (Aborted,"Type of selection is not a branch or floatimage");
insilmaril@432
  1065
		} else if (parser.checkParamCount(1))
insilmaril@395
  1066
		{
insilmaril@432
  1067
			b=parser.parBool(ok,0);
insilmaril@395
  1068
			if (ok) setHideExport (b);
insilmaril@395
  1069
		}
insilmaril@434
  1070
	} else if (com=="setIncludeImagesHorizontally")
insilmaril@473
  1071
	{ 
insilmaril@434
  1072
		if (xelection.isEmpty() )
insilmaril@434
  1073
		{
insilmaril@434
  1074
			parser.setError (Aborted,"Nothing selected");
insilmaril@434
  1075
		} else if (! selb)
insilmaril@434
  1076
		{				  
insilmaril@434
  1077
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@434
  1078
		} else if (parser.checkParamCount(1))
insilmaril@434
  1079
		{
insilmaril@434
  1080
			b=parser.parBool(ok,0);
insilmaril@434
  1081
			if (ok) setIncludeImagesHor(b);
insilmaril@434
  1082
		}
insilmaril@434
  1083
	} else if (com=="setIncludeImagesVertically")
insilmaril@434
  1084
	{
insilmaril@434
  1085
		if (xelection.isEmpty() )
insilmaril@434
  1086
		{
insilmaril@434
  1087
			parser.setError (Aborted,"Nothing selected");
insilmaril@434
  1088
		} else if (! selb)
insilmaril@434
  1089
		{				  
insilmaril@434
  1090
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@434
  1091
		} else if (parser.checkParamCount(1))
insilmaril@434
  1092
		{
insilmaril@434
  1093
			b=parser.parBool(ok,0);
insilmaril@434
  1094
			if (ok) setIncludeImagesVer(b);
insilmaril@434
  1095
		}
insilmaril@491
  1096
	} else if (com=="setHideLinkUnselected")
insilmaril@491
  1097
	{
insilmaril@491
  1098
		if (xelection.isEmpty() )
insilmaril@491
  1099
		{
insilmaril@491
  1100
			parser.setError (Aborted,"Nothing selected");
insilmaril@491
  1101
		} else if ( xelection.type()!=Selection::Branch && xelection.type()!= Selection::MapCenter && xelection.type()!=Selection::FloatImage)
insilmaril@491
  1102
		{				  
insilmaril@491
  1103
			parser.setError (Aborted,"Type of selection does not allow hiding the link");
insilmaril@491
  1104
		} else if (parser.checkParamCount(1))
insilmaril@491
  1105
		{
insilmaril@491
  1106
			b=parser.parBool(ok,0);
insilmaril@491
  1107
			if (ok) setHideLinkUnselected(b);
insilmaril@491
  1108
		}
insilmaril@442
  1109
	} else if (com=="setSelectionColor")
insilmaril@442
  1110
	{
insilmaril@442
  1111
		if (parser.checkParamCount(1))
insilmaril@442
  1112
		{
insilmaril@442
  1113
			QColor c=parser.parColor (ok,0);
insilmaril@442
  1114
			if (ok) setSelectionColorInt (c);
insilmaril@442
  1115
		}	
insilmaril@143
  1116
	} else if (com=="setURL")
insilmaril@143
  1117
	{
insilmaril@424
  1118
		if (xelection.isEmpty() )
insilmaril@394
  1119
		{
insilmaril@432
  1120
			parser.setError (Aborted,"Nothing selected");
insilmaril@424
  1121
		} else if (! selb )
insilmaril@394
  1122
		{				  
insilmaril@432
  1123
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@432
  1124
		} else if (parser.checkParamCount(1))
insilmaril@143
  1125
		{
insilmaril@432
  1126
			s=parser.parString (ok,0);
insilmaril@491
  1127
			if (ok) setURL(s);
insilmaril@143
  1128
		}	
insilmaril@143
  1129
	} else if (com=="setVymLink")
insilmaril@143
  1130
	{
insilmaril@424
  1131
		if (xelection.isEmpty() )
insilmaril@394
  1132
		{
insilmaril@432
  1133
			parser.setError (Aborted,"Nothing selected");
insilmaril@424
  1134
		} else if (! selb )
insilmaril@394
  1135
		{				  
insilmaril@432
  1136
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@432
  1137
		} else if (parser.checkParamCount(1))
insilmaril@143
  1138
		{
insilmaril@432
  1139
			s=parser.parString (ok,0);
insilmaril@366
  1140
			if (ok) setVymLinkInt(s);
insilmaril@143
  1141
		}	
insilmaril@143
  1142
	}
insilmaril@366
  1143
	else if (com=="setFlag")
insilmaril@366
  1144
	{
insilmaril@424
  1145
		if (xelection.isEmpty() )
insilmaril@366
  1146
		{
insilmaril@432
  1147
			parser.setError (Aborted,"Nothing selected");
insilmaril@424
  1148
		} else if (! selb )
insilmaril@394
  1149
		{				  
insilmaril@432
  1150
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@432
  1151
		} else if (parser.checkParamCount(1))
insilmaril@394
  1152
		{
insilmaril@432
  1153
			s=parser.parString(ok,0);
insilmaril@394
  1154
			if (ok) 
insilmaril@394
  1155
			{
insilmaril@424
  1156
				selb->activateStandardFlag(s);
insilmaril@424
  1157
				selb->updateFlagsToolbar();
insilmaril@366
  1158
			}	
insilmaril@366
  1159
		}
insilmaril@445
  1160
	} else if (com=="setFrameType")
insilmaril@445
  1161
	{
insilmaril@445
  1162
		if (xelection.isEmpty() )
insilmaril@445
  1163
		{
insilmaril@445
  1164
			parser.setError (Aborted,"Nothing selected");
insilmaril@445
  1165
		} else if (! selb )
insilmaril@445
  1166
		{				  
insilmaril@445
  1167
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@445
  1168
		} else if (parser.checkParamCount(1))
insilmaril@445
  1169
		{
insilmaril@445
  1170
			s=parser.parString(ok,0);
insilmaril@445
  1171
			if (ok) 
insilmaril@445
  1172
				setFrameType (s);
insilmaril@445
  1173
		}
insilmaril@447
  1174
	} else if (com=="toggleFlag")
insilmaril@447
  1175
	{
insilmaril@447
  1176
		if (xelection.isEmpty() )
insilmaril@447
  1177
		{
insilmaril@447
  1178
			parser.setError (Aborted,"Nothing selected");
insilmaril@447
  1179
		} else if (! selb )
insilmaril@447
  1180
		{				  
insilmaril@447
  1181
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@447
  1182
		} else if (parser.checkParamCount(1))
insilmaril@447
  1183
		{
insilmaril@447
  1184
			s=parser.parString(ok,0);
insilmaril@447
  1185
			if (ok) 
insilmaril@447
  1186
			{
insilmaril@447
  1187
				selb->toggleStandardFlag(s);	
insilmaril@447
  1188
				selb->updateFlagsToolbar();
insilmaril@447
  1189
			}	
insilmaril@447
  1190
		}
insilmaril@427
  1191
	} else if (com=="unscroll")
insilmaril@427
  1192
	{
insilmaril@427
  1193
		if (xelection.isEmpty() )
insilmaril@427
  1194
		{
insilmaril@432
  1195
			parser.setError (Aborted,"Nothing selected");
insilmaril@427
  1196
		} else if (! selb )
insilmaril@427
  1197
		{				  
insilmaril@432
  1198
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@432
  1199
		} else if (parser.checkParamCount(0))
insilmaril@427
  1200
		{	
insilmaril@473
  1201
			if (!unscrollBranch (selb))	
insilmaril@432
  1202
				parser.setError (Aborted,"Could not unscroll branch");
insilmaril@427
  1203
		}	
insilmaril@473
  1204
	} else if (com=="unscrollChilds")
insilmaril@473
  1205
	{
insilmaril@473
  1206
		if (xelection.isEmpty() )
insilmaril@473
  1207
		{
insilmaril@473
  1208
			parser.setError (Aborted,"Nothing selected");
insilmaril@473
  1209
		} else if (! selb )
insilmaril@473
  1210
		{				  
insilmaril@473
  1211
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@473
  1212
		} else if (parser.checkParamCount(0))
insilmaril@473
  1213
		{	
insilmaril@473
  1214
			unscrollChilds ();
insilmaril@473
  1215
		}	
insilmaril@427
  1216
	} else if (com=="unsetFlag")
insilmaril@366
  1217
	{
insilmaril@424
  1218
		if (xelection.isEmpty() )
insilmaril@366
  1219
		{
insilmaril@432
  1220
			parser.setError (Aborted,"Nothing selected");
insilmaril@424
  1221
		} else if (! selb )
insilmaril@394
  1222
		{				  
insilmaril@432
  1223
			parser.setError (Aborted,"Type of selection is not a branch");
insilmaril@432
  1224
		} else if (parser.checkParamCount(1))
insilmaril@394
  1225
		{
insilmaril@432
  1226
			s=parser.parString(ok,0);
insilmaril@394
  1227
			if (ok) 
insilmaril@394
  1228
			{
insilmaril@424
  1229
				selb->deactivateStandardFlag(s);
insilmaril@424
  1230
				selb->updateFlagsToolbar();
insilmaril@366
  1231
			}	
insilmaril@366
  1232
		}
insilmaril@398
  1233
	} else
insilmaril@432
  1234
		parser.setError (Aborted,"Unknown command");
insilmaril@129
  1235
insilmaril@129
  1236
	// Any errors?
insilmaril@432
  1237
	if (parser.errorLevel()==NoError)
insilmaril@432
  1238
	{
insilmaril@394
  1239
		setChanged();
insilmaril@432
  1240
		mapCenter->reposition();
insilmaril@432
  1241
	}	
insilmaril@394
  1242
	else	
insilmaril@106
  1243
	{
insilmaril@366
  1244
		// TODO Error handling
insilmaril@366
  1245
		qWarning("MapEditor::parseAtom: Error!");
insilmaril@432
  1246
		qWarning(parser.errorMessage());
insilmaril@394
  1247
	} 
insilmaril@0
  1248
}
insilmaril@0
  1249
insilmaril@432
  1250
void MapEditor::runScript (QString script)
insilmaril@432
  1251
{
insilmaril@445
  1252
	parser.setScript (script);
insilmaril@445
  1253
	parser.runScript();
insilmaril@447
  1254
	while (parser.next() ) 
insilmaril@447
  1255
		parseAtom(parser.getAtom());
insilmaril@432
  1256
}
insilmaril@0
  1257
insilmaril@0
  1258
bool MapEditor::isDefault()
insilmaril@0
  1259
{
insilmaril@0
  1260
    return mapDefault;
insilmaril@0
  1261
}
insilmaril@0
  1262
insilmaril@0
  1263
bool MapEditor::hasChanged()
insilmaril@0
  1264
{
insilmaril@0
  1265
    return mapChanged;
insilmaril@0
  1266
}
insilmaril@0
  1267
insilmaril@0
  1268
void MapEditor::setChanged()
insilmaril@0
  1269
{
insilmaril@473
  1270
	if (!mapChanged)
insilmaril@473
  1271
		autosaveTimer->start(settings.value("/mapeditor/autosave/ms/",300000).toInt());
insilmaril@0
  1272
	mapChanged=true;
insilmaril@0
  1273
	mapDefault=false;
insilmaril@0
  1274
	mapUnsaved=true;
insilmaril@0
  1275
	findReset();
insilmaril@473
  1276
insilmaril@0
  1277
}
insilmaril@0
  1278
insilmaril@0
  1279
void MapEditor::closeMap()
insilmaril@0
  1280
{
insilmaril@0
  1281
	// Unselect before disabling the toolbar actions
insilmaril@424
  1282
	if (!xelection.isEmpty() ) xelection.unselect();
insilmaril@424
  1283
	xelection.clear();
insilmaril@0
  1284
	updateActions();
insilmaril@0
  1285
insilmaril@0
  1286
    clear();
insilmaril@0
  1287
	close();
insilmaril@0
  1288
}
insilmaril@0
  1289
insilmaril@486
  1290
void MapEditor::setFilePath(QString fpath, QString destname)
insilmaril@0
  1291
{
insilmaril@486
  1292
	if (fpath.isEmpty() || fpath=="")
insilmaril@106
  1293
	{
insilmaril@106
  1294
		filePath="";
insilmaril@106
  1295
		fileName="";
insilmaril@106
  1296
		destPath="";
insilmaril@106
  1297
	} else
insilmaril@106
  1298
	{
insilmaril@486
  1299
		filePath=fpath;		// becomes absolute path
insilmaril@486
  1300
		fileName=fpath;		// gets stripped of path
insilmaril@106
  1301
		destPath=destname;	// needed for vymlinks
insilmaril@106
  1302
insilmaril@486
  1303
		// If fpath is not an absolute path, complete it
insilmaril@486
  1304
		filePath=QDir(fpath).absPath();
insilmaril@106
  1305
		fileDir=filePath.left (1+filePath.findRev ("/"));
insilmaril@106
  1306
insilmaril@106
  1307
		// Set short name, too. Search from behind:
insilmaril@106
  1308
		int i=fileName.findRev("/");
insilmaril@106
  1309
		if (i>=0) fileName=fileName.remove (0,i+1);
insilmaril@106
  1310
insilmaril@106
  1311
		// Forget the .vym (or .xml) for name of map
insilmaril@106
  1312
		mapName=fileName.left(fileName.findRev(".",-1,true) );
insilmaril@106
  1313
	}
insilmaril@0
  1314
}
insilmaril@0
  1315
insilmaril@486
  1316
void MapEditor::setFilePath(QString fpath)
insilmaril@486
  1317
{
insilmaril@486
  1318
	setFilePath (fpath,fpath);
insilmaril@486
  1319
}
insilmaril@486
  1320
insilmaril@0
  1321
QString MapEditor::getFilePath()
insilmaril@0
  1322
{
insilmaril@0
  1323
	return filePath;
insilmaril@0
  1324
}
insilmaril@0
  1325
insilmaril@0
  1326
QString MapEditor::getFileName()
insilmaril@0
  1327
{
insilmaril@0
  1328
	return fileName;
insilmaril@0
  1329
}
insilmaril@0
  1330
insilmaril@0
  1331
QString MapEditor::getMapName()
insilmaril@0
  1332
{
insilmaril@0
  1333
	return mapName;
insilmaril@0
  1334
}
insilmaril@0
  1335
insilmaril@0
  1336
QString MapEditor::getDestPath()
insilmaril@0
  1337
{
insilmaril@0
  1338
	return destPath;
insilmaril@0
  1339
}
insilmaril@0
  1340
insilmaril@205
  1341
ErrorCode MapEditor::load (QString fname, LoadMode lmode)
insilmaril@0
  1342
{
insilmaril@205
  1343
	ErrorCode err=success;
insilmaril@0
  1344
insilmaril@0
  1345
	if (lmode==NewMap)
insilmaril@0
  1346
	{
insilmaril@424
  1347
		if (xelection.isEmpty() ) xelection.unselect();
insilmaril@424
  1348
		xelection.clear();
insilmaril@0
  1349
		mapCenter->clear();
insilmaril@0
  1350
		mapCenter->setMapEditor(this);
insilmaril@0
  1351
		// (map state is set later at end of load...)
insilmaril@0
  1352
	} else
insilmaril@0
  1353
	{
insilmaril@424
  1354
		BranchObj *bo=xelection.getBranch();
insilmaril@424
  1355
		if (!bo) return aborted;
insilmaril@395
  1356
		if (lmode==ImportAdd)
insilmaril@395
  1357
			saveStateChangingPart(
insilmaril@424
  1358
				bo,
insilmaril@424
  1359
				bo,
insilmaril@395
  1360
				QString("addMapInsert (%1)").arg(fname),
insilmaril@395
  1361
				QString("Add map %1 to %2").arg(fname).arg(getName(bo)));
insilmaril@395
  1362
		else	
insilmaril@395
  1363
			saveStateChangingPart(
insilmaril@424
  1364
				bo,
insilmaril@424
  1365
				bo,
insilmaril@395
  1366
				QString("addMapReplace(%1)").arg(fname),
insilmaril@395
  1367
				QString("Add map %1 to %2").arg(fname).arg(getName(bo)));
insilmaril@0
  1368
	}	
insilmaril@0
  1369
	
insilmaril@0
  1370
    
insilmaril@0
  1371
    mapBuilderHandler handler;
insilmaril@0
  1372
	QFile file( fname );
insilmaril@0
  1373
insilmaril@0
  1374
	// I am paranoid: file should exist anyway
insilmaril@0
  1375
	// according to check in mainwindow.
insilmaril@0
  1376
	if (!file.exists() )
insilmaril@0
  1377
	{
insilmaril@0
  1378
		QMessageBox::critical( 0, tr( "Critical Parse Error" ),
insilmaril@0
  1379
				   tr("Couldn't open map " +fname)+".");
insilmaril@205
  1380
		err=aborted;	
insilmaril@0
  1381
	} else
insilmaril@0
  1382
	{
insilmaril@473
  1383
		bool blockSaveStateOrg=blockSaveState;
insilmaril@139
  1384
		blockReposition=true;
insilmaril@473
  1385
		blockSaveState=true;
insilmaril@0
  1386
		QXmlInputSource source( file);
insilmaril@0
  1387
		QXmlSimpleReader reader;
insilmaril@0
  1388
		reader.setContentHandler( &handler );
insilmaril@0
  1389
		reader.setErrorHandler( &handler );
insilmaril@0
  1390
		handler.setMapEditor( this );
insilmaril@494
  1391
insilmaril@494
  1392
insilmaril@494
  1393
		// We need to set the tmpDir in order  to load files with rel. path
insilmaril@494
  1394
		QString tmpdir= fname.left(fname.findRev("/",-1));	
insilmaril@494
  1395
		handler.setTmpDir (tmpdir);
insilmaril@243
  1396
		handler.setInputFile (file.name());
insilmaril@0
  1397
		handler.setLoadMode (lmode);
insilmaril@0
  1398
		bool ok = reader.parse( source );
insilmaril@139
  1399
		blockReposition=false;
insilmaril@473
  1400
		blockSaveState=blockSaveStateOrg;
insilmaril@0
  1401
		file.close();
insilmaril@0
  1402
		if ( ok ) 
insilmaril@0
  1403
		{
insilmaril@0
  1404
			mapCenter->reposition();
insilmaril@442
  1405
			xelection.update();
insilmaril@0
  1406
			if (lmode==NewMap)
insilmaril@0
  1407
			{
insilmaril@0
  1408
				mapDefault=false;
insilmaril@0
  1409
				mapChanged=false;
insilmaril@0
  1410
				mapUnsaved=false;
insilmaril@473
  1411
				autosaveTimer->stop();
insilmaril@0
  1412
			}
insilmaril@0
  1413
		} else 
insilmaril@0
  1414
		{
insilmaril@0
  1415
			QMessageBox::critical( 0, tr( "Critical Parse Error" ),
insilmaril@0
  1416
					   tr( handler.errorProtocol() ) );
insilmaril@0
  1417
			// returnCode=1;	
insilmaril@0
  1418
			// Still return "success": the map maybe at least
insilmaril@0
  1419
			// partially read by the parser
insilmaril@0
  1420
		}	
insilmaril@0
  1421
	}	
insilmaril@0
  1422
	updateActions();
insilmaril@205
  1423
	return err;
insilmaril@0
  1424
}
insilmaril@0
  1425
insilmaril@0
  1426
int MapEditor::save (const SaveMode &savemode)
insilmaril@0
  1427
{
insilmaril@0
  1428
	int returnCode=0;
insilmaril@0
  1429
insilmaril@0
  1430
	// Create mapName and fileDir
insilmaril@0
  1431
	makeSubDirs (fileDir);
insilmaril@0
  1432
	QString fname;
insilmaril@0
  1433
	if (saveZipped())
insilmaril@0
  1434
		// save as .xml
insilmaril@0
  1435
		fname=mapName+".xml";
insilmaril@0
  1436
	else
insilmaril@0
  1437
		// use name given by user, even if he chooses .doc
insilmaril@0
  1438
		fname=fileName;
insilmaril@0
  1439
insilmaril@0
  1440
insilmaril@0
  1441
	QString saveFile;
insilmaril@424
  1442
	if (savemode==CompleteMap || xelection.isEmpty())
insilmaril@408
  1443
		saveFile=saveToDir (fileDir,mapName+"-",true,QPointF(),NULL);
insilmaril@106
  1444
	else	
insilmaril@491
  1445
	{
insilmaril@491
  1446
		// TODO take care of multiselections
insilmaril@491
  1447
		if (xelection.type()==Selection::FloatImage)
insilmaril@491
  1448
			saveFloatImage();
insilmaril@491
  1449
		else	
insilmaril@491
  1450
			saveFile=saveToDir (fileDir,mapName+"-",true,QPointF(),xelection.getBranch());	
insilmaril@491
  1451
	}	
insilmaril@0
  1452
insilmaril@163
  1453
	if (!saveStringToDisk(fileDir+fname,saveFile))
insilmaril@0
  1454
		return 1;
insilmaril@0
  1455
insilmaril@0
  1456
	if (returnCode==0)
insilmaril@0
  1457
	{
insilmaril@0
  1458
		mapChanged=false;
insilmaril@0
  1459
		mapUnsaved=false;
insilmaril@473
  1460
		autosaveTimer->stop();
insilmaril@390
  1461
		updateActions();
insilmaril@0
  1462
	}
insilmaril@0
  1463
insilmaril@0
  1464
	return returnCode;
insilmaril@0
  1465
}
insilmaril@0
  1466
insilmaril@0
  1467
void MapEditor::setZipped (bool z)
insilmaril@0
  1468
{
insilmaril@0
  1469
	zipped=z;
insilmaril@0
  1470
}
insilmaril@0
  1471
insilmaril@0
  1472
bool MapEditor::saveZipped ()
insilmaril@0
  1473
{
insilmaril@0
  1474
	return zipped;
insilmaril@0
  1475
}
insilmaril@0
  1476
insilmaril@0
  1477
void MapEditor::print()
insilmaril@0
  1478
{
insilmaril@0
  1479
	if ( !printer ) 
insilmaril@0
  1480
	{
insilmaril@0
  1481
		printer = new QPrinter;
insilmaril@0
  1482
		printer->setColorMode (QPrinter::Color);
insilmaril@366
  1483
		printer->setPrinterName (settings.value("/mainwindow/printerName",printer->printerName()).toString());
insilmaril@412
  1484
		printer->setOutputFormat((QPrinter::OutputFormat)settings.value("/mainwindow/printerFormat",printer->outputFormat()).toInt());
insilmaril@412
  1485
		printer->setOutputFileName(settings.value("/mainwindow/printerFileName",printer->outputFileName()).toString());
insilmaril@0
  1486
	}
insilmaril@0
  1487
insilmaril@408
  1488
	QRectF totalBBox=mapCenter->getTotalBBox();
insilmaril@0
  1489
insilmaril@0
  1490
	// Try to set orientation automagically
insilmaril@0
  1491
	// Note: Interpretation of generated postscript is amibiguous, if 
insilmaril@0
  1492
	// there are problems with landscape mode, see
insilmaril@0
  1493
	// http://sdb.suse.de/de/sdb/html/jsmeix_print-cups-landscape-81.html
insilmaril@0
  1494
insilmaril@0
  1495
	if (totalBBox.width()>totalBBox.height())
insilmaril@0
  1496
		// recommend landscape
insilmaril@0
  1497
		printer->setOrientation (QPrinter::Landscape);
insilmaril@0
  1498
	else	
insilmaril@0
  1499
		// recommend portrait
insilmaril@0
  1500
		printer->setOrientation (QPrinter::Portrait);
insilmaril@0
  1501
insilmaril@0
  1502
	if ( printer->setup(this) ) 
insilmaril@0
  1503
	// returns false, if printing is canceled
insilmaril@0
  1504
	{
insilmaril@0
  1505
		QPainter pp(printer);
insilmaril@0
  1506
insilmaril@412
  1507
		pp.setRenderHint(QPainter::Antialiasing,true);
insilmaril@412
  1508
insilmaril@0
  1509
		// Don't print the visualisation of selection
insilmaril@424
  1510
		xelection.unselect();
insilmaril@0
  1511
insilmaril@408
  1512
		QRectF mapRect=totalBBox;
insilmaril@411
  1513
		QGraphicsRectItem *frame=NULL;
insilmaril@412
  1514
insilmaril@417
  1515
		if (printFrame) 
insilmaril@0
  1516
		{
insilmaril@417
  1517
			// Print frame around map
insilmaril@417
  1518
			mapRect.setRect (totalBBox.x()-10, totalBBox.y()-10, 
insilmaril@417
  1519
				totalBBox.width()+20, totalBBox.height()+20);
insilmaril@417
  1520
			frame=mapScene->addRect (mapRect, QPen(Qt::black),QBrush(Qt::NoBrush));
insilmaril@417
  1521
			frame->setZValue(0);
insilmaril@417
  1522
			frame->show();    
insilmaril@417
  1523
		}		
insilmaril@417
  1524
insilmaril@417
  1525
insilmaril@412
  1526
		double paperAspect = (double)printer->width()   / (double)printer->height();
insilmaril@412
  1527
		double   mapAspect = (double)mapRect.width() / (double)mapRect.height();
insilmaril@417
  1528
		int viewBottom;
insilmaril@0
  1529
		if (mapAspect>=paperAspect)
insilmaril@0
  1530
		{
insilmaril@0
  1531
			// Fit horizontally to paper width
insilmaril@417
  1532
			//pp.setViewport(0,0, printer->width(),(int)(printer->width()/mapAspect) );	
insilmaril@417
  1533
			viewBottom=(int)(printer->width()/mapAspect);	
insilmaril@0
  1534
		}	else
insilmaril@0
  1535
		{
insilmaril@0
  1536
			// Fit vertically to paper height
insilmaril@417
  1537
			//pp.setViewport(0,0,(int)(printer->height()*mapAspect),printer->height());	
insilmaril@417
  1538
			viewBottom=printer->height();	
insilmaril@0
  1539
		}	
insilmaril@417
  1540
		
insilmaril@417
  1541
		if (printFooter) 
insilmaril@417
  1542
		{
insilmaril@417
  1543
			// Print footer below map
insilmaril@417
  1544
			QFont font;		
insilmaril@417
  1545
			font.setPointSize(10);
insilmaril@417
  1546
			pp.setFont (font);
insilmaril@417
  1547
			QRectF footerBox(0,viewBottom,printer->width(),15);
insilmaril@417
  1548
			pp.drawText ( footerBox,Qt::AlignLeft,"VYM - " +fileName);
insilmaril@417
  1549
			pp.drawText ( footerBox, Qt::AlignRight, QDate::currentDate().toString(Qt::TextDate));
insilmaril@417
  1550
		}
insilmaril@412
  1551
		mapScene->render (
insilmaril@412
  1552
			&pp, 
insilmaril@412
  1553
			QRectF (0,0,printer->width(),printer->height()-15),
insilmaril@412
  1554
			QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height())
insilmaril@412
  1555
		);
insilmaril@412
  1556
		
insilmaril@412
  1557
		// Viewport has paper dimension
insilmaril@412
  1558
		if (frame)  delete (frame);
insilmaril@0
  1559
insilmaril@0
  1560
		// Restore selection
insilmaril@424
  1561
		xelection.reselect();
insilmaril@119
  1562
insilmaril@119
  1563
		// Save settings in vymrc
insilmaril@366
  1564
		settings.writeEntry("/mainwindow/printerName",printer->printerName());
insilmaril@412
  1565
		settings.writeEntry("/mainwindow/printerFormat",printer->outputFormat());
insilmaril@412
  1566
		settings.writeEntry("/mainwindow/printerFileName",printer->outputFileName());
insilmaril@0
  1567
	}
insilmaril@0
  1568
}
insilmaril@0
  1569
insilmaril@408
  1570
void MapEditor::setAntiAlias (bool b)
insilmaril@408
  1571
{
insilmaril@408
  1572
	setRenderHint(QPainter::Antialiasing,b);
insilmaril@408
  1573
}
insilmaril@408
  1574
insilmaril@408
  1575
void MapEditor::setSmoothPixmap(bool b)
insilmaril@408
  1576
{
insilmaril@408
  1577
	setRenderHint(QPainter::SmoothPixmapTransform,b);
insilmaril@408
  1578
}
insilmaril@408
  1579
insilmaril@0
  1580
QPixmap MapEditor::getPixmap()
insilmaril@0
  1581
{
insilmaril@408
  1582
	QRectF mapRect=mapCenter->getTotalBBox();
insilmaril@450
  1583
	QPixmap pix((int)mapRect.width()+2,(int)mapRect.height()+2);
insilmaril@0
  1584
	QPainter pp (&pix);
insilmaril@420
  1585
	
insilmaril@420
  1586
	pp.setRenderHints(renderHints());
insilmaril@0
  1587
insilmaril@0
  1588
	// Don't print the visualisation of selection
insilmaril@424
  1589
	xelection.unselect();
insilmaril@0
  1590
insilmaril@420
  1591
	mapScene->render (	&pp, 
insilmaril@450
  1592
		QRectF(0,0,mapRect.width()+2,mapRect.height()+2),
insilmaril@420
  1593
		QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height() ));
insilmaril@420
  1594
insilmaril@0
  1595
	// Restore selection
insilmaril@424
  1596
	xelection.reselect();
insilmaril@0
  1597
	
insilmaril@0
  1598
	return pix;
insilmaril@0
  1599
}
insilmaril@0
  1600
insilmaril@473
  1601
void MapEditor::setHideTmpMode (BranchObj::HideTmpMode mode)
insilmaril@260
  1602
{
insilmaril@260
  1603
	hidemode=mode;
insilmaril@260
  1604
	mapCenter->setHideTmp (hidemode);
insilmaril@260
  1605
	mapCenter->reposition();
insilmaril@408
  1606
	scene()->update();
insilmaril@260
  1607
}
insilmaril@260
  1608
insilmaril@473
  1609
BranchObj::HideTmpMode MapEditor::getHideTmpMode()
insilmaril@260
  1610
{
insilmaril@260
  1611
	return hidemode;
insilmaril@260
  1612
}
insilmaril@260
  1613
insilmaril@0
  1614
void MapEditor::exportImage(QString fn)
insilmaril@0
  1615
{
insilmaril@260
  1616
	setExportMode (true);
insilmaril@0
  1617
	QPixmap pix (getPixmap());
insilmaril@0
  1618
	pix.save(fn, "PNG");
insilmaril@260
  1619
	setExportMode (false);
insilmaril@260
  1620
}
insilmaril@260
  1621
insilmaril@260
  1622
void MapEditor::setExportMode (bool b)
insilmaril@260
  1623
{
insilmaril@260
  1624
	// should be called before and after exports
insilmaril@260
  1625
	// depending on the settings
insilmaril@366
  1626
	if (b && settings.value("/export/useHideExport","yes")=="yes")
insilmaril@473
  1627
		setHideTmpMode (BranchObj::HideExport);
insilmaril@260
  1628
	else	
insilmaril@473
  1629
		setHideTmpMode (BranchObj::HideNone);
insilmaril@0
  1630
}
insilmaril@0
  1631
insilmaril@366
  1632
void MapEditor::exportImage(QString fn, QString format)
insilmaril@0
  1633
{
insilmaril@260
  1634
	setExportMode (true);
insilmaril@0
  1635
	QPixmap pix (getPixmap());
insilmaril@366
  1636
	pix.save(fn, format);
insilmaril@260
  1637
	setExportMode (false);
insilmaril@0
  1638
}
insilmaril@0
  1639
insilmaril@185
  1640
void MapEditor::exportOOPresentation(const QString &fn, const QString &cf)
insilmaril@163
  1641
{
insilmaril@185
  1642
	ExportOO ex;
insilmaril@199
  1643
	ex.setFile (fn);
insilmaril@185
  1644
	ex.setMapCenter(mapCenter);
insilmaril@260
  1645
	if (ex.setConfigFile(cf)) 
insilmaril@260
  1646
	{
insilmaril@260
  1647
		setExportMode (true);
insilmaril@260
  1648
		ex.exportPresentation();
insilmaril@260
  1649
		setExportMode (false);
insilmaril@260
  1650
	}
insilmaril@163
  1651
}
insilmaril@163
  1652
insilmaril@163
  1653
insilmaril@0
  1654
insilmaril@0
  1655
void MapEditor::exportXML(const QString &dir)
insilmaril@0
  1656
{
insilmaril@260
  1657
	// Hide stuff during export, if settings want this
insilmaril@260
  1658
	setExportMode (true);
insilmaril@260
  1659
insilmaril@0
  1660
	// Create subdirectories
insilmaril@0
  1661
	makeSubDirs (dir);
insilmaril@0
  1662
insilmaril@0
  1663
	// write to directory
insilmaril@106
  1664
	QString saveFile=saveToDir (dir,mapName+"-",true,mapCenter->getTotalBBox().topLeft() ,NULL);
insilmaril@0
  1665
	QFile file;
insilmaril@0
  1666
insilmaril@0
  1667
	file.setName ( dir + "/"+mapName+".xml");
insilmaril@366
  1668
	if ( !file.open( QIODevice::WriteOnly ) )
insilmaril@0
  1669
	{
insilmaril@0
  1670
		// This should neverever happen
insilmaril@159
  1671
		QMessageBox::critical (0,tr("Critical Export Error"),tr("MapEditor::exportXML couldn't open %1").arg(file.name()));
insilmaril@0
  1672
		return;
insilmaril@0
  1673
	}	
insilmaril@0
  1674
insilmaril@0
  1675
	// Write it finally, and write in UTF8, no matter what 
insilmaril@0
  1676
	QTextStream ts( &file );
insilmaril@0
  1677
	ts.setEncoding (QTextStream::UnicodeUTF8);
insilmaril@0
  1678
	ts << saveFile;
insilmaril@0
  1679
	file.close();
insilmaril@0
  1680
insilmaril@0
  1681
	// Now write image, too
insilmaril@0
  1682
	exportImage (dir+"/images/"+mapName+".png");
insilmaril@260
  1683
insilmaril@260
  1684
	setExportMode (false);
insilmaril@0
  1685
}
insilmaril@0
  1686
insilmaril@0
  1687
void MapEditor::clear()
insilmaril@0
  1688
{
insilmaril@424
  1689
	xelection.unselect();
insilmaril@0
  1690
	mapCenter->clear();
insilmaril@0
  1691
}
insilmaril@0
  1692
insilmaril@0
  1693
void MapEditor::copy()
insilmaril@0
  1694
{
insilmaril@424
  1695
	LinkableMapObj *sel=xelection.single();
insilmaril@424
  1696
	if (sel)
insilmaril@0
  1697
	{
insilmaril@502
  1698
		if (redosAvail == 0)
insilmaril@0
  1699
		{
insilmaril@502
  1700
			// Copy to history
insilmaril@502
  1701
			QString s=sel->getSelectString();
insilmaril@502
  1702
			saveState (PartOfMap, s, "nop ()", s, "copy ()","Copy to clipboard",sel  );
insilmaril@502
  1703
			curClipboard=curStep;
insilmaril@502
  1704
		}
insilmaril@502
  1705
insilmaril@502
  1706
		// Copy also to global clipboard, because we are at last step in history
insilmaril@502
  1707
		QString bakMapName=QDir::convertSeparators (QString("history-%1").arg(curStep));
insilmaril@502
  1708
		QString bakMapDir=QDir::convertSeparators (tmpMapDir +"/"+bakMapName);
insilmaril@502
  1709
		copyDir (bakMapDir,clipboardDir );
insilmaril@166
  1710
insilmaril@166
  1711
		clipboardEmpty=false;
insilmaril@166
  1712
		updateActions();
insilmaril@0
  1713
	}	    
insilmaril@0
  1714
}
insilmaril@0
  1715
insilmaril@163
  1716
void MapEditor::redo()
insilmaril@163
  1717
{
insilmaril@381
  1718
	// Can we undo at all?
insilmaril@381
  1719
	if (redosAvail<1) return;
insilmaril@473
  1720
insilmaril@473
  1721
	bool blockSaveStateOrg=blockSaveState;
insilmaril@473
  1722
	blockSaveState=true;
insilmaril@473
  1723
	
insilmaril@381
  1724
	redosAvail--;
insilmaril@381
  1725
insilmaril@381
  1726
	if (undosAvail<stepsTotal) undosAvail++;
insilmaril@366
  1727
	curStep++;
insilmaril@381
  1728
	if (curStep>stepsTotal) curStep=1;
insilmaril@366
  1729
	QString undoCommand=  undoSet.readEntry (QString("/history/step-%1/undoCommand").arg(curStep));
insilmaril@366
  1730
	QString undoSelection=undoSet.readEntry (QString("/history/step-%1/undoSelection").arg(curStep));
insilmaril@366
  1731
	QString redoCommand=  undoSet.readEntry (QString("/history/step-%1/redoCommand").arg(curStep));
insilmaril@366
  1732
	QString redoSelection=undoSet.readEntry (QString("/history/step-%1/redoSelection").arg(curStep));
insilmaril@366
  1733
	QString comment=undoSet.readEntry (QString("/history/step-%1/comment").arg(curStep));
insilmaril@395
  1734
	QString version=undoSet.readEntry ("/history/version");
insilmaril@395
  1735
insilmaril@395
  1736
	if (!checkVersion(version))
insilmaril@395
  1737
		QMessageBox::warning(0,tr("Warning"),
insilmaril@404
  1738
			tr("Version %1 of saved undo/redo data\ndoes not match current vym version %2.").arg(version).arg(vymVersion));
insilmaril@395
  1739
insilmaril@366
  1740
insilmaril@163
  1741
	// Find out current undo directory
insilmaril@366
  1742
	QString bakMapDir=QDir::convertSeparators (QString(tmpMapDir+"/undo-%1").arg(curStep));
insilmaril@366
  1743
insilmaril@473
  1744
	if (debug)
insilmaril@473
  1745
	{
insilmaril@473
  1746
		cout << "ME::redo() begin\n";
insilmaril@473
  1747
		cout << "    undosAvail="<<undosAvail<<endl;
insilmaril@473
  1748
		cout << "    redosAvail="<<redosAvail<<endl;
insilmaril@473
  1749
		cout << "       curStep="<<curStep<<endl;
insilmaril@473
  1750
		cout << "    ---------------------------"<<endl;
insilmaril@473
  1751
		cout << "    comment="<<comment.toStdString()<<endl;
insilmaril@473
  1752
		cout << "    undoCom="<<undoCommand.toStdString()<<endl;
insilmaril@473
  1753
		cout << "    undoSel="<<undoSelection.toStdString()<<endl;
insilmaril@473
  1754
		cout << "    redoCom="<<redoCommand.toStdString()<<endl;
insilmaril@473
  1755
		cout << "    redoSel="<<redoSelection.toStdString()<<endl;
insilmaril@473
  1756
		cout << "    ---------------------------"<<endl<<endl;
insilmaril@473
  1757
	}
insilmaril@163
  1758
insilmaril@163
  1759
	// select  object before redo
insilmaril@163
  1760
	if (!redoSelection.isEmpty())
insilmaril@163
  1761
		select (redoSelection);
insilmaril@163
  1762
insilmaril@366
  1763
insilmaril@366
  1764
	parseAtom (redoCommand);
insilmaril@366
  1765
	mapCenter->reposition();
insilmaril@366
  1766
insilmaril@473
  1767
	blockSaveState=blockSaveStateOrg;
insilmaril@366
  1768
insilmaril@366
  1769
	undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
insilmaril@366
  1770
	undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
insilmaril@366
  1771
	undoSet.setEntry ("/history/curStep",QString::number(curStep));
insilmaril@366
  1772
	undoSet.writeSettings(histPath);
insilmaril@366
  1773
insilmaril@408
  1774
	mainWindow->updateHistory (undoSet);
insilmaril@407
  1775
	updateActions();
insilmaril@366
  1776
insilmaril@366
  1777
	/* TODO remove testing
insilmaril@163
  1778
	cout << "ME::redo() end\n";
insilmaril@163
  1779
	cout << "    undosAvail="<<undosAvail<<endl;
insilmaril@366
  1780
	cout << "    redosAvail="<<redosAvail<<endl;
insilmaril@366
  1781
	cout << "       curStep="<<curStep<<endl;
insilmaril@163
  1782
	cout << "    ---------------------------"<<endl<<endl;
insilmaril@404
  1783
	*/
insilmaril@366
  1784
insilmaril@366
  1785
insilmaril@163
  1786
}
insilmaril@163
  1787
insilmaril@390
  1788
bool MapEditor::isRedoAvailable()
insilmaril@390
  1789
{
insilmaril@393
  1790
	if (undoSet.readNumEntry("/history/redosAvail",0)>0)
insilmaril@390
  1791
		return true;
insilmaril@390
  1792
	else	
insilmaril@390
  1793
		return false;
insilmaril@390
  1794
}
insilmaril@390
  1795
insilmaril@106
  1796
void MapEditor::undo()
insilmaril@106
  1797
{
insilmaril@381
  1798
	// Can we undo at all?
insilmaril@381
  1799
	if (undosAvail<1) return;
insilmaril@381
  1800
insilmaril@502
  1801
	mainWindow->statusMessage (tr("Autosave disabled during undo."));
insilmaril@502
  1802
insilmaril@473
  1803
	bool blockSaveStateOrg=blockSaveState;
insilmaril@473
  1804
	blockSaveState=true;
insilmaril@473
  1805
	
insilmaril@366
  1806
	QString undoCommand=  undoSet.readEntry (QString("/history/step-%1/undoCommand").arg(curStep));
insilmaril@366
  1807
	QString undoSelection=undoSet.readEntry (QString("/history/step-%1/undoSelection").arg(curStep));
insilmaril@366
  1808
	QString redoCommand=  undoSet.readEntry (QString("/history/step-%1/redoCommand").arg(curStep));
insilmaril@366
  1809
	QString redoSelection=undoSet.readEntry (QString("/history/step-%1/redoSelection").arg(curStep));
insilmaril@366
  1810
	QString comment=undoSet.readEntry (QString("/history/step-%1/comment").arg(curStep));
insilmaril@395
  1811
	QString version=undoSet.readEntry ("/history/version");
insilmaril@395
  1812
insilmaril@395
  1813
	if (!checkVersion(version))
insilmaril@395
  1814
		QMessageBox::warning(0,tr("Warning"),
insilmaril@404
  1815
			tr("Version %1 of saved undo/redo data\ndoes not match current vym version %2.").arg(version).arg(vymVersion));
insilmaril@366
  1816
insilmaril@163
  1817
	// Find out current undo directory
insilmaril@366
  1818
	QString bakMapDir=QDir::convertSeparators (QString(tmpMapDir+"/undo-%1").arg(curStep));
insilmaril@163
  1819
insilmaril@136
  1820
	// select  object before undo
insilmaril@136
  1821
	if (!undoSelection.isEmpty())
insilmaril@136
  1822
		select (undoSelection);
insilmaril@136
  1823
insilmaril@473
  1824
	if (debug)
insilmaril@473
  1825
	{
insilmaril@473
  1826
		cout << "ME::undo() begin\n";
insilmaril@473
  1827
		cout << "    undosAvail="<<undosAvail<<endl;
insilmaril@473
  1828
		cout << "    redosAvail="<<redosAvail<<endl;
insilmaril@473
  1829
		cout << "       curStep="<<curStep<<endl;
insilmaril@473
  1830
		cout << "    ---------------------------"<<endl;
insilmaril@473
  1831
		cout << "    comment="<<comment.toStdString()<<endl;
insilmaril@473
  1832
		cout << "    undoCom="<<undoCommand.toStdString()<<endl;
insilmaril@473
  1833
		cout << "    undoSel="<<undoSelection.toStdString()<<endl;
insilmaril@473
  1834
		cout << "    redoCom="<<redoCommand.toStdString()<<endl;
insilmaril@473
  1835
		cout << "    redoSel="<<redoSelection.toStdString()<<endl;
insilmaril@473
  1836
		cout << "    ---------------------------"<<endl<<endl;
insilmaril@473
  1837
	}	
insilmaril@106
  1838
	parseAtom (undoCommand);
insilmaril@106
  1839
	mapCenter->reposition();
insilmaril@106
  1840
insilmaril@163
  1841
	undosAvail--;
insilmaril@366
  1842
	curStep--; 
insilmaril@381
  1843
	if (curStep<1) curStep=stepsTotal;
insilmaril@366
  1844
insilmaril@366
  1845
	redosAvail++;
insilmaril@163
  1846
insilmaril@473
  1847
	blockSaveState=blockSaveStateOrg;
insilmaril@163
  1848
/* TODO remove testing
insilmaril@163
  1849
	cout << "ME::undo() end\n";
insilmaril@163
  1850
	cout << "    undosAvail="<<undosAvail<<endl;
insilmaril@366
  1851
	cout << "    redosAvail="<<redosAvail<<endl;
insilmaril@366
  1852
	cout << "       curStep="<<curStep<<endl;
insilmaril@163
  1853
	cout << "    ---------------------------"<<endl<<endl;
insilmaril@404
  1854
*/
insilmaril@366
  1855
insilmaril@366
  1856
	undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
insilmaril@366
  1857
	undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
insilmaril@366
  1858
	undoSet.setEntry ("/history/curStep",QString::number(curStep));
insilmaril@366
  1859
	undoSet.writeSettings(histPath);
insilmaril@366
  1860
insilmaril@404
  1861
	mainWindow->updateHistory (undoSet);
insilmaril@366
  1862
	updateActions();
insilmaril@442
  1863
	xelection.update();
insilmaril@420
  1864
	ensureSelectionVisible();
insilmaril@106
  1865
}
insilmaril@106
  1866
insilmaril@390
  1867
bool MapEditor::isUndoAvailable()
insilmaril@390
  1868
{
insilmaril@390
  1869
	if (undoSet.readNumEntry("/history/undosAvail",0)>0)
insilmaril@390
  1870
		return true;
insilmaril@390
  1871
	else	
insilmaril@390
  1872
		return false;
insilmaril@390
  1873
}
insilmaril@390
  1874
insilmaril@404
  1875
void MapEditor::gotoHistoryStep (int i)
insilmaril@381
  1876
{
insilmaril@381
  1877
	// Restore variables
insilmaril@381
  1878
	int undosAvail=undoSet.readNumEntry (QString("/history/undosAvail"));
insilmaril@388
  1879
	int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
insilmaril@388
  1880
insilmaril@388
  1881
	if (i<0) i=undosAvail+redosAvail;
insilmaril@388
  1882
insilmaril@388
  1883
	// Clicking above current step makes us undo things
insilmaril@381
  1884
	if (i<undosAvail) 
insilmaril@381
  1885
	{	
insilmaril@381
  1886
		for (int j=0; j<undosAvail-i; j++) undo();
insilmaril@381
  1887
		return;
insilmaril@381
  1888
	}	
insilmaril@388
  1889
	// Clicking below current step makes us redo things
insilmaril@381
  1890
	if (i>undosAvail) 
insilmaril@491
  1891
		for (int j=undosAvail; j<i; j++) 
insilmaril@491
  1892
		{
insilmaril@491
  1893
		cout << "redo "<<j<<"/"<<undosAvail<<" i="<<i<<endl;
insilmaril@491
  1894
		redo();
insilmaril@491
  1895
		}
insilmaril@381
  1896
insilmaril@381
  1897
	// And ignore clicking the current row ;-)	
insilmaril@381
  1898
}
insilmaril@381
  1899
insilmaril@394
  1900
void MapEditor::addMapReplaceInt(const QString &undoSel, const QString &path)
insilmaril@106
  1901
{
insilmaril@393
  1902
	QString pathDir=path.left(path.findRev("/"));
insilmaril@393
  1903
	QDir d(pathDir);
insilmaril@393
  1904
	QFile file (path);
insilmaril@163
  1905
insilmaril@106
  1906
	if (d.exists() )
insilmaril@106
  1907
	{
insilmaril@106
  1908
		// We need to parse saved XML data
insilmaril@106
  1909
		mapBuilderHandler handler;
insilmaril@163
  1910
		QXmlInputSource source( file);
insilmaril@106
  1911
		QXmlSimpleReader reader;
insilmaril@106
  1912
		reader.setContentHandler( &handler );
insilmaril@106
  1913
		reader.setErrorHandler( &handler );
insilmaril@106
  1914
		handler.setMapEditor( this );
insilmaril@393
  1915
		handler.setTmpDir ( pathDir );	// needed to load files with rel. path
insilmaril@106
  1916
		if (undoSel.isEmpty())
insilmaril@106
  1917
		{
insilmaril@139
  1918
			unselect();
insilmaril@106
  1919
			mapCenter->clear();
insilmaril@106
  1920
			handler.setLoadMode (NewMap);
insilmaril@106
  1921
		} else	
insilmaril@106
  1922
		{
insilmaril@106
  1923
			select (undoSel);
insilmaril@106
  1924
			handler.setLoadMode (ImportReplace);
insilmaril@106
  1925
		}	
insilmaril@139
  1926
		blockReposition=true;
insilmaril@106
  1927
		bool ok = reader.parse( source );
insilmaril@139
  1928
		blockReposition=false;
insilmaril@106
  1929
		if (! ok ) 
insilmaril@106
  1930
		{	
insilmaril@106
  1931
			// This should never ever happen
insilmaril@393
  1932
			QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path),
insilmaril@393
  1933
								    handler.errorProtocol());
insilmaril@106
  1934
		}
insilmaril@106
  1935
	} else	
insilmaril@393
  1936
		QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
insilmaril@393
  1937
}
insilmaril@393
  1938
insilmaril@394
  1939
void MapEditor::addMapInsertInt (const QString &path, int pos)
insilmaril@393
  1940
{
insilmaril@424
  1941
	BranchObj *sel=xelection.getBranch();
insilmaril@424
  1942
	if (sel);
insilmaril@106
  1943
	{
insilmaril@393
  1944
		QString pathDir=path.left(path.findRev("/"));
insilmaril@393
  1945
		QDir d(pathDir);
insilmaril@393
  1946
		QFile file (path);
insilmaril@393
  1947
insilmaril@393
  1948
		if (d.exists() )
insilmaril@393
  1949
		{
insilmaril@393
  1950
			// We need to parse saved XML data
insilmaril@393
  1951
			mapBuilderHandler handler;
insilmaril@393
  1952
			QXmlInputSource source( file);
insilmaril@393
  1953
			QXmlSimpleReader reader;
insilmaril@393
  1954
			reader.setContentHandler( &handler );
insilmaril@393
  1955
			reader.setErrorHandler( &handler );
insilmaril@393
  1956
			handler.setMapEditor( this );
insilmaril@393
  1957
			handler.setTmpDir ( pathDir );	// needed to load files with rel. path
insilmaril@394
  1958
			handler.setLoadMode (ImportAdd);
insilmaril@393
  1959
			blockReposition=true;
insilmaril@393
  1960
			bool ok = reader.parse( source );
insilmaril@393
  1961
			blockReposition=false;
insilmaril@393
  1962
			if (! ok ) 
insilmaril@393
  1963
			{	
insilmaril@393
  1964
				// This should never ever happen
insilmaril@393
  1965
				QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path),
insilmaril@393
  1966
										handler.errorProtocol());
insilmaril@393
  1967
			}
insilmaril@424
  1968
			if (sel!=mapCenter)
insilmaril@424
  1969
				sel->getLastBranch()->linkTo (sel,pos);
insilmaril@393
  1970
		} else	
insilmaril@393
  1971
			QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
insilmaril@393
  1972
	}		
insilmaril@106
  1973
}
insilmaril@106
  1974
insilmaril@502
  1975
void MapEditor::pasteNoSave(const int &n)
insilmaril@0
  1976
{
insilmaril@491
  1977
	bool old=blockSaveState;
insilmaril@491
  1978
	blockSaveState=true;
insilmaril@502
  1979
	if (redosAvail > 0 || n!=0)
insilmaril@502
  1980
	{
insilmaril@502
  1981
		// Use the "historical" buffer
insilmaril@502
  1982
		QString bakMapName=QDir::convertSeparators (QString("history-%1").arg(n));
insilmaril@502
  1983
		QString bakMapDir=QDir::convertSeparators (tmpMapDir +"/"+bakMapName);
insilmaril@502
  1984
		load (bakMapDir+"/"+clipboardFile,ImportAdd);
insilmaril@502
  1985
	} else
insilmaril@502
  1986
		// Use the global buffer
insilmaril@502
  1987
		load (clipboardDir+"/"+clipboardFile,ImportAdd);
insilmaril@491
  1988
	blockSaveState=old;
insilmaril@0
  1989
}
insilmaril@0
  1990
insilmaril@497
  1991
void MapEditor::paste()		
insilmaril@0
  1992
{   
insilmaril@424
  1993
	BranchObj *sel=xelection.getBranch();
insilmaril@424
  1994
	if (sel)
insilmaril@166
  1995
	{
insilmaril@395
  1996
		saveStateChangingPart(
insilmaril@424
  1997
			sel,
insilmaril@424
  1998
			sel,
insilmaril@502
  1999
			QString ("paste (%1)").arg(curClipboard),
insilmaril@424
  2000
			QString("Paste to %1").arg( getName(sel))
insilmaril@395
  2001
		);
insilmaril@502
  2002
		pasteNoSave(0);
insilmaril@166
  2003
		mapCenter->reposition();
insilmaril@166
  2004
	}
insilmaril@0
  2005
}
insilmaril@0
  2006
insilmaril@0
  2007
void MapEditor::cut()
insilmaril@0
  2008
{
insilmaril@424
  2009
	LinkableMapObj *sel=xelection.single();
insilmaril@491
  2010
	if ( sel && (xelection.type() == Selection::Branch ||
insilmaril@491
  2011
		xelection.type()==Selection::MapCenter ||
insilmaril@491
  2012
		xelection.type()==Selection::FloatImage))
insilmaril@424
  2013
	{
insilmaril@491
  2014
	/* No savestate! savestate is called in cutNoSave
insilmaril@424
  2015
		saveStateChangingPart(
insilmaril@424
  2016
			sel->getParObj(),
insilmaril@424
  2017
			sel,
insilmaril@424
  2018
			"cut ()",
insilmaril@424
  2019
			QString("Cut %1").arg(getName(sel ))
insilmaril@424
  2020
		);
insilmaril@491
  2021
	*/	
insilmaril@424
  2022
		copy();
insilmaril@491
  2023
		deleteSelection();
insilmaril@424
  2024
		mapCenter->reposition();
insilmaril@424
  2025
	}
insilmaril@0
  2026
}
insilmaril@0
  2027
insilmaril@130
  2028
void MapEditor::move(const int &x, const int &y)
insilmaril@130
  2029
{
insilmaril@424
  2030
	LinkableMapObj *sel=xelection.single();
insilmaril@424
  2031
	if (sel)
insilmaril@366
  2032
	{
insilmaril@424
  2033
		QString ps=qpointfToString (sel->getAbsPos());
insilmaril@424
  2034
		QString s=xelection.single()->getSelectString();
insilmaril@366
  2035
		saveState(
insilmaril@366
  2036
			s, "move "+ps, 
insilmaril@408
  2037
			s, "move "+qpointfToString (QPointF (x,y)), 
insilmaril@424
  2038
			QString("Move %1 to  %2").arg(getName(sel)).arg(ps));
insilmaril@473
  2039
		sel->move(x,y);
insilmaril@366
  2040
		mapCenter->reposition();
insilmaril@442
  2041
		xelection.update();
insilmaril@366
  2042
	}
insilmaril@366
  2043
insilmaril@366
  2044
}
insilmaril@366
  2045
insilmaril@366
  2046
void MapEditor::moveRel (const int &x, const int &y)
insilmaril@366
  2047
{
insilmaril@424
  2048
	LinkableMapObj *sel=xelection.single();
insilmaril@424
  2049
	if (sel)
insilmaril@366
  2050
	{
insilmaril@424
  2051
		QString ps=qpointfToString (sel->getRelPos());
insilmaril@424
  2052
		QString s=sel->getSelectString();
insilmaril@366
  2053
		saveState(
insilmaril@366
  2054
			s, "moveRel "+ps, 
insilmaril@408
  2055
			s, "moveRel "+qpointfToString (QPointF (x,y)), 
insilmaril@424
  2056
			QString("Move %1 to relativ position %2").arg(getName(sel)).arg(ps));
insilmaril@424
  2057
		((OrnamentedObj*)sel)->move2RelPos (x,y);
insilmaril@366
  2058
		mapCenter->reposition();
insilmaril@424
  2059
		sel->updateLink();
insilmaril@442
  2060
		xelection.update();
insilmaril@366
  2061
	}
insilmaril@130
  2062
}
insilmaril@130
  2063
insilmaril@0
  2064
void MapEditor::moveBranchUp()
insilmaril@0
  2065
{
insilmaril@424
  2066
	BranchObj* bo=xelection.getBranch();
insilmaril@0
  2067
	BranchObj* par;
insilmaril@424
  2068
	if (bo)
insilmaril@0
  2069
	{
insilmaril@314
  2070
		if (!bo->canMoveBranchUp()) return;
insilmaril@0
  2071
		par=(BranchObj*)(bo->getParObj());
insilmaril@473
  2072
		BranchObj *obo=par->moveBranchUp (bo);	// bo will be the one below selection
insilmaril@473
  2073
		saveState (bo->getSelectString(),"moveBranchDown ()",obo->getSelectString(),"moveBranchUp ()",QString("Move up %1").arg(getName(bo)));
insilmaril@0
  2074
		mapCenter->reposition();
insilmaril@420
  2075
		scene()->update();
insilmaril@442
  2076
		xelection.update();
insilmaril@0
  2077
		ensureSelectionVisible();
insilmaril@0
  2078
	}
insilmaril@0
  2079
}
insilmaril@0
  2080
insilmaril@0
  2081
void MapEditor::moveBranchDown()
insilmaril@0
  2082
{
insilmaril@424
  2083
	BranchObj* bo=xelection.getBranch();
insilmaril@0
  2084
	BranchObj* par;
insilmaril@424
  2085
	if (bo)
insilmaril@0
  2086
	{
insilmaril@314
  2087
		if (!bo->canMoveBranchDown()) return;
insilmaril@0
  2088
		par=(BranchObj*)(bo->getParObj());
insilmaril@473
  2089
		BranchObj *obo=par->moveBranchDown(bo);	// bo will be the one above selection
insilmaril@473
  2090
		saveState(bo->getSelectString(),"moveBranchUp ()",obo->getSelectString(),"moveBranchDown ()",QString("Move down %1").arg(getName(bo)));
insilmaril@0
  2091
		mapCenter->reposition();
insilmaril@420
  2092
		scene()->update();
insilmaril@442
  2093
		xelection.update();
insilmaril@0
  2094
		ensureSelectionVisible();
insilmaril@0
  2095
	}	
insilmaril@0
  2096
}
insilmaril@0
  2097
insilmaril@491
  2098
void MapEditor::linkTo(const QString &dstString)	
insilmaril@420
  2099
{
insilmaril@424
  2100
	FloatImageObj *fio=xelection.getFloatImage();
insilmaril@424
  2101
	if (fio)
insilmaril@424
  2102
	{
insilmaril@424
  2103
		BranchObj *dst=(BranchObj*)(mapCenter->findObjBySelect(dstString));
insilmaril@424
  2104
		if (dst && (typeid(*dst)==typeid (BranchObj) || 
insilmaril@424
  2105
					typeid(*dst)==typeid (MapCenterObj)))
insilmaril@424
  2106
		{			
insilmaril@424
  2107
			LinkableMapObj *dstPar=dst->getParObj();
insilmaril@424
  2108
			QString parString=dstPar->getSelectString();
insilmaril@420
  2109
			QString fioPreSelectString=fio->getSelectString();
insilmaril@420
  2110
			QString fioPreParentSelectString=fio->getParObj()->getSelectString();
insilmaril@420
  2111
			((BranchObj*)(dst))->addFloatImage (fio);
insilmaril@424
  2112
			xelection.unselect();
insilmaril@420
  2113
			((BranchObj*)(fio->getParObj()))->removeFloatImage (fio);
insilmaril@420
  2114
			fio=((BranchObj*)(dst))->getLastFloatImage();
insilmaril@420
  2115
			fio->setRelPos();
insilmaril@420
  2116
			fio->reposition();
insilmaril@424
  2117
			xelection.select(fio);
insilmaril@420
  2118
			saveState(
insilmaril@420
  2119
				fio->getSelectString(),
insilmaril@420
  2120
				QString("linkTo (\"%1\")").arg(fioPreParentSelectString), 
insilmaril@420
  2121
				fioPreSelectString, 
insilmaril@420
  2122
				QString ("linkTo (\"%1\")").arg(dstString),
insilmaril@420
  2123
				QString ("Link floatimage to %1").arg(getName(dst)));
insilmaril@424
  2124
		}
insilmaril@420
  2125
	}
insilmaril@420
  2126
}
insilmaril@420
  2127
insilmaril@366
  2128
QString MapEditor::getHeading(bool &ok, QPoint &p)
insilmaril@0
  2129
{
insilmaril@424
  2130
	BranchObj *bo=xelection.getBranch();
insilmaril@424
  2131
	if (bo)
insilmaril@366
  2132
	{
insilmaril@366
  2133
		ok=true;
insilmaril@424
  2134
		p=mapFromScene(bo->getAbsPos());
insilmaril@424
  2135
		return bo->getHeading();
insilmaril@366
  2136
	}
insilmaril@366
  2137
	ok=false;
insilmaril@366
  2138
	return QString();
insilmaril@366
  2139
}
insilmaril@366
  2140
insilmaril@366
  2141
void MapEditor::setHeading(const QString &s)
insilmaril@366
  2142
{
insilmaril@424
  2143
	BranchObj *sel=xelection.getBranch();
insilmaril@424
  2144
	if (sel)
insilmaril@0
  2145
	{
insilmaril@395
  2146
		saveState(
insilmaril@424
  2147
			sel,
insilmaril@424
  2148
			"setHeading (\""+sel->getHeading()+"\")", 
insilmaril@424
  2149
			sel,
insilmaril@366
  2150
			"setHeading (\""+s+"\")", 
insilmaril@424
  2151
			QString("Set heading of %1 to \"%2\"").arg(getName(sel)).arg(s) );
insilmaril@424
  2152
		sel->setHeading(s );
insilmaril@106
  2153
		mapCenter->reposition();
insilmaril@442
  2154
		xelection.update();
insilmaril@106
  2155
		ensureSelectionVisible();
insilmaril@106
  2156
	}
insilmaril@106
  2157
}
insilmaril@106
  2158
insilmaril@366
  2159
void MapEditor::setHeadingInt(const QString &s)
insilmaril@366
  2160
{
insilmaril@424
  2161
	BranchObj *bo=xelection.getBranch();
insilmaril@424
  2162
	if (bo)
insilmaril@366
  2163
	{
insilmaril@424
  2164
		bo->setHeading(s);
insilmaril@366
  2165
		mapCenter->reposition();
insilmaril@442
  2166
		xelection.update();
insilmaril@366
  2167
		ensureSelectionVisible();
insilmaril@366
  2168
	}
insilmaril@366
  2169
}
insilmaril@366
  2170
insilmaril@366
  2171
void MapEditor::setVymLinkInt (const QString &s)
insilmaril@143
  2172
{
insilmaril@143
  2173
	// Internal function, no saveState needed
insilmaril@424
  2174
	BranchObj *bo=xelection.getBranch();
insilmaril@424
  2175
	if (bo)
insilmaril@143
  2176
	{
insilmaril@424
  2177
		bo->setVymLink(s);
insilmaril@143
  2178
		mapCenter->reposition();
insilmaril@491
  2179
		updateActions();
insilmaril@442
  2180
		xelection.update();
insilmaril@143
  2181
		ensureSelectionVisible();
insilmaril@143
  2182
	}
insilmaril@143
  2183
}
insilmaril@143
  2184
insilmaril@366
  2185
BranchObj* MapEditor::addNewBranchInt(int num)
insilmaril@0
  2186
{
insilmaril@366
  2187
	// Depending on pos:
insilmaril@366
  2188
	// -3		insert in childs of parent  above selection 
insilmaril@366
  2189
	// -2		add branch to selection 
insilmaril@366
  2190
	// -1		insert in childs of parent below selection 
insilmaril@366
  2191
	// 0..n		insert in childs of parent at pos
insilmaril@366
  2192
	BranchObj *newbo=NULL;
insilmaril@424
  2193
	BranchObj *bo=xelection.getBranch();
insilmaril@424
  2194
	if (bo)
insilmaril@0
  2195
	{
insilmaril@366
  2196
		if (num==-2)
insilmaril@0
  2197
		{
insilmaril@0
  2198
			// save scroll state. If scrolled, automatically select
insilmaril@0
  2199
			// new branch in order to tmp unscroll parent...
insilmaril@366
  2200
			return bo->addBranch();
insilmaril@366
  2201
			
insilmaril@366
  2202
		}else if (num==-1)
insilmaril@0
  2203
		{
insilmaril@366
  2204
			num=bo->getNum()+1;
insilmaril@366
  2205
			bo=(BranchObj*)bo->getParObj();
insilmaril@366
  2206
		}else if (num==-3)
insilmaril@366
  2207
		{
insilmaril@366
  2208
			num=bo->getNum();
insilmaril@366
  2209
			bo=(BranchObj*)bo->getParObj();
insilmaril@366
  2210
		}
insilmaril@366
  2211
		if (!bo) return bo;
insilmaril@366
  2212
		newbo=bo->insertBranch(num);
insilmaril@366
  2213
	}	
insilmaril@366
  2214
	return newbo;
insilmaril@366
  2215
}	
insilmaril@366
  2216
insilmaril@394
  2217
BranchObj* MapEditor::addNewBranch(int pos)
insilmaril@366
  2218
{
insilmaril@366
  2219
	// Different meaning than num in addNewBranchInt!
insilmaril@366
  2220
	// -1	add above
insilmaril@366
  2221
	//  0	add as child
insilmaril@366
  2222
	// +1	add below
insilmaril@424
  2223
	BranchObj *bo = xelection.getBranch();
insilmaril@394
  2224
	BranchObj *newbo=NULL;
insilmaril@394
  2225
insilmaril@424
  2226
	if (bo)
insilmaril@366
  2227
	{
insilmaril@421
  2228
		setCursor (Qt::ArrowCursor);
insilmaril@421
  2229
insilmaril@366
  2230
		newbo=addNewBranchInt (pos-2);
insilmaril@366
  2231
insilmaril@366
  2232
		if (newbo)
insilmaril@366
  2233
		{
insilmaril@395
  2234
			saveState(
insilmaril@417
  2235
				newbo,		
insilmaril@393
  2236
				"delete ()",
insilmaril@424
  2237
				bo,
insilmaril@366
  2238
				QString ("addBranch (%1)").arg(pos-2),
insilmaril@395
  2239
				QString ("Add new branch to %1").arg(getName(bo)));	
insilmaril@366
  2240
insilmaril@366
  2241
			mapCenter->reposition();
insilmaril@442
  2242
			xelection.update();
insilmaril@366
  2243
		}
insilmaril@366
  2244
	}	
insilmaril@394
  2245
	return newbo;
insilmaril@366
  2246
}
insilmaril@366
  2247
insilmaril@366
  2248
insilmaril@394
  2249
BranchObj* MapEditor::addNewBranchBefore()
insilmaril@366
  2250
{
insilmaril@394
  2251
	BranchObj *newbo=NULL;
insilmaril@424
  2252
	BranchObj *bo = xelection.getBranch();
insilmaril@491
  2253
	if (bo && xelection.type()==Selection::Branch)
insilmaril@394
  2254
		 // We accept no MapCenterObj here, so we _have_ a parent
insilmaril@366
  2255
	{
insilmaril@408
  2256
		QPointF p=bo->getRelPos();
insilmaril@394
  2257
insilmaril@394
  2258
insilmaril@424
  2259
		BranchObj *parbo=(BranchObj*)(bo->getParObj());
insilmaril@394
  2260
insilmaril@394
  2261
		// add below selection
insilmaril@394
  2262
		newbo=parbo->insertBranch(bo->getNum()+1);
insilmaril@394
  2263
		if (newbo)
insilmaril@366
  2264
		{
insilmaril@394
  2265
			newbo->move2RelPos (p);
insilmaril@394
  2266
insilmaril@394
  2267
			// Move selection to new branch
insilmaril@424
  2268
			bo->linkTo (newbo,-1);
insilmaril@394
  2269
insilmaril@394
  2270
			saveState (newbo, "deleteKeepChilds ()", newbo, "addBranchBefore ()", 
insilmaril@394
  2271
				QString ("Add branch before %1").arg(getName(bo)));
insilmaril@394
  2272
insilmaril@394
  2273
			mapCenter->reposition();
insilmaril@442
  2274
			xelection.update();
insilmaril@394
  2275
		}
insilmaril@0
  2276
	}	
insilmaril@394
  2277
	return newbo;
insilmaril@0
  2278
}
insilmaril@0
  2279
insilmaril@0
  2280
void MapEditor::deleteSelection()
insilmaril@0
  2281
{
insilmaril@424
  2282
	BranchObj *bo = xelection.getBranch();
insilmaril@491
  2283
	if (bo && xelection.type()==Selection::Branch)
insilmaril@0
  2284
	{
insilmaril@294
  2285
		BranchObj* par=(BranchObj*)(bo->getParObj());
insilmaril@424
  2286
		xelection.unselect();
insilmaril@394
  2287
		saveStateRemovingPart (bo, QString ("Delete %1").arg(getName(bo)));
insilmaril@0
  2288
		par->removeBranch(bo);
insilmaril@424
  2289
		xelection.select (par);
insilmaril@0
  2290
		ensureSelectionVisible();
insilmaril@0
  2291
		mapCenter->reposition();
insilmaril@442
  2292
		xelection.update();
insilmaril@442
  2293
		xelection.update();
insilmaril@424
  2294
		return;
insilmaril@0
  2295
	}
insilmaril@424
  2296
	FloatImageObj *fio=xelection.getFloatImage();
insilmaril@424
  2297
	if (fio)
insilmaril@0
  2298
	{
insilmaril@0
  2299
		BranchObj* par=(BranchObj*)(fio->getParObj());
insilmaril@395
  2300
		saveStateChangingPart(
insilmaril@395
  2301
			par, 
insilmaril@395
  2302
			fio,
insilmaril@395
  2303
			"delete ()",
insilmaril@395
  2304
			QString("Delete %1").arg(getName(fio))
insilmaril@395
  2305
		);
insilmaril@424
  2306
		xelection.unselect();
insilmaril@0
  2307
		par->removeFloatImage(fio);
insilmaril@424
  2308
		xelection.select (par);
insilmaril@442
  2309
		mapCenter->reposition();
insilmaril@442
  2310
		xelection.update();
insilmaril@0
  2311
		ensureSelectionVisible();
insilmaril@424
  2312
		return;
insilmaril@0
  2313
	}
insilmaril@0
  2314
}
insilmaril@0
  2315
insilmaril@0
  2316
LinkableMapObj* MapEditor::getSelection()
insilmaril@0
  2317
{
insilmaril@424
  2318
	return xelection.single();
insilmaril@0
  2319
}
insilmaril@0
  2320
insilmaril@442
  2321
BranchObj* MapEditor::getSelectedBranch()
insilmaril@442
  2322
{
insilmaril@442
  2323
	return xelection.getBranch();
insilmaril@442
  2324
}
insilmaril@442
  2325
insilmaril@442
  2326
FloatImageObj* MapEditor::getSelectedFloatImage()
insilmaril@442
  2327
{
insilmaril@442
  2328
	return xelection.getFloatImage();
insilmaril@442
  2329
}
insilmaril@442
  2330
insilmaril@267
  2331
void MapEditor::unselect()
insilmaril@267
  2332
{
insilmaril@424
  2333
	xelection.unselect();
insilmaril@267
  2334
}	
insilmaril@267
  2335
insilmaril@267
  2336
void MapEditor::reselect()
insilmaril@267
  2337
{
insilmaril@424
  2338
	xelection.reselect();
insilmaril@267
  2339
}	
insilmaril@267
  2340
insilmaril@83
  2341
bool MapEditor::select (const QString &s)
insilmaril@83
  2342
{
insilmaril@106
  2343
	LinkableMapObj *lmo=mapCenter->findObjBySelect(s);
insilmaril@0
  2344
insilmaril@0
  2345
	// Finally select the found object
insilmaril@0
  2346
	if (lmo)
insilmaril@0
  2347
	{
insilmaril@424
  2348
		xelection.unselect();
insilmaril@424
  2349
		xelection.select(lmo);
insilmaril@442
  2350
		xelection.update();
insilmaril@0
  2351
		ensureSelectionVisible();
insilmaril@0
  2352
		return true;
insilmaril@139
  2353
	} 
insilmaril@139
  2354
	return false;
insilmaril@0
  2355
}
insilmaril@0
  2356
insilmaril@366
  2357
QString MapEditor::getSelectString()
insilmaril@366
  2358
{
insilmaril@424
  2359
	return xelection.getSelectString();
insilmaril@366
  2360
}
insilmaril@366
  2361
insilmaril@366
  2362
void MapEditor::selectInt (LinkableMapObj *lmo)
insilmaril@0
  2363
{
insilmaril@491
  2364
	if (lmo && xelection.single()!= lmo && isSelectBlocked()==false )
insilmaril@442
  2365
	{
insilmaril@424
  2366
		xelection.select(lmo);
insilmaril@442
  2367
		xelection.update();
insilmaril@442
  2368
	}	
insilmaril@267
  2369
}
insilmaril@0
  2370
insilmaril@366
  2371
void MapEditor::selectNextBranchInt()
insilmaril@0
  2372
{
insilmaril@0
  2373
	// Increase number of branch
insilmaril@424
  2374
	LinkableMapObj *sel=xelection.single();
insilmaril@424
  2375
	if (sel)
insilmaril@0
  2376
	{
insilmaril@424
  2377
		QString s=sel->getSelectString();
insilmaril@0
  2378
		QString part;
insilmaril@0
  2379
		QString typ;
insilmaril@0
  2380
		QString num;
insilmaril@0
  2381
insilmaril@0
  2382
		// Where am I? 
insilmaril@0
  2383
		part=s.section(",",-1);
insilmaril@0
  2384
		typ=part.left (3);
insilmaril@0
  2385
		num=part.right(part.length() - 3);
insilmaril@0
  2386
insilmaril@0
  2387
		s=s.left (s.length() -num.length());
insilmaril@0
  2388
insilmaril@0
  2389
		// Go to next lmo
insilmaril@0
  2390
		num=QString ("%1").arg(num.toUInt()+1);
insilmaril@0
  2391
insilmaril@0
  2392
		s=s+num;
insilmaril@0
  2393
		
insilmaril@0
  2394
		// Try to select this one
insilmaril@0
  2395
		if (select (s)) return;
insilmaril@0
  2396
insilmaril@0
  2397
		// We have no direct successor, 
insilmaril@0
  2398
		// try to increase the parental number in order to
insilmaril@0
  2399
		// find a successor with same depth
insilmaril@0
  2400
insilmaril@424
  2401
		int d=xelection.single()->getDepth();
insilmaril@0
  2402
		int oldDepth=d;
insilmaril@0
  2403
		int i;
insilmaril@0
  2404
		bool found=false;
insilmaril@0
  2405
		bool b;
insilmaril@0
  2406
		while (!found && d>0)
insilmaril@0
  2407
		{
insilmaril@0
  2408
			s=s.section (",",0,d-1);
insilmaril@0
  2409
			// replace substring of current depth in s with "1"
insilmaril@0
  2410
			part=s.section(",",-1);
insilmaril@0
  2411
			typ=part.left (3);
insilmaril@0
  2412
			num=part.right(part.length() - 3);
insilmaril@0
  2413
insilmaril@0
  2414
			if (d>1)
insilmaril@0
  2415
			{	
insilmaril@0
  2416
				// increase number of parent
insilmaril@0
  2417
				num=QString ("%1").arg(num.toUInt()+1);
insilmaril@0
  2418
				s=s.section (",",0,d-2) + ","+ typ+num;
insilmaril@0
  2419
			} else
insilmaril@0
  2420
			{
insilmaril@0
  2421
				// Special case, look at orientation
insilmaril@473
  2422
				if (xelection.single()->getOrientation()==LinkableMapObj::RightOfCenter)
insilmaril@0
  2423
					num=QString ("%1").arg(num.toUInt()+1);
insilmaril@0
  2424
				else	
insilmaril@0
  2425
					num=QString ("%1").arg(num.toUInt()-1);
insilmaril@0
  2426
				s=typ+num;
insilmaril@0
  2427
			}	
insilmaril@0
  2428
insilmaril@0
  2429
			if (select (s))
insilmaril@0
  2430
				// pad to oldDepth, select the first branch for each depth
insilmaril@0
  2431
				for (i=d;i<oldDepth;i++)
insilmaril@0
  2432
				{
insilmaril@0
  2433
					b=select (s);
insilmaril@0
  2434
					if (b)
insilmaril@0
  2435
					{	
insilmaril@424
  2436
						if ( xelection.getBranch()->countBranches()>0)
insilmaril@0
  2437
							s+=",bo:0";
insilmaril@0
  2438
						else	
insilmaril@0
  2439
							break;
insilmaril@0
  2440
					} else
insilmaril@0
  2441
						break;
insilmaril@0
  2442
				}	
insilmaril@0
  2443
insilmaril@0
  2444
			// try to select the freshly built string
insilmaril@0
  2445
			found=select(s);
insilmaril@0
  2446
			d--;
insilmaril@0
  2447
		}
insilmaril@0
  2448
		return;
insilmaril@0
  2449
	}	
insilmaril@0
  2450
}
insilmaril@0
  2451
insilmaril@366
  2452
void MapEditor::selectPrevBranchInt()
insilmaril@0
  2453
{
insilmaril@0
  2454
	// Decrease number of branch
insilmaril@424
  2455
	BranchObj *bo=xelection.getBranch();
insilmaril@424
  2456
	if (bo)
insilmaril@0
  2457
	{
insilmaril@424
  2458
		QString s=bo->getSelectString();
insilmaril@0
  2459
		QString part;
insilmaril@0
  2460
		QString typ;
insilmaril@0
  2461
		QString num;
insilmaril@0
  2462
insilmaril@0
  2463
		// Where am I? 
insilmaril@0
  2464
		part=s.section(",",-1);
insilmaril@0
  2465
		typ=part.left (3);
insilmaril@0
  2466
		num=part.right(part.length() - 3);
insilmaril@0
  2467
insilmaril@0
  2468
		s=s.left (s.length() -num.length());
insilmaril@0
  2469
insilmaril@411
  2470
		int n=num.toInt()-1;
insilmaril@411
  2471
		
insilmaril@0
  2472
		// Go to next lmo
insilmaril@411
  2473
		num=QString ("%1").arg(n);
insilmaril@0
  2474
		s=s+num;
insilmaril@0
  2475
		
insilmaril@0
  2476
		// Try to select this one
insilmaril@411
  2477
		if (n>=0 && select (s)) return;
insilmaril@0
  2478
insilmaril@0
  2479
		// We have no direct precessor, 
insilmaril@0
  2480
		// try to decrease the parental number in order to
insilmaril@0
  2481
		// find a precessor with same depth
insilmaril@0
  2482
insilmaril@424
  2483
		int d=xelection.single()->getDepth();
insilmaril@0
  2484
		int oldDepth=d;
insilmaril@0
  2485
		int i;
insilmaril@0
  2486
		bool found=false;
insilmaril@0
  2487
		bool b;
insilmaril@0
  2488
		while (!found && d>0)
insilmaril@0
  2489
		{
insilmaril@0
  2490
			s=s.section (",",0,d-1);
insilmaril@0
  2491
			// replace substring of current depth in s with "1"
insilmaril@0
  2492
			part=s.section(",",-1);
insilmaril@0
  2493
			typ=part.left (3);
insilmaril@0
  2494
			num=part.right(part.length() - 3);
insilmaril@0
  2495
insilmaril@0
  2496
			if (d>1)
insilmaril@0
  2497
			{
insilmaril@0
  2498
				// decrease number of parent
insilmaril@411
  2499
				num=QString ("%1").arg(num.toInt()-1);
insilmaril@0
  2500
				s=s.section (",",0,d-2) + ","+ typ+num;
insilmaril@0
  2501
			} else
insilmaril@0
  2502
			{
insilmaril@0
  2503
				// Special case, look at orientation
insilmaril@473
  2504
				if (xelection.single()->getOrientation()==LinkableMapObj::RightOfCenter)
insilmaril@411
  2505
					num=QString ("%1").arg(num.toInt()-1);
insilmaril@0
  2506
				else	
insilmaril@411
  2507
					num=QString ("%1").arg(num.toInt()+1);
insilmaril@0
  2508
				s=typ+num;
insilmaril@0
  2509
			}	
insilmaril@0
  2510
insilmaril@0
  2511
			if (select(s))
insilmaril@0
  2512
				// pad to oldDepth, select the last branch for each depth
insilmaril@0
  2513
				for (i=d;i<oldDepth;i++)
insilmaril@0
  2514
				{
insilmaril@0
  2515
					b=select (s);
insilmaril@0
  2516
					if (b)
insilmaril@424
  2517
						if ( xelection.getBranch()->countBranches()>0)
insilmaril@424
  2518
							s+=",bo:"+ QString ("%1").arg( xelection.getBranch()->countBranches()-1 );
insilmaril@0
  2519
						else	
insilmaril@0
  2520
							break;
insilmaril@0
  2521
					else
insilmaril@0
  2522
						break;
insilmaril@0
  2523
				}	
insilmaril@0
  2524
			
insilmaril@0
  2525
			// try to select the freshly built string
insilmaril@0
  2526
			found=select(s);
insilmaril@0
  2527
			d--;
insilmaril@0
  2528
		}
insilmaril@0
  2529
		return;
insilmaril@0
  2530
	}	
insilmaril@0
  2531
}
insilmaril@0
  2532
insilmaril@0
  2533
void MapEditor::selectUpperBranch()
insilmaril@0
  2534
{
insilmaril@491
  2535
	if (isSelectBlocked() ) return;
insilmaril@491
  2536
insilmaril@424
  2537
	BranchObj *bo=xelection.getBranch();
insilmaril@491
  2538
	if (bo && xelection.type()==Selection::Branch)
insilmaril@0
  2539
	{
insilmaril@473
  2540
		if (bo->getOrientation()==LinkableMapObj::RightOfCenter)
insilmaril@424
  2541
			selectPrevBranchInt();
insilmaril@424
  2542
		else
insilmaril@424
  2543
			if (bo->getDepth()==1)
insilmaril@424
  2544
				selectNextBranchInt();
insilmaril@424
  2545
			else
insilmaril@424
  2546
				selectPrevBranchInt();
insilmaril@424
  2547
	}
insilmaril@424
  2548
}
insilmaril@424
  2549
insilmaril@424
  2550
void MapEditor::selectLowerBranch()
insilmaril@424
  2551
{
insilmaril@491
  2552
	if (isSelectBlocked() ) return;
insilmaril@491
  2553
insilmaril@424
  2554
	BranchObj *bo=xelection.getBranch();
insilmaril@491
  2555
	if (bo && xelection.type()==Selection::Branch)
insilmaril@473
  2556
		if (bo->getOrientation()==LinkableMapObj::RightOfCenter)
insilmaril@424
  2557
			selectNextBranchInt();
insilmaril@424
  2558
		else
insilmaril@424
  2559
			if (bo->getDepth()==1)
insilmaril@366
  2560
				selectPrevBranchInt();
insilmaril@0
  2561
			else
insilmaril@366
  2562
				selectNextBranchInt();
insilmaril@0
  2563
}
insilmaril@0
  2564
insilmaril@0
  2565
insilmaril@0
  2566
void MapEditor::selectLeftBranch()
insilmaril@0
  2567
{
insilmaril@491
  2568
	if (isSelectBlocked() ) return;
insilmaril@491
  2569
insilmaril@0
  2570
	BranchObj* bo;
insilmaril@0
  2571
	BranchObj* par;
insilmaril@424
  2572
	LinkableMapObj *sel=xelection.single();
insilmaril@424
  2573
	if (sel)
insilmaril@0
  2574
	{
insilmaril@491
  2575
		if (xelection.type()== Selection::MapCenter)
insilmaril@0
  2576
		{
insilmaril@424
  2577
			par=xelection.getBranch();
insilmaril@0
  2578
			bo=par->getLastSelectedBranch();
insilmaril@0
  2579
			if (bo)
insilmaril@0
  2580
			{
insilmaril@0
  2581
				// Workaround for reselecting on left and right side
insilmaril@473
  2582
				if (bo->getOrientation()==LinkableMapObj::RightOfCenter)
insilmaril@424
  2583
					bo=par->getLastBranch();
insilmaril@424
  2584
				if (bo)
insilmaril@0
  2585
				{
insilmaril@0
  2586
					bo=par->getLastBranch();
insilmaril@424
  2587
					xelection.select(bo);
insilmaril@442
  2588
					xelection.update();
insilmaril@0
  2589
					ensureSelectionVisible();
insilmaril@0
  2590
				}
insilmaril@0
  2591
			}	
insilmaril@0
  2592
		} else
insilmaril@0
  2593
		{
insilmaril@424
  2594
			par=(BranchObj*)(sel->getParObj());
insilmaril@473
  2595
			if (sel->getOrientation()==LinkableMapObj::RightOfCenter)
insilmaril@0
  2596
			{
insilmaril@491
  2597
				if (xelection.type() == Selection::Branch ||
insilmaril@491
  2598
					xelection.type() == Selection::FloatImage)
insilmaril@0
  2599
				{
insilmaril@424
  2600
					xelection.select(par);
insilmaril@442
  2601
					xelection.update();
insilmaril@0
  2602
					ensureSelectionVisible();
insilmaril@0
  2603
				}
insilmaril@0
  2604
			} else
insilmaril@0
  2605
			{
insilmaril@491
  2606
				if (xelection.type() == Selection::Branch )
insilmaril@0
  2607
				{
insilmaril@424
  2608
					bo=xelection.getBranch()->getLastSelectedBranch();
insilmaril@0
  2609
					if (bo) 
insilmaril@0
  2610
					{
insilmaril@424
  2611
						xelection.select(bo);
insilmaril@442
  2612
						xelection.update();
insilmaril@0
  2613
						ensureSelectionVisible();
insilmaril@0
  2614
					}
insilmaril@0
  2615
				}
insilmaril@0
  2616
			}
insilmaril@0
  2617
		}	
insilmaril@0
  2618
	}
insilmaril@0
  2619
}
insilmaril@0
  2620
insilmaril@0
  2621
void MapEditor::selectRightBranch()
insilmaril@0
  2622
{
insilmaril@491
  2623
	if (isSelectBlocked() ) return;
insilmaril@491
  2624
insilmaril@0
  2625
	BranchObj* bo;
insilmaril@0
  2626
	BranchObj* par;
insilmaril@424
  2627
	LinkableMapObj *sel=xelection.single();
insilmaril@424
  2628
	if (sel)
insilmaril@0
  2629
	{
insilmaril@491
  2630
		if (xelection.type()==Selection::MapCenter) 
insilmaril@0
  2631
		{
insilmaril@424
  2632
			par=xelection.getBranch();
insilmaril@0
  2633
			bo=par->getLastSelectedBranch();
insilmaril@0
  2634
			if (bo)
insilmaril@0
  2635
			{
insilmaril@366
  2636
				// Workaround for reselecting on left and right side
insilmaril@473
  2637
				if (bo->getOrientation()==LinkableMapObj::LeftOfCenter)
insilmaril@0
  2638
					bo=par->getFirstBranch();
insilmaril@0
  2639
				if (bo)
insilmaril@0
  2640
				{
insilmaril@424
  2641
					xelection.select(bo);
insilmaril@442
  2642
					xelection.update();
insilmaril@0
  2643
					ensureSelectionVisible();
insilmaril@0
  2644
				}
insilmaril@0
  2645
			}
insilmaril@0
  2646
		} else
insilmaril@0
  2647
		{
insilmaril@424
  2648
			par=(BranchObj*)(xelection.single()->getParObj());
insilmaril@473
  2649
			if (xelection.single()->getOrientation()==LinkableMapObj::LeftOfCenter)
insilmaril@0
  2650
			{
insilmaril@491
  2651
				if (xelection.type() == Selection::Branch ||
insilmaril@491
  2652
					xelection.type() == Selection::FloatImage)
insilmaril@0
  2653
				{
insilmaril@424
  2654
					xelection.select(par);
insilmaril@442
  2655
					xelection.update();
insilmaril@0
  2656
					ensureSelectionVisible();
insilmaril@0
  2657
				}
insilmaril@0
  2658
			} else
insilmaril@0
  2659
			{
insilmaril@491
  2660
				if (xelection.type()  == Selection::Branch) 
insilmaril@0
  2661
				{
insilmaril@424
  2662
					bo=xelection.getBranch()->getLastSelectedBranch();
insilmaril@0
  2663
					if (bo) 
insilmaril@0
  2664
					{
insilmaril@424
  2665
						xelection.select(bo);
insilmaril@442
  2666
						xelection.update();
insilmaril@0
  2667
						ensureSelectionVisible();
insilmaril@0
  2668
					}
insilmaril@0
  2669
				}
insilmaril@0
  2670
			}
insilmaril@0
  2671
		}
insilmaril@0
  2672
	}
insilmaril@0
  2673
}
insilmaril@0
  2674
insilmaril@0
  2675
void MapEditor::selectFirstBranch()
insilmaril@0
  2676
{
insilmaril@424
  2677
	BranchObj *bo1=xelection.getBranch();
insilmaril@0
  2678
	BranchObj *bo2;
insilmaril@0
  2679
	BranchObj* par;
insilmaril@424
  2680
	if (bo1)
insilmaril@424
  2681
	{
insilmaril@424
  2682
		par=(BranchObj*)(bo1->getParObj());
insilmaril@424
  2683
		bo2=par->getFirstBranch();
insilmaril@424
  2684
		if (bo2) {
insilmaril@424
  2685
			xelection.select(bo2);
insilmaril@442
  2686
			xelection.update();
insilmaril@424
  2687
			ensureSelectionVisible();
insilmaril@424
  2688
		}
insilmaril@424
  2689
	}		
insilmaril@0
  2690
}
insilmaril@0
  2691
insilmaril@0
  2692
void MapEditor::selectLastBranch()
insilmaril@0
  2693
{
insilmaril@424
  2694
	BranchObj *bo1=xelection.getBranch();
insilmaril@0
  2695
	BranchObj *bo2;
insilmaril@0
  2696
	BranchObj* par;
insilmaril@424
  2697
	if (bo1)
insilmaril@424
  2698
	{
insilmaril@424
  2699
		par=(BranchObj*)(bo1->getParObj());
insilmaril@424
  2700
		bo2=par->getLastBranch();
insilmaril@424
  2701
		if (bo2) 
insilmaril@0
  2702
		{
insilmaril@424
  2703
			xelection.select(bo2);
insilmaril@442
  2704
			xelection.update();
insilmaril@424
  2705
			ensureSelectionVisible();
insilmaril@424
  2706
		}
insilmaril@424
  2707
	}		
insilmaril@0
  2708
}
insilmaril@0
  2709
insilmaril@408
  2710
void MapEditor::selectMapBackgroundImage ()
insilmaril@408
  2711
{
insilmaril@408
  2712
	Q3FileDialog *fd=new Q3FileDialog( this);
insilmaril@408
  2713
	fd->setMode (Q3FileDialog::ExistingFile);
insilmaril@408
  2714
	fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
insilmaril@408
  2715
	ImagePreview *p =new ImagePreview (fd);
insilmaril@408
  2716
	fd->setContentsPreviewEnabled( TRUE );
insilmaril@408
  2717
	fd->setContentsPreview( p, p );
insilmaril@408
  2718
	fd->setPreviewMode( Q3FileDialog::Contents );
insilmaril@408
  2719
	fd->setCaption(vymName+" - " +tr("Load background image"));
insilmaril@408
  2720
	fd->setDir (lastImageDir);
insilmaril@408
  2721
	fd->show();
insilmaril@408
  2722
insilmaril@408
  2723
	if ( fd->exec() == QDialog::Accepted )
insilmaril@408
  2724
	{
insilmaril@412
  2725
		// TODO selectMapBackgroundImg in QT4 use:	lastImageDir=fd->directory();
insilmaril@408
  2726
		lastImageDir=QDir (fd->dirPath());
insilmaril@408
  2727
		setMapBackgroundImage (fd->selectedFile());
insilmaril@408
  2728
	}
insilmaril@408
  2729
}	
insilmaril@408
  2730
insilmaril@442
  2731
void MapEditor::setMapBackgroundImage (const QString &fn)	//FIXME missing savestate
insilmaril@408
  2732
{
insilmaril@408
  2733
	QColor oldcol=mapScene->backgroundBrush().color();
insilmaril@408
  2734
	/*
insilmaril@408
  2735
	saveState(
insilmaril@408
  2736
		selection,
insilmaril@408
  2737
		QString ("setMapBackgroundImage (%1)").arg(oldcol.name()),
insilmaril@408
  2738
		selection,
insilmaril@408
  2739
		QString ("setMapBackgroundImage (%1)").arg(col.name()),
insilmaril@408
  2740
		QString("Set background color of map to %1").arg(col.name()));
insilmaril@408
  2741
	*/	
insilmaril@408
  2742
	QBrush brush;
insilmaril@408
  2743
	brush.setTextureImage (QPixmap (fn));
insilmaril@408
  2744
	mapScene->setBackgroundBrush(brush);
insilmaril@408
  2745
}
insilmaril@408
  2746
insilmaril@398
  2747
void MapEditor::selectMapBackgroundColor()
insilmaril@0
  2748
{
insilmaril@408
  2749
	QColor col = QColorDialog::getColor( mapScene->backgroundBrush().color(), this );
insilmaril@0
  2750
	if ( !col.isValid() ) return;
insilmaril@408
  2751
	setMapBackgroundColor( col );
insilmaril@0
  2752
}
insilmaril@0
  2753
insilmaril@398
  2754
insilmaril@398
  2755
void MapEditor::setMapBackgroundColor(QColor col)
insilmaril@0
  2756
{
insilmaril@408
  2757
	QColor oldcol=mapScene->backgroundBrush().color();
insilmaril@398
  2758
	saveState(
insilmaril@424
  2759
		mapCenter,
insilmaril@445
  2760
		QString ("setMapBackgroundColor (\"%1\")").arg(oldcol.name()),
insilmaril@424
  2761
		mapCenter,
insilmaril@445
  2762
		QString ("setMapBackgroundColor (\"%1\")").arg(col.name()),
insilmaril@398
  2763
		QString("Set background color of map to %1").arg(col.name()));
insilmaril@408
  2764
	mapScene->setBackgroundBrush(col);
insilmaril@398
  2765
}
insilmaril@398
  2766
insilmaril@398
  2767
QColor MapEditor::getMapBackgroundColor()
insilmaril@398
  2768
{
insilmaril@408
  2769
    return mapScene->backgroundBrush().color();
insilmaril@0
  2770
}
insilmaril@0
  2771
insilmaril@395
  2772
QColor MapEditor::getCurrentHeadingColor()
insilmaril@0
  2773
{
insilmaril@424
  2774
	BranchObj *bo=xelection.getBranch();
insilmaril@424
  2775
	if (bo) return bo->getColor(); 
insilmaril@395
  2776
	
insilmaril@395
  2777
	QMessageBox::warning(0,tr("Warning"),tr("Can't get color of heading,\nthere's no branch selected"));
insilmaril@395
  2778
	return Qt::black;
insilmaril@0
  2779
}
insilmaril@0
  2780
insilmaril@417
  2781
void MapEditor::colorBranch (QColor c)
insilmaril@0
  2782
{
insilmaril@424
  2783
	BranchObj *bo=xelection.getBranch();
insilmaril@424
  2784
	if (bo)
insilmaril@0
  2785
	{
insilmaril@424
  2786
		saveState(
insilmaril@424
  2787
			bo, 
insilmaril@445
  2788
			QString ("colorBranch (\"%1\")").arg(bo->getColor().name()),
insilmaril@424
  2789
			bo,
insilmaril@445
  2790
			QString ("colorBranch (\"%1\")").arg(c.name()),
insilmaril@424
  2791
			QString("Set color of %1 to %2").arg(getName(bo)).arg(c.name())
insilmaril@424
  2792
		);	
insilmaril@424
  2793
		bo->setColor(c); // color branch
insilmaril@0
  2794
	}
insilmaril@0
  2795
}
insilmaril@0
  2796
insilmaril@417
  2797
void MapEditor::colorSubtree (QColor c)
insilmaril@0
  2798
{
insilmaril@424
  2799
	BranchObj *bo=xelection.getBranch();
insilmaril@424
  2800
	if (bo) 
insilmaril@0
  2801
	{
insilmaril@424
  2802
		saveStateChangingPart(
insilmaril@424
  2803
			bo, 
insilmaril@424
  2804
			bo,
insilmaril@445
  2805
			QString ("colorSubtree (\"%1\")").arg(c.name()),
insilmaril@424
  2806
			QString ("Set color of %1 and childs to %2").arg(getName(bo)).arg(c.name())
insilmaril@424
  2807
		);	
insilmaril@424
  2808
		bo->setColorSubtree (c); // color links, color childs
insilmaril@0
  2809
	}
insilmaril@0
  2810
}
insilmaril@0
  2811
insilmaril@0
  2812
insilmaril@0
  2813
void MapEditor::toggleStandardFlag(QString f)
insilmaril@0
  2814
{
insilmaril@424
  2815
	BranchObj *bo=xelection.getBranch();
insilmaril@424
  2816
	if (bo) 
insilmaril@0
  2817
	{
insilmaril@424
  2818
		QString u,r;
insilmaril@424
  2819
		if (bo->isSetStandardFlag(f))
insilmaril@366
  2820
		{
insilmaril@424
  2821
			r="unsetFlag";
insilmaril@424
  2822
			u="setFlag";
insilmaril@424
  2823
		}	
insilmaril@424
  2824
		else
insilmaril@424
  2825
		{
insilmaril@424
  2826
			u="unsetFlag";
insilmaril@424
  2827
			r="setFlag";
insilmaril@424
  2828
		}	
insilmaril@424
  2829
		saveState(
insilmaril@424
  2830
			bo,
insilmaril@424
  2831
			QString("%1 (\"%2\")").arg(u).arg(f), 
insilmaril@424
  2832
			bo,
insilmaril@424
  2833
			QString("%1 (\"%2\")").arg(r).arg(f),
insilmaril@424
  2834
			QString("Toggling standard flag \"%1\" of %2").arg(f).arg(getName(bo)));
insilmaril@424
  2835
		bo->toggleStandardFlag (f,mainWindow->useFlagGroups());
insilmaril@442
  2836
		xelection.update();
insilmaril@424
  2837
	}
insilmaril@0
  2838
}
insilmaril@0
  2839
insilmaril@0
  2840
insilmaril@0
  2841
BranchObj* MapEditor::findText (QString s, bool cs)
insilmaril@0
  2842
{
insilmaril@366
  2843
	QTextDocument::FindFlags flags=0;
insilmaril@366
  2844
	if (cs) flags=QTextDocument::FindCaseSensitively;
insilmaril@366
  2845
insilmaril@0
  2846
	if (!itFind) 
insilmaril@0
  2847
	{	// Nothing found or new find process
insilmaril@0
  2848
		if (EOFind)
insilmaril@0
  2849
			// nothing found, start again
insilmaril@0
  2850
			EOFind=false;
insilmaril@0
  2851
		itFind=mapCenter->first();
insilmaril@0
  2852
	}	
insilmaril@0
  2853
	bool searching=true;
insilmaril@0
  2854
	bool foundNote=false;
insilmaril@0
  2855
	while (searching && !EOFind)
insilmaril@0
  2856
	{
insilmaril@0
  2857
		if (itFind)
insilmaril@0
  2858
		{
insilmaril@0
  2859
			// Searching in Note
insilmaril@0
  2860
			if (itFind->getNote().contains(s,cs))
insilmaril@0
  2861
			{
insilmaril@424
  2862
				if (xelection.single()!=itFind) 
insilmaril@0
  2863
				{
insilmaril@424
  2864
					xelection.select(itFind);
insilmaril@0
  2865
					ensureSelectionVisible();
insilmaril@0
  2866
				}
insilmaril@366
  2867
				if (textEditor->findText(s,flags)) 
insilmaril@0
  2868
				{
insilmaril@0
  2869
					searching=false;
insilmaril@0
  2870
					foundNote=true;
insilmaril@0
  2871
				}	
insilmaril@0
  2872
			}
insilmaril@0
  2873
			// Searching in Heading
insilmaril@0
  2874
			if (searching && itFind->getHeading().contains (s,cs) ) 
insilmaril@0
  2875
			{
insilmaril@424
  2876
				xelection.select(itFind);
insilmaril@0
  2877
				ensureSelectionVisible();
insilmaril@0
  2878
				searching=false;
insilmaril@0
  2879
			}
insilmaril@0
  2880
		}	
insilmaril@0
  2881
		if (!foundNote)
insilmaril@0
  2882
		{
insilmaril@0
  2883
			itFind=itFind->next();
insilmaril@0
  2884
			if (!itFind) EOFind=true;
insilmaril@0
  2885
		}
insilmaril@0
  2886
	}	
insilmaril@0
  2887
	if (!searching)
insilmaril@424
  2888
		return xelection.getBranch();
insilmaril@424
  2889
	else
insilmaril@0
  2890
		return NULL;
insilmaril@0
  2891
}
insilmaril@0
  2892
insilmaril@0
  2893
void MapEditor::findReset()
insilmaril@0
  2894
{	// Necessary if text to find changes during a find process
insilmaril@0
  2895
	itFind=NULL;
insilmaril@0
  2896
	EOFind=false;
insilmaril@0
  2897
}
insilmaril@395
  2898
void MapEditor::setURL(const QString &url)
insilmaril@395
  2899
{
insilmaril@424
  2900
	BranchObj *bo=xelection.getBranch();
insilmaril@424
  2901
	if (bo)
insilmaril@424
  2902
	{
insilmaril@395
  2903
		QString oldurl=bo->getURL();
insilmaril@395
  2904
		bo->setURL (url);
insilmaril@395
  2905
		saveState (
insilmaril@424
  2906
			bo,
insilmaril@395
  2907
			QString ("setURL (\"%1\")").arg(oldurl),
insilmaril@424
  2908
			bo,
insilmaril@395
  2909
			QString ("setURL (\"%1\")").arg(url),
insilmaril@395
  2910
			QString ("set URL of %1 to %2").arg(getName(bo)).arg(url)
insilmaril@395
  2911
		);
insilmaril@395
  2912
		updateActions();
insilmaril@491
  2913
		mapCenter->reposition();
insilmaril@491
  2914
		xelection.update();
insilmaril@491
  2915
		ensureSelectionVisible();
insilmaril@395
  2916
	}
insilmaril@395
  2917
}	
insilmaril@0
  2918
insilmaril@0
  2919
void MapEditor::editURL()
insilmaril@0
  2920
{
insilmaril@424
  2921
	BranchObj *bo=xelection.getBranch();
insilmaril@424
  2922
	if (bo)
insilmaril@0
  2923
	{		
insilmaril@0
  2924
		bool ok;
insilmaril@0
  2925
		QString text = QInputDialog::getText(
insilmaril@0
  2926
				"VYM", tr("Enter URL:"), QLineEdit::Normal,
insilmaril@143
  2927
				bo->getURL(), &ok, this );
insilmaril@0
  2928
		if ( ok) 
insilmaril@0
  2929
			// user entered something and pressed OK
insilmaril@395
  2930
			setURL (text);
insilmaril@0
  2931
	}
insilmaril@0
  2932
}
insilmaril@0
  2933
insilmaril@266
  2934
QString MapEditor::getURL()
insilmaril@266
  2935
{
insilmaril@424
  2936
	BranchObj *bo=xelection.getBranch();
insilmaril@424
  2937
	if (bo)
insilmaril@424
  2938
		return bo->getURL();
insilmaril@266
  2939
	else
insilmaril@266
  2940
		return "";
insilmaril@266
  2941
}
insilmaril@266
  2942
insilmaril@366
  2943
QStringList MapEditor::getURLs()
insilmaril@366
  2944
{
insilmaril@366
  2945
	QStringList urls;
insilmaril@424
  2946
	BranchObj *bo=xelection.getBranch();
insilmaril@424
  2947
	if (bo)
insilmaril@366
  2948
	{		
insilmaril@366
  2949
		bo=bo->first();	
insilmaril@366
  2950
		while (bo) 
insilmaril@366
  2951
		{
insilmaril@366
  2952
			if (!bo->getURL().isEmpty()) urls.append( bo->getURL());
insilmaril@366
  2953
			bo=bo->next();
insilmaril@366
  2954
		}	
insilmaril@366
  2955
	}	
insilmaril@366
  2956
	return urls;
insilmaril@366
  2957
}
insilmaril@366
  2958
insilmaril@366
  2959
insilmaril@0
  2960
void MapEditor::editHeading2URL()
insilmaril@0
  2961
{
insilmaril@424
  2962
	BranchObj *bo=xelection.getBranch();
insilmaril@424
  2963
	if (bo)
insilmaril@424
  2964
		setURL (bo->getHeading());
insilmaril@0
  2965
}	
insilmaril@0
  2966
insilmaril@0
  2967
void MapEditor::editBugzilla2URL()
insilmaril@0
  2968
{
insilmaril@424
  2969
	BranchObj *bo=xelection.getBranch();
insilmaril@424
  2970
	if (bo)
insilmaril@0
  2971
	{		
insilmaril@143
  2972
		QString url= "https://bugzilla.novell.com/show_bug.cgi?id="+bo->getHeading();
insilmaril@395
  2973
		setURL (url);
insilmaril@0
  2974
	}
insilmaril@0
  2975
}	
insilmaril@0
  2976
insilmaril@175
  2977
void MapEditor::editFATE2URL()
insilmaril@175
  2978
{
insilmaril@424
  2979
	BranchObj *bo=xelection.getBranch();
insilmaril@424
  2980
	if (bo)
insilmaril@175
  2981
	{		
insilmaril@175
  2982
		QString url= "http://keeper.suse.de:8080/webfate/match/id?value=ID"+bo->getHeading();
insilmaril@395
  2983
		saveState(
insilmaril@424
  2984
			bo,
insilmaril@395
  2985
			"setURL (\""+bo->getURL()+"\")",
insilmaril@424
  2986
			bo,
insilmaril@395
  2987
			"setURL (\""+url+"\")",
insilmaril@395
  2988
			QString("Use heading of %1 as link to FATE").arg(getName(bo))
insilmaril@395
  2989
		);	
insilmaril@175
  2990
		bo->setURL (url);
insilmaril@175
  2991
		updateActions();
insilmaril@175
  2992
	}
insilmaril@175
  2993
}	
insilmaril@175
  2994
insilmaril@0
  2995
void MapEditor::editVymLink()
insilmaril@0
  2996
{
insilmaril@424
  2997
	BranchObj *bo=xelection.getBranch();
insilmaril@424
  2998
	if (bo)
insilmaril@0
  2999
	{		
insilmaril@404
  3000
		QStringList filters;
insilmaril@404
  3001
		filters <<"VYM map (*.vym)";
insilmaril@404
  3002
		QFileDialog *fd=new QFileDialog( this,vymName+" - " +tr("Link to another map"));
insilmaril@404
  3003
		fd->setFilters (filters);
insilmaril@404
  3004
		fd->setCaption(vymName+" - " +tr("Link to another map"));
insilmaril@442
  3005
		fd->setDirectory (lastFileDir);
insilmaril@143
  3006
		if (! bo->getVymLink().isEmpty() )
insilmaril@404
  3007
			fd->selectFile( bo->getVymLink() );
insilmaril@0
  3008
		fd->show();
insilmaril@0
  3009
insilmaril@0
  3010
		QString fn;
insilmaril@0
  3011
		if ( fd->exec() == QDialog::Accepted )
insilmaril@143
  3012
		{
insilmaril@442
  3013
			lastFileDir=QDir (fd->directory().path());
insilmaril@395
  3014
			saveState(
insilmaril@424
  3015
				bo,
insilmaril@395
  3016
				"setVymLink (\""+bo->getVymLink()+"\")",
insilmaril@424
  3017
				bo,
insilmaril@395
  3018
				"setVymLink (\""+fd->selectedFile()+"\")",
insilmaril@395
  3019
				QString("Set vymlink of %1 to %2").arg(getName(bo)).arg(fd->selectedFile())
insilmaril@395
  3020
			);	
insilmaril@491
  3021
			setVymLinkInt (fd->selectedFile() );
insilmaril@143
  3022
		}
insilmaril@0
  3023
	}
insilmaril@0
  3024
}
insilmaril@0
  3025
insilmaril@0
  3026
void MapEditor::deleteVymLink()
insilmaril@0
  3027
{
insilmaril@424
  3028
	BranchObj *bo=xelection.getBranch();
insilmaril@424
  3029
	if (bo)
insilmaril@0
  3030
	{		
insilmaril@395
  3031
		saveState(
insilmaril@424
  3032
			bo,
insilmaril@395
  3033
			"setVymLink (\""+bo->getVymLink()+"\")",
insilmaril@424
  3034
			bo,
insilmaril@395
  3035
			"setVymLink (\"\")",
insilmaril@395
  3036
			QString("Unset vymlink of %1").arg(getName(bo))
insilmaril@395
  3037
		);	
insilmaril@143
  3038
		bo->setVymLink ("" );
insilmaril@0
  3039
		updateActions();
insilmaril@0
  3040
		mapCenter->reposition();
insilmaril@408
  3041
		scene()->update();
insilmaril@0
  3042
	}
insilmaril@0
  3043
}
insilmaril@0
  3044
insilmaril@395
  3045
void MapEditor::setHideExport(bool b)
insilmaril@243
  3046
{
insilmaril@424
  3047
	BranchObj *bo=xelection.getBranch();
insilmaril@424
  3048
	if (bo)
insilmaril@395
  3049
	{
insilmaril@424
  3050
		bo->setHideInExport (b);
insilmaril@395
  3051
		QString u= b ? "false" : "true";
insilmaril@395
  3052
		QString r=!b ? "false" : "true";
insilmaril@395
  3053
		
insilmaril@395
  3054
		saveState(
insilmaril@424
  3055
			bo,
insilmaril@395
  3056
			QString ("setHideExport (%1)").arg(u),
insilmaril@424
  3057
			bo,
insilmaril@395
  3058
			QString ("setHideExport (%1)").arg(r),
insilmaril@424
  3059
			QString ("Set HideExport flag of %1 to %2").arg(getName(bo)).arg (r)
insilmaril@395
  3060
		);	
insilmaril@243
  3061
		updateActions();
insilmaril@243
  3062
		mapCenter->reposition();
insilmaril@491
  3063
		xelection.update();
insilmaril@408
  3064
		scene()->update();
insilmaril@243
  3065
	}
insilmaril@243
  3066
}
insilmaril@243
  3067
insilmaril@395
  3068
void MapEditor::toggleHideExport()
insilmaril@395
  3069
{
insilmaril@424
  3070
	BranchObj *bo=xelection.getBranch();
insilmaril@424
  3071
	if (bo)
insilmaril@424
  3072
		setHideExport ( !bo->hideInExport() );
insilmaril@395
  3073
}
insilmaril@395
  3074
insilmaril@0
  3075
QString MapEditor::getVymLink()
insilmaril@0
  3076
{
insilmaril@424
  3077
	BranchObj *bo=xelection.getBranch();
insilmaril@424
  3078
	if (bo)
insilmaril@424
  3079
		return bo->getVymLink();
insilmaril@424
  3080
	else	
insilmaril@424
  3081
		return "";
insilmaril@0
  3082
	
insilmaril@0
  3083
}
insilmaril@0
  3084
insilmaril@366
  3085
QStringList MapEditor::getVymLinks()
insilmaril@366
  3086
{
insilmaril@366
  3087
	QStringList links;
insilmaril@424
  3088
	BranchObj *bo=xelection.getBranch();
insilmaril@424
  3089
	if (bo)
insilmaril@366
  3090
	{		
insilmaril@366
  3091
		bo=bo->first();	
insilmaril@366
  3092
		while (bo) 
insilmaril@366
  3093
		{
insilmaril@366
  3094
			if (!bo->getVymLink().isEmpty()) links.append( bo->getVymLink());
insilmaril@366
  3095
			bo=bo->next();
insilmaril@366
  3096
		}	
insilmaril@366
  3097
	}	
insilmaril@366
  3098
	return links;
insilmaril@366
  3099
}
insilmaril@366
  3100
insilmaril@366
  3101
insilmaril@395
  3102
void MapEditor::deleteKeepChilds()
insilmaril@98
  3103
{
insilmaril@424
  3104
	BranchObj *bo=xelection.getBranch();
insilmaril@424
  3105
	BranchObj *par;
insilmaril@424
  3106
	if (bo)
insilmaril@424
  3107
	{
insilmaril@424
  3108
		par=(BranchObj*)(bo->getParObj());
insilmaril@408
  3109
		QPointF p=bo->getRelPos();
insilmaril@395
  3110
		saveStateChangingPart(
insilmaril@424
  3111
			bo->getParObj(),
insilmaril@424
  3112
			bo,
insilmaril@395
  3113
			"deleteKeepChilds ()",
insilmaril@395
  3114
			QString("Remove %1 and keep its childs").arg(getName(bo))
insilmaril@395
  3115
		);
insilmaril@394
  3116
insilmaril@424
  3117
		QString sel=bo->getSelectString();
insilmaril@98
  3118
		unselect();
insilmaril@98
  3119
		par->removeBranchHere(bo);
insilmaril@98
  3120
		mapCenter->reposition();
insilmaril@98
  3121
		select (sel);
insilmaril@424
  3122
		xelection.getBranch()->move2RelPos (p);
insilmaril@394
  3123
		mapCenter->reposition();
insilmaril@98
  3124
	}	
insilmaril@98
  3125
}
insilmaril@98
  3126
insilmaril@395
  3127
void MapEditor::deleteChilds()
insilmaril@98
  3128
{
insilmaril@424
  3129
	BranchObj *bo=xelection.getBranch();
insilmaril@424
  3130
	if (bo)
insilmaril@98
  3131
	{		
insilmaril@395
  3132
		saveStateChangingPart(
insilmaril@491
  3133
			bo, 
insilmaril@424
  3134
			bo,
insilmaril@395
  3135
			"deleteChilds ()",
insilmaril@424
  3136
			QString( "Remove childs of branch %1").arg(getName(bo))
insilmaril@395
  3137
		);
insilmaril@424
  3138
		bo->removeChilds();
insilmaril@98
  3139
		mapCenter->reposition();
insilmaril@98
  3140
	}	
insilmaril@98
  3141
}
insilmaril@98
  3142
insilmaril@0
  3143
void MapEditor::editMapInfo()
insilmaril@0
  3144
{
insilmaril@0
  3145
	ExtraInfoDialog dia;
insilmaril@0
  3146
	dia.setMapName (getFileName() );
insilmaril@0
  3147
	dia.setAuthor (mapCenter->getAuthor() );
insilmaril@0
  3148
	dia.setComment(mapCenter->getComment() );
insilmaril@0
  3149
insilmaril@0
  3150
	// Calc some stats
insilmaril@0
  3151
	QString stats;
insilmaril@408
  3152
    stats+=tr("%1 items on map\n","Info about map").arg (mapScene->items().size(),6);
insilmaril@0
  3153
insilmaril@0
  3154
	uint b=0;
insilmaril@0
  3155
	uint f=0;
insilmaril@0
  3156
	uint n=0;
insilmaril@103
  3157
	uint xl=0;
insilmaril@0
  3158
	BranchObj *bo;
insilmaril@0
  3159
	bo=mapCenter->first();
insilmaril@0
  3160
	while (bo) 
insilmaril@0
  3161
	{
insilmaril@0
  3162
		if (!bo->getNote().isEmpty() ) n++;
insilmaril@0
  3163
		f+= bo->countFloatImages();
insilmaril@0
  3164
		b++;
insilmaril@103
  3165
		xl+=bo->countXLinks();
insilmaril@0
  3166
		bo=bo->next();
insilmaril@0
  3167
	}
insilmaril@0
  3168
    stats+=QString ("%1 branches\n").arg (b-1,6);
insilmaril@104
  3169
    stats+=QString ("%1 xLinks \n").arg (xl,6);
insilmaril@0
  3170
    stats+=QString ("%1 notes\n").arg (n,6);
insilmaril@0
  3171
    stats+=QString ("%1 images\n").arg (f,6);
insilmaril@0
  3172
	dia.setStats (stats);
insilmaril@0
  3173
insilmaril@0
  3174
	// Finally show dialog
insilmaril@0
  3175
	if (dia.exec() == QDialog::Accepted)
insilmaril@0
  3176
	{
insilmaril@398
  3177
		setMapAuthor (dia.getAuthor() );
insilmaril@398
  3178
		setMapComment (dia.getComment() );
insilmaril@0
  3179
	}
insilmaril@0
  3180
}
insilmaril@0
  3181
insilmaril@442
  3182
void MapEditor::ensureSelectionVisible()
insilmaril@442
  3183
{
insilmaril@442
  3184
	LinkableMapObj *lmo=xelection.single();
insilmaril@442
  3185
	if (lmo) ensureVisible (lmo->getBBox() );
insilmaril@442
  3186
	
insilmaril@442
  3187
}
insilmaril@442
  3188
insilmaril@442
  3189
void MapEditor::updateSelection()
insilmaril@442
  3190
{
insilmaril@442
  3191
	// Tell selection to update geometries
insilmaril@442
  3192
	xelection.update();
insilmaril@442
  3193
}
insilmaril@442
  3194
insilmaril@0
  3195
void MapEditor::updateActions()
insilmaril@0
  3196
{
insilmaril@442
  3197
	// Tell mainwindow to update states of actions
insilmaril@390
  3198
	mainWindow->updateActions();
insilmaril@412
  3199
	// TODO maybe don't update if blockReposition is set
insilmaril@0
  3200
}
insilmaril@0
  3201
insilmaril@160
  3202
void MapEditor::updateNoteFlag()
insilmaril@160
  3203
{
insilmaril@427
  3204
	setChanged();
insilmaril@424
  3205
	BranchObj *bo=xelection.getBranch();
insilmaril@427
  3206
	if (bo) 
insilmaril@427
  3207
	{
insilmaril@427
  3208
		bo->updateNoteFlag();
insilmaril@427
  3209
		mainWindow->updateActions();
insilmaril@427
  3210
	}	
insilmaril@160
  3211
}
insilmaril@160
  3212
insilmaril@398
  3213
void MapEditor::setMapAuthor (const QString &s)
insilmaril@0
  3214
{
insilmaril@398
  3215
	saveState (
insilmaril@424
  3216
		mapCenter,
insilmaril@398
  3217
		QString ("setMapAuthor (\"%1\")").arg(mapCenter->getAuthor()),
insilmaril@424
  3218
		mapCenter,
insilmaril@398
  3219
		QString ("setMapAuthor (\"%1\")").arg(s),
insilmaril@398
  3220
		QString ("Set author of map to \"%1\"").arg(s)
insilmaril@398
  3221
	);
insilmaril@398
  3222
	mapCenter->setAuthor (s);
insilmaril@398
  3223
}
insilmaril@398
  3224
insilmaril@398
  3225
void MapEditor::setMapComment (const QString &s)
insilmaril@398
  3226
{
insilmaril@398
  3227
	saveState (
insilmaril@424
  3228
		mapCenter,
insilmaril@398
  3229
		QString ("setMapComment (\"%1\")").arg(mapCenter->getComment()),
insilmaril@424
  3230
		mapCenter,
insilmaril@398
  3231
		QString ("setMapComment (\"%1\")").arg(s),
insilmaril@398
  3232
		QString ("Set comment of map")
insilmaril@398
  3233
	);
insilmaril@398
  3234
	mapCenter->setComment (s);
insilmaril@398
  3235
}
insilmaril@398
  3236
insilmaril@398
  3237
void MapEditor::setMapLinkStyle (const QString & s)
insilmaril@398
  3238
{
insilmaril@398
  3239
	saveStateChangingPart (
insilmaril@398
  3240
		mapCenter,
insilmaril@398
  3241
		mapCenter,
insilmaril@398
  3242
		QString("setMapLinkStyle (\"%1\")").arg(s),
insilmaril@398
  3243
		QString("Set map link style (\"%1\")").arg(s)
insilmaril@398
  3244
	);	
insilmaril@398
  3245
insilmaril@398
  3246
	if (s=="StyleLine")
insilmaril@473
  3247
		linkstyle=LinkableMapObj::Line;
insilmaril@398
  3248
	else if (s=="StyleParabel")
insilmaril@473
  3249
		linkstyle=LinkableMapObj::Parabel;
insilmaril@398
  3250
	else if (s=="StylePolyLine")
insilmaril@473
  3251
		linkstyle=LinkableMapObj::PolyLine;
insilmaril@398
  3252
	else	
insilmaril@473
  3253
		linkstyle=LinkableMapObj::PolyParabel;
insilmaril@398
  3254
insilmaril@0
  3255
	BranchObj *bo;
insilmaril@0
  3256
	bo=mapCenter->first();
insilmaril@0
  3257
	bo=bo->next();
insilmaril@0
  3258
	while (bo) 
insilmaril@0
  3259
	{
insilmaril@0
  3260
		bo->setLinkStyle(bo->getDefLinkStyle());
insilmaril@0
  3261
		bo=bo->next();
insilmaril@0
  3262
	}
insilmaril@102
  3263
	mapCenter->reposition();
insilmaril@0
  3264
}
insilmaril@0
  3265
insilmaril@473
  3266
LinkableMapObj::Style MapEditor::getMapLinkStyle ()
insilmaril@0
  3267
{
insilmaril@0
  3268
	return linkstyle;
insilmaril@0
  3269
}	
insilmaril@0
  3270
insilmaril@398
  3271
void MapEditor::setMapDefLinkColor(QColor c)
insilmaril@0
  3272
{
insilmaril@97
  3273
	defLinkColor=c;
insilmaril@497
  3274
	BranchObj *bo;
insilmaril@497
  3275
	bo=mapCenter->first();
insilmaril@497
  3276
	while (bo) 
insilmaril@497
  3277
	{
insilmaril@497
  3278
		bo->setLinkColor();
insilmaril@497
  3279
		bo=bo->next();
insilmaril@497
  3280
	}
insilmaril@0
  3281
	updateActions();
insilmaril@0
  3282
}
insilmaril@0
  3283
insilmaril@398
  3284
void MapEditor::setMapLinkColorHintInt()
insilmaril@0
  3285
{
insilmaril@398
  3286
	// called from setMapLinkColorHint(lch) or at end of parse
insilmaril@0
  3287
	BranchObj *bo;
insilmaril@0
  3288
	bo=mapCenter->first();
insilmaril@0
  3289
	while (bo) 
insilmaril@0
  3290
	{
insilmaril@0
  3291
		bo->setLinkColor();
insilmaril@0
  3292
		bo=bo->next();
insilmaril@0
  3293
	}
insilmaril@0
  3294
}
insilmaril@0
  3295
insilmaril@473
  3296
void MapEditor::setMapLinkColorHint(LinkableMapObj::ColorHint lch)
insilmaril@0
  3297
{
insilmaril@0
  3298
	linkcolorhint=lch;
insilmaril@398
  3299
	setMapLinkColorHintInt();
insilmaril@0
  3300
}
insilmaril@0
  3301
insilmaril@398
  3302
void MapEditor::toggleMapLinkColorHint()
insilmaril@0
  3303
{
insilmaril@473
  3304
	if (linkcolorhint==LinkableMapObj::HeadingColor)
insilmaril@473
  3305
		linkcolorhint=LinkableMapObj::DefaultColor;
insilmaril@0
  3306
	else	
insilmaril@473
  3307
		linkcolorhint=LinkableMapObj::HeadingColor;
insilmaril@0
  3308
	BranchObj *bo;
insilmaril@0
  3309
	bo=mapCenter->first();
insilmaril@0
  3310
	while (bo) 
insilmaril@0
  3311
	{
insilmaril@0
  3312
		bo->setLinkColor();
insilmaril@0
  3313
		bo=bo->next();
insilmaril@0
  3314
	}
insilmaril@0
  3315
}
insilmaril@0
  3316
insilmaril@473
  3317
LinkableMapObj::ColorHint MapEditor::getMapLinkColorHint()
insilmaril@0
  3318
{
insilmaril@0
  3319
	return linkcolorhint;
insilmaril@0
  3320
}
insilmaril@0
  3321
insilmaril@398
  3322
QColor MapEditor::getMapDefLinkColor()
insilmaril@0
  3323
{
insilmaril@97
  3324
	return defLinkColor;
insilmaril@97
  3325
}
insilmaril@97
  3326
insilmaril@398
  3327
void MapEditor::setMapDefXLinkColor(QColor col)
insilmaril@97
  3328
{
insilmaril@97
  3329
	defXLinkColor=col;
insilmaril@97
  3330
}
insilmaril@97
  3331
insilmaril@398
  3332
QColor MapEditor::getMapDefXLinkColor()
insilmaril@97
  3333
{
insilmaril@97
  3334
	return defXLinkColor;
insilmaril@97
  3335
}
insilmaril@97
  3336
insilmaril@398
  3337
void MapEditor::setMapDefXLinkWidth (int w)
insilmaril@97
  3338
{
insilmaril@97
  3339
	defXLinkWidth=w;
insilmaril@97
  3340
}
insilmaril@97
  3341
insilmaril@398
  3342
int MapEditor::getMapDefXLinkWidth()
insilmaril@97
  3343
{
insilmaril@97
  3344
	return defXLinkWidth;
insilmaril@0
  3345
}
insilmaril@0
  3346
insilmaril@398
  3347
void MapEditor::selectMapLinkColor()
insilmaril@0
  3348
{
insilmaril@97
  3349
	QColor col = QColorDialog::getColor( defLinkColor, this );
insilmaril@0
  3350
	if ( !col.isValid() ) return;
insilmaril@398
  3351
	saveState (
insilmaril@424
  3352
		mapCenter,
insilmaril@398
  3353
		QString("setMapDefLinkColor (\"%1\")").arg(getMapDefLinkColor().name()),
insilmaril@424
  3354
		mapCenter,
insilmaril@398
  3355
		QString("setMapDefLinkColor (\"%1\")").arg(col.name()),
insilmaril@497
  3356
		QString("Set map link color to %1").arg(col.name())
insilmaril@398
  3357
	);
insilmaril@398
  3358
	setMapDefLinkColor( col );
insilmaril@442
  3359
}
insilmaril@442
  3360
insilmaril@442
  3361
void MapEditor::selectMapSelectionColor()
insilmaril@442
  3362
{
insilmaril@442
  3363
	QColor col = QColorDialog::getColor( defLinkColor, this );
insilmaril@442
  3364
	setSelectionColor (col);
insilmaril@442
  3365
}
insilmaril@442
  3366
insilmaril@442
  3367
void MapEditor::setSelectionColorInt (QColor col)
insilmaril@442
  3368
{
insilmaril@442
  3369
	if ( !col.isValid() ) return;
insilmaril@442
  3370
	xelection.setColor (col);
insilmaril@442
  3371
}
insilmaril@442
  3372
insilmaril@442
  3373
void MapEditor::setSelectionColor(QColor col)
insilmaril@442
  3374
{
insilmaril@442
  3375
	if ( !col.isValid() ) return;
insilmaril@442
  3376
	saveState (
insilmaril@442
  3377
		mapCenter,
insilmaril@442
  3378
		QString("setSelectionColor (%1)").arg(xelection.getColor().name()),
insilmaril@442
  3379
		mapCenter,
insilmaril@442
  3380
		QString("setSelectionColor (%1)").arg(col.name()),
insilmaril@442
  3381
		QString("Set color of selection box to %1").arg(col.name())
insilmaril@442
  3382
	);
insilmaril@442
  3383
	setSelectionColorInt (col);
insilmaril@442
  3384
}
insilmaril@442
  3385
insilmaril@442
  3386
QColor MapEditor::getSelectionColor()
insilmaril@442
  3387
{
insilmaril@442
  3388
	return xelection.getColor();
insilmaril@0
  3389
}
insilmaril@0
  3390
insilmaril@473
  3391
bool MapEditor::scrollBranch(BranchObj *bo)
insilmaril@0
  3392
{
insilmaril@427
  3393
	if (bo)
insilmaril@0
  3394
	{
insilmaril@427
  3395
		if (bo->isScrolled()) return false;
insilmaril@427
  3396
		if (bo->countBranches()==0) return false;
insilmaril@427
  3397
		if (bo->getDepth()==0) return false;
insilmaril@395
  3398
		QString u,r;
insilmaril@427
  3399
		r="scroll";
insilmaril@427
  3400
		u="unscroll";
insilmaril@395
  3401
		saveState(
insilmaril@424
  3402
			bo,
insilmaril@395
  3403
			QString ("%1 ()").arg(u),
insilmaril@424
  3404
			bo,
insilmaril@395
  3405
			QString ("%1 ()").arg(r),
insilmaril@395
  3406
			QString ("%1 %2").arg(r).arg(getName(bo))
insilmaril@395
  3407
		);
insilmaril@0
  3408
		bo->toggleScroll();
insilmaril@491
  3409
		xelection.update();
insilmaril@408
  3410
		scene()->update();
insilmaril@427
  3411
		return true;
insilmaril@427
  3412
	}	
insilmaril@427
  3413
	return false;
insilmaril@427
  3414
}
insilmaril@427
  3415
insilmaril@473
  3416
bool MapEditor::unscrollBranch(BranchObj *bo)
insilmaril@427
  3417
{
insilmaril@427
  3418
	if (bo)
insilmaril@427
  3419
	{
insilmaril@427
  3420
		if (!bo->isScrolled()) return false;
insilmaril@427
  3421
		if (bo->countBranches()==0) return false;
insilmaril@427
  3422
		if (bo->getDepth()==0) return false;
insilmaril@427
  3423
		QString u,r;
insilmaril@427
  3424
		u="scroll";
insilmaril@427
  3425
		r="unscroll";
insilmaril@427
  3426
		saveState(
insilmaril@427
  3427
			bo,
insilmaril@427
  3428
			QString ("%1 ()").arg(u),
insilmaril@427
  3429
			bo,
insilmaril@427
  3430
			QString ("%1 ()").arg(r),
insilmaril@427
  3431
			QString ("%1 %2").arg(r).arg(getName(bo))
insilmaril@427
  3432
		);
insilmaril@427
  3433
		bo->toggleScroll();
insilmaril@491
  3434
		xelection.update();
insilmaril@427
  3435
		scene()->update();
insilmaril@427
  3436
		return true;
insilmaril@427
  3437
	}	
insilmaril@427
  3438
	return false;
insilmaril@427
  3439
}
insilmaril@427
  3440
insilmaril@427
  3441
void MapEditor::toggleScroll()
insilmaril@427
  3442
{
insilmaril@427
  3443
	BranchObj *bo=xelection.getBranch();
insilmaril@491
  3444
	if (xelection.type()==Selection::Branch )
insilmaril@427
  3445
	{
insilmaril@427
  3446
		if (bo->isScrolled())
insilmaril@473
  3447
			unscrollBranch (bo);
insilmaril@427
  3448
		else
insilmaril@473
  3449
			scrollBranch (bo);
insilmaril@0
  3450
	}
insilmaril@0
  3451
}
insilmaril@0
  3452
insilmaril@473
  3453
void MapEditor::unscrollChilds() 
insilmaril@0
  3454
{
insilmaril@428
  3455
	BranchObj *bo=xelection.getBranch();
insilmaril@428
  3456
	if (bo)
insilmaril@0
  3457
	{
insilmaril@428
  3458
		bo->first();
insilmaril@428
  3459
		while (bo) 
insilmaril@428
  3460
		{
insilmaril@473
  3461
			if (bo->isScrolled()) unscrollBranch (bo);
insilmaril@428
  3462
			bo=bo->next();
insilmaril@428
  3463
		}
insilmaril@428
  3464
	}	
insilmaril@0
  3465
}
insilmaril@0
  3466
insilmaril@432
  3467
FloatImageObj* MapEditor::loadFloatImageInt (QString fn)
insilmaril@432
  3468
{
insilmaril@432
  3469
	BranchObj *bo=xelection.getBranch();
insilmaril@432
  3470
	if (bo)
insilmaril@432
  3471
	{
insilmaril@432
  3472
		FloatImageObj *fio;
insilmaril@432
  3473
		bo->addFloatImage();
insilmaril@432
  3474
		fio=bo->getLastFloatImage();
insilmaril@432
  3475
		fio->load(fn);
insilmaril@432
  3476
		mapCenter->reposition();
insilmaril@432
  3477
		scene()->update();
insilmaril@432
  3478
		return fio;
insilmaril@432
  3479
	}
insilmaril@432
  3480
	return NULL;
insilmaril@432
  3481
}	
insilmaril@428
  3482
insilmaril@0
  3483
void MapEditor::loadFloatImage ()
insilmaril@0
  3484
{
insilmaril@424
  3485
	BranchObj *bo=xelection.getBranch();
insilmaril@424
  3486
	if (bo)
insilmaril@0
  3487
	{
insilmaril@0
  3488
insilmaril@366
  3489
		Q3FileDialog *fd=new Q3FileDialog( this);
insilmaril@366
  3490
		fd->setMode (Q3FileDialog::ExistingFiles);
insilmaril@0
  3491
		fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
insilmaril@0
  3492
		ImagePreview *p =new ImagePreview (fd);
insilmaril@0
  3493
		fd->setContentsPreviewEnabled( TRUE );
insilmaril@0
  3494
		fd->setContentsPreview( p, p );
insilmaril@366
  3495
		fd->setPreviewMode( Q3FileDialog::Contents );
insilmaril@404
  3496
		fd->setCaption(vymName+" - " +tr("Load image"));
insilmaril@0
  3497
		fd->setDir (lastImageDir);
insilmaril@0
  3498
		fd->show();
insilmaril@0
  3499
insilmaril@0
  3500
		if ( fd->exec() == QDialog::Accepted )
insilmaril@0
  3501
		{
insilmaril@491
  3502
			// TODO loadFIO in QT4 use:	lastImageDir=fd->directory();
insilmaril@366
  3503
			lastImageDir=QDir (fd->dirPath());
insilmaril@432
  3504
			QString s;
insilmaril@395
  3505
			FloatImageObj *fio;
insilmaril@432
  3506
			for (int j=0; j<fd->selectedFiles().count(); j++)
insilmaril@185
  3507
			{
insilmaril@432
  3508
				s=fd->selectedFiles().at(j);
insilmaril@432
  3509
				fio=loadFloatImageInt (s);
insilmaril@432
  3510
				if (fio)
insilmaril@432
  3511
					saveState(
insilmaril@432
  3512
						(LinkableMapObj*)fio,
insilmaril@432
  3513
						"delete ()",
insilmaril@432
  3514
						bo, 
insilmaril@432
  3515
						QString ("loadImage (%1)").arg(s ),
insilmaril@432
  3516
						QString("Add image %1 to %2").arg(s).arg(getName(bo))
insilmaril@432
  3517
					);
insilmaril@432
  3518
				else
insilmaril@491
  3519
					// TODO loadFIO error handling
insilmaril@432
  3520
					qWarning ("Failed to load "+s);
insilmaril@185
  3521
			}
insilmaril@0
  3522
		}
insilmaril@236
  3523
		delete (p);
insilmaril@236
  3524
		delete (fd);
insilmaril@0
  3525
	}
insilmaril@0
  3526
}
insilmaril@0
  3527
insilmaril@434
  3528
void MapEditor::saveFloatImageInt  (FloatImageObj *fio, const QString &type, const QString &fn)
insilmaril@434
  3529
{
insilmaril@434
  3530
	fio->save (fn,type);
insilmaril@434
  3531
}
insilmaril@434
  3532
insilmaril@366
  3533
void MapEditor::saveFloatImage ()
insilmaril@0
  3534
{
insilmaril@424
  3535
	FloatImageObj *fio=xelection.getFloatImage();
insilmaril@424
  3536
	if (fio)
insilmaril@0
  3537
	{
insilmaril@366
  3538
		QFileDialog *fd=new QFileDialog( this);
insilmaril@366
  3539
		fd->setFilters (imageIO.getFilters());
insilmaril@404
  3540
		fd->setCaption(vymName+" - " +tr("Save image"));
insilmaril@366
  3541
		fd->setFileMode( QFileDialog::AnyFile );
insilmaril@366
  3542
		fd->setDirectory (lastImageDir);
insilmaril@366
  3543
//		fd->setSelection (fio->getOriginalFilename());
insilmaril@0
  3544
		fd->show();
insilmaril@0
  3545
insilmaril@0
  3546
		QString fn;
insilmaril@434
  3547
		if ( fd->exec() == QDialog::Accepted && fd->selectedFiles().count()==1)
insilmaril@0
  3548
		{
insilmaril@434
  3549
			fn=fd->selectedFiles().at(0);
insilmaril@434
  3550
			if (QFile (fn).exists() )
insilmaril@0
  3551
			{
insilmaril@404
  3552
				QMessageBox mb( vymName,
insilmaril@159
  3553
					tr("The file %1 exists already.\n"
insilmaril@434
  3554
					"Do you want to overwrite it?").arg(fn),
insilmaril@0
  3555
				QMessageBox::Warning,
insilmaril@0
  3556
				QMessageBox::Yes | QMessageBox::Default,
insilmaril@0
  3557
				QMessageBox::Cancel | QMessageBox::Escape,
insilmaril@0
  3558
				QMessageBox::QMessageBox::NoButton );
insilmaril@0
  3559
insilmaril@0
  3560
				mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
insilmaril@0
  3561
				mb.setButtonText( QMessageBox::No, tr("Cancel"));
insilmaril@0
  3562
				switch( mb.exec() ) 
insilmaril@0
  3563
				{
insilmaril@0
  3564
					case QMessageBox::Yes:
insilmaril@0
  3565
						// save 
insilmaril@0
  3566
						break;;
insilmaril@0
  3567
					case QMessageBox::Cancel:
insilmaril@0
  3568
						// do nothing
insilmaril@377
  3569
						delete (fd);
insilmaril@0
  3570
						return;
insilmaril@0
  3571
						break;
insilmaril@0
  3572
				}
insilmaril@0
  3573
			}
insilmaril@434
  3574
			saveFloatImageInt (fio,fd->selectedFilter(),fn );
insilmaril@0
  3575
		}
insilmaril@377
  3576
		delete (fd);
insilmaril@0
  3577
	}
insilmaril@0
  3578
}
insilmaril@0
  3579
insilmaril@473
  3580
void MapEditor::setFrameType(const FrameObj::FrameType &t)
insilmaril@0
  3581
{
insilmaril@424
  3582
	BranchObj *bo=xelection.getBranch();
insilmaril@424
  3583
	if (bo)
insilmaril@0
  3584
	{
insilmaril@473
  3585
		QString s=bo->getFrameTypeName();
insilmaril@424
  3586
		bo->setFrameType (t);
insilmaril@473
  3587
		saveState (bo, QString("setFrameType (\"%1\")").arg(s),
insilmaril@473
  3588
			bo, QString ("setFrameType (\"%1\")").arg(bo->getFrameTypeName()),QString ("set type of frame to %1").arg(s));
insilmaril@0
  3589
		mapCenter->reposition();
insilmaril@424
  3590
		bo->updateLink();
insilmaril@0
  3591
	}
insilmaril@0
  3592
}
insilmaril@0
  3593
insilmaril@473
  3594
void MapEditor::setFrameType(const QString &s)	
insilmaril@445
  3595
{
insilmaril@445
  3596
	BranchObj *bo=xelection.getBranch();
insilmaril@445
  3597
	if (bo)
insilmaril@445
  3598
	{
insilmaril@473
  3599
		saveState (bo, QString("setFrameType (\"%1\")").arg(bo->getFrameTypeName()),
insilmaril@473
  3600
			bo, QString ("setFrameType (\"%1\")").arg(s),QString ("set type of frame to %1").arg(s));
insilmaril@445
  3601
		bo->setFrameType (s);
insilmaril@445
  3602
		mapCenter->reposition();
insilmaril@445
  3603
		bo->updateLink();
insilmaril@445
  3604
	}
insilmaril@445
  3605
}
insilmaril@445
  3606
insilmaril@473
  3607
void MapEditor::setFramePenColor(const QColor &c)	
insilmaril@442
  3608
{
insilmaril@442
  3609
	BranchObj *bo=xelection.getBranch();
insilmaril@442
  3610
	if (bo)
insilmaril@473
  3611
	{
insilmaril@473
  3612
		saveState (bo, QString("setFramePenColor (\"%1\")").arg(bo->getFramePenColor().name() ),
insilmaril@473
  3613
			bo, QString ("setFramePenColor (\"%1\")").arg(c.name() ),QString ("set pen color of frame to %1").arg(c.name() ));
insilmaril@442
  3614
		bo->setFramePenColor (c);
insilmaril@473
  3615
	}	
insilmaril@442
  3616
}
insilmaril@442
  3617
insilmaril@473
  3618
void MapEditor::setFrameBrushColor(const QColor &c)	
insilmaril@442
  3619
{
insilmaril@442
  3620
	BranchObj *bo=xelection.getBranch();
insilmaril@442
  3621
	if (bo)
insilmaril@473
  3622
	{
insilmaril@473
  3623
		saveState (bo, QString("setFrameBrushColor (\"%1\")").arg(bo->getFrameBrushColor().name() ),
insilmaril@473
  3624
			bo, QString ("setFrameBrushColor (\"%1\")").arg(c.name() ),QString ("set brush color of frame to %1").arg(c.name() ));
insilmaril@442
  3625
		bo->setFrameBrushColor (c);
insilmaril@473
  3626
	}	
insilmaril@473
  3627
}
insilmaril@473
  3628
insilmaril@486
  3629
void MapEditor::setFramePadding (const int &i)
insilmaril@473
  3630
{
insilmaril@473
  3631
	BranchObj *bo=xelection.getBranch();
insilmaril@473
  3632
	if (bo)
insilmaril@473
  3633
	{
insilmaril@486
  3634
		saveState (bo, QString("setFramePadding (\"%1\")").arg(bo->getFramePadding() ),
insilmaril@486
  3635
			bo, QString ("setFramePadding (\"%1\")").arg(i),QString ("set brush color of frame to %1").arg(i));
insilmaril@486
  3636
		bo->setFramePadding (i);
insilmaril@486
  3637
		mapCenter->reposition();
insilmaril@486
  3638
		bo->updateLink();
insilmaril@473
  3639
	}	
insilmaril@473
  3640
}
insilmaril@473
  3641
insilmaril@486
  3642
void MapEditor::setFrameBorderWidth(const int &i)
insilmaril@473
  3643
{
insilmaril@473
  3644
	BranchObj *bo=xelection.getBranch();
insilmaril@473
  3645
	if (bo)
insilmaril@473
  3646
	{
insilmaril@486
  3647
		saveState (bo, QString("setFrameBorderWidth (\"%1\")").arg(bo->getFrameBorderWidth() ),
insilmaril@486
  3648
			bo, QString ("setFrameBorderWidth (\"%1\")").arg(i),QString ("set border width of frame to %1").arg(i));
insilmaril@486
  3649
		bo->setFrameBorderWidth (i);
insilmaril@486
  3650
		mapCenter->reposition();
insilmaril@486
  3651
		bo->updateLink();
insilmaril@473
  3652
	}	
insilmaril@442
  3653
}
insilmaril@442
  3654
insilmaril@437
  3655
void MapEditor::setIncludeImagesVer(bool b)	
insilmaril@175
  3656
{
insilmaril@424
  3657
	BranchObj *bo=xelection.getBranch();
insilmaril@424
  3658
	if (bo)
insilmaril@424
  3659
	{
insilmaril@434
  3660
		QString u= b ? "false" : "true";
insilmaril@434
  3661
		QString r=!b ? "false" : "true";
insilmaril@434
  3662
		
insilmaril@434
  3663
		saveState(
insilmaril@434
  3664
			bo,
insilmaril@434
  3665
			QString("setIncludeImagesVertically (%1)").arg(u),
insilmaril@434
  3666
			bo, 
insilmaril@434
  3667
			QString("setIncludeImagesVertically (%1)").arg(r),
insilmaril@434
  3668
			QString("Include images vertically in %1").arg(getName(bo))
insilmaril@434
  3669
		);	
insilmaril@424
  3670
		bo->setIncludeImagesVer(b);
insilmaril@175
  3671
		mapCenter->reposition();
insilmaril@424
  3672
	}	
insilmaril@175
  3673
}
insilmaril@175
  3674
insilmaril@437
  3675
void MapEditor::setIncludeImagesHor(bool b)	
insilmaril@175
  3676
{
insilmaril@424
  3677
	BranchObj *bo=xelection.getBranch();
insilmaril@424
  3678
	if (bo)
insilmaril@424
  3679
	{
insilmaril@434
  3680
		QString u= b ? "false" : "true";
insilmaril@434
  3681
		QString r=!b ? "false" : "true";
insilmaril@434
  3682
		
insilmaril@434
  3683
		saveState(
insilmaril@434
  3684
			bo,
insilmaril@434
  3685
			QString("setIncludeImagesHorizontally (%1)").arg(u),
insilmaril@434
  3686
			bo, 
insilmaril@434
  3687
			QString("setIncludeImagesHorizontally (%1)").arg(r),
insilmaril@434
  3688
			QString("Include images horizontally in %1").arg(getName(bo))
insilmaril@434
  3689
		);	
insilmaril@424
  3690
		bo->setIncludeImagesHor(b);
insilmaril@175
  3691
		mapCenter->reposition();
insilmaril@424
  3692
	}	
insilmaril@175
  3693
}
insilmaril@175
  3694
insilmaril@491
  3695
void MapEditor::setHideLinkUnselected (bool b)
insilmaril@160
  3696
{
insilmaril@424
  3697
	LinkableMapObj *sel=xelection.single();
insilmaril@424
  3698
	if (sel &&
insilmaril@491
  3699
		(xelection.type() == Selection::Branch || 
insilmaril@491
  3700
		xelection.type() == Selection::MapCenter  ||
insilmaril@491
  3701
		xelection.type() == Selection::FloatImage ))
insilmaril@491
  3702
	{
insilmaril@491
  3703
		QString u= b ? "false" : "true";
insilmaril@491
  3704
		QString r=!b ? "false" : "true";
insilmaril@491
  3705
		
insilmaril@491
  3706
		saveState(
insilmaril@491
  3707
			sel,
insilmaril@491
  3708
			QString("setHideLinkUnselected (%1)").arg(u),
insilmaril@491
  3709
			sel, 
insilmaril@491
  3710
			QString("setHideLinkUnselected (%1)").arg(r),
insilmaril@491
  3711
			QString("Hide link of %1 if unselected").arg(getName(sel))
insilmaril@491
  3712
		);	
insilmaril@424
  3713
		sel->setHideLinkUnselected(b);
insilmaril@491
  3714
	}
insilmaril@160
  3715
}
insilmaril@160
  3716
insilmaril@491
  3717
void MapEditor::importDirInt(BranchObj *dst, QDir d)
insilmaril@0
  3718
{
insilmaril@424
  3719
	BranchObj *bo=xelection.getBranch();
insilmaril@424
  3720
	if (bo)
insilmaril@0
  3721
	{
insilmaril@0
  3722
		// Traverse directories
insilmaril@0
  3723
		d.setFilter( QDir::Dirs| QDir::Hidden | QDir::NoSymLinks );
insilmaril@366
  3724
		QFileInfoList list = d.entryInfoList();
insilmaril@366
  3725
		QFileInfo fi;
insilmaril@366
  3726
insilmaril@366
  3727
		for (int i = 0; i < list.size(); ++i) 
insilmaril@0
  3728
		{
insilmaril@366
  3729
			fi=list.at(i);
insilmaril@366
  3730
			if (fi.fileName() != "." && fi.fileName() != ".." )
insilmaril@0
  3731
			{
insilmaril@0
  3732
				dst->addBranch();
insilmaril@0
  3733
				bo=dst->getLastBranch();
insilmaril@366
  3734
				bo->setHeading (fi.fileName() );
insilmaril@243
  3735
				bo->setColor (QColor("blue"));
insilmaril@0
  3736
				bo->toggleScroll();
insilmaril@366
  3737
				if ( !d.cd(fi.fileName()) ) 
insilmaril@366
  3738
					QMessageBox::critical (0,tr("Critical Import Error"),tr("Cannot find the directory %1").arg(fi.fileName()));
insilmaril@0
  3739
				else 
insilmaril@0
  3740
				{
insilmaril@103
  3741
					// Recursively add subdirs
insilmaril@366
  3742
					importDirInt (bo,d);
insilmaril@0
  3743
					d.cdUp();
insilmaril@0
  3744
				}
insilmaril@0
  3745
			}	
insilmaril@0
  3746
		}		
insilmaril@0
  3747
		// Traverse files
insilmaril@0
  3748
		d.setFilter( QDir::Files| QDir::Hidden | QDir::NoSymLinks );
insilmaril@366
  3749
		list = d.entryInfoList();
insilmaril@366
  3750
insilmaril@366
  3751
		for (int i = 0; i < list.size(); ++i) 
insilmaril@0
  3752
		{
insilmaril@366
  3753
			fi=list.at(i);
insilmaril@0
  3754
			dst->addBranch();
insilmaril@0
  3755
			bo=dst->getLastBranch();
insilmaril@366
  3756
			bo->setHeading (fi.fileName() );
insilmaril@243
  3757
			bo->setColor (QColor("black"));
insilmaril@366
  3758
			if (fi.fileName().right(4) == ".vym" )
insilmaril@366
  3759
				bo->setVymLink (fi.filePath());
insilmaril@0
  3760
		}	
insilmaril@0
  3761
	}		
insilmaril@0
  3762
}
insilmaril@0
  3763
insilmaril@491
  3764
void MapEditor::importDirInt (const QString &s)
insilmaril@491
  3765
{
insilmaril@491
  3766
	BranchObj *bo=xelection.getBranch();
insilmaril@491
  3767
	if (bo)
insilmaril@491
  3768
	{
insilmaril@491
  3769
		saveStateChangingPart (bo,bo,QString ("importDir (\"%1\")").arg(s),QString("Import directory structure from %1").arg(s));
insilmaril@491
  3770
insilmaril@491
  3771
		QDir d(s);
insilmaril@491
  3772
		importDirInt (bo,d);
insilmaril@491
  3773
	}
insilmaril@491
  3774
}	
insilmaril@491
  3775
insilmaril@0
  3776
void MapEditor::importDir()
insilmaril@0
  3777
{
insilmaril@424
  3778
	BranchObj *bo=xelection.getBranch();
insilmaril@424
  3779
	if (bo)
insilmaril@0
  3780
	{
insilmaril@404
  3781
		QStringList filters;
insilmaril@404
  3782
		filters <<"VYM map (*.vym)";
insilmaril@404
  3783
		QFileDialog *fd=new QFileDialog( this,vymName+ " - " +tr("Choose directory structure to import"));
insilmaril@404
  3784
		fd->setMode (QFileDialog::DirectoryOnly);
insilmaril@404
  3785
		fd->setFilters (filters);
insilmaril@404
  3786
		fd->setCaption(vymName+" - " +tr("Choose directory structure to import"));
insilmaril@0
  3787
		fd->show();
insilmaril@0
  3788
insilmaril@0
  3789
		QString fn;
insilmaril@0
  3790
		if ( fd->exec() == QDialog::Accepted )
insilmaril@0
  3791
		{
insilmaril@491
  3792
			importDirInt (fd->selectedFile() );
insilmaril@0
  3793
			mapCenter->reposition();
insilmaril@408
  3794
			scene()->update();
insilmaril@0
  3795
		}
insilmaril@0
  3796
	}	
insilmaril@0
  3797
}
insilmaril@0
  3798
insilmaril@95
  3799
void MapEditor::followXLink(int i)
insilmaril@89
  3800
{
insilmaril@424
  3801
	BranchObj *bo=xelection.getBranch();
insilmaril@424
  3802
	if (bo)
insilmaril@89
  3803
	{
insilmaril@424
  3804
		bo=bo->XLinkTargetAt(i);
insilmaril@97
  3805
		if (bo) 
insilmaril@97
  3806
		{
insilmaril@424
  3807
			xelection.select(bo);
insilmaril@97
  3808
			ensureSelectionVisible();
insilmaril@97
  3809
		}
insilmaril@89
  3810
	}
insilmaril@89
  3811
}
insilmaril@89
  3812
insilmaril@421
  3813
void MapEditor::editXLink(int i)	// FIXME missing saveState
insilmaril@94
  3814
{
insilmaril@424
  3815
	BranchObj *bo=xelection.getBranch();
insilmaril@424
  3816
	if (bo)
insilmaril@94
  3817
	{
insilmaril@424
  3818
		XLinkObj *xlo=bo->XLinkAt(i);
insilmaril@97
  3819
		if (xlo) 
insilmaril@94
  3820
		{
insilmaril@97
  3821
			EditXLinkDialog dia;
insilmaril@97
  3822
			dia.setXLink (xlo);
insilmaril@424
  3823
			dia.setSelection(bo);
insilmaril@97
  3824
			if (dia.exec() == QDialog::Accepted)
insilmaril@97
  3825
			{
insilmaril@97
  3826
				if (dia.useSettingsGlobal() )
insilmaril@97
  3827
				{
insilmaril@398
  3828
					setMapDefXLinkColor (xlo->getColor() );
insilmaril@398
  3829
					setMapDefXLinkWidth (xlo->getWidth() );
insilmaril@97
  3830
				}
insilmaril@97
  3831
				if (dia.deleteXLink())
insilmaril@424
  3832
					bo->deleteXLinkAt(i);
insilmaril@97
  3833
			}
insilmaril@97
  3834
		}	
insilmaril@97
  3835
	}
insilmaril@94
  3836
}
insilmaril@94
  3837
insilmaril@0
  3838
void MapEditor::testFunction()
insilmaril@0
  3839
{
insilmaril@502
  3840
	mainWindow->statusMessage("Biiiiiiiiiiiiiiing!");
insilmaril@502
  3841
	return;
insilmaril@421
  3842
insilmaril@473
  3843
	BranchObj *bo=xelection.getBranch();
insilmaril@473
  3844
	if (bo) animObjList.append( bo );
insilmaril@432
  3845
	
insilmaril@432
  3846
/*
insilmaril@314
  3847
	WarningDialog dia;
insilmaril@388
  3848
	dia.showCancelButton (true);
insilmaril@314
  3849
	dia.setText("This is a longer \nWarning");
insilmaril@314
  3850
	dia.setCaption("Warning: Flux problem");
insilmaril@432
  3851
	dia.setShowAgainName("mapeditor/testDialog");
insilmaril@314
  3852
	if (dia.exec()==QDialog::Accepted)
insilmaril@314
  3853
		cout << "accepted!\n";
insilmaril@314
  3854
	else	
insilmaril@314
  3855
		cout << "canceled!\n";
insilmaril@301
  3856
	return;
insilmaril@432
  3857
*/
insilmaril@366
  3858
insilmaril@420
  3859
/* TODO Hide hidden stuff temporary, maybe add this as regular function somewhere
insilmaril@260
  3860
	if (hidemode==HideNone)
insilmaril@185
  3861
	{
insilmaril@260
  3862
		setHideTmpMode (HideExport);
insilmaril@260
  3863
		mapCenter->calcBBoxSizeWithChilds();
insilmaril@408
  3864
		QRectF totalBBox=mapCenter->getTotalBBox();
insilmaril@408
  3865
		QRectF mapRect=totalBBox;
insilmaril@260
  3866
		QCanvasRectangle *frame=NULL;
insilmaril@260
  3867
insilmaril@260
  3868
		cout << "  map has =("<<totalBBox.x()<<","<<totalBBox.y()<<","<<totalBBox.width()<<","<<totalBBox.height()<<")\n";
insilmaril@260
  3869
	
insilmaril@260
  3870
		mapRect.setRect (totalBBox.x(), totalBBox.y(), 
insilmaril@260
  3871
			totalBBox.width(), totalBBox.height());
insilmaril@408
  3872
		frame=new QCanvasRectangle (mapRect,mapScene);
insilmaril@260
  3873
		frame->setBrush (QColor(white));
insilmaril@260
  3874
		frame->setPen (QColor(black));
insilmaril@408
  3875
		frame->setZValue(0);
insilmaril@260
  3876
		frame->show();    
insilmaril@260
  3877
	}	
insilmaril@260
  3878
	else	
insilmaril@260
  3879
	{
insilmaril@260
  3880
		setHideTmpMode (HideNone);
insilmaril@260
  3881
	}	
insilmaril@260
  3882
	cout <<"  hidemode="<<hidemode<<endl;
insilmaril@301
  3883
	*/
insilmaril@105
  3884
}
insilmaril@105
  3885
insilmaril@408
  3886
void MapEditor::contextMenuEvent ( QContextMenuEvent * e )
insilmaril@0
  3887
{
insilmaril@0
  3888
	// Lineedits are already closed by preceding
insilmaril@0
  3889
	// mouseEvent, we don't need to close here.
insilmaril@0
  3890
insilmaril@408
  3891
    QPointF p = mapToScene(e->pos());
insilmaril@0
  3892
    LinkableMapObj* lmo=mapCenter->findMapObj(p, NULL);
insilmaril@0
  3893
	
insilmaril@0
  3894
    if (lmo) 
insilmaril@0
  3895
	{	// MapObj was found
insilmaril@424
  3896
		if (xelection.single() != lmo)
insilmaril@0
  3897
		{
insilmaril@0
  3898
			// select the MapObj
insilmaril@424
  3899
			xelection.select(lmo);
insilmaril@0
  3900
		}
insilmaril@0
  3901
		// Context Menu 
insilmaril@424
  3902
		if (xelection.getBranch() ) 
insilmaril@0
  3903
		{
insilmaril@424
  3904
			// Context Menu on branch or mapcenter
insilmaril@424
  3905
			updateActions();
insilmaril@424
  3906
			branchContextMenu->popup(e->globalPos() );
insilmaril@424
  3907
		} else
insilmaril@424
  3908
		{
insilmaril@424
  3909
			if (xelection.getFloatImage() )
insilmaril@0
  3910
			{
insilmaril@0
  3911
				// Context Menu on floatimage
insilmaril@0
  3912
				updateActions();
insilmaril@0
  3913
				floatimageContextMenu->popup(e->globalPos() );
insilmaril@0
  3914
			}	
insilmaril@0
  3915
		}	
insilmaril@0
  3916
	} else 
insilmaril@0
  3917
	{ // No MapObj found, we are on the Canvas itself
insilmaril@408
  3918
		// Context Menu on scene
insilmaril@0
  3919
		updateActions();
insilmaril@0
  3920
		canvasContextMenu->popup(e->globalPos() );
insilmaril@0
  3921
    } 
insilmaril@329
  3922
	e->accept();
insilmaril@0
  3923
}
insilmaril@0
  3924
insilmaril@398
  3925
void MapEditor::keyPressEvent(QKeyEvent* e)
insilmaril@398
  3926
{
insilmaril@411
  3927
	if (e->modifiers() & Qt::ControlModifier)
insilmaril@398
  3928
	{
insilmaril@398
  3929
		switch (mainWindow->getModMode())
insilmaril@398
  3930
		{
insilmaril@473
  3931
			case Main::ModModeColor: 
insilmaril@398
  3932
				setCursor (PickColorCursor);
insilmaril@398
  3933
				break;
insilmaril@473
  3934
			case Main::ModModeCopy: 
insilmaril@398
  3935
				setCursor (CopyCursor);
insilmaril@398
  3936
				break;
insilmaril@473
  3937
			case Main::ModModeXLink: 
insilmaril@398
  3938
				setCursor (XLinkCursor);
insilmaril@398
  3939
				break;
insilmaril@398
  3940
			default :
insilmaril@398
  3941
				setCursor (Qt::ArrowCursor);
insilmaril@398
  3942
				break;
insilmaril@398
  3943
		} 
insilmaril@398
  3944
	}	
insilmaril@398
  3945
}
insilmaril@398
  3946
insilmaril@398
  3947
void MapEditor::keyReleaseEvent(QKeyEvent* e)
insilmaril@398
  3948
{
insilmaril@411
  3949
	if (!(e->modifiers() & Qt::ControlModifier))
insilmaril@398
  3950
		setCursor (Qt::ArrowCursor);
insilmaril@398
  3951
}
insilmaril@398
  3952
insilmaril@408
  3953
void MapEditor::mousePressEvent(QMouseEvent* e)
insilmaril@0
  3954
{
insilmaril@329
  3955
	// Ignore right clicks, these will go to context menus
insilmaril@366
  3956
	if (e->button() == Qt::RightButton )
insilmaril@329
  3957
	{
insilmaril@329
  3958
		e->ignore();
insilmaril@329
  3959
		return;
insilmaril@329
  3960
	}
insilmaril@329
  3961
insilmaril@491
  3962
	//Ignore clicks while editing heading
insilmaril@491
  3963
	if (isSelectBlocked() ) 
insilmaril@491
  3964
	{
insilmaril@491
  3965
		e->ignore();
insilmaril@491
  3966
		return;
insilmaril@491
  3967
	}
insilmaril@491
  3968
insilmaril@408
  3969
    QPointF p = mapToScene(e->pos());
insilmaril@0
  3970
    LinkableMapObj* lmo=mapCenter->findMapObj(p, NULL);
insilmaril@0
  3971
	
insilmaril@329
  3972
	e->accept();
insilmaril@329
  3973
insilmaril@442
  3974
	//Take care of  system flags _or_ modifier modes
insilmaril@267
  3975
	//
insilmaril@267
  3976
	if (lmo && (typeid(*lmo)==typeid(BranchObj) ||
insilmaril@267
  3977
		typeid(*lmo)==typeid(MapCenterObj) ))
insilmaril@267
  3978
	{
insilmaril@267
  3979
		QString foname=((BranchObj*)lmo)->getSystemFlagName(p);
insilmaril@267
  3980
		if (!foname.isEmpty())
insilmaril@267
  3981
		{
insilmaril@267
  3982
			// systemFlag clicked
insilmaril@366
  3983
			selectInt (lmo);
insilmaril@267
  3984
			if (foname=="url") 
insilmaril@267
  3985
			{
insilmaril@366
  3986
				if (e->state() & Qt::ControlModifier)
insilmaril@267
  3987
					mainWindow->editOpenURLTab();
insilmaril@267
  3988
				else	
insilmaril@267
  3989
					mainWindow->editOpenURL();
insilmaril@267
  3990
			}	
insilmaril@267
  3991
			else if (foname=="vymLink")
insilmaril@267
  3992
			{
insilmaril@267
  3993
				mainWindow->editOpenVymLink();
insilmaril@267
  3994
				// tabWidget may change, better return now
insilmaril@267
  3995
				// before segfaulting...
insilmaril@267
  3996
			} else if (foname=="note")
insilmaril@267
  3997
				mainWindow->windowToggleNoteEditor();
insilmaril@267
  3998
			else if (foname=="hideInExport")		
insilmaril@267
  3999
				toggleHideExport();
insilmaril@442
  4000
			xelection.update();	
insilmaril@267
  4001
			return;	
insilmaril@267
  4002
		} 
insilmaril@267
  4003
	} 
insilmaril@442
  4004
insilmaril@442
  4005
	// No system flag clicked, take care of modmodes (CTRL-Click)
insilmaril@366
  4006
	if (e->state() & Qt::ControlModifier)
insilmaril@0
  4007
	{
insilmaril@473
  4008
		if (mainWindow->getModMode()==Main::ModModeColor)
insilmaril@83
  4009
		{
insilmaril@83
  4010
				pickingColor=true;
insilmaril@398
  4011
				setCursor (PickColorCursor);
insilmaril@83
  4012
				return;
insilmaril@83
  4013
		} 
insilmaril@473
  4014
		if (mainWindow->getModMode()==Main::ModModeXLink)
insilmaril@83
  4015
		{	
insilmaril@90
  4016
			BranchObj *bo_begin=NULL;
insilmaril@90
  4017
			if (lmo)
insilmaril@90
  4018
				bo_begin=(BranchObj*)(lmo);
insilmaril@90
  4019
			else	
insilmaril@424
  4020
				if (xelection.getBranch() ) 
insilmaril@424
  4021
					bo_begin=xelection.getBranch();
insilmaril@90
  4022
			if (bo_begin)	
insilmaril@83
  4023
			{
insilmaril@83
  4024
				drawingLink=true;
insilmaril@90
  4025
				linkingObj_src=bo_begin;
insilmaril@408
  4026
				tmpXLink=new XLinkObj (mapScene);
insilmaril@95
  4027
				tmpXLink->setBegin (bo_begin);
insilmaril@95
  4028
				tmpXLink->setEnd   (p);
insilmaril@97
  4029
				tmpXLink->setColor(defXLinkColor);
insilmaril@97
  4030
				tmpXLink->setWidth(defXLinkWidth);
insilmaril@95
  4031
				tmpXLink->updateXLink();
insilmaril@95
  4032
				tmpXLink->setVisibility (true);
insilmaril@83
  4033
				return;
insilmaril@89
  4034
			} 
insilmaril@267
  4035
		}
insilmaril@442
  4036
	}	// End of modmodes
insilmaril@442
  4037
insilmaril@0
  4038
    if (lmo) 
insilmaril@267
  4039
	{	
insilmaril@442
  4040
		// Select the clicked object
insilmaril@366
  4041
		selectInt (lmo);
insilmaril@442
  4042
insilmaril@0
  4043
		// Left Button	    Move Branches
insilmaril@366
  4044
		if (e->button() == Qt::LeftButton )
insilmaril@0
  4045
		{
insilmaril@424
  4046
			//movingObj_start.setX( p.x() - selection->x() );// TODO replaced selection->lmo here	
insilmaril@424
  4047
			//movingObj_start.setY( p.y() - selection->y() );	
insilmaril@424
  4048
			movingObj_start.setX( p.x() - lmo->x() );	
insilmaril@424
  4049
			movingObj_start.setY( p.y() - lmo->y() );	
insilmaril@129
  4050
			movingObj_orgPos.setX (lmo->x() );
insilmaril@129
  4051
			movingObj_orgPos.setY (lmo->y() );
insilmaril@366
  4052
			movingObj_orgRelPos=lmo->getRelPos();
insilmaril@129
  4053
insilmaril@93
  4054
			// If modMode==copy, then we want to "move" the _new_ object around
insilmaril@93
  4055
			// then we need the offset from p to the _old_ selection, because of tmp
insilmaril@473
  4056
			if (mainWindow->getModMode()==Main::ModModeCopy &&
insilmaril@366
  4057
				e->state() & Qt::ControlModifier)
insilmaril@93
  4058
			{
insilmaril@491
  4059
				if (xelection.type()==Selection::Branch)
insilmaril@93
  4060
				{
insilmaril@93
  4061
					copyingObj=true;
insilmaril@424
  4062
					mapCenter->addBranch ((BranchObj*)xelection.single());
insilmaril@93
  4063
					unselect();
insilmaril@424
  4064
					xelection.select(mapCenter->getLastBranch());
insilmaril@104
  4065
					mapCenter->reposition();
insilmaril@93
  4066
				}
insilmaril@421
  4067
			} 
insilmaril@421
  4068
insilmaril@424
  4069
			movingObj=xelection.single();	
insilmaril@0
  4070
		} else
insilmaril@0
  4071
			// Middle Button    Toggle Scroll
insilmaril@0
  4072
			// (On Mac OS X this won't work, but we still have 
insilmaril@0
  4073
			// a button in the toolbar)
insilmaril@366
  4074
			if (e->button() == Qt::MidButton )
insilmaril@0
  4075
				toggleScroll();
insilmaril@0
  4076
		updateActions();
insilmaril@442
  4077
		xelection.update();
insilmaril@0
  4078
	} else 
insilmaril@408
  4079
	{ // No MapObj found, we are on the scene itself
insilmaril@408
  4080
		// Left Button	    move Pos of sceneView
insilmaril@366
  4081
		if (e->button() == Qt::LeftButton )
insilmaril@0
  4082
		{
insilmaril@0
  4083
			movingObj=NULL;	// move Content not Obj
insilmaril@0
  4084
			movingObj_start=e->globalPos();
insilmaril@408
  4085
			movingCont_start=QPointF (
insilmaril@408
  4086
				horizontalScrollBar()->value(),
insilmaril@408
  4087
				verticalScrollBar()->value());
insilmaril@408
  4088
			movingVec=QPointF(0,0);
insilmaril@398
  4089
			setCursor(HandOpenCursor);
insilmaril@0
  4090
		} 
insilmaril@0
  4091
    } 
insilmaril@0
  4092
}
insilmaril@0
  4093
insilmaril@408
  4094
void MapEditor::mouseMoveEvent(QMouseEvent* e)
insilmaril@0
  4095
{
insilmaril@408
  4096
    QPointF p = mapToScene(e->pos());
insilmaril@424
  4097
	LinkableMapObj *lmosel=xelection.single();
insilmaril@83
  4098
insilmaril@0
  4099
    // Move the selected MapObj
insilmaril@424
  4100
    if ( lmosel && movingObj) 
insilmaril@0
  4101
    {	
insilmaril@421
  4102
		// reset cursor if we are moving and don't copy
insilmaril@473
  4103
		if (mainWindow->getModMode()!=Main::ModModeCopy)
insilmaril@421
  4104
			setCursor (Qt::ArrowCursor);
insilmaril@421
  4105
insilmaril@408
  4106
		// To avoid jumping of the sceneView, only 
insilmaril@164
  4107
		// ensureSelectionVisible, if not tmp linked
insilmaril@424
  4108
		if (!lmosel->hasParObjTmp())
insilmaril@164
  4109
			ensureSelectionVisible ();
insilmaril@0
  4110
		
insilmaril@83
  4111
		// Now move the selection, but add relative position 
insilmaril@83
  4112
		// (movingObj_start) where selection was chosen with 
insilmaril@83
  4113
		// mousepointer. (This avoids flickering resp. jumping 
insilmaril@0
  4114
		// of selection back to absPos)
insilmaril@0
  4115
		
insilmaril@0
  4116
		// Check if we could link 
insilmaril@0
  4117
		LinkableMapObj* lmo=mapCenter->findMapObj(p, lmosel);
insilmaril@0
  4118
		
insilmaril@0
  4119
insilmaril@424
  4120
		FloatObj *fio=xelection.getFloatImage();
insilmaril@424
  4121
		if (fio)
insilmaril@0
  4122
		{
insilmaril@424
  4123
			fio->move   (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
insilmaril@424
  4124
			fio->setRelPos();
insilmaril@424
  4125
			fio->updateLink(); //no need for reposition, if we update link here
insilmaril@442
  4126
			xelection.update();
insilmaril@0
  4127
insilmaril@0
  4128
			// Relink float to new mapcenter or branch, if shift is pressed	
insilmaril@0
  4129
			// Only relink, if selection really has a new parent
insilmaril@388
  4130
			if ( (e->modifiers()==Qt::ShiftModifier) && lmo &&
insilmaril@0
  4131
				( (typeid(*lmo)==typeid(BranchObj)) ||
insilmaril@0
  4132
				  (typeid(*lmo)==typeid(MapCenterObj)) ) &&
insilmaril@424
  4133
				( lmo != fio->getParObj())  
insilmaril@0
  4134
				)
insilmaril@0
  4135
			{
insilmaril@424
  4136
				if (typeid(*fio) == typeid(FloatImageObj) && 
insilmaril@420
  4137
				( (typeid(*lmo)==typeid(BranchObj) ||
insilmaril@420
  4138
				  typeid(*lmo)==typeid(MapCenterObj)) ))  
insilmaril@0
  4139
				{
insilmaril@420
  4140
insilmaril@420
  4141
					// Also save the move which was done so far
insilmaril@420
  4142
					QString pold=qpointfToString(movingObj_orgRelPos);
insilmaril@420
  4143
					QString pnow=qpointfToString(fio->getRelPos());
insilmaril@420
  4144
					saveState(
insilmaril@424
  4145
						fio,
insilmaril@420
  4146
						"moveRel "+pold,
insilmaril@424
  4147
						fio,
insilmaril@420
  4148
						"moveRel "+pnow,
insilmaril@424
  4149
						QString("Move %1 to relativ position %2").arg(getName(fio)).arg(pnow));
insilmaril@420
  4150
					fio->getParObj()->requestReposition();
insilmaril@420
  4151
					mapCenter->reposition();
insilmaril@420
  4152
insilmaril@420
  4153
					linkTo (lmo->getSelectString());
insilmaril@442
  4154
					//movingObj=lmosel;
insilmaril@442
  4155
					//movingObj_orgRelPos=lmosel->getRelPos();	
insilmaril@420
  4156
insilmaril@420
  4157
					mapCenter->reposition();
insilmaril@0
  4158
				}	
insilmaril@0
  4159
			}
insilmaril@388
  4160
		} else	
insilmaril@388
  4161
		{	// selection != a FloatObj
insilmaril@0
  4162
			if (lmosel->getDepth()==0)
insilmaril@0
  4163
			{
insilmaril@388
  4164
				// Move MapCenter
insilmaril@388
  4165
				if (e->buttons()== Qt::LeftButton && e->modifiers()==Qt::ShiftModifier) 
insilmaril@0
  4166
					mapCenter->moveAll(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
insilmaril@0
  4167
				else	
insilmaril@0
  4168
					mapCenter->move   (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
insilmaril@388
  4169
				mapCenter->updateRelPositions();	
insilmaril@0
  4170
			} else
insilmaril@0
  4171
			{	
insilmaril@0
  4172
				if (lmosel->getDepth()==1)
insilmaril@0
  4173
				{
insilmaril@388
  4174
					// Move mainbranch
insilmaril@0
  4175
					lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
insilmaril@388
  4176
					lmosel->setRelPos();
insilmaril@0
  4177
				} else
insilmaril@0
  4178
				{
insilmaril@388
  4179
					// Move ordinary branch
insilmaril@473
  4180
					if (lmosel->getOrientation() == LinkableMapObj::LeftOfCenter)
insilmaril@0
  4181
						// Add width of bbox here, otherwise alignRelTo will cause jumping around
insilmaril@0
  4182
						lmosel->move(p.x() -movingObj_start.x()+lmosel->getBBox().width(), 
insilmaril@175
  4183
							p.y()-movingObj_start.y() +lmosel->getTopPad() );		
insilmaril@0
  4184
					else	
insilmaril@175
  4185
						lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() -lmosel->getTopPad());
insilmaril@0
  4186
				} 
insilmaril@0
  4187
insilmaril@431
  4188
				// Maybe we can relink temporary?
insilmaril@431
  4189
				if (lmo && (lmo!=lmosel) && xelection.getBranch() && 
insilmaril@431
  4190
					 (typeid(*lmo)==typeid(BranchObj) ||
insilmaril@431
  4191
					  typeid(*lmo)==typeid(MapCenterObj)) ) 
insilmaril@431
  4192
insilmaril@0
  4193
				{
insilmaril@388
  4194
					if (e->modifiers()==Qt::ControlModifier)
insilmaril@0
  4195
					{
insilmaril@0
  4196
						// Special case: CTRL to link below lmo
insilmaril@0
  4197
						lmosel->setParObjTmp (lmo,p,+1);
insilmaril@0
  4198
					}
insilmaril@388
  4199
					else if (e->modifiers()==Qt::ShiftModifier)
insilmaril@0
  4200
						lmosel->setParObjTmp (lmo,p,-1);
insilmaril@0
  4201
					else
insilmaril@0
  4202
						lmosel->setParObjTmp (lmo,p,0);
insilmaril@0
  4203
				} else	
insilmaril@0
  4204
				{
insilmaril@93
  4205
					lmosel->unsetParObjTmp();
insilmaril@0
  4206
				}		
insilmaril@428
  4207
				// reposition subbranch
insilmaril@428
  4208
				lmosel->reposition();	
insilmaril@0
  4209
			} // depth>0
insilmaril@0
  4210
insilmaril@442
  4211
			xelection.update();
insilmaril@0
  4212
		} // no FloatImageObj
insilmaril@0
  4213
insilmaril@408
  4214
		scene()->update();
insilmaril@0
  4215
		return;
insilmaril@0
  4216
	} // selection && moving_obj
insilmaril@0
  4217
		
insilmaril@83
  4218
	// Draw a link from one branch to another
insilmaril@83
  4219
	if (drawingLink)
insilmaril@83
  4220
	{
insilmaril@95
  4221
		 tmpXLink->setEnd (p);
insilmaril@95
  4222
		 tmpXLink->updateXLink();
insilmaril@83
  4223
	}	 
insilmaril@83
  4224
	
insilmaril@408
  4225
    // Move sceneView 
insilmaril@411
  4226
    if (!movingObj && !pickingColor &&!drawingLink && e->buttons() == Qt::LeftButton ) 
insilmaril@0
  4227
	{
insilmaril@408
  4228
		QPointF p=e->globalPos();
insilmaril@0
  4229
		movingVec.setX(-p.x() + movingObj_start.x() );
insilmaril@0
  4230
		movingVec.setY(-p.y() + movingObj_start.y() );
insilmaril@408
  4231
		horizontalScrollBar()->setSliderPosition((int)( movingCont_start.x()+movingVec.x() ));
insilmaril@408
  4232
		verticalScrollBar()->setSliderPosition((int)( movingCont_start.y()+movingVec.y() ) );
insilmaril@0
  4233
    }
insilmaril@0
  4234
}
insilmaril@0
  4235
insilmaril@0
  4236
insilmaril@408
  4237
void MapEditor::mouseReleaseEvent(QMouseEvent* e)
insilmaril@0
  4238
{
insilmaril@408
  4239
    QPointF p = mapToScene(e->pos());
insilmaril@0
  4240
	LinkableMapObj *dst;
insilmaril@424
  4241
	LinkableMapObj *lmosel=xelection.single();
insilmaril@0
  4242
	// Have we been picking color?
insilmaril@0
  4243
	if (pickingColor)
insilmaril@0
  4244
	{
insilmaril@0
  4245
		pickingColor=false;
insilmaril@366
  4246
		setCursor (Qt::ArrowCursor);
insilmaril@0
  4247
		// Check if we are over another branch
insilmaril@408
  4248
		dst=mapCenter->findMapObj(p, NULL);
insilmaril@424
  4249
		if (dst && lmosel) 
insilmaril@0
  4250
		{	
insilmaril@366
  4251
			if (e->state() & Qt::ShiftModifier)
insilmaril@417
  4252
				colorBranch (((BranchObj*)(dst))->getColor());
insilmaril@0
  4253
			else	
insilmaril@417
  4254
				colorSubtree (((BranchObj*)(dst))->getColor());
insilmaril@0
  4255
		} 
insilmaril@0
  4256
		return;
insilmaril@0
  4257
	}
insilmaril@83
  4258
insilmaril@83
  4259
	// Have we been drawing a link?
insilmaril@491
  4260
	if (drawingLink)	
insilmaril@83
  4261
	{
insilmaril@83
  4262
		drawingLink=false;
insilmaril@83
  4263
		// Check if we are over another branch
insilmaril@408
  4264
		dst=mapCenter->findMapObj(p, NULL);
insilmaril@424
  4265
		if (dst && lmosel) 
insilmaril@83
  4266
		{	
insilmaril@95
  4267
			tmpXLink->setEnd ( ((BranchObj*)(dst)) );
insilmaril@95
  4268
			tmpXLink->updateXLink();
insilmaril@491
  4269
			tmpXLink->activate(); //FIXME savestate missing
insilmaril@473
  4270
			//saveStateComplete(QString("Activate xLink from %1 to %2").arg(getName(tmpXLink->getBegin())).arg(getName(tmpXLink->getEnd())) );	
insilmaril@83
  4271
		} else
insilmaril@83
  4272
		{
insilmaril@95
  4273
			delete(tmpXLink);
insilmaril@95
  4274
			tmpXLink=NULL;
insilmaril@83
  4275
		}
insilmaril@83
  4276
		return;
insilmaril@83
  4277
	}
insilmaril@83
  4278
	
insilmaril@0
  4279
    // Have we been moving something?
insilmaril@424
  4280
    if ( lmosel && movingObj ) 
insilmaril@0
  4281
    {	
insilmaril@424
  4282
		FloatImageObj *fo=xelection.getFloatImage();
insilmaril@424
  4283
		if(fo)
insilmaril@175
  4284
		{
insilmaril@388
  4285
			// Moved FloatObj. Maybe we need to reposition
insilmaril@408
  4286
		    QString pold=qpointfToString(movingObj_orgRelPos);
insilmaril@408
  4287
		    QString pnow=qpointfToString(fo->getRelPos());
insilmaril@395
  4288
			saveState(
insilmaril@424
  4289
				fo,
insilmaril@366
  4290
				"moveRel "+pold,
insilmaril@424
  4291
				fo,
insilmaril@366
  4292
				"moveRel "+pnow,
insilmaril@424
  4293
				QString("Move %1 to relativ position %2").arg(getName(fo)).arg(pnow));
insilmaril@424
  4294
insilmaril@424
  4295
			fo->getParObj()->requestReposition();
insilmaril@175
  4296
			mapCenter->reposition();
insilmaril@175
  4297
		}	
insilmaril@175
  4298
insilmaril@0
  4299
		// Check if we are over another branch, but ignore 
insilmaril@0
  4300
		// any found LMOs, which are FloatObjs
insilmaril@424
  4301
		dst=mapCenter->findMapObj(mapToScene(e->pos() ), lmosel);
insilmaril@0
  4302
insilmaril@336
  4303
		if (dst && (typeid(*dst)!=typeid(BranchObj) && typeid(*dst)!=typeid(MapCenterObj))) 
insilmaril@0
  4304
			dst=NULL;
insilmaril@0
  4305
		
insilmaril@491
  4306
		if (xelection.type() == Selection::MapCenter )
insilmaril@497
  4307
		{	
insilmaril@497
  4308
			// TODO: Check for problems if graphicsview is resized for 
insilmaril@497
  4309
			// undo/redo...
insilmaril@497
  4310
		    QString pold=qpointfToString(movingObj_orgPos);
insilmaril@497
  4311
		    QString pnow=qpointfToString(mapCenter->getAbsPos());
insilmaril@497
  4312
			saveState(
insilmaril@497
  4313
				fo,
insilmaril@497
  4314
				"move "+pold,
insilmaril@497
  4315
				fo,
insilmaril@497
  4316
				"move "+pnow,
insilmaril@497
  4317
				QString("Move mapcenter %1 to position %2").arg(getName(mapCenter)).arg(pnow));
insilmaril@388
  4318
		}
insilmaril@388
  4319
		
insilmaril@491
  4320
		if (xelection.type() == Selection::Branch )
insilmaril@388
  4321
		{	// A branch was moved
insilmaril@388
  4322
			
insilmaril@0
  4323
			// save the position in case we link to mapcenter
insilmaril@424
  4324
			QPointF savePos=QPointF (lmosel->getAbsPos()  );
insilmaril@0
  4325
insilmaril@0
  4326
			// Reset the temporary drawn link to the original one
insilmaril@424
  4327
			lmosel->unsetParObjTmp();
insilmaril@0
  4328
insilmaril@366
  4329
			// For Redo we may need to save original selection
insilmaril@424
  4330
			QString preSelStr=lmosel->getSelectString();
insilmaril@93
  4331
insilmaril@94
  4332
			copyingObj=false;	
insilmaril@129
  4333
			if (dst ) 
insilmaril@93
  4334
			{
insilmaril@424
  4335
				BranchObj* bsel=xelection.getBranch();
insilmaril@366
  4336
				BranchObj* bdst=(BranchObj*)dst;
insilmaril@366
  4337
insilmaril@366
  4338
				QString preParStr=(bsel->getParObj())->getSelectString();
insilmaril@366
  4339
				QString preNum=QString::number (bsel->getNum(),10);
insilmaril@366
  4340
				QString preDstParStr;
insilmaril@366
  4341
insilmaril@366
  4342
				if (e->state() & Qt::ShiftModifier && dst->getParObj())
insilmaril@366
  4343
				{	// Link above dst
insilmaril@366
  4344
					preDstParStr=dst->getParObj()->getSelectString();
insilmaril@421
  4345
					bsel->linkTo ( (BranchObj*)(bdst->getParObj()), bdst->getNum());
insilmaril@366
  4346
				} else 
insilmaril@366
  4347
				if (e->state() & Qt::ControlModifier && dst->getParObj())
insilmaril@366
  4348
				{
insilmaril@366
  4349
					// Link below dst
insilmaril@366
  4350
					preDstParStr=dst->getParObj()->getSelectString();
insilmaril@421
  4351
					bsel->linkTo ( (BranchObj*)(bdst->getParObj()), bdst->getNum()+1);
insilmaril@366
  4352
				} else	
insilmaril@366
  4353
				{	// Append to dst
insilmaril@366
  4354
					preDstParStr=dst->getSelectString();
insilmaril@421
  4355
					bsel->linkTo (bdst,-1);
insilmaril@366
  4356
					if (dst->getDepth()==0) bsel->move (savePos);
insilmaril@366
  4357
				} 
insilmaril@424
  4358
				QString postSelStr=lmosel->getSelectString();
insilmaril@366
  4359
				QString postNum=QString::number (bsel->getNum(),10);
insilmaril@366
  4360
insilmaril@421
  4361
				QString undoCom="linkTo (\""+ 
insilmaril@366
  4362
					preParStr+ "\"," + preNum  +"," + 
insilmaril@366
  4363
					QString ("%1,%2").arg(movingObj_orgPos.x()).arg(movingObj_orgPos.y())+ ")";
insilmaril@366
  4364
insilmaril@421
  4365
				QString redoCom="linkTo (\""+ 
insilmaril@366
  4366
					preDstParStr + "\"," + postNum + "," +
insilmaril@366
  4367
					QString ("%1,%2").arg(savePos.x()).arg(savePos.y())+ ")";
insilmaril@366
  4368
insilmaril@366
  4369
				saveState (
insilmaril@366
  4370
					postSelStr,undoCom,
insilmaril@366
  4371
					preSelStr, redoCom,
insilmaril@366
  4372
					QString("Relink %1 to %2").arg(getName(bsel)).arg(getName(dst)) );
insilmaril@163
  4373
			} else
insilmaril@424
  4374
				if (lmosel->getDepth()==1)
insilmaril@366
  4375
				{
insilmaril@366
  4376
					// The select string might be different _after_ moving around.
insilmaril@366
  4377
					// Therefor reposition and then use string of old selection, too
insilmaril@366
  4378
					mapCenter->reposition();
insilmaril@366
  4379
insilmaril@424
  4380
					QString ps=qpointfToString ( lmosel->getRelPos() );
insilmaril@366
  4381
					saveState(
insilmaril@424
  4382
						lmosel->getSelectString(), "moveRel "+qpointfToString(movingObj_orgRelPos), 
insilmaril@366
  4383
						preSelStr, "moveRel "+ps, 
insilmaril@424
  4384
						QString("Move %1 to relative position %2").arg(getName(lmosel)).arg(ps));
insilmaril@163
  4385
			
insilmaril@366
  4386
				}
insilmaril@0
  4387
			// Draw the original link, before selection was moved around
insilmaril@0
  4388
			mapCenter->reposition();
insilmaril@0
  4389
		}
insilmaril@442
  4390
		xelection.update();
insilmaril@408
  4391
		// Finally resize scene, if needed
insilmaril@408
  4392
		scene()->update();
insilmaril@0
  4393
		movingObj=NULL;		
insilmaril@366
  4394
insilmaril@366
  4395
		// Just make sure, that actions are still ok,e.g. the move branch up/down buttons...
insilmaril@366
  4396
		updateActions();
insilmaril@0
  4397
	} else 
insilmaril@83
  4398
		// maybe we moved View: set old cursor
insilmaril@366
  4399
		setCursor (Qt::ArrowCursor);
insilmaril@83
  4400
    
insilmaril@0
  4401
}
insilmaril@0
  4402
insilmaril@408
  4403
void MapEditor::mouseDoubleClickEvent(QMouseEvent* e)
insilmaril@0
  4404
{
insilmaril@491
  4405
	if (isSelectBlocked() ) 
insilmaril@491
  4406
	{
insilmaril@491
  4407
		e->ignore();
insilmaril@491
  4408
		return;
insilmaril@491
  4409
	}
insilmaril@491
  4410
insilmaril@366
  4411
	if (e->button() == Qt::LeftButton )
insilmaril@0
  4412
	{
insilmaril@408
  4413
		QPointF p = mapToScene(e->pos());
insilmaril@0
  4414
		LinkableMapObj *lmo=mapCenter->findMapObj(p, NULL);
insilmaril@0
  4415
		if (lmo) {	// MapObj was found
insilmaril@0
  4416
			// First select the MapObj than edit heading
insilmaril@424
  4417
			xelection.select(lmo);
insilmaril@366
  4418
			mainWindow->editHeading();
insilmaril@0
  4419
		}
insilmaril@0
  4420
	}
insilmaril@0
  4421
}
insilmaril@0
  4422
insilmaril@0
  4423
void MapEditor::resizeEvent (QResizeEvent* e)
insilmaril@0
  4424
{
insilmaril@408
  4425
	QGraphicsView::resizeEvent( e );
insilmaril@0
  4426
}
insilmaril@0
  4427
insilmaril@412
  4428
void MapEditor::dragEnterEvent(QDragEnterEvent *event)
insilmaril@119
  4429
{
insilmaril@417
  4430
	//for (unsigned int i=0;event->format(i);i++) // Debug mime type
insilmaril@417
  4431
	//	cerr << event->format(i) << endl;
insilmaril@417
  4432
insilmaril@417
  4433
	if (event->mimeData()->hasImage())
insilmaril@417
  4434
		event->acceptProposedAction();
insilmaril@417
  4435
	else	
insilmaril@417
  4436
		if (event->mimeData()->hasUrls())
insilmaril@417
  4437
			event->acceptProposedAction();
insilmaril@417
  4438
}
insilmaril@417
  4439
insilmaril@417
  4440
void MapEditor::dragMoveEvent(QDragMoveEvent *event)
insilmaril@417
  4441
{
insilmaril@417
  4442
}
insilmaril@417
  4443
insilmaril@417
  4444
void MapEditor::dragLeaveEvent(QDragLeaveEvent *event)
insilmaril@417
  4445
{
insilmaril@417
  4446
	event->accept();
insilmaril@119
  4447
}
insilmaril@129
  4448
insilmaril@412
  4449
void MapEditor::dropEvent(QDropEvent *event)
insilmaril@129
  4450
{
insilmaril@424
  4451
	BranchObj *sel=xelection.getBranch();
insilmaril@424
  4452
	if (sel)
insilmaril@128
  4453
	{
insilmaril@497
  4454
		foreach (QString format,event->mimeData()->formats()) 
insilmaril@497
  4455
		{
insilmaril@497
  4456
			cout << "Dropped format: "<<format.ascii()<<endl;
insilmaril@497
  4457
		}
insilmaril@497
  4458
insilmaril@497
  4459
insilmaril@417
  4460
		QList <QUrl> uris;
insilmaril@417
  4461
		if (event->mimeData()->hasImage()) 
insilmaril@128
  4462
		{
insilmaril@417
  4463
			 QVariant imageData = event->mimeData()->imageData();
insilmaril@417
  4464
			 addFloatImageInt (qvariant_cast<QPixmap>(imageData));
insilmaril@417
  4465
		} else
insilmaril@417
  4466
		if (event->mimeData()->hasUrls())
insilmaril@417
  4467
			uris=event->mimeData()->urls();
insilmaril@417
  4468
insilmaril@417
  4469
		if (uris.count()>0)
insilmaril@128
  4470
		{
insilmaril@417
  4471
			QStringList files;
insilmaril@417
  4472
			QString s;
insilmaril@417
  4473
			QString heading;
insilmaril@417
  4474
			BranchObj *bo;
insilmaril@428
  4475
			for (int i=0; i<uris.count();i++)
insilmaril@408
  4476
			{
insilmaril@428
  4477
				// Workaround to avoid adding empty branches
insilmaril@428
  4478
				if (!uris.at(i).toString().isEmpty())
insilmaril@417
  4479
				{
insilmaril@428
  4480
					bo=sel->addBranch();
insilmaril@428
  4481
					if (bo)
insilmaril@417
  4482
					{
insilmaril@428
  4483
						s=uris.at(i).toLocalFile();
insilmaril@428
  4484
						if (!s.isEmpty()) 
insilmaril@428
  4485
						{
insilmaril@428
  4486
						   QString file = QDir::convertSeparators(s);
insilmaril@428
  4487
						   heading = QFileInfo(file).baseName();
insilmaril@428
  4488
						   files.append(file);
insilmaril@428
  4489
						   if (file.endsWith(".vym", false))
insilmaril@428
  4490
							   bo->setVymLink(file);
insilmaril@428
  4491
						   else
insilmaril@428
  4492
							   bo->setURL(uris.at(i).toString());
insilmaril@428
  4493
					   } else 
insilmaril@428
  4494
					   {
insilmaril@428
  4495
						   bo->setURL(uris.at(i).toString());
insilmaril@428
  4496
					   }
insilmaril@428
  4497
insilmaril@428
  4498
					   if (!heading.isEmpty())
insilmaril@428
  4499
						   bo->setHeading(heading);
insilmaril@428
  4500
					   else
insilmaril@428
  4501
						   bo->setHeading(uris.at(i).toString());
insilmaril@428
  4502
					}
insilmaril@417
  4503
				}
insilmaril@417
  4504
			}
insilmaril@420
  4505
			mapCenter->reposition();
insilmaril@417
  4506
		}
insilmaril@417
  4507
	}	
insilmaril@417
  4508
	event->acceptProposedAction();
insilmaril@417
  4509
}
insilmaril@417
  4510
insilmaril@491
  4511
void MapEditor::timerEvent(QTimerEvent *event)  //TODO animation
insilmaril@473
  4512
{
insilmaril@473
  4513
    Q_UNUSED(event);
insilmaril@473
  4514
insilmaril@473
  4515
	cout << "ME::timerEvent\n";
insilmaril@473
  4516
insilmaril@473
  4517
	for (int i=0; i<animObjList.size(); ++i)
insilmaril@473
  4518
	{
insilmaril@473
  4519
		animObjList.at(i)->animate();
insilmaril@473
  4520
		((BranchObj*)animObjList.at(i))->move2RelPos (((BranchObj*)animObjList.at(i))->getRelPos() );
insilmaril@473
  4521
	}
insilmaril@473
  4522
	mapCenter->reposition();
insilmaril@473
  4523
}
insilmaril@473
  4524
insilmaril@473
  4525
void MapEditor::autosave()
insilmaril@473
  4526
{
insilmaril@500
  4527
	// Disable autosave, while we have gone back in history
insilmaril@500
  4528
	int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
insilmaril@500
  4529
	if (redosAvail>0) return;
insilmaril@500
  4530
insilmaril@500
  4531
insilmaril@491
  4532
	if (mapUnsaved &&mapChanged && settings.value ("/mapeditor/autosave/use",true).toBool() )
insilmaril@473
  4533
		mainWindow->fileSave (this);
insilmaril@473
  4534
}
insilmaril@473
  4535
insilmaril@473
  4536
insilmaril@491
  4537
/*TODO not needed? void MapEditor::contentsDropEvent(QDropEvent *event) 
insilmaril@417
  4538
{
insilmaril@408
  4539
insilmaril@408
  4540
		} else if (event->provides("application/x-moz-file-promise-url") && 
insilmaril@408
  4541
			 event->provides("application/x-moz-nativeimage")) 
insilmaril@408
  4542
		{
insilmaril@129
  4543
			// Contains url to the img src in unicode16
insilmaril@128
  4544
			QByteArray d = event->encodedData("application/x-moz-file-promise-url");
insilmaril@128
  4545
			QString url = QString((const QChar*)d.data(),d.size()/2);
insilmaril@408
  4546
			fetchImage(url);
insilmaril@408
  4547
			event->accept();
insilmaril@128
  4548
			update=true;
insilmaril@408
  4549
		} else if (event->provides ("text/uri-list"))
insilmaril@408
  4550
		{	// Uris provided e.g. by konqueror
insilmaril@408
  4551
			Q3UriDrag::decode (event,uris);
insilmaril@408
  4552
		} else if (event->provides ("_NETSCAPE_URL"))
insilmaril@128
  4553
		{	// Uris provided by Mozilla
insilmaril@129
  4554
		  QStringList l = QStringList::split("\n", event->encodedData("_NETSCAPE_URL"));
insilmaril@129
  4555
		  uris.append(l[0]);
insilmaril@129
  4556
		  heading = l[1];
insilmaril@129
  4557
		} else if (event->provides("text/html")) {
insilmaril@129
  4558
insilmaril@129
  4559
		  // Handels text mime types
insilmaril@129
  4560
		  // Look like firefox allways handle text as unicode16 (2 bytes per char.)
insilmaril@129
  4561
		  QByteArray d = event->encodedData("text/html");
insilmaril@129
  4562
		  QString text;
insilmaril@129
  4563
		  if (isUnicode16(d)) 
insilmaril@129
  4564
		    text = QString((const QChar*)d.data(),d.size()/2);
insilmaril@129
  4565
		  else 
insilmaril@129
  4566
		    text = QString(d);
insilmaril@129
  4567
insilmaril@129
  4568
		  textEditor->setText(text);
insilmaril@129
  4569
insilmaril@129
  4570
		  event->accept();
insilmaril@129
  4571
		  update=true;
insilmaril@129
  4572
		} else if (event->provides("text/plain")) {
insilmaril@129
  4573
		  QByteArray d = event->encodedData("text/plain");
insilmaril@129
  4574
		  QString text;
insilmaril@129
  4575
		  if (isUnicode16(d))
insilmaril@129
  4576
		    text = QString((const QChar*)d.data(),d.size()/2);
insilmaril@129
  4577
		  else 
insilmaril@129
  4578
		    text = QString(d);
insilmaril@129
  4579
insilmaril@129
  4580
		  textEditor->setText(text);
insilmaril@129
  4581
		  
insilmaril@129
  4582
		  event->accept();
insilmaril@129
  4583
		  update= true;
insilmaril@128
  4584
		}
insilmaril@417
  4585
insilmaril@406
  4586
		*/
insilmaril@406
  4587
insilmaril@417
  4588
insilmaril@412
  4589
insilmaril@412
  4590
bool isUnicode16(const QByteArray &d) 
insilmaril@412
  4591
{
insilmaril@412
  4592
  // TODO: make more precise check for unicode 16.
insilmaril@412
  4593
  // Guess unicode16 if any of second bytes are zero
insilmaril@412
  4594
  unsigned int length = max(0,d.size()-2)/2;
insilmaril@412
  4595
  for (unsigned int i = 0; i<length ; i++)
insilmaril@412
  4596
    if (d.at(i*2+1)==0) return true;
insilmaril@412
  4597
  return false;
insilmaril@412
  4598
}
insilmaril@412
  4599
      
insilmaril@395
  4600
void MapEditor::addFloatImageInt (const QPixmap &img) 
insilmaril@119
  4601
{
insilmaril@424
  4602
	BranchObj *bo=xelection.getBranch();
insilmaril@424
  4603
	if (bo)
insilmaril@119
  4604
  {
insilmaril@395
  4605
	FloatImageObj *fio=bo->addFloatImage();
insilmaril@395
  4606
    fio->load(img);
insilmaril@491
  4607
    fio->setOriginalFilename("Image added by Drag and Drop");	// FIXME savestate missing
insilmaril@119
  4608
    mapCenter->reposition();
insilmaril@408
  4609
    scene()->update();
insilmaril@119
  4610
  }
insilmaril@119
  4611
}
insilmaril@119
  4612
insilmaril@412
  4613
/*
insilmaril@412
  4614
insilmaril@412
  4615
void MapEditor::imageDataFetched(const QByteArray &a, Q3NetworkOperation * / *nop* /) 
insilmaril@119
  4616
{
insilmaril@119
  4617
  if (!imageBuffer) imageBuffer = new QBuffer();
insilmaril@119
  4618
  if (!imageBuffer->isOpen()) {
insilmaril@366
  4619
    imageBuffer->open(QIODevice::WriteOnly | QIODevice::Append);
insilmaril@119
  4620
  }
insilmaril@119
  4621
  imageBuffer->at(imageBuffer->at()+imageBuffer->writeBlock(a));
insilmaril@119
  4622
}
insilmaril@119
  4623
insilmaril@119
  4624
insilmaril@366
  4625
void MapEditor::imageDataFinished(Q3NetworkOperation *nop) 
insilmaril@119
  4626
{
insilmaril@366
  4627
	if (nop->state()==Q3NetworkProtocol::StDone) {
insilmaril@129
  4628
		QPixmap img(imageBuffer->buffer());
insilmaril@395
  4629
		addFloatImageInt (img);
insilmaril@129
  4630
	}
insilmaril@129
  4631
insilmaril@129
  4632
	if (imageBuffer) {
insilmaril@129
  4633
		imageBuffer->close();
insilmaril@129
  4634
		if (imageBuffer) {
insilmaril@129
  4635
			imageBuffer->close();
insilmaril@129
  4636
			delete imageBuffer;
insilmaril@129
  4637
			imageBuffer = 0;
insilmaril@129
  4638
		}
insilmaril@129
  4639
	}
insilmaril@119
  4640
}
insilmaril@119
  4641
insilmaril@129
  4642
void MapEditor::fetchImage(const QString &url) 
insilmaril@119
  4643
{
insilmaril@119
  4644
  if (urlOperator) {
insilmaril@119
  4645
    urlOperator->stop();
insilmaril@119
  4646
    disconnect(urlOperator);
insilmaril@119
  4647
    delete urlOperator;
insilmaril@119
  4648
  }
insilmaril@119
  4649
  
insilmaril@366
  4650
  urlOperator = new Q3UrlOperator(url);
insilmaril@366
  4651
  connect(urlOperator, SIGNAL(finished(Q3NetworkOperation *)), 
insilmaril@366
  4652
	  this, SLOT(imageDataFinished(Q3NetworkOperation*)));
insilmaril@366
  4653
insilmaril@366
  4654
  connect(urlOperator, SIGNAL(data(const QByteArray &, Q3NetworkOperation *)),
insilmaril@366
  4655
	  this, SLOT(imageDataFetched(const QByteArray &, Q3NetworkOperation *)));
insilmaril@119
  4656
  urlOperator->get();
insilmaril@119
  4657
}
insilmaril@412
  4658
*/
insilmaril@412
  4659