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