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