mapeditor.cpp
author insilmaril
Thu, 06 Jul 2006 14:47:01 +0000
branchqt4-port
changeset 9 f94317a94db1
parent 7 84f76a3876d7
child 11 b8c547f0fc58
permissions -rw-r--r--
Text Editor now shows states again
insilmaril@0
     1
#include "mapeditor.h"
insilmaril@0
     2
insilmaril@0
     3
#include <qstatusbar.h>
insilmaril@9
     4
#include <QMessageBox>
insilmaril@0
     5
#include <qapplication.h>
insilmaril@0
     6
#include <qpainter.h>
insilmaril@2
     7
#include <q3popupmenu.h>
insilmaril@0
     8
#include <qprinter.h>
insilmaril@2
     9
#include <q3paintdevicemetrics.h>
insilmaril@9
    10
#include <QFile>
insilmaril@2
    11
#include <q3filedialog.h>
insilmaril@9
    12
#include <QDir>
insilmaril@9
    13
#include <QColor>
insilmaril@9
    14
#include <QColorDialog>
insilmaril@9
    15
#include <QBitmap>
insilmaril@0
    16
#include <qinputdialog.h>
insilmaril@2
    17
#include <q3dragobject.h>
insilmaril@2
    18
#include <q3urloperator.h>
insilmaril@2
    19
#include <q3networkprotocol.h>
insilmaril@9
    20
#include <QRegExp>
insilmaril@2
    21
//Added by qt3to4:
insilmaril@2
    22
#include <QResizeEvent>
insilmaril@2
    23
#include <QContextMenuEvent>
insilmaril@2
    24
#include <Q3StrList>
insilmaril@2
    25
#include <QPixmap>
insilmaril@2
    26
#include <QMouseEvent>
insilmaril@2
    27
#include <QDragEnterEvent>
insilmaril@2
    28
#include <QTextStream>
insilmaril@2
    29
#include <QDropEvent>
insilmaril@3
    30
#include <QActionGroup>
insilmaril@2
    31
#include <Q3PtrList>
insilmaril@0
    32
insilmaril@0
    33
#include <iostream>
insilmaril@2
    34
#include <cstdlib>
insilmaril@0
    35
#include <typeinfo>
insilmaril@0
    36
insilmaril@0
    37
#include "version.h"
insilmaril@0
    38
insilmaril@2
    39
#include "api.h"
insilmaril@2
    40
#include "editxlinkdialog.h"
insilmaril@2
    41
#include "exports.h"
insilmaril@2
    42
#include "extrainfodialog.h"
insilmaril@2
    43
#include "file.h"
insilmaril@2
    44
#include "linkablemapobj.h"
insilmaril@2
    45
#include "mainwindow.h"
insilmaril@2
    46
#include "misc.h"
insilmaril@2
    47
#include "settings.h"
insilmaril@2
    48
#include "texteditor.h"
insilmaril@2
    49
#include "warningdialog.h"
insilmaril@0
    50
#include "xml.h"
insilmaril@2
    51
insilmaril@0
    52
insilmaril@0
    53
extern TextEditor *textEditor;
insilmaril@0
    54
extern int statusbarTime;
insilmaril@0
    55
extern Main *mainWindow;
insilmaril@2
    56
extern QString tmpVymDir;
insilmaril@2
    57
extern QString clipboardDir;
insilmaril@2
    58
extern bool clipboardEmpty;
insilmaril@0
    59
extern FlagRowObj *systemFlagsDefault;
insilmaril@0
    60
extern FlagRowObj *standardFlagsDefault;
insilmaril@2
    61
insilmaril@2
    62
extern Q3PtrList <QAction> actionListBranches;
insilmaril@0
    63
insilmaril@0
    64
extern QAction *actionFileSave;
insilmaril@0
    65
extern QAction *actionEditUndo;
insilmaril@0
    66
extern QAction *actionEditCopy;
insilmaril@0
    67
extern QAction *actionEditCut;
insilmaril@0
    68
extern QAction *actionEditPaste;
insilmaril@0
    69
extern QAction *actionEditMoveUp;
insilmaril@0
    70
extern QAction *actionEditMoveDown;
insilmaril@0
    71
extern QAction *actionEditToggleScroll;
insilmaril@0
    72
extern QAction *actionEditOpenURL;
insilmaril@2
    73
extern QAction *actionEditOpenURLTab;
insilmaril@0
    74
extern QAction *actionEditURL;
insilmaril@0
    75
extern QAction *actionEditHeading2URL;
insilmaril@0
    76
extern QAction *actionEditBugzilla2URL;
insilmaril@2
    77
extern QAction *actionEditFATE2URL;
insilmaril@0
    78
extern QAction *actionEditOpenVymLink;
insilmaril@0
    79
extern QAction *actionEditVymLink;
insilmaril@0
    80
extern QAction *actionEditDeleteVymLink;
insilmaril@2
    81
extern QAction *actionEditToggleHideExport;
insilmaril@0
    82
extern QAction *actionEditHeading;
insilmaril@0
    83
extern QAction *actionEditDelete;
insilmaril@0
    84
extern QAction *actionEditAddBranch;
insilmaril@0
    85
extern QAction *actionEditAddBranchAbove;
insilmaril@0
    86
extern QAction *actionEditAddBranchBelow;
insilmaril@2
    87
extern QAction *actionEditRemoveBranchHere;
insilmaril@2
    88
extern QAction *actionEditRemoveChilds;
insilmaril@0
    89
extern QAction *actionEditImportAdd;
insilmaril@0
    90
extern QAction *actionEditImportReplace;
insilmaril@0
    91
extern QAction *actionEditSaveBranch;
insilmaril@0
    92
extern QAction *actionEditSelectFirst;
insilmaril@0
    93
extern QAction *actionEditSelectLast;
insilmaril@0
    94
extern QAction *actionEditLoadImage;
insilmaril@0
    95
insilmaril@0
    96
extern QAction* actionFormatPickColor;
insilmaril@0
    97
extern QAction* actionFormatColorBranch;
insilmaril@0
    98
extern QAction* actionFormatColorSubtree;
insilmaril@0
    99
extern QAction *actionFormatLinkColorHint;
insilmaril@0
   100
extern QAction *actionFormatBackColor;
insilmaril@0
   101
extern QAction *actionFormatLinkColor;
insilmaril@0
   102
insilmaril@3
   103
extern QActionGroup* actionGroupModModes;
insilmaril@2
   104
extern QAction* actionModModeColor;
insilmaril@2
   105
extern QAction* actionModModeLink;
insilmaril@2
   106
extern QAction* actionModModeCopy;
insilmaril@2
   107
insilmaril@3
   108
extern QActionGroup *actionGroupFormatFrameTypes;
insilmaril@0
   109
extern QAction *actionFormatFrameNone;
insilmaril@0
   110
extern QAction *actionFormatFrameRectangle;
insilmaril@0
   111
insilmaril@3
   112
extern QActionGroup *actionGroupFormatLinkStyles;
insilmaril@2
   113
extern QAction *actionFormatIncludeImagesVer;
insilmaril@2
   114
extern QAction *actionFormatIncludeImagesHor;
insilmaril@2
   115
extern QAction *actionFormatHideLinkUnselected;
insilmaril@0
   116
extern QAction *actionFormatLinkStyleLine;
insilmaril@0
   117
extern QAction *actionFormatLinkStyleParabel;
insilmaril@0
   118
extern QAction *actionFormatLinkStylePolyLine;
insilmaril@0
   119
extern QAction *actionFormatLinkStylePolyParabel;
insilmaril@0
   120
insilmaril@0
   121
extern QAction *actionViewToggleNoteEditor;
insilmaril@0
   122
insilmaril@0
   123
extern QAction *actionSettingsAutoedit;
insilmaril@0
   124
extern QAction *actionSettingsAutoselectHeading;
insilmaril@0
   125
extern QAction *actionSettingsAutoselectText;
insilmaril@0
   126
extern QAction *actionSettingsPasteNewHeading;
insilmaril@2
   127
extern QAction *actionSettingsUseFlagGroups;
insilmaril@2
   128
insilmaril@4
   129
extern QMenu* branchContextMenu;
insilmaril@4
   130
extern QMenu* branchAddContextMenu;
insilmaril@4
   131
extern QMenu* branchRemoveContextMenu;
insilmaril@7
   132
extern QMenu* branchLinksContextMenuEdit;
insilmaril@7
   133
extern QMenu* branchLinksContextMenuFollow;
insilmaril@4
   134
extern QMenu* floatimageContextMenu;
insilmaril@4
   135
extern QMenu* saveImageFormatMenu;
insilmaril@4
   136
extern QMenu* canvasContextMenu;
insilmaril@4
   137
extern QMenu* lastMapsMenu;
insilmaril@4
   138
extern QMenu* importMenu;
insilmaril@4
   139
extern QMenu* exportMenu;
insilmaril@4
   140
insilmaril@0
   141
insilmaril@0
   142
extern Settings settings;
insilmaril@4
   143
extern ImageIO imageIO;
insilmaril@0
   144
insilmaril@2
   145
extern QString iconPath;
insilmaril@2
   146
extern QDir vymBaseDir;
insilmaril@4
   147
extern QDir lastImageDir;
insilmaril@2
   148
insilmaril@2
   149
int MapEditor::mapNum=0;	// make instance
insilmaril@0
   150
insilmaril@0
   151
///////////////////////////////////////////////////////////////////////
insilmaril@0
   152
///////////////////////////////////////////////////////////////////////
insilmaril@0
   153
MapEditor::MapEditor(
insilmaril@2
   154
	QWidget* parent, bool interactive, const char* name, Qt::WFlags f) :
insilmaril@2
   155
  Q3CanvasView(parent,name,f), urlOperator(0), imageBuffer(0)
insilmaril@0
   156
{
insilmaril@0
   157
	//cout << "Constructor ME "<<this<<endl;
insilmaril@2
   158
	mapNum++;
insilmaril@2
   159
insilmaril@2
   160
    viewport()->setAcceptDrops(true);
insilmaril@2
   161
insilmaril@2
   162
    mapCanvas = new Q3Canvas(1000,800);
insilmaril@0
   163
	mapCanvas->setAdvancePeriod(30);
insilmaril@2
   164
	mapCanvas->setBackgroundColor (Qt::white);
insilmaril@0
   165
insilmaril@0
   166
    setCanvas (mapCanvas);
insilmaril@0
   167
	
insilmaril@2
   168
	// Always show scroll bars (automatic would flicker sometimes)
insilmaril@2
   169
	setVScrollBarMode ( Q3ScrollView::AlwaysOn );
insilmaril@2
   170
	setHScrollBarMode ( Q3ScrollView::AlwaysOn );
insilmaril@2
   171
insilmaril@0
   172
    mapCenter = new MapCenterObj(mapCanvas);
insilmaril@0
   173
    mapCenter->setVisibility (true);
insilmaril@0
   174
	mapCenter->setMapEditor (this);
insilmaril@2
   175
	mapCenter->setHeading (tr("New Map","Heading of mapcenter in new map"));
insilmaril@2
   176
	mapCenter->move(mapCanvas->width()/2-mapCenter->width()/2,mapCanvas->height()/2-mapCenter->height()/2);
insilmaril@0
   177
insilmaril@0
   178
    printer=NULL;
insilmaril@0
   179
insilmaril@0
   180
    lineedit = new QLineEdit(this, "lineedit"  );
insilmaril@2
   181
    connect( lineedit, SIGNAL( returnPressed() ), SLOT( finishedLineEdit() ) );
insilmaril@0
   182
    lineedit->hide();
insilmaril@0
   183
insilmaril@2
   184
    actColor=Qt::black; setColor (actColor);
insilmaril@2
   185
	defLinkColor=QColor (0,0,255);
insilmaril@2
   186
	defXLinkColor=QColor (180,180,180);
insilmaril@0
   187
	linkcolorhint=DefaultColor;
insilmaril@0
   188
	linkstyle=StylePolyParabel;
insilmaril@0
   189
insilmaril@0
   190
	// Create bitmap cursors, patform dependant
insilmaril@0
   191
	#if defined(Q_OS_MACX)
insilmaril@2
   192
		handOpenCursor=QCursor ( QPixmap(iconPath+"cursorhandopen16.png") );		
insilmaril@0
   193
		// set hot spot to tip of picker			
insilmaril@2
   194
		pickColorCursor=QCursor ( QPixmap (iconPath+"cursorcolorpicker16.png"), 1,15 ); 
insilmaril@0
   195
	#else
insilmaril@2
   196
		handOpenCursor=QCursor (QPixmap(iconPath+"cursorhandopen16.png"));		
insilmaril@0
   197
		// set hot spot to tip of picker			
insilmaril@2
   198
		pickColorCursor=QCursor ( QPixmap(iconPath+"cursorcolorpicker.png"), 5,27 ); 
insilmaril@0
   199
	#endif
insilmaril@0
   200
insilmaril@0
   201
	pickingColor=false;
insilmaril@2
   202
	drawingLink=false;
insilmaril@2
   203
	copyingObj=false;
insilmaril@0
   204
insilmaril@0
   205
    editingBO=NULL;
insilmaril@0
   206
    selection=NULL;
insilmaril@0
   207
    selectionLast=NULL;
insilmaril@0
   208
    movingObj=NULL;
insilmaril@0
   209
insilmaril@2
   210
	defXLinkWidth=1;
insilmaril@2
   211
	defXLinkColor=QColor (230,230,230);
insilmaril@2
   212
insilmaril@0
   213
    mapChanged=false;
insilmaril@0
   214
	mapDefault=true;
insilmaril@0
   215
	mapUnsaved=false;
insilmaril@0
   216
	
insilmaril@0
   217
	zipped=true;
insilmaril@0
   218
	filePath="";
insilmaril@2
   219
	fileName=tr("unnamed");
insilmaril@0
   220
	mapName="";
insilmaril@0
   221
insilmaril@2
   222
	undosTotal=settings.readNumEntry("/vym/mapeditor/undoLevels",50);
insilmaril@2
   223
	undosAvail=0;	
insilmaril@2
   224
	undoNum=0;
insilmaril@2
   225
	
insilmaril@0
   226
	// Initialize find routine
insilmaril@0
   227
	itFind=NULL;				
insilmaril@0
   228
	EOFind=false;
insilmaril@0
   229
insilmaril@0
   230
	printFrame=true;
insilmaril@0
   231
	printFooter=true;
insilmaril@0
   232
insilmaril@2
   233
	blockReposition=false;
insilmaril@2
   234
	blockSaveState=false;
insilmaril@2
   235
insilmaril@2
   236
	hidemode=HideNone;
insilmaril@2
   237
insilmaril@0
   238
	isInteractive=interactive;
insilmaril@0
   239
	if (isInteractive)
insilmaril@0
   240
		// Create temporary files
insilmaril@0
   241
		makeTmpDirs();
insilmaril@0
   242
insilmaril@0
   243
	// Initially set movingCentre
insilmaril@0
   244
	updateViewCenter();
insilmaril@0
   245
insilmaril@2
   246
	// For testing purposes create history window
insilmaril@2
   247
	historyWindow = new ShowTextDialog (this);
insilmaril@2
   248
	historyWindow->setCaption (fileName);
insilmaril@2
   249
insilmaril@0
   250
	mapCenter->reposition();	//	for positioning heading
insilmaril@0
   251
}
insilmaril@0
   252
insilmaril@0
   253
MapEditor::~MapEditor()
insilmaril@0
   254
{
insilmaril@2
   255
  if (imageBuffer) delete imageBuffer;
insilmaril@2
   256
  if (urlOperator) {
insilmaril@2
   257
    urlOperator->stop();
insilmaril@2
   258
    delete urlOperator;
insilmaril@2
   259
  }
insilmaril@2
   260
insilmaril@0
   261
	//cout <<"Destructor MapEditor\n";
insilmaril@0
   262
insilmaril@0
   263
	// Save Settings
insilmaril@0
   264
	//settings.writeEntry( "/vym/mapeditor/editmode/autoselect",  );
insilmaril@0
   265
insilmaril@0
   266
}
insilmaril@0
   267
insilmaril@0
   268
QColor MapEditor::color()
insilmaril@0
   269
{
insilmaril@0
   270
    return actColor;
insilmaril@0
   271
}
insilmaril@0
   272
insilmaril@0
   273
QColor MapEditor::backgroundColor()
insilmaril@0
   274
{
insilmaril@0
   275
    return mapCanvas->backgroundColor();
insilmaril@0
   276
}
insilmaril@0
   277
insilmaril@0
   278
MapCenterObj* MapEditor::getMapCenter()
insilmaril@0
   279
{
insilmaril@0
   280
    return mapCenter;
insilmaril@0
   281
}
insilmaril@0
   282
insilmaril@2
   283
Q3Canvas* MapEditor::getCanvas()
insilmaril@0
   284
{
insilmaril@0
   285
    return mapCanvas;
insilmaril@0
   286
}
insilmaril@0
   287
insilmaril@0
   288
void MapEditor::adjustCanvasSize()
insilmaril@0
   289
{
insilmaril@0
   290
	// To adjust the canvas to map, viewport size and position, we have to
insilmaril@0
   291
	// do some coordinate magic...
insilmaril@0
   292
	//
insilmaril@0
   293
	// Get rectangle of (scroll-)view. 
insilmaril@0
   294
	// We want to be in canvas coords, so
insilmaril@0
   295
	// we map. Important if view is zoomed...
insilmaril@0
   296
	QRect view = inverseWorldMatrix().mapRect( QRect( contentsX(), contentsY(),
insilmaril@0
   297
												visibleWidth(), visibleHeight()) );	
insilmaril@0
   298
												
insilmaril@0
   299
	// Now we need the bounding box of view AND map to calc the correct canvas size.
insilmaril@0
   300
	// Why? Because if the map itself is moved out of view, the view has to be enlarged
insilmaril@0
   301
	// to avoid jumping aroung...
insilmaril@0
   302
	QRect map=mapCenter->getTotalBBox();
insilmaril@0
   303
insilmaril@0
   304
	// right edge - left edge
insilmaril@0
   305
	int cw= max(map.x() + map.width(),  view.x() + view.width())  - min(map.x(), view.x());
insilmaril@0
   306
	int ch= max(map.y() + map.height(), view.y() + view.height()) - min(map.y(), view.y());
insilmaril@0
   307
insilmaril@0
   308
insilmaril@0
   309
	if ( (cw!=mapCanvas->width()) || (ch!=mapCanvas->height()) ||
insilmaril@0
   310
		!mapCanvas->onCanvas (map.topLeft()) || !mapCanvas->onCanvas (map.bottomRight())
insilmaril@0
   311
	)	
insilmaril@0
   312
	{	
insilmaril@0
   313
		// move the map on canvas (in order to not move it on screen) this is neccessary
insilmaril@0
   314
		// a) if topleft corner of canvas is left or above topleft corner of view and also left of
insilmaril@0
   315
		//    above topleft corner of map. E.g. if map is completly inside view, but it would be possible 
insilmaril@0
   316
		//    to scroll to an empty area of canvas to the left.
insilmaril@0
   317
		// b) if topleft corner of map left of or above topleft of canvas
insilmaril@0
   318
		int dx=0;
insilmaril@0
   319
		int dy=0;
insilmaril@0
   320
insilmaril@0
   321
		if (cw > mapCanvas->width() )
insilmaril@0
   322
		{
insilmaril@0
   323
			if (map.x()<0) dx=-map.x();	
insilmaril@0
   324
		}
insilmaril@0
   325
		if (cw <  mapCanvas->width() )
insilmaril@0
   326
			dx=-min (view.x(),map.x());
insilmaril@0
   327
		if (ch > mapCanvas->height() )
insilmaril@0
   328
		{
insilmaril@0
   329
			if (map.y()<0) dy=-map.y();	
insilmaril@0
   330
		}
insilmaril@0
   331
		if (ch <  mapCanvas->height() )
insilmaril@0
   332
		{
insilmaril@0
   333
			dy=-min (view.y(),map.y());
insilmaril@0
   334
		}
insilmaril@0
   335
		// We really have to resize now. Let's go...
insilmaril@0
   336
		mapCanvas->resize (cw,ch);
insilmaril@0
   337
		if ( (dx!=0) || (dy!=0) ) 
insilmaril@0
   338
		{
insilmaril@0
   339
			mapCenter->moveAllBy(dx,dy);
insilmaril@0
   340
			mapCenter->reposition();
insilmaril@2
   341
//			mapCenter->positionBBox();	// To move float
insilmaril@0
   342
insilmaril@0
   343
			// scroll the view (in order to not move map on screen)
insilmaril@0
   344
			scrollBy (dx,dy);
insilmaril@0
   345
		}	
insilmaril@0
   346
	}
insilmaril@0
   347
}
insilmaril@0
   348
insilmaril@2
   349
bool MapEditor::isRepositionBlocked()
insilmaril@0
   350
{
insilmaril@2
   351
	return blockReposition;
insilmaril@2
   352
}
insilmaril@2
   353
insilmaril@2
   354
