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