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