QString MapEditor::getName (LinkableMapObj *lmo)
insilmaril@2
   355
{
insilmaril@2
   356
	QString s;
insilmaril@2
   357
	if (!lmo) return QString("Error: NULL has no name!");
insilmaril@2
   358
insilmaril@2
   359
	if ((typeid(*lmo) == typeid(BranchObj) ||
insilmaril@2
   360
				      typeid(*lmo) == typeid(MapCenterObj))) 
insilmaril@2
   361
	{
insilmaril@2
   362
		
insilmaril@2
   363
		s=(((BranchObj*)lmo)->getHeading());
insilmaril@2
   364
		if (s=="") s="unnamed";
insilmaril@2
   365
		return QString("branch (<font color=\"#0000ff\">%1</font>)").arg(s);
insilmaril@2
   366
	}	
insilmaril@2
   367
	if ((typeid(*lmo) == typeid(FloatImageObj) ))
insilmaril@2
   368
		return QString ("floatimage [<font color=\"#0000ff\">%1</font>]").arg(((FloatImageObj*)lmo)->getOriginalFilename());
insilmaril@2
   369
	return QString("Unknown type has no name!");
insilmaril@0
   370
}
insilmaril@0
   371
insilmaril@0
   372
void MapEditor::makeTmpDirs()
insilmaril@0
   373
{
insilmaril@0
   374
	// Create unique temporary directories
insilmaril@2
   375
	tmpMapDir=tmpVymDir+QString("/mapeditor-%1").arg(mapNum);
insilmaril@2
   376
	QDir d;
insilmaril@2
   377
	d.mkdir (tmpMapDir,true);
insilmaril@0
   378
}
insilmaril@0
   379
insilmaril@2
   380
QString MapEditor::saveToDir(const QString &tmpdir, const QString &prefix, bool writeflags, const QPoint &offset, LinkableMapObj *saveSelection)
insilmaril@0
   381
{
insilmaril@2
   382
	// tmpdir		temporary directory to which data will be written
insilmaril@0
   383
	// prefix		mapname, which will be appended to images etc.
insilmaril@0
   384
	// writeflags	Only write flags for "real" save of map, not undo
insilmaril@0
   385
	// offset		offset of bbox of whole map in canvas. 
insilmaril@0
   386
	//				Needed for XML export
insilmaril@0
   387
	
insilmaril@0
   388
	// Save Header
insilmaril@0
   389
	QString ls;
insilmaril@0
   390
	switch (linkstyle)
insilmaril@0
   391
	{
insilmaril@0
   392
		case StyleLine: 
insilmaril@0
   393
			ls="StyleLine";
insilmaril@0
   394
			break;
insilmaril@0
   395
		case StyleParabel:
insilmaril@0
   396
			ls="StyleParabel";
insilmaril@0
   397
			break;
insilmaril@0
   398
		case StylePolyLine:	
insilmaril@0
   399
			ls="StylePolyLine";
insilmaril@0
   400
			break;
insilmaril@0
   401
		default:
insilmaril@0
   402
			ls="StylePolyParabel";
insilmaril@0
   403
			break;
insilmaril@0
   404
	}	
insilmaril@0
   405
insilmaril@0
   406
	QString s="<?xml version=\"1.0\" encoding=\"utf-8\"?><!DOCTYPE vymmap>\n";
insilmaril@0
   407
	QString colhint="";
insilmaril@0
   408
	if (linkcolorhint==HeadingColor) 
insilmaril@0
   409
		colhint=attribut("linkColorHint","HeadingColor");
insilmaril@0
   410
insilmaril@2
   411
	QString mapAttr=attribut("version",__VYM_VERSION);
insilmaril@2
   412
	if (!saveSelection)
insilmaril@0
   413
		mapAttr+= attribut("author",mapCenter->getAuthor()) +
insilmaril@0
   414
				  attribut("comment",mapCenter->getComment()) +
insilmaril@0
   415
			      attribut("date",mapCenter->getDate()) +
insilmaril@0
   416
		          attribut("backgroundColor", mapCanvas->backgroundColor().name() ) +
insilmaril@0
   417
		          attribut("linkStyle", ls ) +
insilmaril@2
   418
		          attribut("linkColor", defLinkColor.name() ) +
insilmaril@2
   419
		          attribut("defXLinkColor", defXLinkColor.name() ) +
insilmaril@2
   420
		          attribut("defXLinkWidth", QString().setNum(defXLinkWidth,10) ) +
insilmaril@0
   421
		          colhint; 
insilmaril@0
   422
	s+=beginElement("vymmap",mapAttr);
insilmaril@0
   423
	incIndent();
insilmaril@0
   424
insilmaril@0
   425
	// Find the used flags while traversing the tree
insilmaril@0
   426
	standardFlagsDefault->resetUsedCounter();
insilmaril@0
   427
	
insilmaril@2
   428
	// Reset the counters before saving
insilmaril@2
   429
	FloatImageObj (mapCanvas).resetSaveCounter();
insilmaril@2
   430
insilmaril@0
   431
	// Build xml recursivly
insilmaril@2
   432
	if (!saveSelection)
insilmaril@0
   433
		s+=mapCenter->saveToDir(tmpdir,prefix,writeflags,offset);
insilmaril@0
   434
	else
insilmaril@0
   435
	{
insilmaril@2
   436
		if ( typeid(*saveSelection) == typeid(BranchObj) )
insilmaril@2
   437
			s+=((BranchObj*)(saveSelection))->saveToDir(tmpdir,prefix,offset);
insilmaril@2
   438
		else if ( typeid(*saveSelection) == typeid(FloatImageObj) )
insilmaril@2
   439
			s+=((FloatImageObj*)(saveSelection))->saveToDir(tmpdir,prefix,offset);
insilmaril@2
   440
			
insilmaril@2
   441
		else if (selection && typeid(*selection)==typeid(BranchObj))
insilmaril@2
   442
				// This is used if selected branch is saved from mainwindow
insilmaril@2
   443
				s+=((BranchObj*)selection)->saveToDir(tmpdir,prefix,offset);
insilmaril@0
   444
	}
insilmaril@0
   445
insilmaril@0
   446
	// Save local settings
insilmaril@0
   447
	s+=settings.getXMLData (destPath);
insilmaril@0
   448
insilmaril@0
   449
	// Save selection
insilmaril@2
   450
	if (selection && !saveSelection ) 
insilmaril@0
   451
		s+=valueElement("select",selection->getSelectString());
insilmaril@0
   452
insilmaril@0
   453
	decIndent();
insilmaril@0
   454
	s+=endElement("vymmap");
insilmaril@0
   455
insilmaril@0
   456
	if (writeflags)
insilmaril@0
   457
		standardFlagsDefault->saveToDir (tmpdir+"/flags/","",writeflags);
insilmaril@0
   458
	return s;
insilmaril@0
   459
}
insilmaril@0
   460
insilmaril@2
   461
void MapEditor::saveState(const QString &comment)
insilmaril@0
   462
{
insilmaril@2
   463
	// Save complete map
insilmaril@2
   464
	saveState (CompleteMap,"",NULL,"",NULL, comment);
insilmaril@0
   465
}
insilmaril@0
   466
insilmaril@2
   467
void MapEditor::saveState(LinkableMapObj *undoSel, const QString &comment)
insilmaril@0
   468
{
insilmaril@2
   469
	// save the given part of the map 
insilmaril@2
   470
	saveState (PartOfMap,"",undoSel,"",NULL, comment);
insilmaril@2
   471
}
insilmaril@2
   472
insilmaril@2
   473
void MapEditor::saveState(const QString &uc, const QString &rc, const QString &comment)
insilmaril@2
   474
{
insilmaril@2
   475
	// selection does not change during action,
insilmaril@2
   476
	// so just save commands for undo and redo
insilmaril@2
   477
	LinkableMapObj *unsel;
insilmaril@2
   478
	if (selection)
insilmaril@2
   479
		unsel=selection;
insilmaril@2
   480
	else
insilmaril@2
   481
		unsel=NULL;
insilmaril@2
   482
	saveState (UndoCommand,uc,unsel,rc,unsel, comment);
insilmaril@2
   483
}
insilmaril@2
   484
insilmaril@2
   485
void MapEditor::saveState(const QString & uncom, LinkableMapObj *unsel, const QString &comment) 
insilmaril@2
   486
{
insilmaril@2
   487
	saveState (UndoCommand,uncom,unsel,"FIXME-redoCom",NULL, comment);
insilmaril@2
   488
}
insilmaril@2
   489
insilmaril@2
   490
void MapEditor::saveState(const SaveMode &savemode, const QString &undoCom, LinkableMapObj *undoSel, const QString &redoCom, LinkableMapObj *redoSel, const QString &comment)
insilmaril@2
   491
{
insilmaril@2
   492
	// Main saveState
insilmaril@2
   493
insilmaril@2
   494
	if (blockSaveState) return;
insilmaril@2
   495
insilmaril@2
   496
	/* TODO remove after testing
insilmaril@2
   497
	cout << "ME::saveState()  begin\n"<<endl;
insilmaril@2
   498
	cout << "    undosTotal="<<undosTotal<<endl;
insilmaril@2
   499
	cout << "    undosAvail="<<undosAvail<<endl;
insilmaril@2
   500
	cout << "       undoNum="<<undoNum<<endl;
insilmaril@2
   501
	cout << "    ---------------------------"<<endl;
insilmaril@2
   502
	*/
insilmaril@2
   503
insilmaril@2
   504
	historyWindow->append (comment);
insilmaril@2
   505
	
insilmaril@2
   506
	setChanged();
insilmaril@2
   507
insilmaril@2
   508
	// Find out current undo directory
insilmaril@2
   509
	if (undosAvail<undosTotal) undosAvail++;
insilmaril@2
   510
	undoNum++;
insilmaril@2
   511
	if (undoNum>undosTotal) undoNum=1;
insilmaril@2
   512
	
insilmaril@2
   513
	QString backupXML;
insilmaril@2
   514
	QString bakMapDir=QDir::convertSeparators (QString(tmpMapDir+"/undo-%1").arg(undoNum));
insilmaril@2
   515
	QString bakMapPath=QDir::convertSeparators(bakMapDir+"/map.xml");
insilmaril@2
   516
insilmaril@2
   517
	// Create bakMapDir if not available
insilmaril@2
   518
	QDir d(bakMapDir);
insilmaril@2
   519
	if (!d.exists()) 
insilmaril@2
   520
		makeSubDirs (bakMapDir);
insilmaril@2
   521
insilmaril@2
   522
	// Save current selection 
insilmaril@2
   523
	QString redoSelection="";
insilmaril@2
   524
	if (redoSel)
insilmaril@2
   525
		redoSelection=redoSel->getSelectString();
insilmaril@2
   526
insilmaril@2
   527
	// Save the object, which should be undone
insilmaril@2
   528
	QString undoSelection="";
insilmaril@2
   529
	if (undoSel)
insilmaril@2
   530
		undoSelection=undoSel->getSelectString();
insilmaril@2
   531
		
insilmaril@2
   532
	// Save depending on how much needs to be saved	
insilmaril@2
   533
	QString undoCommand="";
insilmaril@2
   534
	if (savemode==UndoCommand)
insilmaril@0
   535
	{
insilmaril@2
   536
		undoCommand=undoCom;
insilmaril@2
   537
		backupXML="";
insilmaril@2
   538
	}	
insilmaril@2
   539
	else if (savemode==PartOfMap && undoSel)
insilmaril@0
   540
	{
insilmaril@2
   541
		undoCommand="undoPart (\""+ undoSelection+"\",\""+bakMapPath+"\")";
insilmaril@2
   542
		backupXML=saveToDir (bakMapDir,mapName+"-",false, QPoint (),undoSel);
insilmaril@2
   543
	} else
insilmaril@2
   544
	{
insilmaril@2
   545
		undoCommand="undoMap (\""+bakMapPath+"\")";
insilmaril@2
   546
		backupXML=saveToDir (bakMapDir,mapName+"-",false, QPoint (),NULL);
insilmaril@2
   547
		undoSelection="";
insilmaril@2
   548
	}
insilmaril@2
   549
	if (!backupXML.isEmpty())
insilmaril@2
   550
		// Write XML Data to disk
insilmaril@2
   551
		saveStringToDisk (QString(bakMapPath),backupXML);
insilmaril@2
   552
insilmaril@2
   553
	SimpleSettings set;
insilmaril@2
   554
	set.setEntry (QString("undoCommand"),undoCommand);
insilmaril@2
   555
	set.setEntry (QString("undoSelection"),undoSelection);
insilmaril@2
   556
	set.setEntry (QString("redoCommand"),redoCom);
insilmaril@2
   557
	set.setEntry (QString("redoSelection"),redoSelection);
insilmaril@2
   558
	set.setEntry (QString("comment"),comment);
insilmaril@2
   559
	set.writeSettings(QString(bakMapDir+"/commands"));
insilmaril@2
   560
insilmaril@2
   561
	/* TODO remove after testing
insilmaril@2
   562
	cout << "          into="<< bakMapDir<<endl;
insilmaril@2
   563
	cout << "    undosAvail="<<undosAvail<<endl;
insilmaril@2
   564
	cout << "       undoNum="<<undoNum<<endl;
insilmaril@2
   565
	cout << "    ---------------------------"<<endl;
insilmaril@2
   566
	cout << "    comment="<<comment<<endl;
insilmaril@2
   567
	cout << "    ---------------------------"<<endl;
insilmaril@2
   568
	cout << "    undoCom="<<undoCommand<<endl;
insilmaril@2
   569
	cout << "    undoSel="<<undoSelection<<endl;
insilmaril@2
   570
	cout << "    ---------------------------"<<endl;
insilmaril@2
   571
	cout << "    redoCom="<<redoCom<<endl;
insilmaril@2
   572
	cout << "    redoSel="<<redoSelection<<endl;
insilmaril@2
   573
	cout << "    ---------------------------"<<endl<<endl;
insilmaril@2
   574
	*/
insilmaril@2
   575
}
insilmaril@2
   576
insilmaril@2
   577
void MapEditor::parseAtom(const QString &atom)
insilmaril@2
   578
{
insilmaril@2
   579
	API api;
insilmaril@2
   580
	QString s,t;
insilmaril@2
   581
	int x,y;
insilmaril@2
   582
	bool ok;
insilmaril@2
   583
insilmaril@2
   584
	// Split string s into command and parameters
insilmaril@2
   585
	api.parseCommand (atom);
insilmaril@2
   586
	QString com=api.command();
insilmaril@2
   587
	
insilmaril@2
   588
	// External commands
insilmaril@2
   589
	if (com=="moveBranchUp")
insilmaril@2
   590
		moveBranchUp();
insilmaril@2
   591
	else if (com=="moveBranchDown")
insilmaril@2
   592
		moveBranchDown();
insilmaril@2
   593
	else if (com=="move")
insilmaril@2
   594
	{
insilmaril@2
   595
		if (api.checkParamCount(2) && selection )
insilmaril@2
   596
		{	
insilmaril@2
   597
			x=api.parInt (ok,0);
insilmaril@2
   598
			if (ok)
insilmaril@2
   599
			{
insilmaril@2
   600
				y=api.parInt (ok,1);
insilmaril@2
   601
				if (ok) move (x,y);
insilmaril@2
   602
			}
insilmaril@2
   603
		}	
insilmaril@2
   604
	}
insilmaril@2
   605
	else if (com=="linkBranchToPos")
insilmaril@2
   606
	{
insilmaril@2
   607
		if (selection && typeid(*selection) == typeid(BranchObj) ) 
insilmaril@2
   608
		{
insilmaril@2
   609
			if (api.checkParamCount(4))
insilmaril@2
   610
			{
insilmaril@2
   611
				s=api.parString(ok,0);
insilmaril@2
   612
				LinkableMapObj *dst=mapCenter->findObjBySelect (s);
insilmaril@2
   613
				if (dst)
insilmaril@2
   614
				{	
insilmaril@2
   615
					if (typeid(*dst) == typeid(BranchObj) ) 
insilmaril@2
   616
					{
insilmaril@2
   617
						// Get number in parent
insilmaril@2
   618
						x=api.parInt (ok,1);
insilmaril@2
   619
						if (ok)
insilmaril@2
   620
							((BranchObj*)selection)->moveBranchTo ((BranchObj*)(dst),x);
insilmaril@2
   621
					} else if (typeid(*dst) == typeid(MapCenterObj) ) 
insilmaril@2
   622
					{
insilmaril@2
   623
						((BranchObj*)selection)->moveBranchTo ((BranchObj*)(dst),-1);
insilmaril@2
   624
						// Get coordinates of mainbranch
insilmaril@2
   625
						x=api.parInt (ok,2);
insilmaril@2
   626
						if (ok)
insilmaril@2
   627
						{
insilmaril@2
   628
							y=api.parInt (ok,3);
insilmaril@2
   629
							if (ok) ((BranchObj*)selection)->move (x,y);
insilmaril@2
   630
						}
insilmaril@2
   631
					}	
insilmaril@2
   632
				}	
insilmaril@2
   633
			}
insilmaril@2
   634
		}
insilmaril@2
   635
	} else if (com=="setHeading")
insilmaril@2
   636
	{
insilmaril@2
   637
		if (api.checkParamCount(1))
insilmaril@2
   638
		{
insilmaril@2
   639
			s=api.parString (ok,0);
insilmaril@2
   640
			if (ok) setHeading (s);
insilmaril@2
   641
		}	
insilmaril@2
   642
	} else if (com=="setURL")
insilmaril@2
   643
	{
insilmaril@2
   644
		if (api.checkParamCount(1))
insilmaril@2
   645
		{
insilmaril@2
   646
			s=api.parString (ok,0);
insilmaril@2
   647
			if (ok) setURL(s);
insilmaril@2
   648
		}	
insilmaril@2
   649
	} else if (com=="setVymLink")
insilmaril@2
   650
	{
insilmaril@2
   651
		if (api.checkParamCount(1))
insilmaril@2
   652
		{
insilmaril@2
   653
			s=api.parString (ok,0);
insilmaril@2
   654
			if (ok) setVymLink(s);
insilmaril@2
   655
		}	
insilmaril@2
   656
	}
insilmaril@2
   657
	// Internal commands, used for undo etc.	
insilmaril@2
   658
	else if (com==QString("undoMap"))
insilmaril@2
   659
	{
insilmaril@2
   660
		if (api.checkParamCount(1))
insilmaril@2
   661
			undoXML("",api.parString (ok,0));
insilmaril@2
   662
	} else if (com==QString("undoPart"))
insilmaril@2
   663
	{
insilmaril@2
   664
		if (api.checkParamCount(2))
insilmaril@2
   665
		{
insilmaril@2
   666
			s=api.parString (ok,0);
insilmaril@2
   667
			t=api.parString (ok,1);
insilmaril@2
   668
			undoXML(s,t);	
insilmaril@2
   669
		}
insilmaril@2
   670
	} else if (com=="select")
insilmaril@2
   671
		if (api.checkParamCount(1))
insilmaril@2
   672
		{
insilmaril@2
   673
			s=api.parString(ok,0);
insilmaril@2
   674
			if (ok) select (s);
insilmaril@2
   675
		}	
insilmaril@2
   676
	else
insilmaril@2
   677
	{
insilmaril@2
   678
		api.setError ("Unknown command in: "+atom);
insilmaril@2
   679
		cout << "ME::parse   api should have error now...\n";
insilmaril@2
   680
	}
insilmaril@2
   681
insilmaril@2
   682
	// Any errors?
insilmaril@2
   683
	if (api.error())
insilmaril@2
   684
	{
insilmaril@2
   685
		qWarning("MapEditor::parseAtom: Error!");
insilmaril@2
   686
		qWarning(api.errorDesc());
insilmaril@0
   687
	}	
insilmaril@0
   688
}
insilmaril@0
   689
insilmaril@2
   690
insilmaril@2
   691
void MapEditor::finishedLineEdit()
insilmaril@0
   692
{
insilmaril@0
   693
	// This is called by finishedLineEdit or any MapEditor method,
insilmaril@0
   694
	// which wants to assure, that lineedits finish, before e.g. a branch is 
insilmaril@0
   695
	// deleted
insilmaril@0
   696
    
insilmaril@0
   697
	// After calling LineEdit and using the clipboard, the 
insilmaril@0
   698
    // focus is not any longer on the main widget, we
insilmaril@0
   699
    // have to restore it using parentWidget()->setFocus()
insilmaril@0
   700
insilmaril@0
   701
    if (editingBO!=NULL) 
insilmaril@0
   702
	{
insilmaril@2
   703
		saveState("setHeading (\""+editingBO->getHeading()+"\")",editingBO, QString("Set heading of %1 to \"%2\"").arg(getName(editingBO)).arg(lineedit->text()) );
insilmaril@0
   704
		editingBO->setHeading(lineedit->text() );
insilmaril@0
   705
		editingBO=NULL;
insilmaril@0
   706
		lineedit->releaseKeyboard();
insilmaril@0
   707
		lineedit->hide();
insilmaril@0
   708
		parentWidget()->setFocus();
insilmaril@0
   709
		mapCenter->reposition();
insilmaril@0
   710
		adjustCanvasSize();
insilmaril@0
   711
		ensureSelectionVisible();
insilmaril@0
   712
    }		
insilmaril@0
   713
}
insilmaril@0
   714
insilmaril@2
   715
void MapEditor::toggleHistoryWindow()
insilmaril@2
   716
{
insilmaril@2
   717
	if (historyWindow->isVisible())
insilmaril@2
   718
		historyWindow->hide();
insilmaril@2
   719
	else	
insilmaril@2
   720
		historyWindow->show();
insilmaril@2
   721
}
insilmaril@2
   722
insilmaril@0
   723
insilmaril@0
   724
bool MapEditor::isDefault()
insilmaril@0
   725
{
insilmaril@0
   726
    return mapDefault;
insilmaril@0
   727
}
insilmaril@0
   728
insilmaril@0
   729
