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