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