bool MapEditor::isUnsaved()
insilmaril@0
   730
{
insilmaril@0
   731
    return mapUnsaved;
insilmaril@0
   732
}
insilmaril@0
   733
insilmaril@0
   734
bool MapEditor::hasChanged()
insilmaril@0
   735
{
insilmaril@0
   736
    return mapChanged;
insilmaril@0
   737
}
insilmaril@0
   738
insilmaril@0
   739
void MapEditor::setChanged()
insilmaril@0
   740
{
insilmaril@0
   741
	mapChanged=true;
insilmaril@0
   742
	mapDefault=false;
insilmaril@0
   743
	mapUnsaved=true;
insilmaril@0
   744
	actionEditUndo->setEnabled (true);
insilmaril@0
   745
	actionFileSave->setEnabled (true);
insilmaril@0
   746
	findReset();
insilmaril@0
   747
}
insilmaril@0
   748
insilmaril@0
   749
void MapEditor::closeMap()
insilmaril@0
   750
{
insilmaril@0
   751
	// Finish open lineEdits
insilmaril@2
   752
	if (lineedit) finishedLineEdit();
insilmaril@0
   753
insilmaril@0
   754
	// Unselect before disabling the toolbar actions
insilmaril@0
   755
	if (selection) selection->unselect();
insilmaril@0
   756
	selection=NULL;
insilmaril@0
   757
	updateActions();
insilmaril@0
   758
insilmaril@0
   759
    clear();
insilmaril@0
   760
	close();
insilmaril@0
   761
}
insilmaril@0
   762
insilmaril@0
   763
void MapEditor::setFilePath(QString fname)
insilmaril@0
   764
{
insilmaril@0
   765
	setFilePath (fname,fname);
insilmaril@0
   766
}
insilmaril@0
   767
insilmaril@0
   768
void MapEditor::setFilePath(QString fname, QString destname)
insilmaril@0
   769
{
insilmaril@2
   770
	if (fname.isEmpty() || fname=="")
insilmaril@2
   771
	{
insilmaril@2
   772
		filePath="";
insilmaril@2
   773
		fileName="";
insilmaril@2
   774
		destPath="";
insilmaril@2
   775
	} else
insilmaril@2
   776
	{
insilmaril@2
   777
		filePath=fname;		// becomes absolute path
insilmaril@2
   778
		fileName=fname;		// gets stripped of path
insilmaril@2
   779
		destPath=destname;	// needed for vymlinks
insilmaril@2
   780
insilmaril@2
   781
		// If fname is not an absolute path, complete it
insilmaril@2
   782
		filePath=QDir(fname).absPath();
insilmaril@2
   783
		fileDir=filePath.left (1+filePath.findRev ("/"));
insilmaril@2
   784
insilmaril@2
   785
		// Set short name, too. Search from behind:
insilmaril@2
   786
		int i=fileName.findRev("/");
insilmaril@2
   787
		if (i>=0) fileName=fileName.remove (0,i+1);
insilmaril@2
   788
insilmaril@2
   789
		// Forget the .vym (or .xml) for name of map
insilmaril@2
   790
		mapName=fileName.left(fileName.findRev(".",-1,true) );
insilmaril@2
   791
insilmaril@2
   792
		// Adjust history window
insilmaril@2
   793
		historyWindow->setCaption (fileName);
insilmaril@2
   794
	}
insilmaril@0
   795
}
insilmaril@0
   796
insilmaril@0
   797
QString MapEditor::getFilePath()
insilmaril@0
   798
{
insilmaril@0
   799
	return filePath;
insilmaril@0
   800
}
insilmaril@0
   801
insilmaril@0
   802
QString MapEditor::getFileName()
insilmaril@0
   803
{
insilmaril@0
   804
	return fileName;
insilmaril@0
   805
}
insilmaril@0
   806
insilmaril@0
   807
QString MapEditor::getMapName()
insilmaril@0
   808
{
insilmaril@0
   809
	return mapName;
insilmaril@0
   810
}
insilmaril@0
   811
insilmaril@0
   812
QString MapEditor::getDestPath()
insilmaril@0
   813
{
insilmaril@0
   814
	return destPath;
insilmaril@0
   815
}
insilmaril@0
   816
insilmaril@2
   817
ErrorCode MapEditor::load (QString fname, LoadMode lmode)
insilmaril@0
   818
{
insilmaril@0
   819
	// Finish open lineEdits
insilmaril@2
   820
	if (lineedit) finishedLineEdit();
insilmaril@2
   821
insilmaril@2
   822
	ErrorCode err=success;
insilmaril@0
   823
insilmaril@0
   824
	if (lmode==NewMap)
insilmaril@0
   825
	{
insilmaril@0
   826
		if (selection) selection->unselect();
insilmaril@0
   827
		selection=NULL;
insilmaril@0
   828
		mapCenter->clear();
insilmaril@0
   829
		mapCenter->setMapEditor(this);
insilmaril@0
   830
		// (map state is set later at end of load...)
insilmaril@0
   831
	} else
insilmaril@0
   832
	{
insilmaril@2
   833
		saveState(selection,"Load map");
insilmaril@0
   834
	}	
insilmaril@0
   835
	
insilmaril@0
   836
    
insilmaril@0
   837
    mapBuilderHandler handler;
insilmaril@0
   838
	QFile file( fname );
insilmaril@0
   839
insilmaril@0
   840
	// I am paranoid: file should exist anyway
insilmaril@0
   841
	// according to check in mainwindow.
insilmaril@0
   842
	if (!file.exists() )
insilmaril@0
   843
	{
insilmaril@0
   844
		QMessageBox::critical( 0, tr( "Critical Parse Error" ),
insilmaril@0
   845
				   tr("Couldn't open map " +fname)+".");
insilmaril@2
   846
		err=aborted;	
insilmaril@0
   847
	} else
insilmaril@0
   848
	{
insilmaril@2
   849
		blockReposition=true;
insilmaril@0
   850
		QXmlInputSource source( file);
insilmaril@0
   851
		QXmlSimpleReader reader;
insilmaril@0
   852
		reader.setContentHandler( &handler );
insilmaril@0
   853
		reader.setErrorHandler( &handler );
insilmaril@0
   854
		handler.setMapEditor( this );
insilmaril@0
   855
		handler.setTmpDir (filePath.left(filePath.findRev("/",-1)));	// needed to load files with rel. path
insilmaril@2
   856
		handler.setInputFile (file.name());
insilmaril@0
   857
		handler.setLoadMode (lmode);
insilmaril@2
   858
		blockSaveState=true;
insilmaril@0
   859
		bool ok = reader.parse( source );
insilmaril@2
   860
		blockReposition=false;
insilmaril@2
   861
		blockSaveState=false;
insilmaril@0
   862
		file.close();
insilmaril@0
   863
		if ( ok ) 
insilmaril@0
   864
		{
insilmaril@0
   865
			mapCenter->reposition();
insilmaril@0
   866
			adjustCanvasSize();
insilmaril@0
   867
			if (lmode==NewMap)
insilmaril@0
   868
			{
insilmaril@0
   869
				mapDefault=false;
insilmaril@0
   870
				mapChanged=false;
insilmaril@0
   871
				mapUnsaved=false;
insilmaril@0
   872
			}
insilmaril@0
   873
		} else 
insilmaril@0
   874
		{
insilmaril@0
   875
			QMessageBox::critical( 0, tr( "Critical Parse Error" ),
insilmaril@0
   876
					   tr( handler.errorProtocol() ) );
insilmaril@0
   877
			// returnCode=1;	
insilmaril@0
   878
			// Still return "success": the map maybe at least
insilmaril@0
   879
			// partially read by the parser
insilmaril@0
   880
		}	
insilmaril@0
   881
	}	
insilmaril@0
   882
	updateActions();
insilmaril@2
   883
	return err;
insilmaril@0
   884
}
insilmaril@0
   885
insilmaril@0
   886
int MapEditor::save (const SaveMode &savemode)
insilmaril@0
   887
{
insilmaril@0
   888
	// Finish open lineEdits
insilmaril@2
   889
	if (lineedit) finishedLineEdit();
insilmaril@0
   890
insilmaril@0
   891
	int returnCode=0;
insilmaril@0
   892
insilmaril@2
   893
	// The SaveMode UndoCommand is not supported here
insilmaril@2
   894
	if (savemode==UndoCommand) return 1;
insilmaril@2
   895
insilmaril@0
   896
	// Create mapName and fileDir
insilmaril@0
   897
	makeSubDirs (fileDir);
insilmaril@0
   898
	QString fname;
insilmaril@0
   899
	if (saveZipped())
insilmaril@0
   900
		// save as .xml
insilmaril@0
   901
		fname=mapName+".xml";
insilmaril@0
   902
	else
insilmaril@0
   903
		// use name given by user, even if he chooses .doc
insilmaril@0
   904
		fname=fileName;
insilmaril@0
   905
insilmaril@0
   906
insilmaril@2
   907
	QString saveFile;
insilmaril@2
   908
	if (savemode==CompleteMap || selection==NULL)
insilmaril@2
   909
		saveFile=saveToDir (fileDir,mapName+"-",true,QPoint(),NULL);
insilmaril@2
   910
	else	
insilmaril@2
   911
		saveFile=saveToDir (fileDir,mapName+"-",true,QPoint(),selection);
insilmaril@2
   912
insilmaril@2
   913
	if (!saveStringToDisk(fileDir+fname,saveFile))
insilmaril@0
   914
		return 1;
insilmaril@0
   915
insilmaril@0
   916
	if (returnCode==0)
insilmaril@0
   917
	{
insilmaril@0
   918
		mapChanged=false;
insilmaril@0
   919
		mapUnsaved=false;
insilmaril@0
   920
		actionFileSave->setEnabled(false);
insilmaril@0
   921
	}
insilmaril@0
   922
insilmaril@0
   923
	return returnCode;
insilmaril@0
   924
}
insilmaril@0
   925
insilmaril@0
   926
void MapEditor::setZipped (bool z)
insilmaril@0
   927
{
insilmaril@0
   928
	zipped=z;
insilmaril@0
   929
}
insilmaril@0
   930
insilmaril@0
   931
bool MapEditor::saveZipped ()
insilmaril@0
   932
{
insilmaril@0
   933
	return zipped;
insilmaril@0
   934
}
insilmaril@0
   935
insilmaril@0
   936
void MapEditor::print()
insilmaril@0
   937
{
insilmaril@0
   938
	// Finish open lineEdits
insilmaril@2
   939
	if (lineedit) finishedLineEdit();
insilmaril@0
   940
insilmaril@0
   941
	if ( !printer ) 
insilmaril@0
   942
	{
insilmaril@0
   943
		printer = new QPrinter;
insilmaril@0
   944
		printer->setColorMode (QPrinter::Color);
insilmaril@2
   945
		printer->setPrinterName (settings.readEntry("/vym/mainwindow/printerName",printer->printerName()));
insilmaril@0
   946
	}
insilmaril@0
   947
insilmaril@0
   948
	QRect totalBBox=mapCenter->getTotalBBox();
insilmaril@0
   949
insilmaril@0
   950
	// Try to set orientation automagically
insilmaril@0
   951
	// Note: Interpretation of generated postscript is amibiguous, if 
insilmaril@0
   952
	// there are problems with landscape mode, see
insilmaril@0
   953
	// http://sdb.suse.de/de/sdb/html/jsmeix_print-cups-landscape-81.html
insilmaril@0
   954
insilmaril@0
   955
	if (totalBBox.width()>totalBBox.height())
insilmaril@0
   956
		// recommend landscape
insilmaril@0
   957
		printer->setOrientation (QPrinter::Landscape);
insilmaril@0
   958
	else	
insilmaril@0
   959
		// recommend portrait
insilmaril@0
   960
		printer->setOrientation (QPrinter::Portrait);
insilmaril@0
   961
insilmaril@0
   962
	if ( printer->setup(this) ) 
insilmaril@0
   963
	// returns false, if printing is canceled
insilmaril@0
   964
	{
insilmaril@0
   965
		QPainter pp(printer);
insilmaril@0
   966
insilmaril@0
   967
		// Don't print the visualisation of selection
insilmaril@0
   968
		LinkableMapObj *oldselection=NULL;
insilmaril@0
   969
		if (selection) 
insilmaril@0
   970
		{
insilmaril@0
   971
			oldselection=selection;
insilmaril@0
   972
			selection->unselect();
insilmaril@0
   973
		}
insilmaril@0
   974
insilmaril@0
   975
		// Handle sizes of map and paper:
insilmaril@0
   976
		//
insilmaril@0
   977
		// setWindow defines which part of the canvas will be transformed 
insilmaril@0
   978
		// setViewport defines area on paper in device coordinates (dpi)
insilmaril@0
   979
		// e.g. (0,50,700,700) is upper part on A4
insilmaril@0
   980
		// see also /usr/lib/qt3/doc/html/coordsys.html
insilmaril@0
   981
insilmaril@2
   982
		Q3PaintDeviceMetrics metrics (printer);
insilmaril@0
   983
insilmaril@0
   984
		double paperAspect = (double)metrics.width()   / (double)metrics.height();
insilmaril@0
   985
		double   mapAspect = (double)totalBBox.width() / (double)totalBBox.height();
insilmaril@0
   986
insilmaril@2
   987
		QRect mapRect=totalBBox;
insilmaril@2
   988
		Q3CanvasRectangle *frame=NULL;
insilmaril@2
   989
		Q3CanvasText *footerFN=NULL;
insilmaril@2
   990
		Q3CanvasText *footerDate=NULL;
insilmaril@0
   991
		if (printFrame || printFooter)
insilmaril@0
   992
		{
insilmaril@0
   993
			
insilmaril@0
   994
			if (printFrame) 
insilmaril@0
   995
			{
insilmaril@0
   996
				// Print frame around map
insilmaril@2
   997
				mapRect.setRect (totalBBox.x()-10, totalBBox.y()-10, 
insilmaril@2
   998
					totalBBox.width()+20, totalBBox.height()+20);
insilmaril@2
   999
				frame=new Q3CanvasRectangle (mapRect,mapCanvas);
insilmaril@2
  1000
				frame->setBrush (QColor(Qt::white));
insilmaril@2
  1001
				frame->setPen (QColor(Qt::black));
insilmaril@0
  1002
				frame->setZ(0);
insilmaril@0
  1003
				frame->show();    
insilmaril@0
  1004
			}		
insilmaril@2
  1005
			/* TODO remove after testing 
insilmaril@2
  1006
			QCanvasLine *l=new QCanvasLine (mapCanvas);
insilmaril@2
  1007
			l->setPoints (0,0,mapRect.width(),mapRect.height());
insilmaril@2
  1008
			l->setPen (QPen(QColor(black), 1));
insilmaril@2
  1009
			l->setZ (200);
insilmaril@2
  1010
			l->show();
insilmaril@2
  1011
			*/
insilmaril@2
  1012
insilmaril@0
  1013
			if (printFooter) 
insilmaril@0
  1014
			{
insilmaril@0
  1015
				// Print footer below map
insilmaril@0
  1016
				QFont font;		
insilmaril@0
  1017
				font.setPointSize(10);
insilmaril@2
  1018
				footerFN=new Q3CanvasText (mapCanvas);
insilmaril@0
  1019
				footerFN->setText ("VYM - " + fileName);
insilmaril@0
  1020
				footerFN->setFont(font);
insilmaril@0
  1021
				footerFN->move (mapRect.x(), mapRect.y() + mapRect.height() );
insilmaril@0
  1022
				footerFN->setZ(Z_TEXT);
insilmaril@0
  1023
				footerFN->show();    
insilmaril@2
  1024
				footerDate=new Q3CanvasText (mapCanvas);
insilmaril@0
  1025
				footerDate->setText (QDate::currentDate().toString(Qt::TextDate));
insilmaril@0
  1026
				footerDate->setFont(font);
insilmaril@0
  1027
				footerDate->move (mapRect.x()+mapRect.width()-footerDate->boundingRect().width(), mapRect.y() + mapRect.height() );
insilmaril@0
  1028
				footerDate->setZ(Z_TEXT);
insilmaril@0
  1029
				footerDate->show();    
insilmaril@0
  1030
			}
insilmaril@2
  1031
			pp.setWindow (mapRect.x(), mapRect.y(), mapRect.width(), mapRect.height()+20);
insilmaril@0
  1032
		}	else	
insilmaril@0
  1033
		{
insilmaril@0
  1034
			pp.setWindow (mapRect);
insilmaril@0
  1035
		}	
insilmaril@0
  1036
insilmaril@0
  1037
		if (mapAspect>=paperAspect)
insilmaril@0
  1038
		{
insilmaril@0
  1039
			// Fit horizontally to paper width
insilmaril@0
  1040
			pp.setViewport(0,0, metrics.width(),(int)(metrics.width()/mapAspect) );	
insilmaril@0
  1041
		}	else
insilmaril@0
  1042
		{
insilmaril@0
  1043
			// Fit vertically to paper height
insilmaril@0
  1044
			pp.setViewport(0,0,(int)(metrics.height()*mapAspect),metrics.height());	
insilmaril@0
  1045
		}	
insilmaril@0
  1046
insilmaril@0
  1047
		mapCanvas->drawArea(mapRect, &pp);	// draw Canvas to printer
insilmaril@0
  1048
insilmaril@0
  1049
		// Delete Frame and footer
insilmaril@0
  1050
		if (footerFN) 
insilmaril@0
  1051
		{
insilmaril@0
  1052
			delete (footerFN);
insilmaril@0
  1053
			delete (footerDate);
insilmaril@0
  1054
		}	
insilmaril@0
  1055
		if (frame)  delete (frame);
insilmaril@0
  1056
insilmaril@0
  1057
		// Restore selection
insilmaril@0
  1058
		if (oldselection) 
insilmaril@0
  1059
		{
insilmaril@0
  1060
			selection=oldselection;
insilmaril@0
  1061
			selection->select();
insilmaril@0
  1062
		}	
insilmaril@2
  1063
insilmaril@2
  1064
		// Save settings in vymrc
insilmaril@2
  1065
		settings.writeEntry("/vym/mainwindow/printerName",printer->printerName());
insilmaril@0
  1066
	}
insilmaril@0
  1067
}
insilmaril@0
  1068
insilmaril@0
  1069
QPixmap MapEditor::getPixmap()
insilmaril@0
  1070
{
insilmaril@0
  1071
	QRect mapRect=mapCenter->getTotalBBox();
insilmaril@0
  1072
	QPixmap pix (mapRect.size());
insilmaril@0
  1073
	QPainter pp (&pix);
insilmaril@0
  1074
insilmaril@0
  1075
	// Don't print the visualisation of selection
insilmaril@0
  1076
	LinkableMapObj *oldselection=NULL;
insilmaril@0
  1077
	if (selection) 
insilmaril@0
  1078
	{
insilmaril@0
  1079
		oldselection=selection;
insilmaril@0
  1080
		selection->unselect();
insilmaril@0
  1081
	}
insilmaril@0
  1082
insilmaril@0
  1083
	pp.setWindow (mapRect);
insilmaril@0
  1084
insilmaril@0
  1085
	mapCanvas->drawArea(mapRect, &pp);	// draw Canvas to painter
insilmaril@0
  1086
insilmaril@0
  1087
insilmaril@0
  1088
	// Restore selection
insilmaril@0
  1089
	if (oldselection) 
insilmaril@0
  1090
	{
insilmaril@0
  1091
		selection=oldselection;
insilmaril@0
  1092
		selection->select();
insilmaril@0
  1093
	}	
insilmaril@0
  1094
	
insilmaril@0
  1095
	return pix;
insilmaril@0
  1096
}
insilmaril@0
  1097
insilmaril@2
  1098
void MapEditor::setHideTmpMode (HideTmpMode mode)
insilmaril@2
  1099
{
insilmaril@2
  1100
	hidemode=mode;
insilmaril@2
  1101
	mapCenter->setHideTmp (hidemode);
insilmaril@2
  1102
	mapCenter->reposition();
insilmaril@2
  1103
	adjustCanvasSize();
insilmaril@2
  1104
	canvas()->update();
insilmaril@2
  1105
}
insilmaril@2
  1106
insilmaril@2
  1107
HideTmpMode MapEditor::getHideTmpMode()
insilmaril@2
  1108
{
insilmaril@2
  1109
	return hidemode;
insilmaril@2
  1110
}
insilmaril@2
  1111
insilmaril@0
  1112
void MapEditor::exportImage(QString fn)
insilmaril@0
  1113
{
insilmaril@0
  1114
	// Finish open lineEdits
insilmaril@2
  1115
	if (lineedit) finishedLineEdit();
insilmaril@2
  1116
insilmaril@2
  1117
	setExportMode (true);
insilmaril@0
  1118
	QPixmap pix (getPixmap());
insilmaril@0
  1119
	pix.save(fn, "PNG");
insilmaril@2
  1120
	setExportMode (false);
insilmaril@2
  1121
}
insilmaril@2
  1122
insilmaril@2
  1123
void MapEditor::setExportMode (bool b)
insilmaril@2
  1124
{
insilmaril@2
  1125
	// should be called before and after exports
insilmaril@2
  1126
	// depending on the settings
insilmaril@2
  1127
	if (b && settings.value("/vym/export/useHideExport","yes")=="yes")
insilmaril@2
  1128
		setHideTmpMode (HideExport);
insilmaril@2
  1129
	else	
insilmaril@2
  1130
		setHideTmpMode (HideNone);
insilmaril@0
  1131
}
insilmaril@0
  1132
