mapeditor.cpp
author insilmaril
Fri, 08 Dec 2006 20:18:56 +0000
changeset 408 c2a05fa925a1
parent 407 5db8dfd30ea2
child 411 910ba9fab728
permissions -rw-r--r--
1.6.1 Port to QGraphics
insilmaril@0
     1
#include "mapeditor.h"
insilmaril@0
     2
insilmaril@408
     3
#include <q3dragobject.h>
insilmaril@408
     4
#include <q3urloperator.h>
insilmaril@408
     5
#include <q3networkprotocol.h>
insilmaril@408
     6
#include <q3paintdevicemetrics.h>
insilmaril@374
     7
#include <q3filedialog.h>
insilmaril@0
     8
insilmaril@0
     9
#include <iostream>
insilmaril@95
    10
#include <cstdlib>
insilmaril@0
    11
#include <typeinfo>
insilmaril@0
    12
insilmaril@0
    13
#include "version.h"
insilmaril@0
    14
insilmaril@105
    15
#include "api.h"
insilmaril@175
    16
#include "editxlinkdialog.h"
insilmaril@175
    17
#include "exports.h"
insilmaril@175
    18
#include "extrainfodialog.h"
insilmaril@236
    19
#include "file.h"
insilmaril@175
    20
#include "linkablemapobj.h"
insilmaril@175
    21
#include "mainwindow.h"
insilmaril@175
    22
#include "misc.h"
insilmaril@175
    23
#include "texteditor.h"
insilmaril@314
    24
#include "warningdialog.h"
insilmaril@0
    25
#include "xml.h"
insilmaril@0
    26
insilmaril@0
    27
insilmaril@0
    28
extern TextEditor *textEditor;
insilmaril@0
    29
extern int statusbarTime;
insilmaril@0
    30
extern Main *mainWindow;
insilmaril@164
    31
extern QString tmpVymDir;
insilmaril@166
    32
extern QString clipboardDir;
insilmaril@166
    33
extern bool clipboardEmpty;
insilmaril@0
    34
extern FlagRowObj *standardFlagsDefault;
insilmaril@0
    35
insilmaril@366
    36
extern QMenu* branchContextMenu;
insilmaril@366
    37
extern QMenu* branchAddContextMenu;
insilmaril@366
    38
extern QMenu* branchRemoveContextMenu;
insilmaril@366
    39
extern QMenu* branchLinksContextMenu;
insilmaril@366
    40
extern QMenu* branchXLinksContextMenuEdit;
insilmaril@366
    41
extern QMenu* branchXLinksContextMenuFollow;
insilmaril@366
    42
extern QMenu* floatimageContextMenu;
insilmaril@366
    43
extern QMenu* canvasContextMenu;
insilmaril@366
    44
insilmaril@0
    45
insilmaril@0
    46
extern Settings settings;
insilmaril@366
    47
extern ImageIO imageIO;
insilmaril@0
    48
insilmaril@404
    49
extern QString vymName;
insilmaril@404
    50
extern QString vymVersion;
insilmaril@404
    51
insilmaril@269
    52
extern QString iconPath;
insilmaril@301
    53
extern QDir vymBaseDir;
insilmaril@366
    54
extern QDir lastImageDir;
insilmaril@269
    55
insilmaril@164
    56
int MapEditor::mapNum=0;	// make instance
insilmaril@0
    57
insilmaril@0
    58
///////////////////////////////////////////////////////////////////////
insilmaril@0
    59
///////////////////////////////////////////////////////////////////////
insilmaril@408
    60
MapEditor::MapEditor( QWidget* parent) :
insilmaril@408
    61
  QGraphicsView(parent), urlOperator(0), imageBuffer(0)
insilmaril@0
    62
{
insilmaril@0
    63
	//cout << "Constructor ME "<<this<<endl;
insilmaril@164
    64
	mapNum++;
insilmaril@0
    65
insilmaril@119
    66
    viewport()->setAcceptDrops(true);
insilmaril@119
    67
insilmaril@408
    68
	mapScene= new QGraphicsScene(parent);
insilmaril@408
    69
	//mapScene= new QGraphicsScene(QRectF(0,0,width(),height()), parent);
insilmaril@408
    70
	mapScene->setBackgroundBrush (QBrush(Qt::white, Qt::SolidPattern));
insilmaril@408
    71
insilmaril@408
    72
insilmaril@408
    73
    setScene (mapScene);
insilmaril@0
    74
	
insilmaril@408
    75
    /* FIXME not needed in QT4? 
insilmaril@408
    76
	// Maybe check QAbstractScrollArea...
insilmaril@160
    77
	// Always show scroll bars (automatic would flicker sometimes)
insilmaril@408
    78
	setVScrollBarMode ( QScrollView::AlwaysOn );
insilmaril@408
    79
	setHScrollBarMode ( QScrollView::AlwaysOn );
insilmaril@408
    80
	*/
insilmaril@408
    81
insilmaril@408
    82
    mapCenter = new MapCenterObj(mapScene);
insilmaril@0
    83
    mapCenter->setVisibility (true);
insilmaril@0
    84
	mapCenter->setMapEditor (this);
insilmaril@93
    85
	mapCenter->setHeading (tr("New Map","Heading of mapcenter in new map"));
insilmaril@408
    86
	//mapCenter->move(mapScene->width()/2-mapCenter->width()/2,mapScene->height()/2-mapCenter->height()/2);
insilmaril@408
    87
	mapCenter->move(0,0);
insilmaril@0
    88
insilmaril@0
    89
    printer=NULL;
insilmaril@0
    90
insilmaril@97
    91
	defLinkColor=QColor (0,0,255);
insilmaril@97
    92
	defXLinkColor=QColor (180,180,180);
insilmaril@0
    93
	linkcolorhint=DefaultColor;
insilmaril@0
    94
	linkstyle=StylePolyParabel;
insilmaril@0
    95
insilmaril@366
    96
	// Create bitmap cursors, platform dependant
insilmaril@404
    97
	HandOpenCursor=QCursor (QPixmap(iconPath+"cursorhandopen.png"),1,1);		
insilmaril@404
    98
	PickColorCursor=QCursor ( QPixmap(iconPath+"cursorcolorpicker.png"), 5,27 ); 
insilmaril@404
    99
	CopyCursor=QCursor ( QPixmap(iconPath+"cursorcopy.png"), 1,1 ); 
insilmaril@404
   100
	XLinkCursor=QCursor ( QPixmap(iconPath+"cursorxlink.png"), 1,7 ); 
insilmaril@398
   101
insilmaril@398
   102
	setFocusPolicy (Qt::StrongFocus);
insilmaril@0
   103
insilmaril@0
   104
	pickingColor=false;
insilmaril@91
   105
	drawingLink=false;
insilmaril@93
   106
	copyingObj=false;
insilmaril@0
   107
insilmaril@0
   108
    editingBO=NULL;
insilmaril@0
   109
    selection=NULL;
insilmaril@0
   110
    selectionLast=NULL;
insilmaril@0
   111
    movingObj=NULL;
insilmaril@0
   112
insilmaril@366
   113
	xelection.setMapCenter (mapCenter);
insilmaril@366
   114
insilmaril@102
   115
	defXLinkWidth=1;
insilmaril@102
   116
	defXLinkColor=QColor (230,230,230);
insilmaril@102
   117
insilmaril@0
   118
    mapChanged=false;
insilmaril@0
   119
	mapDefault=true;
insilmaril@0
   120
	mapUnsaved=false;
insilmaril@0
   121
	
insilmaril@0
   122
	zipped=true;
insilmaril@0
   123
	filePath="";
insilmaril@294
   124
	fileName=tr("unnamed");
insilmaril@0
   125
	mapName="";
insilmaril@0
   126
insilmaril@398
   127
	stepsTotal=settings.readNumEntry("/mapeditor/stepsTotal",100);
insilmaril@381
   128
	undoSet.setEntry ("/history/stepsTotal",QString::number(stepsTotal));
insilmaril@163
   129
	
insilmaril@0
   130
	// Initialize find routine
insilmaril@0
   131
	itFind=NULL;				
insilmaril@0
   132
	EOFind=false;
insilmaril@0
   133
insilmaril@0
   134
	printFrame=true;
insilmaril@0
   135
	printFooter=true;
insilmaril@0
   136
insilmaril@139
   137
	blockReposition=false;
insilmaril@139
   138
	blockSaveState=false;
insilmaril@260
   139
insilmaril@260
   140
	hidemode=HideNone;
insilmaril@260
   141
insilmaril@377
   142
	// Create temporary files
insilmaril@377
   143
	makeTmpDirs();
insilmaril@0
   144
insilmaril@0
   145
	// Initially set movingCentre
insilmaril@0
   146
	updateViewCenter();
insilmaril@0
   147
insilmaril@0
   148
	mapCenter->reposition();	//	for positioning heading
insilmaril@381
   149
insilmaril@0
   150
}
insilmaril@0
   151
insilmaril@0
   152
MapEditor::~MapEditor()
insilmaril@0
   153
{
insilmaril@119
   154
  if (imageBuffer) delete imageBuffer;
insilmaril@119
   155
  if (urlOperator) {
insilmaril@119
   156
    urlOperator->stop();
insilmaril@119
   157
    delete urlOperator;
insilmaril@119
   158
  }
insilmaril@119
   159
insilmaril@0
   160
	//cout <<"Destructor MapEditor\n";
insilmaril@0
   161
}
insilmaril@0
   162
insilmaril@0
   163
MapCenterObj* MapEditor::getMapCenter()
insilmaril@0
   164
{
insilmaril@0
   165
    return mapCenter;
insilmaril@0
   166
}
insilmaril@0
   167
insilmaril@408
   168
QGraphicsScene * MapEditor::getScene()
insilmaril@0
   169
{
insilmaril@408
   170
    return mapScene;
insilmaril@0
   171
}
insilmaril@0
   172
insilmaril@0
   173
void MapEditor::adjustCanvasSize()
insilmaril@0
   174
{
insilmaril@408
   175
insilmaril@408
   176
/* FIXME QT4 probably not needed any longer!
insilmaril@408
   177
insilmaril@408
   178
	// To adjust the scene to map, viewport size and position, we have to
insilmaril@0
   179
	// do some coordinate magic...
insilmaril@0
   180
	//
insilmaril@0
   181
	// Get rectangle of (scroll-)view. 
insilmaril@408
   182
	// We want to be in scene coords, so
insilmaril@0
   183
	// we map. Important if view is zoomed...
insilmaril@408
   184
	QRectF view = inverseWorldMatrix().mapRect( QRectF( contentsX(), contentsY(),
insilmaril@0
   185
												visibleWidth(), visibleHeight()) );	
insilmaril@0
   186
												
insilmaril@408
   187
	// Now we need the bounding box of view AND map to calc the correct scene size.
insilmaril@0
   188
	// Why? Because if the map itself is moved out of view, the view has to be enlarged
insilmaril@0
   189
	// to avoid jumping aroung...
insilmaril@408
   190
	QRectF map=mapCenter->getTotalBBox();
insilmaril@0
   191
insilmaril@0
   192
	// right edge - left edge
insilmaril@0
   193
	int cw= max(map.x() + map.width(),  view.x() + view.width())  - min(map.x(), view.x());
insilmaril@0
   194
	int ch= max(map.y() + map.height(), view.y() + view.height()) - min(map.y(), view.y());
insilmaril@0
   195
insilmaril@0
   196
insilmaril@408
   197
	if ( (cw!=mapScene->width()) || (ch!=mapScene->height()) ||
insilmaril@408
   198
		!mapScene->onCanvas (map.topLeft()) || !mapScene->onCanvas (map.bottomRight())
insilmaril@0
   199
	)	
insilmaril@0
   200
	{	
insilmaril@408
   201
		// move the map on scene (in order to not move it on screen) this is neccessary
insilmaril@408
   202
		// a) if topleft corner of scene is left or above topleft corner of view and also left of
insilmaril@0
   203
		//    above topleft corner of map. E.g. if map is completly inside view, but it would be possible 
insilmaril@408
   204
		//    to scroll to an empty area of scene to the left.
insilmaril@408
   205
		// b) if topleft corner of map left of or above topleft of scene
insilmaril@0
   206
		int dx=0;
insilmaril@0
   207
		int dy=0;
insilmaril@0
   208
insilmaril@408
   209
		if (cw > mapScene->width() )
insilmaril@0
   210
		{
insilmaril@0
   211
			if (map.x()<0) dx=-map.x();	
insilmaril@0
   212
		}
insilmaril@408
   213
		if (cw <  mapScene->width() )
insilmaril@0
   214
			dx=-min (view.x(),map.x());
insilmaril@408
   215
		if (ch > mapScene->height() )
insilmaril@0
   216
		{
insilmaril@0
   217
			if (map.y()<0) dy=-map.y();	
insilmaril@0
   218
		}
insilmaril@408
   219
		if (ch <  mapScene->height() )
insilmaril@0
   220
		{
insilmaril@0
   221
			dy=-min (view.y(),map.y());
insilmaril@0
   222
		}
insilmaril@0
   223
		// We really have to resize now. Let's go...
insilmaril@408
   224
		// FIXME old code mapScene->resize (cw,ch);
insilmaril@408
   225
		mapScene->setSceneRect (QRectFF(0,0,cw,ch));
insilmaril@0
   226
		if ( (dx!=0) || (dy!=0) ) 
insilmaril@0
   227
		{
insilmaril@0
   228
			mapCenter->moveAllBy(dx,dy);
insilmaril@0
   229
			mapCenter->reposition();
insilmaril@336
   230
//			mapCenter->positionBBox();	// To move float
insilmaril@0
   231
insilmaril@0
   232
			// scroll the view (in order to not move map on screen)
insilmaril@0
   233
			scrollBy (dx,dy);
insilmaril@0
   234
		}	
insilmaril@0
   235
	}
insilmaril@408
   236
insilmaril@408
   237
	*/
insilmaril@0
   238
}
insilmaril@0
   239
insilmaril@139
   240
bool MapEditor::isRepositionBlocked()
insilmaril@0
   241
{
insilmaril@139
   242
	return blockReposition;
insilmaril@0
   243
}
insilmaril@0
   244
insilmaril@294
   245
QString MapEditor::getName (LinkableMapObj *lmo)
insilmaril@294
   246
{
insilmaril@314
   247
	QString s;
insilmaril@314
   248
	if (!lmo) return QString("Error: NULL has no name!");
insilmaril@294
   249
insilmaril@294
   250
	if ((typeid(*lmo) == typeid(BranchObj) ||
insilmaril@294
   251
				      typeid(*lmo) == typeid(MapCenterObj))) 
insilmaril@314
   252
	{
insilmaril@314
   253
		
insilmaril@314
   254
		s=(((BranchObj*)lmo)->getHeading());
insilmaril@314
   255
		if (s=="") s="unnamed";
insilmaril@377
   256
		return QString("branch (%1)").arg(s);
insilmaril@377
   257
		//return QString("branch (<font color=\"#0000ff\">%1</font>)").arg(s);
insilmaril@314
   258
	}	
insilmaril@294
   259
	if ((typeid(*lmo) == typeid(FloatImageObj) ))
insilmaril@377
   260
		return QString ("floatimage [%1]").arg(((FloatImageObj*)lmo)->getOriginalFilename());
insilmaril@377
   261
		//return QString ("floatimage [<font color=\"#0000ff\">%1</font>]").arg(((FloatImageObj*)lmo)->getOriginalFilename());
insilmaril@294
   262
	return QString("Unknown type has no name!");
insilmaril@294
   263
}
insilmaril@294
   264
insilmaril@0
   265
void MapEditor::makeTmpDirs()
insilmaril@0
   266
{
insilmaril@0
   267
	// Create unique temporary directories
insilmaril@366
   268
	tmpMapDir=QDir::convertSeparators (tmpVymDir+QString("/mapeditor-%1").arg(mapNum));
insilmaril@366
   269
	histPath=QDir::convertSeparators (tmpMapDir+"/history");
insilmaril@164
   270
	QDir d;
insilmaril@377
   271
	d.mkdir (tmpMapDir);
insilmaril@0
   272
}
insilmaril@0
   273
insilmaril@408
   274
QString MapEditor::saveToDir(const QString &tmpdir, const QString &prefix, bool writeflags, const QPointF &offset, LinkableMapObj *saveSel)
insilmaril@0
   275
{
insilmaril@163
   276
	// tmpdir		temporary directory to which data will be written
insilmaril@0
   277
	// prefix		mapname, which will be appended to images etc.
insilmaril@0
   278
	// writeflags	Only write flags for "real" save of map, not undo
insilmaril@408
   279
	// offset		offset of bbox of whole map in scene. 
insilmaril@0
   280
	//				Needed for XML export
insilmaril@0
   281
	
insilmaril@0
   282
	// Save Header
insilmaril@0
   283
	QString ls;
insilmaril@0
   284
	switch (linkstyle)
insilmaril@0
   285
	{
insilmaril@0
   286
		case StyleLine: 
insilmaril@0
   287
			ls="StyleLine";
insilmaril@0
   288
			break;
insilmaril@0
   289
		case StyleParabel:
insilmaril@0
   290
			ls="StyleParabel";
insilmaril@0
   291
			break;
insilmaril@0
   292
		case StylePolyLine:	
insilmaril@0
   293
			ls="StylePolyLine";
insilmaril@0
   294
			break;
insilmaril@0
   295
		default:
insilmaril@0
   296
			ls="StylePolyParabel";
insilmaril@0
   297
			break;
insilmaril@0
   298
	}	
insilmaril@0
   299
insilmaril@0
   300
	QString s="<?xml version=\"1.0\" encoding=\"utf-8\"?><!DOCTYPE vymmap>\n";
insilmaril@0
   301
	QString colhint="";
insilmaril@0
   302
	if (linkcolorhint==HeadingColor) 
insilmaril@0
   303
		colhint=attribut("linkColorHint","HeadingColor");
insilmaril@0
   304
insilmaril@404
   305
	QString mapAttr=attribut("version",vymVersion);
insilmaril@398
   306
	if (!saveSel || saveSel==mapCenter)
insilmaril@0
   307
		mapAttr+= attribut("author",mapCenter->getAuthor()) +
insilmaril@0
   308
				  attribut("comment",mapCenter->getComment()) +
insilmaril@0
   309
			      attribut("date",mapCenter->getDate()) +
insilmaril@408
   310
		          attribut("backgroundColor", mapScene->backgroundBrush().color().name() ) +
insilmaril@0
   311
		          attribut("linkStyle", ls ) +
insilmaril@97
   312
		          attribut("linkColor", defLinkColor.name() ) +
insilmaril@102
   313
		          attribut("defXLinkColor", defXLinkColor.name() ) +
insilmaril@102
   314
		          attribut("defXLinkWidth", QString().setNum(defXLinkWidth,10) ) +
insilmaril@0
   315
		          colhint; 
insilmaril@0
   316
	s+=beginElement("vymmap",mapAttr);
insilmaril@0
   317
	incIndent();
insilmaril@0
   318
insilmaril@0
   319
	// Find the used flags while traversing the tree
insilmaril@0
   320
	standardFlagsDefault->resetUsedCounter();
insilmaril@0
   321
	
insilmaril@166
   322
	// Reset the counters before saving
insilmaril@406
   323
	// TODO constr. of FIO creates lots of objects, better do this in some other way...
insilmaril@408
   324
	FloatImageObj (mapScene).resetSaveCounter();
insilmaril@166
   325
insilmaril@0
   326
	// Build xml recursivly
insilmaril@398
   327
	if (!saveSel || typeid (*saveSel) == typeid (MapCenterObj))
insilmaril@366
   328
		// Save complete map, if saveSel not set
insilmaril@0
   329
		s+=mapCenter->saveToDir(tmpdir,prefix,writeflags,offset);
insilmaril@0
   330
	else
insilmaril@0
   331
	{
insilmaril@366
   332
		if ( typeid(*saveSel) == typeid(BranchObj) )
insilmaril@366
   333
			// Save Subtree
insilmaril@366
   334
			s+=((BranchObj*)(saveSel))->saveToDir(tmpdir,prefix,offset);
insilmaril@366
   335
		else if ( typeid(*saveSel) == typeid(FloatImageObj) )
insilmaril@366
   336
			// Save image
insilmaril@377
   337
			s+=((FloatImageObj*)(saveSel))->saveToDir(tmpdir,prefix);
insilmaril@166
   338
			
insilmaril@166
   339
		else if (selection && typeid(*selection)==typeid(BranchObj))
insilmaril@366
   340
			// Save selected branch is saved from mainwindow		//FIXME maybe use "subtree" above?
insilmaril@398
   341
			// FIXME is this possible at all? BO is already above...
insilmaril@366
   342
			s+=((BranchObj*)selection)->saveToDir(tmpdir,prefix,offset);
insilmaril@0
   343
	}
insilmaril@0
   344
insilmaril@0
   345
	// Save local settings
insilmaril@0
   346
	s+=settings.getXMLData (destPath);
insilmaril@0
   347
insilmaril@0
   348
	// Save selection
insilmaril@366
   349
	if (selection && !saveSel ) 
insilmaril@0
   350
		s+=valueElement("select",selection->getSelectString());
insilmaril@0
   351
insilmaril@0
   352
	decIndent();
insilmaril@0
   353
	s+=endElement("vymmap");
insilmaril@0
   354
insilmaril@0
   355
	if (writeflags)
insilmaril@0
   356
		standardFlagsDefault->saveToDir (tmpdir+"/flags/","",writeflags);
insilmaril@0
   357
	return s;
insilmaril@0
   358
}
insilmaril@0
   359
