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