insilmaril@4
  1133
void MapEditor::exportImage(QString fn, QString format)
insilmaril@0
  1134
{
insilmaril@0
  1135
	// Finish open lineEdits
insilmaril@2
  1136
	if (lineedit) finishedLineEdit();
insilmaril@2
  1137
insilmaril@2
  1138
	setExportMode (true);
insilmaril@0
  1139
	QPixmap pix (getPixmap());
insilmaril@4
  1140
	pix.save(fn, format);
insilmaril@2
  1141
	setExportMode (false);
insilmaril@0
  1142
}
insilmaril@0
  1143
insilmaril@2
  1144
void MapEditor::exportOOPresentation(const QString &fn, const QString &cf)
insilmaril@0
  1145
{
insilmaril@2
  1146
	ExportOO ex;
insilmaril@2
  1147
	ex.setFile (fn);
insilmaril@2
  1148
	ex.setMapCenter(mapCenter);
insilmaril@2
  1149
	if (ex.setConfigFile(cf)) 
insilmaril@0
  1150
	{
insilmaril@2
  1151
		setExportMode (true);
insilmaril@2
  1152
		ex.exportPresentation();
insilmaril@2
  1153
		setExportMode (false);
insilmaril@0
  1154
	}
insilmaril@0
  1155
}
insilmaril@0
  1156
insilmaril@0
  1157
insilmaril@2
  1158
insilmaril@0
  1159
void MapEditor::exportXML(const QString &dir)
insilmaril@0
  1160
{
insilmaril@2
  1161
	// Hide stuff during export, if settings want this
insilmaril@2
  1162
	setExportMode (true);
insilmaril@2
  1163
insilmaril@0
  1164
	// Create subdirectories
insilmaril@0
  1165
	makeSubDirs (dir);
insilmaril@0
  1166
insilmaril@0
  1167
	// write to directory
insilmaril@2
  1168
	QString saveFile=saveToDir (dir,mapName+"-",true,mapCenter->getTotalBBox().topLeft() ,NULL);
insilmaril@0
  1169
	QFile file;
insilmaril@0
  1170
insilmaril@0
  1171
	file.setName ( dir + "/"+mapName+".xml");
insilmaril@2
  1172
	if ( !file.open( QIODevice::WriteOnly ) )
insilmaril@0
  1173
	{
insilmaril@0
  1174
		// This should neverever happen
insilmaril@2
  1175
		QMessageBox::critical (0,tr("Critical Export Error"),tr("MapEditor::exportXML couldn't open %1").arg(file.name()));
insilmaril@0
  1176
		return;
insilmaril@0
  1177
	}	
insilmaril@0
  1178
insilmaril@0
  1179
	// Write it finally, and write in UTF8, no matter what 
insilmaril@0
  1180
	QTextStream ts( &file );
insilmaril@0
  1181
	ts.setEncoding (QTextStream::UnicodeUTF8);
insilmaril@0
  1182
	ts << saveFile;
insilmaril@0
  1183
	file.close();
insilmaril@0
  1184
insilmaril@0
  1185
	// Now write image, too
insilmaril@0
  1186
	exportImage (dir+"/images/"+mapName+".png");
insilmaril@2
  1187
insilmaril@2
  1188
	setExportMode (false);
insilmaril@0
  1189
}
insilmaril@0
  1190
insilmaril@0
  1191
void MapEditor::clear()
insilmaril@0
  1192
{
insilmaril@0
  1193
	if (selection)
insilmaril@0
  1194
	{
insilmaril@0
  1195
		selection->unselect();
insilmaril@0
  1196
		selection=NULL;
insilmaril@0
  1197
	}	
insilmaril@0
  1198
insilmaril@0
  1199
	mapCenter->clear();
insilmaril@0
  1200
}
insilmaril@0
  1201
insilmaril@2
  1202
void MapEditor::copy()
insilmaril@2
  1203
{
insilmaril@2
  1204
	// Finish open lineEdits
insilmaril@2
  1205
	if (lineedit) finishedLineEdit();
insilmaril@2
  1206
insilmaril@2
  1207
	if (selection) 
insilmaril@2
  1208
	{
insilmaril@2
  1209
		// write to directory
insilmaril@2
  1210
		QString clipfile="part";
insilmaril@2
  1211
		QString saveFile=saveToDir (fileDir,clipfile+"-",true,QPoint(),selection);
insilmaril@2
  1212
		QFile file;
insilmaril@2
  1213
insilmaril@2
  1214
		file.setName ( clipboardDir + "/"+clipfile+".xml");
insilmaril@2
  1215
		if ( !file.open( QIODevice::WriteOnly ) )
insilmaril@2
  1216
		{
insilmaril@2
  1217
			// This should neverever happen
insilmaril@2
  1218
			QMessageBox::critical (0,tr("Critical Export Error"),tr("MapEditor::exportXML couldn't open %1").arg(file.name()));
insilmaril@2
  1219
			return;
insilmaril@2
  1220
		}	
insilmaril@2
  1221
insilmaril@2
  1222
		// Write it finally, and write in UTF8, no matter what 
insilmaril@2
  1223
		QTextStream ts( &file );
insilmaril@2
  1224
		ts.setEncoding (QTextStream::UnicodeUTF8);
insilmaril@2
  1225
		ts << saveFile;
insilmaril@2
  1226
		file.close();
insilmaril@2
  1227
insilmaril@2
  1228
		clipboardEmpty=false;
insilmaril@2
  1229
		updateActions();
insilmaril@2
  1230
	}	    
insilmaril@2
  1231
}
insilmaril@2
  1232
insilmaril@2
  1233
void MapEditor::redo()
insilmaril@2
  1234
{
insilmaril@2
  1235
	// Finish open lineEdits
insilmaril@2
  1236
	if (lineedit) finishedLineEdit();
insilmaril@2
  1237
insilmaril@2
  1238
	blockSaveState=true;
insilmaril@2
  1239
	
insilmaril@2
  1240
	// Find out current undo directory
insilmaril@2
  1241
	QString bakMapDir=QDir::convertSeparators (QString(tmpMapDir+"/undo-%1").arg(undoNum));
insilmaril@2
  1242
insilmaril@2
  1243
	// Restore variables
insilmaril@2
  1244
	QString undoCommand;
insilmaril@2
  1245
	QString undoSelection;
insilmaril@2
  1246
	QString redoCommand;
insilmaril@2
  1247
	QString redoSelection;
insilmaril@2
  1248
	SimpleSettings set;
insilmaril@2
  1249
	set.readSettings(QString(bakMapDir+"/commands"));
insilmaril@2
  1250
	undoCommand=set.readEntry ("undoCommand");
insilmaril@2
  1251
	undoSelection=set.readEntry ("undoSelection");
insilmaril@2
  1252
	redoCommand=set.readEntry ("redoCommand");
insilmaril@2
  1253
	redoSelection=set.readEntry ("redoSelection");
insilmaril@2
  1254
insilmaril@2
  1255
	// select  object before redo
insilmaril@2
  1256
	if (!redoSelection.isEmpty())
insilmaril@2
  1257
		select (redoSelection);
insilmaril@2
  1258
insilmaril@2
  1259
/* TODO remove testing
insilmaril@2
  1260
	cout << "ME::redo() begin\n";
insilmaril@2
  1261
	cout << "    undosTotal="<<undosTotal<<endl;
insilmaril@2
  1262
	cout << "    undosAvail="<<undosAvail<<endl;
insilmaril@2
  1263
	cout << "       undoNum="<<undoNum<<endl;
insilmaril@2
  1264
	cout << "    ---------------------------"<<endl;
insilmaril@2
  1265
	cout << "    undoCom="<<undoCommand<<endl;
insilmaril@2
  1266
	cout << "    undoSel="<<undoSelection<<endl;
insilmaril@2
  1267
	cout << "    ---------------------------"<<endl;
insilmaril@2
  1268
	cout << "    redoCom="<<redoCommand<<endl;
insilmaril@2
  1269
	cout << "    redoSel="<<redoSelection<<endl;
insilmaril@2
  1270
	cout << "    ---------------------------"<<endl;
insilmaril@2
  1271
*/
insilmaril@2
  1272
	parseAtom (undoCommand);
insilmaril@2
  1273
	mapCenter->reposition();
insilmaril@2
  1274
insilmaril@2
  1275
	//if (!redoSelection.isEmpty())
insilmaril@2
  1276
	//	select (redoSelection);
insilmaril@2
  1277
insilmaril@2
  1278
	undosAvail--;
insilmaril@2
  1279
	if (undosAvail<1)
insilmaril@2
  1280
		// Undo not longer available now
insilmaril@2
  1281
		actionEditUndo->setEnabled (false);
insilmaril@2
  1282
	else	
insilmaril@2
  1283
		undoNum--; if (undoNum<1) undoNum=undosTotal;
insilmaril@2
  1284
insilmaril@2
  1285
	blockSaveState=false;
insilmaril@2
  1286
/* TODO remove testing
insilmaril@2
  1287
	cout << "ME::redo() end\n";
insilmaril@2
  1288
	cout << "    undosAvail="<<undosAvail<<endl;
insilmaril@2
  1289
	cout << "       undoNum="<<undoNum<<endl;
insilmaril@2
  1290
	cout << "    ---------------------------"<<endl<<endl;
insilmaril@2
  1291
*/	
insilmaril@2
  1292
}
insilmaril@2
  1293
insilmaril@0
  1294
void MapEditor::undo()
insilmaril@0
  1295
{
insilmaril@2
  1296
	// Finish open lineEdits
insilmaril@2
  1297
	if (lineedit) finishedLineEdit();
insilmaril@2
  1298
insilmaril@2
  1299
	blockSaveState=true;
insilmaril@2
  1300
	
insilmaril@2
  1301
	// Find out current undo directory
insilmaril@2
  1302
	QString bakMapDir=QDir::convertSeparators (QString(tmpMapDir+"/undo-%1").arg(undoNum));
insilmaril@2
  1303
insilmaril@2
  1304
	// Restore variables
insilmaril@2
  1305
	QString undoCommand;
insilmaril@2
  1306
	QString undoSelection;
insilmaril@2
  1307
	QString redoCommand;
insilmaril@2
  1308
	QString redoSelection;
insilmaril@2
  1309
	SimpleSettings set;
insilmaril@2
  1310
	set.readSettings(QString(bakMapDir+"/commands"));
insilmaril@2
  1311
	undoCommand=  set.readEntry ("undoCommand");
insilmaril@2
  1312
	undoSelection=set.readEntry ("undoSelection");
insilmaril@2
  1313
	redoCommand=  set.readEntry ("redoCommand");
insilmaril@2
  1314
	redoSelection=set.readEntry ("redoSelection");
insilmaril@2
  1315
insilmaril@2
  1316
	// select  object before undo
insilmaril@2
  1317
	if (!undoSelection.isEmpty())
insilmaril@2
  1318
		select (undoSelection);
insilmaril@2
  1319
insilmaril@2
  1320
/* TODO testing
insilmaril@2
  1321
	cout << "ME::undo() begin\n";
insilmaril@2
  1322
	cout << "    undosTotal="<<undosTotal<<endl;
insilmaril@2
  1323
	cout << "    undosAvail="<<undosAvail<<endl;
insilmaril@2
  1324
	cout << "       undoNum="<<undoNum<<endl;
insilmaril@2
  1325
	cout << "    ---------------------------"<<endl;
insilmaril@2
  1326
	cout << "    undoCom="<<undoCommand<<endl;
insilmaril@2
  1327
	cout << "    undoSel="<<undoSelection<<endl;
insilmaril@2
  1328
	cout << "    ---------------------------"<<endl;
insilmaril@2
  1329
	cout << "    redoCom="<<redoCommand<<endl;
insilmaril@2
  1330
	cout << "    redoSel="<<redoSelection<<endl;
insilmaril@2
  1331
	cout << "    ---------------------------"<<endl;
insilmaril@2
  1332
*/	
insilmaril@2
  1333
	parseAtom (undoCommand);
insilmaril@2
  1334
	mapCenter->reposition();
insilmaril@2
  1335
insilmaril@2
  1336
	//if (!redoSelection.isEmpty())
insilmaril@2
  1337
	//	select (redoSelection);
insilmaril@2
  1338
insilmaril@2
  1339
	undosAvail--;
insilmaril@2
  1340
	if (undosAvail<1)
insilmaril@2
  1341
		// Undo not longer available now
insilmaril@2
  1342
		actionEditUndo->setEnabled (false);
insilmaril@2
  1343
	else	
insilmaril@2
  1344
		undoNum--; if (undoNum<1) undoNum=undosTotal;
insilmaril@2
  1345
insilmaril@2
  1346
	blockSaveState=false;
insilmaril@2
  1347
/* TODO remove testing
insilmaril@2
  1348
	cout << "ME::undo() end\n";
insilmaril@2
  1349
	cout << "    undosAvail="<<undosAvail<<endl;
insilmaril@2
  1350
	cout << "       undoNum="<<undoNum<<endl;
insilmaril@2
  1351
	cout << "    ---------------------------"<<endl<<endl;
insilmaril@2
  1352
*/
insilmaril@2
  1353
}
insilmaril@2
  1354
insilmaril@2
  1355
void MapEditor::undoXML(const QString &undoSel, const QString &bakMapPath)
insilmaril@2
  1356
{
insilmaril@2
  1357
	QString bakMapDir=bakMapPath.left(bakMapPath.findRev("/"));
insilmaril@2
  1358
	QDir d(bakMapDir);
insilmaril@2
  1359
	QFile file (bakMapPath);
insilmaril@2
  1360
insilmaril@0
  1361
	if (d.exists() )
insilmaril@0
  1362
	{
insilmaril@2
  1363
		// We need to parse saved XML data
insilmaril@0
  1364
		mapBuilderHandler handler;
insilmaril@2
  1365
		QXmlInputSource source( file);
insilmaril@0
  1366
		QXmlSimpleReader reader;
insilmaril@0
  1367
		reader.setContentHandler( &handler );
insilmaril@0
  1368
		reader.setErrorHandler( &handler );
insilmaril@0
  1369
		handler.setMapEditor( this );
insilmaril@0
  1370
		handler.setTmpDir ( bakMapDir );	// needed to load files with rel. path
insilmaril@2
  1371
		if (undoSel.isEmpty())
insilmaril@0
  1372
		{
insilmaril@2
  1373
			unselect();
insilmaril@2
  1374
			mapCenter->clear();
insilmaril@2
  1375
			handler.setLoadMode (NewMap);
insilmaril@0
  1376
		} else	
insilmaril@0
  1377
		{
insilmaril@2
  1378
			select (undoSel);
insilmaril@2
  1379
			handler.setLoadMode (ImportReplace);
insilmaril@0
  1380
		}	
insilmaril@2
  1381
		blockReposition=true;
insilmaril@0
  1382
		bool ok = reader.parse( source );
insilmaril@2
  1383
		blockReposition=false;
insilmaril@2
  1384
		if (! ok ) 
insilmaril@0
  1385
		{	
insilmaril@0
  1386
			// This should never ever happen
insilmaril@0
  1387
			QMessageBox::critical( 0, tr( "Critical Parse Error by reading backupFile" ),
insilmaril@2
  1388
								   tr( handler.errorProtocol() )+" in "+bakMapDir );
insilmaril@0
  1389
		}
insilmaril@0
  1390
	} else	
insilmaril@0
  1391
	{
insilmaril@0
  1392
		QMessageBox::critical( 0, tr( "Critical Error" ),
insilmaril@2
  1393
			tr("Temporary directory %1 used for undo is gone. \n"
insilmaril@0
  1394
			"I will create a new one, but at the moment no undo is available.\n"
insilmaril@0
  1395
			"Maybe you want to reload your original data.\n\n"
insilmaril@2
  1396
			"Sorry for any inconveniences.").arg(bakMapDir) );
insilmaril@0
  1397
		makeTmpDirs();	
insilmaril@0
  1398
	}							
insilmaril@0
  1399
}
insilmaril@0
  1400
insilmaril@2
  1401
void MapEditor::pasteNoSave()
insilmaril@0
  1402
{
insilmaril@0
  1403
	// Finish open lineEdits
insilmaril@2
  1404
	if (lineedit) finishedLineEdit();
insilmaril@2
  1405
insilmaril@2
  1406
	load (clipboardDir+"/part.xml",ImportAdd);
insilmaril@0
  1407
}
insilmaril@0
  1408
insilmaril@0
  1409
void MapEditor::cutNoSave()
insilmaril@0
  1410
{
insilmaril@2
  1411
	copy();
insilmaril@2
  1412
	deleteSelection();
insilmaril@0
  1413
}
insilmaril@0
  1414
insilmaril@0
  1415
void MapEditor::paste()
insilmaril@0
  1416
{   
insilmaril@2
  1417
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@2
  1418
				      typeid(*selection) == typeid(MapCenterObj))) 
insilmaril@2
  1419
	{
insilmaril@2
  1420
		saveState(selection,QString("Paste to %1").arg( getName(selection)));
insilmaril@2
  1421
		pasteNoSave();
insilmaril@2
  1422
		mapCenter->reposition();
insilmaril@2
  1423
		adjustCanvasSize();
insilmaril@2
  1424
	}
insilmaril@0
  1425
}
insilmaril@0
  1426
insilmaril@0
  1427
void MapEditor::cut()
insilmaril@0
  1428
{
insilmaril@2
  1429
	saveState(selection->getParObj(),QString("Cut %1").arg(getName(selection)));
insilmaril@0
  1430
	copy();
insilmaril@0
  1431
	cutNoSave();
insilmaril@0
  1432
	mapCenter->reposition();
insilmaril@0
  1433
	adjustCanvasSize();
insilmaril@0
  1434
}
insilmaril@0
  1435
insilmaril@2
  1436
void MapEditor::move(const int &x, const int &y)
insilmaril@2
  1437
{
insilmaril@2
  1438
	// TODO no saveState, because this is only internal at undo so far
insilmaril@2
  1439
	if (selection) selection->move(x,y);
insilmaril@2
  1440
	if (typeid(*selection) == typeid(FloatImageObj))
insilmaril@2
  1441
		((FloatImageObj*)selection)->setRelPos();
insilmaril@2
  1442
}
insilmaril@2
  1443
insilmaril@0
  1444
void MapEditor::moveBranchUp()
insilmaril@0
  1445
{
insilmaril@0
  1446
	// Finish open lineEdits
insilmaril@2
  1447
	if (lineedit) finishedLineEdit();
insilmaril@0
  1448
insilmaril@0
  1449
	BranchObj* bo;
insilmaril@0
  1450
	BranchObj* par;
insilmaril@0
  1451
	if (typeid(*selection) == typeid(BranchObj)  ) 
insilmaril@0
  1452
	{
insilmaril@2
  1453
		bo=(BranchObj*)selection;
insilmaril@2
  1454
		if (!bo->canMoveBranchUp()) return;
insilmaril@0
  1455
		par=(BranchObj*)(bo->getParObj());
insilmaril@0
  1456
		selection->unselect();
insilmaril@0
  1457
		selection=par->moveBranchUp (bo);
insilmaril@0
  1458
		selection->select();
insilmaril@2
  1459
		saveState("moveBranchDown ()",bo,QString("Move up %1").arg(getName(bo)));
insilmaril@0
  1460
		mapCenter->reposition();
insilmaril@0
  1461
		ensureSelectionVisible();
insilmaril@0
  1462
	}
insilmaril@0
  1463
}
insilmaril@0
  1464
insilmaril@0
  1465
void MapEditor::moveBranchDown()
insilmaril@0
  1466
{
insilmaril@0
  1467
	// Finish open lineEdits
insilmaril@2
  1468
	if (lineedit) finishedLineEdit();
insilmaril@0
  1469
insilmaril@0
  1470
	BranchObj* bo;
insilmaril@0
  1471
	BranchObj* par;
insilmaril@0
  1472
	if (typeid(*selection) == typeid(BranchObj)  ) 
insilmaril@0
  1473
	{
insilmaril@2
  1474
		bo=(BranchObj*)selection;
insilmaril@2
  1475
		if (!bo->canMoveBranchDown()) return;
insilmaril@0
  1476
		par=(BranchObj*)(bo->getParObj());
insilmaril@0
  1477
		selection->unselect(); 
insilmaril@0
  1478
		selection=par->moveBranchDown(bo);
insilmaril@0
  1479
		selection->select();
insilmaril@2
  1480
		saveState("moveBranchUp ()",bo,QString("Move down %1").arg(getName(bo)));
insilmaril@0
  1481
		mapCenter->reposition();
insilmaril@0
  1482
		ensureSelectionVisible();
insilmaril@0
  1483
	}	
insilmaril@0
  1484
}
insilmaril@0
  1485
insilmaril@0
  1486