insilmaril@395
   360
void MapEditor::saveStateChangingPart(LinkableMapObj *undoSel, LinkableMapObj* redoSel, const QString &rc, const QString &comment)
insilmaril@0
   361
{
insilmaril@366
   362
	// save the selected part of the map, Undo will replace part of map 
insilmaril@366
   363
	QString undoSelection="";
insilmaril@395
   364
	if (undoSel)
insilmaril@395
   365
		undoSelection=undoSel->getSelectString();
insilmaril@395
   366
	else
insilmaril@395
   367
		qWarning ("MapEditor::saveStateChangingPart  no undoSel given!");
insilmaril@395
   368
	QString redoSelection="";
insilmaril@395
   369
	if (redoSel)
insilmaril@395
   370
		redoSelection=undoSel->getSelectString();
insilmaril@395
   371
	else
insilmaril@395
   372
		qWarning ("MapEditor::saveStateChangingPart  no redoSel given!");
insilmaril@395
   373
		
insilmaril@366
   374
insilmaril@366
   375
	saveState (PartOfMap,
insilmaril@398
   376
		undoSelection, "addMapReplace (\"PATH\")",
insilmaril@395
   377
		redoSelection, rc, 
insilmaril@366
   378
		comment, 
insilmaril@366
   379
		undoSel);
insilmaril@105
   380
}
insilmaril@105
   381
insilmaril@393
   382
void MapEditor::saveStateRemovingPart(LinkableMapObj *redoSel, const QString &comment)
insilmaril@393
   383
{
insilmaril@394
   384
	if (!redoSel)
insilmaril@393
   385
	{
insilmaril@394
   386
		qWarning ("MapEditor::saveStateRemovingPart  no redoSel given!");
insilmaril@393
   387
		return;
insilmaril@393
   388
	}
insilmaril@393
   389
	QString undoSelection=redoSel->getParObj()->getSelectString();
insilmaril@393
   390
	QString redoSelection=redoSel->getSelectString();
insilmaril@398
   391
	if (typeid(*redoSel) == typeid(BranchObj)  ) 
insilmaril@394
   392
	{
insilmaril@394
   393
		// save the selected branch of the map, Undo will insert part of map 
insilmaril@394
   394
		saveState (PartOfMap,
insilmaril@394
   395
			undoSelection, QString("addMapInsert (\"PATH\",%1)").arg(((BranchObj*)redoSel)->getNum()),
insilmaril@394
   396
			redoSelection, "delete ()", 
insilmaril@394
   397
			comment, 
insilmaril@394
   398
			redoSel);
insilmaril@394
   399
	}
insilmaril@393
   400
}
insilmaril@393
   401
insilmaril@106
   402
insilmaril@366
   403
void MapEditor::saveState(LinkableMapObj *undoSel, const QString &uc, LinkableMapObj *redoSel, const QString &rc, const QString &comment) 
insilmaril@366
   404
{
insilmaril@366
   405
	// "Normal" savestate: save commands, selections and comment
insilmaril@366
   406
	// so just save commands for undo and redo
insilmaril@366
   407
	// and use current selection
insilmaril@366
   408
insilmaril@366
   409
	QString redoSelection="";
insilmaril@366
   410
	if (redoSel) redoSelection=redoSel->getSelectString();
insilmaril@366
   411
	QString undoSelection="";
insilmaril@366
   412
	if (undoSel) undoSelection=undoSel->getSelectString();
insilmaril@366
   413
insilmaril@366
   414
	saveState (UndoCommand,
insilmaril@366
   415
		undoSelection, uc,
insilmaril@366
   416
		redoSelection, rc, 
insilmaril@366
   417
		comment, 
insilmaril@366
   418
		NULL);
insilmaril@366
   419
}
insilmaril@366
   420
insilmaril@366
   421
void MapEditor::saveState(const QString &undoSel, const QString &uc, const QString &redoSel, const QString &rc, const QString &comment) 
insilmaril@366
   422
{
insilmaril@366
   423
	// "Normal" savestate: save commands, selections and comment
insilmaril@366
   424
	// so just save commands for undo and redo
insilmaril@366
   425
	// and use current selection
insilmaril@366
   426
	saveState (UndoCommand,
insilmaril@366
   427
		undoSel, uc,
insilmaril@366
   428
		redoSel, rc, 
insilmaril@366
   429
		comment, 
insilmaril@366
   430
		NULL);
insilmaril@366
   431
}
insilmaril@366
   432
insilmaril@366
   433
		
insilmaril@366
   434
void MapEditor::saveState(const SaveMode &savemode, const QString &undoSelection, const QString &undoCom, const QString &redoSelection, const QString &redoCom, const QString &comment, LinkableMapObj *saveSel)
insilmaril@163
   435
{
insilmaril@163
   436
	// Main saveState
insilmaril@163
   437
insilmaril@139
   438
	if (blockSaveState) return;
insilmaril@139
   439
insilmaril@163
   440
	/* TODO remove after testing
insilmaril@163
   441
	*/
insilmaril@388
   442
	cout << "ME::saveState()  "<<endl;
insilmaril@294
   443
	
insilmaril@366
   444
	int undosAvail=undoSet.readNumEntry ("/history/undosAvail",0);
insilmaril@381
   445
	int redosAvail=undoSet.readNumEntry ("/history/redosAvail",0);
insilmaril@366
   446
	int curStep=undoSet.readNumEntry ("/history/curStep",0);
insilmaril@163
   447
	// Find out current undo directory
insilmaril@381
   448
	if (undosAvail<stepsTotal) undosAvail++;
insilmaril@366
   449
	curStep++;
insilmaril@381
   450
	if (curStep>stepsTotal) curStep=1;
insilmaril@163
   451
	
insilmaril@398
   452
	QString backupXML="";
insilmaril@388
   453
	QString bakMapName=QDir::convertSeparators (QString("history-%1").arg(curStep));
insilmaril@388
   454
	QString bakMapDir=QDir::convertSeparators (tmpMapDir +"/"+bakMapName);
insilmaril@164
   455
	QString bakMapPath=QDir::convertSeparators(bakMapDir+"/map.xml");
insilmaril@164
   456
insilmaril@164
   457
	// Create bakMapDir if not available
insilmaril@164
   458
	QDir d(bakMapDir);
insilmaril@164
   459
	if (!d.exists()) 
insilmaril@164
   460
		makeSubDirs (bakMapDir);
insilmaril@163
   461
insilmaril@366
   462
	// Save depending on how much needs to be saved	
insilmaril@398
   463
	if (saveSel)
insilmaril@408
   464
		backupXML=saveToDir (bakMapDir,mapName+"-",false, QPointF (),saveSel);
insilmaril@136
   465
		
insilmaril@163
   466
	QString undoCommand="";
insilmaril@106
   467
	if (savemode==UndoCommand)
insilmaril@0
   468
	{
insilmaril@105
   469
		undoCommand=undoCom;
insilmaril@106
   470
	}	
insilmaril@366
   471
	else if (savemode==PartOfMap )
insilmaril@0
   472
	{
insilmaril@393
   473
		undoCommand=undoCom;
insilmaril@393
   474
		undoCommand.replace ("PATH",bakMapPath);
insilmaril@106
   475
	}
insilmaril@398
   476
insilmaril@163
   477
	if (!backupXML.isEmpty())
insilmaril@163
   478
		// Write XML Data to disk
insilmaril@163
   479
		saveStringToDisk (QString(bakMapPath),backupXML);
insilmaril@163
   480
insilmaril@388
   481
	// We would have to save all actions in a tree, to keep track of 
insilmaril@393
   482
	// possible redos after a action. Possible, but we are too lazy: forget about redos.
insilmaril@388
   483
	redosAvail=0;
insilmaril@388
   484
insilmaril@388
   485
	// Write the current state to disk
insilmaril@366
   486
	undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
insilmaril@388
   487
	undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
insilmaril@366
   488
	undoSet.setEntry ("/history/curStep",QString::number(curStep));
insilmaril@366
   489
	undoSet.setEntry (QString("/history/step-%1/undoCommand").arg(curStep),undoCommand);
insilmaril@366
   490
	undoSet.setEntry (QString("/history/step-%1/undoSelection").arg(curStep),undoSelection);
insilmaril@366
   491
	undoSet.setEntry (QString("/history/step-%1/redoCommand").arg(curStep),redoCom);
insilmaril@366
   492
	undoSet.setEntry (QString("/history/step-%1/redoSelection").arg(curStep),redoSelection);
insilmaril@366
   493
	undoSet.setEntry (QString("/history/step-%1/comment").arg(curStep),comment);
insilmaril@404
   494
	undoSet.setEntry (QString("/history/version"),vymVersion);
insilmaril@366
   495
	undoSet.writeSettings(histPath);
insilmaril@163
   496
insilmaril@163
   497
	/* TODO remove after testing
insilmaril@366
   498
	*/
insilmaril@366
   499
	//cout << "          into="<< histPath.toStdString()<<endl;
insilmaril@381
   500
	cout << "    stepsTotal="<<stepsTotal<<
insilmaril@366
   501
	", undosAvail="<<undosAvail<<
insilmaril@381
   502
	", redosAvail="<<redosAvail<<
insilmaril@366
   503
	", curStep="<<curStep<<endl;
insilmaril@163
   504
	cout << "    ---------------------------"<<endl;
insilmaril@366
   505
	cout << "    comment="<<comment.toStdString()<<endl;
insilmaril@366
   506
	cout << "    undoCom="<<undoCommand.toStdString()<<endl;
insilmaril@366
   507
	cout << "    undoSel="<<undoSelection.toStdString()<<endl;
insilmaril@366
   508
	cout << "    redoCom="<<redoCom.toStdString()<<endl;
insilmaril@366
   509
	cout << "    redoSel="<<redoSelection.toStdString()<<endl;
insilmaril@398
   510
	if (saveSel) cout << "    saveSel="<<saveSel->getSelectString().ascii()<<endl;
insilmaril@294
   511
	cout << "    ---------------------------"<<endl;
insilmaril@377
   512
insilmaril@408
   513
	mainWindow->updateHistory (undoSet);
insilmaril@407
   514
	setChanged();
insilmaril@395
   515
	updateActions();
insilmaril@106
   516
}
insilmaril@106
   517
insilmaril@129
   518
