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