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