void MapEditor::parseAtom(const QString &atom)
insilmaril@106
   519
{
insilmaril@106
   520
	API api;
insilmaril@163
   521
	QString s,t;
insilmaril@129
   522
	int x,y;
insilmaril@395
   523
	bool b,ok;
insilmaril@129
   524
insilmaril@129
   525
	// Split string s into command and parameters
insilmaril@394
   526
	api.parseInput (atom);
insilmaril@129
   527
	QString com=api.command();
insilmaril@129
   528
	
insilmaril@106
   529
	// External commands
insilmaril@393
   530
	if (com=="addBranch")
insilmaril@366
   531
	{
insilmaril@394
   532
		if (!selection)
insilmaril@394
   533
		{
insilmaril@394
   534
			api.setError (Aborted,"Nothing selected");
insilmaril@394
   535
		} else if ( (typeid(*selection) != typeid(BranchObj) && 
insilmaril@394
   536
					 typeid(*selection) != typeid(MapCenterObj)) )
insilmaril@394
   537
		{				  
insilmaril@394
   538
			api.setError (Aborted,"Type of selection is not a branch");
insilmaril@394
   539
		} else 
insilmaril@366
   540
		{	
insilmaril@394
   541
			QList <int> pl;
insilmaril@394
   542
			pl << 0 <<1;
insilmaril@394
   543
			if (api.checkParamCount(pl))
insilmaril@394
   544
			{
insilmaril@394
   545
				if (api.paramCount()==0)
insilmaril@394
   546
					addNewBranchInt (-2);
insilmaril@394
   547
				else
insilmaril@394
   548
				{
insilmaril@394
   549
					y=api.parInt (ok,0);
insilmaril@394
   550
					if (ok ) addNewBranchInt (y);
insilmaril@394
   551
				}
insilmaril@394
   552
			}
insilmaril@394
   553
		}
insilmaril@394
   554
	} else if (com=="addBranchBefore")
insilmaril@394
   555
	{
insilmaril@394
   556
		if (!selection)
insilmaril@394
   557
		{
insilmaril@394
   558
			api.setError (Aborted,"Nothing selected");
insilmaril@394
   559
		} else if ( (typeid(*selection) != typeid(BranchObj) && 
insilmaril@394
   560
					 typeid(*selection) != typeid(MapCenterObj)) )
insilmaril@394
   561
		{				  
insilmaril@394
   562
			api.setError (Aborted,"Type of selection is not a branch");
insilmaril@394
   563
		} else 
insilmaril@394
   564
		{	
insilmaril@394
   565
			if (api.paramCount()==0)
insilmaril@394
   566
			{
insilmaril@394
   567
				addNewBranchBefore ();
insilmaril@394
   568
			}	
insilmaril@394
   569
		}
insilmaril@393
   570
	} else if (com==QString("addMapReplace"))
insilmaril@393
   571
	{
insilmaril@394
   572
		if (!selection)
insilmaril@394
   573
		{
insilmaril@394
   574
			api.setError (Aborted,"Nothing selected");
insilmaril@394
   575
		} else if ( (typeid(*selection) != typeid(BranchObj) && 
insilmaril@394
   576
					 typeid(*selection) != typeid(MapCenterObj)) )
insilmaril@394
   577
		{				  
insilmaril@394
   578
			api.setError (Aborted,"Type of selection is not a branch");
insilmaril@398
   579
		} else if (api.checkParamCount(1))
insilmaril@393
   580
		{
insilmaril@398
   581
			//s=api.parString (ok,0);	// selection
insilmaril@398
   582
			t=api.parString (ok,0);	// path to map
insilmaril@393
   583
			if (QDir::isRelativePath(t)) t=QDir::convertSeparators (tmpMapDir + "/"+t);
insilmaril@398
   584
			addMapReplaceInt(selection->getSelectString(),t);	
insilmaril@393
   585
		}
insilmaril@393
   586
	} else if (com==QString("addMapInsert"))
insilmaril@393
   587
	{
insilmaril@394
   588
		if (!selection)
insilmaril@393
   589
		{
insilmaril@394
   590
			api.setError (Aborted,"Nothing selected");
insilmaril@394
   591
		} else if ( (typeid(*selection) != typeid(BranchObj) && 
insilmaril@394
   592
					 typeid(*selection) != typeid(MapCenterObj)) )
insilmaril@394
   593
		{				  
insilmaril@394
   594
			api.setError (Aborted,"Type of selection is not a branch");
insilmaril@394
   595
		} else 
insilmaril@394
   596
		{	
insilmaril@394
   597
			if (api.checkParamCount(2))
insilmaril@394
   598
			{
insilmaril@394
   599
				t=api.parString (ok,0);	// path to map
insilmaril@394
   600
				y=api.parInt(ok,1);		// position
insilmaril@394
   601
				if (QDir::isRelativePath(t)) t=QDir::convertSeparators (tmpMapDir + "/"+t);
insilmaril@394
   602
				addMapInsertInt(t,y);	
insilmaril@394
   603
			}
insilmaril@393
   604
		}
insilmaril@395
   605
	} else if (com=="colorItem")
insilmaril@395
   606
	{
insilmaril@395
   607
		if (!selection)
insilmaril@395
   608
		{
insilmaril@395
   609
			api.setError (Aborted,"Nothing selected");
insilmaril@395
   610
		} else if ( typeid(*selection) != typeid(BranchObj) && 
insilmaril@395
   611
					typeid(*selection) != typeid(MapCenterObj))
insilmaril@395
   612
		{				  
insilmaril@395
   613
			api.setError (Aborted,"Type of selection is not a branch");
insilmaril@395
   614
		} else if (api.checkParamCount(1))
insilmaril@395
   615
		{	
insilmaril@395
   616
			QColor c=api.parColor (ok,0);
insilmaril@395
   617
			if (ok) colorItem (c);
insilmaril@395
   618
		}	
insilmaril@395
   619
	} else if (com=="colorBranch")
insilmaril@395
   620
	{
insilmaril@395
   621
		if (!selection)
insilmaril@395
   622
		{
insilmaril@395
   623
			api.setError (Aborted,"Nothing selected");
insilmaril@395
   624
		} else if ( typeid(*selection) != typeid(BranchObj) && 
insilmaril@395
   625
					typeid(*selection) != typeid(MapCenterObj))
insilmaril@395
   626
		{				  
insilmaril@395
   627
			api.setError (Aborted,"Type of selection is not a branch");
insilmaril@395
   628
		} else if (api.checkParamCount(1))
insilmaril@395
   629
		{	
insilmaril@395
   630
			QColor c=api.parColor (ok,0);
insilmaril@395
   631
			if (ok) colorBranch (c);
insilmaril@395
   632
		}	
insilmaril@395
   633
	} else if (com=="cut")
insilmaril@395
   634
	{
insilmaril@395
   635
		if (!selection)
insilmaril@395
   636
		{
insilmaril@395
   637
			api.setError (Aborted,"Nothing selected");
insilmaril@395
   638
		} else if ( typeid(*selection) != typeid(BranchObj) && 
insilmaril@395
   639
					typeid(*selection) != typeid(MapCenterObj) &&
insilmaril@395
   640
					typeid(*selection) != typeid(FloatImageObj) )
insilmaril@395
   641
		{				  
insilmaril@395
   642
			api.setError (Aborted,"Type of selection is not a branch or floatimage");
insilmaril@395
   643
		} else if (api.checkParamCount(0))
insilmaril@395
   644
		{	
insilmaril@395
   645
			cut();
insilmaril@395
   646
		}	
insilmaril@393
   647
	} else if (com=="delete")
insilmaril@393
   648
	{
insilmaril@394
   649
		if (!selection)
insilmaril@394
   650
		{
insilmaril@394
   651
			api.setError (Aborted,"Nothing selected");
insilmaril@394
   652
		} else if ( (typeid(*selection) != typeid(BranchObj) && 
insilmaril@394
   653
					 typeid(*selection) != typeid(MapCenterObj)) )
insilmaril@394
   654
		{				  
insilmaril@394
   655
			api.setError (Aborted,"Type of selection is not a branch");
insilmaril@394
   656
		} else if (api.checkParamCount(0))
insilmaril@393
   657
		{	
insilmaril@393
   658
			deleteSelection();
insilmaril@393
   659
		}	
insilmaril@394
   660
	} else if (com=="deleteKeepChilds")
insilmaril@394
   661
	{
insilmaril@394
   662
		if (!selection)
insilmaril@394
   663
		{
insilmaril@394
   664
			api.setError (Aborted,"Nothing selected");
insilmaril@394
   665
		} else if ( (typeid(*selection) != typeid(BranchObj) && 
insilmaril@394
   666
					 typeid(*selection) != typeid(MapCenterObj)) )
insilmaril@394
   667
		{				  
insilmaril@394
   668
			api.setError (Aborted,"Type of selection is not a branch");
insilmaril@394
   669
		} else if (api.checkParamCount(0))
insilmaril@394
   670
		{	
insilmaril@395
   671
			deleteKeepChilds();
insilmaril@395
   672
		}	
insilmaril@395
   673
	} else if (com=="deleteChilds")
insilmaril@395
   674
	{
insilmaril@395
   675
		if (!selection)
insilmaril@395
   676
		{
insilmaril@395
   677
			api.setError (Aborted,"Nothing selected");
insilmaril@395
   678
		} else if ( (typeid(*selection) != typeid(BranchObj) && 
insilmaril@395
   679
					 typeid(*selection) != typeid(MapCenterObj)) )
insilmaril@395
   680
		{				  
insilmaril@395
   681
			api.setError (Aborted,"Type of selection is not a branch");
insilmaril@395
   682
		} else if (api.checkParamCount(0))
insilmaril@395
   683
		{	
insilmaril@395
   684
			deleteChilds();
insilmaril@394
   685
		}	
insilmaril@393
   686
	} else if (com=="linkBranchToPos")
insilmaril@129
   687
	{
insilmaril@394
   688
		if (!selection)
insilmaril@129
   689
		{
insilmaril@394
   690
			api.setError (Aborted,"Nothing selected");
insilmaril@394
   691
		} else if ( (typeid(*selection) != typeid(BranchObj) && 
insilmaril@394
   692
					 typeid(*selection) != typeid(MapCenterObj)) )
insilmaril@394
   693
		{				  
insilmaril@394
   694
			api.setError (Aborted,"Type of selection is not a branch");
insilmaril@394
   695
		} else if (api.checkParamCount(4))
insilmaril@394
   696
		{
insilmaril@394
   697
			// 0	selectstring of parent
insilmaril@394
   698
			// 1	num in parent (for branches)
insilmaril@394
   699
			// 2,3	x,y of mainbranch or mapcenter
insilmaril@394
   700
			s=api.parString(ok,0);
insilmaril@394
   701
			LinkableMapObj *dst=mapCenter->findObjBySelect (s);
insilmaril@394
   702
			if (dst)
insilmaril@394
   703
			{	
insilmaril@394
   704
				if (typeid(*dst) == typeid(BranchObj) ) 
insilmaril@394
   705
				{
insilmaril@394
   706
					// Get number in parent
insilmaril@394
   707
					x=api.parInt (ok,1);
insilmaril@394
   708
					if (ok)
insilmaril@394
   709
						((BranchObj*)selection)->moveBranchTo ((BranchObj*)(dst),x);
insilmaril@394
   710
				} else if (typeid(*dst) == typeid(MapCenterObj) ) 
insilmaril@394
   711
				{
insilmaril@394
   712
					((BranchObj*)selection)->moveBranchTo ((BranchObj*)(dst),-1);
insilmaril@394
   713
					// Get coordinates of mainbranch
insilmaril@394
   714
					x=api.parInt (ok,2);
insilmaril@394
   715
					if (ok)
insilmaril@139
   716
					{
insilmaril@394
   717
						y=api.parInt (ok,3);
insilmaril@394
   718
						if (ok) ((BranchObj*)selection)->move (x,y);
insilmaril@394
   719
					}
insilmaril@129
   720
				}	
insilmaril@394
   721
			}	
insilmaril@129
   722
		}
insilmaril@393
   723
	} else if (com=="moveBranchUp")
insilmaril@393
   724
	{
insilmaril@394
   725
		if (!selection)
insilmaril@394
   726
		{
insilmaril@394
   727
			api.setError (Aborted,"Nothing selected");
insilmaril@394
   728
		} else if ( (typeid(*selection) != typeid(BranchObj) && 
insilmaril@394
   729
					 typeid(*selection) != typeid(MapCenterObj)) )
insilmaril@394
   730
		{				  
insilmaril@394
   731
			api.setError (Aborted,"Type of selection is not a branch");
insilmaril@394
   732
		} else if (api.checkParamCount(0))
insilmaril@394
   733
		{
insilmaril@394
   734
			moveBranchUp();
insilmaril@394
   735
		}	
insilmaril@394
   736
	} else if (com=="moveBranchDown")
insilmaril@394
   737
	{
insilmaril@394
   738
		if (!selection)
insilmaril@394
   739
		{
insilmaril@394
   740
			api.setError (Aborted,"Nothing selected");
insilmaril@394
   741
		} else if ( (typeid(*selection) != typeid(BranchObj) && 
insilmaril@394
   742
					 typeid(*selection) != typeid(MapCenterObj)) )
insilmaril@394
   743
		{				  
insilmaril@394
   744
			api.setError (Aborted,"Type of selection is not a branch");
insilmaril@394
   745
		} else if (api.checkParamCount(0))
insilmaril@394
   746
		{
insilmaril@394
   747
			moveBranchDown();
insilmaril@394
   748
		}	
insilmaril@394
   749
	} else if (com=="move")
insilmaril@394
   750
	{
insilmaril@394
   751
		if (!selection)
insilmaril@394
   752
		{
insilmaril@394
   753
			api.setError (Aborted,"Nothing selected");
insilmaril@394
   754
		} else if ( typeid(*selection) != typeid(BranchObj) && 
insilmaril@394
   755
					typeid(*selection) != typeid(MapCenterObj) &&
insilmaril@394
   756
					typeid(*selection) != typeid(FloatImageObj) )
insilmaril@394
   757
		{				  
insilmaril@394
   758
			api.setError (Aborted,"Type of selection is not a branch or floatimage");
insilmaril@394
   759
		} else if (api.checkParamCount(2))
insilmaril@393
   760
		{	
insilmaril@393
   761
			x=api.parInt (ok,0);
insilmaril@393
   762
			if (ok)
insilmaril@393
   763
			{
insilmaril@393
   764
				y=api.parInt (ok,1);
insilmaril@393
   765
				if (ok) move (x,y);
insilmaril@393
   766
			}
insilmaril@393
   767
		}	
insilmaril@395
   768
	} else if (com=="moveRel")
insilmaril@393
   769
	{
insilmaril@394
   770
		if (!selection)
insilmaril@394
   771
		{
insilmaril@394
   772
			api.setError (Aborted,"Nothing selected");
insilmaril@394
   773
		} else if ( typeid(*selection) != typeid(BranchObj) && 
insilmaril@394
   774
					typeid(*selection) != typeid(MapCenterObj) &&
insilmaril@394
   775
					typeid(*selection) != typeid(FloatImageObj) )
insilmaril@394
   776
		{				  
insilmaril@394
   777
			api.setError (Aborted,"Type of selection is not a branch or floatimage");
insilmaril@394
   778
		} else if (api.checkParamCount(2))
insilmaril@393
   779
		{	
insilmaril@393
   780
			x=api.parInt (ok,0);
insilmaril@393
   781
			if (ok)
insilmaril@393
   782
			{
insilmaril@393
   783
				y=api.parInt (ok,1);
insilmaril@393
   784
				if (ok) moveRel (x,y);
insilmaril@393
   785
			}
insilmaril@393
   786
		}	
insilmaril@395
   787
	} else if (com=="paste")
insilmaril@395
   788
	{
insilmaril@395
   789
		if (!selection)
insilmaril@395
   790
		{
insilmaril@395
   791
			api.setError (Aborted,"Nothing selected");
insilmaril@395
   792
		} else if ( typeid(*selection) != typeid(BranchObj) && 
insilmaril@395
   793
					typeid(*selection) != typeid(MapCenterObj) )
insilmaril@395
   794
		{				  
insilmaril@395
   795
			api.setError (Aborted,"Type of selection is not a branch");
insilmaril@395
   796
		} else if (api.checkParamCount(0))
insilmaril@395
   797
		{	
insilmaril@395
   798
			paste();
insilmaril@395
   799
		}	
insilmaril@398
   800
	} else if (com=="select")
insilmaril@398
   801
	{
insilmaril@398
   802
		if (api.checkParamCount(1))
insilmaril@398
   803
		{
insilmaril@398
   804
			s=api.parString(ok,0);
insilmaril@398
   805
			if (ok) select (s);
insilmaril@398
   806
		}	
insilmaril@398
   807
	} else if (com=="setMapAuthor")
insilmaril@398
   808
	{
insilmaril@398
   809
		if (api.checkParamCount(1))
insilmaril@398
   810
		{
insilmaril@398
   811
			s=api.parString(ok,0);
insilmaril@398
   812
			if (ok) setMapAuthor (s);
insilmaril@398
   813
		}	
insilmaril@398
   814
	} else if (com=="setMapComment")
insilmaril@398
   815
	{
insilmaril@398
   816
		if (api.checkParamCount(1))
insilmaril@398
   817
		{
insilmaril@398
   818
			s=api.parString(ok,0);
insilmaril@398
   819
			if (ok) setMapComment(s);
insilmaril@398
   820
		}	
insilmaril@398
   821
	} else if (com=="setMapBackgroundColor")
insilmaril@398
   822
	{
insilmaril@398
   823
		if (!selection)
insilmaril@398
   824
		{
insilmaril@398
   825
			api.setError (Aborted,"Nothing selected");
insilmaril@398
   826
		} else if ( (typeid(*selection) != typeid(BranchObj) && 
insilmaril@398
   827
					 typeid(*selection) != typeid(MapCenterObj)) )
insilmaril@398
   828
		{				  
insilmaril@398
   829
			api.setError (Aborted,"Type of selection is not a branch");
insilmaril@398
   830
		} else if (api.checkParamCount(1))
insilmaril@398
   831
		{
insilmaril@398
   832
			QColor c=api.parColor (ok,0);
insilmaril@398
   833
			if (ok) setMapBackgroundColor (c);
insilmaril@398
   834
		}	
insilmaril@398
   835
	} else if (com=="setMapDefLinkColor")
insilmaril@398
   836
	{
insilmaril@398
   837
		if (!selection)
insilmaril@398
   838
		{
insilmaril@398
   839
			api.setError (Aborted,"Nothing selected");
insilmaril@398
   840
		} else if ( (typeid(*selection) != typeid(BranchObj) && 
insilmaril@398
   841
					 typeid(*selection) != typeid(MapCenterObj)) )
insilmaril@398
   842
		{				  
insilmaril@398
   843
			api.setError (Aborted,"Type of selection is not a branch");
insilmaril@398
   844
		} else if (api.checkParamCount(1))
insilmaril@398
   845
		{
insilmaril@398
   846
			QColor c=api.parColor (ok,0);
insilmaril@398
   847
			if (ok) setMapDefLinkColor (c);
insilmaril@398
   848
		}	
insilmaril@398
   849
	} else if (com=="setMapLinkStyle")
insilmaril@398
   850
	{
insilmaril@398
   851
		if (api.checkParamCount(1))
insilmaril@398
   852
		{
insilmaril@398
   853
			s=api.parString (ok,0);
insilmaril@398
   854
			if (ok) setMapLinkStyle(s);
insilmaril@398
   855
		}	
insilmaril@129
   856
	} else if (com=="setHeading")
insilmaril@129
   857
	{
insilmaril@394
   858
		if (!selection)
insilmaril@394
   859
		{
insilmaril@394
   860
			api.setError (Aborted,"Nothing selected");
insilmaril@394
   861
		} else if ( (typeid(*selection) != typeid(BranchObj) && 
insilmaril@394
   862
					 typeid(*selection) != typeid(MapCenterObj)) )
insilmaril@394
   863
		{				  
insilmaril@394
   864
			api.setError (Aborted,"Type of selection is not a branch");
insilmaril@394
   865
		} else if (api.checkParamCount(1))
insilmaril@129
   866
		{
insilmaril@129
   867
			s=api.parString (ok,0);
insilmaril@366
   868
			if (ok) 
insilmaril@366
   869
				setHeading (s);
insilmaril@129
   870
		}	
insilmaril@395
   871
	} else if (com=="setHideExport")
insilmaril@395
   872
	{
insilmaril@395
   873
		if (!selection)
insilmaril@395
   874
		{
insilmaril@395
   875
			api.setError (Aborted,"Nothing selected");
insilmaril@395
   876
		} else if ( typeid(*selection) != typeid(BranchObj) && 
insilmaril@395
   877
					typeid(*selection) != typeid(FloatImageObj) )
insilmaril@395
   878
		{				  
insilmaril@395
   879
			api.setError (Aborted,"Type of selection is not a branch or floatimage");
insilmaril@395
   880
		} else if (api.checkParamCount(1))
insilmaril@395
   881
		{
insilmaril@395
   882
			b=api.parBool(ok,0);
insilmaril@395
   883
			if (ok) setHideExport (b);
insilmaril@395
   884
		}
insilmaril@143
   885
	} else if (com=="setURL")
insilmaril@143
   886
	{
insilmaril@394
   887
		if (!selection)
insilmaril@394
   888
		{
insilmaril@394
   889
			api.setError (Aborted,"Nothing selected");
insilmaril@394
   890
		} else if ( (typeid(*selection) != typeid(BranchObj) && 
insilmaril@394
   891
					 typeid(*selection) != typeid(MapCenterObj)) )
insilmaril@394
   892
		{				  
insilmaril@394
   893
			api.setError (Aborted,"Type of selection is not a branch");
insilmaril@394
   894
		} else if (api.checkParamCount(1))
insilmaril@143
   895
		{
insilmaril@143
   896
			s=api.parString (ok,0);
insilmaril@366
   897
			if (ok) setURLInt(s);
insilmaril@143
   898
		}	
insilmaril@143
   899
	} else if (com=="setVymLink")
insilmaril@143
   900
	{
insilmaril@394
   901
		if (!selection)
insilmaril@394
   902
		{
insilmaril@394
   903
			api.setError (Aborted,"Nothing selected");
insilmaril@394
   904
		} else if ( (typeid(*selection) != typeid(BranchObj) && 
insilmaril@394
   905
					 typeid(*selection) != typeid(MapCenterObj)) )
insilmaril@394
   906
		{				  
insilmaril@394
   907
			api.setError (Aborted,"Type of selection is not a branch");
insilmaril@394
   908
		} else if (api.checkParamCount(1))
insilmaril@143
   909
		{
insilmaril@143
   910
			s=api.parString (ok,0);
insilmaril@366
   911
			if (ok) setVymLinkInt(s);
insilmaril@143
   912
		}	
insilmaril@143
   913
	}
insilmaril@366
   914
	else if (com=="setFlag")
insilmaril@366
   915
	{
insilmaril@394
   916
		if (!selection)
insilmaril@366
   917
		{
insilmaril@394
   918
			api.setError (Aborted,"Nothing selected");
insilmaril@394
   919
		} else if ( (typeid(*selection) != typeid(BranchObj) && 
insilmaril@394
   920
					 typeid(*selection) != typeid(MapCenterObj)) )
insilmaril@394
   921
		{				  
insilmaril@394
   922
			api.setError (Aborted,"Type of selection is not a branch");
insilmaril@394
   923
		} else if (api.checkParamCount(1))
insilmaril@394
   924
		{
insilmaril@394
   925
			s=api.parString(ok,0);
insilmaril@394
   926
			if (ok) 
insilmaril@394
   927
			{
insilmaril@394
   928
				BranchObj* bo=(BranchObj*)selection;
insilmaril@394
   929
				bo->activateStandardFlag(s);
insilmaril@394
   930
				bo->updateFlagsToolbar();
insilmaril@366
   931
			}	
insilmaril@366
   932
		}
insilmaril@366
   933
	}	
insilmaril@366
   934
	else if (com=="unsetFlag")
insilmaril@366
   935
	{
insilmaril@394
   936
		if (!selection)
insilmaril@366
   937
		{
insilmaril@394
   938
			api.setError (Aborted,"Nothing selected");
insilmaril@394
   939
		} else if ( (typeid(*selection) != typeid(BranchObj) && 
insilmaril@394
   940
					 typeid(*selection) != typeid(MapCenterObj)) )
insilmaril@394
   941
		{				  
insilmaril@394
   942
			api.setError (Aborted,"Type of selection is not a branch");
insilmaril@394
   943
		} else if (api.checkParamCount(1))
insilmaril@394
   944
		{
insilmaril@394
   945
			s=api.parString(ok,0);
insilmaril@394
   946
			if (ok) 
insilmaril@394
   947
			{
insilmaril@394
   948
				BranchObj* bo=(BranchObj*)selection;
insilmaril@394
   949
				bo->deactivateStandardFlag(s);
insilmaril@394
   950
				bo->updateFlagsToolbar();
insilmaril@366
   951
			}	
insilmaril@366
   952
		}
insilmaril@398
   953
	} else
insilmaril@163
   954
	{
insilmaril@394
   955
		api.setError (Aborted,"Unknown command");
insilmaril@163
   956
	}
insilmaril@129
   957
insilmaril@129
   958
	// Any errors?
insilmaril@394
   959
	if (api.errorLevel()==NoError)
insilmaril@394
   960
		setChanged();
insilmaril@394
   961
	else	
insilmaril@106
   962
	{
insilmaril@366
   963
		// TODO Error handling
insilmaril@366
   964
		qWarning("MapEditor::parseAtom: Error!");
insilmaril@394
   965
		qWarning(api.errorMessage());
insilmaril@394
   966
	} 
insilmaril@0
   967
}
insilmaril@0
   968
insilmaril@0
   969
insilmaril@0
   970
bool MapEditor::isDefault()
insilmaril@0
   971
{
insilmaril@0
   972
    return mapDefault;
insilmaril@0
   973
}
insilmaril@0
   974
insilmaril@0
   975
bool MapEditor::isUnsaved()
insilmaril@0
   976
{
insilmaril@0
   977
    return mapUnsaved;
insilmaril@0
   978
}
insilmaril@0
   979
insilmaril@0
   980
bool MapEditor::hasChanged()
insilmaril@0
   981
{
insilmaril@0
   982
    return mapChanged;
insilmaril@0
   983
}
insilmaril@0
   984
insilmaril@0
   985
void MapEditor::setChanged()
insilmaril@0
   986
{
insilmaril@0
   987
	mapChanged=true;
insilmaril@0
   988
	mapDefault=false;
insilmaril@0
   989
	mapUnsaved=true;
insilmaril@0
   990
	findReset();
insilmaril@0
   991
}
insilmaril@0
   992
insilmaril@0
   993
void MapEditor::closeMap()
insilmaril@0
   994
{
insilmaril@0
   995
	// Unselect before disabling the toolbar actions
insilmaril@0
   996
	if (selection) selection->unselect();
insilmaril@0
   997
	selection=NULL;
insilmaril@0
   998
	updateActions();
insilmaril@0
   999
insilmaril@0
  1000
    clear();
insilmaril@0
  1001
	close();
insilmaril@0
  1002
}
insilmaril@0
  1003
insilmaril@0
  1004
void MapEditor::setFilePath(QString fname)
insilmaril@0
  1005
{
insilmaril@0
  1006
	setFilePath (fname,fname);
insilmaril@0
  1007
}
insilmaril@0
  1008
insilmaril@0
  1009
void MapEditor::setFilePath(QString fname, QString destname)
insilmaril@0
  1010
{
insilmaril@205
  1011
	if (fname.isEmpty() || fname=="")
insilmaril@106
  1012
	{
insilmaril@106
  1013
		filePath="";
insilmaril@106
  1014
		fileName="";
insilmaril@106
  1015
		destPath="";
insilmaril@106
  1016
	} else
insilmaril@106
  1017
	{
insilmaril@106
  1018
		filePath=fname;		// becomes absolute path
insilmaril@106
  1019
		fileName=fname;		// gets stripped of path
insilmaril@106
  1020
		destPath=destname;	// needed for vymlinks
insilmaril@106
  1021
insilmaril@106
  1022
		// If fname is not an absolute path, complete it
insilmaril@106
  1023
		filePath=QDir(fname).absPath();
insilmaril@106
  1024
		fileDir=filePath.left (1+filePath.findRev ("/"));
insilmaril@106
  1025
insilmaril@106
  1026
		// Set short name, too. Search from behind:
insilmaril@106
  1027
		int i=fileName.findRev("/");
insilmaril@106
  1028
		if (i>=0) fileName=fileName.remove (0,i+1);
insilmaril@106
  1029
insilmaril@106
  1030
		// Forget the .vym (or .xml) for name of map
insilmaril@106
  1031
		mapName=fileName.left(fileName.findRev(".",-1,true) );
insilmaril@106
  1032
	}
insilmaril@0
  1033
}
insilmaril@0
  1034
insilmaril@0
  1035
QString MapEditor::getFilePath()
insilmaril@0
  1036
{
insilmaril@0
  1037
	return filePath;
insilmaril@0
  1038
}
insilmaril@0
  1039
insilmaril@0
  1040
QString MapEditor::getFileName()
insilmaril@0
  1041
{
insilmaril@0
  1042
	return fileName;
insilmaril@0
  1043
}
insilmaril@0
  1044
insilmaril@0
  1045
QString MapEditor::getMapName()
insilmaril@0
  1046
{
insilmaril@0
  1047
	return mapName;
insilmaril@0
  1048
}
insilmaril@0
  1049
insilmaril@0
  1050
