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