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