QString MapEditor::getDestPath()
insilmaril@0
  1051
{
insilmaril@0
  1052
	return destPath;
insilmaril@0
  1053
}
insilmaril@0
  1054
insilmaril@205
  1055
ErrorCode MapEditor::load (QString fname, LoadMode lmode)
insilmaril@0
  1056
{
insilmaril@205
  1057
	ErrorCode err=success;
insilmaril@0
  1058
insilmaril@0
  1059
	if (lmode==NewMap)
insilmaril@0
  1060
	{
insilmaril@0
  1061
		if (selection) selection->unselect();
insilmaril@0
  1062
		selection=NULL;
insilmaril@0
  1063
		mapCenter->clear();
insilmaril@0
  1064
		mapCenter->setMapEditor(this);
insilmaril@0
  1065
		// (map state is set later at end of load...)
insilmaril@0
  1066
	} else
insilmaril@0
  1067
	{
insilmaril@395
  1068
		if (!selection || (typeid(*selection) != typeid(BranchObj) &&
insilmaril@395
  1069
			typeid(*selection) != typeid (MapCenterObj)))
insilmaril@395
  1070
			return aborted;
insilmaril@395
  1071
		BranchObj *bo=(BranchObj*)selection;	
insilmaril@395
  1072
		if (lmode==ImportAdd)
insilmaril@395
  1073
			saveStateChangingPart(
insilmaril@395
  1074
				selection,
insilmaril@395
  1075
				selection,
insilmaril@395
  1076
				QString("addMapInsert (%1)").arg(fname),
insilmaril@395
  1077
				QString("Add map %1 to %2").arg(fname).arg(getName(bo)));
insilmaril@395
  1078
		else	
insilmaril@395
  1079
			saveStateChangingPart(
insilmaril@395
  1080
				selection,
insilmaril@395
  1081
				selection,
insilmaril@395
  1082
				QString("addMapReplace(%1)").arg(fname),
insilmaril@395
  1083
				QString("Add map %1 to %2").arg(fname).arg(getName(bo)));
insilmaril@0
  1084
	}	
insilmaril@0
  1085
	
insilmaril@0
  1086
    
insilmaril@0
  1087
    mapBuilderHandler handler;
insilmaril@0
  1088
	QFile file( fname );
insilmaril@0
  1089
insilmaril@0
  1090
	// I am paranoid: file should exist anyway
insilmaril@0
  1091
	// according to check in mainwindow.
insilmaril@0
  1092
	if (!file.exists() )
insilmaril@0
  1093
	{
insilmaril@0
  1094
		QMessageBox::critical( 0, tr( "Critical Parse Error" ),
insilmaril@0
  1095
				   tr("Couldn't open map " +fname)+".");
insilmaril@205
  1096
		err=aborted;	
insilmaril@0
  1097
	} else
insilmaril@0
  1098
	{
insilmaril@139
  1099
		blockReposition=true;
insilmaril@0
  1100
		QXmlInputSource source( file);
insilmaril@0
  1101
		QXmlSimpleReader reader;
insilmaril@0
  1102
		reader.setContentHandler( &handler );
insilmaril@0
  1103
		reader.setErrorHandler( &handler );
insilmaril@0
  1104
		handler.setMapEditor( this );
insilmaril@0
  1105
		handler.setTmpDir (filePath.left(filePath.findRev("/",-1)));	// needed to load files with rel. path
insilmaril@243
  1106
		handler.setInputFile (file.name());
insilmaril@0
  1107
		handler.setLoadMode (lmode);
insilmaril@163
  1108
		blockSaveState=true;
insilmaril@0
  1109
		bool ok = reader.parse( source );
insilmaril@139
  1110
		blockReposition=false;
insilmaril@163
  1111
		blockSaveState=false;
insilmaril@0
  1112
		file.close();
insilmaril@0
  1113
		if ( ok ) 
insilmaril@0
  1114
		{
insilmaril@0
  1115
			mapCenter->reposition();
insilmaril@0
  1116
			adjustCanvasSize();
insilmaril@0
  1117
			if (lmode==NewMap)
insilmaril@0
  1118
			{
insilmaril@0
  1119
				mapDefault=false;
insilmaril@0
  1120
				mapChanged=false;
insilmaril@0
  1121
				mapUnsaved=false;
insilmaril@0
  1122
			}
insilmaril@0
  1123
		} else 
insilmaril@0
  1124
		{
insilmaril@0
  1125
			QMessageBox::critical( 0, tr( "Critical Parse Error" ),
insilmaril@0
  1126
					   tr( handler.errorProtocol() ) );
insilmaril@0
  1127
			// returnCode=1;	
insilmaril@0
  1128
			// Still return "success": the map maybe at least
insilmaril@0
  1129
			// partially read by the parser
insilmaril@0
  1130
		}	
insilmaril@0
  1131
	}	
insilmaril@0
  1132
	updateActions();
insilmaril@205
  1133
	return err;
insilmaril@0
  1134
}
insilmaril@0
  1135
insilmaril@0
  1136
int MapEditor::save (const SaveMode &savemode)
insilmaril@0
  1137
{
insilmaril@0
  1138
	int returnCode=0;
insilmaril@0
  1139
insilmaril@0
  1140
	// Create mapName and fileDir
insilmaril@0
  1141
	makeSubDirs (fileDir);
insilmaril@0
  1142
	QString fname;
insilmaril@0
  1143
	if (saveZipped())
insilmaril@0
  1144
		// save as .xml
insilmaril@0
  1145
		fname=mapName+".xml";
insilmaril@0
  1146
	else
insilmaril@0
  1147
		// use name given by user, even if he chooses .doc
insilmaril@0
  1148
		fname=fileName;
insilmaril@0
  1149
insilmaril@0
  1150
insilmaril@0
  1151
	QString saveFile;
insilmaril@106
  1152
	if (savemode==CompleteMap || selection==NULL)
insilmaril@408
  1153
		saveFile=saveToDir (fileDir,mapName+"-",true,QPointF(),NULL);
insilmaril@106
  1154
	else	
insilmaril@408
  1155
		saveFile=saveToDir (fileDir,mapName+"-",true,QPointF(),selection);
insilmaril@0
  1156
insilmaril@163
  1157
	if (!saveStringToDisk(fileDir+fname,saveFile))
insilmaril@0
  1158
		return 1;
insilmaril@0
  1159
insilmaril@0
  1160
	if (returnCode==0)
insilmaril@0
  1161
	{
insilmaril@0
  1162
		mapChanged=false;
insilmaril@0
  1163
		mapUnsaved=false;
insilmaril@390
  1164
		updateActions();
insilmaril@0
  1165
	}
insilmaril@0
  1166
insilmaril@0
  1167
	return returnCode;
insilmaril@0
  1168
}
insilmaril@0
  1169
insilmaril@0
  1170
void MapEditor::setZipped (bool z)
insilmaril@0
  1171
{
insilmaril@0
  1172
	zipped=z;
insilmaril@0
  1173
}
insilmaril@0
  1174
insilmaril@0
  1175
bool MapEditor::saveZipped ()
insilmaril@0
  1176
{
insilmaril@0
  1177
	return zipped;
insilmaril@0
  1178
}
insilmaril@0
  1179
insilmaril@0
  1180
void MapEditor::print()
insilmaril@0
  1181
{
insilmaril@0
  1182
	if ( !printer ) 
insilmaril@0
  1183
	{
insilmaril@0
  1184
		printer = new QPrinter;
insilmaril@0
  1185
		printer->setColorMode (QPrinter::Color);
insilmaril@366
  1186
		printer->setPrinterName (settings.value("/mainwindow/printerName",printer->printerName()).toString());
insilmaril@0
  1187
	}
insilmaril@0
  1188
insilmaril@408
  1189
	QRectF totalBBox=mapCenter->getTotalBBox();
insilmaril@0
  1190
insilmaril@0
  1191
	// Try to set orientation automagically
insilmaril@0
  1192
	// Note: Interpretation of generated postscript is amibiguous, if 
insilmaril@0
  1193
	// there are problems with landscape mode, see
insilmaril@0
  1194
	// http://sdb.suse.de/de/sdb/html/jsmeix_print-cups-landscape-81.html
insilmaril@0
  1195
insilmaril@0
  1196
	if (totalBBox.width()>totalBBox.height())
insilmaril@0
  1197
		// recommend landscape
insilmaril@0
  1198
		printer->setOrientation (QPrinter::Landscape);
insilmaril@0
  1199
	else	
insilmaril@0
  1200
		// recommend portrait
insilmaril@0
  1201
		printer->setOrientation (QPrinter::Portrait);
insilmaril@0
  1202
insilmaril@0
  1203
	if ( printer->setup(this) ) 
insilmaril@0
  1204
	// returns false, if printing is canceled
insilmaril@0
  1205
	{
insilmaril@0
  1206
		QPainter pp(printer);
insilmaril@0
  1207
insilmaril@0
  1208
		// Don't print the visualisation of selection
insilmaril@0
  1209
		LinkableMapObj *oldselection=NULL;
insilmaril@0
  1210
		if (selection) 
insilmaril@0
  1211
		{
insilmaril@0
  1212
			oldselection=selection;
insilmaril@0
  1213
			selection->unselect();
insilmaril@0
  1214
		}
insilmaril@0
  1215
insilmaril@0
  1216
		// Handle sizes of map and paper:
insilmaril@0
  1217
		//
insilmaril@408
  1218
		// setWindow defines which part of the scene will be transformed 
insilmaril@0
  1219
		// setViewport defines area on paper in device coordinates (dpi)
insilmaril@0
  1220
		// e.g. (0,50,700,700) is upper part on A4
insilmaril@0
  1221
		// see also /usr/lib/qt3/doc/html/coordsys.html
insilmaril@0
  1222
insilmaril@408
  1223
		Q3PaintDeviceMetrics metrics (printer);
insilmaril@408
  1224
insilmaril@408
  1225
		double paperAspect = (double)metrics.width()   / (double)metrics.height();
insilmaril@0
  1226
		double   mapAspect = (double)totalBBox.width() / (double)totalBBox.height();
insilmaril@0
  1227
insilmaril@408
  1228
		QRectF mapRect=totalBBox;
insilmaril@408
  1229
/* FIXME todo for QT4
insilmaril@366
  1230
		Q3CanvasRectangle *frame=NULL;
insilmaril@366
  1231
		Q3CanvasText *footerFN=NULL;
insilmaril@366
  1232
		Q3CanvasText *footerDate=NULL;
insilmaril@0
  1233
		if (printFrame || printFooter)
insilmaril@0
  1234
		{
insilmaril@0
  1235
			
insilmaril@0
  1236
			if (printFrame) 
insilmaril@0
  1237
			{
insilmaril@0
  1238
				// Print frame around map
insilmaril@102
  1239
				mapRect.setRect (totalBBox.x()-10, totalBBox.y()-10, 
insilmaril@102
  1240
					totalBBox.width()+20, totalBBox.height()+20);
insilmaril@408
  1241
				frame=new Q3CanvasRectangle (mapRect,mapScene);
insilmaril@366
  1242
				frame->setBrush (QColor(Qt::white));
insilmaril@366
  1243
				frame->setPen (QColor(Qt::black));
insilmaril@0
  1244
				frame->setZ(0);
insilmaril@0
  1245
				frame->show();    
insilmaril@0
  1246
			}		
insilmaril@408
  1247
			// TODO remove after testing 
insilmaril@408
  1248
			//QCanvasLine *l=new QCanvasLine (mapScene);
insilmaril@408
  1249
			//l->setPoints (0,0,mapRect.width(),mapRect.height());
insilmaril@408
  1250
			//l->setPen (QPen(QColor(black), 1));
insilmaril@408
  1251
			//l->setZ (200);
insilmaril@408
  1252
			//l->show();
insilmaril@102
  1253
insilmaril@0
  1254
			if (printFooter) 
insilmaril@0
  1255
			{
insilmaril@0
  1256
				// Print footer below map
insilmaril@0
  1257
				QFont font;		
insilmaril@0
  1258
				font.setPointSize(10);
insilmaril@408
  1259
				footerFN=new Q3CanvasText (mapScene);
insilmaril@0
  1260
				footerFN->setText ("VYM - " + fileName);
insilmaril@0
  1261
				footerFN->setFont(font);
insilmaril@0
  1262
				footerFN->move (mapRect.x(), mapRect.y() + mapRect.height() );
insilmaril@0
  1263
				footerFN->setZ(Z_TEXT);
insilmaril@0
  1264
				footerFN->show();    
insilmaril@408
  1265
				footerDate=new Q3CanvasText (mapScene);
insilmaril@0
  1266
				footerDate->setText (QDate::currentDate().toString(Qt::TextDate));
insilmaril@0
  1267
				footerDate->setFont(font);
insilmaril@0
  1268
				footerDate->move (mapRect.x()+mapRect.width()-footerDate->boundingRect().width(), mapRect.y() + mapRect.height() );
insilmaril@0
  1269
				footerDate->setZ(Z_TEXT);
insilmaril@0
  1270
				footerDate->show();    
insilmaril@0
  1271
			}
insilmaril@102
  1272
			pp.setWindow (mapRect.x(), mapRect.y(), mapRect.width(), mapRect.height()+20);
insilmaril@0
  1273
		}	else	
insilmaril@0
  1274
		{
insilmaril@0
  1275
			pp.setWindow (mapRect);
insilmaril@0
  1276
		}	
insilmaril@408
  1277
*/
insilmaril@0
  1278
insilmaril@0
  1279
		if (mapAspect>=paperAspect)
insilmaril@0
  1280
		{
insilmaril@0
  1281
			// Fit horizontally to paper width
insilmaril@408
  1282
			pp.setViewport(0,0, metrics.width(),(int)(metrics.width()/mapAspect) );	
insilmaril@0
  1283
		}	else
insilmaril@0
  1284
		{
insilmaril@0
  1285
			// Fit vertically to paper height
insilmaril@408
  1286
			pp.setViewport(0,0,(int)(metrics.height()*mapAspect),metrics.height());	
insilmaril@0
  1287
		}	
insilmaril@0
  1288
insilmaril@408
  1289
		mapScene->render(&pp);	// draw scene to printer
insilmaril@408
  1290
insilmaril@408
  1291
/*
insilmaril@0
  1292
		// Delete Frame and footer
insilmaril@0
  1293
		if (footerFN) 
insilmaril@0
  1294
		{
insilmaril@0
  1295
			delete (footerFN);
insilmaril@0
  1296
			delete (footerDate);
insilmaril@0
  1297
		}	
insilmaril@0
  1298
		if (frame)  delete (frame);
insilmaril@408
  1299
*/		
insilmaril@0
  1300
insilmaril@0
  1301
		// Restore selection
insilmaril@0
  1302
		if (oldselection) 
insilmaril@0
  1303
		{
insilmaril@0
  1304
			selection=oldselection;
insilmaril@0
  1305
			selection->select();
insilmaril@0
  1306
		}	
insilmaril@119
  1307
insilmaril@119
  1308
		// Save settings in vymrc
insilmaril@366
  1309
		settings.writeEntry("/mainwindow/printerName",printer->printerName());
insilmaril@0
  1310
	}
insilmaril@0
  1311
}
insilmaril@0
  1312
insilmaril@408
  1313
void MapEditor::setAntiAlias (bool b)
insilmaril@408
  1314
{
insilmaril@408
  1315
	setRenderHint(QPainter::Antialiasing,b);
insilmaril@408
  1316
}
insilmaril@408
  1317
insilmaril@408
  1318
void MapEditor::setSmoothPixmap(bool b)
insilmaril@408
  1319
{
insilmaril@408
  1320
	setRenderHint(QPainter::SmoothPixmapTransform,b);
insilmaril@408
  1321
}
insilmaril@408
  1322
insilmaril@0
  1323
QPixmap MapEditor::getPixmap()
insilmaril@0
  1324
{
insilmaril@408
  1325
/* FIXME not ported yet to QT4
insilmaril@408
  1326
	QRectF mapRect=mapCenter->getTotalBBox();
insilmaril@0
  1327
	QPixmap pix (mapRect.size());
insilmaril@0
  1328
	QPainter pp (&pix);
insilmaril@0
  1329
insilmaril@0
  1330
	// Don't print the visualisation of selection
insilmaril@0
  1331
	LinkableMapObj *oldselection=NULL;
insilmaril@0
  1332
	if (selection) 
insilmaril@0
  1333
	{
insilmaril@0
  1334
		oldselection=selection;
insilmaril@0
  1335
		selection->unselect();
insilmaril@0
  1336
	}
insilmaril@0
  1337
insilmaril@0
  1338
	pp.setWindow (mapRect);
insilmaril@0
  1339
insilmaril@408
  1340
	mapScene->drawArea(mapRect, &pp);	// draw scene to painter
insilmaril@0
  1341
insilmaril@0
  1342
insilmaril@0
  1343
	// Restore selection
insilmaril@0
  1344
	if (oldselection) 
insilmaril@0
  1345
	{
insilmaril@0
  1346
		selection=oldselection;
insilmaril@0
  1347
		selection->select();
insilmaril@0
  1348
	}	
insilmaril@0
  1349
	
insilmaril@0
  1350
	return pix;
insilmaril@408
  1351
*/
insilmaril@408
  1352
return QPixmap();
insilmaril@0
  1353
}
insilmaril@0
  1354
insilmaril@260
  1355
void MapEditor::setHideTmpMode (HideTmpMode mode)
insilmaril@260
  1356
{
insilmaril@260
  1357
	hidemode=mode;
insilmaril@260
  1358
	mapCenter->setHideTmp (hidemode);
insilmaril@260
  1359
	mapCenter->reposition();
insilmaril@260
  1360
	adjustCanvasSize();
insilmaril@408
  1361
	scene()->update();
insilmaril@260
  1362
}
insilmaril@260
  1363
insilmaril@260
  1364
HideTmpMode MapEditor::getHideTmpMode()
insilmaril@260
  1365
{
insilmaril@260
  1366
	return hidemode;
insilmaril@260
  1367
}
insilmaril@260
  1368
insilmaril@0
  1369
void MapEditor::exportImage(QString fn)
insilmaril@0
  1370
{
insilmaril@260
  1371
	setExportMode (true);
insilmaril@0
  1372
	QPixmap pix (getPixmap());
insilmaril@0
  1373
	pix.save(fn, "PNG");
insilmaril@260
  1374
	setExportMode (false);
insilmaril@260
  1375
}
insilmaril@260
  1376
insilmaril@260
  1377
void MapEditor::setExportMode (bool b)
insilmaril@260
  1378
{
insilmaril@260
  1379
	// should be called before and after exports
insilmaril@260
  1380
	// depending on the settings
insilmaril@366
  1381
	if (b && settings.value("/export/useHideExport","yes")=="yes")
insilmaril@260
  1382
		setHideTmpMode (HideExport);
insilmaril@260
  1383
	else	
insilmaril@260
  1384
		setHideTmpMode (HideNone);
insilmaril@0
  1385
}
insilmaril@0
  1386
insilmaril@366
  1387
void MapEditor::exportImage(QString fn, QString format)
insilmaril@0
  1388
{
insilmaril@260
  1389
	setExportMode (true);
insilmaril@0
  1390
	QPixmap pix (getPixmap());
insilmaril@366
  1391
	pix.save(fn, format);
insilmaril@260
  1392
	setExportMode (false);
insilmaril@0
  1393
}
insilmaril@0
  1394
insilmaril@185
  1395
void MapEditor::exportOOPresentation(const QString &fn, const QString &cf)
insilmaril@163
  1396
{
insilmaril@185
  1397
	ExportOO ex;
insilmaril@199
  1398
	ex.setFile (fn);
insilmaril@185
  1399
	ex.setMapCenter(mapCenter);
insilmaril@260
  1400
	if (ex.setConfigFile(cf)) 
insilmaril@260
  1401
	{
insilmaril@260
  1402
		setExportMode (true);
insilmaril@260
  1403
		ex.exportPresentation();
insilmaril@260
  1404
		setExportMode (false);
insilmaril@260
  1405
	}
insilmaril@163
  1406
}
insilmaril@163
  1407
insilmaril@163
  1408
insilmaril@0
  1409
insilmaril@0
  1410