void MapEditor::editHeading()
insilmaril@0
  1487
{
insilmaril@0
  1488
	// Finish open lineEdits
insilmaril@6
  1489
	if (lineedit->isVisible()) 
insilmaril@6
  1490
	{	
insilmaril@6
  1491
		finishedLineEdit();
insilmaril@6
  1492
		return;
insilmaril@6
  1493
	}	
insilmaril@0
  1494
insilmaril@0
  1495
	if (selection  &&  
insilmaril@0
  1496
		 (typeid(*selection) == typeid(BranchObj) || 
insilmaril@0
  1497
		  typeid(*selection) == typeid(MapCenterObj) ) ) 
insilmaril@0
  1498
	{
insilmaril@2
  1499
		editingBO=(BranchObj*)selection;
insilmaril@0
  1500
insilmaril@0
  1501
		ensureSelectionVisible();
insilmaril@2
  1502
		QPoint p = worldMatrix().map(QPoint (editingBO->x(),editingBO->y()));
insilmaril@0
  1503
		lineedit->setGeometry(p.x()-contentsX(),p.y()-contentsY(),200,25);
insilmaril@2
  1504
		QString s=editingBO->getHeading();
insilmaril@0
  1505
		lineedit->setText(s);
insilmaril@0
  1506
		lineedit->setCursorPosition(1);
insilmaril@0
  1507
		if (actionSettingsAutoselectText->isOn() && !s.isEmpty() && actionSettingsPasteNewHeading->isOn() )
insilmaril@0
  1508
			lineedit->selectAll();
insilmaril@0
  1509
		lineedit->show();
insilmaril@0
  1510
		lineedit->grabKeyboard();
insilmaril@0
  1511
		lineedit->setFocus();
insilmaril@0
  1512
	}				
insilmaril@0
  1513
}
insilmaril@0
  1514
insilmaril@2
  1515
void MapEditor::setHeading(const QString &s)
insilmaril@0
  1516
{
insilmaril@2
  1517
	// Internal function, no saveState needed
insilmaril@0
  1518
	if (selection  &&  
insilmaril@0
  1519
		 (typeid(*selection) == typeid(BranchObj) || 
insilmaril@0
  1520
		  typeid(*selection) == typeid(MapCenterObj) ) ) 
insilmaril@0
  1521
	{
insilmaril@2
  1522
		((BranchObj*)selection)->setHeading(s);
insilmaril@2
  1523
		mapCenter->reposition();
insilmaril@2
  1524
		adjustCanvasSize();
insilmaril@2
  1525
		ensureSelectionVisible();
insilmaril@2
  1526
	}
insilmaril@2
  1527
}
insilmaril@2
  1528
insilmaril@2
  1529
void MapEditor::setURL (const QString &s)
insilmaril@2
  1530
{
insilmaril@2
  1531
	// Internal function, no saveState needed
insilmaril@2
  1532
	if (selection  &&  
insilmaril@2
  1533
		 (typeid(*selection) == typeid(BranchObj) || 
insilmaril@2
  1534
		  typeid(*selection) == typeid(MapCenterObj) ) ) 
insilmaril@2
  1535
	{
insilmaril@2
  1536
		((BranchObj*)selection)->setURL(s);
insilmaril@2
  1537
		mapCenter->reposition();
insilmaril@2
  1538
		adjustCanvasSize();
insilmaril@2
  1539
		ensureSelectionVisible();
insilmaril@2
  1540
	}
insilmaril@2
  1541
}
insilmaril@2
  1542
insilmaril@2
  1543
void MapEditor::setVymLink (const QString &s)
insilmaril@2
  1544
{
insilmaril@2
  1545
	// Internal function, no saveState needed
insilmaril@2
  1546
	if (selection  &&  
insilmaril@2
  1547
		 (typeid(*selection) == typeid(BranchObj) || 
insilmaril@2
  1548
		  typeid(*selection) == typeid(MapCenterObj) ) ) 
insilmaril@2
  1549
	{
insilmaril@2
  1550
		((BranchObj*)selection)->setVymLink(s);
insilmaril@2
  1551
		mapCenter->reposition();
insilmaril@2
  1552
		adjustCanvasSize();
insilmaril@2
  1553
		ensureSelectionVisible();
insilmaril@2
  1554
	}
insilmaril@2
  1555
}
insilmaril@2
  1556
insilmaril@2
  1557
void MapEditor::addNewBranch(int pos)
insilmaril@2
  1558
{
insilmaril@2
  1559
	// Finish open lineEdits
insilmaril@2
  1560
	if (lineedit) finishedLineEdit();
insilmaril@2
  1561
insilmaril@2
  1562
	if (selection  &&  
insilmaril@2
  1563
		 (typeid(*selection) == typeid(BranchObj) || 
insilmaril@2
  1564
		  typeid(*selection) == typeid(MapCenterObj) ) ) 
insilmaril@2
  1565
	{
insilmaril@2
  1566
		BranchObj* bo1 = (BranchObj*) selection;
insilmaril@2
  1567
		saveState(selection, QString("Add new branch to %1").arg(getName(bo1)));	//TODO undoCommand
insilmaril@2
  1568
insilmaril@0
  1569
		bool wasScrolled=false;
insilmaril@0
  1570
		BranchObj *newbo=NULL;
insilmaril@0
  1571
		if (pos==0)
insilmaril@0
  1572
		{
insilmaril@0
  1573
			// save scroll state. If scrolled, automatically select
insilmaril@0
  1574
			// new branch in order to tmp unscroll parent...
insilmaril@0
  1575
			wasScrolled=bo1->isScrolled();
insilmaril@0
  1576
			newbo=bo1->addBranch();
insilmaril@0
  1577
		}	else 
insilmaril@0
  1578
		{
insilmaril@0
  1579
			BranchObj *parbo=(BranchObj*)(selection->getParObj());
insilmaril@0
  1580
			if (parbo)
insilmaril@0
  1581
			{
insilmaril@0
  1582
				if (pos<0)
insilmaril@0
  1583
					// add above selection
insilmaril@0
  1584
					newbo=parbo->insertBranch(bo1->getNum());
insilmaril@0
  1585
				else
insilmaril@0
  1586
					// add below selection
insilmaril@0
  1587
					newbo=parbo->insertBranch(bo1->getNum()+1);
insilmaril@0
  1588
			} else
insilmaril@0
  1589
				// This should not happen...
insilmaril@0
  1590
				return;
insilmaril@0
  1591
			
insilmaril@0
  1592
		}	
insilmaril@0
  1593
insilmaril@0
  1594
		LinkableMapObj *oldselection=selection;
insilmaril@0
  1595
insilmaril@0
  1596
		mapCenter->reposition();
insilmaril@0
  1597
		adjustCanvasSize();
insilmaril@0
  1598
		if (actionSettingsAutoedit->isOn() ||
insilmaril@0
  1599
			actionSettingsAutoselectHeading->isOn() )
insilmaril@0
  1600
		{
insilmaril@0
  1601
			selection->unselect();
insilmaril@0
  1602
			selection=newbo;
insilmaril@0
  1603
			selection->select();
insilmaril@0
  1604
			if (actionSettingsPasteNewHeading->isOn() )
insilmaril@0
  1605
			{
insilmaril@2
  1606
				BranchObj *bo2= (BranchObj*)selection;
insilmaril@0
  1607
				bo2->setHeading("");
insilmaril@0
  1608
			}	
insilmaril@0
  1609
			if (actionSettingsAutoedit->isOn() )
insilmaril@0
  1610
				editHeading();
insilmaril@0
  1611
			if (!actionSettingsAutoselectHeading->isOn()
insilmaril@0
  1612
				&& !wasScrolled)
insilmaril@0
  1613
			{
insilmaril@0
  1614
				selection->unselect();
insilmaril@0
  1615
				selection=oldselection;
insilmaril@0
  1616
				selection->select();
insilmaril@0
  1617
			}
insilmaril@0
  1618
		}	
insilmaril@0
  1619
	}	
insilmaril@0
  1620
}
insilmaril@0
  1621
insilmaril@2
  1622
insilmaril@2
  1623
void MapEditor::addNewBranchHere()
insilmaril@2
  1624
{
insilmaril@2
  1625
	// Finish open lineEdits
insilmaril@2
  1626
	if (lineedit) finishedLineEdit();
insilmaril@2
  1627
insilmaril@2
  1628
	if (selection  &&  
insilmaril@2
  1629
		 (typeid(*selection) == typeid(BranchObj) ) )
insilmaril@2
  1630
	{
insilmaril@2
  1631
		BranchObj* bo1 = (BranchObj*) selection;
insilmaril@2
  1632
		saveState(selection, QString("Add new branch here").arg(getName(bo1)));
insilmaril@2
  1633
insilmaril@2
  1634
		bool wasScrolled=false;
insilmaril@2
  1635
		BranchObj *newbo=NULL;
insilmaril@2
  1636
		BranchObj *parbo=(BranchObj*)(selection->getParObj());
insilmaril@2
  1637
		if (parbo)
insilmaril@2
  1638
		{
insilmaril@2
  1639
			// add below selection
insilmaril@2
  1640
			newbo=parbo->insertBranch(bo1->getNum()+1);
insilmaril@2
  1641
		} 
insilmaril@2
  1642
insilmaril@2
  1643
		LinkableMapObj *oldselection=selection;
insilmaril@2
  1644
		((BranchObj*)selection)->moveBranchTo (newbo,-1);
insilmaril@2
  1645
insilmaril@2
  1646
		mapCenter->reposition();
insilmaril@2
  1647
		adjustCanvasSize();
insilmaril@2
  1648
		if (actionSettingsAutoedit->isOn() ||
insilmaril@2
  1649
			actionSettingsAutoselectHeading->isOn() )
insilmaril@2
  1650
		{
insilmaril@2
  1651
			selection->unselect();
insilmaril@2
  1652
			selection=newbo;
insilmaril@2
  1653
			selection->select();
insilmaril@2
  1654
			if (actionSettingsPasteNewHeading->isOn() )
insilmaril@2
  1655
			{
insilmaril@2
  1656
				BranchObj *bo2= (BranchObj*)selection;
insilmaril@2
  1657
				bo2->setHeading("");
insilmaril@2
  1658
			}	
insilmaril@2
  1659
			if (actionSettingsAutoedit->isOn() )
insilmaril@2
  1660
				editHeading();
insilmaril@2
  1661
			if (!actionSettingsAutoselectHeading->isOn()
insilmaril@2
  1662
				&& !wasScrolled)
insilmaril@2
  1663
			{
insilmaril@2
  1664
				selection->unselect();
insilmaril@2
  1665
				selection=oldselection;
insilmaril@2
  1666
				selection->select();
insilmaril@2
  1667
			}
insilmaril@2
  1668
		}	
insilmaril@2
  1669
	}	
insilmaril@2
  1670
}
insilmaril@2
  1671
insilmaril@0
  1672
void MapEditor::deleteSelection()
insilmaril@0
  1673
{
insilmaril@0
  1674
	// Finish open lineEdits
insilmaril@2
  1675
	if (lineedit) finishedLineEdit();
insilmaril@0
  1676
insilmaril@0
  1677
	if (selection  && typeid(*selection) ==typeid(BranchObj) ) 
insilmaril@0
  1678
	{
insilmaril@0
  1679
		BranchObj* bo=dynamic_cast <BranchObj*> (selection);
insilmaril@0
  1680
		BranchObj* par=(BranchObj*)(bo->getParObj());
insilmaril@0
  1681
		bo->unselect();
insilmaril@2
  1682
		if (selection->getDepth()>1)
insilmaril@2
  1683
			// Normal branch, save parent with childs
insilmaril@2
  1684
			saveState(par,QString("Delete %1").arg(getName(bo)));
insilmaril@2
  1685
		else
insilmaril@2
  1686
			// Mainbranch, save whole map
insilmaril@2
  1687
			// TODO Better would be to insert mainbranch again at pos
insilmaril@2
  1688
			// But undoCommand is missing right now
insilmaril@2
  1689
			saveState(QString("Delete %1").arg(getName(bo)));
insilmaril@0
  1690
		selection=NULL;
insilmaril@0
  1691
		par->removeBranch(bo);
insilmaril@0
  1692
		selection=par;
insilmaril@0
  1693
		selection->select();
insilmaril@0
  1694
		ensureSelectionVisible();
insilmaril@0
  1695
		mapCenter->reposition();
insilmaril@0
  1696
		adjustCanvasSize();
insilmaril@0
  1697
	}
insilmaril@0
  1698
	if (selection  && typeid(*selection) ==typeid(FloatImageObj) ) 
insilmaril@0
  1699
	{
insilmaril@0
  1700
		FloatImageObj* fio=dynamic_cast <FloatImageObj*> (selection);
insilmaril@0
  1701
		BranchObj* par=(BranchObj*)(fio->getParObj());
insilmaril@2
  1702
		saveState(par, QString("Delete %1").arg(getName(fio)));
insilmaril@0
  1703
		fio->unselect();
insilmaril@0
  1704
		selection=NULL;
insilmaril@0
  1705
		par->removeFloatImage(fio);
insilmaril@0
  1706
		selection=par;
insilmaril@0
  1707
		selection->select();
insilmaril@0
  1708
		ensureSelectionVisible();
insilmaril@0
  1709
		mapCenter->reposition();
insilmaril@0
  1710
		adjustCanvasSize();
insilmaril@0
  1711
	}
insilmaril@0
  1712
}
insilmaril@0
  1713
insilmaril@0
  1714
LinkableMapObj* MapEditor::getSelection()
insilmaril@0
  1715
{
insilmaril@0
  1716
	return selection;
insilmaril@0
  1717
}
insilmaril@0
  1718
insilmaril@0
  1719
void MapEditor::unselect()
insilmaril@0
  1720
{
insilmaril@0
  1721
	if (selection) 
insilmaril@0
  1722
	{
insilmaril@0
  1723
		selectionLast=selection;
insilmaril@0
  1724
		selection->unselect();
insilmaril@0
  1725
		selection=NULL;
insilmaril@0
  1726
	}
insilmaril@0
  1727
}	
insilmaril@0
  1728
insilmaril@0
  1729
void MapEditor::reselect()
insilmaril@0
  1730
{
insilmaril@0
  1731
	if (selectionLast)
insilmaril@0
  1732
	{
insilmaril@0
  1733
		selection=selectionLast;
insilmaril@0
  1734
		selection->select();
insilmaril@0
  1735
		selectionLast=NULL;
insilmaril@0
  1736
	}
insilmaril@0
  1737
}	
insilmaril@0
  1738
insilmaril@2
  1739
bool MapEditor::select (const QString &s)
insilmaril@2
  1740
{
insilmaril@2
  1741
	LinkableMapObj *lmo=mapCenter->findObjBySelect(s);
insilmaril@2
  1742
insilmaril@2
  1743
	// Finally select the found object
insilmaril@2
  1744
	if (lmo)
insilmaril@2
  1745
	{
insilmaril@2
  1746
		if (selection) unselect();
insilmaril@2
  1747
		selection=lmo;
insilmaril@2
  1748
		selection->select();
insilmaril@2
  1749
		adjustCanvasSize();
insilmaril@2
  1750
		ensureSelectionVisible();
insilmaril@2
  1751
		return true;
insilmaril@2
  1752
	} 
insilmaril@2
  1753
	return false;
insilmaril@2
  1754
}
insilmaril@2
  1755
insilmaril@2
  1756
void MapEditor::select (LinkableMapObj *lmo)
insilmaril@2
  1757
{
insilmaril@2
  1758
	if (lmo && selection != lmo)
insilmaril@2
  1759
	{
insilmaril@2
  1760
		// select the MapObj
insilmaril@2
  1761
		if (selection) selection->unselect();
insilmaril@2
  1762
		selection=lmo;
insilmaril@2
  1763
		selection->select();
insilmaril@2
  1764
			
insilmaril@2
  1765
		adjustCanvasSize();
insilmaril@2
  1766
	}
insilmaril@2
  1767
insilmaril@2
  1768
}
insilmaril@2
  1769
insilmaril@0
  1770
void MapEditor::selectNextBranch()
insilmaril@0
  1771
{
insilmaril@0
  1772
	// Increase number of branch
insilmaril@0
  1773
	if (selection)
insilmaril@0
  1774
	{
insilmaril@0
  1775
		QString s=selection->getSelectString();
insilmaril@0
  1776
		QString part;
insilmaril@0
  1777
		QString typ;
insilmaril@0
  1778
		QString num;
insilmaril@0
  1779
insilmaril@0
  1780
		// Where am I? 
insilmaril@0
  1781
		part=s.section(",",-1);
insilmaril@0
  1782
		typ=part.left (3);
insilmaril@0
  1783
		num=part.right(part.length() - 3);
insilmaril@0
  1784
insilmaril@0
  1785
		s=s.left (s.length() -num.length());
insilmaril@0
  1786
insilmaril@0
  1787
		// Go to next lmo
insilmaril@0
  1788
		num=QString ("%1").arg(num.toUInt()+1);
insilmaril@0
  1789
insilmaril@0
  1790
		s=s+num;
insilmaril@0
  1791
		
insilmaril@0
  1792
		// Try to select this one
insilmaril@0
  1793
		if (select (s)) return;
insilmaril@0
  1794
insilmaril@0
  1795
		// We have no direct successor, 
insilmaril@0
  1796
		// try to increase the parental number in order to
insilmaril@0
  1797
		// find a successor with same depth
insilmaril@0
  1798
insilmaril@0
  1799
		int d=selection->getDepth();
insilmaril@0
  1800
		int oldDepth=d;
insilmaril@0
  1801
		int i;
insilmaril@0
  1802
		bool found=false;
insilmaril@0
  1803
		bool b;
insilmaril@0
  1804
		while (!found && d>0)
insilmaril@0
  1805
		{
insilmaril@0
  1806
			s=s.section (",",0,d-1);
insilmaril@0
  1807
			// replace substring of current depth in s with "1"
insilmaril@0
  1808
			part=s.section(",",-1);
insilmaril@0
  1809
			typ=part.left (3);
insilmaril@0
  1810
			num=part.right(part.length() - 3);
insilmaril@0
  1811
insilmaril@0
  1812
			if (d>1)
insilmaril@0
  1813
			{	
insilmaril@0
  1814
				// increase number of parent
insilmaril@0
  1815
				num=QString ("%1").arg(num.toUInt()+1);
insilmaril@0
  1816
				s=s.section (",",0,d-2) + ","+ typ+num;
insilmaril@0
  1817
			} else
insilmaril@0
  1818
			{
insilmaril@0
  1819
				// Special case, look at orientation
insilmaril@0
  1820
				if (selection->getOrientation()==OrientRightOfCenter)
insilmaril@0
  1821
					num=QString ("%1").arg(num.toUInt()+1);
insilmaril@0
  1822
				else	
insilmaril@0
  1823
					num=QString ("%1").arg(num.toUInt()-1);
insilmaril@0
  1824
				s=typ+num;
insilmaril@0
  1825
			}	
insilmaril@0
  1826
insilmaril@0
  1827
			if (select (s))
insilmaril@0
  1828
				// pad to oldDepth, select the first branch for each depth
insilmaril@0
  1829
				for (i=d;i<oldDepth;i++)
insilmaril@0
  1830
				{
insilmaril@0
  1831
					b=select (s);
insilmaril@0
  1832
					if (b)
insilmaril@0
  1833
					{	
insilmaril@2
  1834
						if ( ((BranchObj*)selection)->countBranches()>0)
insilmaril@0
  1835
							s+=",bo:0";
insilmaril@0
  1836
						else	
insilmaril@0
  1837
							break;
insilmaril@0
  1838
					} else
insilmaril@0
  1839
						break;
insilmaril@0
  1840
				}	
insilmaril@0
  1841
insilmaril@0
  1842
			// try to select the freshly built string
insilmaril@0
  1843
			found=select(s);
insilmaril@0
  1844
			d--;
insilmaril@0
  1845
		}
insilmaril@0
  1846
		return;
insilmaril@0
  1847
	}	
insilmaril@0
  1848
}
insilmaril@0
  1849
insilmaril@0
  1850
void MapEditor::selectPrevBranch()
insilmaril@0
  1851
{
insilmaril@0
  1852
	// Decrease number of branch
insilmaril@0
  1853
	if (selection)
insilmaril@0
  1854
	{
insilmaril@0
  1855
		QString s=selection->getSelectString();
insilmaril@0
  1856
		QString part;
insilmaril@0
  1857
		QString typ;
insilmaril@0
  1858
		QString num;
insilmaril@0
  1859
insilmaril@0
  1860
		// Where am I? 
insilmaril@0
  1861
		part=s.section(",",-1);
insilmaril@0
  1862
		typ=part.left (3);
insilmaril@0
  1863
		num=part.right(part.length() - 3);
insilmaril@0
  1864
insilmaril@0
  1865
		s=s.left (s.length() -num.length());
insilmaril@0
  1866
insilmaril@0
  1867
		// Go to next lmo
insilmaril@0
  1868
		num=QString ("%1").arg(num.toUInt()-1);
insilmaril@0
  1869
insilmaril@0
  1870
		s=s+num;
insilmaril@0
  1871
		
insilmaril@0
  1872
		// Try to select this one
insilmaril@0
  1873
		if (select (s)) return;
insilmaril@0
  1874
insilmaril@0
  1875
		// We have no direct precessor, 
insilmaril@0
  1876
		// try to decrease the parental number in order to
insilmaril@0
  1877
		// find a precessor with same depth
insilmaril@0
  1878
insilmaril@0
  1879
		int d=selection->getDepth();
insilmaril@0
  1880
		int oldDepth=d;
insilmaril@0
  1881
		int i;
insilmaril@0
  1882
		bool found=false;
insilmaril@0
  1883
		bool b;
insilmaril@0
  1884
		while (!found && d>0)
insilmaril@0
  1885
		{
insilmaril@0
  1886
			s=s.section (",",0,d-1);
insilmaril@0
  1887
			// replace substring of current depth in s with "1"
insilmaril@0
  1888
			part=s.section(",",-1);
insilmaril@0
  1889
			typ=part.left (3);
insilmaril@0
  1890
			num=part.right(part.length() - 3);
insilmaril@0
  1891
insilmaril@0
  1892
			if (d>1)
insilmaril@0
  1893
			{
insilmaril@0
  1894
				// decrease number of parent
insilmaril@0
  1895
				num=QString ("%1").arg(num.toUInt()-1);
insilmaril@0
  1896
				s=s.section (",",0,d-2) + ","+ typ+num;
insilmaril@0
  1897
			} else
insilmaril@0
  1898
			{
insilmaril@0
  1899
				// Special case, look at orientation
insilmaril@0
  1900
				if (selection->getOrientation()==OrientRightOfCenter)
insilmaril@0
  1901
					num=QString ("%1").arg(num.toUInt()-1);
insilmaril@0
  1902
				else	
insilmaril@0
  1903
					num=QString ("%1").arg(num.toUInt()+1);
insilmaril@0
  1904
				s=typ+num;
insilmaril@0
  1905
			}	
insilmaril@0
  1906
insilmaril@0
  1907
			if (select(s))
insilmaril@0
  1908
				// pad to oldDepth, select the last branch for each depth
insilmaril@0
  1909
				for (i=d;i<oldDepth;i++)
insilmaril@0
  1910
				{
insilmaril@0
  1911
					b=select (s);
insilmaril@0
  1912
					if (b)
insilmaril@2
  1913
						if ( ((BranchObj*)selection)->countBranches()>0)
insilmaril@2
  1914
							s+=",bo:"+ QString ("%1").arg( ((BranchObj*)selection)->countBranches()-1 );
insilmaril@0
  1915
						else	
insilmaril@0
  1916
							break;
insilmaril@0
  1917
					else
insilmaril@0
  1918
						break;
insilmaril@0
  1919
				}	
insilmaril@0
  1920
			
insilmaril@0
  1921
			// try to select the freshly built string
insilmaril@0
  1922
			found=select(s);
insilmaril@0
  1923
			d--;
insilmaril@0
  1924
		}
insilmaril@0
  1925
		return;
insilmaril@0
  1926
	}	
insilmaril@0
  1927
}
insilmaril@0
  1928
