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