void MapEditor::exportXML(const QString &dir)
insilmaril@0
  1411
{
insilmaril@260
  1412
	// Hide stuff during export, if settings want this
insilmaril@260
  1413
	setExportMode (true);
insilmaril@260
  1414
insilmaril@0
  1415
	// Create subdirectories
insilmaril@0
  1416
	makeSubDirs (dir);
insilmaril@0
  1417
insilmaril@0
  1418
	// write to directory
insilmaril@106
  1419
	QString saveFile=saveToDir (dir,mapName+"-",true,mapCenter->getTotalBBox().topLeft() ,NULL);
insilmaril@0
  1420
	QFile file;
insilmaril@0
  1421
insilmaril@0
  1422
	file.setName ( dir + "/"+mapName+".xml");
insilmaril@366
  1423
	if ( !file.open( QIODevice::WriteOnly ) )
insilmaril@0
  1424
	{
insilmaril@0
  1425
		// This should neverever happen
insilmaril@159
  1426
		QMessageBox::critical (0,tr("Critical Export Error"),tr("MapEditor::exportXML couldn't open %1").arg(file.name()));
insilmaril@0
  1427
		return;
insilmaril@0
  1428
	}	
insilmaril@0
  1429
insilmaril@0
  1430
	// Write it finally, and write in UTF8, no matter what 
insilmaril@0
  1431
	QTextStream ts( &file );
insilmaril@0
  1432
	ts.setEncoding (QTextStream::UnicodeUTF8);
insilmaril@0
  1433
	ts << saveFile;
insilmaril@0
  1434
	file.close();
insilmaril@0
  1435
insilmaril@0
  1436
	// Now write image, too
insilmaril@0
  1437
	exportImage (dir+"/images/"+mapName+".png");
insilmaril@260
  1438
insilmaril@260
  1439
	setExportMode (false);
insilmaril@0
  1440
}
insilmaril@0
  1441
insilmaril@0
  1442
void MapEditor::clear()
insilmaril@0
  1443
{
insilmaril@0
  1444
	if (selection)
insilmaril@0
  1445
	{
insilmaril@0
  1446
		selection->unselect();
insilmaril@0
  1447
		selection=NULL;
insilmaril@0
  1448
	}	
insilmaril@0
  1449
insilmaril@0
  1450
	mapCenter->clear();
insilmaril@0
  1451
}
insilmaril@0
  1452
insilmaril@0
  1453
void MapEditor::copy()
insilmaril@0
  1454
{
insilmaril@0
  1455
	if (selection) 
insilmaril@0
  1456
	{
insilmaril@166
  1457
		// write to directory
insilmaril@166
  1458
		QString clipfile="part";
insilmaril@408
  1459
		QString saveFile=saveToDir (fileDir,clipfile+"-",true,QPointF(),selection);
insilmaril@166
  1460
		QFile file;
insilmaril@166
  1461
insilmaril@166
  1462
		file.setName ( clipboardDir + "/"+clipfile+".xml");
insilmaril@366
  1463
		if ( !file.open( QIODevice::WriteOnly ) )
insilmaril@0
  1464
		{
insilmaril@166
  1465
			// This should neverever happen
insilmaril@166
  1466
			QMessageBox::critical (0,tr("Critical Export Error"),tr("MapEditor::exportXML couldn't open %1").arg(file.name()));
insilmaril@166
  1467
			return;
insilmaril@166
  1468
		}	
insilmaril@166
  1469
insilmaril@166
  1470
		// Write it finally, and write in UTF8, no matter what 
insilmaril@166
  1471
		QTextStream ts( &file );
insilmaril@166
  1472
		ts.setEncoding (QTextStream::UnicodeUTF8);
insilmaril@166
  1473
		ts << saveFile;
insilmaril@166
  1474
		file.close();
insilmaril@166
  1475
insilmaril@166
  1476
		clipboardEmpty=false;
insilmaril@166
  1477
		updateActions();
insilmaril@0
  1478
	}	    
insilmaril@0
  1479
}
insilmaril@0
  1480
insilmaril@163
  1481
void MapEditor::redo()
insilmaril@163
  1482
{
insilmaril@163
  1483
	blockSaveState=true;
insilmaril@163
  1484
	
insilmaril@366
  1485
	// Restore variables
insilmaril@366
  1486
	int curStep=undoSet.readNumEntry (QString("/history/curStep"));
insilmaril@366
  1487
	int undosAvail=undoSet.readNumEntry (QString("/history/undosAvail"));
insilmaril@366
  1488
	int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
insilmaril@381
  1489
	// Can we undo at all?
insilmaril@381
  1490
	if (redosAvail<1) return;
insilmaril@381
  1491
	redosAvail--;
insilmaril@381
  1492
insilmaril@381
  1493
	if (undosAvail<stepsTotal) undosAvail++;
insilmaril@366
  1494
	curStep++;
insilmaril@381
  1495
	if (curStep>stepsTotal) curStep=1;
insilmaril@366
  1496
	QString undoCommand=  undoSet.readEntry (QString("/history/step-%1/undoCommand").arg(curStep));
insilmaril@366
  1497
	QString undoSelection=undoSet.readEntry (QString("/history/step-%1/undoSelection").arg(curStep));
insilmaril@366
  1498
	QString redoCommand=  undoSet.readEntry (QString("/history/step-%1/redoCommand").arg(curStep));
insilmaril@366
  1499
	QString redoSelection=undoSet.readEntry (QString("/history/step-%1/redoSelection").arg(curStep));
insilmaril@366
  1500
	QString comment=undoSet.readEntry (QString("/history/step-%1/comment").arg(curStep));
insilmaril@395
  1501
	QString version=undoSet.readEntry ("/history/version");
insilmaril@395
  1502
insilmaril@395
  1503
	if (!checkVersion(version))
insilmaril@395
  1504
		QMessageBox::warning(0,tr("Warning"),
insilmaril@404
  1505
			tr("Version %1 of saved undo/redo data\ndoes not match current vym version %2.").arg(version).arg(vymVersion));
insilmaril@395
  1506
insilmaril@366
  1507
insilmaril@163
  1508
	// Find out current undo directory
insilmaril@366
  1509
	QString bakMapDir=QDir::convertSeparators (QString(tmpMapDir+"/undo-%1").arg(curStep));
insilmaril@366
  1510
insilmaril@366
  1511
/* TODO remove testing
insilmaril@366
  1512
*/
insilmaril@366
  1513
	cout << "ME::redo() begin\n";
insilmaril@366
  1514
	cout << "    undosAvail="<<undosAvail<<endl;
insilmaril@366
  1515
	cout << "    redosAvail="<<redosAvail<<endl;
insilmaril@366
  1516
	cout << "       curStep="<<curStep<<endl;
insilmaril@366
  1517
	cout << "    ---------------------------"<<endl;
insilmaril@366
  1518
	cout << "    comment="<<comment.toStdString()<<endl;
insilmaril@366
  1519
	cout << "    undoCom="<<undoCommand.toStdString()<<endl;
insilmaril@366
  1520
	cout << "    undoSel="<<undoSelection.toStdString()<<endl;
insilmaril@366
  1521
	cout << "    redoCom="<<redoCommand.toStdString()<<endl;
insilmaril@366
  1522
	cout << "    redoSel="<<redoSelection.toStdString()<<endl;
insilmaril@366
  1523
	cout << "    ---------------------------"<<endl<<endl;
insilmaril@163
  1524
insilmaril@163
  1525
	// select  object before redo
insilmaril@163
  1526
	if (!redoSelection.isEmpty())
insilmaril@163
  1527
		select (redoSelection);
insilmaril@163
  1528
insilmaril@366
  1529
insilmaril@366
  1530
	parseAtom (redoCommand);
insilmaril@366
  1531
	mapCenter->reposition();
insilmaril@366
  1532
insilmaril@366
  1533
	blockSaveState=false;
insilmaril@366
  1534
insilmaril@366
  1535
	undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
insilmaril@366
  1536
	undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
insilmaril@366
  1537
	undoSet.setEntry ("/history/curStep",QString::number(curStep));
insilmaril@366
  1538
	undoSet.writeSettings(histPath);
insilmaril@366
  1539
insilmaril@408
  1540
	mainWindow->updateHistory (undoSet);
insilmaril@407
  1541
	updateActions();
insilmaril@366
  1542
insilmaril@366
  1543
	/* TODO remove testing
insilmaril@163
  1544
	cout << "ME::redo() end\n";
insilmaril@163
  1545
	cout << "    undosAvail="<<undosAvail<<endl;
insilmaril@366
  1546
	cout << "    redosAvail="<<redosAvail<<endl;
insilmaril@366
  1547
	cout << "       curStep="<<curStep<<endl;
insilmaril@163
  1548
	cout << "    ---------------------------"<<endl<<endl;
insilmaril@404
  1549
	*/
insilmaril@366
  1550
insilmaril@366
  1551
insilmaril@163
  1552
}
insilmaril@163
  1553
insilmaril@390
  1554
bool MapEditor::isRedoAvailable()
insilmaril@390
  1555
{
insilmaril@393
  1556
	if (undoSet.readNumEntry("/history/redosAvail",0)>0)
insilmaril@390
  1557
		return true;
insilmaril@390
  1558
	else	
insilmaril@390
  1559
		return false;
insilmaril@390
  1560
}
insilmaril@390
  1561
insilmaril@106
  1562
void MapEditor::undo()
insilmaril@106
  1563
{
insilmaril@163
  1564
	blockSaveState=true;
insilmaril@163
  1565
	
insilmaril@366
  1566
	// Restore variables
insilmaril@366
  1567
	int curStep=undoSet.readNumEntry (QString("/history/curStep"));
insilmaril@366
  1568
	int undosAvail=undoSet.readNumEntry (QString("/history/undosAvail"));
insilmaril@366
  1569
	int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
insilmaril@381
  1570
insilmaril@381
  1571
	// Can we undo at all?
insilmaril@381
  1572
	if (undosAvail<1) return;
insilmaril@381
  1573
insilmaril@366
  1574
	QString undoCommand=  undoSet.readEntry (QString("/history/step-%1/undoCommand").arg(curStep));
insilmaril@366
  1575
	QString undoSelection=undoSet.readEntry (QString("/history/step-%1/undoSelection").arg(curStep));
insilmaril@366
  1576
	QString redoCommand=  undoSet.readEntry (QString("/history/step-%1/redoCommand").arg(curStep));
insilmaril@366
  1577
	QString redoSelection=undoSet.readEntry (QString("/history/step-%1/redoSelection").arg(curStep));
insilmaril@366
  1578
	QString comment=undoSet.readEntry (QString("/history/step-%1/comment").arg(curStep));
insilmaril@395
  1579
	QString version=undoSet.readEntry ("/history/version");
insilmaril@395
  1580
insilmaril@395
  1581
	if (!checkVersion(version))
insilmaril@395
  1582
		QMessageBox::warning(0,tr("Warning"),
insilmaril@404
  1583
			tr("Version %1 of saved undo/redo data\ndoes not match current vym version %2.").arg(version).arg(vymVersion));
insilmaril@366
  1584
insilmaril@163
  1585
	// Find out current undo directory
insilmaril@366
  1586
	QString bakMapDir=QDir::convertSeparators (QString(tmpMapDir+"/undo-%1").arg(curStep));
insilmaril@163
  1587
insilmaril@136
  1588
	// select  object before undo
insilmaril@136
  1589
	if (!undoSelection.isEmpty())
insilmaril@136
  1590
		select (undoSelection);
insilmaril@136
  1591
insilmaril@164
  1592
/* TODO testing
insilmaril@366
  1593
*/	
insilmaril@163
  1594
	cout << "ME::undo() begin\n";
insilmaril@163
  1595
	cout << "    undosAvail="<<undosAvail<<endl;
insilmaril@366
  1596
	cout << "    redosAvail="<<redosAvail<<endl;
insilmaril@366
  1597
	cout << "       curStep="<<curStep<<endl;
insilmaril@163
  1598
	cout << "    ---------------------------"<<endl;
insilmaril@366
  1599
	cout << "    comment="<<comment.toStdString()<<endl;
insilmaril@366
  1600
	cout << "    undoCom="<<undoCommand.toStdString()<<endl;
insilmaril@366
  1601
	cout << "    undoSel="<<undoSelection.toStdString()<<endl;
insilmaril@366
  1602
	cout << "    redoCom="<<redoCommand.toStdString()<<endl;
insilmaril@366
  1603
	cout << "    redoSel="<<redoSelection.toStdString()<<endl;
insilmaril@366
  1604
	cout << "    ---------------------------"<<endl<<endl;
insilmaril@106
  1605
	parseAtom (undoCommand);
insilmaril@106
  1606
	mapCenter->reposition();
insilmaril@106
  1607
insilmaril@163
  1608
	undosAvail--;
insilmaril@366
  1609
	curStep--; 
insilmaril@381
  1610
	if (curStep<1) curStep=stepsTotal;
insilmaril@366
  1611
insilmaril@366
  1612
	redosAvail++;
insilmaril@163
  1613
insilmaril@163
  1614
	blockSaveState=false;
insilmaril@163
  1615
/* TODO remove testing
insilmaril@163
  1616
	cout << "ME::undo() end\n";
insilmaril@163
  1617
	cout << "    undosAvail="<<undosAvail<<endl;
insilmaril@366
  1618
	cout << "    redosAvail="<<redosAvail<<endl;
insilmaril@366
  1619
	cout << "       curStep="<<curStep<<endl;
insilmaril@163
  1620
	cout << "    ---------------------------"<<endl<<endl;
insilmaril@404
  1621
*/
insilmaril@366
  1622
insilmaril@366
  1623
	undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
insilmaril@366
  1624
	undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
insilmaril@366
  1625
	undoSet.setEntry ("/history/curStep",QString::number(curStep));
insilmaril@366
  1626
	undoSet.writeSettings(histPath);
insilmaril@366
  1627
insilmaril@404
  1628
	mainWindow->updateHistory (undoSet);
insilmaril@366
  1629
	updateActions();
insilmaril@106
  1630
}
insilmaril@106
  1631
insilmaril@390
  1632
bool MapEditor::isUndoAvailable()
insilmaril@390
  1633
{
insilmaril@390
  1634
	if (undoSet.readNumEntry("/history/undosAvail",0)>0)
insilmaril@390
  1635
		return true;
insilmaril@390
  1636
	else	
insilmaril@390
  1637
		return false;
insilmaril@390
  1638
}
insilmaril@390
  1639
insilmaril@404
  1640
void MapEditor::gotoHistoryStep (int i)
insilmaril@381
  1641
{
insilmaril@381
  1642
	// Restore variables
insilmaril@381
  1643
	int undosAvail=undoSet.readNumEntry (QString("/history/undosAvail"));
insilmaril@388
  1644
	int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
insilmaril@388
  1645
insilmaril@388
  1646
	if (i<0) i=undosAvail+redosAvail;
insilmaril@388
  1647
insilmaril@388
  1648
	// Clicking above current step makes us undo things
insilmaril@381
  1649
	if (i<undosAvail) 
insilmaril@381
  1650
	{	
insilmaril@381
  1651
		for (int j=0; j<undosAvail-i; j++) undo();
insilmaril@381
  1652
		return;
insilmaril@381
  1653
	}	
insilmaril@388
  1654
	// Clicking below current step makes us redo things
insilmaril@381
  1655
	if (i>undosAvail) 
insilmaril@381
  1656
		for (int j=undosAvail; j<i; j++) redo();
insilmaril@381
  1657
insilmaril@381
  1658
	// And ignore clicking the current row ;-)	
insilmaril@381
  1659
}
insilmaril@381
  1660
insilmaril@394
  1661
void MapEditor::addMapReplaceInt(const QString &undoSel, const QString &path)
insilmaril@106
  1662
{
insilmaril@393
  1663
	QString pathDir=path.left(path.findRev("/"));
insilmaril@393
  1664
	QDir d(pathDir);
insilmaril@393
  1665
	QFile file (path);
insilmaril@163
  1666
insilmaril@106
  1667
	if (d.exists() )
insilmaril@106
  1668
	{
insilmaril@106
  1669
		// We need to parse saved XML data
insilmaril@106
  1670
		mapBuilderHandler handler;
insilmaril@163
  1671
		QXmlInputSource source( file);
insilmaril@106
  1672
		QXmlSimpleReader reader;
insilmaril@106
  1673
		reader.setContentHandler( &handler );
insilmaril@106
  1674
		reader.setErrorHandler( &handler );
insilmaril@106
  1675
		handler.setMapEditor( this );
insilmaril@393
  1676
		handler.setTmpDir ( pathDir );	// needed to load files with rel. path
insilmaril@106
  1677
		if (undoSel.isEmpty())
insilmaril@106
  1678
		{
insilmaril@139
  1679
			unselect();
insilmaril@106
  1680
			mapCenter->clear();
insilmaril@106
  1681
			handler.setLoadMode (NewMap);
insilmaril@106
  1682
		} else	
insilmaril@106
  1683
		{
insilmaril@106
  1684
			select (undoSel);
insilmaril@106
  1685
			handler.setLoadMode (ImportReplace);
insilmaril@106
  1686
		}	
insilmaril@139
  1687
		blockReposition=true;
insilmaril@106
  1688
		bool ok = reader.parse( source );
insilmaril@139
  1689
		blockReposition=false;
insilmaril@106
  1690
		if (! ok ) 
insilmaril@106
  1691
		{	
insilmaril@106
  1692
			// This should never ever happen
insilmaril@393
  1693
			QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path),
insilmaril@393
  1694
								    handler.errorProtocol());
insilmaril@106
  1695
		}
insilmaril@106
  1696
	} else	
insilmaril@393
  1697
		QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
insilmaril@393
  1698
}
insilmaril@393
  1699
insilmaril@394
  1700
void MapEditor::addMapInsertInt (const QString &path, int pos)
insilmaril@393
  1701
{
insilmaril@393
  1702
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@393
  1703
				      typeid(*selection) == typeid(MapCenterObj))) 
insilmaril@106
  1704
	{
insilmaril@393
  1705
		QString pathDir=path.left(path.findRev("/"));
insilmaril@393
  1706
		QDir d(pathDir);
insilmaril@393
  1707
		QFile file (path);
insilmaril@393
  1708
insilmaril@393
  1709
		if (d.exists() )
insilmaril@393
  1710
		{
insilmaril@393
  1711
			// We need to parse saved XML data
insilmaril@393
  1712
			mapBuilderHandler handler;
insilmaril@393
  1713
			QXmlInputSource source( file);
insilmaril@393
  1714
			QXmlSimpleReader reader;
insilmaril@393
  1715
			reader.setContentHandler( &handler );
insilmaril@393
  1716
			reader.setErrorHandler( &handler );
insilmaril@393
  1717
			handler.setMapEditor( this );
insilmaril@393
  1718
			handler.setTmpDir ( pathDir );	// needed to load files with rel. path
insilmaril@394
  1719
			handler.setLoadMode (ImportAdd);
insilmaril@393
  1720
			blockReposition=true;
insilmaril@393
  1721
			bool ok = reader.parse( source );
insilmaril@393
  1722
			blockReposition=false;
insilmaril@393
  1723
			if (! ok ) 
insilmaril@393
  1724
			{	
insilmaril@393
  1725
				// This should never ever happen
insilmaril@393
  1726
				QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path),
insilmaril@393
  1727
										handler.errorProtocol());
insilmaril@393
  1728
			}
insilmaril@394
  1729
			if (selection!=mapCenter)
insilmaril@394
  1730
				((BranchObj*)selection)->getLastBranch()->moveBranchTo ((BranchObj*)(selection),pos);
insilmaril@393
  1731
		} else	
insilmaril@393
  1732
			QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
insilmaril@393
  1733
	}		
insilmaril@106
  1734
}
insilmaril@106
  1735
insilmaril@166
  1736
void MapEditor::pasteNoSave()
insilmaril@0
  1737
{
insilmaril@166
  1738
	load (clipboardDir+"/part.xml",ImportAdd);
insilmaril@0
  1739
}
insilmaril@0
  1740
insilmaril@0
  1741
void MapEditor::cutNoSave()
insilmaril@0
  1742
{
insilmaril@166
  1743
	copy();
insilmaril@166
  1744
	deleteSelection();
insilmaril@0
  1745
}
insilmaril@0
  1746
insilmaril@0
  1747
void MapEditor::paste()
insilmaril@0
  1748
{   
insilmaril@166
  1749
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@166
  1750
				      typeid(*selection) == typeid(MapCenterObj))) 
insilmaril@166
  1751
	{
insilmaril@166
  1752
		pasteNoSave();
insilmaril@395
  1753
		saveStateChangingPart(
insilmaril@395
  1754
			selection,
insilmaril@395
  1755
			selection,
insilmaril@395
  1756
			"paste ()",
insilmaril@395
  1757
			QString("Paste to %1").arg( getName(selection))
insilmaril@395
  1758
		);
insilmaril@166
  1759
		mapCenter->reposition();
insilmaril@166
  1760
		adjustCanvasSize();
insilmaril@166
  1761
	}
