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