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