insilmaril@0
  1762
}
insilmaril@0
  1763
insilmaril@0
  1764
void MapEditor::cut()
insilmaril@0
  1765
{
insilmaril@395
  1766
	saveStateChangingPart(
insilmaril@395
  1767
		selection->getParObj(),
insilmaril@395
  1768
		selection,
insilmaril@395
  1769
		"cut ()",
insilmaril@395
  1770
		QString("Cut %1").arg(getName(selection))
insilmaril@395
  1771
	);
insilmaril@0
  1772
	copy();
insilmaril@0
  1773
	cutNoSave();
insilmaril@0
  1774
	mapCenter->reposition();
insilmaril@0
  1775
	adjustCanvasSize();
insilmaril@0
  1776
}
insilmaril@0
  1777
insilmaril@130
  1778
void MapEditor::move(const int &x, const int &y)
insilmaril@130
  1779
{
insilmaril@366
  1780
	if (selection)
insilmaril@366
  1781
	{
insilmaril@408
  1782
		QString ps=qpointfToString (selection->getAbsPos());
insilmaril@366
  1783
		QString s=selection->getSelectString();
insilmaril@366
  1784
		saveState(
insilmaril@366
  1785
			s, "move "+ps, 
insilmaril@408
  1786
			s, "move "+qpointfToString (QPointF (x,y)), 
insilmaril@366
  1787
			QString("Move %1 to  %2").arg(getName(selection)).arg(ps));
insilmaril@366
  1788
		selection->move(x,y);
insilmaril@366
  1789
		mapCenter->reposition();
insilmaril@366
  1790
		adjustCanvasSize();
insilmaril@366
  1791
	}
insilmaril@366
  1792
insilmaril@366
  1793
}
insilmaril@366
  1794
insilmaril@366
  1795
void MapEditor::moveRel (const int &x, const int &y)
insilmaril@366
  1796
{
insilmaril@388
  1797
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@388
  1798
				      typeid(*selection) == typeid(MapCenterObj) ||
insilmaril@388
  1799
					  typeid(*selection) == typeid (FloatImageObj))) 
insilmaril@366
  1800
	{
insilmaril@408
  1801
		QString ps=qpointfToString (selection->getRelPos());
insilmaril@366
  1802
		QString s=selection->getSelectString();
insilmaril@366
  1803
		saveState(
insilmaril@366
  1804
			s, "moveRel "+ps, 
insilmaril@408
  1805
			s, "moveRel "+qpointfToString (QPointF (x,y)), 
insilmaril@366
  1806
			QString("Move %1 to relativ position %2").arg(getName(selection)).arg(ps));
insilmaril@388
  1807
		((OrnamentedObj*)selection)->move2RelPos (x,y);
insilmaril@366
  1808
		mapCenter->reposition();
insilmaril@366
  1809
		adjustCanvasSize();
insilmaril@366
  1810
	}
insilmaril@130
  1811
}
insilmaril@130
  1812
insilmaril@0
  1813
void MapEditor::moveBranchUp()
insilmaril@0
  1814
{
insilmaril@0
  1815
	BranchObj* bo;
insilmaril@0
  1816
	BranchObj* par;
insilmaril@0
  1817
	if (typeid(*selection) == typeid(BranchObj)  ) 
insilmaril@0
  1818
	{
insilmaril@163
  1819
		bo=(BranchObj*)selection;
insilmaril@314
  1820
		if (!bo->canMoveBranchUp()) return;
insilmaril@0
  1821
		par=(BranchObj*)(bo->getParObj());
insilmaril@0
  1822
		selection->unselect();
insilmaril@366
  1823
		bo=par->moveBranchUp (bo);	// bo will be the one below selection
insilmaril@0
  1824
		selection->select();
insilmaril@366
  1825
		saveState (selection,"moveBranchDown ()",bo,"moveBranchUp ()",QString("Move up %1").arg(getName(bo)));
insilmaril@0
  1826
		mapCenter->reposition();
insilmaril@0
  1827
		ensureSelectionVisible();
insilmaril@0
  1828
	}
insilmaril@0
  1829
}
insilmaril@0
  1830
insilmaril@0
  1831
void MapEditor::moveBranchDown()
insilmaril@0
  1832
{
insilmaril@0
  1833
	BranchObj* bo;
insilmaril@0
  1834
	BranchObj* par;
insilmaril@0
  1835
	if (typeid(*selection) == typeid(BranchObj)  ) 
insilmaril@0
  1836
	{
insilmaril@163
  1837
		bo=(BranchObj*)selection;
insilmaril@314
  1838
		if (!bo->canMoveBranchDown()) return;
insilmaril@0
  1839
		par=(BranchObj*)(bo->getParObj());
insilmaril@0
  1840
		selection->unselect(); 
insilmaril@366
  1841
		bo=par->moveBranchDown(bo);	// bo will be the one above selection
insilmaril@0
  1842
		selection->select();
insilmaril@366
  1843
		saveState(selection,"moveBranchUp ()",bo,"moveBranchDown ()",QString("Move down %1").arg(getName(bo)));
insilmaril@0
  1844
		mapCenter->reposition();
insilmaril@0
  1845
		ensureSelectionVisible();
insilmaril@0
  1846
	}	
insilmaril@0
  1847
}
insilmaril@0
  1848
insilmaril@366
  1849
QString MapEditor::getHeading(bool &ok, QPoint &p)
insilmaril@0
  1850
{
insilmaril@366
  1851
	if (selection  &&  
insilmaril@366
  1852
		 (typeid(*selection) == typeid(BranchObj) || 
insilmaril@366
  1853
		  typeid(*selection) == typeid(MapCenterObj) ) ) 
insilmaril@366
  1854
	{
insilmaril@366
  1855
		ok=true;
insilmaril@366
  1856
		ensureSelectionVisible();
insilmaril@408
  1857
		p=mapFromScene(selection->getAbsPos());
insilmaril@366
  1858
		return ((BranchObj*)selection)->getHeading();
insilmaril@366
  1859
	}
insilmaril@366
  1860
	ok=false;
insilmaril@366
  1861
	return QString();
insilmaril@366
  1862
}
insilmaril@366
  1863
insilmaril@366
  1864
void MapEditor::setHeading(const QString &s)
insilmaril@366
  1865
{
insilmaril@0
  1866
	if (selection  &&  
insilmaril@0
  1867
		 (typeid(*selection) == typeid(BranchObj) || 
insilmaril@0
  1868
		  typeid(*selection) == typeid(MapCenterObj) ) ) 
insilmaril@0
  1869
	{
insilmaril@163
  1870
		editingBO=(BranchObj*)selection;
insilmaril@395
  1871
		saveState(
insilmaril@395
  1872
			selection,
insilmaril@366
  1873
			"setHeading (\""+editingBO->getHeading()+"\")", 
insilmaril@395
  1874
			selection,
insilmaril@366
  1875
			"setHeading (\""+s+"\")", 
insilmaril@366
  1876
			QString("Set heading of %1 to \"%2\"").arg(getName(editingBO)).arg(s) );
insilmaril@366
  1877
		editingBO->setHeading(s );
insilmaril@366
  1878
		editingBO=NULL;
insilmaril@106
  1879
		mapCenter->reposition();
insilmaril@106
  1880
		adjustCanvasSize();
insilmaril@106
  1881
		ensureSelectionVisible();
insilmaril@106
  1882
	}
insilmaril@106
  1883
}
insilmaril@106
  1884
insilmaril@366
  1885
void MapEditor::setURLInt (const QString &s)
insilmaril@143
  1886
{
insilmaril@143
  1887
	// Internal function, no saveState needed
insilmaril@143
  1888
	if (selection  &&  
insilmaril@143
  1889
		 (typeid(*selection) == typeid(BranchObj) || 
insilmaril@143
  1890
		  typeid(*selection) == typeid(MapCenterObj) ) ) 
insilmaril@143
  1891
	{
insilmaril@163
  1892
		((BranchObj*)selection)->setURL(s);
insilmaril@143
  1893
		mapCenter->reposition();
insilmaril@143
  1894
		adjustCanvasSize();
insilmaril@143
  1895
		ensureSelectionVisible();
insilmaril@143
  1896
	}
insilmaril@143
  1897
}
insilmaril@143
  1898
insilmaril@366
  1899
void MapEditor::setHeadingInt(const QString &s)
insilmaril@366
  1900
{
insilmaril@366
  1901
	if (selection  &&  
insilmaril@366
  1902
		 (typeid(*selection) == typeid(BranchObj) || 
insilmaril@366
  1903
		  typeid(*selection) == typeid(MapCenterObj) ) ) 
insilmaril@366
  1904
	{
insilmaril@366
  1905
		((BranchObj*)selection)->setHeading(s);
insilmaril@366
  1906
		mapCenter->reposition();
insilmaril@366
  1907
		adjustCanvasSize();
insilmaril@366
  1908
		ensureSelectionVisible();
insilmaril@366
  1909
	}
insilmaril@366
  1910
}
insilmaril@366
  1911
insilmaril@366
  1912
void MapEditor::setVymLinkInt (const QString &s)
insilmaril@143
  1913
{
insilmaril@143
  1914
	// Internal function, no saveState needed
insilmaril@143
  1915
	if (selection  &&  
insilmaril@143
  1916
		 (typeid(*selection) == typeid(BranchObj) || 
insilmaril@143
  1917
		  typeid(*selection) == typeid(MapCenterObj) ) ) 
insilmaril@143
  1918
	{
insilmaril@163
  1919
		((BranchObj*)selection)->setVymLink(s);
insilmaril@143
  1920
		mapCenter->reposition();
insilmaril@143
  1921
		adjustCanvasSize();
insilmaril@143
  1922
		ensureSelectionVisible();
insilmaril@143
  1923
	}
insilmaril@143
  1924
}
insilmaril@143
  1925
insilmaril@366
  1926
BranchObj* MapEditor::addNewBranchInt(int num)
insilmaril@0
  1927
{
insilmaril@366
  1928
	// Depending on pos:
insilmaril@366
  1929
	// -3		insert in childs of parent  above selection 
insilmaril@366
  1930
	// -2		add branch to selection 
insilmaril@366
  1931
	// -1		insert in childs of parent below selection 
insilmaril@366
  1932
	// 0..n		insert in childs of parent at pos
insilmaril@366
  1933
	BranchObj *newbo=NULL;
insilmaril@0
  1934
	if (selection  &&  
insilmaril@0
  1935
		 (typeid(*selection) == typeid(BranchObj) || 
insilmaril@0
  1936
		  typeid(*selection) == typeid(MapCenterObj) ) ) 
insilmaril@0
  1937
	{
insilmaril@366
  1938
		BranchObj* bo = (BranchObj*) selection;
insilmaril@366
  1939
		if (num==-2)
insilmaril@0
  1940
		{
insilmaril@0
  1941
			// save scroll state. If scrolled, automatically select
insilmaril@0
  1942
			// new branch in order to tmp unscroll parent...
insilmaril@366
  1943
			return bo->addBranch();
insilmaril@366
  1944
			
insilmaril@366
  1945
		}else if (num==-1)
insilmaril@0
  1946
		{
insilmaril@366
  1947
			num=bo->getNum()+1;
insilmaril@366
  1948
			bo=(BranchObj*)bo->getParObj();
insilmaril@366
  1949
		}else if (num==-3)
insilmaril@366
  1950
		{
insilmaril@366
  1951
			num=bo->getNum();
insilmaril@366
  1952
			bo=(BranchObj*)bo->getParObj();
insilmaril@366
  1953
		}
insilmaril@366
  1954
		if (!bo) return bo;
insilmaril@366
  1955
		newbo=bo->insertBranch(num);
insilmaril@366
  1956
	}	
insilmaril@366
  1957
	return newbo;
insilmaril@366
  1958
}	
insilmaril@366
  1959
insilmaril@394
  1960
BranchObj* MapEditor::addNewBranch(int pos)
insilmaril@366
  1961
{
insilmaril@366
  1962
	// Different meaning than num in addNewBranchInt!
insilmaril@366
  1963
	// -1	add above
insilmaril@366
  1964
	//  0	add as child
insilmaril@366
  1965
	// +1	add below
insilmaril@394
  1966
	BranchObj *bo = (BranchObj*) selection;
insilmaril@394
  1967
	BranchObj *newbo=NULL;
insilmaril@394
  1968
insilmaril@366
  1969
	if (selection  &&  
insilmaril@366
  1970
		 (typeid(*selection) == typeid(BranchObj) || 
insilmaril@366
  1971
		  typeid(*selection) == typeid(MapCenterObj) ) ) 
insilmaril@366
  1972
	{
insilmaril@366
  1973
		newbo=addNewBranchInt (pos-2);
insilmaril@366
  1974
insilmaril@366
  1975
		if (newbo)
insilmaril@366
  1976
		{
insilmaril@395
  1977
			saveState(
insilmaril@395
  1978
				selection,		// FIXME sholdnt newbo be deleted here???
insilmaril@393
  1979
				"delete ()",
insilmaril@395
  1980
				selection,
insilmaril@366
  1981
				QString ("addBranch (%1)").arg(pos-2),
insilmaril@395
  1982
				QString ("Add new branch to %1").arg(getName(bo)));	
insilmaril@366
  1983
insilmaril@366
  1984
			mapCenter->reposition();
insilmaril@366
  1985
			adjustCanvasSize();
insilmaril@366
  1986
		}
insilmaril@366
  1987
	}	
insilmaril@394
  1988
	return newbo;
insilmaril@366
  1989
}
insilmaril@366
  1990
insilmaril@366
  1991
insilmaril@394
  1992
BranchObj* MapEditor::addNewBranchBefore()
insilmaril@366
  1993
{
insilmaril@394
  1994
	BranchObj *newbo=NULL;
insilmaril@366
  1995
	if (selection  &&  
insilmaril@366
  1996
		 (typeid(*selection) == typeid(BranchObj) ) )
insilmaril@394
  1997
		 // We accept no MapCenterObj here, so we _have_ a parent
insilmaril@366
  1998
	{
insilmaril@394
  1999
		BranchObj* bo = (BranchObj*) selection;
insilmaril@408
  2000
		QPointF p=bo->getRelPos();
insilmaril@394
  2001
insilmaril@394
  2002
insilmaril@366
  2003
		BranchObj *parbo=(BranchObj*)(selection->getParObj());
insilmaril@394
  2004
insilmaril@394
  2005
		// add below selection
insilmaril@394
  2006
		newbo=parbo->insertBranch(bo->getNum()+1);
insilmaril@394
  2007
		if (newbo)
insilmaril@366
  2008
		{
insilmaril@394
  2009
			newbo->move2RelPos (p);
insilmaril@394
  2010
insilmaril@394
  2011
			// Move selection to new branch
insilmaril@394
  2012
			((BranchObj*)selection)->moveBranchTo (newbo,-1);
insilmaril@394
  2013
insilmaril@394
  2014
			saveState (newbo, "deleteKeepChilds ()", newbo, "addBranchBefore ()", 
insilmaril@394
  2015
				QString ("Add branch before %1").arg(getName(bo)));
insilmaril@394
  2016
insilmaril@394
  2017
			mapCenter->reposition();
insilmaril@394
  2018
			adjustCanvasSize();
insilmaril@394
  2019
		}
insilmaril@0
  2020
	}	
insilmaril@394
  2021
	return newbo;
insilmaril@0
  2022
}
insilmaril@0
  2023
insilmaril@0
  2024
void MapEditor::deleteSelection()
insilmaril@0
  2025
{
insilmaril@0
  2026
	if (selection  && typeid(*selection) ==typeid(BranchObj) ) 
insilmaril@0
  2027
	{
insilmaril@366
  2028
		BranchObj* bo=(BranchObj*)selection;
insilmaril@294
  2029
		BranchObj* par=(BranchObj*)(bo->getParObj());
insilmaril@294
  2030
		bo->unselect();
insilmaril@394
  2031
		saveStateRemovingPart (bo, QString ("Delete %1").arg(getName(bo)));
insilmaril@0
  2032
		selection=NULL;
insilmaril@0
  2033
		par->removeBranch(bo);
insilmaril@0
  2034
		selection=par;
insilmaril@0
  2035
		selection->select();
insilmaril@0
  2036
		ensureSelectionVisible();
insilmaril@0
  2037
		mapCenter->reposition();
insilmaril@0
  2038
		adjustCanvasSize();
insilmaril@0
  2039
	}
insilmaril@0
  2040
	if (selection  && typeid(*selection) ==typeid(FloatImageObj) ) 
insilmaril@0
  2041
	{
insilmaril@366
  2042
		FloatImageObj* fio=(FloatImageObj*)selection;
insilmaril@0
  2043
		BranchObj* par=(BranchObj*)(fio->getParObj());
insilmaril@395
  2044
		saveStateChangingPart(
insilmaril@395
  2045
			par, 
insilmaril@395
  2046
			fio,
insilmaril@395
  2047
			"delete ()",
insilmaril@395
  2048
			QString("Delete %1").arg(getName(fio))
insilmaril@395
  2049
		);
insilmaril@0
  2050
		fio->unselect();
insilmaril@0
  2051
		selection=NULL;
insilmaril@0
  2052
		par->removeFloatImage(fio);
insilmaril@0
  2053
		selection=par;
insilmaril@0
  2054
		selection->select();
insilmaril@0
  2055
		ensureSelectionVisible();
insilmaril@0
  2056
		mapCenter->reposition();
insilmaril@0
  2057
		adjustCanvasSize();
insilmaril@0
  2058
	}
insilmaril@0
  2059
}
insilmaril@0
  2060
insilmaril@0
  2061
LinkableMapObj* MapEditor::getSelection()
insilmaril@0
  2062
{
insilmaril@0
  2063
	return selection;
insilmaril@0
  2064
}
insilmaril@0
  2065
insilmaril@267
  2066
void MapEditor::unselect()
insilmaril@267
  2067
{
insilmaril@267
  2068
	if (selection) 
insilmaril@267
  2069
	{
insilmaril@267
  2070
		selectionLast=selection;
insilmaril@267
  2071
		selection->unselect();
insilmaril@267
  2072
		selection=NULL;
insilmaril@267
  2073
	}
insilmaril@267
  2074
}	
insilmaril@267
  2075
insilmaril@267
  2076
void MapEditor::reselect()
insilmaril@267
  2077
{
insilmaril@267
  2078
	if (selectionLast)
insilmaril@267
  2079
	{
insilmaril@267
  2080
		selection=selectionLast;
insilmaril@267
  2081
		selection->select();
insilmaril@267
  2082
		selectionLast=NULL;
insilmaril@267
  2083
	}
insilmaril@267
  2084
}	
insilmaril@267
  2085
insilmaril@83
  2086
bool MapEditor::select (const QString &s)
insilmaril@83
  2087
{
insilmaril@106
  2088
	LinkableMapObj *lmo=mapCenter->findObjBySelect(s);
insilmaril@0
  2089
insilmaril@0
  2090
	// Finally select the found object
insilmaril@0
  2091
	if (lmo)
insilmaril@0
  2092
	{
insilmaril@98
  2093
		if (selection) unselect();
insilmaril@0
  2094
		selection=lmo;
insilmaril@0
  2095
		selection->select();
insilmaril@0
  2096
		adjustCanvasSize();
insilmaril@0
  2097
		ensureSelectionVisible();
insilmaril@0
  2098
		return true;
insilmaril@139
  2099
	} 
insilmaril@139
  2100
	return false;
insilmaril@0
  2101
}
insilmaril@0
  2102
insilmaril@366
  2103
QString MapEditor::getSelectString()
insilmaril@366
  2104
{
insilmaril@366
  2105
	if (selection) return selection->getSelectString();
insilmaril@366
  2106
	return QString();
insilmaril@366
  2107
}
insilmaril@366
  2108
insilmaril@366
  2109
void MapEditor::selectInt (LinkableMapObj *lmo)
insilmaril@0
  2110
{
insilmaril@267
  2111
	if (lmo && selection != lmo)
insilmaril@0
  2112
	{
insilmaril@267
  2113
		// select the MapObj
insilmaril@267
  2114
		if (selection) selection->unselect();
insilmaril@267
  2115
		selection=lmo;
insilmaril@267
  2116
		selection->select();
insilmaril@267
  2117
			
insilmaril@267
  2118
		adjustCanvasSize();
insilmaril@0
  2119
	}
insilmaril@267
  2120
}
insilmaril@0
  2121