insilmaril@0
  1929
void MapEditor::selectUpperBranch()
insilmaril@0
  1930
{
insilmaril@0
  1931
	// Finish open lineEdits
insilmaril@2
  1932
	if (lineedit) finishedLineEdit();
insilmaril@0
  1933
insilmaril@0
  1934
	if (selection) 
insilmaril@0
  1935
	{
insilmaril@0
  1936
		if (typeid(*selection) == typeid(BranchObj))
insilmaril@0
  1937
		{
insilmaril@0
  1938
			if (selection->getOrientation()==OrientRightOfCenter)
insilmaril@0
  1939
				selectPrevBranch();
insilmaril@0
  1940
			else
insilmaril@0
  1941
				if (selection->getDepth()==1)
insilmaril@0
  1942
					selectNextBranch();
insilmaril@0
  1943
				else
insilmaril@0
  1944
					selectPrevBranch();
insilmaril@0
  1945
		}		
insilmaril@0
  1946
	}
insilmaril@0
  1947
}
insilmaril@0
  1948
insilmaril@0
  1949
void MapEditor::selectLowerBranch()
insilmaril@0
  1950
{
insilmaril@0
  1951
	// Finish open lineEdits
insilmaril@2
  1952
	if (lineedit) finishedLineEdit();
insilmaril@0
  1953
insilmaril@0
  1954
	if (selection) 
insilmaril@0
  1955
	{
insilmaril@0
  1956
		if (typeid(*selection) == typeid(BranchObj))
insilmaril@0
  1957
		{
insilmaril@0
  1958
			if (selection->getOrientation()==OrientRightOfCenter)
insilmaril@0
  1959
				selectNextBranch();
insilmaril@0
  1960
			else
insilmaril@0
  1961
				if (selection->getDepth()==1)
insilmaril@0
  1962
					selectPrevBranch();
insilmaril@0
  1963
				else
insilmaril@0
  1964
					selectNextBranch();
insilmaril@0
  1965
		}		
insilmaril@0
  1966
	}
insilmaril@0
  1967
}
insilmaril@0
  1968
insilmaril@0
  1969
insilmaril@0
  1970
void MapEditor::selectLeftBranch()
insilmaril@0
  1971
{
insilmaril@0
  1972
	// Finish open lineEdits
insilmaril@2
  1973
	if (lineedit) finishedLineEdit();
insilmaril@0
  1974
insilmaril@0
  1975
	BranchObj* bo;
insilmaril@0
  1976
	BranchObj* par;
insilmaril@0
  1977
	if (selection) 
insilmaril@0
  1978
	{
insilmaril@0
  1979
		if (typeid(*selection) == typeid(MapCenterObj))
insilmaril@0
  1980
		{
insilmaril@2
  1981
			par=  (BranchObj*) selection;
insilmaril@0
  1982
			bo=par->getLastSelectedBranch();
insilmaril@0
  1983
			if (bo)
insilmaril@0
  1984
			{
insilmaril@0
  1985
				// Workaround for reselecting on left and right side
insilmaril@0
  1986
				if (bo->getOrientation()==OrientRightOfCenter)
insilmaril@0
  1987
				{
insilmaril@0
  1988
					bo=par->getLastBranch();
insilmaril@0
  1989
				}	
insilmaril@0
  1990
				if (bo)
insilmaril@0
  1991
				{
insilmaril@0
  1992
					par->unselect();
insilmaril@0
  1993
					selection=bo;
insilmaril@0
  1994
					selection->select();
insilmaril@0
  1995
					adjustCanvasSize();
insilmaril@0
  1996
					ensureSelectionVisible();
insilmaril@0
  1997
				}
insilmaril@0
  1998
			}	
insilmaril@0
  1999
		} else
insilmaril@0
  2000
		{
insilmaril@0
  2001
			par=(BranchObj*)(selection->getParObj());
insilmaril@0
  2002
			if (selection->getOrientation()==OrientRightOfCenter)
insilmaril@0
  2003
			{
insilmaril@0
  2004
				if (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2005
					typeid(*selection) == typeid(FloatImageObj))
insilmaril@0
  2006
				{
insilmaril@0
  2007
					selection->unselect();
insilmaril@0
  2008
					selection=par;
insilmaril@0
  2009
					selection->select();
insilmaril@0
  2010
					adjustCanvasSize();
insilmaril@0
  2011
					ensureSelectionVisible();
insilmaril@0
  2012
				}
insilmaril@0
  2013
			} else
insilmaril@0
  2014
			{
insilmaril@0
  2015
				if (typeid(*selection) == typeid(BranchObj) )
insilmaril@0
  2016
				{
insilmaril@2
  2017
					bo=((BranchObj*)selection)->getLastSelectedBranch();
insilmaril@0
  2018
					if (bo) 
insilmaril@0
  2019
					{
insilmaril@0
  2020
						selection->unselect();
insilmaril@0
  2021
						selection=bo;
insilmaril@0
  2022
						selection->select();
insilmaril@0
  2023
						adjustCanvasSize();
insilmaril@0
  2024
						ensureSelectionVisible();
insilmaril@0
  2025
					}
insilmaril@0
  2026
				}
insilmaril@0
  2027
			}
insilmaril@0
  2028
		}	
insilmaril@0
  2029
	}
insilmaril@0
  2030
}
insilmaril@0
  2031
insilmaril@0
  2032
void MapEditor::selectRightBranch()
insilmaril@0
  2033
{
insilmaril@0
  2034
	// Finish open lineEdits
insilmaril@2
  2035
	if (lineedit) finishedLineEdit();
insilmaril@0
  2036
insilmaril@0
  2037
	BranchObj* bo;
insilmaril@0
  2038
	BranchObj* par;
insilmaril@0
  2039
insilmaril@0
  2040
	if (selection) 
insilmaril@0
  2041
	{
insilmaril@0
  2042
		if (typeid(*selection) == typeid(MapCenterObj))
insilmaril@0
  2043
		{
insilmaril@2
  2044
			par=  (BranchObj*) selection;
insilmaril@0
  2045
			bo=par->getLastSelectedBranch();
insilmaril@0
  2046
			if (bo)
insilmaril@0
  2047
			{
insilmaril@0
  2048
				// Workaround for relecting on left and right side
insilmaril@0
  2049
				if (bo->getOrientation()==OrientLeftOfCenter)
insilmaril@0
  2050
					bo=par->getFirstBranch();
insilmaril@0
  2051
				if (bo)
insilmaril@0
  2052
				{
insilmaril@0
  2053
					par->unselect();
insilmaril@0
  2054
					selection=bo;
insilmaril@0
  2055
					selection->select();
insilmaril@0
  2056
					ensureSelectionVisible();
insilmaril@0
  2057
				}
insilmaril@0
  2058
			}
insilmaril@0
  2059
		} else
insilmaril@0
  2060
		{
insilmaril@0
  2061
			par=(BranchObj*)(selection->getParObj());
insilmaril@0
  2062
			if (selection->getOrientation()==OrientLeftOfCenter)
insilmaril@0
  2063
			{
insilmaril@0
  2064
				if (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2065
					typeid(*selection) == typeid(FloatImageObj))
insilmaril@0
  2066
				{
insilmaril@0
  2067
					selection->unselect();
insilmaril@0
  2068
					selection=par;
insilmaril@0
  2069
					selection->select();
insilmaril@0
  2070
					adjustCanvasSize();
insilmaril@0
  2071
					ensureSelectionVisible();
insilmaril@0
  2072
				}
insilmaril@0
  2073
			} else
insilmaril@0
  2074
			{
insilmaril@0
  2075
				if (typeid(*selection) == typeid(BranchObj) )
insilmaril@0
  2076
				{
insilmaril@2
  2077
					bo=((BranchObj*)selection)->getLastSelectedBranch();
insilmaril@0
  2078
					if (bo) 
insilmaril@0
  2079
					{
insilmaril@0
  2080
						selection->unselect();
insilmaril@0
  2081
						selection=bo;
insilmaril@0
  2082
						selection->select();
insilmaril@0
  2083
						adjustCanvasSize();
insilmaril@0
  2084
						ensureSelectionVisible();
insilmaril@0
  2085
					}
insilmaril@0
  2086
				}
insilmaril@0
  2087
			}
insilmaril@0
  2088
		}
insilmaril@0
  2089
	}
insilmaril@0
  2090
}
insilmaril@0
  2091
insilmaril@0
  2092
void MapEditor::selectFirstBranch()
insilmaril@0
  2093
{
insilmaril@0
  2094
	// Finish open lineEdits
insilmaril@2
  2095
	if (lineedit) finishedLineEdit();
insilmaril@0
  2096
insilmaril@0
  2097
	BranchObj *bo1;
insilmaril@0
  2098
	BranchObj *bo2;
insilmaril@0
  2099
	BranchObj* par;
insilmaril@0
  2100
	if (selection) {
insilmaril@0
  2101
		if (typeid(*selection) == typeid(BranchObj))
insilmaril@0
  2102
		{
insilmaril@2
  2103
			bo1=  (BranchObj*) selection;
insilmaril@0
  2104
			par=(BranchObj*)(bo1->getParObj());
insilmaril@0
  2105
			bo2=par->getFirstBranch();
insilmaril@0
  2106
			if (bo2) {
insilmaril@0
  2107
				bo1->unselect();
insilmaril@0
  2108
				selection=bo2;
insilmaril@0
  2109
				selection->select();
insilmaril@0
  2110
				ensureSelectionVisible();
insilmaril@0
  2111
			}
insilmaril@0
  2112
		}		
insilmaril@0
  2113
		adjustCanvasSize();
insilmaril@0
  2114
	}
insilmaril@0
  2115
}
insilmaril@0
  2116
insilmaril@0
  2117
void MapEditor::selectLastBranch()
insilmaril@0
  2118
{
insilmaril@0
  2119
	// Finish open lineEdits
insilmaril@2
  2120
	if (lineedit) finishedLineEdit();
insilmaril@0
  2121
insilmaril@0
  2122
	BranchObj *bo1;
insilmaril@0
  2123
	BranchObj *bo2;
insilmaril@0
  2124
	BranchObj* par;
insilmaril@0
  2125
	if (selection) {
insilmaril@0
  2126
		if (typeid(*selection) == typeid(BranchObj))
insilmaril@0
  2127
		{
insilmaril@2
  2128
			bo1=  (BranchObj*) selection;
insilmaril@0
  2129
			par=(BranchObj*)(bo1->getParObj());
insilmaril@0
  2130
			bo2=par->getLastBranch();
insilmaril@0
  2131
			if (bo2) {
insilmaril@0
  2132
				bo1->unselect();
insilmaril@0
  2133
				selection=bo2;
insilmaril@0
  2134
				selection->select();
insilmaril@0
  2135
				ensureSelectionVisible();
insilmaril@0
  2136
			}
insilmaril@0
  2137
		}		
insilmaril@0
  2138
		adjustCanvasSize();
insilmaril@0
  2139
	}
insilmaril@0
  2140
}
insilmaril@0
  2141
insilmaril@0
  2142
void MapEditor::setColor(QColor c)
insilmaril@0
  2143
{
insilmaril@0
  2144
	actColor=c;
insilmaril@0
  2145
}
insilmaril@0
  2146
insilmaril@0
  2147
void MapEditor::selectBackgroundColor()
insilmaril@0
  2148
{
insilmaril@0
  2149
	// Finish open lineEdits
insilmaril@2
  2150
	if (lineedit) finishedLineEdit();
insilmaril@0
  2151
insilmaril@0
  2152
	QColor col = QColorDialog::getColor( mapCanvas->backgroundColor(), this );
insilmaril@0
  2153
	if ( !col.isValid() ) return;
insilmaril@0
  2154
	setBackgroundColor( col );
insilmaril@2
  2155
	saveState(QString("Set background color of map to %1").arg(col.name()));
insilmaril@0
  2156
}
insilmaril@0
  2157
insilmaril@0
  2158
void MapEditor::setBackgroundColor(QColor c)
insilmaril@0
  2159
{
insilmaril@0
  2160
	mapCanvas->setBackgroundColor (c);
insilmaril@0
  2161
}
insilmaril@0
  2162
insilmaril@0
  2163
QColor MapEditor::pickColor()
insilmaril@0
  2164
{
insilmaril@0
  2165
	if (selection) 
insilmaril@0
  2166
	{
insilmaril@0
  2167
		if (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2168
			typeid(*selection) == typeid(MapCenterObj))
insilmaril@0
  2169
		{
insilmaril@2
  2170
			BranchObj *bo=(BranchObj*)selection;
insilmaril@0
  2171
			actColor=bo->getColor(); 
insilmaril@0
  2172
		}    
insilmaril@0
  2173
	}
insilmaril@0
  2174
	return actColor;
insilmaril@0
  2175
}
insilmaril@0
  2176
insilmaril@0
  2177
void MapEditor::colorItem()
insilmaril@0
  2178
{
insilmaril@0
  2179
	if (selection) 
insilmaril@0
  2180
	{
insilmaril@0
  2181
		if (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2182
			typeid(*selection) == typeid(MapCenterObj))
insilmaril@0
  2183
		{
insilmaril@2
  2184
			BranchObj *bo=(BranchObj*)selection;
insilmaril@2
  2185
			saveState(selection, QString("Set color of %1 to %2").arg(getName(bo)).arg(actColor.name()));	//TODO undoCommand
insilmaril@2
  2186
			bo->setColor(actColor); // color branch
insilmaril@0
  2187
		}    
insilmaril@0
  2188
	}
insilmaril@0
  2189
}
insilmaril@0
  2190
insilmaril@0
  2191
void MapEditor::colorBranch()
insilmaril@0
  2192
{
insilmaril@0
  2193
	if (selection) 
insilmaril@0
  2194
	{
insilmaril@0
  2195
		if (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2196
			typeid(*selection) == typeid(MapCenterObj))
insilmaril@0
  2197
		{
insilmaril@2
  2198
			BranchObj *bo=(BranchObj*)selection;
insilmaril@2
  2199
			saveState(selection, QString ("Set color of %1 and childs to %2").arg(getName(bo)).arg(actColor.name()));	//TODO undoCommand
insilmaril@2
  2200
			bo->setColorChilds(actColor); // color links, color childs
insilmaril@0
  2201
		}    
insilmaril@0
  2202
	}
insilmaril@0
  2203
}
insilmaril@0
  2204
insilmaril@0
  2205
insilmaril@0
  2206
void MapEditor::toggleStandardFlag(QString f)
insilmaril@0
  2207
{
insilmaril@0
  2208
	if (selection)
insilmaril@0
  2209
	{
insilmaril@3
  2210
		if (typeid(*selection) == typeid(BranchObj) ||
insilmaril@3
  2211
			typeid(*selection) == typeid(MapCenterObj))
insilmaril@3
  2212
		{
insilmaril@3
  2213
			BranchObj *bo=(BranchObj*)selection;
insilmaril@3
  2214
			QString s;
insilmaril@3
  2215
			if (bo->isSetStandardFlag(f))
insilmaril@3
  2216
				s="Unset";
insilmaril@3
  2217
			else
insilmaril@3
  2218
				s="Set";
insilmaril@3
  2219
			saveState(selection, QString("%1 standard flag \"%2\" of %3").arg(s).arg(f).arg(getName(bo)));// TODO undoCommand	
insilmaril@3
  2220
			bo->toggleStandardFlag (f,actionSettingsUseFlagGroups->isOn());
insilmaril@3
  2221
			adjustCanvasSize();
insilmaril@3
  2222
		}
insilmaril@0
  2223
	}	
insilmaril@0
  2224
}
insilmaril@0
  2225
insilmaril@0
  2226
void MapEditor::setViewCenter()
insilmaril@0
  2227
{
insilmaril@0
  2228
	// transform to CanvasView Coord:
insilmaril@0
  2229
	QPoint p=worldMatrix().map(movingCenter);
insilmaril@0
  2230
	center ( p.x(), p.y());
insilmaril@0
  2231
}
insilmaril@0
  2232
insilmaril@0
  2233
insilmaril@0
  2234
BranchObj* MapEditor::findText (QString s, bool cs)
insilmaril@0
  2235
{
insilmaril@9
  2236
	QTextDocument::FindFlags flags=0;
insilmaril@9
  2237
	if (cs) flags=QTextDocument::QTextDocument::FindCaseSensitively;
insilmaril@9
  2238
insilmaril@0
  2239
	if (!itFind) 
insilmaril@0
  2240
	{	// Nothing found or new find process
insilmaril@0
  2241
		if (EOFind)
insilmaril@0
  2242
			// nothing found, start again
insilmaril@0
  2243
			EOFind=false;
insilmaril@0
  2244
		itFind=mapCenter->first();
insilmaril@0
  2245
	}	
insilmaril@0
  2246
	bool searching=true;
insilmaril@0
  2247
	bool foundNote=false;
insilmaril@0
  2248
	while (searching && !EOFind)
insilmaril@0
  2249
	{
insilmaril@0
  2250
		if (itFind)
insilmaril@0
  2251
		{
insilmaril@0
  2252
			// Searching in Note
insilmaril@0
  2253
			if (itFind->getNote().contains(s,cs))
insilmaril@0
  2254
			{
insilmaril@0
  2255
				if (selection!=itFind) 
insilmaril@0
  2256
				{
insilmaril@2
  2257
					if (selection) ((BranchObj*)selection)->unselect();
insilmaril@0
  2258
					selection=itFind;
insilmaril@0
  2259
					selection->select();
insilmaril@0
  2260
					adjustCanvasSize();
insilmaril@0
  2261
					ensureSelectionVisible();
insilmaril@0
  2262
				}
insilmaril@9
  2263
				if (textEditor->findText(s,flags)) 
insilmaril@0
  2264
				{
insilmaril@0
  2265
					searching=false;
insilmaril@0
  2266
					foundNote=true;
insilmaril@0
  2267
				}	
insilmaril@0
  2268
			}
insilmaril@0
  2269
			// Searching in Heading
insilmaril@0
  2270
			if (searching && itFind->getHeading().contains (s,cs) ) 
insilmaril@0
  2271
			{
insilmaril@2
  2272
				if (selection) ((BranchObj*)selection)->unselect();
insilmaril@0
  2273
				selection=itFind;
insilmaril@0
  2274
				selection->select();
insilmaril@0
  2275
				adjustCanvasSize();
insilmaril@0
  2276
				ensureSelectionVisible();
insilmaril@0
  2277
				searching=false;
insilmaril@0
  2278
			}
insilmaril@0
  2279
		}	
insilmaril@0
  2280
		if (!foundNote)
insilmaril@0
  2281
		{
insilmaril@0
  2282
			itFind=itFind->next();
insilmaril@0
  2283
			if (!itFind) EOFind=true;
insilmaril@0
  2284
		}
insilmaril@0
  2285
	}	
insilmaril@0
  2286
	if (!searching)
insilmaril@0
  2287
	{
insilmaril@0
  2288
		adjustCanvasSize();
insilmaril@2
  2289
		return (BranchObj*)selection;
insilmaril@0
  2290
	}	else
insilmaril@0
  2291
		return NULL;
insilmaril@0
  2292
}
insilmaril@0
  2293
insilmaril@0
  2294
void MapEditor::findReset()
insilmaril@0
  2295
{	// Necessary if text to find changes during a find process
insilmaril@0
  2296
	itFind=NULL;
insilmaril@0
  2297
	EOFind=false;
insilmaril@0
  2298
}
insilmaril@0
  2299
insilmaril@0
  2300
