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