insilmaril@366
  2122
void MapEditor::selectNextBranchInt()
insilmaril@0
  2123
{
insilmaril@0
  2124
	// Increase number of branch
insilmaril@0
  2125
	if (selection)
insilmaril@0
  2126
	{
insilmaril@0
  2127
		QString s=selection->getSelectString();
insilmaril@0
  2128
		QString part;
insilmaril@0
  2129
		QString typ;
insilmaril@0
  2130
		QString num;
insilmaril@0
  2131
insilmaril@0
  2132
		// Where am I? 
insilmaril@0
  2133
		part=s.section(",",-1);
insilmaril@0
  2134
		typ=part.left (3);
insilmaril@0
  2135
		num=part.right(part.length() - 3);
insilmaril@0
  2136
insilmaril@0
  2137
		s=s.left (s.length() -num.length());
insilmaril@0
  2138
insilmaril@0
  2139
		// Go to next lmo
insilmaril@0
  2140
		num=QString ("%1").arg(num.toUInt()+1);
insilmaril@0
  2141
insilmaril@0
  2142
		s=s+num;
insilmaril@0
  2143
		
insilmaril@0
  2144
		// Try to select this one
insilmaril@0
  2145
		if (select (s)) return;
insilmaril@0
  2146
insilmaril@0
  2147
		// We have no direct successor, 
insilmaril@0
  2148
		// try to increase the parental number in order to
insilmaril@0
  2149
		// find a successor with same depth
insilmaril@0
  2150
insilmaril@0
  2151
		int d=selection->getDepth();
insilmaril@0
  2152
		int oldDepth=d;
insilmaril@0
  2153
		int i;
insilmaril@0
  2154
		bool found=false;
insilmaril@0
  2155
		bool b;
insilmaril@0
  2156
		while (!found && d>0)
insilmaril@0
  2157
		{
insilmaril@0
  2158
			s=s.section (",",0,d-1);
insilmaril@0
  2159
			// replace substring of current depth in s with "1"
insilmaril@0
  2160
			part=s.section(",",-1);
insilmaril@0
  2161
			typ=part.left (3);
insilmaril@0
  2162
			num=part.right(part.length() - 3);
insilmaril@0
  2163
insilmaril@0
  2164
			if (d>1)
insilmaril@0
  2165
			{	
insilmaril@0
  2166
				// increase number of parent
insilmaril@0
  2167
				num=QString ("%1").arg(num.toUInt()+1);
insilmaril@0
  2168
				s=s.section (",",0,d-2) + ","+ typ+num;
insilmaril@0
  2169
			} else
insilmaril@0
  2170
			{
insilmaril@0
  2171
				// Special case, look at orientation
insilmaril@0
  2172
				if (selection->getOrientation()==OrientRightOfCenter)
insilmaril@0
  2173
					num=QString ("%1").arg(num.toUInt()+1);
insilmaril@0
  2174
				else	
insilmaril@0
  2175
					num=QString ("%1").arg(num.toUInt()-1);
insilmaril@0
  2176
				s=typ+num;
insilmaril@0
  2177
			}	
insilmaril@0
  2178
insilmaril@0
  2179
			if (select (s))
insilmaril@0
  2180
				// pad to oldDepth, select the first branch for each depth
insilmaril@0
  2181
				for (i=d;i<oldDepth;i++)
insilmaril@0
  2182
				{
insilmaril@0
  2183
					b=select (s);
insilmaril@0
  2184
					if (b)
insilmaril@0
  2185
					{	
insilmaril@163
  2186
						if ( ((BranchObj*)selection)->countBranches()>0)
insilmaril@0
  2187
							s+=",bo:0";
insilmaril@0
  2188
						else	
insilmaril@0
  2189
							break;
insilmaril@0
  2190
					} else
insilmaril@0
  2191
						break;
insilmaril@0
  2192
				}	
insilmaril@0
  2193
insilmaril@0
  2194
			// try to select the freshly built string
insilmaril@0
  2195
			found=select(s);
insilmaril@0
  2196
			d--;
insilmaril@0
  2197
		}
insilmaril@0
  2198
		return;
insilmaril@0
  2199
	}	
insilmaril@0
  2200
}
insilmaril@0
  2201
insilmaril@366
  2202
void MapEditor::selectPrevBranchInt()
insilmaril@0
  2203
{
insilmaril@0
  2204
	// Decrease number of branch
insilmaril@0
  2205
	if (selection)
insilmaril@0
  2206
	{
insilmaril@0
  2207
		QString s=selection->getSelectString();
insilmaril@0
  2208
		QString part;
insilmaril@0
  2209
		QString typ;
insilmaril@0
  2210
		QString num;
insilmaril@0
  2211
insilmaril@0
  2212
		// Where am I? 
insilmaril@0
  2213
		part=s.section(",",-1);
insilmaril@0
  2214
		typ=part.left (3);
insilmaril@0
  2215
		num=part.right(part.length() - 3);
insilmaril@0
  2216
insilmaril@0
  2217
		s=s.left (s.length() -num.length());
insilmaril@0
  2218
insilmaril@0
  2219
		// Go to next lmo
insilmaril@0
  2220
		num=QString ("%1").arg(num.toUInt()-1);
insilmaril@0
  2221
insilmaril@0
  2222
		s=s+num;
insilmaril@0
  2223
		
insilmaril@0
  2224
		// Try to select this one
insilmaril@0
  2225
		if (select (s)) return;
insilmaril@0
  2226
insilmaril@0
  2227
		// We have no direct precessor, 
insilmaril@0
  2228
		// try to decrease the parental number in order to
insilmaril@0
  2229
		// find a precessor with same depth
insilmaril@0
  2230
insilmaril@0
  2231
		int d=selection->getDepth();
insilmaril@0
  2232
		int oldDepth=d;
insilmaril@0
  2233
		int i;
insilmaril@0
  2234
		bool found=false;
insilmaril@0
  2235
		bool b;
insilmaril@0
  2236
		while (!found && d>0)
insilmaril@0
  2237
		{
insilmaril@0
  2238
			s=s.section (",",0,d-1);
insilmaril@0
  2239
			// replace substring of current depth in s with "1"
insilmaril@0
  2240
			part=s.section(",",-1);
insilmaril@0
  2241
			typ=part.left (3);
insilmaril@0
  2242
			num=part.right(part.length() - 3);
insilmaril@0
  2243
insilmaril@0
  2244
			if (d>1)
insilmaril@0
  2245
			{
insilmaril@0
  2246
				// decrease number of parent
insilmaril@0
  2247
				num=QString ("%1").arg(num.toUInt()-1);
insilmaril@0
  2248
				s=s.section (",",0,d-2) + ","+ typ+num;
insilmaril@0
  2249
			} else
insilmaril@0
  2250
			{
insilmaril@0
  2251
				// Special case, look at orientation
insilmaril@0
  2252
				if (selection->getOrientation()==OrientRightOfCenter)
insilmaril@0
  2253
					num=QString ("%1").arg(num.toUInt()-1);
insilmaril@0
  2254
				else	
insilmaril@0
  2255
					num=QString ("%1").arg(num.toUInt()+1);
insilmaril@0
  2256
				s=typ+num;
insilmaril@0
  2257
			}	
insilmaril@0
  2258
insilmaril@0
  2259
			if (select(s))
insilmaril@0
  2260
				// pad to oldDepth, select the last branch for each depth
insilmaril@0
  2261
				for (i=d;i<oldDepth;i++)
insilmaril@0
  2262
				{
insilmaril@0
  2263
					b=select (s);
insilmaril@0
  2264
					if (b)
insilmaril@163
  2265
						if ( ((BranchObj*)selection)->countBranches()>0)
insilmaril@163
  2266
							s+=",bo:"+ QString ("%1").arg( ((BranchObj*)selection)->countBranches()-1 );
insilmaril@0
  2267
						else	
insilmaril@0
  2268
							break;
insilmaril@0
  2269
					else
insilmaril@0
  2270
						break;
insilmaril@0
  2271
				}	
insilmaril@0
  2272
			
insilmaril@0
  2273
			// try to select the freshly built string
insilmaril@0
  2274
			found=select(s);
insilmaril@0
  2275
			d--;
insilmaril@0
  2276
		}
insilmaril@0
  2277
		return;
insilmaril@0
  2278
	}	
insilmaril@0
  2279
}
insilmaril@0
  2280
insilmaril@0
  2281
void MapEditor::selectUpperBranch()
insilmaril@0
  2282
{
insilmaril@0
  2283
	if (selection) 
insilmaril@0
  2284
	{
insilmaril@0
  2285
		if (typeid(*selection) == typeid(BranchObj))
insilmaril@0
  2286
		{
insilmaril@0
  2287
			if (selection->getOrientation()==OrientRightOfCenter)
insilmaril@366
  2288
				selectPrevBranchInt();
insilmaril@0
  2289
			else
insilmaril@0
  2290
				if (selection->getDepth()==1)
insilmaril@366
  2291
					selectNextBranchInt();
insilmaril@0
  2292
				else
insilmaril@366
  2293
					selectPrevBranchInt();
insilmaril@0
  2294
		}		
insilmaril@0
  2295
	}
insilmaril@0
  2296
}
insilmaril@0
  2297
insilmaril@0
  2298
void MapEditor::selectLowerBranch()
insilmaril@0
  2299
{
insilmaril@0
  2300
	if (selection) 
insilmaril@0
  2301
	{
insilmaril@0
  2302
		if (typeid(*selection) == typeid(BranchObj))
insilmaril@0
  2303
		{
insilmaril@0
  2304
			if (selection->getOrientation()==OrientRightOfCenter)
insilmaril@366
  2305
				selectNextBranchInt();
insilmaril@0
  2306
			else
insilmaril@0
  2307
				if (selection->getDepth()==1)
insilmaril@366
  2308
					selectPrevBranchInt();
insilmaril@0
  2309
				else
insilmaril@366
  2310
					selectNextBranchInt();
insilmaril@0
  2311
		}		
insilmaril@0
  2312
	}
insilmaril@0
  2313
}
insilmaril@0
  2314
insilmaril@0
  2315
insilmaril@0
  2316
void MapEditor::selectLeftBranch()
insilmaril@0
  2317
{
insilmaril@0
  2318
	BranchObj* bo;
insilmaril@0
  2319
	BranchObj* par;
insilmaril@0
  2320
	if (selection) 
insilmaril@0
  2321
	{
insilmaril@0
  2322
		if (typeid(*selection) == typeid(MapCenterObj))
insilmaril@0
  2323
		{
insilmaril@163
  2324
			par=  (BranchObj*) selection;
insilmaril@0
  2325
			bo=par->getLastSelectedBranch();
insilmaril@0
  2326
			if (bo)
insilmaril@0
  2327
			{
insilmaril@0
  2328
				// Workaround for reselecting on left and right side
insilmaril@0
  2329
				if (bo->getOrientation()==OrientRightOfCenter)
insilmaril@0
  2330
				{
insilmaril@0
  2331
					bo=par->getLastBranch();
insilmaril@0
  2332
				}	
insilmaril@0
  2333
				if (bo)
insilmaril@0
  2334
				{
insilmaril@0
  2335
					par->unselect();
insilmaril@0
  2336
					selection=bo;
insilmaril@0
  2337
					selection->select();
insilmaril@0
  2338
					adjustCanvasSize();
insilmaril@0
  2339
					ensureSelectionVisible();
insilmaril@0
  2340
				}
insilmaril@0
  2341
			}	
insilmaril@0
  2342
		} else
insilmaril@0
  2343
		{
insilmaril@0
  2344
			par=(BranchObj*)(selection->getParObj());
insilmaril@0
  2345
			if (selection->getOrientation()==OrientRightOfCenter)
insilmaril@0
  2346
			{
insilmaril@0
  2347
				if (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2348
					typeid(*selection) == typeid(FloatImageObj))
insilmaril@0
  2349
				{
insilmaril@0
  2350
					selection->unselect();
insilmaril@0
  2351
					selection=par;
insilmaril@0
  2352
					selection->select();
insilmaril@0
  2353
					adjustCanvasSize();
insilmaril@0
  2354
					ensureSelectionVisible();
insilmaril@0
  2355
				}
insilmaril@0
  2356
			} else
insilmaril@0
  2357
			{
insilmaril@0
  2358
				if (typeid(*selection) == typeid(BranchObj) )
insilmaril@0
  2359
				{
insilmaril@163
  2360
					bo=((BranchObj*)selection)->getLastSelectedBranch();
insilmaril@0
  2361
					if (bo) 
insilmaril@0
  2362
					{
insilmaril@0
  2363
						selection->unselect();
insilmaril@0
  2364
						selection=bo;
insilmaril@0
  2365
						selection->select();
insilmaril@0
  2366
						adjustCanvasSize();
insilmaril@0
  2367
						ensureSelectionVisible();
insilmaril@0
  2368
					}
insilmaril@0
  2369
				}
insilmaril@0
  2370
			}
insilmaril@0
  2371
		}	
insilmaril@0
  2372
	}
insilmaril@0
  2373
}
insilmaril@0
  2374
insilmaril@0
  2375
void MapEditor::selectRightBranch()
insilmaril@0
  2376
{
insilmaril@0
  2377
	BranchObj* bo;
insilmaril@0
  2378
	BranchObj* par;
insilmaril@0
  2379
insilmaril@0
  2380
	if (selection) 
insilmaril@0
  2381
	{
insilmaril@0
  2382
		if (typeid(*selection) == typeid(MapCenterObj))
insilmaril@0
  2383
		{
insilmaril@163
  2384
			par=  (BranchObj*) selection;
insilmaril@0
  2385
			bo=par->getLastSelectedBranch();
insilmaril@0
  2386
			if (bo)
insilmaril@0
  2387
			{
insilmaril@366
  2388
				// Workaround for reselecting on left and right side
insilmaril@0
  2389
				if (bo->getOrientation()==OrientLeftOfCenter)
insilmaril@0
  2390
					bo=par->getFirstBranch();
insilmaril@0
  2391
				if (bo)
insilmaril@0
  2392
				{
insilmaril@0
  2393
					par->unselect();
insilmaril@0
  2394
					selection=bo;
insilmaril@0
  2395
					selection->select();
insilmaril@0
  2396
					ensureSelectionVisible();
insilmaril@0
  2397
				}
insilmaril@0
  2398
			}
insilmaril@0
  2399
		} else
insilmaril@0
  2400
		{
insilmaril@0
  2401
			par=(BranchObj*)(selection->getParObj());
insilmaril@0
  2402
			if (selection->getOrientation()==OrientLeftOfCenter)
insilmaril@0
  2403
			{
insilmaril@0
  2404
				if (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2405
					typeid(*selection) == typeid(FloatImageObj))
insilmaril@0
  2406
				{
insilmaril@0
  2407
					selection->unselect();
insilmaril@0
  2408
					selection=par;
insilmaril@0
  2409
					selection->select();
insilmaril@0
  2410
					adjustCanvasSize();
insilmaril@0
  2411
					ensureSelectionVisible();
insilmaril@0
  2412
				}
insilmaril@0
  2413
			} else
insilmaril@0
  2414
			{
insilmaril@0
  2415
				if (typeid(*selection) == typeid(BranchObj) )
insilmaril@0
  2416
				{
insilmaril@163
  2417
					bo=((BranchObj*)selection)->getLastSelectedBranch();
insilmaril@0
  2418
					if (bo) 
insilmaril@0
  2419
					{
insilmaril@0
  2420
						selection->unselect();
insilmaril@0
  2421
						selection=bo;
insilmaril@0
  2422
						selection->select();
insilmaril@0
  2423
						adjustCanvasSize();
insilmaril@0
  2424
						ensureSelectionVisible();
insilmaril@0
  2425
					}
insilmaril@0
  2426
				}
insilmaril@0
  2427
			}
insilmaril@0
  2428
		}
insilmaril@0
  2429
	}
insilmaril@0
  2430
}
insilmaril@0
  2431
insilmaril@0
  2432
void MapEditor::selectFirstBranch()
insilmaril@0
  2433
{
insilmaril@0
  2434
	BranchObj *bo1;
insilmaril@0
  2435
	BranchObj *bo2;
insilmaril@0
  2436
	BranchObj* par;
insilmaril@0
  2437
	if (selection) {
insilmaril@0
  2438
		if (typeid(*selection) == typeid(BranchObj))
insilmaril@0
  2439
		{
insilmaril@163
  2440
			bo1=  (BranchObj*) selection;
insilmaril@0
  2441
			par=(BranchObj*)(bo1->getParObj());
insilmaril@0
  2442
			bo2=par->getFirstBranch();
insilmaril@0
  2443
			if (bo2) {
insilmaril@0
  2444
				bo1->unselect();
insilmaril@0
  2445
				selection=bo2;
insilmaril@0
  2446
				selection->select();
insilmaril@0
  2447
				ensureSelectionVisible();
insilmaril@0
  2448
			}
insilmaril@0
  2449
		}		
insilmaril@0
  2450
		adjustCanvasSize();
insilmaril@0
  2451
	}
insilmaril@0
  2452
}
insilmaril@0
  2453
insilmaril@0
  2454
void MapEditor::selectLastBranch()
insilmaril@0
  2455
{
insilmaril@0
  2456
	BranchObj *bo1;
insilmaril@0
  2457
	BranchObj *bo2;
insilmaril@0
  2458
	BranchObj* par;
insilmaril@0
  2459
	if (selection) {
insilmaril@0
  2460
		if (typeid(*selection) == typeid(BranchObj))
insilmaril@0
  2461
		{
insilmaril@163
  2462
			bo1=  (BranchObj*) selection;
insilmaril@0
  2463
			par=(BranchObj*)(bo1->getParObj());
insilmaril@0
  2464
			bo2=par->getLastBranch();
insilmaril@0
  2465
			if (bo2) {
insilmaril@0
  2466
				bo1->unselect();
insilmaril@0
  2467
				selection=bo2;
insilmaril@0
  2468
				selection->select();
insilmaril@0
  2469
				ensureSelectionVisible();
insilmaril@0
  2470
			}
insilmaril@0
  2471
		}		
insilmaril@0
  2472
		adjustCanvasSize();
insilmaril@0
  2473
	}
insilmaril@0
  2474
}
insilmaril@0
  2475
insilmaril@408
  2476
void MapEditor::selectMapBackgroundImage ()
insilmaril@408
  2477
{
insilmaril@408
  2478
	Q3FileDialog *fd=new Q3FileDialog( this);
insilmaril@408
  2479
	fd->setMode (Q3FileDialog::ExistingFile);
insilmaril@408
  2480
	fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
insilmaril@408
  2481
	ImagePreview *p =new ImagePreview (fd);
insilmaril@408
  2482
	fd->setContentsPreviewEnabled( TRUE );
insilmaril@408
  2483
	fd->setContentsPreview( p, p );
insilmaril@408
  2484
	fd->setPreviewMode( Q3FileDialog::Contents );
insilmaril@408
  2485
	fd->setCaption(vymName+" - " +tr("Load background image"));
insilmaril@408
  2486
	fd->setDir (lastImageDir);
insilmaril@408
  2487
	fd->show();
insilmaril@408
  2488
insilmaril@408
  2489
	if ( fd->exec() == QDialog::Accepted )
insilmaril@408
  2490
	{
insilmaril@408
  2491
		// FIXME in QT4 use:	lastImageDir=fd->directory();
insilmaril@408
  2492
		lastImageDir=QDir (fd->dirPath());
insilmaril@408
  2493
		setMapBackgroundImage (fd->selectedFile());
insilmaril@408
  2494
	}
insilmaril@408
  2495
}	
insilmaril@408
  2496
insilmaril@408
  2497
void MapEditor::setMapBackgroundImage (const QString &fn)
insilmaril@408
  2498
{
insilmaril@408
  2499
	QColor oldcol=mapScene->backgroundBrush().color();
insilmaril@408
  2500
	/*
insilmaril@408
  2501
	saveState(
insilmaril@408
  2502
		selection,
insilmaril@408
  2503
		QString ("setMapBackgroundImage (%1)").arg(oldcol.name()),
insilmaril@408
  2504
		selection,
insilmaril@408
  2505
		QString ("setMapBackgroundImage (%1)").arg(col.name()),
insilmaril@408
  2506
		QString("Set background color of map to %1").arg(col.name()));
insilmaril@408
  2507
	*/	
insilmaril@408
  2508
	cout << "Trying to load "<<fn.ascii()<<endl;
insilmaril@408
  2509
	QBrush brush;
insilmaril@408
  2510
	brush.setTextureImage (QPixmap (fn));
insilmaril@408
  2511
	mapScene->setBackgroundBrush(brush);
insilmaril@408
  2512
}
insilmaril@408
  2513