void MapEditor::editURL()
insilmaril@0
  2301
{
insilmaril@0
  2302
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2303
			typeid(*selection) == typeid(MapCenterObj)) )
insilmaril@0
  2304
	{		
insilmaril@0
  2305
		bool ok;
insilmaril@2
  2306
		BranchObj *bo=(BranchObj*)selection;
insilmaril@0
  2307
		QString text = QInputDialog::getText(
insilmaril@0
  2308
				"VYM", tr("Enter URL:"), QLineEdit::Normal,
insilmaril@2
  2309
				bo->getURL(), &ok, this );
insilmaril@0
  2310
		if ( ok) 
insilmaril@0
  2311
		{
insilmaril@0
  2312
			// user entered something and pressed OK
insilmaril@2
  2313
			saveState("setURL (\""+bo->getURL()+"\")","setURL (\""+text+"\")", QString("Set URL of %1 to %21").arg(getName(bo)).arg(text));	
insilmaril@2
  2314
			bo->setURL (text);
insilmaril@0
  2315
			updateActions();
insilmaril@0
  2316
		}	
insilmaril@0
  2317
	}
insilmaril@0
  2318
}
insilmaril@0
  2319
insilmaril@2
  2320
QString MapEditor::getURL()
insilmaril@2
  2321
{
insilmaril@2
  2322
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@2
  2323
			typeid(*selection) == typeid(MapCenterObj)) )
insilmaril@2
  2324
		return ((BranchObj*)selection)->getURL();
insilmaril@2
  2325
	else
insilmaril@2
  2326
		return "";
insilmaril@2
  2327
}
insilmaril@2
  2328
insilmaril@0
  2329
void MapEditor::editHeading2URL()
insilmaril@0
  2330
{
insilmaril@0
  2331
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2332
			typeid(*selection) == typeid(MapCenterObj)) )
insilmaril@0
  2333
	{		
insilmaril@2
  2334
		BranchObj *bo=(BranchObj*)selection;
insilmaril@2
  2335
		saveState("setURL (\""+bo->getURL()+"\")","setURL (\""+bo->getHeading()+"\")",QString("Copy heading of %1 to URL").arg(getName(bo)));	
insilmaril@2
  2336
		bo->setURL (bo->getHeading());
insilmaril@0
  2337
		updateActions();
insilmaril@0
  2338
	}
insilmaril@0
  2339
}	
insilmaril@0
  2340
insilmaril@0
  2341
void MapEditor::editBugzilla2URL()
insilmaril@0
  2342
{
insilmaril@0
  2343
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2344
			typeid(*selection) == typeid(MapCenterObj)) )
insilmaril@0
  2345
	{		
insilmaril@2
  2346
		BranchObj *bo=(BranchObj*)selection;
insilmaril@2
  2347
		QString url= "https://bugzilla.novell.com/show_bug.cgi?id="+bo->getHeading();
insilmaril@2
  2348
		saveState("setURL (\""+bo->getURL()+"\")","setURL (\""+url+"\")",QString("Use heading of %1 as link to Novell Bugzilla").arg(getName(bo)));	
insilmaril@2
  2349
		bo->setURL (url);
insilmaril@0
  2350
		updateActions();
insilmaril@2
  2351
	}
insilmaril@2
  2352
}	
insilmaril@2
  2353
insilmaril@2
  2354
void MapEditor::editFATE2URL()
insilmaril@2
  2355
{
insilmaril@2
  2356
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@2
  2357
			typeid(*selection) == typeid(MapCenterObj)) )
insilmaril@2
  2358
	{		
insilmaril@2
  2359
		BranchObj *bo=(BranchObj*)selection;
insilmaril@2
  2360
		QString url= "http://keeper.suse.de:8080/webfate/match/id?value=ID"+bo->getHeading();
insilmaril@2
  2361
		saveState("setURL (\""+bo->getURL()+"\")","setURL (\""+url+"\")",QString("Use heading of %1 as link to FATE").arg(getName(bo)));	
insilmaril@2
  2362
		bo->setURL (url);
insilmaril@2
  2363
		updateActions();
insilmaril@0
  2364
	}
insilmaril@0
  2365
}	
insilmaril@0
  2366
insilmaril@0
  2367
void MapEditor::editVymLink()
insilmaril@0
  2368
{
insilmaril@0
  2369
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2370
			typeid(*selection) == typeid(MapCenterObj)) )
insilmaril@0
  2371
	{		
insilmaril@2
  2372
		BranchObj *bo=(BranchObj*)selection;
insilmaril@2
  2373
		Q3FileDialog *fd=new Q3FileDialog( this,__VYM " - " +tr("Link to another map"));
insilmaril@0
  2374
		fd->addFilter (QString (tr("vym map") + " (*.vym)"));
insilmaril@2
  2375
		fd->setCaption(__VYM " - " +tr("Link to another map"));
insilmaril@2
  2376
		if (! bo->getVymLink().isEmpty() )
insilmaril@2
  2377
			fd->setSelection( bo->getVymLink() );
insilmaril@0
  2378
		fd->show();
insilmaril@0
  2379
insilmaril@0
  2380
		QString fn;
insilmaril@0
  2381
		if ( fd->exec() == QDialog::Accepted )
insilmaril@2
  2382
		{
insilmaril@2
  2383
			saveState("setVymLink (\""+bo->getVymLink()+"\")","setVymLink (\""+fd->selectedFile()+"\")",QString("Set vymlink of %1 to %2").arg(getName(bo)).arg(fd->selectedFile()));	
insilmaril@2
  2384
			bo->setVymLink (fd->selectedFile() );
insilmaril@2
  2385
			updateActions();
insilmaril@2
  2386
			mapCenter->reposition();
insilmaril@2
  2387
			adjustCanvasSize();
insilmaril@2
  2388
			canvas()->update();
insilmaril@2
  2389
		}
insilmaril@0
  2390
	}
insilmaril@0
  2391
}
insilmaril@0
  2392
insilmaril@0
  2393
void MapEditor::deleteVymLink()
insilmaril@0
  2394
{
insilmaril@0
  2395
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2396
			typeid(*selection) == typeid(MapCenterObj)) )
insilmaril@0
  2397
	{		
insilmaril@2
  2398
		BranchObj *bo=(BranchObj*)selection;
insilmaril@2
  2399
		saveState("setVymLink (\""+bo->getVymLink()+"\")","setVymLink (\"\")",QString("Unset vymlink of %1").arg(getName(bo)));	
insilmaril@2
  2400
		bo->setVymLink ("" );
insilmaril@0
  2401
		updateActions();
insilmaril@0
  2402
		mapCenter->reposition();
insilmaril@0
  2403
		adjustCanvasSize();
insilmaril@0
  2404
		canvas()->update();
insilmaril@2
  2405
	}
insilmaril@2
  2406
}
insilmaril@2
  2407
insilmaril@2
  2408
void MapEditor::toggleHideExport()
insilmaril@2
  2409
{
insilmaril@2
  2410
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@2
  2411
			typeid(*selection) == typeid(MapCenterObj)) ||
insilmaril@2
  2412
			(typeid(*selection)==typeid(FloatImageObj))
insilmaril@2
  2413
			)
insilmaril@2
  2414
	{		
insilmaril@2
  2415
		OrnamentedObj *oo=(OrnamentedObj*)selection;
insilmaril@2
  2416
		QString s;
insilmaril@2
  2417
		if (oo->hideInExport())
insilmaril@2
  2418
		{
insilmaril@2
  2419
			oo->setHideInExport(false);
insilmaril@2
  2420
			s="Unset";
insilmaril@2
  2421
		}	
insilmaril@2
  2422
		else	
insilmaril@2
  2423
		{
insilmaril@2
  2424
			oo->setHideInExport(true);
insilmaril@2
  2425
			s="Set";
insilmaril@2
  2426
		}	
insilmaril@2
  2427
		saveState(QString ("%1 hide export flag of %2").arg(s).arg(getName(selection)));	//TODO undoCommand
insilmaril@2
  2428
		actionEditToggleHideExport->setOn (oo->hideInExport());	
insilmaril@2
  2429
		updateActions();
insilmaril@2
  2430
		mapCenter->reposition();
insilmaril@2
  2431
		adjustCanvasSize();
insilmaril@2
  2432
		canvas()->update();
insilmaril@0
  2433
	}
insilmaril@0
  2434
}
insilmaril@0
  2435
insilmaril@0
  2436
QString MapEditor::getVymLink()
insilmaril@0
  2437
{
insilmaril@0
  2438
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2439
			typeid(*selection) == typeid(MapCenterObj)) )
insilmaril@0
  2440
	{		
insilmaril@2
  2441
		return ((BranchObj*)selection)->getVymLink();
insilmaril@0
  2442
	}
insilmaril@0
  2443
	return "";
insilmaril@0
  2444
	
insilmaril@0
  2445
}
insilmaril@0
  2446
insilmaril@2
  2447
void MapEditor::removeBranchKeepChilds()
insilmaril@2
  2448
{
insilmaril@2
  2449
	if (selection && (typeid(*selection) == typeid(BranchObj) ))
insilmaril@2
  2450
	{		
insilmaril@2
  2451
		BranchObj* bo=(BranchObj*)selection;
insilmaril@2
  2452
		BranchObj* par=(BranchObj*)(bo->getParObj());
insilmaril@2
  2453
		QString s=QString("Remove %1 and keep its childs").arg(getName(bo));
insilmaril@2
  2454
		if (bo->getDepth()==1)
insilmaril@2
  2455
			saveState(s);
insilmaril@2
  2456
		else	
insilmaril@2
  2457
			saveState(selection->getParObj(),s);	// TODO undoCommand
insilmaril@2
  2458
		QString sel=selection->getSelectString();
insilmaril@2
  2459
		unselect();
insilmaril@2
  2460
		par->removeBranchHere(bo);
insilmaril@2
  2461
		mapCenter->reposition();
insilmaril@2
  2462
		select (sel);
insilmaril@2
  2463
	}	
insilmaril@2
  2464
}
insilmaril@2
  2465
insilmaril@2
  2466
void MapEditor::removeChilds()
insilmaril@2
  2467
{
insilmaril@2
  2468
	if (selection && (typeid(*selection) == typeid(BranchObj) ))
insilmaril@2
  2469
	{		
insilmaril@2
  2470
		saveState(selection->getParObj(), QString("Remove childs of branch %1").arg(getName(selection)));
insilmaril@2
  2471
		((BranchObj*)selection)->removeChilds();
insilmaril@2
  2472
		mapCenter->reposition();
insilmaril@2
  2473
	}	
insilmaril@2
  2474
}
insilmaril@2
  2475
insilmaril@0
  2476
void MapEditor::editMapInfo()
insilmaril@0
  2477
{
insilmaril@0
  2478
	ExtraInfoDialog dia;
insilmaril@0
  2479
	dia.setMapName (getFileName() );
insilmaril@0
  2480
	dia.setAuthor (mapCenter->getAuthor() );
insilmaril@0
  2481
	dia.setComment(mapCenter->getComment() );
insilmaril@0
  2482
insilmaril@0
  2483
	// Calc some stats
insilmaril@0
  2484
	QString stats;
insilmaril@0
  2485
    int i=0;
insilmaril@2
  2486
    Q3CanvasItemList l=canvas()->allItems();
insilmaril@2
  2487
    for (Q3CanvasItemList::Iterator it=l.begin(); it!=l.end(); ++it) 
insilmaril@0
  2488
        i++;
insilmaril@0
  2489
    stats+=QString ("%1 items on canvas\n").arg (i,6);
insilmaril@0
  2490
insilmaril@0
  2491
	uint b=0;
insilmaril@0
  2492
	uint f=0;
insilmaril@0
  2493
	uint n=0;
insilmaril@2
  2494
	uint xl=0;
insilmaril@0
  2495
	BranchObj *bo;
insilmaril@0
  2496
	bo=mapCenter->first();
insilmaril@0
  2497
	while (bo) 
insilmaril@0
  2498
	{
insilmaril@0
  2499
		if (!bo->getNote().isEmpty() ) n++;
insilmaril@0
  2500
		f+= bo->countFloatImages();
insilmaril@0
  2501
		b++;
insilmaril@2
  2502
		xl+=bo->countXLinks();
insilmaril@0
  2503
		bo=bo->next();
insilmaril@0
  2504
	}
insilmaril@0
  2505
    stats+=QString ("%1 branches\n").arg (b-1,6);
insilmaril@2
  2506
    stats+=QString ("%1 xLinks \n").arg (xl,6);
insilmaril@0
  2507
    stats+=QString ("%1 notes\n").arg (n,6);
insilmaril@0
  2508
    stats+=QString ("%1 images\n").arg (f,6);
insilmaril@0
  2509
	dia.setStats (stats);
insilmaril@0
  2510
insilmaril@0
  2511
	// Finally show dialog
insilmaril@0
  2512
	if (dia.exec() == QDialog::Accepted)
insilmaril@0
  2513
	{
insilmaril@2
  2514
		saveState("Edit info about map");	//TODO undoCommand
insilmaril@0
  2515
		mapCenter->setAuthor (dia.getAuthor() );
insilmaril@0
  2516
		mapCenter->setComment (dia.getComment() );
insilmaril@0
  2517
	}
insilmaril@0
  2518
}
insilmaril@0
  2519
insilmaril@0
  2520
void MapEditor::updateActions()
insilmaril@0
  2521
{
insilmaril@2
  2522
	QAction *a;
insilmaril@0
  2523
	if (getLinkColorHint()==HeadingColor) 
insilmaril@0
  2524
		actionFormatLinkColorHint->setOn(true);
insilmaril@0
  2525
	else	
insilmaril@0
  2526
		actionFormatLinkColorHint->setOn(false);
insilmaril@0
  2527
insilmaril@0
  2528
	switch (linkstyle)
insilmaril@0
  2529
	{
insilmaril@0
  2530
		case StyleLine: 
insilmaril@0
  2531
			actionFormatLinkStyleLine->setOn(true);
insilmaril@0
  2532
			break;
insilmaril@0
  2533
		case StyleParabel:
insilmaril@0
  2534
			actionFormatLinkStyleParabel->setOn(true);
insilmaril@0
  2535
			break;
insilmaril@0
  2536
		case StylePolyLine:	
insilmaril@0
  2537
			actionFormatLinkStylePolyLine->setOn(true);
insilmaril@0
  2538
			break;
insilmaril@0
  2539
		case StylePolyParabel:	
insilmaril@0
  2540
			actionFormatLinkStylePolyParabel->setOn(true);
insilmaril@0
  2541
			break;
insilmaril@0
  2542
		default:
insilmaril@0
  2543
			break;
insilmaril@0
  2544
	}	
insilmaril@0
  2545
insilmaril@0
  2546
	QPixmap pix( 16, 16 );
insilmaril@0
  2547
    pix.fill( mapCanvas->backgroundColor() );
insilmaril@0
  2548
    actionFormatBackColor->setIconSet( pix );
insilmaril@2
  2549
    pix.fill( defLinkColor );
insilmaril@0
  2550
    actionFormatLinkColor->setIconSet( pix );
insilmaril@0
  2551
insilmaril@0
  2552
	actionEditUndo->setEnabled( mapChanged );
insilmaril@0
  2553
	actionFileSave->setEnabled( mapUnsaved );
insilmaril@0
  2554
insilmaril@0
  2555
	if (selection)
insilmaril@0
  2556
	{
insilmaril@0
  2557
		if ( (typeid(*selection) == typeid(BranchObj)) || 
insilmaril@0
  2558
			(typeid(*selection) == typeid(MapCenterObj))  )
insilmaril@0
  2559
		{
insilmaril@2
  2560
			BranchObj *bo=(BranchObj*)selection;
insilmaril@2
  2561
			// Take care of links
insilmaril@2
  2562
			if (bo->countXLinks()==0)
insilmaril@2
  2563
			{
insilmaril@7
  2564
				branchLinksContextMenuEdit->clear();
insilmaril@7
  2565
				branchLinksContextMenuFollow->clear();
insilmaril@2
  2566
			} else
insilmaril@2
  2567
			{
insilmaril@2
  2568
				BranchObj *bot;
insilmaril@2
  2569
				QString s;
insilmaril@7
  2570
				branchLinksContextMenuEdit->clear();
insilmaril@7
  2571
				branchLinksContextMenuFollow->clear();
insilmaril@2
  2572
				for (int i=0; i<=bo->countXLinks();i++)
insilmaril@2
  2573
				{
insilmaril@2
  2574
					bot=bo->XLinkTargetAt(i);
insilmaril@2
  2575
					if (bot)
insilmaril@2
  2576
					{
insilmaril@2
  2577
						s=bot->getHeading();
insilmaril@2
  2578
						if (s.length()>25)
insilmaril@2
  2579
							s=s.left(25)+"...";
insilmaril@7
  2580
						branchLinksContextMenuFollow->addAction (s);
insilmaril@7
  2581
						branchLinksContextMenuEdit->addAction (s);
insilmaril@2
  2582
					}	
insilmaril@2
  2583
				}
insilmaril@2
  2584
			}
insilmaril@2
  2585
insilmaril@0
  2586
			standardFlagsDefault->setEnabled (true);
insilmaril@0
  2587
insilmaril@2
  2588
			actionEditToggleScroll->setEnabled (true);
insilmaril@2
  2589
			if ( bo->isScrolled() )
insilmaril@2
  2590
				actionEditToggleScroll->setOn(true);
insilmaril@2
  2591
			else	
insilmaril@2
  2592
				actionEditToggleScroll->setOn(false);
insilmaril@2
  2593
insilmaril@2
  2594
			if ( bo->getURL().isEmpty() )
insilmaril@2
  2595
			{
insilmaril@0
  2596
				actionEditOpenURL->setEnabled (false);
insilmaril@2
  2597
				actionEditOpenURLTab->setEnabled (false);
insilmaril@2
  2598
			}	
insilmaril@0
  2599
			else	
insilmaril@2
  2600
			{
insilmaril@0
  2601
				actionEditOpenURL->setEnabled (true);
insilmaril@2
  2602
				actionEditOpenURLTab->setEnabled (true);
insilmaril@2
  2603
			}
insilmaril@2
  2604
			if ( bo->getVymLink().isEmpty() )
insilmaril@0
  2605
			{
insilmaril@0
  2606
				actionEditOpenVymLink->setEnabled (false);
insilmaril@0
  2607
				actionEditDeleteVymLink->setEnabled (false);
insilmaril@0
  2608
			} else	
insilmaril@0
  2609
			{
insilmaril@0
  2610
				actionEditOpenVymLink->setEnabled (true);
insilmaril@0
  2611
				actionEditDeleteVymLink->setEnabled (true);
insilmaril@0
  2612
			}	
insilmaril@2
  2613
insilmaril@2
  2614
			if (bo->canMoveBranchUp()) 
insilmaril@2
  2615
				actionEditMoveUp->setEnabled (true);
insilmaril@2
  2616
			else	
insilmaril@2
  2617
				actionEditMoveUp->setEnabled (false);
insilmaril@2
  2618
			if (bo->canMoveBranchDown()) 
insilmaril@2
  2619
				actionEditMoveDown->setEnabled (true);
insilmaril@2
  2620
			else	
insilmaril@2
  2621
				actionEditMoveDown->setEnabled (false);
insilmaril@2
  2622
insilmaril@2
  2623
insilmaril@2
  2624
			actionEditToggleHideExport->setEnabled (true);	
insilmaril@2
  2625
			actionEditToggleHideExport->setOn (bo->hideInExport() );	
insilmaril@0
  2626
insilmaril@0
  2627
			actionEditCopy->setEnabled (true);	
insilmaril@0
  2628
			actionEditCut->setEnabled (true);	
insilmaril@2
  2629
			if (!clipboardEmpty)
insilmaril@2
  2630
				actionEditPaste->setEnabled (true);	
insilmaril@2
  2631
			else	
insilmaril@2
  2632
				actionEditPaste->setEnabled (false);	
insilmaril@2
  2633
			for (a=actionListBranches.first();a;a=actionListBranches.next())
insilmaril@2
  2634
				a->setEnabled(true);
insilmaril@0
  2635
			actionEditDelete->setEnabled (true);
insilmaril@0
  2636
			switch (selection->getFrameType())
insilmaril@0
  2637
			{
insilmaril@0
  2638
				case NoFrame: 
insilmaril@0
  2639
					actionFormatFrameNone->setOn(true);
insilmaril@0
  2640
					break;
insilmaril@0
  2641
				case Rectangle:
insilmaril@0
  2642
					actionFormatFrameRectangle->setOn(true);
insilmaril@0
  2643
					break;
insilmaril@0
  2644
				default:
insilmaril@0
  2645
					break;
insilmaril@0
  2646
			}	
insilmaril@2
  2647
			actionFormatIncludeImagesVer->setOn
insilmaril@2
  2648
				( ((BranchObj*)selection)->getIncludeImagesVer());
insilmaril@2
  2649
			actionFormatIncludeImagesHor->setOn
insilmaril@2
  2650
				( ((BranchObj*)selection)->getIncludeImagesHor());
insilmaril@2
  2651
			actionFormatHideLinkUnselected->setOn
insilmaril@2
  2652
				(selection->getHideLinkUnselected());
insilmaril@0
  2653
		}
insilmaril@0
  2654
		if ( (typeid(*selection) == typeid(FloatImageObj)) )
insilmaril@0
  2655
		{
insilmaril@2
  2656
			FloatObj *fo=(FloatImageObj*)selection;
insilmaril@3
  2657
//FIXME do this in mainwindow					standardFlagsDefault->setEnabled (false);
insilmaril@0
  2658
insilmaril@0
  2659
			actionEditOpenURL->setEnabled (false);
insilmaril@0
  2660
			actionEditOpenVymLink->setEnabled (false);
insilmaril@0
  2661
			actionEditDeleteVymLink->setEnabled (false);	
insilmaril@2
  2662
			actionEditToggleHideExport->setEnabled (true);	
insilmaril@2
  2663
			actionEditToggleHideExport->setOn (fo->hideInExport() );	
insilmaril@2
  2664
insilmaril@0
  2665
insilmaril@0
  2666
			actionEditCopy->setEnabled (true);
insilmaril@0
  2667
			actionEditCut->setEnabled (true);	
insilmaril@2
  2668
			actionEditPaste->setEnabled (false);
insilmaril@2
  2669
			for (a=actionListBranches.first();a;a=actionListBranches.next())
insilmaril@2
  2670
				a->setEnabled(false);
insilmaril@0
  2671
			actionEditDelete->setEnabled (true);
insilmaril@2
  2672
			actionFormatHideLinkUnselected->setOn
insilmaril@2
  2673
				( selection->getHideLinkUnselected());
insilmaril@2
  2674
			actionEditMoveUp->setEnabled (false);
insilmaril@2
  2675
			actionEditMoveDown->setEnabled (false);
insilmaril@0
  2676
		}
insilmaril@0
  2677
insilmaril@0
  2678
	} else
insilmaril@0
  2679
	{
insilmaril@3
  2680
//FIXME do this in mainwindow				standardFlagsDefault->setEnabled (false);
insilmaril@0
  2681
insilmaril@0
  2682
		actionEditCopy->setEnabled (false);	
insilmaril@0
  2683
		actionEditCut->setEnabled (false);	
insilmaril@0
  2684
		actionEditPaste->setEnabled (false);	
insilmaril@2
  2685
		for (a=actionListBranches.first();a;a=actionListBranches.next())
insilmaril@2
  2686
			a->setEnabled(false);
insilmaril@2
  2687
insilmaril@2
  2688
		actionEditToggleScroll->setEnabled (false);
insilmaril@0
  2689
		actionEditOpenURL->setEnabled (false);
insilmaril@0
  2690
		actionEditOpenVymLink->setEnabled (false);
insilmaril@0
  2691
		actionEditDeleteVymLink->setEnabled (false);	
insilmaril@0
  2692
		actionEditHeading2URL->setEnabled (false);	
insilmaril@0
  2693
		actionEditDelete->setEnabled (false);
insilmaril@2
  2694
		actionEditMoveUp->setEnabled (false);
insilmaril@2
  2695
		actionEditMoveDown->setEnabled (false);
insilmaril@2
  2696
		actionEditToggleHideExport->setEnabled (false);	
insilmaril@0
  2697
	}	
insilmaril@0
  2698
}
insilmaril@0
  2699
