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