insilmaril@398
  2514
void MapEditor::selectMapBackgroundColor()
insilmaril@0
  2515
{
insilmaril@408
  2516
	QColor col = QColorDialog::getColor( mapScene->backgroundBrush().color(), this );
insilmaril@0
  2517
	if ( !col.isValid() ) return;
insilmaril@408
  2518
	setMapBackgroundColor( col );
insilmaril@0
  2519
}
insilmaril@0
  2520
insilmaril@398
  2521
insilmaril@398
  2522
void MapEditor::setMapBackgroundColor(QColor col)
insilmaril@0
  2523
{
insilmaril@408
  2524
	QColor oldcol=mapScene->backgroundBrush().color();
insilmaril@398
  2525
	saveState(
insilmaril@398
  2526
		selection,
insilmaril@398
  2527
		QString ("setMapBackgroundColor (%1)").arg(oldcol.name()),
insilmaril@398
  2528
		selection,
insilmaril@398
  2529
		QString ("setMapBackgroundColor (%1)").arg(col.name()),
insilmaril@398
  2530
		QString("Set background color of map to %1").arg(col.name()));
insilmaril@408
  2531
	mapScene->setBackgroundBrush(col);
insilmaril@398
  2532
}
insilmaril@398
  2533
insilmaril@398
  2534
QColor MapEditor::getMapBackgroundColor()
insilmaril@398
  2535
{
insilmaril@408
  2536
    return mapScene->backgroundBrush().color();
insilmaril@0
  2537
}
insilmaril@0
  2538
insilmaril@395
  2539
QColor MapEditor::getCurrentHeadingColor()
insilmaril@0
  2540
{
insilmaril@0
  2541
	if (selection) 
insilmaril@0
  2542
	{
insilmaril@0
  2543
		if (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2544
			typeid(*selection) == typeid(MapCenterObj))
insilmaril@0
  2545
		{
insilmaril@163
  2546
			BranchObj *bo=(BranchObj*)selection;
insilmaril@395
  2547
			return bo->getColor(); 
insilmaril@0
  2548
		}    
insilmaril@0
  2549
	}
insilmaril@395
  2550
	
insilmaril@395
  2551
	QMessageBox::warning(0,tr("Warning"),tr("Can't get color of heading,\nthere's no branch selected"));
insilmaril@395
  2552
	return Qt::black;
insilmaril@0
  2553
}
insilmaril@0
  2554
insilmaril@395
  2555
void MapEditor::colorItem(QColor c) 
insilmaril@0
  2556
{
insilmaril@0
  2557
	if (selection) 
insilmaril@0
  2558
	{
insilmaril@0
  2559
		if (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2560
			typeid(*selection) == typeid(MapCenterObj))
insilmaril@0
  2561
		{
insilmaril@163
  2562
			BranchObj *bo=(BranchObj*)selection;
insilmaril@398
  2563
			saveState(
insilmaril@395
  2564
				selection, 
insilmaril@398
  2565
				QString ("colorItem (%1)").arg(bo->getColor().name()),
insilmaril@395
  2566
				selection,
insilmaril@395
  2567
				QString ("colorItem (%1)").arg(c.name()),
insilmaril@395
  2568
				QString("Set color of %1 to %2").arg(getName(bo)).arg(c.name())
insilmaril@398
  2569
			);	
insilmaril@395
  2570
			bo->setColor(c); // color branch
insilmaril@0
  2571
		}    
insilmaril@0
  2572
	}
insilmaril@0
  2573
}
insilmaril@0
  2574
insilmaril@395
  2575
void MapEditor::colorBranch(QColor c)
insilmaril@0
  2576
{
insilmaril@0
  2577
	if (selection) 
insilmaril@0
  2578
	{
insilmaril@0
  2579
		if (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2580
			typeid(*selection) == typeid(MapCenterObj))
insilmaril@0
  2581
		{
insilmaril@163
  2582
			BranchObj *bo=(BranchObj*)selection;
insilmaril@395
  2583
			saveStateChangingPart(
insilmaril@395
  2584
				selection, 
insilmaril@395
  2585
				selection,
insilmaril@395
  2586
				QString ("colorBranch (%1)").arg(c.name()),
insilmaril@395
  2587
				QString ("Set color of %1 and childs to %2").arg(getName(bo)).arg(c.name())
insilmaril@395
  2588
			);	
insilmaril@395
  2589
			bo->setColorChilds(c); // color links, color childs
insilmaril@0
  2590
		}    
insilmaril@0
  2591
	}
insilmaril@0
  2592
}
insilmaril@0
  2593
insilmaril@0
  2594
insilmaril@0
  2595
void MapEditor::toggleStandardFlag(QString f)
insilmaril@0
  2596
{
insilmaril@0
  2597
	if (selection)
insilmaril@0
  2598
	{
insilmaril@366
  2599
		if (typeid(*selection) == typeid(BranchObj) ||
insilmaril@366
  2600
			typeid(*selection) == typeid(MapCenterObj))
insilmaril@366
  2601
		{
insilmaril@366
  2602
			BranchObj *bo=(BranchObj*)selection;
insilmaril@366
  2603
			QString u,r;
insilmaril@366
  2604
			if (bo->isSetStandardFlag(f))
insilmaril@366
  2605
			{
insilmaril@366
  2606
				r="unsetFlag";
insilmaril@366
  2607
				u="setFlag";
insilmaril@366
  2608
			}	
insilmaril@366
  2609
			else
insilmaril@366
  2610
			{
insilmaril@366
  2611
				u="unsetFlag";
insilmaril@366
  2612
				r="setFlag";
insilmaril@366
  2613
			}	
insilmaril@395
  2614
			saveState(
insilmaril@395
  2615
				selection,
insilmaril@395
  2616
				QString("%1 (\"%2\")").arg(u).arg(f), 
insilmaril@395
  2617
				selection,
insilmaril@366
  2618
				QString("%1 (\"%2\")").arg(r).arg(f),
insilmaril@366
  2619
				QString("Toggling standard flag \"%1\" of %2").arg(f).arg(getName(bo)));
insilmaril@390
  2620
			bo->toggleStandardFlag (f,mainWindow->useFlagGroups());
insilmaril@366
  2621
			adjustCanvasSize();
insilmaril@366
  2622
		}
insilmaril@0
  2623
	}	
insilmaril@0
  2624
}
insilmaril@0
  2625
insilmaril@0
  2626
void MapEditor::setViewCenter()
insilmaril@0
  2627
{
insilmaril@408
  2628
	// transform to sceneView Coord:
insilmaril@408
  2629
	QPointF p=matrix().map(movingCenter);
insilmaril@408
  2630
	//FIXME not ported yet to QT4 (center ist TextStream there)
insilmaril@408
  2631
	//center ( p.x(), p.y());
insilmaril@0
  2632
}
insilmaril@0
  2633
insilmaril@0
  2634
insilmaril@0
  2635
BranchObj* MapEditor::findText (QString s, bool cs)
insilmaril@0
  2636
{
insilmaril@366
  2637
	QTextDocument::FindFlags flags=0;
insilmaril@366
  2638
	if (cs) flags=QTextDocument::FindCaseSensitively;
insilmaril@366
  2639
insilmaril@0
  2640
	if (!itFind) 
insilmaril@0
  2641
	{	// Nothing found or new find process
insilmaril@0
  2642
		if (EOFind)
insilmaril@0
  2643
			// nothing found, start again
insilmaril@0
  2644
			EOFind=false;
insilmaril@0
  2645
		itFind=mapCenter->first();
insilmaril@0
  2646
	}	
insilmaril@0
  2647
	bool searching=true;
insilmaril@0
  2648
	bool foundNote=false;
insilmaril@0
  2649
	while (searching && !EOFind)
insilmaril@0
  2650
	{
insilmaril@0
  2651
		if (itFind)
insilmaril@0
  2652
		{
insilmaril@0
  2653
			// Searching in Note
insilmaril@0
  2654
			if (itFind->getNote().contains(s,cs))
insilmaril@0
  2655
			{
insilmaril@0
  2656
				if (selection!=itFind) 
insilmaril@0
  2657
				{
insilmaril@163
  2658
					if (selection) ((BranchObj*)selection)->unselect();
insilmaril@0
  2659
					selection=itFind;
insilmaril@0
  2660
					selection->select();
insilmaril@0
  2661
					adjustCanvasSize();
insilmaril@0
  2662
					ensureSelectionVisible();
insilmaril@0
  2663
				}
insilmaril@366
  2664
				if (textEditor->findText(s,flags)) 
insilmaril@0
  2665
				{
insilmaril@0
  2666
					searching=false;
insilmaril@0
  2667
					foundNote=true;
insilmaril@0
  2668
				}	
insilmaril@0
  2669
			}
insilmaril@0
  2670
			// Searching in Heading
insilmaril@0
  2671
			if (searching && itFind->getHeading().contains (s,cs) ) 
insilmaril@0
  2672
			{
insilmaril@163
  2673
				if (selection) ((BranchObj*)selection)->unselect();
insilmaril@0
  2674
				selection=itFind;
insilmaril@0
  2675
				selection->select();
insilmaril@0
  2676
				adjustCanvasSize();
insilmaril@0
  2677
				ensureSelectionVisible();
insilmaril@0
  2678
				searching=false;
insilmaril@0
  2679
			}
insilmaril@0
  2680
		}	
insilmaril@0
  2681
		if (!foundNote)
insilmaril@0
  2682
		{
insilmaril@0
  2683
			itFind=itFind->next();
insilmaril@0
  2684
			if (!itFind) EOFind=true;
insilmaril@0
  2685
		}
insilmaril@0
  2686
	}	
insilmaril@0
  2687
	if (!searching)
insilmaril@0
  2688
	{
insilmaril@0
  2689
		adjustCanvasSize();
insilmaril@163
  2690
		return (BranchObj*)selection;
insilmaril@0
  2691
	}	else
insilmaril@0
  2692
		return NULL;
insilmaril@0
  2693
}
insilmaril@0
  2694
insilmaril@0
  2695
void MapEditor::findReset()
insilmaril@0
  2696
{	// Necessary if text to find changes during a find process
insilmaril@0
  2697
	itFind=NULL;
insilmaril@0
  2698
	EOFind=false;
insilmaril@0
  2699
}
insilmaril@395
  2700
void MapEditor::setURL(const QString &url)
insilmaril@395
  2701
{
insilmaril@395
  2702
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@395
  2703
			typeid(*selection) == typeid(MapCenterObj)) )
insilmaril@395
  2704
	{		
insilmaril@395
  2705
		BranchObj *bo=(BranchObj*)selection;
insilmaril@395
  2706
		QString oldurl=bo->getURL();
insilmaril@395
  2707
		bo->setURL (url);
insilmaril@395
  2708
		saveState (
insilmaril@395
  2709
			selection,
insilmaril@395
  2710
			QString ("setURL (\"%1\")").arg(oldurl),
insilmaril@395
  2711
			selection,
insilmaril@395
  2712
			QString ("setURL (\"%1\")").arg(url),
insilmaril@395
  2713
			QString ("set URL of %1 to %2").arg(getName(bo)).arg(url)
insilmaril@395
  2714
		);
insilmaril@395
  2715
		updateActions();
insilmaril@395
  2716
	}
insilmaril@395
  2717
}	
insilmaril@0
  2718
insilmaril@0
  2719
void MapEditor::editURL()
insilmaril@0
  2720
{
insilmaril@0
  2721
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2722
			typeid(*selection) == typeid(MapCenterObj)) )
insilmaril@0
  2723
	{		
insilmaril@0
  2724
		bool ok;
insilmaril@163
  2725
		BranchObj *bo=(BranchObj*)selection;
insilmaril@0
  2726
		QString text = QInputDialog::getText(
insilmaril@0
  2727
				"VYM", tr("Enter URL:"), QLineEdit::Normal,
insilmaril@143
  2728
				bo->getURL(), &ok, this );
insilmaril@0
  2729
		if ( ok) 
insilmaril@0
  2730
			// user entered something and pressed OK
insilmaril@395
  2731
			setURL (text);
insilmaril@0
  2732
	}
insilmaril@0
  2733
}
insilmaril@0
  2734
insilmaril@266
  2735
QString MapEditor::getURL()
insilmaril@266
  2736
{
insilmaril@266
  2737
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@266
  2738
			typeid(*selection) == typeid(MapCenterObj)) )
insilmaril@266
  2739
		return ((BranchObj*)selection)->getURL();
insilmaril@266
  2740
	else
insilmaril@266
  2741
		return "";
insilmaril@266
  2742
}
insilmaril@266
  2743
insilmaril@366
  2744
QStringList MapEditor::getURLs()
insilmaril@366
  2745
{
insilmaril@366
  2746
	QStringList urls;
insilmaril@366
  2747
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@366
  2748
			typeid(*selection) == typeid(MapCenterObj)) )
insilmaril@366
  2749
	{		
insilmaril@366
  2750
		BranchObj *bo=(BranchObj*)selection;
insilmaril@366
  2751
		bo=bo->first();	
insilmaril@366
  2752
		while (bo) 
insilmaril@366
  2753
		{
insilmaril@366
  2754
			if (!bo->getURL().isEmpty()) urls.append( bo->getURL());
insilmaril@366
  2755
			bo=bo->next();
insilmaril@366
  2756
		}	
insilmaril@366
  2757
	}	
insilmaril@366
  2758
	return urls;
insilmaril@366
  2759
}
insilmaril@366
  2760
insilmaril@366
  2761
insilmaril@0
  2762
void MapEditor::editHeading2URL()
insilmaril@0
  2763
{
insilmaril@0
  2764
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2765
			typeid(*selection) == typeid(MapCenterObj)) )
insilmaril@395
  2766
		setURL (((BranchObj*)selection)->getHeading());
insilmaril@0
  2767
}	
insilmaril@0
  2768
insilmaril@0
  2769
void MapEditor::editBugzilla2URL()
insilmaril@0
  2770
{
insilmaril@0
  2771
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2772
			typeid(*selection) == typeid(MapCenterObj)) )
insilmaril@0
  2773
	{		
insilmaril@163
  2774
		BranchObj *bo=(BranchObj*)selection;
insilmaril@143
  2775
		QString url= "https://bugzilla.novell.com/show_bug.cgi?id="+bo->getHeading();
insilmaril@395
  2776
		setURL (url);
insilmaril@0
  2777
	}
insilmaril@0
  2778
}	
insilmaril@0
  2779
insilmaril@175
  2780
void MapEditor::editFATE2URL()
insilmaril@175
  2781
{
insilmaril@175
  2782
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@175
  2783
			typeid(*selection) == typeid(MapCenterObj)) )
insilmaril@175
  2784
	{		
insilmaril@175
  2785
		BranchObj *bo=(BranchObj*)selection;
insilmaril@175
  2786
		QString url= "http://keeper.suse.de:8080/webfate/match/id?value=ID"+bo->getHeading();
insilmaril@395
  2787
		saveState(
insilmaril@395
  2788
			selection,
insilmaril@395
  2789
			"setURL (\""+bo->getURL()+"\")",
insilmaril@395
  2790
			selection,
insilmaril@395
  2791
			"setURL (\""+url+"\")",
insilmaril@395
  2792
			QString("Use heading of %1 as link to FATE").arg(getName(bo))
insilmaril@395
  2793
		);	
insilmaril@175
  2794
		bo->setURL (url);
insilmaril@175
  2795
		updateActions();
insilmaril@175
  2796
	}
insilmaril@175
  2797
}	
insilmaril@175
  2798
insilmaril@0
  2799
void MapEditor::editVymLink()
insilmaril@0
  2800
{
insilmaril@0
  2801
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2802
			typeid(*selection) == typeid(MapCenterObj)) )
insilmaril@0
  2803
	{		
insilmaril@163
  2804
		BranchObj *bo=(BranchObj*)selection;
insilmaril@404
  2805
		QStringList filters;
insilmaril@404
  2806
		filters <<"VYM map (*.vym)";
insilmaril@404
  2807
		QFileDialog *fd=new QFileDialog( this,vymName+" - " +tr("Link to another map"));
insilmaril@404
  2808
		fd->setFilters (filters);
insilmaril@404
  2809
		fd->setCaption(vymName+" - " +tr("Link to another map"));
insilmaril@143
  2810
		if (! bo->getVymLink().isEmpty() )
insilmaril@404
  2811
			fd->selectFile( bo->getVymLink() );
insilmaril@0
  2812
		fd->show();
insilmaril@0
  2813
insilmaril@0
  2814
		QString fn;
insilmaril@0
  2815
		if ( fd->exec() == QDialog::Accepted )
insilmaril@143
  2816
		{
insilmaril@395
  2817
			saveState(
insilmaril@395
  2818
				selection,
insilmaril@395
  2819
				"setVymLink (\""+bo->getVymLink()+"\")",
insilmaril@395
  2820
				selection,
insilmaril@395
  2821
				"setVymLink (\""+fd->selectedFile()+"\")",
insilmaril@395
  2822
				QString("Set vymlink of %1 to %2").arg(getName(bo)).arg(fd->selectedFile())
insilmaril@395
  2823
			);	
insilmaril@143
  2824
			bo->setVymLink (fd->selectedFile() );
insilmaril@143
  2825
			updateActions();
insilmaril@143
  2826
			mapCenter->reposition();
insilmaril@143
  2827
			adjustCanvasSize();
insilmaril@408
  2828
			scene()->update();
insilmaril@143
  2829
		}
insilmaril@0
  2830
	}
insilmaril@0
  2831
}
insilmaril@0
  2832
insilmaril@0
  2833
void MapEditor::deleteVymLink()
insilmaril@0
  2834
{
insilmaril@0
  2835
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2836
			typeid(*selection) == typeid(MapCenterObj)) )
insilmaril@0
  2837
	{		
insilmaril@163
  2838
		BranchObj *bo=(BranchObj*)selection;
insilmaril@395
  2839
		saveState(
insilmaril@395
  2840
			selection,
insilmaril@395
  2841
			"setVymLink (\""+bo->getVymLink()+"\")",
insilmaril@395
  2842
			selection,
insilmaril@395
  2843
			"setVymLink (\"\")",
insilmaril@395
  2844
			QString("Unset vymlink of %1").arg(getName(bo))
insilmaril@395
  2845
		);	
insilmaril@143
  2846
		bo->setVymLink ("" );
insilmaril@0
  2847
		updateActions();
insilmaril@0
  2848
		mapCenter->reposition();
insilmaril@0
  2849
		adjustCanvasSize();
insilmaril@408
  2850
		scene()->update();
insilmaril@0
  2851
	}
insilmaril@0
  2852
}
insilmaril@0
  2853
insilmaril@395
  2854
void MapEditor::setHideExport(bool b)
insilmaril@243
  2855
{
insilmaril@243
  2856
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@395
  2857
			typeid(*selection)==typeid(FloatImageObj)))
insilmaril@395
  2858
	{
insilmaril@250
  2859
		OrnamentedObj *oo=(OrnamentedObj*)selection;
insilmaril@395
  2860
		oo->setHideInExport (b);
insilmaril@395
  2861
		QString u= b ? "false" : "true";
insilmaril@395
  2862
		QString r=!b ? "false" : "true";
insilmaril@395
  2863
		
insilmaril@395
  2864
		saveState(
insilmaril@395
  2865
			selection,
insilmaril@395
  2866
			QString ("setHideExport (%1)").arg(u),
insilmaril@395
  2867
			selection,
insilmaril@395
  2868
			QString ("setHideExport (%1)").arg(r),
insilmaril@395
  2869
			QString ("Set HideExport flag of %1 to %2").arg(getName(oo)).arg (r)
insilmaril@395
  2870
		);	
insilmaril@243
  2871
		updateActions();
insilmaril@243
  2872
		mapCenter->reposition();
insilmaril@243
  2873
		adjustCanvasSize();
insilmaril@408
  2874
		scene()->update();
insilmaril@243
  2875
	}
insilmaril@243
  2876
}
insilmaril@243
  2877
insilmaril@395
  2878
void MapEditor::toggleHideExport()
insilmaril@395
  2879
{
insilmaril@395
  2880
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@395
  2881
			typeid(*selection)==typeid(FloatImageObj)))
insilmaril@395
  2882
		setHideExport ( !((OrnamentedObj*)selection)->hideInExport() );
insilmaril@395
  2883
}