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