insilmaril@2
  2700
void MapEditor::updateNoteFlag()
insilmaril@2
  2701
{
insilmaril@2
  2702
	if (selection)
insilmaril@2
  2703
		if ( (typeid(*selection) == typeid(BranchObj)) || 
insilmaril@2
  2704
			(typeid(*selection) == typeid(MapCenterObj))  )
insilmaril@2
  2705
			((BranchObj*)selection)->updateNoteFlag();
insilmaril@2
  2706
}
insilmaril@2
  2707
insilmaril@0
  2708
void MapEditor::setLinkStyle (LinkStyle ls)
insilmaril@0
  2709
{
insilmaril@0
  2710
	linkstyle=ls;
insilmaril@0
  2711
insilmaril@2
  2712
	saveState("Set link style");	// TODO undoCommand
insilmaril@0
  2713
	BranchObj *bo;
insilmaril@0
  2714
	bo=mapCenter->first();
insilmaril@0
  2715
	bo=bo->next();
insilmaril@0
  2716
	while (bo) 
insilmaril@0
  2717
	{
insilmaril@0
  2718
		bo->setLinkStyle(bo->getDefLinkStyle());
insilmaril@0
  2719
		bo=bo->next();
insilmaril@0
  2720
	}
insilmaril@2
  2721
	mapCenter->reposition();
insilmaril@0
  2722
}
insilmaril@0
  2723
insilmaril@0
  2724
LinkStyle MapEditor::getLinkStyle ()
insilmaril@0
  2725
{
insilmaril@0
  2726
	return linkstyle;
insilmaril@0
  2727
}	
insilmaril@0
  2728
insilmaril@0
  2729
void MapEditor::setLinkColor(QColor c)
insilmaril@0
  2730
{
insilmaril@2
  2731
	defLinkColor=c;
insilmaril@0
  2732
	updateActions();
insilmaril@0
  2733
}
insilmaril@0
  2734
insilmaril@0
  2735
void MapEditor::setLinkColorHint()
insilmaril@0
  2736
{
insilmaril@0
  2737
	// called from setLinkColorHint(lch) or at end of parse
insilmaril@0
  2738
	BranchObj *bo;
insilmaril@0
  2739
	bo=mapCenter->first();
insilmaril@0
  2740
	while (bo) 
insilmaril@0
  2741
	{
insilmaril@0
  2742
		bo->setLinkColor();
insilmaril@0
  2743
		bo=bo->next();
insilmaril@0
  2744
	}
insilmaril@0
  2745
}
insilmaril@0
  2746
insilmaril@0
  2747
void MapEditor::setLinkColorHint(LinkColorHint lch)
insilmaril@0
  2748
{
insilmaril@0
  2749
	linkcolorhint=lch;
insilmaril@0
  2750
	setLinkColorHint();
insilmaril@0
  2751
}
insilmaril@0
  2752
insilmaril@0
  2753
void MapEditor::toggleLinkColorHint()
insilmaril@0
  2754
{
insilmaril@0
  2755
	if (linkcolorhint==HeadingColor)
insilmaril@0
  2756
		linkcolorhint=DefaultColor;
insilmaril@0
  2757
	else	
insilmaril@0
  2758
		linkcolorhint=HeadingColor;
insilmaril@0
  2759
	BranchObj *bo;
insilmaril@0
  2760
	bo=mapCenter->first();
insilmaril@0
  2761
	while (bo) 
insilmaril@0
  2762
	{
insilmaril@0
  2763
		bo->setLinkColor();
insilmaril@0
  2764
		bo=bo->next();
insilmaril@0
  2765
	}
insilmaril@0
  2766
}
insilmaril@0
  2767
insilmaril@0
  2768
LinkColorHint MapEditor::getLinkColorHint()
insilmaril@0
  2769
{
insilmaril@0
  2770
	return linkcolorhint;
insilmaril@0
  2771
}
insilmaril@0
  2772
insilmaril@0
  2773
QColor MapEditor::getDefLinkColor()
insilmaril@0
  2774
{
insilmaril@2
  2775
	return defLinkColor;
insilmaril@2
  2776
}
insilmaril@2
  2777
insilmaril@2
  2778
void MapEditor::setDefXLinkColor(QColor col)
insilmaril@2
  2779
{
insilmaril@2
  2780
	defXLinkColor=col;
insilmaril@2
  2781
}
insilmaril@2
  2782
insilmaril@2
  2783
QColor MapEditor::getDefXLinkColor()
insilmaril@2
  2784
{
insilmaril@2
  2785
	return defXLinkColor;
insilmaril@2
  2786
}
insilmaril@2
  2787
insilmaril@2
  2788
void MapEditor::setDefXLinkWidth (int w)
insilmaril@2
  2789
{
insilmaril@2
  2790
	defXLinkWidth=w;
insilmaril@2
  2791
}
insilmaril@2
  2792
insilmaril@2
  2793
int MapEditor::getDefXLinkWidth()
insilmaril@2
  2794
{
insilmaril@2
  2795
	return defXLinkWidth;
insilmaril@0
  2796
}
insilmaril@0
  2797
insilmaril@0
  2798
void MapEditor::selectLinkColor()
insilmaril@0
  2799
{
insilmaril@0
  2800
	// Finish open lineEdits
insilmaril@2
  2801
	if (lineedit) finishedLineEdit();
insilmaril@2
  2802
insilmaril@2
  2803
	QColor col = QColorDialog::getColor( defLinkColor, this );
insilmaril@0
  2804
	if ( !col.isValid() ) return;
insilmaril@0
  2805
	setLinkColor( col );
insilmaril@2
  2806
	saveState(QString("Set link color to %1").arg(col.name()));	//TODO undoCommand
insilmaril@2
  2807
insilmaril@0
  2808
}
insilmaril@0
  2809
insilmaril@0
  2810
void MapEditor::toggleScroll()
insilmaril@0
  2811
{
insilmaril@0
  2812
	if (selection && (typeid(*selection) == typeid(BranchObj)) )
insilmaril@0
  2813
	{
insilmaril@2
  2814
		BranchObj *bo=((BranchObj*)selection);
insilmaril@0
  2815
		if (bo->countBranches()==0) return;
insilmaril@0
  2816
		if (bo->getDepth()==0) return;
insilmaril@2
  2817
		QString s;
insilmaril@2
  2818
		if (bo->isScrolled())
insilmaril@2
  2819
			s="Unscroll";
insilmaril@2
  2820
		else	
insilmaril@2
  2821
			s="Scroll";
insilmaril@2
  2822
		saveState(selection, QString ("%1 %2").arg(s).arg(getName(bo)));
insilmaril@0
  2823
		bo->toggleScroll();
insilmaril@0
  2824
		adjustCanvasSize();
insilmaril@0
  2825
		canvas()->update();
insilmaril@0
  2826
	}
insilmaril@0
  2827
}
insilmaril@0
  2828
insilmaril@0
  2829
void MapEditor::unScrollAll()
insilmaril@0
  2830
{
insilmaril@0
  2831
	BranchObj *bo;
insilmaril@0
  2832
	bo=mapCenter->first();
insilmaril@0
  2833
	while (bo) 
insilmaril@0
  2834
	{
insilmaril@0
  2835
		if (bo->isScrolled()) bo->toggleScroll();
insilmaril@0
  2836
		bo=bo->next();
insilmaril@0
  2837
	}
insilmaril@0
  2838
}
insilmaril@0
  2839
insilmaril@0
  2840
void MapEditor::loadFloatImage ()
insilmaril@0
  2841
{
insilmaril@0
  2842
	if (selection && 
insilmaril@0
  2843
		(typeid(*selection) == typeid(BranchObj)) || 
insilmaril@0
  2844
		(typeid(*selection) == typeid(MapCenterObj))  )
insilmaril@0
  2845
	{
insilmaril@2
  2846
		BranchObj *bo=((BranchObj*)selection);
insilmaril@2
  2847
insilmaril@2
  2848
		Q3FileDialog *fd=new Q3FileDialog( this);
insilmaril@2
  2849
		fd->setMode (Q3FileDialog::ExistingFiles);
insilmaril@0
  2850
		fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
insilmaril@0
  2851
		ImagePreview *p =new ImagePreview (fd);
insilmaril@0
  2852
		fd->setContentsPreviewEnabled( TRUE );
insilmaril@0
  2853
		fd->setContentsPreview( p, p );
insilmaril@2
  2854
		fd->setPreviewMode( Q3FileDialog::Contents );
insilmaril@2
  2855
		fd->setCaption(__VYM " - " +tr("Load image"));
insilmaril@0
  2856
		fd->setDir (lastImageDir);
insilmaril@0
  2857
		fd->show();
insilmaril@0
  2858
insilmaril@0
  2859
		QString fn;
insilmaril@0
  2860
		if ( fd->exec() == QDialog::Accepted )
insilmaril@0
  2861
		{
insilmaril@2
  2862
			saveState(selection, QString("Add floatimage to %1").arg(getName(selection)));
insilmaril@4
  2863
			// FIXME in QT4 use:	lastImageDir=fd->directory();
insilmaril@4
  2864
			lastImageDir=QDir (fd->dirPath());
insilmaril@2
  2865
			QStringList flist = fd->selectedFiles();
insilmaril@2
  2866
			QStringList::Iterator it = flist.begin();
insilmaril@2
  2867
			while( it != flist.end() ) 
insilmaril@2
  2868
			{
insilmaril@2
  2869
				fn = *it;
insilmaril@2
  2870
				bo->addFloatImage();
insilmaril@2
  2871
				// TODO check if load was successful
insilmaril@2
  2872
				bo->getLastFloatImage()->load(*it);
insilmaril@2
  2873
				bo->getLastFloatImage()->setOriginalFilename(fn);
insilmaril@2
  2874
				++it;
insilmaril@2
  2875
			}
insilmaril@2
  2876
insilmaril@0
  2877
			mapCenter->reposition();
insilmaril@0
  2878
			adjustCanvasSize();
insilmaril@0
  2879
			canvas()->update();
insilmaril@0
  2880
		}
insilmaril@2
  2881
		delete (p);
insilmaril@2
  2882
		delete (fd);
insilmaril@0
  2883
	}
insilmaril@0
  2884
}
insilmaril@0
  2885
insilmaril@0
  2886
void MapEditor::saveFloatImage (int item)
insilmaril@0
  2887
{
insilmaril@0
  2888
	if (selection && 
insilmaril@0
  2889
		(typeid(*selection) == typeid(FloatImageObj)) )
insilmaril@0
  2890
	{
insilmaril@2
  2891
		FloatImageObj *fio=((FloatImageObj*)selection);
insilmaril@0
  2892
		const char* fmt = saveImageFormatMenu->text(item);
insilmaril@0
  2893
insilmaril@2
  2894
		Q3FileDialog *fd=new Q3FileDialog( this, tr("vym - save image as") + fmt);
insilmaril@0
  2895
		fd->addFilter ("PNG (*.png)");
insilmaril@0
  2896
		fd->addFilter ("BMP (*.bmp)");
insilmaril@0
  2897
		fd->addFilter ("XBM (*.xbm)");
insilmaril@0
  2898
		fd->addFilter ("JPG (*.jpg)");
insilmaril@0
  2899
		fd->addFilter ("XPM (*.xpm)");
insilmaril@0
  2900
		fd->addFilter ("GIF (*.gif)");
insilmaril@0
  2901
		fd->addFilter ("PNM (*.pnm)");
insilmaril@0
  2902
		fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
insilmaril@2
  2903
		fd->setCaption(__VYM " - " +tr("Save image as %1").arg(fmt));
insilmaril@2
  2904
		fd->setMode( Q3FileDialog::AnyFile );
insilmaril@0
  2905
		fd->setSelection (fio->getOriginalFilename());
insilmaril@0
  2906
		fd->show();
insilmaril@0
  2907
insilmaril@0
  2908
		QString fn;
insilmaril@0
  2909
		if ( fd->exec() == QDialog::Accepted )
insilmaril@0
  2910
		{
insilmaril@0
  2911
			if (QFile (fd->selectedFile()).exists() )
insilmaril@0
  2912
			{
insilmaril@2
  2913
				QMessageBox mb( __VYM,
insilmaril@2
  2914
					tr("The file %1 exists already.\n"
insilmaril@2
  2915
					"Do you want to overwrite it?").arg(fd->selectedFile()),
insilmaril@0
  2916
				QMessageBox::Warning,
insilmaril@0
  2917
				QMessageBox::Yes | QMessageBox::Default,
insilmaril@0
  2918
				QMessageBox::Cancel | QMessageBox::Escape,
insilmaril@0
  2919
				QMessageBox::QMessageBox::NoButton );
insilmaril@0
  2920
insilmaril@0
  2921
				mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
insilmaril@0
  2922
				mb.setButtonText( QMessageBox::No, tr("Cancel"));
insilmaril@0
  2923
				switch( mb.exec() ) 
insilmaril@0
  2924
				{
insilmaril@0
  2925
					case QMessageBox::Yes:
insilmaril@0
  2926
						// save 
insilmaril@0
  2927
						break;;
insilmaril@0
  2928
					case QMessageBox::Cancel:
insilmaril@0
  2929
						// do nothing
insilmaril@0
  2930
						return;
insilmaril@0
  2931
						break;
insilmaril@0
  2932
				}
insilmaril@0
  2933
			}
insilmaril@0
  2934
			fio->save (fd->selectedFile(),fmt);
insilmaril@0
  2935
		}
insilmaril@0
  2936
	}
insilmaril@0
  2937
}
insilmaril@0
  2938
insilmaril@0
  2939
void MapEditor::setFrame(const FrameType &t)
insilmaril@0
  2940
{
insilmaril@0
  2941
	if (selection && 
insilmaril@0
  2942
		(typeid(*selection) == typeid(BranchObj)) || 
insilmaril@0
  2943
		(typeid(*selection) == typeid(MapCenterObj))  )
insilmaril@0
  2944
	{
insilmaril@0
  2945
		selection->setFrameType (t);
insilmaril@0
  2946
		mapCenter->reposition();
insilmaril@0
  2947
		selection->updateLink();
insilmaril@0
  2948
	}
insilmaril@0
  2949
}
insilmaril@0
  2950
insilmaril@2
  2951
void MapEditor::setIncludeImagesVer(bool b)
insilmaril@2
  2952
{
insilmaril@2
  2953
	if (selection && 
insilmaril@2
  2954
		(typeid(*selection) == typeid(BranchObj)) || 
insilmaril@2
  2955
		(typeid(*selection) == typeid(MapCenterObj))  )
insilmaril@2
  2956
		((BranchObj*)selection)->setIncludeImagesVer(b);
insilmaril@2
  2957
		mapCenter->reposition();
insilmaril@2
  2958
}
insilmaril@2
  2959
insilmaril@2
  2960
void MapEditor::setIncludeImagesHor(bool b)
insilmaril@2
  2961
{
insilmaril@2
  2962
	if (selection && 
insilmaril@2
  2963
		(typeid(*selection) == typeid(BranchObj)) || 
insilmaril@2
  2964
		(typeid(*selection) == typeid(MapCenterObj))  )
insilmaril@2
  2965
		((BranchObj*)selection)->setIncludeImagesHor(b);
insilmaril@2
  2966
		mapCenter->reposition();
insilmaril@2
  2967
}
insilmaril@2
  2968
insilmaril@2
  2969
void MapEditor::setHideLinkUnselected (bool b)
insilmaril@2
  2970
{
insilmaril@2
  2971
	if (selection && 
insilmaril@2
  2972
		(typeid(*selection) == typeid(BranchObj)) || 
insilmaril@2
  2973
		(typeid(*selection) == typeid(MapCenterObj))  ||
insilmaril@2
  2974
		(typeid(*selection) == typeid(FloatImageObj)) )
insilmaril@2
  2975
		selection->setHideLinkUnselected(b);
insilmaril@2
  2976
}
insilmaril@2
  2977
insilmaril@0
  2978
void MapEditor::importDir(BranchObj *dst, QDir d)
insilmaril@0
  2979
{
insilmaril@0
  2980
	if (selection && 
insilmaril@0
  2981
		(typeid(*selection) == typeid(BranchObj)) || 
insilmaril@0
  2982
		(typeid(*selection) == typeid(MapCenterObj))  )
insilmaril@0
  2983
	{
insilmaril@0
  2984
		BranchObj *bo;
insilmaril@0
  2985
		
insilmaril@0
  2986
		// Traverse directories
insilmaril@0
  2987
		d.setFilter( QDir::Dirs| QDir::Hidden | QDir::NoSymLinks );
insilmaril@2
  2988
		QFileInfoList list = d.entryInfoList();
insilmaril@2
  2989
		QFileInfo fi;
insilmaril@2
  2990
insilmaril@2
  2991
		for (int i = 0; i < list.size(); ++i) 
insilmaril@0
  2992
		{
insilmaril@2
  2993
			fi=list.at(i);
insilmaril@2
  2994
			if (fi.fileName() != "." && fi.fileName() != ".." )
insilmaril@0
  2995
			{
insilmaril@0
  2996
				dst->addBranch();
insilmaril@0
  2997
				bo=dst->getLastBranch();
insilmaril@2
  2998
				bo->setHeading (fi.fileName() );
insilmaril@2
  2999
				bo->setColor (QColor("blue"));
insilmaril@0
  3000
				bo->toggleScroll();
insilmaril@2
  3001
				if ( !d.cd(fi.fileName()) ) 
insilmaril@2
  3002
					QMessageBox::critical (0,tr("Critical Import Error"),tr("Cannot find the directory %1").arg(fi.fileName()));
insilmaril@0
  3003
				else 
insilmaril@0
  3004
				{
insilmaril@2
  3005
					// Recursively add subdirs
insilmaril@0
  3006
					importDir (bo,d);
insilmaril@0
  3007
					d.cdUp();
insilmaril@0
  3008
				}
insilmaril@0
  3009
			}	
insilmaril@0
  3010
		}		
insilmaril@0
  3011
		// Traverse files
insilmaril@0
  3012
		d.setFilter( QDir::Files| QDir::Hidden | QDir::NoSymLinks );
insilmaril@2
  3013
		list = d.entryInfoList();
insilmaril@2
  3014
insilmaril@2
  3015
		for (int i = 0; i < list.size(); ++i) 
insilmaril@0
  3016
		{
insilmaril@2
  3017
			fi=list.at(i);
insilmaril@0
  3018
			dst->addBranch();
insilmaril@0
  3019
			bo=dst->getLastBranch();
insilmaril@2
  3020
			bo->setHeading (fi.fileName() );
insilmaril@2
  3021
			bo->setColor (QColor("black"));
insilmaril@2
  3022