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