mapeditor.cpp
author insilmaril
Thu, 14 Sep 2006 11:38:18 +0000
changeset 388 3a58c9ef4a18
parent 381 c79df732d095
child 390 0e1aeb21cb78
permissions -rw-r--r--
1.8.57 - more changes in history window, Note Editor is QT4 now
insilmaril@0
     1
#include "mapeditor.h"
insilmaril@0
     2
insilmaril@374
     3
#include <QActionGroup>
insilmaril@366
     4
#include <QColor>
insilmaril@366
     5
#include <QColorDialog>
insilmaril@374
     6
#include <QContextMenuEvent>
insilmaril@374
     7
#include <QDir>
insilmaril@374
     8
#include <QDragEnterEvent>
insilmaril@374
     9
#include <QDropEvent>
insilmaril@374
    10
#include <QFile>
insilmaril@374
    11
#include <QInputDialog>
insilmaril@374
    12
#include <QMessageBox>
insilmaril@374
    13
#include <QMouseEvent>
insilmaril@374
    14
#include <QPainter>
insilmaril@374
    15
#include <QPixmap>
insilmaril@374
    16
#include <QPrinter>
insilmaril@374
    17
#include <QResizeEvent>
insilmaril@374
    18
#include <QTextStream>
insilmaril@374
    19
insilmaril@374
    20
#include <Q3PtrList>
insilmaril@366
    21
#include <q3dragobject.h>
insilmaril@366
    22
#include <q3urloperator.h>
insilmaril@366
    23
#include <q3networkprotocol.h>
insilmaril@374
    24
#include <q3paintdevicemetrics.h>
insilmaril@374
    25
#include <q3filedialog.h>
insilmaril@0
    26
insilmaril@0
    27
#include <iostream>
insilmaril@95
    28
#include <cstdlib>
insilmaril@0
    29
#include <typeinfo>
insilmaril@0
    30
insilmaril@0
    31
#include "version.h"
insilmaril@0
    32
insilmaril@105
    33
#include "api.h"
insilmaril@175
    34
#include "editxlinkdialog.h"
insilmaril@175
    35
#include "exports.h"
insilmaril@175
    36
#include "extrainfodialog.h"
insilmaril@236
    37
#include "file.h"
insilmaril@175
    38
#include "linkablemapobj.h"
insilmaril@175
    39
#include "mainwindow.h"
insilmaril@175
    40
#include "misc.h"
insilmaril@175
    41
#include "texteditor.h"
insilmaril@314
    42
#include "warningdialog.h"
insilmaril@0
    43
#include "xml.h"
insilmaril@0
    44
insilmaril@0
    45
insilmaril@0
    46
extern TextEditor *textEditor;
insilmaril@0
    47
extern int statusbarTime;
insilmaril@0
    48
extern Main *mainWindow;
insilmaril@164
    49
extern QString tmpVymDir;
insilmaril@166
    50
extern QString clipboardDir;
insilmaril@166
    51
extern bool clipboardEmpty;
insilmaril@0
    52
extern FlagRowObj *systemFlagsDefault;
insilmaril@0
    53
extern FlagRowObj *standardFlagsDefault;
insilmaril@0
    54
insilmaril@366
    55
extern Q3PtrList <QAction> actionListBranches;
insilmaril@98
    56
insilmaril@0
    57
extern QAction *actionFileSave;
insilmaril@0
    58
extern QAction *actionEditUndo;
insilmaril@366
    59
extern QAction *actionEditRedo;
insilmaril@0
    60
extern QAction *actionEditCopy;
insilmaril@0
    61
extern QAction *actionEditCut;
insilmaril@0
    62
extern QAction *actionEditPaste;
insilmaril@0
    63
extern QAction *actionEditMoveUp;
insilmaril@0
    64
extern QAction *actionEditMoveDown;
insilmaril@0
    65
extern QAction *actionEditToggleScroll;
insilmaril@0
    66
extern QAction *actionEditOpenURL;
insilmaril@266
    67
extern QAction *actionEditOpenURLTab;
insilmaril@0
    68
extern QAction *actionEditURL;
insilmaril@0
    69
extern QAction *actionEditHeading2URL;
insilmaril@0
    70
extern QAction *actionEditBugzilla2URL;
insilmaril@175
    71
extern QAction *actionEditFATE2URL;
insilmaril@0
    72
extern QAction *actionEditOpenVymLink;
insilmaril@0
    73
extern QAction *actionEditVymLink;
insilmaril@0
    74
extern QAction *actionEditDeleteVymLink;
insilmaril@243
    75
extern QAction *actionEditToggleHideExport;
insilmaril@0
    76
extern QAction *actionEditHeading;
insilmaril@0
    77
extern QAction *actionEditDelete;
insilmaril@0
    78
extern QAction *actionEditAddBranch;
insilmaril@0
    79
extern QAction *actionEditAddBranchAbove;
insilmaril@0
    80
extern QAction *actionEditAddBranchBelow;
insilmaril@98
    81
extern QAction *actionEditRemoveBranchHere;
insilmaril@98
    82
extern QAction *actionEditRemoveChilds;
insilmaril@0
    83
extern QAction *actionEditImportAdd;
insilmaril@0
    84
extern QAction *actionEditImportReplace;
insilmaril@0
    85
extern QAction *actionEditSaveBranch;
insilmaril@0
    86
extern QAction *actionEditSelectFirst;
insilmaril@0
    87
extern QAction *actionEditSelectLast;
insilmaril@0
    88
extern QAction *actionEditLoadImage;
insilmaril@0
    89
insilmaril@0
    90
extern QAction* actionFormatPickColor;
insilmaril@0
    91
extern QAction* actionFormatColorBranch;
insilmaril@0
    92
extern QAction* actionFormatColorSubtree;
insilmaril@0
    93
extern QAction *actionFormatLinkColorHint;
insilmaril@0
    94
extern QAction *actionFormatBackColor;
insilmaril@0
    95
extern QAction *actionFormatLinkColor;
insilmaril@0
    96
insilmaril@89
    97
extern QActionGroup* actionGroupModModes;
insilmaril@89
    98
extern QAction* actionModModeColor;
insilmaril@89
    99
extern QAction* actionModModeLink;
insilmaril@93
   100
extern QAction* actionModModeCopy;
insilmaril@89
   101
insilmaril@0
   102
extern QActionGroup *actionGroupFormatFrameTypes;
insilmaril@0
   103
extern QAction *actionFormatFrameNone;
insilmaril@0
   104
extern QAction *actionFormatFrameRectangle;
insilmaril@0
   105
insilmaril@0
   106
extern QActionGroup *actionGroupFormatLinkStyles;
insilmaril@175
   107
extern QAction *actionFormatIncludeImagesVer;
insilmaril@175
   108
extern QAction *actionFormatIncludeImagesHor;
insilmaril@160
   109
extern QAction *actionFormatHideLinkUnselected;
insilmaril@0
   110
extern QAction *actionFormatLinkStyleLine;
insilmaril@0
   111
extern QAction *actionFormatLinkStyleParabel;
insilmaril@0
   112
extern QAction *actionFormatLinkStylePolyLine;
insilmaril@0
   113
extern QAction *actionFormatLinkStylePolyParabel;
insilmaril@0
   114
insilmaril@0
   115
extern QAction *actionViewToggleNoteEditor;
insilmaril@0
   116
insilmaril@0
   117
extern QAction *actionSettingsAutoedit;
insilmaril@0
   118
extern QAction *actionSettingsAutoselectHeading;
insilmaril@0
   119
extern QAction *actionSettingsAutoselectText;
insilmaril@103
   120
extern QAction *actionSettingsUseFlagGroups;
insilmaril@0
   121
insilmaril@366
   122
extern QMenu* branchContextMenu;
insilmaril@366
   123
extern QMenu* branchAddContextMenu;
insilmaril@366
   124
extern QMenu* branchRemoveContextMenu;
insilmaril@366
   125
extern QMenu* branchLinksContextMenu;
insilmaril@366
   126
extern QMenu* branchXLinksContextMenuEdit;
insilmaril@366
   127
extern QMenu* branchXLinksContextMenuFollow;
insilmaril@366
   128
extern QMenu* floatimageContextMenu;
insilmaril@366
   129
extern QMenu* saveImageFormatMenu;
insilmaril@366
   130
extern QMenu* canvasContextMenu;
insilmaril@366
   131
extern QMenu* lastMapsMenu;
insilmaril@366
   132
extern QMenu* importMenu;
insilmaril@366
   133
extern QMenu* exportMenu;
insilmaril@366
   134
insilmaril@0
   135
insilmaril@0
   136
extern Settings settings;
insilmaril@366
   137
extern ImageIO imageIO;
insilmaril@0
   138
insilmaril@269
   139
extern QString iconPath;
insilmaril@301
   140
extern QDir vymBaseDir;
insilmaril@366
   141
extern QDir lastImageDir;
insilmaril@269
   142
insilmaril@164
   143
int MapEditor::mapNum=0;	// make instance
insilmaril@0
   144
insilmaril@0
   145
///////////////////////////////////////////////////////////////////////
insilmaril@0
   146
///////////////////////////////////////////////////////////////////////
insilmaril@0
   147
MapEditor::MapEditor(
insilmaril@366
   148
	QWidget* parent, bool interactive, const char* name, Qt::WFlags f) :
insilmaril@366
   149
  Q3CanvasView(parent,name,f), urlOperator(0), imageBuffer(0)
insilmaril@0
   150
{
insilmaril@0
   151
	//cout << "Constructor ME "<<this<<endl;
insilmaril@164
   152
	mapNum++;
insilmaril@0
   153
insilmaril@119
   154
    viewport()->setAcceptDrops(true);
insilmaril@119
   155
insilmaril@366
   156
    mapCanvas = new Q3Canvas(width(),height());
insilmaril@0
   157
	mapCanvas->setAdvancePeriod(30);
insilmaril@366
   158
	mapCanvas->setBackgroundColor (Qt::white);
insilmaril@0
   159
insilmaril@0
   160
    setCanvas (mapCanvas);
insilmaril@0
   161
	
insilmaril@160
   162
	// Always show scroll bars (automatic would flicker sometimes)
insilmaril@366
   163
	setVScrollBarMode ( Q3ScrollView::AlwaysOn );
insilmaril@366
   164
	setHScrollBarMode ( Q3ScrollView::AlwaysOn );
insilmaril@0
   165
insilmaril@0
   166
    mapCenter = new MapCenterObj(mapCanvas);
insilmaril@0
   167
    mapCenter->setVisibility (true);
insilmaril@0
   168
	mapCenter->setMapEditor (this);
insilmaril@93
   169
	mapCenter->setHeading (tr("New Map","Heading of mapcenter in new map"));
insilmaril@175
   170
	mapCenter->move(mapCanvas->width()/2-mapCenter->width()/2,mapCanvas->height()/2-mapCenter->height()/2);
insilmaril@0
   171
insilmaril@0
   172
    printer=NULL;
insilmaril@0
   173
insilmaril@366
   174
    actColor=Qt::black; setColor (actColor);
insilmaril@97
   175
	defLinkColor=QColor (0,0,255);
insilmaril@97
   176
	defXLinkColor=QColor (180,180,180);
insilmaril@0
   177
	linkcolorhint=DefaultColor;
insilmaril@0
   178
	linkstyle=StylePolyParabel;
insilmaril@0
   179
insilmaril@366
   180
	// Create bitmap cursors, platform dependant
insilmaril@0
   181
	#if defined(Q_OS_MACX)
insilmaril@366
   182
		handOpenCursor=QCursor ( QPixmap(iconPath+"cursorhandopen16.png"),1,1 );		
insilmaril@269
   183
		pickColorCursor=QCursor ( QPixmap (iconPath+"cursorcolorpicker16.png"), 1,15 ); 
insilmaril@0
   184
	#else
insilmaril@366
   185
		handOpenCursor=QCursor (QPixmap(iconPath+"cursorhandopen.png"),1,1);		
insilmaril@269
   186
		pickColorCursor=QCursor ( QPixmap(iconPath+"cursorcolorpicker.png"), 5,27 ); 
insilmaril@0
   187
	#endif
insilmaril@0
   188
insilmaril@0
   189
	pickingColor=false;
insilmaril@91
   190
	drawingLink=false;
insilmaril@93
   191
	copyingObj=false;
insilmaril@0
   192
insilmaril@0
   193
    editingBO=NULL;
insilmaril@0
   194
    selection=NULL;
insilmaril@0
   195
    selectionLast=NULL;
insilmaril@0
   196
    movingObj=NULL;
insilmaril@0
   197
insilmaril@366
   198
	xelection.setMapCenter (mapCenter);
insilmaril@366
   199
insilmaril@102
   200
	defXLinkWidth=1;
insilmaril@102
   201
	defXLinkColor=QColor (230,230,230);
insilmaril@102
   202
insilmaril@0
   203
    mapChanged=false;
insilmaril@0
   204
	mapDefault=true;
insilmaril@0
   205
	mapUnsaved=false;
insilmaril@0
   206
	
insilmaril@0
   207
	zipped=true;
insilmaril@0
   208
	filePath="";
insilmaril@294
   209
	fileName=tr("unnamed");
insilmaril@0
   210
	mapName="";
insilmaril@0
   211
insilmaril@388
   212
	stepsTotal=settings.readNumEntry("/mapeditor/stepsTotal",50);
insilmaril@381
   213
	undoSet.setEntry ("/history/stepsTotal",QString::number(stepsTotal));
insilmaril@163
   214
	
insilmaril@0
   215
	// Initialize find routine
insilmaril@0
   216
	itFind=NULL;				
insilmaril@0
   217
	EOFind=false;
insilmaril@0
   218
insilmaril@0
   219
	printFrame=true;
insilmaril@0
   220
	printFooter=true;
insilmaril@0
   221
insilmaril@139
   222
	blockReposition=false;
insilmaril@139
   223
	blockSaveState=false;
insilmaril@260
   224
insilmaril@260
   225
	hidemode=HideNone;
insilmaril@260
   226
insilmaril@377
   227
	isInteractive=interactive;	// FIXME still needed?
insilmaril@377
   228
insilmaril@377
   229
	// Create temporary files
insilmaril@377
   230
	makeTmpDirs();
insilmaril@0
   231
insilmaril@0
   232
	// Initially set movingCentre
insilmaril@0
   233
	updateViewCenter();
insilmaril@0
   234
insilmaril@0
   235
	mapCenter->reposition();	//	for positioning heading
insilmaril@381
   236
insilmaril@381
   237
	// Initialize history window;
insilmaril@381
   238
	historyWindow.setME(this);
insilmaril@381
   239
	historyWindow.setStepsTotal(stepsTotal);
insilmaril@381
   240
	historyWindow.update (undoSet);
insilmaril@0
   241
}
insilmaril@0
   242
insilmaril@0
   243
MapEditor::~MapEditor()
insilmaril@0
   244
{
insilmaril@119
   245
  if (imageBuffer) delete imageBuffer;
insilmaril@119
   246
  if (urlOperator) {
insilmaril@119
   247
    urlOperator->stop();
insilmaril@119
   248
    delete urlOperator;
insilmaril@119
   249
  }
insilmaril@119
   250
insilmaril@0
   251
	//cout <<"Destructor MapEditor\n";
insilmaril@0
   252
insilmaril@0
   253
	// Save Settings
insilmaril@366
   254
	//settings.writeEntry( "/mapeditor/editmode/autoselect",  );
insilmaril@0
   255
insilmaril@0
   256
}
insilmaril@0
   257
insilmaril@0
   258
QColor MapEditor::color()
insilmaril@0
   259
{
insilmaril@0
   260
    return actColor;
insilmaril@0
   261
}
insilmaril@0
   262
insilmaril@0
   263
QColor MapEditor::backgroundColor()
insilmaril@0
   264
{
insilmaril@0
   265
    return mapCanvas->backgroundColor();
insilmaril@0
   266
}
insilmaril@0
   267
insilmaril@0
   268
MapCenterObj* MapEditor::getMapCenter()
insilmaril@0
   269
{
insilmaril@0
   270
    return mapCenter;
insilmaril@0
   271
}
insilmaril@0
   272
insilmaril@366
   273
Q3Canvas* MapEditor::getCanvas()
insilmaril@0
   274
{
insilmaril@0
   275
    return mapCanvas;
insilmaril@0
   276
}
insilmaril@0
   277
insilmaril@0
   278
void MapEditor::adjustCanvasSize()
insilmaril@0
   279
{
insilmaril@0
   280
	// To adjust the canvas to map, viewport size and position, we have to
insilmaril@0
   281
	// do some coordinate magic...
insilmaril@0
   282
	//
insilmaril@0
   283
	// Get rectangle of (scroll-)view. 
insilmaril@0
   284
	// We want to be in canvas coords, so
insilmaril@0
   285
	// we map. Important if view is zoomed...
insilmaril@0
   286
	QRect view = inverseWorldMatrix().mapRect( QRect( contentsX(), contentsY(),
insilmaril@0
   287
												visibleWidth(), visibleHeight()) );	
insilmaril@0
   288
												
insilmaril@0
   289
	// Now we need the bounding box of view AND map to calc the correct canvas size.
insilmaril@0
   290
	// Why? Because if the map itself is moved out of view, the view has to be enlarged
insilmaril@0
   291
	// to avoid jumping aroung...
insilmaril@0
   292
	QRect map=mapCenter->getTotalBBox();
insilmaril@0
   293
insilmaril@0
   294
	// right edge - left edge
insilmaril@0
   295
	int cw= max(map.x() + map.width(),  view.x() + view.width())  - min(map.x(), view.x());
insilmaril@0
   296
	int ch= max(map.y() + map.height(), view.y() + view.height()) - min(map.y(), view.y());
insilmaril@0
   297
insilmaril@0
   298
insilmaril@0
   299
	if ( (cw!=mapCanvas->width()) || (ch!=mapCanvas->height()) ||
insilmaril@0
   300
		!mapCanvas->onCanvas (map.topLeft()) || !mapCanvas->onCanvas (map.bottomRight())
insilmaril@0
   301
	)	
insilmaril@0
   302
	{	
insilmaril@0
   303
		// move the map on canvas (in order to not move it on screen) this is neccessary
insilmaril@0
   304
		// a) if topleft corner of canvas is left or above topleft corner of view and also left of
insilmaril@0
   305
		//    above topleft corner of map. E.g. if map is completly inside view, but it would be possible 
insilmaril@0
   306
		//    to scroll to an empty area of canvas to the left.
insilmaril@0
   307
		// b) if topleft corner of map left of or above topleft of canvas
insilmaril@0
   308
		int dx=0;
insilmaril@0
   309
		int dy=0;
insilmaril@0
   310
insilmaril@0
   311
		if (cw > mapCanvas->width() )
insilmaril@0
   312
		{
insilmaril@0
   313
			if (map.x()<0) dx=-map.x();	
insilmaril@0
   314
		}
insilmaril@0
   315
		if (cw <  mapCanvas->width() )
insilmaril@0
   316
			dx=-min (view.x(),map.x());
insilmaril@0
   317
		if (ch > mapCanvas->height() )
insilmaril@0
   318
		{
insilmaril@0
   319
			if (map.y()<0) dy=-map.y();	
insilmaril@0
   320
		}
insilmaril@0
   321
		if (ch <  mapCanvas->height() )
insilmaril@0
   322
		{
insilmaril@0
   323
			dy=-min (view.y(),map.y());
insilmaril@0
   324
		}
insilmaril@0
   325
		// We really have to resize now. Let's go...
insilmaril@0
   326
		mapCanvas->resize (cw,ch);
insilmaril@0
   327
		if ( (dx!=0) || (dy!=0) ) 
insilmaril@0
   328
		{
insilmaril@0
   329
			mapCenter->moveAllBy(dx,dy);
insilmaril@0
   330
			mapCenter->reposition();
insilmaril@336
   331
//			mapCenter->positionBBox();	// To move float
insilmaril@0
   332
insilmaril@0
   333
			// scroll the view (in order to not move map on screen)
insilmaril@0
   334
			scrollBy (dx,dy);
insilmaril@0
   335
		}	
insilmaril@0
   336
	}
insilmaril@0
   337
}
insilmaril@0
   338
insilmaril@139
   339
bool MapEditor::isRepositionBlocked()
insilmaril@0
   340
{
insilmaril@139
   341
	return blockReposition;
insilmaril@0
   342
}
insilmaril@0
   343
insilmaril@294
   344
QString MapEditor::getName (LinkableMapObj *lmo)
insilmaril@294
   345
{
insilmaril@314
   346
	QString s;
insilmaril@314
   347
	if (!lmo) return QString("Error: NULL has no name!");
insilmaril@294
   348
insilmaril@294
   349
	if ((typeid(*lmo) == typeid(BranchObj) ||
insilmaril@294
   350
				      typeid(*lmo) == typeid(MapCenterObj))) 
insilmaril@314
   351
	{
insilmaril@314
   352
		
insilmaril@314
   353
		s=(((BranchObj*)lmo)->getHeading());
insilmaril@314
   354
		if (s=="") s="unnamed";
insilmaril@377
   355
		return QString("branch (%1)").arg(s);
insilmaril@377
   356
		//return QString("branch (<font color=\"#0000ff\">%1</font>)").arg(s);
insilmaril@314
   357
	}	
insilmaril@294
   358
	if ((typeid(*lmo) == typeid(FloatImageObj) ))
insilmaril@377
   359
		return QString ("floatimage [%1]").arg(((FloatImageObj*)lmo)->getOriginalFilename());
insilmaril@377
   360
		//return QString ("floatimage [<font color=\"#0000ff\">%1</font>]").arg(((FloatImageObj*)lmo)->getOriginalFilename());
insilmaril@294
   361
	return QString("Unknown type has no name!");
insilmaril@294
   362
}
insilmaril@294
   363
insilmaril@0
   364
void MapEditor::makeTmpDirs()
insilmaril@0
   365
{
insilmaril@0
   366
	// Create unique temporary directories
insilmaril@366
   367
	tmpMapDir=QDir::convertSeparators (tmpVymDir+QString("/mapeditor-%1").arg(mapNum));
insilmaril@366
   368
	histPath=QDir::convertSeparators (tmpMapDir+"/history");
insilmaril@164
   369
	QDir d;
insilmaril@377
   370
	d.mkdir (tmpMapDir);
insilmaril@0
   371
}
insilmaril@0
   372
insilmaril@366
   373
QString MapEditor::saveToDir(const QString &tmpdir, const QString &prefix, bool writeflags, const QPoint &offset, LinkableMapObj *saveSel)
insilmaril@0
   374
{
insilmaril@163
   375
	// tmpdir		temporary directory to which data will be written
insilmaril@0
   376
	// prefix		mapname, which will be appended to images etc.
insilmaril@0
   377
	// writeflags	Only write flags for "real" save of map, not undo
insilmaril@0
   378
	// offset		offset of bbox of whole map in canvas. 
insilmaril@0
   379
	//				Needed for XML export
insilmaril@0
   380
	
insilmaril@0
   381
	// Save Header
insilmaril@0
   382
	QString ls;
insilmaril@0
   383
	switch (linkstyle)
insilmaril@0
   384
	{
insilmaril@0
   385
		case StyleLine: 
insilmaril@0
   386
			ls="StyleLine";
insilmaril@0
   387
			break;
insilmaril@0
   388
		case StyleParabel:
insilmaril@0
   389
			ls="StyleParabel";
insilmaril@0
   390
			break;
insilmaril@0
   391
		case StylePolyLine:	
insilmaril@0
   392
			ls="StylePolyLine";
insilmaril@0
   393
			break;
insilmaril@0
   394
		default:
insilmaril@0
   395
			ls="StylePolyParabel";
insilmaril@0
   396
			break;
insilmaril@0
   397
	}	
insilmaril@0
   398
insilmaril@0
   399
	QString s="<?xml version=\"1.0\" encoding=\"utf-8\"?><!DOCTYPE vymmap>\n";
insilmaril@0
   400
	QString colhint="";
insilmaril@0
   401
	if (linkcolorhint==HeadingColor) 
insilmaril@0
   402
		colhint=attribut("linkColorHint","HeadingColor");
insilmaril@0
   403
insilmaril@185
   404
	QString mapAttr=attribut("version",__VYM_VERSION);
insilmaril@366
   405
	if (!saveSel)
insilmaril@0
   406
		mapAttr+= attribut("author",mapCenter->getAuthor()) +
insilmaril@0
   407
				  attribut("comment",mapCenter->getComment()) +
insilmaril@0
   408
			      attribut("date",mapCenter->getDate()) +
insilmaril@0
   409
		          attribut("backgroundColor", mapCanvas->backgroundColor().name() ) +
insilmaril@0
   410
		          attribut("linkStyle", ls ) +
insilmaril@97
   411
		          attribut("linkColor", defLinkColor.name() ) +
insilmaril@102
   412
		          attribut("defXLinkColor", defXLinkColor.name() ) +
insilmaril@102
   413
		          attribut("defXLinkWidth", QString().setNum(defXLinkWidth,10) ) +
insilmaril@0
   414
		          colhint; 
insilmaril@0
   415
	s+=beginElement("vymmap",mapAttr);
insilmaril@0
   416
	incIndent();
insilmaril@0
   417
insilmaril@0
   418
	// Find the used flags while traversing the tree
insilmaril@0
   419
	standardFlagsDefault->resetUsedCounter();
insilmaril@0
   420
	
insilmaril@166
   421
	// Reset the counters before saving
insilmaril@166
   422
	FloatImageObj (mapCanvas).resetSaveCounter();
insilmaril@166
   423
insilmaril@0
   424
	// Build xml recursivly
insilmaril@366
   425
	if (!saveSel)
insilmaril@366
   426
		// Save complete map, if saveSel not set
insilmaril@0
   427
		s+=mapCenter->saveToDir(tmpdir,prefix,writeflags,offset);
insilmaril@0
   428
	else
insilmaril@0
   429
	{
insilmaril@366
   430
		if ( typeid(*saveSel) == typeid(BranchObj) )
insilmaril@366
   431
			// Save Subtree
insilmaril@366
   432
			s+=((BranchObj*)(saveSel))->saveToDir(tmpdir,prefix,offset);
insilmaril@366
   433
		else if ( typeid(*saveSel) == typeid(FloatImageObj) )
insilmaril@366
   434
			// Save image
insilmaril@377
   435
			s+=((FloatImageObj*)(saveSel))->saveToDir(tmpdir,prefix);
insilmaril@166
   436
			
insilmaril@166
   437
		else if (selection && typeid(*selection)==typeid(BranchObj))
insilmaril@366
   438
			// Save selected branch is saved from mainwindow		//FIXME maybe use "subtree" above?
insilmaril@366
   439
			s+=((BranchObj*)selection)->saveToDir(tmpdir,prefix,offset);
insilmaril@0
   440
	}
insilmaril@0
   441
insilmaril@0
   442
	// Save local settings
insilmaril@0
   443
	s+=settings.getXMLData (destPath);
insilmaril@0
   444
insilmaril@0
   445
	// Save selection
insilmaril@366
   446
	if (selection && !saveSel ) 
insilmaril@0
   447
		s+=valueElement("select",selection->getSelectString());
insilmaril@0
   448
insilmaril@0
   449
	decIndent();
insilmaril@0
   450
	s+=endElement("vymmap");
insilmaril@0
   451
insilmaril@0
   452
	if (writeflags)
insilmaril@0
   453
		standardFlagsDefault->saveToDir (tmpdir+"/flags/","",writeflags);
insilmaril@0
   454
	return s;
insilmaril@0
   455
}
insilmaril@0
   456
insilmaril@366
   457
void MapEditor::saveStateComplete(const QString &comment)
insilmaril@0
   458
{
insilmaril@366
   459
	// Save complete map, Undo will replace whole map
insilmaril@366
   460
	saveState (CompleteMap,
insilmaril@366
   461
		"", "",
insilmaril@366
   462
		"", "", 
insilmaril@366
   463
		comment, 
insilmaril@366
   464
		mapCenter);
insilmaril@0
   465
}
insilmaril@0
   466
insilmaril@366
   467
void MapEditor::saveStatePart(LinkableMapObj *undoSel, const QString &comment)
insilmaril@0
   468
{
insilmaril@366
   469
	// save the selected part of the map, Undo will replace part of map 
insilmaril@366
   470
	QString undoSelection="";
insilmaril@366
   471
	if (undoSel) undoSelection=undoSel->getSelectString();
insilmaril@366
   472
insilmaril@366
   473
	saveState (PartOfMap,
insilmaril@366
   474
		undoSelection, "",
insilmaril@366
   475
		"", "", 
insilmaril@366
   476
		comment, 
insilmaril@366
   477
		undoSel);
insilmaril@105
   478
}
insilmaril@105
   479
insilmaril@366
   480
void MapEditor::saveStateConstSelection(const QString &uc, const QString &rc, const QString &comment)
insilmaril@105
   481
{
insilmaril@163
   482
	// selection does not change during action,
insilmaril@163
   483
	// so just save commands for undo and redo
insilmaril@366
   484
	// and use current selection
insilmaril@366
   485
insilmaril@366
   486
	QString sel;
insilmaril@366
   487
	if (selection) sel=selection->getSelectString();
insilmaril@366
   488
insilmaril@366
   489
	saveState (UndoCommand,
insilmaril@366
   490
		sel, uc,
insilmaril@366
   491
		sel, rc, 
insilmaril@366
   492
		comment, 
insilmaril@366
   493
		NULL);
insilmaril@106
   494
}
insilmaril@106
   495
insilmaril@366
   496
void MapEditor::saveStateComData(LinkableMapObj *undoSel, const QString &uc, LinkableMapObj *redoSel, const QString &rc, const QString &comment, LinkableMapObj *saveSel) 
insilmaril@106
   497
{
insilmaril@366
   498
	QString redoSelection="";
insilmaril@366
   499
	if (redoSel) redoSelection=redoSel->getSelectString();
insilmaril@366
   500
	QString undoSelection="";
insilmaril@366
   501
	if (undoSel) undoSelection=undoSel->getSelectString();
insilmaril@366
   502
insilmaril@366
   503
	saveState (UndoCommand,
insilmaril@366
   504
		undoSelection, uc,
insilmaril@366
   505
		redoSelection, "FIXME-redoCom",	//FIXME
insilmaril@366
   506
		comment, 
insilmaril@366
   507
		saveSel);
insilmaril@163
   508
}
insilmaril@163
   509
insilmaril@366
   510
void MapEditor::saveState(LinkableMapObj *undoSel, const QString &uc, LinkableMapObj *redoSel, const QString &rc, const QString &comment) 
insilmaril@366
   511
{
insilmaril@366
   512
	// "Normal" savestate: save commands, selections and comment
insilmaril@366
   513
	// so just save commands for undo and redo
insilmaril@366
   514
	// and use current selection
insilmaril@366
   515
insilmaril@366
   516
	QString redoSelection="";
insilmaril@366
   517
	if (redoSel) redoSelection=redoSel->getSelectString();
insilmaril@366
   518
	QString undoSelection="";
insilmaril@366
   519
	if (undoSel) undoSelection=undoSel->getSelectString();
insilmaril@366
   520
insilmaril@366
   521
	saveState (UndoCommand,
insilmaril@366
   522
		undoSelection, uc,
insilmaril@366
   523
		redoSelection, rc, 
insilmaril@366
   524
		comment, 
insilmaril@366
   525
		NULL);
insilmaril@366
   526
}
insilmaril@366
   527
insilmaril@366
   528
void MapEditor::saveState(const QString &undoSel, const QString &uc, const QString &redoSel, const QString &rc, const QString &comment) 
insilmaril@366
   529
{
insilmaril@366
   530
	// "Normal" savestate: save commands, selections and comment
insilmaril@366
   531
	// so just save commands for undo and redo
insilmaril@366
   532
	// and use current selection
insilmaril@366
   533
	saveState (UndoCommand,
insilmaril@366
   534
		undoSel, uc,
insilmaril@366
   535
		redoSel, rc, 
insilmaril@366
   536
		comment, 
insilmaril@366
   537
		NULL);
insilmaril@366
   538
}
insilmaril@366
   539
insilmaril@366
   540
		
insilmaril@366
   541
void MapEditor::saveState(const SaveMode &savemode, const QString &undoSelection, const QString &undoCom, const QString &redoSelection, const QString &redoCom, const QString &comment, LinkableMapObj *saveSel)
insilmaril@163
   542
{
insilmaril@163
   543
	// Main saveState
insilmaril@163
   544
insilmaril@139
   545
	if (blockSaveState) return;
insilmaril@139
   546
insilmaril@163
   547
	/* TODO remove after testing
insilmaril@163
   548
	*/
insilmaril@388
   549
	cout << "ME::saveState()  "<<endl;
insilmaril@294
   550
	
insilmaril@131
   551
	setChanged();
insilmaril@136
   552
insilmaril@366
   553
	int undosAvail=undoSet.readNumEntry ("/history/undosAvail",0);
insilmaril@381
   554
	int redosAvail=undoSet.readNumEntry ("/history/redosAvail",0);
insilmaril@366
   555
	int curStep=undoSet.readNumEntry ("/history/curStep",0);
insilmaril@163
   556
	// Find out current undo directory
insilmaril@381
   557
	if (undosAvail<stepsTotal) undosAvail++;
insilmaril@366
   558
	curStep++;
insilmaril@381
   559
	if (curStep>stepsTotal) curStep=1;
insilmaril@163
   560
	
insilmaril@163
   561
	QString backupXML;
insilmaril@388
   562
	QString bakMapName=QDir::convertSeparators (QString("history-%1").arg(curStep));
insilmaril@388
   563
	QString bakMapDir=QDir::convertSeparators (tmpMapDir +"/"+bakMapName);
insilmaril@164
   564
	QString bakMapPath=QDir::convertSeparators(bakMapDir+"/map.xml");
insilmaril@164
   565
insilmaril@164
   566
	// Create bakMapDir if not available
insilmaril@164
   567
	QDir d(bakMapDir);
insilmaril@164
   568
	if (!d.exists()) 
insilmaril@164
   569
		makeSubDirs (bakMapDir);
insilmaril@163
   570
insilmaril@366
   571
	// Save depending on how much needs to be saved	
insilmaril@366
   572
	if (!saveSel)
insilmaril@366
   573
		backupXML="";
insilmaril@366
   574
	else 
insilmaril@366
   575
		backupXML=saveToDir (bakMapDir,mapName+"-",false, QPoint (),saveSel);
insilmaril@136
   576
		
insilmaril@163
   577
	QString undoCommand="";
insilmaril@106
   578
	if (savemode==UndoCommand)
insilmaril@0
   579
	{
insilmaril@105
   580
		undoCommand=undoCom;
insilmaril@106
   581
	}	
insilmaril@366
   582
	else if (savemode==PartOfMap )
insilmaril@0
   583
	{
insilmaril@163
   584
		undoCommand="undoPart (\""+ undoSelection+"\",\""+bakMapPath+"\")";
insilmaril@106
   585
	} else
insilmaril@106
   586
	{
insilmaril@163
   587
		undoCommand="undoMap (\""+bakMapPath+"\")";
insilmaril@106
   588
	}
insilmaril@163
   589
	if (!backupXML.isEmpty())
insilmaril@163
   590
		// Write XML Data to disk
insilmaril@163
   591
		saveStringToDisk (QString(bakMapPath),backupXML);
insilmaril@163
   592
insilmaril@388
   593
	// We would have to save all actions in a tree, to keep track of 
insilmaril@388
   594
	// possible redos after a action. Too complex, forget about redos.
insilmaril@388
   595
	redosAvail=0;
insilmaril@388
   596
insilmaril@388
   597
	// Write the current state to disk
insilmaril@366
   598
	undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
insilmaril@388
   599
	undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
insilmaril@366
   600
	undoSet.setEntry ("/history/curStep",QString::number(curStep));
insilmaril@366
   601
	undoSet.setEntry (QString("/history/step-%1/undoCommand").arg(curStep),undoCommand);
insilmaril@366
   602
	undoSet.setEntry (QString("/history/step-%1/undoSelection").arg(curStep),undoSelection);
insilmaril@366
   603
	undoSet.setEntry (QString("/history/step-%1/redoCommand").arg(curStep),redoCom);
insilmaril@366
   604
	undoSet.setEntry (QString("/history/step-%1/redoSelection").arg(curStep),redoSelection);
insilmaril@366
   605
	undoSet.setEntry (QString("/history/step-%1/comment").arg(curStep),comment);
insilmaril@366
   606
	undoSet.writeSettings(histPath);
insilmaril@163
   607
insilmaril@163
   608
	/* TODO remove after testing
insilmaril@366
   609
	*/
insilmaril@366
   610
	//cout << "          into="<< histPath.toStdString()<<endl;
insilmaril@381
   611
	cout << "    stepsTotal="<<stepsTotal<<
insilmaril@366
   612
	", undosAvail="<<undosAvail<<
insilmaril@381
   613
	", redosAvail="<<redosAvail<<
insilmaril@366
   614
	", curStep="<<curStep<<endl;
insilmaril@163
   615
	cout << "    ---------------------------"<<endl;
insilmaril@366
   616
	cout << "    comment="<<comment.toStdString()<<endl;
insilmaril@366
   617
	cout << "    undoCom="<<undoCommand.toStdString()<<endl;
insilmaril@366
   618
	cout << "    undoSel="<<undoSelection.toStdString()<<endl;
insilmaril@366
   619
	cout << "    redoCom="<<redoCom.toStdString()<<endl;
insilmaril@366
   620
	cout << "    redoSel="<<redoSelection.toStdString()<<endl;
insilmaril@294
   621
	cout << "    ---------------------------"<<endl;
insilmaril@377
   622
insilmaril@377
   623
	historyWindow.update (undoSet);
insilmaril@106
   624
}
insilmaril@106
   625
insilmaril@129
   626
void MapEditor::parseAtom(const QString &atom)
insilmaril@106
   627
{
insilmaril@106
   628
	API api;
insilmaril@163
   629
	QString s,t;
insilmaril@129
   630
	int x,y;
insilmaril@129
   631
	bool ok;
insilmaril@129
   632
insilmaril@129
   633
	// Split string s into command and parameters
insilmaril@129
   634
	api.parseCommand (atom);
insilmaril@129
   635
	QString com=api.command();
insilmaril@129
   636
	
insilmaril@106
   637
	// External commands
insilmaril@163
   638
	if (com=="moveBranchUp")
insilmaril@106
   639
		moveBranchUp();
insilmaril@129
   640
	else if (com=="moveBranchDown")
insilmaril@106
   641
		moveBranchDown();
insilmaril@129
   642
	else if (com=="move")
insilmaril@129
   643
	{
insilmaril@139
   644
		if (api.checkParamCount(2) && selection )
insilmaril@129
   645
		{	
insilmaril@129
   646
			x=api.parInt (ok,0);
insilmaril@129
   647
			if (ok)
insilmaril@129
   648
			{
insilmaril@129
   649
				y=api.parInt (ok,1);
insilmaril@130
   650
				if (ok) move (x,y);
insilmaril@129
   651
			}
insilmaril@129
   652
		}	
insilmaril@129
   653
	}
insilmaril@366
   654
	else if (com=="moveRel")
insilmaril@366
   655
	{
insilmaril@366
   656
		if (api.checkParamCount(2) && selection )
insilmaril@366
   657
		{	
insilmaril@366
   658
			x=api.parInt (ok,0);
insilmaril@366
   659
			if (ok)
insilmaril@366
   660
			{
insilmaril@366
   661
				y=api.parInt (ok,1);
insilmaril@366
   662
				if (ok) moveRel (x,y);
insilmaril@366
   663
			}
insilmaril@366
   664
		}	
insilmaril@366
   665
	}
insilmaril@366
   666
	else if (com=="delete")
insilmaril@366
   667
	{
insilmaril@366
   668
		if (api.checkParamCount(1) && selection )
insilmaril@366
   669
		{	
insilmaril@366
   670
			s=api.parString(ok,0);
insilmaril@366
   671
			if (ok &&select (s)) deleteSelection();
insilmaril@366
   672
		}	
insilmaril@366
   673
	}	
insilmaril@366
   674
	else if (com=="addBranch")
insilmaril@366
   675
	{
insilmaril@366
   676
		if (api.checkParamCount(1) && selection )
insilmaril@366
   677
		{	
insilmaril@366
   678
			y=api.parInt (ok,0);
insilmaril@366
   679
			if (ok ) addNewBranchInt (y);
insilmaril@366
   680
		}	
insilmaril@366
   681
	}	
insilmaril@129
   682
	else if (com=="linkBranchToPos")
insilmaril@129
   683
	{
insilmaril@129
   684
		if (selection && typeid(*selection) == typeid(BranchObj) ) 
insilmaril@129
   685
		{
insilmaril@129
   686
			if (api.checkParamCount(4))
insilmaril@129
   687
			{
insilmaril@366
   688
				// 0	selectstring of parent
insilmaril@366
   689
				// 1	num in parent (for branches)
insilmaril@366
   690
				// 2,3	x,y of mainbranch or mapcenter
insilmaril@129
   691
				s=api.parString(ok,0);
insilmaril@129
   692
				LinkableMapObj *dst=mapCenter->findObjBySelect (s);
insilmaril@129
   693
				if (dst)
insilmaril@129
   694
				{	
insilmaril@129
   695
					if (typeid(*dst) == typeid(BranchObj) ) 
insilmaril@139
   696
					{
insilmaril@139
   697
						// Get number in parent
insilmaril@139
   698
						x=api.parInt (ok,1);
insilmaril@139
   699
						if (ok)
insilmaril@163
   700
							((BranchObj*)selection)->moveBranchTo ((BranchObj*)(dst),x);
insilmaril@139
   701
					} else if (typeid(*dst) == typeid(MapCenterObj) ) 
insilmaril@129
   702
					{
insilmaril@163
   703
						((BranchObj*)selection)->moveBranchTo ((BranchObj*)(dst),-1);
insilmaril@139
   704
						// Get coordinates of mainbranch
insilmaril@129
   705
						x=api.parInt (ok,2);
insilmaril@129
   706
						if (ok)
insilmaril@129
   707
						{
insilmaril@129
   708
							y=api.parInt (ok,3);
insilmaril@163
   709
							if (ok) ((BranchObj*)selection)->move (x,y);
insilmaril@129
   710
						}
insilmaril@129
   711
					}	
insilmaril@129
   712
				}	
insilmaril@129
   713
			}
insilmaril@129
   714
		}
insilmaril@129
   715
	} else if (com=="setHeading")
insilmaril@129
   716
	{
insilmaril@129
   717
		if (api.checkParamCount(1))
insilmaril@129
   718
		{
insilmaril@129
   719
			s=api.parString (ok,0);
insilmaril@366
   720
			if (ok) 
insilmaril@366
   721
				setHeading (s);
insilmaril@129
   722
		}	
insilmaril@143
   723
	} else if (com=="setURL")
insilmaril@143
   724
	{
insilmaril@143
   725
		if (api.checkParamCount(1))
insilmaril@143
   726
		{
insilmaril@143
   727
			s=api.parString (ok,0);
insilmaril@366
   728
			if (ok) setURLInt(s);
insilmaril@143
   729
		}	
insilmaril@143
   730
	} else if (com=="setVymLink")
insilmaril@143
   731
	{
insilmaril@143
   732
		if (api.checkParamCount(1))
insilmaril@143
   733
		{
insilmaril@143
   734
			s=api.parString (ok,0);
insilmaril@366
   735
			if (ok) setVymLinkInt(s);
insilmaril@143
   736
		}	
insilmaril@143
   737
	}
insilmaril@366
   738
	else if (com=="setFlag")
insilmaril@366
   739
	{
insilmaril@366
   740
		if (selection && typeid(*selection) == typeid(BranchObj) ) 
insilmaril@366
   741
		{
insilmaril@366
   742
			if (api.checkParamCount(1) )
insilmaril@366
   743
			{	
insilmaril@366
   744
				s=api.parString(ok,0);
insilmaril@366
   745
				if (ok) 
insilmaril@366
   746
				{
insilmaril@366
   747
					BranchObj* bo=(BranchObj*)selection;
insilmaril@366
   748
					bo->activateStandardFlag(s);
insilmaril@366
   749
					bo->updateFlagsToolbar();
insilmaril@366
   750
				}	
insilmaril@366
   751
			}	
insilmaril@366
   752
		}
insilmaril@366
   753
	}	
insilmaril@366
   754
	else if (com=="unsetFlag")
insilmaril@366
   755
	{
insilmaril@366
   756
		if (selection && typeid(*selection) == typeid(BranchObj) ) 
insilmaril@366
   757
		{
insilmaril@366
   758
			if (api.checkParamCount(1) )
insilmaril@366
   759
			{	
insilmaril@366
   760
				s=api.parString(ok,0);
insilmaril@366
   761
				if (ok) 
insilmaril@366
   762
				{
insilmaril@366
   763
					BranchObj* bo=(BranchObj*)selection;
insilmaril@366
   764
					bo->deactivateStandardFlag(s);
insilmaril@366
   765
					bo->updateFlagsToolbar();
insilmaril@366
   766
				}	
insilmaril@366
   767
			}	
insilmaril@366
   768
		}
insilmaril@366
   769
	}	
insilmaril@106
   770
	// Internal commands, used for undo etc.	
insilmaril@129
   771
	else if (com==QString("undoMap"))
insilmaril@139
   772
	{
insilmaril@163
   773
		if (api.checkParamCount(1))
insilmaril@163
   774
			undoXML("",api.parString (ok,0));
insilmaril@139
   775
	} else if (com==QString("undoPart"))
insilmaril@129
   776
	{
insilmaril@163
   777
		if (api.checkParamCount(2))
insilmaril@129
   778
		{
insilmaril@366
   779
			s=api.parString (ok,0);	// undo selection
insilmaril@366
   780
			t=api.parString (ok,1);	// backup dir
insilmaril@388
   781
			if (QDir::isRelativePath(t)) t=QDir::convertSeparators (tmpMapDir + "/"+t);
insilmaril@388
   782
			cout <<t.ascii()<<endl;
insilmaril@163
   783
			undoXML(s,t);	
insilmaril@129
   784
		}
insilmaril@129
   785
	} else if (com=="select")
insilmaril@366
   786
	{
insilmaril@129
   787
		if (api.checkParamCount(1))
insilmaril@129
   788
		{
insilmaril@129
   789
			s=api.parString(ok,0);
insilmaril@129
   790
			if (ok) select (s);
insilmaril@129
   791
		}	
insilmaril@366
   792
	}	
insilmaril@106
   793
	else
insilmaril@163
   794
	{
insilmaril@129
   795
		api.setError ("Unknown command in: "+atom);
insilmaril@163
   796
	}
insilmaril@129
   797
insilmaril@129
   798
	// Any errors?
insilmaril@129
   799
	if (api.error())
insilmaril@106
   800
	{
insilmaril@366
   801
		// TODO Error handling
insilmaril@366
   802
		qWarning("MapEditor::parseAtom: Error!");
insilmaril@366
   803
		qWarning(api.errorDesc());
insilmaril@0
   804
	}	
insilmaril@0
   805
}
insilmaril@0
   806
insilmaril@294
   807
void MapEditor::toggleHistoryWindow()
insilmaril@294
   808
{
insilmaril@377
   809
	if (historyWindow.isVisible())
insilmaril@377
   810
		historyWindow.hide();
insilmaril@294
   811
	else	
insilmaril@377
   812
		historyWindow.show();
insilmaril@294
   813
}
insilmaril@294
   814
insilmaril@0
   815
insilmaril@0
   816
bool MapEditor::isDefault()
insilmaril@0
   817
{
insilmaril@0
   818
    return mapDefault;
insilmaril@0
   819
}
insilmaril@0
   820
insilmaril@0
   821
bool MapEditor::isUnsaved()
insilmaril@0
   822
{
insilmaril@0
   823
    return mapUnsaved;
insilmaril@0
   824
}
insilmaril@0
   825
insilmaril@0
   826
bool MapEditor::hasChanged()
insilmaril@0
   827
{
insilmaril@0
   828
    return mapChanged;
insilmaril@0
   829
}
insilmaril@0
   830
insilmaril@0
   831
void MapEditor::setChanged()
insilmaril@0
   832
{
insilmaril@0
   833
	mapChanged=true;
insilmaril@0
   834
	mapDefault=false;
insilmaril@0
   835
	mapUnsaved=true;
insilmaril@0
   836
	actionEditUndo->setEnabled (true);
insilmaril@0
   837
	actionFileSave->setEnabled (true);
insilmaril@0
   838
	findReset();
insilmaril@0
   839
}
insilmaril@0
   840
insilmaril@0
   841
void MapEditor::closeMap()
insilmaril@0
   842
{
insilmaril@0
   843
	// Unselect before disabling the toolbar actions
insilmaril@0
   844
	if (selection) selection->unselect();
insilmaril@0
   845
	selection=NULL;
insilmaril@0
   846
	updateActions();
insilmaril@0
   847
insilmaril@0
   848
    clear();
insilmaril@0
   849
	close();
insilmaril@0
   850
}
insilmaril@0
   851
insilmaril@0
   852
void MapEditor::setFilePath(QString fname)
insilmaril@0
   853
{
insilmaril@0
   854
	setFilePath (fname,fname);
insilmaril@0
   855
}
insilmaril@0
   856
insilmaril@0
   857
void MapEditor::setFilePath(QString fname, QString destname)
insilmaril@0
   858
{
insilmaril@205
   859
	if (fname.isEmpty() || fname=="")
insilmaril@106
   860
	{
insilmaril@106
   861
		filePath="";
insilmaril@106
   862
		fileName="";
insilmaril@106
   863
		destPath="";
insilmaril@106
   864
	} else
insilmaril@106
   865
	{
insilmaril@106
   866
		filePath=fname;		// becomes absolute path
insilmaril@106
   867
		fileName=fname;		// gets stripped of path
insilmaril@106
   868
		destPath=destname;	// needed for vymlinks
insilmaril@106
   869
insilmaril@106
   870
		// If fname is not an absolute path, complete it
insilmaril@106
   871
		filePath=QDir(fname).absPath();
insilmaril@106
   872
		fileDir=filePath.left (1+filePath.findRev ("/"));
insilmaril@106
   873
insilmaril@106
   874
		// Set short name, too. Search from behind:
insilmaril@106
   875
		int i=fileName.findRev("/");
insilmaril@106
   876
		if (i>=0) fileName=fileName.remove (0,i+1);
insilmaril@106
   877
insilmaril@106
   878
		// Forget the .vym (or .xml) for name of map
insilmaril@106
   879
		mapName=fileName.left(fileName.findRev(".",-1,true) );
insilmaril@294
   880
insilmaril@294
   881
		// Adjust history window
insilmaril@377
   882
		historyWindow.setCaption (__VYM " - " +tr("History for ")+fileName);
insilmaril@106
   883
	}
insilmaril@0
   884
}
insilmaril@0
   885
insilmaril@0
   886
QString MapEditor::getFilePath()
insilmaril@0
   887
{
insilmaril@0
   888
	return filePath;
insilmaril@0
   889
}
insilmaril@0
   890
insilmaril@0
   891
QString MapEditor::getFileName()
insilmaril@0
   892
{
insilmaril@0
   893
	return fileName;
insilmaril@0
   894
}
insilmaril@0
   895
insilmaril@0
   896
QString MapEditor::getMapName()
insilmaril@0
   897
{
insilmaril@0
   898
	return mapName;
insilmaril@0
   899
}
insilmaril@0
   900
insilmaril@0
   901
QString MapEditor::getDestPath()
insilmaril@0
   902
{
insilmaril@0
   903
	return destPath;
insilmaril@0
   904
}
insilmaril@0
   905
insilmaril@205
   906
ErrorCode MapEditor::load (QString fname, LoadMode lmode)
insilmaril@0
   907
{
insilmaril@205
   908
	ErrorCode err=success;
insilmaril@0
   909
insilmaril@0
   910
	if (lmode==NewMap)
insilmaril@0
   911
	{
insilmaril@0
   912
		if (selection) selection->unselect();
insilmaril@0
   913
		selection=NULL;
insilmaril@0
   914
		mapCenter->clear();
insilmaril@0
   915
		mapCenter->setMapEditor(this);
insilmaril@0
   916
		// (map state is set later at end of load...)
insilmaril@0
   917
	} else
insilmaril@0
   918
	{
insilmaril@366
   919
		saveStatePart(selection,"Load map");
insilmaril@0
   920
	}	
insilmaril@0
   921
	
insilmaril@0
   922
    
insilmaril@0
   923
    mapBuilderHandler handler;
insilmaril@0
   924
	QFile file( fname );
insilmaril@0
   925
insilmaril@0
   926
	// I am paranoid: file should exist anyway
insilmaril@0
   927
	// according to check in mainwindow.
insilmaril@0
   928
	if (!file.exists() )
insilmaril@0
   929
	{
insilmaril@0
   930
		QMessageBox::critical( 0, tr( "Critical Parse Error" ),
insilmaril@0
   931
				   tr("Couldn't open map " +fname)+".");
insilmaril@205
   932
		err=aborted;	
insilmaril@0
   933
	} else
insilmaril@0
   934
	{
insilmaril@139
   935
		blockReposition=true;
insilmaril@0
   936
		QXmlInputSource source( file);
insilmaril@0
   937
		QXmlSimpleReader reader;
insilmaril@0
   938
		reader.setContentHandler( &handler );
insilmaril@0
   939
		reader.setErrorHandler( &handler );
insilmaril@0
   940
		handler.setMapEditor( this );
insilmaril@0
   941
		handler.setTmpDir (filePath.left(filePath.findRev("/",-1)));	// needed to load files with rel. path
insilmaril@243
   942
		handler.setInputFile (file.name());
insilmaril@0
   943
		handler.setLoadMode (lmode);
insilmaril@163
   944
		blockSaveState=true;
insilmaril@0
   945
		bool ok = reader.parse( source );
insilmaril@139
   946
		blockReposition=false;
insilmaril@163
   947
		blockSaveState=false;
insilmaril@0
   948
		file.close();
insilmaril@0
   949
		if ( ok ) 
insilmaril@0
   950
		{
insilmaril@0
   951
			mapCenter->reposition();
insilmaril@0
   952
			adjustCanvasSize();
insilmaril@0
   953
			if (lmode==NewMap)
insilmaril@0
   954
			{
insilmaril@0
   955
				mapDefault=false;
insilmaril@0
   956
				mapChanged=false;
insilmaril@0
   957
				mapUnsaved=false;
insilmaril@0
   958
			}
insilmaril@0
   959
		} else 
insilmaril@0
   960
		{
insilmaril@0
   961
			QMessageBox::critical( 0, tr( "Critical Parse Error" ),
insilmaril@0
   962
					   tr( handler.errorProtocol() ) );
insilmaril@0
   963
			// returnCode=1;	
insilmaril@0
   964
			// Still return "success": the map maybe at least
insilmaril@0
   965
			// partially read by the parser
insilmaril@0
   966
		}	
insilmaril@0
   967
	}	
insilmaril@0
   968
	updateActions();
insilmaril@205
   969
	return err;
insilmaril@0
   970
}
insilmaril@0
   971
insilmaril@0
   972
int MapEditor::save (const SaveMode &savemode)
insilmaril@0
   973
{
insilmaril@0
   974
	int returnCode=0;
insilmaril@0
   975
insilmaril@106
   976
	// The SaveMode UndoCommand is not supported here
insilmaril@106
   977
	if (savemode==UndoCommand) return 1;
insilmaril@106
   978
insilmaril@0
   979
	// Create mapName and fileDir
insilmaril@0
   980
	makeSubDirs (fileDir);
insilmaril@0
   981
	QString fname;
insilmaril@0
   982
	if (saveZipped())
insilmaril@0
   983
		// save as .xml
insilmaril@0
   984
		fname=mapName+".xml";
insilmaril@0
   985
	else
insilmaril@0
   986
		// use name given by user, even if he chooses .doc
insilmaril@0
   987
		fname=fileName;
insilmaril@0
   988
insilmaril@0
   989
insilmaril@0
   990
	QString saveFile;
insilmaril@106
   991
	if (savemode==CompleteMap || selection==NULL)
insilmaril@106
   992
		saveFile=saveToDir (fileDir,mapName+"-",true,QPoint(),NULL);
insilmaril@106
   993
	else	
insilmaril@106
   994
		saveFile=saveToDir (fileDir,mapName+"-",true,QPoint(),selection);
insilmaril@0
   995
insilmaril@163
   996
	if (!saveStringToDisk(fileDir+fname,saveFile))
insilmaril@0
   997
		return 1;
insilmaril@0
   998
insilmaril@0
   999
	if (returnCode==0)
insilmaril@0
  1000
	{
insilmaril@0
  1001
		mapChanged=false;
insilmaril@0
  1002
		mapUnsaved=false;
insilmaril@0
  1003
		actionFileSave->setEnabled(false);
insilmaril@0
  1004
	}
insilmaril@0
  1005
insilmaril@0
  1006
	return returnCode;
insilmaril@0
  1007
}
insilmaril@0
  1008
insilmaril@0
  1009
void MapEditor::setZipped (bool z)
insilmaril@0
  1010
{
insilmaril@0
  1011
	zipped=z;
insilmaril@0
  1012
}
insilmaril@0
  1013
insilmaril@0
  1014
bool MapEditor::saveZipped ()
insilmaril@0
  1015
{
insilmaril@0
  1016
	return zipped;
insilmaril@0
  1017
}
insilmaril@0
  1018
insilmaril@0
  1019
void MapEditor::print()
insilmaril@0
  1020
{
insilmaril@0
  1021
	if ( !printer ) 
insilmaril@0
  1022
	{
insilmaril@0
  1023
		printer = new QPrinter;
insilmaril@0
  1024
		printer->setColorMode (QPrinter::Color);
insilmaril@366
  1025
		printer->setPrinterName (settings.value("/mainwindow/printerName",printer->printerName()).toString());
insilmaril@0
  1026
	}
insilmaril@0
  1027
insilmaril@0
  1028
	QRect totalBBox=mapCenter->getTotalBBox();
insilmaril@0
  1029
insilmaril@0
  1030
	// Try to set orientation automagically
insilmaril@0
  1031
	// Note: Interpretation of generated postscript is amibiguous, if 
insilmaril@0
  1032
	// there are problems with landscape mode, see
insilmaril@0
  1033
	// http://sdb.suse.de/de/sdb/html/jsmeix_print-cups-landscape-81.html
insilmaril@0
  1034
insilmaril@0
  1035
	if (totalBBox.width()>totalBBox.height())
insilmaril@0
  1036
		// recommend landscape
insilmaril@0
  1037
		printer->setOrientation (QPrinter::Landscape);
insilmaril@0
  1038
	else	
insilmaril@0
  1039
		// recommend portrait
insilmaril@0
  1040
		printer->setOrientation (QPrinter::Portrait);
insilmaril@0
  1041
insilmaril@0
  1042
	if ( printer->setup(this) ) 
insilmaril@0
  1043
	// returns false, if printing is canceled
insilmaril@0
  1044
	{
insilmaril@0
  1045
		QPainter pp(printer);
insilmaril@0
  1046
insilmaril@0
  1047
		// Don't print the visualisation of selection
insilmaril@0
  1048
		LinkableMapObj *oldselection=NULL;
insilmaril@0
  1049
		if (selection) 
insilmaril@0
  1050
		{
insilmaril@0
  1051
			oldselection=selection;
insilmaril@0
  1052
			selection->unselect();
insilmaril@0
  1053
		}
insilmaril@0
  1054
insilmaril@0
  1055
		// Handle sizes of map and paper:
insilmaril@0
  1056
		//
insilmaril@0
  1057
		// setWindow defines which part of the canvas will be transformed 
insilmaril@0
  1058
		// setViewport defines area on paper in device coordinates (dpi)
insilmaril@0
  1059
		// e.g. (0,50,700,700) is upper part on A4
insilmaril@0
  1060
		// see also /usr/lib/qt3/doc/html/coordsys.html
insilmaril@0
  1061
insilmaril@366
  1062
		Q3PaintDeviceMetrics metrics (printer);
insilmaril@0
  1063
insilmaril@0
  1064
		double paperAspect = (double)metrics.width()   / (double)metrics.height();
insilmaril@0
  1065
		double   mapAspect = (double)totalBBox.width() / (double)totalBBox.height();
insilmaril@0
  1066
insilmaril@102
  1067
		QRect mapRect=totalBBox;
insilmaril@366
  1068
		Q3CanvasRectangle *frame=NULL;
insilmaril@366
  1069
		Q3CanvasText *footerFN=NULL;
insilmaril@366
  1070
		Q3CanvasText *footerDate=NULL;
insilmaril@0
  1071
		if (printFrame || printFooter)
insilmaril@0
  1072
		{
insilmaril@0
  1073
			
insilmaril@0
  1074
			if (printFrame) 
insilmaril@0
  1075
			{
insilmaril@0
  1076
				// Print frame around map
insilmaril@102
  1077
				mapRect.setRect (totalBBox.x()-10, totalBBox.y()-10, 
insilmaril@102
  1078
					totalBBox.width()+20, totalBBox.height()+20);
insilmaril@366
  1079
				frame=new Q3CanvasRectangle (mapRect,mapCanvas);
insilmaril@366
  1080
				frame->setBrush (QColor(Qt::white));
insilmaril@366
  1081
				frame->setPen (QColor(Qt::black));
insilmaril@0
  1082
				frame->setZ(0);
insilmaril@0
  1083
				frame->show();    
insilmaril@0
  1084
			}		
insilmaril@163
  1085
			/* TODO remove after testing 
insilmaril@102
  1086
			QCanvasLine *l=new QCanvasLine (mapCanvas);
insilmaril@102
  1087
			l->setPoints (0,0,mapRect.width(),mapRect.height());
insilmaril@102
  1088
			l->setPen (QPen(QColor(black), 1));
insilmaril@102
  1089
			l->setZ (200);
insilmaril@102
  1090
			l->show();
insilmaril@131
  1091
			*/
insilmaril@102
  1092
insilmaril@0
  1093
			if (printFooter) 
insilmaril@0
  1094
			{
insilmaril@0
  1095
				// Print footer below map
insilmaril@0
  1096
				QFont font;		
insilmaril@0
  1097
				font.setPointSize(10);
insilmaril@366
  1098
				footerFN=new Q3CanvasText (mapCanvas);
insilmaril@0
  1099
				footerFN->setText ("VYM - " + fileName);
insilmaril@0
  1100
				footerFN->setFont(font);
insilmaril@0
  1101
				footerFN->move (mapRect.x(), mapRect.y() + mapRect.height() );
insilmaril@0
  1102
				footerFN->setZ(Z_TEXT);
insilmaril@0
  1103
				footerFN->show();    
insilmaril@366
  1104
				footerDate=new Q3CanvasText (mapCanvas);
insilmaril@0
  1105
				footerDate->setText (QDate::currentDate().toString(Qt::TextDate));
insilmaril@0
  1106
				footerDate->setFont(font);
insilmaril@0
  1107
				footerDate->move (mapRect.x()+mapRect.width()-footerDate->boundingRect().width(), mapRect.y() + mapRect.height() );
insilmaril@0
  1108
				footerDate->setZ(Z_TEXT);
insilmaril@0
  1109
				footerDate->show();    
insilmaril@0
  1110
			}
insilmaril@102
  1111
			pp.setWindow (mapRect.x(), mapRect.y(), mapRect.width(), mapRect.height()+20);
insilmaril@0
  1112
		}	else	
insilmaril@0
  1113
		{
insilmaril@0
  1114
			pp.setWindow (mapRect);
insilmaril@0
  1115
		}	
insilmaril@0
  1116
insilmaril@0
  1117
		if (mapAspect>=paperAspect)
insilmaril@0
  1118
		{
insilmaril@0
  1119
			// Fit horizontally to paper width
insilmaril@0
  1120
			pp.setViewport(0,0, metrics.width(),(int)(metrics.width()/mapAspect) );	
insilmaril@0
  1121
		}	else
insilmaril@0
  1122
		{
insilmaril@0
  1123
			// Fit vertically to paper height
insilmaril@0
  1124
			pp.setViewport(0,0,(int)(metrics.height()*mapAspect),metrics.height());	
insilmaril@0
  1125
		}	
insilmaril@0
  1126
insilmaril@0
  1127
		mapCanvas->drawArea(mapRect, &pp);	// draw Canvas to printer
insilmaril@0
  1128
insilmaril@0
  1129
		// Delete Frame and footer
insilmaril@0
  1130
		if (footerFN) 
insilmaril@0
  1131
		{
insilmaril@0
  1132
			delete (footerFN);
insilmaril@0
  1133
			delete (footerDate);
insilmaril@0
  1134
		}	
insilmaril@0
  1135
		if (frame)  delete (frame);
insilmaril@0
  1136
insilmaril@0
  1137
		// Restore selection
insilmaril@0
  1138
		if (oldselection) 
insilmaril@0
  1139
		{
insilmaril@0
  1140
			selection=oldselection;
insilmaril@0
  1141
			selection->select();
insilmaril@0
  1142
		}	
insilmaril@119
  1143
insilmaril@119
  1144
		// Save settings in vymrc
insilmaril@366
  1145
		settings.writeEntry("/mainwindow/printerName",printer->printerName());
insilmaril@0
  1146
	}
insilmaril@0
  1147
}
insilmaril@0
  1148
insilmaril@0
  1149
QPixmap MapEditor::getPixmap()
insilmaril@0
  1150
{
insilmaril@0
  1151
	QRect mapRect=mapCenter->getTotalBBox();
insilmaril@0
  1152
	QPixmap pix (mapRect.size());
insilmaril@0
  1153
	QPainter pp (&pix);
insilmaril@0
  1154
insilmaril@0
  1155
	// Don't print the visualisation of selection
insilmaril@0
  1156
	LinkableMapObj *oldselection=NULL;
insilmaril@0
  1157
	if (selection) 
insilmaril@0
  1158
	{
insilmaril@0
  1159
		oldselection=selection;
insilmaril@0
  1160
		selection->unselect();
insilmaril@0
  1161
	}
insilmaril@0
  1162
insilmaril@0
  1163
	pp.setWindow (mapRect);
insilmaril@0
  1164
insilmaril@0
  1165
	mapCanvas->drawArea(mapRect, &pp);	// draw Canvas to painter
insilmaril@0
  1166
insilmaril@0
  1167
insilmaril@0
  1168
	// Restore selection
insilmaril@0
  1169
	if (oldselection) 
insilmaril@0
  1170
	{
insilmaril@0
  1171
		selection=oldselection;
insilmaril@0
  1172
		selection->select();
insilmaril@0
  1173
	}	
insilmaril@0
  1174
	
insilmaril@0
  1175
	return pix;
insilmaril@0
  1176
}
insilmaril@0
  1177
insilmaril@260
  1178
void MapEditor::setHideTmpMode (HideTmpMode mode)
insilmaril@260
  1179
{
insilmaril@260
  1180
	hidemode=mode;
insilmaril@260
  1181
	mapCenter->setHideTmp (hidemode);
insilmaril@260
  1182
	mapCenter->reposition();
insilmaril@260
  1183
	adjustCanvasSize();
insilmaril@260
  1184
	canvas()->update();
insilmaril@260
  1185
}
insilmaril@260
  1186
insilmaril@260
  1187
HideTmpMode MapEditor::getHideTmpMode()
insilmaril@260
  1188
{
insilmaril@260
  1189
	return hidemode;
insilmaril@260
  1190
}
insilmaril@260
  1191
insilmaril@0
  1192
void MapEditor::exportImage(QString fn)
insilmaril@0
  1193
{
insilmaril@260
  1194
	setExportMode (true);
insilmaril@0
  1195
	QPixmap pix (getPixmap());
insilmaril@0
  1196
	pix.save(fn, "PNG");
insilmaril@260
  1197
	setExportMode (false);
insilmaril@260
  1198
}
insilmaril@260
  1199
insilmaril@260
  1200
void MapEditor::setExportMode (bool b)
insilmaril@260
  1201
{
insilmaril@260
  1202
	// should be called before and after exports
insilmaril@260
  1203
	// depending on the settings
insilmaril@366
  1204
	if (b && settings.value("/export/useHideExport","yes")=="yes")
insilmaril@260
  1205
		setHideTmpMode (HideExport);
insilmaril@260
  1206
	else	
insilmaril@260
  1207
		setHideTmpMode (HideNone);
insilmaril@0
  1208
}
insilmaril@0
  1209
insilmaril@366
  1210
void MapEditor::exportImage(QString fn, QString format)
insilmaril@0
  1211
{
insilmaril@260
  1212
	setExportMode (true);
insilmaril@0
  1213
	QPixmap pix (getPixmap());
insilmaril@366
  1214
	pix.save(fn, format);
insilmaril@260
  1215
	setExportMode (false);
insilmaril@0
  1216
}
insilmaril@0
  1217
insilmaril@185
  1218
void MapEditor::exportOOPresentation(const QString &fn, const QString &cf)
insilmaril@163
  1219
{
insilmaril@185
  1220
	ExportOO ex;
insilmaril@199
  1221
	ex.setFile (fn);
insilmaril@185
  1222
	ex.setMapCenter(mapCenter);
insilmaril@260
  1223
	if (ex.setConfigFile(cf)) 
insilmaril@260
  1224
	{
insilmaril@260
  1225
		setExportMode (true);
insilmaril@260
  1226
		ex.exportPresentation();
insilmaril@260
  1227
		setExportMode (false);
insilmaril@260
  1228
	}
insilmaril@163
  1229
}
insilmaril@163
  1230
insilmaril@163
  1231
insilmaril@0
  1232
insilmaril@0
  1233
void MapEditor::exportXML(const QString &dir)
insilmaril@0
  1234
{
insilmaril@260
  1235
	// Hide stuff during export, if settings want this
insilmaril@260
  1236
	setExportMode (true);
insilmaril@260
  1237
insilmaril@0
  1238
	// Create subdirectories
insilmaril@0
  1239
	makeSubDirs (dir);
insilmaril@0
  1240
insilmaril@0
  1241
	// write to directory
insilmaril@106
  1242
	QString saveFile=saveToDir (dir,mapName+"-",true,mapCenter->getTotalBBox().topLeft() ,NULL);
insilmaril@0
  1243
	QFile file;
insilmaril@0
  1244
insilmaril@0
  1245
	file.setName ( dir + "/"+mapName+".xml");
insilmaril@366
  1246
	if ( !file.open( QIODevice::WriteOnly ) )
insilmaril@0
  1247
	{
insilmaril@0
  1248
		// This should neverever happen
insilmaril@159
  1249
		QMessageBox::critical (0,tr("Critical Export Error"),tr("MapEditor::exportXML couldn't open %1").arg(file.name()));
insilmaril@0
  1250
		return;
insilmaril@0
  1251
	}	
insilmaril@0
  1252
insilmaril@0
  1253
	// Write it finally, and write in UTF8, no matter what 
insilmaril@0
  1254
	QTextStream ts( &file );
insilmaril@0
  1255
	ts.setEncoding (QTextStream::UnicodeUTF8);
insilmaril@0
  1256
	ts << saveFile;
insilmaril@0
  1257
	file.close();
insilmaril@0
  1258
insilmaril@0
  1259
	// Now write image, too
insilmaril@0
  1260
	exportImage (dir+"/images/"+mapName+".png");
insilmaril@260
  1261
insilmaril@260
  1262
	setExportMode (false);
insilmaril@0
  1263
}
insilmaril@0
  1264
insilmaril@0
  1265
void MapEditor::clear()
insilmaril@0
  1266
{
insilmaril@0
  1267
	if (selection)
insilmaril@0
  1268
	{
insilmaril@0
  1269
		selection->unselect();
insilmaril@0
  1270
		selection=NULL;
insilmaril@0
  1271
	}	
insilmaril@0
  1272
insilmaril@0
  1273
	mapCenter->clear();
insilmaril@0
  1274
}
insilmaril@0
  1275
insilmaril@0
  1276
void MapEditor::copy()
insilmaril@0
  1277
{
insilmaril@0
  1278
	if (selection) 
insilmaril@0
  1279
	{
insilmaril@166
  1280
		// write to directory
insilmaril@166
  1281
		QString clipfile="part";
insilmaril@166
  1282
		QString saveFile=saveToDir (fileDir,clipfile+"-",true,QPoint(),selection);
insilmaril@166
  1283
		QFile file;
insilmaril@166
  1284
insilmaril@166
  1285
		file.setName ( clipboardDir + "/"+clipfile+".xml");
insilmaril@366
  1286
		if ( !file.open( QIODevice::WriteOnly ) )
insilmaril@0
  1287
		{
insilmaril@166
  1288
			// This should neverever happen
insilmaril@166
  1289
			QMessageBox::critical (0,tr("Critical Export Error"),tr("MapEditor::exportXML couldn't open %1").arg(file.name()));
insilmaril@166
  1290
			return;
insilmaril@166
  1291
		}	
insilmaril@166
  1292
insilmaril@166
  1293
		// Write it finally, and write in UTF8, no matter what 
insilmaril@166
  1294
		QTextStream ts( &file );
insilmaril@166
  1295
		ts.setEncoding (QTextStream::UnicodeUTF8);
insilmaril@166
  1296
		ts << saveFile;
insilmaril@166
  1297
		file.close();
insilmaril@166
  1298
insilmaril@166
  1299
		clipboardEmpty=false;
insilmaril@166
  1300
		updateActions();
insilmaril@0
  1301
	}	    
insilmaril@0
  1302
}
insilmaril@0
  1303
insilmaril@163
  1304
void MapEditor::redo()
insilmaril@163
  1305
{
insilmaril@163
  1306
	blockSaveState=true;
insilmaril@163
  1307
	
insilmaril@366
  1308
	// Restore variables
insilmaril@366
  1309
	int curStep=undoSet.readNumEntry (QString("/history/curStep"));
insilmaril@366
  1310
	int undosAvail=undoSet.readNumEntry (QString("/history/undosAvail"));
insilmaril@366
  1311
	int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
insilmaril@381
  1312
	// Can we undo at all?
insilmaril@381
  1313
	if (redosAvail<1) return;
insilmaril@381
  1314
	redosAvail--;
insilmaril@381
  1315
insilmaril@381
  1316
	if (undosAvail<stepsTotal) undosAvail++;
insilmaril@366
  1317
	curStep++;
insilmaril@381
  1318
	if (curStep>stepsTotal) curStep=1;
insilmaril@366
  1319
	QString undoCommand=  undoSet.readEntry (QString("/history/step-%1/undoCommand").arg(curStep));
insilmaril@366
  1320
	QString undoSelection=undoSet.readEntry (QString("/history/step-%1/undoSelection").arg(curStep));
insilmaril@366
  1321
	QString redoCommand=  undoSet.readEntry (QString("/history/step-%1/redoCommand").arg(curStep));
insilmaril@366
  1322
	QString redoSelection=undoSet.readEntry (QString("/history/step-%1/redoSelection").arg(curStep));
insilmaril@366
  1323
	QString comment=undoSet.readEntry (QString("/history/step-%1/comment").arg(curStep));
insilmaril@366
  1324
insilmaril@163
  1325
	// Find out current undo directory
insilmaril@366
  1326
	QString bakMapDir=QDir::convertSeparators (QString(tmpMapDir+"/undo-%1").arg(curStep));
insilmaril@366
  1327
insilmaril@366
  1328
/* TODO remove testing
insilmaril@366
  1329
*/
insilmaril@366
  1330
	cout << "ME::redo() begin\n";
insilmaril@366
  1331
	cout << "    undosAvail="<<undosAvail<<endl;
insilmaril@366
  1332
	cout << "    redosAvail="<<redosAvail<<endl;
insilmaril@366
  1333
	cout << "       curStep="<<curStep<<endl;
insilmaril@366
  1334
	cout << "    ---------------------------"<<endl;
insilmaril@366
  1335
	cout << "    comment="<<comment.toStdString()<<endl;
insilmaril@366
  1336
	cout << "    undoCom="<<undoCommand.toStdString()<<endl;
insilmaril@366
  1337
	cout << "    undoSel="<<undoSelection.toStdString()<<endl;
insilmaril@366
  1338
	cout << "    redoCom="<<redoCommand.toStdString()<<endl;
insilmaril@366
  1339
	cout << "    redoSel="<<redoSelection.toStdString()<<endl;
insilmaril@366
  1340
	cout << "    ---------------------------"<<endl<<endl;
insilmaril@163
  1341
insilmaril@163
  1342
	// select  object before redo
insilmaril@366
  1343
	// FIXME better give up if no selection there...
insilmaril@163
  1344
	if (!redoSelection.isEmpty())
insilmaril@163
  1345
		select (redoSelection);
insilmaril@163
  1346
insilmaril@366
  1347
insilmaril@366
  1348
	parseAtom (redoCommand);
insilmaril@366
  1349
	mapCenter->reposition();
insilmaril@366
  1350
insilmaril@366
  1351
	blockSaveState=false;
insilmaril@366
  1352
insilmaril@366
  1353
	undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
insilmaril@366
  1354
	undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
insilmaril@366
  1355
	undoSet.setEntry ("/history/curStep",QString::number(curStep));
insilmaril@366
  1356
	undoSet.writeSettings(histPath);
insilmaril@366
  1357
insilmaril@381
  1358
	historyWindow.update (undoSet);
insilmaril@366
  1359
	updateActions();
insilmaril@366
  1360
insilmaril@366
  1361
	/* TODO remove testing
insilmaril@163
  1362
*/
insilmaril@163
  1363
	cout << "ME::redo() end\n";
insilmaril@163
  1364
	cout << "    undosAvail="<<undosAvail<<endl;
insilmaril@366
  1365
	cout << "    redosAvail="<<redosAvail<<endl;
insilmaril@366
  1366
	cout << "       curStep="<<curStep<<endl;
insilmaril@163
  1367
	cout << "    ---------------------------"<<endl<<endl;
insilmaril@366
  1368
insilmaril@366
  1369
insilmaril@163
  1370
}
insilmaril@163
  1371
insilmaril@106
  1372
void MapEditor::undo()
insilmaril@106
  1373
{
insilmaril@163
  1374
	blockSaveState=true;
insilmaril@163
  1375
	
insilmaril@366
  1376
	// Restore variables
insilmaril@366
  1377
	int curStep=undoSet.readNumEntry (QString("/history/curStep"));
insilmaril@366
  1378
	int undosAvail=undoSet.readNumEntry (QString("/history/undosAvail"));
insilmaril@366
  1379
	int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
insilmaril@381
  1380
insilmaril@381
  1381
	// Can we undo at all?
insilmaril@381
  1382
	if (undosAvail<1) return;
insilmaril@381
  1383
insilmaril@366
  1384
	QString undoCommand=  undoSet.readEntry (QString("/history/step-%1/undoCommand").arg(curStep));
insilmaril@366
  1385
	QString undoSelection=undoSet.readEntry (QString("/history/step-%1/undoSelection").arg(curStep));
insilmaril@366
  1386
	QString redoCommand=  undoSet.readEntry (QString("/history/step-%1/redoCommand").arg(curStep));
insilmaril@366
  1387
	QString redoSelection=undoSet.readEntry (QString("/history/step-%1/redoSelection").arg(curStep));
insilmaril@366
  1388
	QString comment=undoSet.readEntry (QString("/history/step-%1/comment").arg(curStep));
insilmaril@366
  1389
insilmaril@163
  1390
	// Find out current undo directory
insilmaril@366
  1391
	QString bakMapDir=QDir::convertSeparators (QString(tmpMapDir+"/undo-%1").arg(curStep));
insilmaril@163
  1392
insilmaril@136
  1393
	// select  object before undo
insilmaril@136
  1394
	if (!undoSelection.isEmpty())
insilmaril@136
  1395
		select (undoSelection);
insilmaril@136
  1396
insilmaril@164
  1397
/* TODO testing
insilmaril@366
  1398
*/	
insilmaril@163
  1399
	cout << "ME::undo() begin\n";
insilmaril@163
  1400
	cout << "    undosAvail="<<undosAvail<<endl;
insilmaril@366
  1401
	cout << "    redosAvail="<<redosAvail<<endl;
insilmaril@366
  1402
	cout << "       curStep="<<curStep<<endl;
insilmaril@163
  1403
	cout << "    ---------------------------"<<endl;
insilmaril@366
  1404
	cout << "    comment="<<comment.toStdString()<<endl;
insilmaril@366
  1405
	cout << "    undoCom="<<undoCommand.toStdString()<<endl;
insilmaril@366
  1406
	cout << "    undoSel="<<undoSelection.toStdString()<<endl;
insilmaril@366
  1407
	cout << "    redoCom="<<redoCommand.toStdString()<<endl;
insilmaril@366
  1408
	cout << "    redoSel="<<redoSelection.toStdString()<<endl;
insilmaril@366
  1409
	cout << "    ---------------------------"<<endl<<endl;
insilmaril@106
  1410
	parseAtom (undoCommand);
insilmaril@106
  1411
	mapCenter->reposition();
insilmaril@106
  1412
insilmaril@163
  1413
	undosAvail--;
insilmaril@366
  1414
	curStep--; 
insilmaril@381
  1415
	if (curStep<1) curStep=stepsTotal;
insilmaril@366
  1416
insilmaril@366
  1417
	redosAvail++;
insilmaril@163
  1418
insilmaril@163
  1419
	blockSaveState=false;
insilmaril@163
  1420
/* TODO remove testing
insilmaril@366
  1421
*/
insilmaril@163
  1422
	cout << "ME::undo() end\n";
insilmaril@163
  1423
	cout << "    undosAvail="<<undosAvail<<endl;
insilmaril@366
  1424
	cout << "    redosAvail="<<redosAvail<<endl;
insilmaril@366
  1425
	cout << "       curStep="<<curStep<<endl;
insilmaril@163
  1426
	cout << "    ---------------------------"<<endl<<endl;
insilmaril@366
  1427
insilmaril@366
  1428
	undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
insilmaril@366
  1429
	undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
insilmaril@366
  1430
	undoSet.setEntry ("/history/curStep",QString::number(curStep));
insilmaril@366
  1431
	undoSet.writeSettings(histPath);
insilmaril@366
  1432
insilmaril@381
  1433
	historyWindow.update (undoSet);
insilmaril@366
  1434
	updateActions();
insilmaril@106
  1435
}
insilmaril@106
  1436
insilmaril@381
  1437
void MapEditor::gotoStep (int i)
insilmaril@381
  1438
{
insilmaril@381
  1439
	// Restore variables
insilmaril@381
  1440
	int undosAvail=undoSet.readNumEntry (QString("/history/undosAvail"));
insilmaril@388
  1441
	int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
insilmaril@388
  1442
insilmaril@388
  1443
	cout << "ME::goto "<<i<<endl;
insilmaril@381
  1444
	
insilmaril@388
  1445
	if (i<0) i=undosAvail+redosAvail;
insilmaril@388
  1446
insilmaril@388
  1447
	// Clicking above current step makes us undo things
insilmaril@381
  1448
	if (i<undosAvail) 
insilmaril@381
  1449
	{	
insilmaril@381
  1450
		for (int j=0; j<undosAvail-i; j++) undo();
insilmaril@381
  1451
		return;
insilmaril@381
  1452
	}	
insilmaril@388
  1453
	// Clicking below current step makes us redo things
insilmaril@381
  1454
	if (i>undosAvail) 
insilmaril@381
  1455
		for (int j=undosAvail; j<i; j++) redo();
insilmaril@381
  1456
insilmaril@381
  1457
	// And ignore clicking the current row ;-)	
insilmaril@381
  1458
}
insilmaril@381
  1459
insilmaril@163
  1460
void MapEditor::undoXML(const QString &undoSel, const QString &bakMapPath)
insilmaril@106
  1461
{
insilmaril@163
  1462
	QString bakMapDir=bakMapPath.left(bakMapPath.findRev("/"));
insilmaril@163
  1463
	QDir d(bakMapDir);
insilmaril@163
  1464
	QFile file (bakMapPath);
insilmaril@163
  1465
insilmaril@106
  1466
	if (d.exists() )
insilmaril@106
  1467
	{
insilmaril@106
  1468
		// We need to parse saved XML data
insilmaril@106
  1469
		mapBuilderHandler handler;
insilmaril@163
  1470
		QXmlInputSource source( file);
insilmaril@106
  1471
		QXmlSimpleReader reader;
insilmaril@106
  1472
		reader.setContentHandler( &handler );
insilmaril@106
  1473
		reader.setErrorHandler( &handler );
insilmaril@106
  1474
		handler.setMapEditor( this );
insilmaril@106
  1475
		handler.setTmpDir ( bakMapDir );	// needed to load files with rel. path
insilmaril@106
  1476
		if (undoSel.isEmpty())
insilmaril@106
  1477
		{
insilmaril@139
  1478
			unselect();
insilmaril@106
  1479
			mapCenter->clear();
insilmaril@106
  1480
			handler.setLoadMode (NewMap);
insilmaril@106
  1481
		} else	
insilmaril@106
  1482
		{
insilmaril@106
  1483
			select (undoSel);
insilmaril@106
  1484
			handler.setLoadMode (ImportReplace);
insilmaril@106
  1485
		}	
insilmaril@139
  1486
		blockReposition=true;
insilmaril@106
  1487
		bool ok = reader.parse( source );
insilmaril@139
  1488
		blockReposition=false;
insilmaril@106
  1489
		if (! ok ) 
insilmaril@106
  1490
		{	
insilmaril@106
  1491
			// This should never ever happen
insilmaril@106
  1492
			QMessageBox::critical( 0, tr( "Critical Parse Error by reading backupFile" ),
insilmaril@163
  1493
								   tr( handler.errorProtocol() )+" in "+bakMapDir );
insilmaril@106
  1494
		}
insilmaril@106
  1495
	} else	
insilmaril@106
  1496
	{
insilmaril@106
  1497
		QMessageBox::critical( 0, tr( "Critical Error" ),
insilmaril@159
  1498
			tr("Temporary directory %1 used for undo is gone. \n"
insilmaril@106
  1499
			"I will create a new one, but at the moment no undo is available.\n"
insilmaril@106
  1500
			"Maybe you want to reload your original data.\n\n"
insilmaril@159
  1501
			"Sorry for any inconveniences.").arg(bakMapDir) );
insilmaril@106
  1502
		makeTmpDirs();	
insilmaril@106
  1503
	}							
insilmaril@106
  1504
}
insilmaril@106
  1505
insilmaril@166
  1506
void MapEditor::pasteNoSave()
insilmaril@0
  1507
{
insilmaril@166
  1508
	load (clipboardDir+"/part.xml",ImportAdd);
insilmaril@0
  1509
}
insilmaril@0
  1510
insilmaril@0
  1511
void MapEditor::cutNoSave()
insilmaril@0
  1512
{
insilmaril@166
  1513
	copy();
insilmaril@166
  1514
	deleteSelection();
insilmaril@0
  1515
}
insilmaril@0
  1516
insilmaril@0
  1517
void MapEditor::paste()
insilmaril@0
  1518
{   
insilmaril@166
  1519
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@166
  1520
				      typeid(*selection) == typeid(MapCenterObj))) 
insilmaril@166
  1521
	{
insilmaril@366
  1522
		saveStatePart(selection,QString("Paste to %1").arg( getName(selection)));
insilmaril@166
  1523
		pasteNoSave();
insilmaril@166
  1524
		mapCenter->reposition();
insilmaril@166
  1525
		adjustCanvasSize();
insilmaril@166
  1526
	}
insilmaril@0
  1527
}
insilmaril@0
  1528
insilmaril@0
  1529
void MapEditor::cut()
insilmaril@0
  1530
{
insilmaril@366
  1531
	saveStatePart(selection->getParObj(),QString("Cut %1").arg(getName(selection)));
insilmaril@0
  1532
	copy();
insilmaril@0
  1533
	cutNoSave();
insilmaril@0
  1534
	mapCenter->reposition();
insilmaril@0
  1535
	adjustCanvasSize();
insilmaril@0
  1536
}
insilmaril@0
  1537
insilmaril@130
  1538
void MapEditor::move(const int &x, const int &y)
insilmaril@130
  1539
{
insilmaril@366
  1540
	if (selection)
insilmaril@366
  1541
	{
insilmaril@366
  1542
		QString ps=qpointToString (selection->getAbsPos());
insilmaril@366
  1543
		QString s=selection->getSelectString();
insilmaril@366
  1544
		saveState(
insilmaril@366
  1545
			s, "move "+ps, 
insilmaril@366
  1546
			s, "move "+qpointToString (QPoint (x,y)), 
insilmaril@366
  1547
			QString("Move %1 to  %2").arg(getName(selection)).arg(ps));
insilmaril@366
  1548
		selection->move(x,y);
insilmaril@366
  1549
		mapCenter->reposition();
insilmaril@366
  1550
		adjustCanvasSize();
insilmaril@366
  1551
	}
insilmaril@366
  1552
insilmaril@366
  1553
}
insilmaril@366
  1554
insilmaril@366
  1555
void MapEditor::moveRel (const int &x, const int &y)
insilmaril@366
  1556
{
insilmaril@388
  1557
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@388
  1558
				      typeid(*selection) == typeid(MapCenterObj) ||
insilmaril@388
  1559
					  typeid(*selection) == typeid (FloatImageObj))) 
insilmaril@366
  1560
	{
insilmaril@366
  1561
		QString ps=qpointToString (selection->getRelPos());
insilmaril@366
  1562
		QString s=selection->getSelectString();
insilmaril@366
  1563
		saveState(
insilmaril@366
  1564
			s, "moveRel "+ps, 
insilmaril@366
  1565
			s, "moveRel "+qpointToString (QPoint (x,y)), 
insilmaril@366
  1566
			QString("Move %1 to relativ position %2").arg(getName(selection)).arg(ps));
insilmaril@388
  1567
		((OrnamentedObj*)selection)->move2RelPos (x,y);
insilmaril@366
  1568
		mapCenter->reposition();
insilmaril@366
  1569
		adjustCanvasSize();
insilmaril@366
  1570
	}
insilmaril@130
  1571
}
insilmaril@130
  1572
insilmaril@0
  1573
void MapEditor::moveBranchUp()
insilmaril@0
  1574
{
insilmaril@0
  1575
	BranchObj* bo;
insilmaril@0
  1576
	BranchObj* par;
insilmaril@0
  1577
	if (typeid(*selection) == typeid(BranchObj)  ) 
insilmaril@0
  1578
	{
insilmaril@163
  1579
		bo=(BranchObj*)selection;
insilmaril@314
  1580
		if (!bo->canMoveBranchUp()) return;
insilmaril@0
  1581
		par=(BranchObj*)(bo->getParObj());
insilmaril@0
  1582
		selection->unselect();
insilmaril@366
  1583
		bo=par->moveBranchUp (bo);	// bo will be the one below selection
insilmaril@0
  1584
		selection->select();
insilmaril@366
  1585
		saveState (selection,"moveBranchDown ()",bo,"moveBranchUp ()",QString("Move up %1").arg(getName(bo)));
insilmaril@0
  1586
		mapCenter->reposition();
insilmaril@0
  1587
		ensureSelectionVisible();
insilmaril@0
  1588
	}
insilmaril@0
  1589
}
insilmaril@0
  1590
insilmaril@0
  1591
void MapEditor::moveBranchDown()
insilmaril@0
  1592
{
insilmaril@0
  1593
	BranchObj* bo;
insilmaril@0
  1594
	BranchObj* par;
insilmaril@0
  1595
	if (typeid(*selection) == typeid(BranchObj)  ) 
insilmaril@0
  1596
	{
insilmaril@163
  1597
		bo=(BranchObj*)selection;
insilmaril@314
  1598
		if (!bo->canMoveBranchDown()) return;
insilmaril@0
  1599
		par=(BranchObj*)(bo->getParObj());
insilmaril@0
  1600
		selection->unselect(); 
insilmaril@366
  1601
		bo=par->moveBranchDown(bo);	// bo will be the one above selection
insilmaril@0
  1602
		selection->select();
insilmaril@366
  1603
		saveState(selection,"moveBranchUp ()",bo,"moveBranchDown ()",QString("Move down %1").arg(getName(bo)));
insilmaril@0
  1604
		mapCenter->reposition();
insilmaril@0
  1605
		ensureSelectionVisible();
insilmaril@0
  1606
	}	
insilmaril@0
  1607
}
insilmaril@0
  1608
insilmaril@366
  1609
QString MapEditor::getHeading(bool &ok, QPoint &p)
insilmaril@0
  1610
{
insilmaril@366
  1611
	if (selection  &&  
insilmaril@366
  1612
		 (typeid(*selection) == typeid(BranchObj) || 
insilmaril@366
  1613
		  typeid(*selection) == typeid(MapCenterObj) ) ) 
insilmaril@366
  1614
	{
insilmaril@366
  1615
		ok=true;
insilmaril@366
  1616
		ensureSelectionVisible();
insilmaril@366
  1617
		p = ((BranchObj*)selection)->getAbsPos();
insilmaril@366
  1618
		p.setX (p.x() - contentsX());
insilmaril@366
  1619
		p.setY (p.y() - contentsY() + ((BranchObj*)selection)->height()/2);
insilmaril@366
  1620
		return ((BranchObj*)selection)->getHeading();
insilmaril@366
  1621
	}
insilmaril@366
  1622
	ok=false;
insilmaril@366
  1623
	return QString();
insilmaril@366
  1624
}
insilmaril@366
  1625
insilmaril@366
  1626
void MapEditor::setHeading(const QString &s)
insilmaril@366
  1627
{
insilmaril@0
  1628
	if (selection  &&  
insilmaril@0
  1629
		 (typeid(*selection) == typeid(BranchObj) || 
insilmaril@0
  1630
		  typeid(*selection) == typeid(MapCenterObj) ) ) 
insilmaril@0
  1631
	{
insilmaril@163
  1632
		editingBO=(BranchObj*)selection;
insilmaril@366
  1633
		saveStateConstSelection(
insilmaril@366
  1634
			"setHeading (\""+editingBO->getHeading()+"\")", 
insilmaril@366
  1635
			"setHeading (\""+s+"\")", 
insilmaril@366
  1636
			QString("Set heading of %1 to \"%2\"").arg(getName(editingBO)).arg(s) );
insilmaril@366
  1637
		editingBO->setHeading(s );
insilmaril@366
  1638
		editingBO=NULL;
insilmaril@106
  1639
		mapCenter->reposition();
insilmaril@106
  1640
		adjustCanvasSize();
insilmaril@106
  1641
		ensureSelectionVisible();
insilmaril@106
  1642
	}
insilmaril@106
  1643
}
insilmaril@106
  1644
insilmaril@366
  1645
void MapEditor::setURLInt (const QString &s)
insilmaril@143
  1646
{
insilmaril@143
  1647
	// Internal function, no saveState needed
insilmaril@143
  1648
	if (selection  &&  
insilmaril@143
  1649
		 (typeid(*selection) == typeid(BranchObj) || 
insilmaril@143
  1650
		  typeid(*selection) == typeid(MapCenterObj) ) ) 
insilmaril@143
  1651
	{
insilmaril@163
  1652
		((BranchObj*)selection)->setURL(s);
insilmaril@143
  1653
		mapCenter->reposition();
insilmaril@143
  1654
		adjustCanvasSize();
insilmaril@143
  1655
		ensureSelectionVisible();
insilmaril@143
  1656
	}
insilmaril@143
  1657
}
insilmaril@143
  1658
insilmaril@366
  1659
void MapEditor::setHeadingInt(const QString &s)
insilmaril@366
  1660
{
insilmaril@366
  1661
	if (selection  &&  
insilmaril@366
  1662
		 (typeid(*selection) == typeid(BranchObj) || 
insilmaril@366
  1663
		  typeid(*selection) == typeid(MapCenterObj) ) ) 
insilmaril@366
  1664
	{
insilmaril@366
  1665
		((BranchObj*)selection)->setHeading(s);
insilmaril@366
  1666
		mapCenter->reposition();
insilmaril@366
  1667
		adjustCanvasSize();
insilmaril@366
  1668
		ensureSelectionVisible();
insilmaril@366
  1669
	}
insilmaril@366
  1670
}
insilmaril@366
  1671
insilmaril@366
  1672
void MapEditor::setVymLinkInt (const QString &s)
insilmaril@143
  1673
{
insilmaril@143
  1674
	// Internal function, no saveState needed
insilmaril@143
  1675
	if (selection  &&  
insilmaril@143
  1676
		 (typeid(*selection) == typeid(BranchObj) || 
insilmaril@143
  1677
		  typeid(*selection) == typeid(MapCenterObj) ) ) 
insilmaril@143
  1678
	{
insilmaril@163
  1679
		((BranchObj*)selection)->setVymLink(s);
insilmaril@143
  1680
		mapCenter->reposition();
insilmaril@143
  1681
		adjustCanvasSize();
insilmaril@143
  1682
		ensureSelectionVisible();
insilmaril@143
  1683
	}
insilmaril@143
  1684
}
insilmaril@143
  1685
insilmaril@366
  1686
BranchObj* MapEditor::addNewBranchInt(int num)
insilmaril@0
  1687
{
insilmaril@366
  1688
	// Depending on pos:
insilmaril@366
  1689
	// -3		insert in childs of parent  above selection 
insilmaril@366
  1690
	// -2		add branch to selection 
insilmaril@366
  1691
	// -1		insert in childs of parent below selection 
insilmaril@366
  1692
	// 0..n		insert in childs of parent at pos
insilmaril@366
  1693
	BranchObj *newbo=NULL;
insilmaril@0
  1694
	if (selection  &&  
insilmaril@0
  1695
		 (typeid(*selection) == typeid(BranchObj) || 
insilmaril@0
  1696
		  typeid(*selection) == typeid(MapCenterObj) ) ) 
insilmaril@0
  1697
	{
insilmaril@366
  1698
		BranchObj* bo = (BranchObj*) selection;
insilmaril@366
  1699
		if (num==-2)
insilmaril@0
  1700
		{
insilmaril@0
  1701
			// save scroll state. If scrolled, automatically select
insilmaril@0
  1702
			// new branch in order to tmp unscroll parent...
insilmaril@366
  1703
			return bo->addBranch();
insilmaril@366
  1704
			
insilmaril@366
  1705
		}else if (num==-1)
insilmaril@0
  1706
		{
insilmaril@366
  1707
			num=bo->getNum()+1;
insilmaril@366
  1708
			bo=(BranchObj*)bo->getParObj();
insilmaril@366
  1709
		}else if (num==-3)
insilmaril@366
  1710
		{
insilmaril@366
  1711
			num=bo->getNum();
insilmaril@366
  1712
			bo=(BranchObj*)bo->getParObj();
insilmaril@366
  1713
		}
insilmaril@366
  1714
		if (!bo) return bo;
insilmaril@366
  1715
		newbo=bo->insertBranch(num);
insilmaril@366
  1716
	}	
insilmaril@366
  1717
	return newbo;
insilmaril@366
  1718
}	
insilmaril@366
  1719
insilmaril@366
  1720
void MapEditor::addNewBranch(int pos)
insilmaril@366
  1721
{
insilmaril@366
  1722
	// Different meaning than num in addNewBranchInt!
insilmaril@366
  1723
	// -1	add above
insilmaril@366
  1724
	//  0	add as child
insilmaril@366
  1725
	// +1	add below
insilmaril@366
  1726
	if (selection  &&  
insilmaril@366
  1727
		 (typeid(*selection) == typeid(BranchObj) || 
insilmaril@366
  1728
		  typeid(*selection) == typeid(MapCenterObj) ) ) 
insilmaril@366
  1729
	{
insilmaril@366
  1730
		BranchObj *bo = (BranchObj*) selection;
insilmaril@366
  1731
		BranchObj *newbo;
insilmaril@366
  1732
		newbo=addNewBranchInt (pos-2);
insilmaril@366
  1733
insilmaril@366
  1734
		if (newbo)
insilmaril@366
  1735
		{
insilmaril@366
  1736
			saveStateConstSelection (
insilmaril@366
  1737
				QString ("delete (\"%1\")").arg(newbo->getSelectString()),
insilmaril@366
  1738
				QString ("addBranch (%1)").arg(pos-2),
insilmaril@366
  1739
				QString ("Add new branch to %1").arg(getName(bo)));	//TODO undoCommand
insilmaril@366
  1740
insilmaril@366
  1741
			LinkableMapObj *oldselection=selection;
insilmaril@366
  1742
insilmaril@366
  1743
			mapCenter->reposition();
insilmaril@366
  1744
			adjustCanvasSize();
insilmaril@366
  1745
insilmaril@366
  1746
insilmaril@366
  1747
			if (actionSettingsAutoedit->isOn() ||
insilmaril@366
  1748
				actionSettingsAutoselectHeading->isOn() )
insilmaril@0
  1749
			{
insilmaril@366
  1750
				selection->unselect();
insilmaril@366
  1751
				selection=newbo;
insilmaril@366
  1752
				selection->select();
insilmaril@366
  1753
				if (actionSettingsAutoedit->isOn() )
insilmaril@366
  1754
					mainWindow->editHeading();
insilmaril@366
  1755
				if (!actionSettingsAutoselectHeading->isOn()
insilmaril@366
  1756
					)//&& !wasScrolled)  //FIXME wasScrolled was moved to addNewBranchInt
insilmaril@366
  1757
				{
insilmaril@366
  1758
					selection->unselect();
insilmaril@366
  1759
					selection=oldselection;
insilmaril@366
  1760
					selection->select();
insilmaril@366
  1761
				}
insilmaril@366
  1762
			}	
insilmaril@366
  1763
		}
insilmaril@366
  1764
	}	
insilmaril@366
  1765
}
insilmaril@366
  1766
insilmaril@366
  1767
insilmaril@366
  1768
void MapEditor::addNewBranchHere()
insilmaril@366
  1769
{
insilmaril@366
  1770
	if (selection  &&  
insilmaril@366
  1771
		 (typeid(*selection) == typeid(BranchObj) ) )
insilmaril@366
  1772
	{
insilmaril@366
  1773
		BranchObj* bo1 = (BranchObj*) selection;
insilmaril@366
  1774
		saveStatePart(selection, QString("Add new branch here").arg(getName(bo1)));
insilmaril@366
  1775
insilmaril@366
  1776
		bool wasScrolled=false;
insilmaril@366
  1777
		BranchObj *newbo=NULL;
insilmaril@366
  1778
		BranchObj *parbo=(BranchObj*)(selection->getParObj());
insilmaril@366
  1779
		if (parbo)
insilmaril@366
  1780
		{
insilmaril@366
  1781
			// add below selection
insilmaril@366
  1782
			newbo=parbo->insertBranch(bo1->getNum()+1);
insilmaril@366
  1783
		} 
insilmaril@0
  1784
insilmaril@0
  1785
		LinkableMapObj *oldselection=selection;
insilmaril@366
  1786
		((BranchObj*)selection)->moveBranchTo (newbo,-1);
insilmaril@0
  1787
insilmaril@0
  1788
		mapCenter->reposition();
insilmaril@0
  1789
		adjustCanvasSize();
insilmaril@0
  1790
		if (actionSettingsAutoedit->isOn() ||
insilmaril@0
  1791
			actionSettingsAutoselectHeading->isOn() )
insilmaril@0
  1792
		{
insilmaril@0
  1793
			selection->unselect();
insilmaril@0
  1794
			selection=newbo;
insilmaril@0
  1795
			selection->select();
insilmaril@0
  1796
			if (actionSettingsAutoedit->isOn() )
insilmaril@366
  1797
				mainWindow->editHeading();
insilmaril@0
  1798
			if (!actionSettingsAutoselectHeading->isOn()
insilmaril@0
  1799
				&& !wasScrolled)
insilmaril@0
  1800
			{
insilmaril@0
  1801
				selection->unselect();
insilmaril@0
  1802
				selection=oldselection;
insilmaril@0
  1803
				selection->select();
insilmaril@0
  1804
			}
insilmaril@0
  1805
		}	
insilmaril@0
  1806
	}	
insilmaril@0
  1807
}
insilmaril@0
  1808
insilmaril@0
  1809
void MapEditor::deleteSelection()
insilmaril@0
  1810
{
insilmaril@0
  1811
	if (selection  && typeid(*selection) ==typeid(BranchObj) ) 
insilmaril@0
  1812
	{
insilmaril@366
  1813
		BranchObj* bo=(BranchObj*)selection;
insilmaril@294
  1814
		BranchObj* par=(BranchObj*)(bo->getParObj());
insilmaril@294
  1815
		bo->unselect();
insilmaril@139
  1816
		if (selection->getDepth()>1)
insilmaril@139
  1817
			// Normal branch, save parent with childs
insilmaril@366
  1818
			saveStatePart(par,QString("Delete %1").arg(getName(bo)));
insilmaril@366
  1819
		//	naveState (UndoCom, par, QString("insertBranchAt (%1)", bo, "", QString ("Delete %1").arg(getName(bo))));
insilmaril@139
  1820
		else
insilmaril@139
  1821
			// Mainbranch, save whole map
insilmaril@164
  1822
			// TODO Better would be to insert mainbranch again at pos
insilmaril@139
  1823
			// But undoCommand is missing right now
insilmaril@366
  1824
			saveStateComplete(QString("Delete %1").arg(getName(bo)));
insilmaril@0
  1825
		selection=NULL;
insilmaril@0
  1826
		par->removeBranch(bo);
insilmaril@0
  1827
		selection=par;
insilmaril@0
  1828
		selection->select();
insilmaril@0
  1829
		ensureSelectionVisible();
insilmaril@0
  1830
		mapCenter->reposition();
insilmaril@0
  1831
		adjustCanvasSize();
insilmaril@0
  1832
	}
insilmaril@0
  1833
	if (selection  && typeid(*selection) ==typeid(FloatImageObj) ) 
insilmaril@0
  1834
	{
insilmaril@366
  1835
		FloatImageObj* fio=(FloatImageObj*)selection;
insilmaril@0
  1836
		BranchObj* par=(BranchObj*)(fio->getParObj());
insilmaril@366
  1837
		saveStatePart(par, QString("Delete %1").arg(getName(fio)));
insilmaril@0
  1838
		fio->unselect();
insilmaril@0
  1839
		selection=NULL;
insilmaril@0
  1840
		par->removeFloatImage(fio);
insilmaril@0
  1841
		selection=par;
insilmaril@0
  1842
		selection->select();
insilmaril@0
  1843
		ensureSelectionVisible();
insilmaril@0
  1844
		mapCenter->reposition();
insilmaril@0
  1845
		adjustCanvasSize();
insilmaril@0
  1846
	}
insilmaril@0
  1847
}
insilmaril@0
  1848
insilmaril@0
  1849
LinkableMapObj* MapEditor::getSelection()
insilmaril@0
  1850
{
insilmaril@0
  1851
	return selection;
insilmaril@0
  1852
}
insilmaril@0
  1853
insilmaril@267
  1854
void MapEditor::unselect()
insilmaril@267
  1855
{
insilmaril@267
  1856
	if (selection) 
insilmaril@267
  1857
	{
insilmaril@267
  1858
		selectionLast=selection;
insilmaril@267
  1859
		selection->unselect();
insilmaril@267
  1860
		selection=NULL;
insilmaril@267
  1861
	}
insilmaril@267
  1862
}	
insilmaril@267
  1863
insilmaril@267
  1864
void MapEditor::reselect()
insilmaril@267
  1865
{
insilmaril@267
  1866
	if (selectionLast)
insilmaril@267
  1867
	{
insilmaril@267
  1868
		selection=selectionLast;
insilmaril@267
  1869
		selection->select();
insilmaril@267
  1870
		selectionLast=NULL;
insilmaril@267
  1871
	}
insilmaril@267
  1872
}	
insilmaril@267
  1873
insilmaril@83
  1874
bool MapEditor::select (const QString &s)
insilmaril@83
  1875
{
insilmaril@106
  1876
	LinkableMapObj *lmo=mapCenter->findObjBySelect(s);
insilmaril@0
  1877
insilmaril@0
  1878
	// Finally select the found object
insilmaril@0
  1879
	if (lmo)
insilmaril@0
  1880
	{
insilmaril@98
  1881
		if (selection) unselect();
insilmaril@0
  1882
		selection=lmo;
insilmaril@0
  1883
		selection->select();
insilmaril@0
  1884
		adjustCanvasSize();
insilmaril@0
  1885
		ensureSelectionVisible();
insilmaril@0
  1886
		return true;
insilmaril@139
  1887
	} 
insilmaril@139
  1888
	return false;
insilmaril@0
  1889
}
insilmaril@0
  1890
insilmaril@366
  1891
QString MapEditor::getSelectString()
insilmaril@366
  1892
{
insilmaril@366
  1893
	if (selection) return selection->getSelectString();
insilmaril@366
  1894
	return QString();
insilmaril@366
  1895
}
insilmaril@366
  1896
insilmaril@366
  1897
void MapEditor::selectInt (LinkableMapObj *lmo)
insilmaril@0
  1898
{
insilmaril@267
  1899
	if (lmo && selection != lmo)
insilmaril@0
  1900
	{
insilmaril@267
  1901
		// select the MapObj
insilmaril@267
  1902
		if (selection) selection->unselect();
insilmaril@267
  1903
		selection=lmo;
insilmaril@267
  1904
		selection->select();
insilmaril@267
  1905
			
insilmaril@267
  1906
		adjustCanvasSize();
insilmaril@0
  1907
	}
insilmaril@267
  1908
}
insilmaril@0
  1909
insilmaril@366
  1910
void MapEditor::selectNextBranchInt()
insilmaril@0
  1911
{
insilmaril@0
  1912
	// Increase number of branch
insilmaril@0
  1913
	if (selection)
insilmaril@0
  1914
	{
insilmaril@0
  1915
		QString s=selection->getSelectString();
insilmaril@0
  1916
		QString part;
insilmaril@0
  1917
		QString typ;
insilmaril@0
  1918
		QString num;
insilmaril@0
  1919
insilmaril@0
  1920
		// Where am I? 
insilmaril@0
  1921
		part=s.section(",",-1);
insilmaril@0
  1922
		typ=part.left (3);
insilmaril@0
  1923
		num=part.right(part.length() - 3);
insilmaril@0
  1924
insilmaril@0
  1925
		s=s.left (s.length() -num.length());
insilmaril@0
  1926
insilmaril@0
  1927
		// Go to next lmo
insilmaril@0
  1928
		num=QString ("%1").arg(num.toUInt()+1);
insilmaril@0
  1929
insilmaril@0
  1930
		s=s+num;
insilmaril@0
  1931
		
insilmaril@0
  1932
		// Try to select this one
insilmaril@0
  1933
		if (select (s)) return;
insilmaril@0
  1934
insilmaril@0
  1935
		// We have no direct successor, 
insilmaril@0
  1936
		// try to increase the parental number in order to
insilmaril@0
  1937
		// find a successor with same depth
insilmaril@0
  1938
insilmaril@0
  1939
		int d=selection->getDepth();
insilmaril@0
  1940
		int oldDepth=d;
insilmaril@0
  1941
		int i;
insilmaril@0
  1942
		bool found=false;
insilmaril@0
  1943
		bool b;
insilmaril@0
  1944
		while (!found && d>0)
insilmaril@0
  1945
		{
insilmaril@0
  1946
			s=s.section (",",0,d-1);
insilmaril@0
  1947
			// replace substring of current depth in s with "1"
insilmaril@0
  1948
			part=s.section(",",-1);
insilmaril@0
  1949
			typ=part.left (3);
insilmaril@0
  1950
			num=part.right(part.length() - 3);
insilmaril@0
  1951
insilmaril@0
  1952
			if (d>1)
insilmaril@0
  1953
			{	
insilmaril@0
  1954
				// increase number of parent
insilmaril@0
  1955
				num=QString ("%1").arg(num.toUInt()+1);
insilmaril@0
  1956
				s=s.section (",",0,d-2) + ","+ typ+num;
insilmaril@0
  1957
			} else
insilmaril@0
  1958
			{
insilmaril@0
  1959
				// Special case, look at orientation
insilmaril@0
  1960
				if (selection->getOrientation()==OrientRightOfCenter)
insilmaril@0
  1961
					num=QString ("%1").arg(num.toUInt()+1);
insilmaril@0
  1962
				else	
insilmaril@0
  1963
					num=QString ("%1").arg(num.toUInt()-1);
insilmaril@0
  1964
				s=typ+num;
insilmaril@0
  1965
			}	
insilmaril@0
  1966
insilmaril@0
  1967
			if (select (s))
insilmaril@0
  1968
				// pad to oldDepth, select the first branch for each depth
insilmaril@0
  1969
				for (i=d;i<oldDepth;i++)
insilmaril@0
  1970
				{
insilmaril@0
  1971
					b=select (s);
insilmaril@0
  1972
					if (b)
insilmaril@0
  1973
					{	
insilmaril@163
  1974
						if ( ((BranchObj*)selection)->countBranches()>0)
insilmaril@0
  1975
							s+=",bo:0";
insilmaril@0
  1976
						else	
insilmaril@0
  1977
							break;
insilmaril@0
  1978
					} else
insilmaril@0
  1979
						break;
insilmaril@0
  1980
				}	
insilmaril@0
  1981
insilmaril@0
  1982
			// try to select the freshly built string
insilmaril@0
  1983
			found=select(s);
insilmaril@0
  1984
			d--;
insilmaril@0
  1985
		}
insilmaril@0
  1986
		return;
insilmaril@0
  1987
	}	
insilmaril@0
  1988
}
insilmaril@0
  1989
insilmaril@366
  1990
void MapEditor::selectPrevBranchInt()
insilmaril@0
  1991
{
insilmaril@0
  1992
	// Decrease number of branch
insilmaril@0
  1993
	if (selection)
insilmaril@0
  1994
	{
insilmaril@0
  1995
		QString s=selection->getSelectString();
insilmaril@0
  1996
		QString part;
insilmaril@0
  1997
		QString typ;
insilmaril@0
  1998
		QString num;
insilmaril@0
  1999
insilmaril@0
  2000
		// Where am I? 
insilmaril@0
  2001
		part=s.section(",",-1);
insilmaril@0
  2002
		typ=part.left (3);
insilmaril@0
  2003
		num=part.right(part.length() - 3);
insilmaril@0
  2004
insilmaril@0
  2005
		s=s.left (s.length() -num.length());
insilmaril@0
  2006
insilmaril@0
  2007
		// Go to next lmo
insilmaril@0
  2008
		num=QString ("%1").arg(num.toUInt()-1);
insilmaril@0
  2009
insilmaril@0
  2010
		s=s+num;
insilmaril@0
  2011
		
insilmaril@0
  2012
		// Try to select this one
insilmaril@0
  2013
		if (select (s)) return;
insilmaril@0
  2014
insilmaril@0
  2015
		// We have no direct precessor, 
insilmaril@0
  2016
		// try to decrease the parental number in order to
insilmaril@0
  2017
		// find a precessor with same depth
insilmaril@0
  2018
insilmaril@0
  2019
		int d=selection->getDepth();
insilmaril@0
  2020
		int oldDepth=d;
insilmaril@0
  2021
		int i;
insilmaril@0
  2022
		bool found=false;
insilmaril@0
  2023
		bool b;
insilmaril@0
  2024
		while (!found && d>0)
insilmaril@0
  2025
		{
insilmaril@0
  2026
			s=s.section (",",0,d-1);
insilmaril@0
  2027
			// replace substring of current depth in s with "1"
insilmaril@0
  2028
			part=s.section(",",-1);
insilmaril@0
  2029
			typ=part.left (3);
insilmaril@0
  2030
			num=part.right(part.length() - 3);
insilmaril@0
  2031
insilmaril@0
  2032
			if (d>1)
insilmaril@0
  2033
			{
insilmaril@0
  2034
				// decrease number of parent
insilmaril@0
  2035
				num=QString ("%1").arg(num.toUInt()-1);
insilmaril@0
  2036
				s=s.section (",",0,d-2) + ","+ typ+num;
insilmaril@0
  2037
			} else
insilmaril@0
  2038
			{
insilmaril@0
  2039
				// Special case, look at orientation
insilmaril@0
  2040
				if (selection->getOrientation()==OrientRightOfCenter)
insilmaril@0
  2041
					num=QString ("%1").arg(num.toUInt()-1);
insilmaril@0
  2042
				else	
insilmaril@0
  2043
					num=QString ("%1").arg(num.toUInt()+1);
insilmaril@0
  2044
				s=typ+num;
insilmaril@0
  2045
			}	
insilmaril@0
  2046
insilmaril@0
  2047
			if (select(s))
insilmaril@0
  2048
				// pad to oldDepth, select the last branch for each depth
insilmaril@0
  2049
				for (i=d;i<oldDepth;i++)
insilmaril@0
  2050
				{
insilmaril@0
  2051
					b=select (s);
insilmaril@0
  2052
					if (b)
insilmaril@163
  2053
						if ( ((BranchObj*)selection)->countBranches()>0)
insilmaril@163
  2054
							s+=",bo:"+ QString ("%1").arg( ((BranchObj*)selection)->countBranches()-1 );
insilmaril@0
  2055
						else	
insilmaril@0
  2056
							break;
insilmaril@0
  2057
					else
insilmaril@0
  2058
						break;
insilmaril@0
  2059
				}	
insilmaril@0
  2060
			
insilmaril@0
  2061
			// try to select the freshly built string
insilmaril@0
  2062
			found=select(s);
insilmaril@0
  2063
			d--;
insilmaril@0
  2064
		}
insilmaril@0
  2065
		return;
insilmaril@0
  2066
	}	
insilmaril@0
  2067
}
insilmaril@0
  2068
insilmaril@0
  2069
void MapEditor::selectUpperBranch()
insilmaril@0
  2070
{
insilmaril@0
  2071
	if (selection) 
insilmaril@0
  2072
	{
insilmaril@0
  2073
		if (typeid(*selection) == typeid(BranchObj))
insilmaril@0
  2074
		{
insilmaril@0
  2075
			if (selection->getOrientation()==OrientRightOfCenter)
insilmaril@366
  2076
				selectPrevBranchInt();
insilmaril@0
  2077
			else
insilmaril@0
  2078
				if (selection->getDepth()==1)
insilmaril@366
  2079
					selectNextBranchInt();
insilmaril@0
  2080
				else
insilmaril@366
  2081
					selectPrevBranchInt();
insilmaril@0
  2082
		}		
insilmaril@0
  2083
	}
insilmaril@0
  2084
}
insilmaril@0
  2085
insilmaril@0
  2086
void MapEditor::selectLowerBranch()
insilmaril@0
  2087
{
insilmaril@0
  2088
	if (selection) 
insilmaril@0
  2089
	{
insilmaril@0
  2090
		if (typeid(*selection) == typeid(BranchObj))
insilmaril@0
  2091
		{
insilmaril@0
  2092
			if (selection->getOrientation()==OrientRightOfCenter)
insilmaril@366
  2093
				selectNextBranchInt();
insilmaril@0
  2094
			else
insilmaril@0
  2095
				if (selection->getDepth()==1)
insilmaril@366
  2096
					selectPrevBranchInt();
insilmaril@0
  2097
				else
insilmaril@366
  2098
					selectNextBranchInt();
insilmaril@0
  2099
		}		
insilmaril@0
  2100
	}
insilmaril@0
  2101
}
insilmaril@0
  2102
insilmaril@0
  2103
insilmaril@0
  2104
void MapEditor::selectLeftBranch()
insilmaril@0
  2105
{
insilmaril@0
  2106
	BranchObj* bo;
insilmaril@0
  2107
	BranchObj* par;
insilmaril@0
  2108
	if (selection) 
insilmaril@0
  2109
	{
insilmaril@0
  2110
		if (typeid(*selection) == typeid(MapCenterObj))
insilmaril@0
  2111
		{
insilmaril@163
  2112
			par=  (BranchObj*) selection;
insilmaril@0
  2113
			bo=par->getLastSelectedBranch();
insilmaril@0
  2114
			if (bo)
insilmaril@0
  2115
			{
insilmaril@0
  2116
				// Workaround for reselecting on left and right side
insilmaril@0
  2117
				if (bo->getOrientation()==OrientRightOfCenter)
insilmaril@0
  2118
				{
insilmaril@0
  2119
					bo=par->getLastBranch();
insilmaril@0
  2120
				}	
insilmaril@0
  2121
				if (bo)
insilmaril@0
  2122
				{
insilmaril@0
  2123
					par->unselect();
insilmaril@0
  2124
					selection=bo;
insilmaril@0
  2125
					selection->select();
insilmaril@0
  2126
					adjustCanvasSize();
insilmaril@0
  2127
					ensureSelectionVisible();
insilmaril@0
  2128
				}
insilmaril@0
  2129
			}	
insilmaril@0
  2130
		} else
insilmaril@0
  2131
		{
insilmaril@0
  2132
			par=(BranchObj*)(selection->getParObj());
insilmaril@0
  2133
			if (selection->getOrientation()==OrientRightOfCenter)
insilmaril@0
  2134
			{
insilmaril@0
  2135
				if (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2136
					typeid(*selection) == typeid(FloatImageObj))
insilmaril@0
  2137
				{
insilmaril@0
  2138
					selection->unselect();
insilmaril@0
  2139
					selection=par;
insilmaril@0
  2140
					selection->select();
insilmaril@0
  2141
					adjustCanvasSize();
insilmaril@0
  2142
					ensureSelectionVisible();
insilmaril@0
  2143
				}
insilmaril@0
  2144
			} else
insilmaril@0
  2145
			{
insilmaril@0
  2146
				if (typeid(*selection) == typeid(BranchObj) )
insilmaril@0
  2147
				{
insilmaril@163
  2148
					bo=((BranchObj*)selection)->getLastSelectedBranch();
insilmaril@0
  2149
					if (bo) 
insilmaril@0
  2150
					{
insilmaril@0
  2151
						selection->unselect();
insilmaril@0
  2152
						selection=bo;
insilmaril@0
  2153
						selection->select();
insilmaril@0
  2154
						adjustCanvasSize();
insilmaril@0
  2155
						ensureSelectionVisible();
insilmaril@0
  2156
					}
insilmaril@0
  2157
				}
insilmaril@0
  2158
			}
insilmaril@0
  2159
		}	
insilmaril@0
  2160
	}
insilmaril@0
  2161
}
insilmaril@0
  2162
insilmaril@0
  2163
void MapEditor::selectRightBranch()
insilmaril@0
  2164
{
insilmaril@0
  2165
	BranchObj* bo;
insilmaril@0
  2166
	BranchObj* par;
insilmaril@0
  2167
insilmaril@0
  2168
	if (selection) 
insilmaril@0
  2169
	{
insilmaril@0
  2170
		if (typeid(*selection) == typeid(MapCenterObj))
insilmaril@0
  2171
		{
insilmaril@163
  2172
			par=  (BranchObj*) selection;
insilmaril@0
  2173
			bo=par->getLastSelectedBranch();
insilmaril@0
  2174
			if (bo)
insilmaril@0
  2175
			{
insilmaril@366
  2176
				// Workaround for reselecting on left and right side
insilmaril@0
  2177
				if (bo->getOrientation()==OrientLeftOfCenter)
insilmaril@0
  2178
					bo=par->getFirstBranch();
insilmaril@0
  2179
				if (bo)
insilmaril@0
  2180
				{
insilmaril@0
  2181
					par->unselect();
insilmaril@0
  2182
					selection=bo;
insilmaril@0
  2183
					selection->select();
insilmaril@0
  2184
					ensureSelectionVisible();
insilmaril@0
  2185
				}
insilmaril@0
  2186
			}
insilmaril@0
  2187
		} else
insilmaril@0
  2188
		{
insilmaril@0
  2189
			par=(BranchObj*)(selection->getParObj());
insilmaril@0
  2190
			if (selection->getOrientation()==OrientLeftOfCenter)
insilmaril@0
  2191
			{
insilmaril@0
  2192
				if (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2193
					typeid(*selection) == typeid(FloatImageObj))
insilmaril@0
  2194
				{
insilmaril@0
  2195
					selection->unselect();
insilmaril@0
  2196
					selection=par;
insilmaril@0
  2197
					selection->select();
insilmaril@0
  2198
					adjustCanvasSize();
insilmaril@0
  2199
					ensureSelectionVisible();
insilmaril@0
  2200
				}
insilmaril@0
  2201
			} else
insilmaril@0
  2202
			{
insilmaril@0
  2203
				if (typeid(*selection) == typeid(BranchObj) )
insilmaril@0
  2204
				{
insilmaril@163
  2205
					bo=((BranchObj*)selection)->getLastSelectedBranch();
insilmaril@0
  2206
					if (bo) 
insilmaril@0
  2207
					{
insilmaril@0
  2208
						selection->unselect();
insilmaril@0
  2209
						selection=bo;
insilmaril@0
  2210
						selection->select();
insilmaril@0
  2211
						adjustCanvasSize();
insilmaril@0
  2212
						ensureSelectionVisible();
insilmaril@0
  2213
					}
insilmaril@0
  2214
				}
insilmaril@0
  2215
			}
insilmaril@0
  2216
		}
insilmaril@0
  2217
	}
insilmaril@0
  2218
}
insilmaril@0
  2219
insilmaril@0
  2220
void MapEditor::selectFirstBranch()
insilmaril@0
  2221
{
insilmaril@0
  2222
	BranchObj *bo1;
insilmaril@0
  2223
	BranchObj *bo2;
insilmaril@0
  2224
	BranchObj* par;
insilmaril@0
  2225
	if (selection) {
insilmaril@0
  2226
		if (typeid(*selection) == typeid(BranchObj))
insilmaril@0
  2227
		{
insilmaril@163
  2228
			bo1=  (BranchObj*) selection;
insilmaril@0
  2229
			par=(BranchObj*)(bo1->getParObj());
insilmaril@0
  2230
			bo2=par->getFirstBranch();
insilmaril@0
  2231
			if (bo2) {
insilmaril@0
  2232
				bo1->unselect();
insilmaril@0
  2233
				selection=bo2;
insilmaril@0
  2234
				selection->select();
insilmaril@0
  2235
				ensureSelectionVisible();
insilmaril@0
  2236
			}
insilmaril@0
  2237
		}		
insilmaril@0
  2238
		adjustCanvasSize();
insilmaril@0
  2239
	}
insilmaril@0
  2240
}
insilmaril@0
  2241
insilmaril@0
  2242
void MapEditor::selectLastBranch()
insilmaril@0
  2243
{
insilmaril@0
  2244
	BranchObj *bo1;
insilmaril@0
  2245
	BranchObj *bo2;
insilmaril@0
  2246
	BranchObj* par;
insilmaril@0
  2247
	if (selection) {
insilmaril@0
  2248
		if (typeid(*selection) == typeid(BranchObj))
insilmaril@0
  2249
		{
insilmaril@163
  2250
			bo1=  (BranchObj*) selection;
insilmaril@0
  2251
			par=(BranchObj*)(bo1->getParObj());
insilmaril@0
  2252
			bo2=par->getLastBranch();
insilmaril@0
  2253
			if (bo2) {
insilmaril@0
  2254
				bo1->unselect();
insilmaril@0
  2255
				selection=bo2;
insilmaril@0
  2256
				selection->select();
insilmaril@0
  2257
				ensureSelectionVisible();
insilmaril@0
  2258
			}
insilmaril@0
  2259
		}		
insilmaril@0
  2260
		adjustCanvasSize();
insilmaril@0
  2261
	}
insilmaril@0
  2262
}
insilmaril@0
  2263
insilmaril@0
  2264
void MapEditor::setColor(QColor c)
insilmaril@0
  2265
{
insilmaril@0
  2266
	actColor=c;
insilmaril@0
  2267
}
insilmaril@0
  2268
insilmaril@0
  2269
void MapEditor::selectBackgroundColor()
insilmaril@0
  2270
{
insilmaril@0
  2271
	QColor col = QColorDialog::getColor( mapCanvas->backgroundColor(), this );
insilmaril@0
  2272
	if ( !col.isValid() ) return;
insilmaril@0
  2273
	setBackgroundColor( col );
insilmaril@366
  2274
	//TODO undocomm
insilmaril@366
  2275
	saveStateComplete(QString("Set background color of map to %1").arg(col.name()));
insilmaril@0
  2276
}
insilmaril@0
  2277
insilmaril@0
  2278
void MapEditor::setBackgroundColor(QColor c)
insilmaril@0
  2279
{
insilmaril@0
  2280
	mapCanvas->setBackgroundColor (c);
insilmaril@0
  2281
}
insilmaril@0
  2282
insilmaril@0
  2283
QColor MapEditor::pickColor()
insilmaril@0
  2284
{
insilmaril@0
  2285
	if (selection) 
insilmaril@0
  2286
	{
insilmaril@0
  2287
		if (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2288
			typeid(*selection) == typeid(MapCenterObj))
insilmaril@0
  2289
		{
insilmaril@163
  2290
			BranchObj *bo=(BranchObj*)selection;
insilmaril@0
  2291
			actColor=bo->getColor(); 
insilmaril@0
  2292
		}    
insilmaril@0
  2293
	}
insilmaril@0
  2294
	return actColor;
insilmaril@0
  2295
}
insilmaril@0
  2296
insilmaril@0
  2297
void MapEditor::colorItem()
insilmaril@0
  2298
{
insilmaril@0
  2299
	if (selection) 
insilmaril@0
  2300
	{
insilmaril@0
  2301
		if (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2302
			typeid(*selection) == typeid(MapCenterObj))
insilmaril@0
  2303
		{
insilmaril@163
  2304
			BranchObj *bo=(BranchObj*)selection;
insilmaril@366
  2305
			saveStatePart(selection, QString("Set color of %1 to %2").arg(getName(bo)).arg(actColor.name()));	//TODO undoCommand
insilmaril@243
  2306
			bo->setColor(actColor); // color branch
insilmaril@0
  2307
		}    
insilmaril@0
  2308
	}
insilmaril@0
  2309
}
insilmaril@0
  2310
insilmaril@0
  2311
void MapEditor::colorBranch()
insilmaril@0
  2312
{
insilmaril@0
  2313
	if (selection) 
insilmaril@0
  2314
	{
insilmaril@0
  2315
		if (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2316
			typeid(*selection) == typeid(MapCenterObj))
insilmaril@0
  2317
		{
insilmaril@163
  2318
			BranchObj *bo=(BranchObj*)selection;
insilmaril@366
  2319
			saveStatePart(selection, QString ("Set color of %1 and childs to %2").arg(getName(bo)).arg(actColor.name()));	//TODO undoCommand
insilmaril@243
  2320
			bo->setColorChilds(actColor); // color links, color childs
insilmaril@0
  2321
		}    
insilmaril@0
  2322
	}
insilmaril@0
  2323
}
insilmaril@0
  2324
insilmaril@0
  2325
insilmaril@0
  2326
void MapEditor::toggleStandardFlag(QString f)
insilmaril@0
  2327
{
insilmaril@0
  2328
	if (selection)
insilmaril@0
  2329
	{
insilmaril@366
  2330
		if (typeid(*selection) == typeid(BranchObj) ||
insilmaril@366
  2331
			typeid(*selection) == typeid(MapCenterObj))
insilmaril@366
  2332
		{
insilmaril@366
  2333
			BranchObj *bo=(BranchObj*)selection;
insilmaril@366
  2334
			QString u,r;
insilmaril@366
  2335
			if (bo->isSetStandardFlag(f))
insilmaril@366
  2336
			{
insilmaril@366
  2337
				r="unsetFlag";
insilmaril@366
  2338
				u="setFlag";
insilmaril@366
  2339
			}	
insilmaril@366
  2340
			else
insilmaril@366
  2341
			{
insilmaril@366
  2342
				u="unsetFlag";
insilmaril@366
  2343
				r="setFlag";
insilmaril@366
  2344
			}	
insilmaril@366
  2345
			saveStateConstSelection(QString("%1 (\"%2\")").arg(u).arg(f), 
insilmaril@366
  2346
				QString("%1 (\"%2\")").arg(r).arg(f),
insilmaril@366
  2347
				QString("Toggling standard flag \"%1\" of %2").arg(f).arg(getName(bo)));
insilmaril@366
  2348
			bo->toggleStandardFlag (f,actionSettingsUseFlagGroups->isOn());
insilmaril@366
  2349
			adjustCanvasSize();
insilmaril@366
  2350
		}
insilmaril@0
  2351
	}	
insilmaril@0
  2352
}
insilmaril@0
  2353
insilmaril@0
  2354
void MapEditor::setViewCenter()
insilmaril@0
  2355
{
insilmaril@0
  2356
	// transform to CanvasView Coord:
insilmaril@0
  2357
	QPoint p=worldMatrix().map(movingCenter);
insilmaril@0
  2358
	center ( p.x(), p.y());
insilmaril@0
  2359
}
insilmaril@0
  2360
insilmaril@0
  2361
insilmaril@0
  2362
BranchObj* MapEditor::findText (QString s, bool cs)
insilmaril@0
  2363
{
insilmaril@366
  2364
	QTextDocument::FindFlags flags=0;
insilmaril@366
  2365
	if (cs) flags=QTextDocument::FindCaseSensitively;
insilmaril@366
  2366
insilmaril@0
  2367
	if (!itFind) 
insilmaril@0
  2368
	{	// Nothing found or new find process
insilmaril@0
  2369
		if (EOFind)
insilmaril@0
  2370
			// nothing found, start again
insilmaril@0
  2371
			EOFind=false;
insilmaril@0
  2372
		itFind=mapCenter->first();
insilmaril@0
  2373
	}	
insilmaril@0
  2374
	bool searching=true;
insilmaril@0
  2375
	bool foundNote=false;
insilmaril@0
  2376
	while (searching && !EOFind)
insilmaril@0
  2377
	{
insilmaril@0
  2378
		if (itFind)
insilmaril@0
  2379
		{
insilmaril@0
  2380
			// Searching in Note
insilmaril@0
  2381
			if (itFind->getNote().contains(s,cs))
insilmaril@0
  2382
			{
insilmaril@0
  2383
				if (selection!=itFind) 
insilmaril@0
  2384
				{
insilmaril@163
  2385
					if (selection) ((BranchObj*)selection)->unselect();
insilmaril@0
  2386
					selection=itFind;
insilmaril@0
  2387
					selection->select();
insilmaril@0
  2388
					adjustCanvasSize();
insilmaril@0
  2389
					ensureSelectionVisible();
insilmaril@0
  2390
				}
insilmaril@366
  2391
				if (textEditor->findText(s,flags)) 
insilmaril@0
  2392
				{
insilmaril@0
  2393
					searching=false;
insilmaril@0
  2394
					foundNote=true;
insilmaril@0
  2395
				}	
insilmaril@0
  2396
			}
insilmaril@0
  2397
			// Searching in Heading
insilmaril@0
  2398
			if (searching && itFind->getHeading().contains (s,cs) ) 
insilmaril@0
  2399
			{
insilmaril@163
  2400
				if (selection) ((BranchObj*)selection)->unselect();
insilmaril@0
  2401
				selection=itFind;
insilmaril@0
  2402
				selection->select();
insilmaril@0
  2403
				adjustCanvasSize();
insilmaril@0
  2404
				ensureSelectionVisible();
insilmaril@0
  2405
				searching=false;
insilmaril@0
  2406
			}
insilmaril@0
  2407
		}	
insilmaril@0
  2408
		if (!foundNote)
insilmaril@0
  2409
		{
insilmaril@0
  2410
			itFind=itFind->next();
insilmaril@0
  2411
			if (!itFind) EOFind=true;
insilmaril@0
  2412
		}
insilmaril@0
  2413
	}	
insilmaril@0
  2414
	if (!searching)
insilmaril@0
  2415
	{
insilmaril@0
  2416
		adjustCanvasSize();
insilmaril@163
  2417
		return (BranchObj*)selection;
insilmaril@0
  2418
	}	else
insilmaril@0
  2419
		return NULL;
insilmaril@0
  2420
}
insilmaril@0
  2421
insilmaril@0
  2422
void MapEditor::findReset()
insilmaril@0
  2423
{	// Necessary if text to find changes during a find process
insilmaril@0
  2424
	itFind=NULL;
insilmaril@0
  2425
	EOFind=false;
insilmaril@0
  2426
}
insilmaril@0
  2427
insilmaril@0
  2428
void MapEditor::editURL()
insilmaril@0
  2429
{
insilmaril@0
  2430
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2431
			typeid(*selection) == typeid(MapCenterObj)) )
insilmaril@0
  2432
	{		
insilmaril@0
  2433
		bool ok;
insilmaril@163
  2434
		BranchObj *bo=(BranchObj*)selection;
insilmaril@0
  2435
		QString text = QInputDialog::getText(
insilmaril@0
  2436
				"VYM", tr("Enter URL:"), QLineEdit::Normal,
insilmaril@143
  2437
				bo->getURL(), &ok, this );
insilmaril@0
  2438
		if ( ok) 
insilmaril@0
  2439
		{
insilmaril@0
  2440
			// user entered something and pressed OK
insilmaril@366
  2441
			saveStateConstSelection("setURL (\""+bo->getURL()+"\")","setURL (\""+text+"\")", QString("Set URL of %1 to %21").arg(getName(bo)).arg(text));	
insilmaril@143
  2442
			bo->setURL (text);
insilmaril@0
  2443
			updateActions();
insilmaril@0
  2444
		}	
insilmaril@0
  2445
	}
insilmaril@0
  2446
}
insilmaril@0
  2447
insilmaril@266
  2448
QString MapEditor::getURL()
insilmaril@266
  2449
{
insilmaril@266
  2450
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@266
  2451
			typeid(*selection) == typeid(MapCenterObj)) )
insilmaril@266
  2452
		return ((BranchObj*)selection)->getURL();
insilmaril@266
  2453
	else
insilmaril@266
  2454
		return "";
insilmaril@266
  2455
}
insilmaril@266
  2456
insilmaril@366
  2457
QStringList MapEditor::getURLs()
insilmaril@366
  2458
{
insilmaril@366
  2459
	QStringList urls;
insilmaril@366
  2460
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@366
  2461
			typeid(*selection) == typeid(MapCenterObj)) )
insilmaril@366
  2462
	{		
insilmaril@366
  2463
		BranchObj *bo=(BranchObj*)selection;
insilmaril@366
  2464
		bo=bo->first();	
insilmaril@366
  2465
		while (bo) 
insilmaril@366
  2466
		{
insilmaril@366
  2467
			if (!bo->getURL().isEmpty()) urls.append( bo->getURL());
insilmaril@366
  2468
			bo=bo->next();
insilmaril@366
  2469
		}	
insilmaril@366
  2470
	}	
insilmaril@366
  2471
	return urls;
insilmaril@366
  2472
}
insilmaril@366
  2473
insilmaril@366
  2474
insilmaril@0
  2475
void MapEditor::editHeading2URL()
insilmaril@0
  2476
{
insilmaril@0
  2477
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2478
			typeid(*selection) == typeid(MapCenterObj)) )
insilmaril@0
  2479
	{		
insilmaril@163
  2480
		BranchObj *bo=(BranchObj*)selection;
insilmaril@366
  2481
		saveStateConstSelection("setURL (\""+bo->getURL()+"\")","setURL (\""+bo->getHeading()+"\")",QString("Copy heading of %1 to URL").arg(getName(bo)));	
insilmaril@143
  2482
		bo->setURL (bo->getHeading());
insilmaril@0
  2483
		updateActions();
insilmaril@0
  2484
	}
insilmaril@0
  2485
}	
insilmaril@0
  2486
insilmaril@0
  2487
void MapEditor::editBugzilla2URL()
insilmaril@0
  2488
{
insilmaril@0
  2489
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2490
			typeid(*selection) == typeid(MapCenterObj)) )
insilmaril@0
  2491
	{		
insilmaril@163
  2492
		BranchObj *bo=(BranchObj*)selection;
insilmaril@143
  2493
		QString url= "https://bugzilla.novell.com/show_bug.cgi?id="+bo->getHeading();
insilmaril@366
  2494
		saveStateConstSelection("setURL (\""+bo->getURL()+"\")","setURL (\""+url+"\")",QString("Use heading of %1 as link to Novell Bugzilla").arg(getName(bo)));	
insilmaril@143
  2495
		bo->setURL (url);
insilmaril@0
  2496
		updateActions();
insilmaril@0
  2497
	}
insilmaril@0
  2498
}	
insilmaril@0
  2499
insilmaril@175
  2500
void MapEditor::editFATE2URL()
insilmaril@175
  2501
{
insilmaril@175
  2502
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@175
  2503
			typeid(*selection) == typeid(MapCenterObj)) )
insilmaril@175
  2504
	{		
insilmaril@175
  2505
		BranchObj *bo=(BranchObj*)selection;
insilmaril@175
  2506
		QString url= "http://keeper.suse.de:8080/webfate/match/id?value=ID"+bo->getHeading();
insilmaril@366
  2507
		saveStateConstSelection("setURL (\""+bo->getURL()+"\")","setURL (\""+url+"\")",QString("Use heading of %1 as link to FATE").arg(getName(bo)));	
insilmaril@175
  2508
		bo->setURL (url);
insilmaril@175
  2509
		updateActions();
insilmaril@175
  2510
	}
insilmaril@175
  2511
}	
insilmaril@175
  2512
insilmaril@0
  2513
void MapEditor::editVymLink()
insilmaril@0
  2514
{
insilmaril@0
  2515
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2516
			typeid(*selection) == typeid(MapCenterObj)) )
insilmaril@0
  2517
	{		
insilmaril@163
  2518
		BranchObj *bo=(BranchObj*)selection;
insilmaril@366
  2519
		Q3FileDialog *fd=new Q3FileDialog( this,__VYM " - " +tr("Link to another map"));
insilmaril@0
  2520
		fd->addFilter (QString (tr("vym map") + " (*.vym)"));
insilmaril@185
  2521
		fd->setCaption(__VYM " - " +tr("Link to another map"));
insilmaril@143
  2522
		if (! bo->getVymLink().isEmpty() )
insilmaril@143
  2523
			fd->setSelection( bo->getVymLink() );
insilmaril@0
  2524
		fd->show();
insilmaril@0
  2525
insilmaril@0
  2526
		QString fn;
insilmaril@0
  2527
		if ( fd->exec() == QDialog::Accepted )
insilmaril@143
  2528
		{
insilmaril@366
  2529
			saveStateConstSelection("setVymLink (\""+bo->getVymLink()+"\")","setVymLink (\""+fd->selectedFile()+"\")",QString("Set vymlink of %1 to %2").arg(getName(bo)).arg(fd->selectedFile()));	
insilmaril@143
  2530
			bo->setVymLink (fd->selectedFile() );
insilmaril@143
  2531
			updateActions();
insilmaril@143
  2532
			mapCenter->reposition();
insilmaril@143
  2533
			adjustCanvasSize();
insilmaril@143
  2534
			canvas()->update();
insilmaril@143
  2535
		}
insilmaril@0
  2536
	}
insilmaril@0
  2537
}
insilmaril@0
  2538
insilmaril@0
  2539
void MapEditor::deleteVymLink()
insilmaril@0
  2540
{
insilmaril@0
  2541
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2542
			typeid(*selection) == typeid(MapCenterObj)) )
insilmaril@0
  2543
	{		
insilmaril@163
  2544
		BranchObj *bo=(BranchObj*)selection;
insilmaril@366
  2545
		saveStateConstSelection("setVymLink (\""+bo->getVymLink()+"\")","setVymLink (\"\")",QString("Unset vymlink of %1").arg(getName(bo)));	
insilmaril@143
  2546
		bo->setVymLink ("" );
insilmaril@0
  2547
		updateActions();
insilmaril@0
  2548
		mapCenter->reposition();
insilmaril@0
  2549
		adjustCanvasSize();
insilmaril@0
  2550
		canvas()->update();
insilmaril@0
  2551
	}
insilmaril@0
  2552
}
insilmaril@0
  2553
insilmaril@243
  2554
void MapEditor::toggleHideExport()
insilmaril@243
  2555
{
insilmaril@243
  2556
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@250
  2557
			typeid(*selection) == typeid(MapCenterObj)) ||
insilmaril@250
  2558
			(typeid(*selection)==typeid(FloatImageObj))
insilmaril@243
  2559
			)
insilmaril@243
  2560
	{		
insilmaril@250
  2561
		OrnamentedObj *oo=(OrnamentedObj*)selection;
insilmaril@294
  2562
		QString s;
insilmaril@250
  2563
		if (oo->hideInExport())
insilmaril@294
  2564
		{
insilmaril@250
  2565
			oo->setHideInExport(false);
insilmaril@294
  2566
			s="Unset";
insilmaril@294
  2567
		}	
insilmaril@250
  2568
		else	
insilmaril@294
  2569
		{
insilmaril@250
  2570
			oo->setHideInExport(true);
insilmaril@294
  2571
			s="Set";
insilmaril@294
  2572
		}	
insilmaril@366
  2573
		saveStateComplete(QString ("%1 hide export flag of %2").arg(s).arg(getName(selection)));	//TODO undoCommand
insilmaril@250
  2574
		actionEditToggleHideExport->setOn (oo->hideInExport());	
insilmaril@243
  2575
		updateActions();
insilmaril@243
  2576
		mapCenter->reposition();
insilmaril@243
  2577
		adjustCanvasSize();
insilmaril@243
  2578
		canvas()->update();
insilmaril@243
  2579
	}
insilmaril@243
  2580
}
insilmaril@243
  2581
insilmaril@0
  2582
QString MapEditor::getVymLink()
insilmaril@0
  2583
{
insilmaril@0
  2584
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@0
  2585
			typeid(*selection) == typeid(MapCenterObj)) )
insilmaril@0
  2586
	{		
insilmaril@163
  2587
		return ((BranchObj*)selection)->getVymLink();
insilmaril@0
  2588
	}
insilmaril@0
  2589
	return "";
insilmaril@0
  2590
	
insilmaril@0
  2591
}
insilmaril@0
  2592
insilmaril@366
  2593
QStringList MapEditor::getVymLinks()
insilmaril@366
  2594
{
insilmaril@366
  2595
	QStringList links;
insilmaril@366
  2596
	if (selection && (typeid(*selection) == typeid(BranchObj) ||
insilmaril@366
  2597
			typeid(*selection) == typeid(MapCenterObj)) )
insilmaril@366
  2598
	{		
insilmaril@366
  2599
		BranchObj *bo=(BranchObj*)selection;
insilmaril@366
  2600
		bo=bo->first();	
insilmaril@366
  2601
		while (bo) 
insilmaril@366
  2602
		{
insilmaril@366
  2603
			if (!bo->getVymLink().isEmpty()) links.append( bo->getVymLink());
insilmaril@366
  2604
			bo=bo->next();
insilmaril@366
  2605
		}	
insilmaril@366
  2606
	}	
insilmaril@366
  2607
	return links;
insilmaril@366
  2608
}
insilmaril@366
  2609
insilmaril@366
  2610
insilmaril@294
  2611
void MapEditor::removeBranchKeepChilds()
insilmaril@98
  2612
{
insilmaril@98
  2613
	if (selection && (typeid(*selection) == typeid(BranchObj) ))
insilmaril@98
  2614
	{		
insilmaril@163
  2615
		BranchObj* bo=(BranchObj*)selection;
insilmaril@98
  2616
		BranchObj* par=(BranchObj*)(bo->getParObj());
insilmaril@294
  2617
		QString s=QString("Remove %1 and keep its childs").arg(getName(bo));
insilmaril@139
  2618
		if (bo->getDepth()==1)
insilmaril@366
  2619
			saveStateComplete(s);
insilmaril@139
  2620
		else	
insilmaril@366
  2621
			saveStatePart(selection->getParObj(),s);	// TODO undoCommand
insilmaril@139
  2622
		QString sel=selection->getSelectString();
insilmaril@98
  2623
		unselect();
insilmaril@98
  2624
		par->removeBranchHere(bo);
insilmaril@98
  2625
		mapCenter->reposition();
insilmaril@98
  2626
		select (sel);
insilmaril@98
  2627
	}	
insilmaril@98
  2628
}
insilmaril@98
  2629
insilmaril@98
  2630
void MapEditor::removeChilds()
insilmaril@98
  2631
{
insilmaril@98
  2632
	if (selection && (typeid(*selection) == typeid(BranchObj) ))
insilmaril@98
  2633
	{		
insilmaril@366
  2634
		saveStatePart(selection->getParObj(), QString("Remove childs of branch %1").arg(getName(selection)));
insilmaril@163
  2635
		((BranchObj*)selection)->removeChilds();
insilmaril@98
  2636
		mapCenter->reposition();
insilmaril@98
  2637
	}	
insilmaril@98
  2638
}
insilmaril@98
  2639
insilmaril@0
  2640
void MapEditor::editMapInfo()
insilmaril@0
  2641
{
insilmaril@0
  2642
	ExtraInfoDialog dia;
insilmaril@0
  2643
	dia.setMapName (getFileName() );
insilmaril@0
  2644
	dia.setAuthor (mapCenter->getAuthor() );
insilmaril@0
  2645
	dia.setComment(mapCenter->getComment() );
insilmaril@0
  2646
insilmaril@0
  2647
	// Calc some stats
insilmaril@0
  2648
	QString stats;
insilmaril@0
  2649
    int i=0;
insilmaril@366
  2650
    Q3CanvasItemList l=canvas()->allItems();
insilmaril@366
  2651
    for (Q3CanvasItemList::Iterator it=l.begin(); it!=l.end(); ++it) 
insilmaril@0
  2652
        i++;
insilmaril@0
  2653
    stats+=QString ("%1 items on canvas\n").arg (i,6);
insilmaril@0
  2654
insilmaril@0
  2655
	uint b=0;
insilmaril@0
  2656
	uint f=0;
insilmaril@0
  2657
	uint n=0;
insilmaril@103
  2658
	uint xl=0;
insilmaril@0
  2659
	BranchObj *bo;
insilmaril@0
  2660
	bo=mapCenter->first();
insilmaril@0
  2661
	while (bo) 
insilmaril@0
  2662
	{
insilmaril@0
  2663
		if (!bo->getNote().isEmpty() ) n++;
insilmaril@0
  2664
		f+= bo->countFloatImages();
insilmaril@0
  2665
		b++;
insilmaril@103
  2666
		xl+=bo->countXLinks();
insilmaril@0
  2667
		bo=bo->next();
insilmaril@0
  2668
	}
insilmaril@0
  2669
    stats+=QString ("%1 branches\n").arg (b-1,6);
insilmaril@104
  2670
    stats+=QString ("%1 xLinks \n").arg (xl,6);
insilmaril@0
  2671
    stats+=QString ("%1 notes\n").arg (n,6);
insilmaril@0
  2672
    stats+=QString ("%1 images\n").arg (f,6);
insilmaril@0
  2673
	dia.setStats (stats);
insilmaril@0
  2674
insilmaril@0
  2675
	// Finally show dialog
insilmaril@0
  2676
	if (dia.exec() == QDialog::Accepted)
insilmaril@0
  2677
	{
insilmaril@366
  2678
		saveStateComplete("Edit info about map");	//TODO undoCommand
insilmaril@0
  2679
		mapCenter->setAuthor (dia.getAuthor() );
insilmaril@0
  2680
		mapCenter->setComment (dia.getComment() );
insilmaril@0
  2681
	}
insilmaril@0
  2682
}
insilmaril@0
  2683
insilmaril@0
  2684
void MapEditor::updateActions()
insilmaril@0
  2685
{
insilmaril@98
  2686
	QAction *a;
insilmaril@0
  2687
	if (getLinkColorHint()==HeadingColor) 
insilmaril@0
  2688
		actionFormatLinkColorHint->setOn(true);
insilmaril@0
  2689
	else	
insilmaril@0
  2690
		actionFormatLinkColorHint->setOn(false);
insilmaril@0
  2691
insilmaril@0
  2692
	switch (linkstyle)
insilmaril@0
  2693
	{
insilmaril@0
  2694
		case StyleLine: 
insilmaril@0
  2695
			actionFormatLinkStyleLine->setOn(true);
insilmaril@0
  2696
			break;
insilmaril@0
  2697
		case StyleParabel:
insilmaril@0
  2698
			actionFormatLinkStyleParabel->setOn(true);
insilmaril@0
  2699
			break;
insilmaril@0
  2700
		case StylePolyLine:	
insilmaril@0
  2701
			actionFormatLinkStylePolyLine->setOn(true);
insilmaril@0
  2702
			break;
insilmaril@0
  2703
		case StylePolyParabel:	
insilmaril@0
  2704
			actionFormatLinkStylePolyParabel->setOn(true);
insilmaril@0
  2705
			break;
insilmaril@0
  2706
		default:
insilmaril@0
  2707
			break;
insilmaril@0
  2708
	}	
insilmaril@0
  2709
insilmaril@0
  2710
	QPixmap pix( 16, 16 );
insilmaril@0
  2711
    pix.fill( mapCanvas->backgroundColor() );
insilmaril@0
  2712
    actionFormatBackColor->setIconSet( pix );
insilmaril@97
  2713
    pix.fill( defLinkColor );
insilmaril@0
  2714
    actionFormatLinkColor->setIconSet( pix );
insilmaril@0
  2715
insilmaril@0
  2716
	actionFileSave->setEnabled( mapUnsaved );
insilmaril@366
  2717
	if (undoSet.readNumEntry("/history/undosAvail",0)>0)
insilmaril@366
  2718
		actionEditUndo->setEnabled( true);
insilmaril@366
  2719
	else	
insilmaril@366
  2720
		actionEditUndo->setEnabled( false);
insilmaril@366
  2721
insilmaril@366
  2722
	if (undoSet.readNumEntry("/history/redosAvail",0)>0)
insilmaril@366
  2723
		actionEditRedo->setEnabled( true);
insilmaril@366
  2724
	else	
insilmaril@366
  2725
		actionEditRedo->setEnabled( false);
insilmaril@0
  2726
insilmaril@0
  2727
	if (selection)
insilmaril@0
  2728
	{
insilmaril@0
  2729
		if ( (typeid(*selection) == typeid(BranchObj)) || 
insilmaril@0
  2730
			(typeid(*selection) == typeid(MapCenterObj))  )
insilmaril@0
  2731
		{
insilmaril@163
  2732
			BranchObj *bo=(BranchObj*)selection;
insilmaril@89
  2733
			// Take care of links
insilmaril@95
  2734
			if (bo->countXLinks()==0)
insilmaril@89
  2735
			{
insilmaril@366
  2736
				branchXLinksContextMenuEdit->clear();
insilmaril@366
  2737
				branchXLinksContextMenuFollow->clear();
insilmaril@89
  2738
			} else
insilmaril@89
  2739
			{
insilmaril@89
  2740
				BranchObj *bot;
insilmaril@89
  2741
				QString s;
insilmaril@366
  2742
				branchXLinksContextMenuEdit->clear();
insilmaril@366
  2743
				branchXLinksContextMenuFollow->clear();
insilmaril@95
  2744
				for (int i=0; i<=bo->countXLinks();i++)
insilmaril@89
  2745
				{
insilmaril@95
  2746
					bot=bo->XLinkTargetAt(i);
insilmaril@89
  2747
					if (bot)
insilmaril@89
  2748
					{
insilmaril@89
  2749
						s=bot->getHeading();
insilmaril@89
  2750
						if (s.length()>25)
insilmaril@89
  2751
							s=s.left(25)+"...";
insilmaril@366
  2752
						branchXLinksContextMenuFollow->addAction (s);
insilmaril@366
  2753
						branchXLinksContextMenuEdit->addAction (s);
insilmaril@89
  2754
					}	
insilmaril@89
  2755
				}
insilmaril@89
  2756
			}
insilmaril@89
  2757
insilmaril@0
  2758
			standardFlagsDefault->setEnabled (true);
insilmaril@0
  2759
insilmaril@250
  2760
			actionEditToggleScroll->setEnabled (true);
insilmaril@250
  2761
			if ( bo->isScrolled() )
insilmaril@250
  2762
				actionEditToggleScroll->setOn(true);
insilmaril@250
  2763
			else	
insilmaril@250
  2764
				actionEditToggleScroll->setOn(false);
insilmaril@250
  2765
insilmaril@89
  2766
			if ( bo->getURL().isEmpty() )
insilmaril@266
  2767
			{
insilmaril@0
  2768
				actionEditOpenURL->setEnabled (false);
insilmaril@266
  2769
				actionEditOpenURLTab->setEnabled (false);
insilmaril@266
  2770
			}	
insilmaril@0
  2771
			else	
insilmaril@266
  2772
			{
insilmaril@0
  2773
				actionEditOpenURL->setEnabled (true);
insilmaril@266
  2774
				actionEditOpenURLTab->setEnabled (true);
insilmaril@266
  2775
			}
insilmaril@89
  2776
			if ( bo->getVymLink().isEmpty() )
insilmaril@0
  2777
			{
insilmaril@0
  2778
				actionEditOpenVymLink->setEnabled (false);
insilmaril@0
  2779
				actionEditDeleteVymLink->setEnabled (false);
insilmaril@0
  2780
			} else	
insilmaril@0
  2781
			{
insilmaril@0
  2782
				actionEditOpenVymLink->setEnabled (true);
insilmaril@0
  2783
				actionEditDeleteVymLink->setEnabled (true);
insilmaril@0
  2784
			}	
insilmaril@314
  2785
insilmaril@314
  2786
			if (bo->canMoveBranchUp()) 
insilmaril@314
  2787
				actionEditMoveUp->setEnabled (true);
insilmaril@314
  2788
			else	
insilmaril@314
  2789
				actionEditMoveUp->setEnabled (false);
insilmaril@314
  2790
			if (bo->canMoveBranchDown()) 
insilmaril@314
  2791
				actionEditMoveDown->setEnabled (true);
insilmaril@314
  2792
			else	
insilmaril@314
  2793
				actionEditMoveDown->setEnabled (false);
insilmaril@314
  2794
insilmaril@314
  2795
insilmaril@243
  2796
			actionEditToggleHideExport->setEnabled (true);	
insilmaril@243
  2797
			actionEditToggleHideExport->setOn (bo->hideInExport() );	
insilmaril@0
  2798
insilmaril@0
  2799
			actionEditCopy->setEnabled (true);	
insilmaril@0
  2800
			actionEditCut->setEnabled (true);	
insilmaril@166
  2801
			if (!clipboardEmpty)
insilmaril@119
  2802
				actionEditPaste->setEnabled (true);	
insilmaril@119
  2803
			else	
insilmaril@119
  2804
				actionEditPaste->setEnabled (false);	
insilmaril@119
  2805
			for (a=actionListBranches.first();a;a=actionListBranches.next())
insilmaril@119
  2806
				a->setEnabled(true);
insilmaril@0
  2807
			actionEditDelete->setEnabled (true);
insilmaril@0
  2808
			switch (selection->getFrameType())
insilmaril@0
  2809
			{
insilmaril@0
  2810
				case NoFrame: 
insilmaril@0
  2811
					actionFormatFrameNone->setOn(true);
insilmaril@0
  2812
					break;
insilmaril@0
  2813
				case Rectangle:
insilmaril@0
  2814
					actionFormatFrameRectangle->setOn(true);
insilmaril@0
  2815
					break;
insilmaril@0
  2816
				default:
insilmaril@0
  2817
					break;
insilmaril@0
  2818
			}	
insilmaril@175
  2819
			actionFormatIncludeImagesVer->setOn
insilmaril@175
  2820
				( ((BranchObj*)selection)->getIncludeImagesVer());
insilmaril@175
  2821
			actionFormatIncludeImagesHor->setOn
insilmaril@175
  2822
				( ((BranchObj*)selection)->getIncludeImagesHor());
insilmaril@160
  2823
			actionFormatHideLinkUnselected->setOn
insilmaril@175
  2824
				(selection->getHideLinkUnselected());
insilmaril@0
  2825
		}
insilmaril@0
  2826
		if ( (typeid(*selection) == typeid(FloatImageObj)) )
insilmaril@0
  2827
		{
insilmaril@250
  2828
			FloatObj *fo=(FloatImageObj*)selection;
insilmaril@366
  2829
//FIXME do this in mainwindow					standardFlagsDefault->setEnabled (false);
insilmaril@0
  2830
insilmaril@0
  2831
			actionEditOpenURL->setEnabled (false);
insilmaril@0
  2832
			actionEditOpenVymLink->setEnabled (false);
insilmaril@0
  2833
			actionEditDeleteVymLink->setEnabled (false);	
insilmaril@250
  2834
			actionEditToggleHideExport->setEnabled (true);	
insilmaril@250
  2835
			actionEditToggleHideExport->setOn (fo->hideInExport() );	
insilmaril@250
  2836
insilmaril@0
  2837
insilmaril@0
  2838
			actionEditCopy->setEnabled (true);
insilmaril@0
  2839
			actionEditCut->setEnabled (true);	
insilmaril@243
  2840
			actionEditPaste->setEnabled (false);
insilmaril@98
  2841
			for (a=actionListBranches.first();a;a=actionListBranches.next())
insilmaril@98
  2842
				a->setEnabled(false);
insilmaril@0
  2843
			actionEditDelete->setEnabled (true);
insilmaril@160
  2844
			actionFormatHideLinkUnselected->setOn
insilmaril@160
  2845
				( selection->getHideLinkUnselected());
insilmaril@314
  2846
			actionEditMoveUp->setEnabled (false);
insilmaril@314
  2847
			actionEditMoveDown->setEnabled (false);
insilmaril@0
  2848
		}
insilmaril@0
  2849
insilmaril@0
  2850
	} else
insilmaril@0
  2851
	{
insilmaril@366
  2852
//FIXME do this in mainwindow				standardFlagsDefault->setEnabled (false);
insilmaril@0
  2853
insilmaril@0
  2854
		actionEditCopy->setEnabled (false);	
insilmaril@0
  2855
		actionEditCut->setEnabled (false);	
insilmaril@0
  2856
		actionEditPaste->setEnabled (false);	
insilmaril@98
  2857
		for (a=actionListBranches.first();a;a=actionListBranches.next())
insilmaril@98
  2858
			a->setEnabled(false);
insilmaril@98
  2859
insilmaril@314
  2860
		actionEditToggleScroll->setEnabled (false);
insilmaril@0
  2861
		actionEditOpenURL->setEnabled (false);
insilmaril@0
  2862
		actionEditOpenVymLink->setEnabled (false);
insilmaril@0
  2863
		actionEditDeleteVymLink->setEnabled (false);	
insilmaril@0
  2864
		actionEditHeading2URL->setEnabled (false);	
insilmaril@0
  2865
		actionEditDelete->setEnabled (false);
insilmaril@314
  2866
		actionEditMoveUp->setEnabled (false);
insilmaril@314
  2867
		actionEditMoveDown->setEnabled (false);
insilmaril@314
  2868
		actionEditToggleHideExport->setEnabled (false);	
insilmaril@0
  2869
	}	
insilmaril@0
  2870
}
insilmaril@0
  2871
insilmaril@160
  2872
void MapEditor::updateNoteFlag()
insilmaril@160
  2873
{
insilmaril@160
  2874
	if (selection)
insilmaril@160
  2875
		if ( (typeid(*selection) == typeid(BranchObj)) || 
insilmaril@160
  2876
			(typeid(*selection) == typeid(MapCenterObj))  )
insilmaril@160
  2877
			((BranchObj*)selection)->updateNoteFlag();
insilmaril@160
  2878
}
insilmaril@160
  2879
insilmaril@0
  2880
void MapEditor::setLinkStyle (LinkStyle ls)
insilmaril@0
  2881
{
insilmaril@0
  2882
	linkstyle=ls;
insilmaril@0
  2883
insilmaril@366
  2884
	saveStateComplete("Set link style");	// TODO undoCommand
insilmaril@0
  2885
	BranchObj *bo;
insilmaril@0
  2886
	bo=mapCenter->first();
insilmaril@0
  2887
	bo=bo->next();
insilmaril@0
  2888
	while (bo) 
insilmaril@0
  2889
	{
insilmaril@0
  2890
		bo->setLinkStyle(bo->getDefLinkStyle());
insilmaril@0
  2891
		bo=bo->next();
insilmaril@0
  2892
	}
insilmaril@102
  2893
	mapCenter->reposition();
insilmaril@0
  2894
}
insilmaril@0
  2895
insilmaril@0
  2896
LinkStyle MapEditor::getLinkStyle ()
insilmaril@0
  2897
{
insilmaril@0
  2898
	return linkstyle;
insilmaril@0
  2899
}	
insilmaril@0
  2900
insilmaril@0
  2901
void MapEditor::setLinkColor(QColor c)
insilmaril@0
  2902
{
insilmaril@97
  2903
	defLinkColor=c;
insilmaril@0
  2904
	updateActions();
insilmaril@0
  2905
}
insilmaril@0
  2906
insilmaril@0
  2907
void MapEditor::setLinkColorHint()
insilmaril@0
  2908
{
insilmaril@0
  2909
	// called from setLinkColorHint(lch) or at end of parse
insilmaril@0
  2910
	BranchObj *bo;
insilmaril@0
  2911
	bo=mapCenter->first();
insilmaril@0
  2912
	while (bo) 
insilmaril@0
  2913
	{
insilmaril@0
  2914
		bo->setLinkColor();
insilmaril@0
  2915
		bo=bo->next();
insilmaril@0
  2916
	}
insilmaril@0
  2917
}
insilmaril@0
  2918
insilmaril@0
  2919
void MapEditor::setLinkColorHint(LinkColorHint lch)
insilmaril@0
  2920
{
insilmaril@0
  2921
	linkcolorhint=lch;
insilmaril@0
  2922
	setLinkColorHint();
insilmaril@0
  2923
}
insilmaril@0
  2924
insilmaril@0
  2925
void MapEditor::toggleLinkColorHint()
insilmaril@0
  2926
{
insilmaril@0
  2927
	if (linkcolorhint==HeadingColor)
insilmaril@0
  2928
		linkcolorhint=DefaultColor;
insilmaril@0
  2929
	else	
insilmaril@0
  2930
		linkcolorhint=HeadingColor;
insilmaril@0
  2931
	BranchObj *bo;
insilmaril@0
  2932
	bo=mapCenter->first();
insilmaril@0
  2933
	while (bo) 
insilmaril@0
  2934
	{
insilmaril@0
  2935
		bo->setLinkColor();
insilmaril@0
  2936
		bo=bo->next();
insilmaril@0
  2937
	}
insilmaril@0
  2938
}
insilmaril@0
  2939
insilmaril@0
  2940
LinkColorHint MapEditor::getLinkColorHint()
insilmaril@0
  2941
{
insilmaril@0
  2942
	return linkcolorhint;
insilmaril@0
  2943
}
insilmaril@0
  2944
insilmaril@0
  2945
QColor MapEditor::getDefLinkColor()
insilmaril@0
  2946
{
insilmaril@97
  2947
	return defLinkColor;
insilmaril@97
  2948
}
insilmaril@97
  2949
insilmaril@97
  2950
void MapEditor::setDefXLinkColor(QColor col)
insilmaril@97
  2951
{
insilmaril@97
  2952
	defXLinkColor=col;
insilmaril@97
  2953
}
insilmaril@97
  2954
insilmaril@97
  2955
QColor MapEditor::getDefXLinkColor()
insilmaril@97
  2956
{
insilmaril@97
  2957
	return defXLinkColor;
insilmaril@97
  2958
}
insilmaril@97
  2959
insilmaril@97
  2960
void MapEditor::setDefXLinkWidth (int w)
insilmaril@97
  2961
{
insilmaril@97
  2962
	defXLinkWidth=w;
insilmaril@97
  2963
}
insilmaril@97
  2964
insilmaril@97
  2965
int MapEditor::getDefXLinkWidth()
insilmaril@97
  2966
{
insilmaril@97
  2967
	return defXLinkWidth;
insilmaril@0
  2968
}
insilmaril@0
  2969
insilmaril@0
  2970
void MapEditor::selectLinkColor()
insilmaril@0
  2971
{
insilmaril@97
  2972
	QColor col = QColorDialog::getColor( defLinkColor, this );
insilmaril@0
  2973
	if ( !col.isValid() ) return;
insilmaril@0
  2974
	setLinkColor( col );
insilmaril@366
  2975
	saveStateComplete(QString("Set link color to %1").arg(col.name()));	//TODO undoCommand
insilmaril@131
  2976
insilmaril@0
  2977
}
insilmaril@0
  2978
insilmaril@0
  2979
void MapEditor::toggleScroll()
insilmaril@0
  2980
{
insilmaril@0
  2981
	if (selection && (typeid(*selection) == typeid(BranchObj)) )
insilmaril@0
  2982
	{
insilmaril@163
  2983
		BranchObj *bo=((BranchObj*)selection);
insilmaril@0
  2984
		if (bo->countBranches()==0) return;
insilmaril@0
  2985
		if (bo->getDepth()==0) return;
insilmaril@294
  2986
		QString s;
insilmaril@294
  2987
		if (bo->isScrolled())
insilmaril@294
  2988
			s="Unscroll";
insilmaril@294
  2989
		else	
insilmaril@294
  2990
			s="Scroll";
insilmaril@366
  2991
		saveStatePart(selection, QString ("%1 %2").arg(s).arg(getName(bo)));
insilmaril@0
  2992
		bo->toggleScroll();
insilmaril@0
  2993
		adjustCanvasSize();
insilmaril@0
  2994
		canvas()->update();
insilmaril@0
  2995
	}
insilmaril@0
  2996
}
insilmaril@0
  2997
insilmaril@0
  2998
void MapEditor::unScrollAll()
insilmaril@0
  2999
{
insilmaril@0
  3000
	BranchObj *bo;
insilmaril@0
  3001
	bo=mapCenter->first();
insilmaril@0
  3002
	while (bo) 
insilmaril@0
  3003
	{
insilmaril@0
  3004
		if (bo->isScrolled()) bo->toggleScroll();
insilmaril@0
  3005
		bo=bo->next();
insilmaril@0
  3006
	}
insilmaril@0
  3007
}
insilmaril@0
  3008
insilmaril@0
  3009
void MapEditor::loadFloatImage ()
insilmaril@0
  3010
{
insilmaril@0
  3011
	if (selection && 
insilmaril@0
  3012
		(typeid(*selection) == typeid(BranchObj)) || 
insilmaril@0
  3013
		(typeid(*selection) == typeid(MapCenterObj))  )
insilmaril@0
  3014
	{
insilmaril@163
  3015
		BranchObj *bo=((BranchObj*)selection);
insilmaril@0
  3016
insilmaril@366
  3017
		Q3FileDialog *fd=new Q3FileDialog( this);
insilmaril@366
  3018
		fd->setMode (Q3FileDialog::ExistingFiles);
insilmaril@0
  3019
		fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
insilmaril@0
  3020
		ImagePreview *p =new ImagePreview (fd);
insilmaril@0
  3021
		fd->setContentsPreviewEnabled( TRUE );
insilmaril@0
  3022
		fd->setContentsPreview( p, p );
insilmaril@366
  3023
		fd->setPreviewMode( Q3FileDialog::Contents );
insilmaril@185
  3024
		fd->setCaption(__VYM " - " +tr("Load image"));
insilmaril@0
  3025
		fd->setDir (lastImageDir);
insilmaril@0
  3026
		fd->show();
insilmaril@0
  3027
insilmaril@0
  3028
		QString fn;
insilmaril@0
  3029
		if ( fd->exec() == QDialog::Accepted )
insilmaril@0
  3030
		{
insilmaril@366
  3031
			saveStatePart(selection, QString("Add floatimage to %1").arg(getName(selection)));
insilmaril@366
  3032
			// FIXME in QT4 use:	lastImageDir=fd->directory();
insilmaril@366
  3033
			lastImageDir=QDir (fd->dirPath());
insilmaril@185
  3034
			QStringList flist = fd->selectedFiles();
insilmaril@185
  3035
			QStringList::Iterator it = flist.begin();
insilmaril@185
  3036
			while( it != flist.end() ) 
insilmaril@185
  3037
			{
insilmaril@185
  3038
				fn = *it;
insilmaril@185
  3039
				bo->addFloatImage();
insilmaril@185
  3040
				// TODO check if load was successful
insilmaril@185
  3041
				bo->getLastFloatImage()->load(*it);
insilmaril@185
  3042
				bo->getLastFloatImage()->setOriginalFilename(fn);
insilmaril@185
  3043
				++it;
insilmaril@185
  3044
			}
insilmaril@185
  3045
insilmaril@0
  3046
			mapCenter->reposition();
insilmaril@0
  3047
			adjustCanvasSize();
insilmaril@0
  3048
			canvas()->update();
insilmaril@0
  3049
		}
insilmaril@236
  3050
		delete (p);
insilmaril@236
  3051
		delete (fd);
insilmaril@0
  3052
	}
insilmaril@0
  3053
}
insilmaril@0
  3054
insilmaril@366
  3055
void MapEditor::saveFloatImage ()
insilmaril@0
  3056
{
insilmaril@0
  3057
	if (selection && 
insilmaril@0
  3058
		(typeid(*selection) == typeid(FloatImageObj)) )
insilmaril@0
  3059
	{
insilmaril@163
  3060
		FloatImageObj *fio=((FloatImageObj*)selection);
insilmaril@366
  3061
		QFileDialog *fd=new QFileDialog( this);
insilmaril@366
  3062
		fd->setFilters (imageIO.getFilters());
insilmaril@366
  3063
		fd->setCaption(__VYM " - " +tr("Save image"));
insilmaril@366
  3064
		fd->setFileMode( QFileDialog::AnyFile );
insilmaril@366
  3065
		fd->setDirectory (lastImageDir);
insilmaril@366
  3066
//		fd->setSelection (fio->getOriginalFilename());
insilmaril@0
  3067
		fd->show();
insilmaril@0
  3068
insilmaril@0
  3069
		QString fn;
insilmaril@0
  3070
		if ( fd->exec() == QDialog::Accepted )
insilmaril@0
  3071
		{
insilmaril@0
  3072
			if (QFile (fd->selectedFile()).exists() )
insilmaril@0
  3073
			{
insilmaril@185
  3074
				QMessageBox mb( __VYM,
insilmaril@159
  3075
					tr("The file %1 exists already.\n"
insilmaril@159
  3076
					"Do you want to overwrite it?").arg(fd->selectedFile()),
insilmaril@0
  3077
				QMessageBox::Warning,
insilmaril@0
  3078
				QMessageBox::Yes | QMessageBox::Default,
insilmaril@0
  3079
				QMessageBox::Cancel | QMessageBox::Escape,
insilmaril@0
  3080
				QMessageBox::QMessageBox::NoButton );
insilmaril@0
  3081
insilmaril@0
  3082
				mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
insilmaril@0
  3083
				mb.setButtonText( QMessageBox::No, tr("Cancel"));
insilmaril@0
  3084
				switch( mb.exec() ) 
insilmaril@0
  3085
				{
insilmaril@0
  3086
					case QMessageBox::Yes:
insilmaril@0
  3087
						// save 
insilmaril@0
  3088
						break;;
insilmaril@0
  3089
					case QMessageBox::Cancel:
insilmaril@0
  3090
						// do nothing
insilmaril@377
  3091
						delete (fd);
insilmaril@0
  3092
						return;
insilmaril@0
  3093
						break;
insilmaril@0
  3094
				}
insilmaril@0
  3095
			}
insilmaril@366
  3096
			fio->save (fd->selectedFile(),imageIO.getType (fd->selectedFilter() ) );
insilmaril@0
  3097
		}
insilmaril@377
  3098
		delete (fd);
insilmaril@0
  3099
	}
insilmaril@0
  3100
}
insilmaril@0
  3101
insilmaril@0
  3102
void MapEditor::setFrame(const FrameType &t)
insilmaril@0
  3103
{
insilmaril@0
  3104
	if (selection && 
insilmaril@0
  3105
		(typeid(*selection) == typeid(BranchObj)) || 
insilmaril@0
  3106
		(typeid(*selection) == typeid(MapCenterObj))  )
insilmaril@0
  3107
	{
insilmaril@0
  3108
		selection->setFrameType (t);
insilmaril@0
  3109
		mapCenter->reposition();
insilmaril@0
  3110
		selection->updateLink();
insilmaril@0
  3111
	}
insilmaril@0
  3112
}
insilmaril@0
  3113
insilmaril@175
  3114
void MapEditor::setIncludeImagesVer(bool b)
insilmaril@175
  3115
{
insilmaril@175
  3116
	if (selection && 
insilmaril@175
  3117
		(typeid(*selection) == typeid(BranchObj)) || 
insilmaril@175
  3118
		(typeid(*selection) == typeid(MapCenterObj))  )
insilmaril@175
  3119
		((BranchObj*)selection)->setIncludeImagesVer(b);
insilmaril@175
  3120
		mapCenter->reposition();
insilmaril@175
  3121
}
insilmaril@175
  3122
insilmaril@175
  3123
void MapEditor::setIncludeImagesHor(bool b)
insilmaril@175
  3124
{
insilmaril@175
  3125
	if (selection && 
insilmaril@175
  3126
		(typeid(*selection) == typeid(BranchObj)) || 
insilmaril@175
  3127
		(typeid(*selection) == typeid(MapCenterObj))  )
insilmaril@175
  3128
		((BranchObj*)selection)->setIncludeImagesHor(b);
insilmaril@175
  3129
		mapCenter->reposition();
insilmaril@175
  3130
}
insilmaril@175
  3131
insilmaril@160
  3132
void MapEditor::setHideLinkUnselected (bool b)
insilmaril@160
  3133
{
insilmaril@160
  3134
	if (selection && 
insilmaril@160
  3135
		(typeid(*selection) == typeid(BranchObj)) || 
insilmaril@160
  3136
		(typeid(*selection) == typeid(MapCenterObj))  ||
insilmaril@160
  3137
		(typeid(*selection) == typeid(FloatImageObj)) )
insilmaril@160
  3138
		selection->setHideLinkUnselected(b);
insilmaril@160
  3139
}
insilmaril@160
  3140
insilmaril@366
  3141
void MapEditor::importDirInt(BranchObj *dst, QDir d)
insilmaril@0
  3142
{
insilmaril@0
  3143
	if (selection && 
insilmaril@0
  3144
		(typeid(*selection) == typeid(BranchObj)) || 
insilmaril@0
  3145
		(typeid(*selection) == typeid(MapCenterObj))  )
insilmaril@0
  3146
	{
insilmaril@0
  3147
		BranchObj *bo;
insilmaril@0
  3148
		
insilmaril@0
  3149
		// Traverse directories
insilmaril@0
  3150
		d.setFilter( QDir::Dirs| QDir::Hidden | QDir::NoSymLinks );
insilmaril@366
  3151
		QFileInfoList list = d.entryInfoList();
insilmaril@366
  3152
		QFileInfo fi;
insilmaril@366
  3153
insilmaril@366
  3154
		for (int i = 0; i < list.size(); ++i) 
insilmaril@0
  3155
		{
insilmaril@366
  3156
			fi=list.at(i);
insilmaril@366
  3157
			if (fi.fileName() != "." && fi.fileName() != ".." )
insilmaril@0
  3158
			{
insilmaril@0
  3159
				dst->addBranch();
insilmaril@0
  3160
				bo=dst->getLastBranch();
insilmaril@366
  3161
				bo->setHeading (fi.fileName() );
insilmaril@243
  3162
				bo->setColor (QColor("blue"));
insilmaril@0
  3163
				bo->toggleScroll();
insilmaril@366
  3164
				if ( !d.cd(fi.fileName()) ) 
insilmaril@366
  3165
					QMessageBox::critical (0,tr("Critical Import Error"),tr("Cannot find the directory %1").arg(fi.fileName()));
insilmaril@0
  3166
				else 
insilmaril@0
  3167
				{
insilmaril@103
  3168
					// Recursively add subdirs
insilmaril@366
  3169
					importDirInt (bo,d);
insilmaril@0
  3170
					d.cdUp();
insilmaril@0
  3171
				}
insilmaril@0
  3172
			}	
insilmaril@0
  3173
		}		
insilmaril@0
  3174
		// Traverse files
insilmaril@0
  3175
		d.setFilter( QDir::Files| QDir::Hidden | QDir::NoSymLinks );
insilmaril@366
  3176
		list = d.entryInfoList();
insilmaril@366
  3177
insilmaril@366
  3178
		for (int i = 0; i < list.size(); ++i) 
insilmaril@0
  3179
		{
insilmaril@366
  3180
			fi=list.at(i);
insilmaril@0
  3181
			dst->addBranch();
insilmaril@0
  3182
			bo=dst->getLastBranch();
insilmaril@366
  3183
			bo->setHeading (fi.fileName() );
insilmaril@243
  3184
			bo->setColor (QColor("black"));
insilmaril@366
  3185
			if (fi.fileName().right(4) == ".vym" )
insilmaril@366
  3186
				bo->setVymLink (fi.filePath());
insilmaril@0
  3187
		}	
insilmaril@0
  3188
	}		
insilmaril@0
  3189
}
insilmaril@0
  3190
insilmaril@0
  3191
void MapEditor::importDir()
insilmaril@0
  3192
{
insilmaril@0
  3193
	if (selection && 
insilmaril@0
  3194
		(typeid(*selection) == typeid(BranchObj)) || 
insilmaril@0
  3195
		(typeid(*selection) == typeid(MapCenterObj))  )
insilmaril@0
  3196
	{
insilmaril@366
  3197
		Q3FileDialog *fd=new Q3FileDialog( this,__VYM " - " +tr("Choose directory structure to import"));
insilmaril@366
  3198
		fd->setMode (Q3FileDialog::DirectoryOnly);
insilmaril@0
  3199
		fd->addFilter (QString (tr("vym map") + " (*.vym)"));
insilmaril@185
  3200
		fd->setCaption(__VYM " - " +tr("Choose directory structure to import"));
insilmaril@0
  3201
		fd->show();
insilmaril@0
  3202
insilmaril@0
  3203
		QString fn;
insilmaril@0
  3204
		if ( fd->exec() == QDialog::Accepted )
insilmaril@0
  3205
		{
insilmaril@163
  3206
			BranchObj *bo=((BranchObj*)selection);
insilmaril@366
  3207
			importDirInt (bo,QDir(fd->selectedFile()) );
insilmaril@0
  3208
			mapCenter->reposition();
insilmaril@0
  3209
			adjustCanvasSize();
insilmaril@0
  3210
			canvas()->update();
insilmaril@0
  3211
		}
insilmaril@0
  3212
	}	
insilmaril@0
  3213
}
insilmaril@0
  3214
insilmaril@95
  3215
void MapEditor::followXLink(int i)
insilmaril@89
  3216
{
insilmaril@97
  3217
	if (selection && 
insilmaril@97
  3218
		(typeid(*selection) == typeid(BranchObj)) || 
insilmaril@97
  3219
		(typeid(*selection) == typeid(MapCenterObj))  )
insilmaril@89
  3220
	{
insilmaril@163
  3221
		BranchObj *bo=((BranchObj*)selection)->XLinkTargetAt(i);
insilmaril@97
  3222
		if (bo) 
insilmaril@97
  3223
		{
insilmaril@97
  3224
			selection->unselect();
insilmaril@97
  3225
			selection=bo;
insilmaril@97
  3226
			selection->select();
insilmaril@97
  3227
			ensureSelectionVisible();
insilmaril@97
  3228
		}
insilmaril@89
  3229
	}
insilmaril@89
  3230
}
insilmaril@89
  3231
insilmaril@95
  3232
void MapEditor::editXLink(int i)
insilmaril@94
  3233
{
insilmaril@97
  3234
	if (selection && 
insilmaril@97
  3235
		(typeid(*selection) == typeid(BranchObj)) || 
insilmaril@97
  3236
		(typeid(*selection) == typeid(MapCenterObj))  )
insilmaril@94
  3237
	{
insilmaril@163
  3238
		XLinkObj *xlo=((BranchObj*)selection)->XLinkAt(i);
insilmaril@97
  3239
		if (xlo) 
insilmaril@94
  3240
		{
insilmaril@97
  3241
			EditXLinkDialog dia;
insilmaril@97
  3242
			dia.setXLink (xlo);
insilmaril@102
  3243
			dia.setSelection(selection);
insilmaril@97
  3244
			if (dia.exec() == QDialog::Accepted)
insilmaril@97
  3245
			{
insilmaril@97
  3246
				if (dia.useSettingsGlobal() )
insilmaril@97
  3247
				{
insilmaril@97
  3248
					setDefXLinkColor (xlo->getColor() );
insilmaril@97
  3249
					setDefXLinkWidth (xlo->getWidth() );
insilmaril@97
  3250
				}
insilmaril@97
  3251
				if (dia.deleteXLink())
insilmaril@163
  3252
					((BranchObj*)selection)->deleteXLinkAt(i);
insilmaril@366
  3253
				saveStateComplete("Edit xLink");	//TODO undoCommand
insilmaril@97
  3254
			}
insilmaril@97
  3255
		}	
insilmaril@97
  3256
	}
insilmaril@94
  3257
}
insilmaril@94
  3258
insilmaril@0
  3259
void MapEditor::testFunction()
insilmaril@0
  3260
{
insilmaril@0
  3261
	cout << "MapEditor::testFunction() called\n";
insilmaril@314
  3262
insilmaril@314
  3263
	WarningDialog dia;
insilmaril@388
  3264
	dia.showCancelButton (true);
insilmaril@314
  3265
	dia.setText("This is a longer \nWarning");
insilmaril@388
  3266
	/*
insilmaril@314
  3267
	dia.setCaption("Warning: Flux problem");
insilmaril@366
  3268
	dia.setShowAgainName("/warnings/mapeditor");
insilmaril@388
  3269
	*/
insilmaril@314
  3270
	if (dia.exec()==QDialog::Accepted)
insilmaril@314
  3271
		cout << "accepted!\n";
insilmaril@314
  3272
	else	
insilmaril@314
  3273
		cout << "canceled!\n";
insilmaril@301
  3274
	return;
insilmaril@366
  3275
insilmaril@366
  3276
/* Hide hidden stuff temporary, maybe add this as regular function somewhere
insilmaril@260
  3277
	if (hidemode==HideNone)
insilmaril@185
  3278
	{
insilmaril@260
  3279
		setHideTmpMode (HideExport);
insilmaril@260
  3280
		mapCenter->calcBBoxSizeWithChilds();
insilmaril@260
  3281
		QRect totalBBox=mapCenter->getTotalBBox();
insilmaril@260
  3282
		QRect mapRect=totalBBox;
insilmaril@260
  3283
		QCanvasRectangle *frame=NULL;
insilmaril@260
  3284
insilmaril@260
  3285
		cout << "  map has =("<<totalBBox.x()<<","<<totalBBox.y()<<","<<totalBBox.width()<<","<<totalBBox.height()<<")\n";
insilmaril@260
  3286
	
insilmaril@260
  3287
		mapRect.setRect (totalBBox.x(), totalBBox.y(), 
insilmaril@260
  3288
			totalBBox.width(), totalBBox.height());
insilmaril@260
  3289
		frame=new QCanvasRectangle (mapRect,mapCanvas);
insilmaril@260
  3290
		frame->setBrush (QColor(white));
insilmaril@260
  3291
		frame->setPen (QColor(black));
insilmaril@260
  3292
		frame->setZ(0);
insilmaril@260
  3293
		frame->show();    
insilmaril@260
  3294
	}	
insilmaril@260
  3295
	else	
insilmaril@260
  3296
	{
insilmaril@260
  3297
		setHideTmpMode (HideNone);
insilmaril@260
  3298
	}	
insilmaril@260
  3299
	cout <<"  hidemode="<<hidemode<<endl;
insilmaril@301
  3300
	*/
insilmaril@105
  3301
}
insilmaril@105
  3302
insilmaril@0
  3303
void MapEditor::ensureSelectionVisible()
insilmaril@0
  3304
{
insilmaril@98
  3305
	if (selection)
insilmaril@98
  3306
	{
insilmaril@98
  3307
		LinkableMapObj* lmo= dynamic_cast <LinkableMapObj*> (selection);
insilmaril@98
  3308
		QPoint p;
insilmaril@98
  3309
		if (selection->getOrientation() == OrientLeftOfCenter)
insilmaril@98
  3310
			p= worldMatrix().map(QPoint (lmo->x(),lmo->y()));
insilmaril@98
  3311
		else	
insilmaril@98
  3312
			p= worldMatrix().map(QPoint (lmo->x()+lmo->width(),lmo->y()+lmo->height()));
insilmaril@98
  3313
		ensureVisible (p.x(), p.y() );
insilmaril@98
  3314
	}
insilmaril@0
  3315
insilmaril@0
  3316
}
insilmaril@0
  3317
insilmaril@0
  3318
void MapEditor::updateViewCenter()
insilmaril@0
  3319
{
insilmaril@0
  3320
	// Update movingCenter, so that we can zoom comfortably later
insilmaril@0
  3321
	QRect rc = QRect( contentsX(), contentsY(),
insilmaril@0
  3322
				  visibleWidth(), visibleHeight() );
insilmaril@0
  3323
	QRect canvasRect = inverseWorldMatrix().mapRect(rc);
insilmaril@0
  3324
	movingCenter.setX((canvasRect.right() + canvasRect.left())/2);
insilmaril@0
  3325
	movingCenter.setY((canvasRect.top() + canvasRect.bottom())/2);
insilmaril@0
  3326
}
insilmaril@0
  3327
insilmaril@0
  3328
void MapEditor::contentsContextMenuEvent ( QContextMenuEvent * e )
insilmaril@0
  3329
{
insilmaril@0
  3330
	// Lineedits are already closed by preceding
insilmaril@0
  3331
	// mouseEvent, we don't need to close here.
insilmaril@0
  3332
insilmaril@0
  3333
    QPoint p = inverseWorldMatrix().map(e->pos());
insilmaril@0
  3334
    LinkableMapObj* lmo=mapCenter->findMapObj(p, NULL);
insilmaril@0
  3335
	
insilmaril@0
  3336
    if (lmo) 
insilmaril@0
  3337
	{	// MapObj was found
insilmaril@0
  3338
		if (selection != lmo)
insilmaril@0
  3339
		{
insilmaril@0
  3340
			// select the MapObj
insilmaril@0
  3341
			if (selection) selection->unselect();
insilmaril@0
  3342
			selection=lmo;
insilmaril@0
  3343
			selection->select();
insilmaril@0
  3344
			adjustCanvasSize();
insilmaril@0
  3345
		}
insilmaril@0
  3346
		// Context Menu 
insilmaril@0
  3347
		if (selection) 
insilmaril@0
  3348
		{
insilmaril@0
  3349
			if (typeid(*selection)==typeid(BranchObj) ||
insilmaril@0
  3350
				typeid(*selection)==typeid(MapCenterObj) )
insilmaril@0
  3351
			{
insilmaril@0
  3352
				// Context Menu on branch or mapcenter
insilmaril@0
  3353
				updateActions();
insilmaril@0
  3354
				branchContextMenu->popup(e->globalPos() );
insilmaril@0
  3355
			}	
insilmaril@0
  3356
			if (typeid(*selection)==typeid(FloatImageObj))
insilmaril@0
  3357
			{
insilmaril@0
  3358
				// Context Menu on floatimage
insilmaril@0
  3359
				updateActions();
insilmaril@0
  3360
				floatimageContextMenu->popup(e->globalPos() );
insilmaril@0
  3361
			}	
insilmaril@0
  3362
		}	
insilmaril@0
  3363
	} else 
insilmaril@0
  3364
	{ // No MapObj found, we are on the Canvas itself
insilmaril@0
  3365
		// Context Menu on Canvas
insilmaril@0
  3366
		updateActions();
insilmaril@0
  3367
		canvasContextMenu->popup(e->globalPos() );
insilmaril@0
  3368
    } 
insilmaril@329
  3369
	e->accept();
insilmaril@0
  3370
}
insilmaril@0
  3371
insilmaril@0
  3372
void MapEditor::contentsMousePressEvent(QMouseEvent* e)
insilmaril@0
  3373
{
insilmaril@329
  3374
	// Ignore right clicks, these will go to context menus
insilmaril@366
  3375
	if (e->button() == Qt::RightButton )
insilmaril@329
  3376
	{
insilmaril@329
  3377
		e->ignore();
insilmaril@329
  3378
		return;
insilmaril@329
  3379
	}
insilmaril@329
  3380
insilmaril@0
  3381
    QPoint p = inverseWorldMatrix().map(e->pos());
insilmaril@0
  3382
    LinkableMapObj* lmo=mapCenter->findMapObj(p, NULL);
insilmaril@0
  3383
	
insilmaril@329
  3384
	e->accept();
insilmaril@329
  3385
insilmaril@267
  3386
	//Take care of clickdesystem flags _or_ modifier modes
insilmaril@267
  3387
	//
insilmaril@267
  3388
	if (lmo && (typeid(*lmo)==typeid(BranchObj) ||
insilmaril@267
  3389
		typeid(*lmo)==typeid(MapCenterObj) ))
insilmaril@267
  3390
	{
insilmaril@267
  3391
		QString foname=((BranchObj*)lmo)->getSystemFlagName(p);
insilmaril@267
  3392
		if (!foname.isEmpty())
insilmaril@267
  3393
		{
insilmaril@267
  3394
			// systemFlag clicked
insilmaril@366
  3395
			selectInt (lmo);
insilmaril@267
  3396
			if (foname=="url") 
insilmaril@267
  3397
			{
insilmaril@366
  3398
				if (e->state() & Qt::ControlModifier)
insilmaril@267
  3399
					mainWindow->editOpenURLTab();
insilmaril@267
  3400
				else	
insilmaril@267
  3401
					mainWindow->editOpenURL();
insilmaril@267
  3402
			}	
insilmaril@267
  3403
			else if (foname=="vymLink")
insilmaril@267
  3404
			{
insilmaril@267
  3405
				mainWindow->editOpenVymLink();
insilmaril@267
  3406
				// tabWidget may change, better return now
insilmaril@267
  3407
				// before segfaulting...
insilmaril@267
  3408
			} else if (foname=="note")
insilmaril@267
  3409
				mainWindow->windowToggleNoteEditor();
insilmaril@267
  3410
			else if (foname=="hideInExport")		
insilmaril@267
  3411
				toggleHideExport();
insilmaril@267
  3412
			return;	
insilmaril@267
  3413
		} 
insilmaril@267
  3414
	} 
insilmaril@267
  3415
	// No system flag clicked, take care of modmodes
insilmaril@267
  3416
insilmaril@83
  3417
	// Special case: CTRL is pressed
insilmaril@366
  3418
	if (e->state() & Qt::ControlModifier)
insilmaril@0
  3419
	{
insilmaril@89
  3420
		if (actionModModeColor->isOn())
insilmaril@83
  3421
		{
insilmaril@83
  3422
				pickingColor=true;
insilmaril@83
  3423
				setCursor (pickColorCursor);
insilmaril@83
  3424
				return;
insilmaril@83
  3425
		} 
insilmaril@89
  3426
		if (actionModModeLink->isOn())
insilmaril@83
  3427
		{	
insilmaril@90
  3428
			BranchObj *bo_begin=NULL;
insilmaril@90
  3429
			if (lmo)
insilmaril@90
  3430
				bo_begin=(BranchObj*)(lmo);
insilmaril@90
  3431
			else	
insilmaril@90
  3432
				if (selection && 
insilmaril@90
  3433
					((typeid(*selection) == typeid(BranchObj)) || 
insilmaril@90
  3434
					(typeid(*selection) == typeid(MapCenterObj)))  )
insilmaril@163
  3435
				bo_begin=(BranchObj*)selection;
insilmaril@90
  3436
			if (bo_begin)	
insilmaril@83
  3437
			{
insilmaril@83
  3438
				drawingLink=true;
insilmaril@90
  3439
				linkingObj_src=bo_begin;
insilmaril@95
  3440
				tmpXLink=new XLinkObj (mapCanvas);
insilmaril@95
  3441
				tmpXLink->setBegin (bo_begin);
insilmaril@95
  3442
				tmpXLink->setEnd   (p);
insilmaril@97
  3443
				tmpXLink->setColor(defXLinkColor);
insilmaril@97
  3444
				tmpXLink->setWidth(defXLinkWidth);
insilmaril@95
  3445
				tmpXLink->updateXLink();
insilmaril@95
  3446
				tmpXLink->setVisibility (true);
insilmaril@83
  3447
				return;
insilmaril@89
  3448
			} 
insilmaril@267
  3449
		}
insilmaril@0
  3450
	}
insilmaril@0
  3451
    if (lmo) 
insilmaril@267
  3452
	{	
insilmaril@366
  3453
		selectInt (lmo);
insilmaril@0
  3454
		// Left Button	    Move Branches
insilmaril@366
  3455
		if (e->button() == Qt::LeftButton )
insilmaril@0
  3456
		{
insilmaril@129
  3457
			movingObj_start.setX( p.x() - selection->x() );	
insilmaril@129
  3458
			movingObj_start.setY( p.y() - selection->y() );	
insilmaril@129
  3459
			movingObj_orgPos.setX (lmo->x() );
insilmaril@129
  3460
			movingObj_orgPos.setY (lmo->y() );
insilmaril@366
  3461
			movingObj_orgRelPos=lmo->getRelPos();
insilmaril@129
  3462
insilmaril@93
  3463
			// If modMode==copy, then we want to "move" the _new_ object around
insilmaril@93
  3464
			// then we need the offset from p to the _old_ selection, because of tmp
insilmaril@93
  3465
			if (actionModModeCopy->isOn() &&
insilmaril@366
  3466
				e->state() & Qt::ControlModifier)
insilmaril@93
  3467
			{
insilmaril@93
  3468
				if (typeid(*selection)==typeid(BranchObj) )
insilmaril@93
  3469
				{
insilmaril@93
  3470
					copyingObj=true;
insilmaril@163
  3471
					mapCenter->addBranch ((BranchObj*)selection);
insilmaril@93
  3472
					unselect();
insilmaril@93
  3473
					selection=mapCenter->getLastBranch();
insilmaril@93
  3474
					selection->select();
insilmaril@104
  3475
					mapCenter->reposition();
insilmaril@93
  3476
				}
insilmaril@129
  3477
			}	
insilmaril@0
  3478
			movingObj=selection;	
insilmaril@0
  3479
		} else
insilmaril@0
  3480
			// Middle Button    Toggle Scroll
insilmaril@0
  3481
			// (On Mac OS X this won't work, but we still have 
insilmaril@0
  3482
			// a button in the toolbar)
insilmaril@366
  3483
			if (e->button() == Qt::MidButton )
insilmaril@0
  3484
				toggleScroll();
insilmaril@0
  3485
		updateActions();
insilmaril@0
  3486
	} else 
insilmaril@0
  3487
	{ // No MapObj found, we are on the Canvas itself
insilmaril@0
  3488
		// Left Button	    move Pos of CanvasView
insilmaril@366
  3489
		if (e->button() == Qt::LeftButton )
insilmaril@0
  3490
		{
insilmaril@0
  3491
			movingObj=NULL;	// move Content not Obj
insilmaril@0
  3492
			movingObj_start=e->globalPos();
insilmaril@0
  3493
			movingCont_start=QPoint (contentsX(), contentsY() );
insilmaril@0
  3494
			movingVec=QPoint(0,0);
insilmaril@0
  3495
			setCursor(handOpenCursor);
insilmaril@0
  3496
		} 
insilmaril@0
  3497
    } 
insilmaril@0
  3498
}
insilmaril@0
  3499
insilmaril@0
  3500
void MapEditor::contentsMouseMoveEvent(QMouseEvent* e)
insilmaril@0
  3501
{
insilmaril@83
  3502
	QPoint p = inverseWorldMatrix().map(e->pos());
insilmaril@83
  3503
insilmaril@0
  3504
    // Move the selected MapObj
insilmaril@0
  3505
    if ( selection && movingObj) 
insilmaril@0
  3506
    {	
insilmaril@164
  3507
		// To avoid jumping of the CanvasView, only 
insilmaril@164
  3508
		// ensureSelectionVisible, if not tmp linked
insilmaril@164
  3509
		if (!selection->hasParObjTmp())
insilmaril@164
  3510
			ensureSelectionVisible ();
insilmaril@0
  3511
		
insilmaril@83
  3512
		// Now move the selection, but add relative position 
insilmaril@83
  3513
		// (movingObj_start) where selection was chosen with 
insilmaril@83
  3514
		// mousepointer. (This avoids flickering resp. jumping 
insilmaril@0
  3515
		// of selection back to absPos)
insilmaril@0
  3516
		
insilmaril@0
  3517
		LinkableMapObj *lmosel;
insilmaril@0
  3518
		lmosel =  dynamic_cast <LinkableMapObj*> (selection);
insilmaril@0
  3519
insilmaril@0
  3520
		// Check if we could link 
insilmaril@0
  3521
		LinkableMapObj* lmo=mapCenter->findMapObj(p, lmosel);
insilmaril@0
  3522
		
insilmaril@0
  3523
insilmaril@0
  3524
		if (typeid(*selection) == typeid(FloatImageObj))
insilmaril@0
  3525
		{
insilmaril@163
  3526
			FloatObj *fo=(FloatObj*)selection;
insilmaril@0
  3527
			fo->move   (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
insilmaril@0
  3528
			fo->setRelPos();
insilmaril@0
  3529
			fo->reposition();
insilmaril@0
  3530
insilmaril@0
  3531
			// Relink float to new mapcenter or branch, if shift is pressed	
insilmaril@0
  3532
			// Only relink, if selection really has a new parent
insilmaril@388
  3533
			if ( (e->modifiers()==Qt::ShiftModifier) && lmo &&
insilmaril@0
  3534
				( (typeid(*lmo)==typeid(BranchObj)) ||
insilmaril@0
  3535
				  (typeid(*lmo)==typeid(MapCenterObj)) ) &&
insilmaril@0
  3536
				( lmo != fo->getParObj())  
insilmaril@0
  3537
				)
insilmaril@0
  3538
			{
insilmaril@0
  3539
				if (typeid(*fo) == typeid(FloatImageObj)) 
insilmaril@0
  3540
				{
insilmaril@366
  3541
					//TODO undocom
insilmaril@366
  3542
					saveStateComplete(QString("Relink %1 to %2").arg(getName(fo)).arg(getName(lmo) ) );
insilmaril@0
  3543
					FloatImageObj *fio=(FloatImageObj*)(fo);
insilmaril@0
  3544
					((BranchObj*)(lmo))->addFloatImage (fio);
insilmaril@0
  3545
					fio->unselect();
insilmaril@0
  3546
					((BranchObj*)(fio->getParObj()))->removeFloatImage (fio);
insilmaril@0
  3547
					fio=((BranchObj*)(lmo))->getLastFloatImage();
insilmaril@0
  3548
					fio->setRelPos();
insilmaril@0
  3549
					fio->reposition();
insilmaril@0
  3550
					selection=(LinkableMapObj*)(fio);
insilmaril@0
  3551
					selection->select();
insilmaril@0
  3552
					movingObj=(MapObj*)(fio);
insilmaril@0
  3553
				}	
insilmaril@0
  3554
			}
insilmaril@388
  3555
		} else	
insilmaril@388
  3556
		{	// selection != a FloatObj
insilmaril@0
  3557
			if (lmosel->getDepth()==0)
insilmaril@0
  3558
			{
insilmaril@388
  3559
				// Move MapCenter
insilmaril@388
  3560
				if (e->buttons()== Qt::LeftButton && e->modifiers()==Qt::ShiftModifier) 
insilmaril@0
  3561
					mapCenter->moveAll(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
insilmaril@0
  3562
				else	
insilmaril@0
  3563
					mapCenter->move   (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
insilmaril@388
  3564
				mapCenter->updateRelPositions();	
insilmaril@0
  3565
			} else
insilmaril@0
  3566
			{	
insilmaril@0
  3567
				if (lmosel->getDepth()==1)
insilmaril@0
  3568
				{
insilmaril@388
  3569
					// Move mainbranch
insilmaril@0
  3570
					lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
insilmaril@388
  3571
					lmosel->setRelPos();
insilmaril@0
  3572
				} else
insilmaril@0
  3573
				{
insilmaril@388
  3574
					// Move ordinary branch
insilmaril@0
  3575
					if (lmosel->getOrientation() == OrientLeftOfCenter)
insilmaril@0
  3576
						// Add width of bbox here, otherwise alignRelTo will cause jumping around
insilmaril@0
  3577
						lmosel->move(p.x() -movingObj_start.x()+lmosel->getBBox().width(), 
insilmaril@175
  3578
							p.y()-movingObj_start.y() +lmosel->getTopPad() );		
insilmaril@0
  3579
					else	
insilmaril@175
  3580
						lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() -lmosel->getTopPad());
insilmaril@0
  3581
				} 
insilmaril@0
  3582
				// reposition subbranch
insilmaril@0
  3583
				lmosel->reposition();	
insilmaril@0
  3584
insilmaril@0
  3585
				if (lmo && (lmo!=selection) &&  
insilmaril@0
  3586
					(typeid(*lmo) == typeid(BranchObj) ||
insilmaril@0
  3587
					(typeid(*lmo) == typeid(MapCenterObj) )
insilmaril@0
  3588
					) )
insilmaril@0
  3589
				{
insilmaril@388
  3590
					if (e->modifiers()==Qt::ControlModifier)
insilmaril@0
  3591
					{
insilmaril@0
  3592
						// Special case: CTRL to link below lmo
insilmaril@0
  3593
						lmosel->setParObjTmp (lmo,p,+1);
insilmaril@0
  3594
					}
insilmaril@388
  3595
					else if (e->modifiers()==Qt::ShiftModifier)
insilmaril@0
  3596
						lmosel->setParObjTmp (lmo,p,-1);
insilmaril@0
  3597
					else
insilmaril@0
  3598
						lmosel->setParObjTmp (lmo,p,0);
insilmaril@0
  3599
				} else	
insilmaril@0
  3600
				{
insilmaril@93
  3601
					lmosel->unsetParObjTmp();
insilmaril@0
  3602
				}		
insilmaril@0
  3603
			} // depth>0
insilmaril@0
  3604
insilmaril@0
  3605
		} // no FloatImageObj
insilmaril@0
  3606
insilmaril@0
  3607
		canvas()->update();
insilmaril@0
  3608
		return;
insilmaril@0
  3609
	} // selection && moving_obj
insilmaril@0
  3610
		
insilmaril@83
  3611
	// Draw a link from one branch to another
insilmaril@83
  3612
	if (drawingLink)
insilmaril@83
  3613
	{
insilmaril@95
  3614
		 tmpXLink->setEnd (p);
insilmaril@95
  3615
		 tmpXLink->updateXLink();
insilmaril@83
  3616
	}	 
insilmaril@83
  3617
	
insilmaril@0
  3618
    // Move CanvasView 
insilmaril@83
  3619
    if (!movingObj && !pickingColor &&!drawingLink) 
insilmaril@0
  3620
	{
insilmaril@0
  3621
		QPoint p=e->globalPos();
insilmaril@0
  3622
		movingVec.setX(-p.x() + movingObj_start.x() );
insilmaril@0
  3623
		movingVec.setY(-p.y() + movingObj_start.y() );
insilmaril@0
  3624
		setContentsPos( movingCont_start.x() + movingVec.x(),
insilmaril@0
  3625
	    movingCont_start.y() + movingVec.y());
insilmaril@0
  3626
insilmaril@0
  3627
		updateViewCenter();
insilmaril@0
  3628
    }
insilmaril@0
  3629
}
insilmaril@0
  3630
insilmaril@0
  3631
insilmaril@0
  3632
void MapEditor::contentsMouseReleaseEvent(QMouseEvent* e)
insilmaril@0
  3633
{
insilmaril@0
  3634
	LinkableMapObj *dst;
insilmaril@0
  3635
	// Have we been picking color?
insilmaril@0
  3636
	if (pickingColor)
insilmaril@0
  3637
	{
insilmaril@0
  3638
		pickingColor=false;
insilmaril@366
  3639
		setCursor (Qt::ArrowCursor);
insilmaril@0
  3640
		// Check if we are over another branch
insilmaril@0
  3641
		dst=mapCenter->findMapObj(inverseWorldMatrix().map(e->pos() ), NULL);
insilmaril@0
  3642
		if (dst && selection) 
insilmaril@0
  3643
		{	
insilmaril@366
  3644
			if (e->state() & Qt::ShiftModifier)
insilmaril@0
  3645
			{
insilmaril@243
  3646
				((BranchObj*)selection)->setColor (((BranchObj*)(dst))->getColor());
insilmaril@163
  3647
				((BranchObj*)selection)->setLinkColor ();
insilmaril@0
  3648
			}	
insilmaril@0
  3649
			else	
insilmaril@0
  3650
			{
insilmaril@243
  3651
				((BranchObj*)selection)->setColorChilds (((BranchObj*)(dst))->getColor());
insilmaril@163
  3652
				((BranchObj*)selection)->setLinkColor ();
insilmaril@0
  3653
			}	
insilmaril@0
  3654
		} 
insilmaril@0
  3655
		return;
insilmaril@0
  3656
	}
insilmaril@83
  3657
insilmaril@83
  3658
	// Have we been drawing a link?
insilmaril@83
  3659
	if (drawingLink)
insilmaril@83
  3660
	{
insilmaril@83
  3661
		drawingLink=false;
insilmaril@83
  3662
		// Check if we are over another branch
insilmaril@83
  3663
		dst=mapCenter->findMapObj(inverseWorldMatrix().map(e->pos() ), NULL);
insilmaril@83
  3664
		if (dst && selection) 
insilmaril@83
  3665
		{	
insilmaril@95
  3666
			tmpXLink->setEnd ( ((BranchObj*)(dst)) );
insilmaril@95
  3667
			tmpXLink->updateXLink();
insilmaril@95
  3668
			tmpXLink->activate();
insilmaril@366
  3669
			saveStateComplete(QString("Activate xLink from %1 to %2").arg(getName(tmpXLink->getBegin())).arg(getName(tmpXLink->getEnd())) );	//TODO undoCommand
insilmaril@83
  3670
		} else
insilmaril@83
  3671
		{
insilmaril@95
  3672
			delete(tmpXLink);
insilmaril@95
  3673
			tmpXLink=NULL;
insilmaril@83
  3674
		}
insilmaril@83
  3675
		return;
insilmaril@83
  3676
	}
insilmaril@83
  3677
	
insilmaril@0
  3678
    // Have we been moving something?
insilmaril@0
  3679
    if ( selection && movingObj ) 
insilmaril@0
  3680
    {	
insilmaril@175
  3681
		if(typeid(*selection)==typeid (FloatImageObj))
insilmaril@175
  3682
		{
insilmaril@388
  3683
			// Moved FloatObj. Maybe we need to reposition
insilmaril@366
  3684
			FloatImageObj *fo=(FloatImageObj*)selection;
insilmaril@366
  3685
		    QString pold=qpointToString(movingObj_orgRelPos);
insilmaril@366
  3686
		    QString pnow=qpointToString(fo->getRelPos());
insilmaril@366
  3687
			saveStateConstSelection(
insilmaril@366
  3688
				"moveRel "+pold,
insilmaril@366
  3689
				"moveRel "+pnow,
insilmaril@366
  3690
				QString("Move %1 to relativ position %2").arg(getName(selection)).arg(pnow));
insilmaril@366
  3691
insilmaril@388
  3692
			// FIXME Why calling parObj here?
insilmaril@175
  3693
			selection->getParObj()->requestReposition();
insilmaril@175
  3694
			mapCenter->reposition();
insilmaril@175
  3695
		}	
insilmaril@175
  3696
insilmaril@0
  3697
		// Check if we are over another branch, but ignore 
insilmaril@0
  3698
		// any found LMOs, which are FloatObjs
insilmaril@0
  3699
		dst=mapCenter->findMapObj(inverseWorldMatrix().map(e->pos() ), 
insilmaril@163
  3700
			((LinkableMapObj*)selection) );
insilmaril@0
  3701
insilmaril@336
  3702
		if (dst && (typeid(*dst)!=typeid(BranchObj) && typeid(*dst)!=typeid(MapCenterObj))) 
insilmaril@0
  3703
			dst=NULL;
insilmaril@0
  3704
		
insilmaril@388
  3705
		if (typeid(*selection) == typeid(MapCenterObj)  )
insilmaril@388
  3706
		{	// The MapCenter was moved
insilmaril@388
  3707
			cout << "FIXME missing savestate...\n";	
insilmaril@388
  3708
		}
insilmaril@388
  3709
		
insilmaril@0
  3710
		if (typeid(*selection) == typeid(BranchObj)  )
insilmaril@388
  3711
		{	// A branch was moved
insilmaril@388
  3712
			
insilmaril@0
  3713
			// save the position in case we link to mapcenter
insilmaril@0
  3714
			QPoint savePos=QPoint (selection->x(),selection->y() );
insilmaril@0
  3715
insilmaril@0
  3716
			// Reset the temporary drawn link to the original one
insilmaril@163
  3717
			((LinkableMapObj*)selection)->unsetParObjTmp();
insilmaril@0
  3718
insilmaril@366
  3719
			// For Redo we may need to save original selection
insilmaril@366
  3720
			QString preSelStr=selection->getSelectString();
insilmaril@93
  3721
insilmaril@94
  3722
			copyingObj=false;	
insilmaril@129
  3723
			if (dst ) 
insilmaril@93
  3724
			{
insilmaril@366
  3725
				BranchObj* bsel=(BranchObj*)selection;
insilmaril@366
  3726
				BranchObj* bdst=(BranchObj*)dst;
insilmaril@366
  3727
insilmaril@366
  3728
				QString preParStr=(bsel->getParObj())->getSelectString();
insilmaril@366
  3729
				QString preNum=QString::number (bsel->getNum(),10);
insilmaril@366
  3730
				QString preDstParStr;
insilmaril@366
  3731
insilmaril@366
  3732
				if (e->state() & Qt::ShiftModifier && dst->getParObj())
insilmaril@366
  3733
				{	// Link above dst
insilmaril@366
  3734
					preDstParStr=dst->getParObj()->getSelectString();
insilmaril@366
  3735
					bsel->moveBranchTo ( (BranchObj*)(bdst->getParObj()), bdst->getNum());
insilmaril@366
  3736
				} else 
insilmaril@366
  3737
				if (e->state() & Qt::ControlModifier && dst->getParObj())
insilmaril@366
  3738
				{
insilmaril@366
  3739
					// Link below dst
insilmaril@366
  3740
					preDstParStr=dst->getParObj()->getSelectString();
insilmaril@366
  3741
					bsel->moveBranchTo ( (BranchObj*)(bdst->getParObj()), bdst->getNum()+1);
insilmaril@366
  3742
				} else	
insilmaril@366
  3743
				{	// Append to dst
insilmaril@366
  3744
					preDstParStr=dst->getSelectString();
insilmaril@366
  3745
					bsel->moveBranchTo (bdst,-1);
insilmaril@366
  3746
					if (dst->getDepth()==0) bsel->move (savePos);
insilmaril@366
  3747
				} 
insilmaril@366
  3748
				QString postSelStr=selection->getSelectString();
insilmaril@366
  3749
				QString postNum=QString::number (bsel->getNum(),10);
insilmaril@366
  3750
insilmaril@139
  3751
				QString undoCom="linkBranchToPos (\""+ 
insilmaril@366
  3752
					preParStr+ "\"," + preNum  +"," + 
insilmaril@366
  3753
					QString ("%1,%2").arg(movingObj_orgPos.x()).arg(movingObj_orgPos.y())+ ")";
insilmaril@366
  3754
insilmaril@366
  3755
				QString redoCom="linkBranchToPos (\""+ 
insilmaril@366
  3756
					preDstParStr + "\"," + postNum + "," +
insilmaril@366
  3757
					QString ("%1,%2").arg(savePos.x()).arg(savePos.y())+ ")";
insilmaril@366
  3758
insilmaril@366
  3759
				saveState (
insilmaril@366
  3760
					postSelStr,undoCom,
insilmaril@366
  3761
					preSelStr, redoCom,
insilmaril@366
  3762
					QString("Relink %1 to %2").arg(getName(bsel)).arg(getName(dst)) );
insilmaril@163
  3763
			} else
insilmaril@163
  3764
				if (selection->getDepth()==1)
insilmaril@366
  3765
				{
insilmaril@366
  3766
					// The select string might be different _after_ moving around.
insilmaril@366
  3767
					// Therefor reposition and then use string of old selection, too
insilmaril@366
  3768
					mapCenter->reposition();
insilmaril@366
  3769
insilmaril@366
  3770
					QString ps=qpointToString ( ((BranchObj*)selection)->getRelPos() );
insilmaril@366
  3771
					saveState(
insilmaril@366
  3772
						selection->getSelectString(), "moveRel "+qpointToString(movingObj_orgRelPos), 
insilmaril@366
  3773
						preSelStr, "moveRel "+ps, 
insilmaril@366
  3774
						QString("Move %1 to relative position %2").arg(getName(selection)).arg(ps));
insilmaril@163
  3775
			
insilmaril@366
  3776
				}
insilmaril@0
  3777
			// Draw the original link, before selection was moved around
insilmaril@0
  3778
			mapCenter->reposition();
insilmaril@0
  3779
		}
insilmaril@0
  3780
		// Finally resize canvas, if needed
insilmaril@0
  3781
		adjustCanvasSize();
insilmaril@0
  3782
		canvas()->update();
insilmaril@0
  3783
		movingObj=NULL;		
insilmaril@366
  3784
insilmaril@366
  3785
		// Just make sure, that actions are still ok,e.g. the move branch up/down buttons...
insilmaril@366
  3786
		updateActions();
insilmaril@0
  3787
	} else 
insilmaril@83
  3788
		// maybe we moved View: set old cursor
insilmaril@366
  3789
		setCursor (Qt::ArrowCursor);
insilmaril@83
  3790
    
insilmaril@0
  3791
}
insilmaril@0
  3792
insilmaril@0
  3793
void MapEditor::contentsMouseDoubleClickEvent(QMouseEvent* e)
insilmaril@0
  3794
{
insilmaril@366
  3795
	if (e->button() == Qt::LeftButton )
insilmaril@0
  3796
	{
insilmaril@0
  3797
		QPoint p = inverseWorldMatrix().map(e->pos());
insilmaril@0
  3798
		LinkableMapObj *lmo=mapCenter->findMapObj(p, NULL);
insilmaril@0
  3799
		if (lmo) {	// MapObj was found
insilmaril@0
  3800
			// First select the MapObj than edit heading
insilmaril@0
  3801
			if (selection) selection->unselect();
insilmaril@0
  3802
			selection=lmo;
insilmaril@0
  3803
			selection->select();
insilmaril@366
  3804
			mainWindow->editHeading();
insilmaril@0
  3805
		}
insilmaril@0
  3806
	}
insilmaril@0
  3807
}
insilmaril@0
  3808
insilmaril@0
  3809
void MapEditor::resizeEvent (QResizeEvent* e)
insilmaril@0
  3810
{
insilmaril@366
  3811
	Q3CanvasView::resizeEvent( e );
insilmaril@0
  3812
	adjustCanvasSize();
insilmaril@0
  3813
}
insilmaril@0
  3814
insilmaril@128
  3815
void MapEditor::contentsDragEnterEvent(QDragEnterEvent *event) 
insilmaril@119
  3816
{
insilmaril@129
  3817
insilmaril@129
  3818
//  for (unsigned int i=0;event->format(i);i++) // Debug mime type
insilmaril@129
  3819
//    cerr << event->format(i) << endl;
insilmaril@129
  3820
insilmaril@119
  3821
  if (selection && 
insilmaril@119
  3822
      (typeid(*selection) == typeid(BranchObj)) || 
insilmaril@119
  3823
      (typeid(*selection) == typeid(MapCenterObj))) {
insilmaril@119
  3824
    
insilmaril@119
  3825
    // If QImageDrag can decode mime type 
insilmaril@366
  3826
    if (Q3ImageDrag::canDecode(event)) {
insilmaril@119
  3827
      event->accept();
insilmaril@119
  3828
      return;
insilmaril@119
  3829
    }
insilmaril@119
  3830
    
insilmaril@128
  3831
    // If image are dragged from firefox 
insilmaril@119
  3832
    if (event->provides("application/x-moz-file-promise-url") && 
insilmaril@119
  3833
	event->provides("application/x-moz-nativeimage")) {
insilmaril@119
  3834
      event->accept(true);
insilmaril@119
  3835
      return;
insilmaril@119
  3836
    }
insilmaril@125
  3837
insilmaril@128
  3838
    // If QUriDrag can decode mime type 
insilmaril@366
  3839
    if (Q3UriDrag::canDecode(event)) {
insilmaril@128
  3840
      event->accept();
insilmaril@128
  3841
      return;
insilmaril@128
  3842
    }
insilmaril@128
  3843
    
insilmaril@128
  3844
	// If Uri are dragged from firefox 
insilmaril@129
  3845
    if (event->provides("_NETSCAPE_URL")){
insilmaril@128
  3846
      event->accept();
insilmaril@128
  3847
      return;
insilmaril@128
  3848
    }
insilmaril@129
  3849
insilmaril@129
  3850
    // If QTextDrag can decode mime type
insilmaril@366
  3851
    if (Q3TextDrag::canDecode(event)) {
insilmaril@129
  3852
      event->accept();
insilmaril@129
  3853
      return;
insilmaril@129
  3854
    }
insilmaril@129
  3855
insilmaril@119
  3856
  }
insilmaril@119
  3857
  event->ignore();
insilmaril@119
  3858
}
insilmaril@129
  3859
insilmaril@129
  3860
bool isUnicode16(const QByteArray &d) 
insilmaril@129
  3861
{
insilmaril@164
  3862
  // TODO: make more precise check for unicode 16.
insilmaril@129
  3863
  // Guess unicode16 if any of second bytes are zero
insilmaril@129
  3864
  unsigned int length = max(0,d.size()-2)/2;
insilmaril@129
  3865
  for (unsigned int i = 0; i<length ; i++)
insilmaril@129
  3866
    if (d.at(i*2+1)==0) return true;
insilmaril@129
  3867
  return false;
insilmaril@129
  3868
}
insilmaril@119
  3869
      
insilmaril@128
  3870
void MapEditor::contentsDropEvent(QDropEvent *event) 
insilmaril@119
  3871
{
insilmaril@128
  3872
	if (selection && 
insilmaril@128
  3873
      (typeid(*selection) == typeid(BranchObj)) || 
insilmaril@128
  3874
      (typeid(*selection) == typeid(MapCenterObj))) 
insilmaril@128
  3875
	{
insilmaril@128
  3876
		bool update=false;
insilmaril@366
  3877
		Q3StrList uris;
insilmaril@129
  3878
		QString heading;
insilmaril@128
  3879
		if (event->provides("image/png")) 
insilmaril@128
  3880
		{
insilmaril@128
  3881
			QPixmap pix;
insilmaril@366
  3882
			if (Q3ImageDrag::decode(event, pix)) 
insilmaril@128
  3883
			{
insilmaril@128
  3884
				addFloatImage(pix);
insilmaril@128
  3885
				event->accept();
insilmaril@128
  3886
				update=true;
insilmaril@128
  3887
			} else
insilmaril@128
  3888
				event->ignore();
insilmaril@128
  3889
insilmaril@128
  3890
		} else if (event->provides("application/x-moz-file-promise-url") && 
insilmaril@128
  3891
			 event->provides("application/x-moz-nativeimage")) 
insilmaril@128
  3892
		{
insilmaril@129
  3893
			// Contains url to the img src in unicode16
insilmaril@128
  3894
			QByteArray d = event->encodedData("application/x-moz-file-promise-url");
insilmaril@128
  3895
			QString url = QString((const QChar*)d.data(),d.size()/2);
insilmaril@128
  3896
			fetchImage(url);
insilmaril@128
  3897
			event->accept();
insilmaril@128
  3898
			update=true;
insilmaril@128
  3899
		} else if (event->provides ("text/uri-list"))
insilmaril@128
  3900
		{	// Uris provided e.g. by konqueror
insilmaril@366
  3901
			Q3UriDrag::decode (event,uris);
insilmaril@129
  3902
		} else if (event->provides ("_NETSCAPE_URL"))
insilmaril@128
  3903
		{	// Uris provided by Mozilla
insilmaril@129
  3904
		  QStringList l = QStringList::split("\n", event->encodedData("_NETSCAPE_URL"));
insilmaril@129
  3905
		  uris.append(l[0]);
insilmaril@129
  3906
		  heading = l[1];
insilmaril@129
  3907
		} else if (event->provides("text/html")) {
insilmaril@129
  3908
insilmaril@129
  3909
		  // Handels text mime types
insilmaril@129
  3910
		  // Look like firefox allways handle text as unicode16 (2 bytes per char.)
insilmaril@129
  3911
		  QByteArray d = event->encodedData("text/html");
insilmaril@129
  3912
		  QString text;
insilmaril@129
  3913
		  if (isUnicode16(d)) 
insilmaril@129
  3914
		    text = QString((const QChar*)d.data(),d.size()/2);
insilmaril@129
  3915
		  else 
insilmaril@129
  3916
		    text = QString(d);
insilmaril@129
  3917
insilmaril@129
  3918
		  textEditor->setText(text);
insilmaril@129
  3919
insilmaril@129
  3920
		  event->accept();
insilmaril@129
  3921
		  update=true;
insilmaril@129
  3922
		} else if (event->provides("text/plain")) {
insilmaril@129
  3923
		  QByteArray d = event->encodedData("text/plain");
insilmaril@129
  3924
		  QString text;
insilmaril@129
  3925
		  if (isUnicode16(d))
insilmaril@129
  3926
		    text = QString((const QChar*)d.data(),d.size()/2);
insilmaril@129
  3927
		  else 
insilmaril@129
  3928
		    text = QString(d);
insilmaril@129
  3929
insilmaril@129
  3930
		  textEditor->setText(text);
insilmaril@129
  3931
		  
insilmaril@129
  3932
		  event->accept();
insilmaril@129
  3933
		  update= true;
insilmaril@128
  3934
		}
insilmaril@128
  3935
insilmaril@128
  3936
		if (uris.count()>0)
insilmaril@128
  3937
		{
insilmaril@128
  3938
			QStringList files;
insilmaril@128
  3939
			QStringList urls;
insilmaril@128
  3940
			QString s;
insilmaril@128
  3941
			BranchObj *bo;
insilmaril@128
  3942
			for (const char* u=uris.first(); u; u=uris.next())
insilmaril@128
  3943
			{
insilmaril@163
  3944
				bo=((BranchObj*)selection)->addBranch();
insilmaril@128
  3945
				if (bo)
insilmaril@128
  3946
				{
insilmaril@366
  3947
					s=Q3UriDrag::uriToLocalFile(u);
insilmaril@366
  3948
					if (!s.isEmpty()) 
insilmaril@366
  3949
					{
insilmaril@129
  3950
                       QString file = QDir::convertSeparators(s);
insilmaril@129
  3951
                       heading = QFileInfo(file).baseName();
insilmaril@129
  3952
                       files.append(file);
insilmaril@129
  3953
                       if (file.endsWith(".vym", false))
insilmaril@129
  3954
                           bo->setVymLink(file);
insilmaril@129
  3955
                       else
insilmaril@129
  3956
                           bo->setURL(u);
insilmaril@366
  3957
                   } else 
insilmaril@366
  3958
				   {
insilmaril@129
  3959
                       urls.append (u);
insilmaril@129
  3960
                       bo->setURL(u);
insilmaril@129
  3961
                   }
insilmaril@129
  3962
insilmaril@366
  3963
                   if (!heading.isEmpty())
insilmaril@129
  3964
                       bo->setHeading(heading);
insilmaril@129
  3965
                   else
insilmaril@129
  3966
                       bo->setHeading(u);
insilmaril@128
  3967
				}
insilmaril@128
  3968
			}
insilmaril@128
  3969
			update=true;
insilmaril@128
  3970
		}
insilmaril@128
  3971
insilmaril@128
  3972
		if (update) 
insilmaril@128
  3973
		{
insilmaril@294
  3974
			//FIXME saveState has to be called earlier for each of the drops...
insilmaril@366
  3975
			saveStateComplete("Drop Event");	//TODO undo Command
insilmaril@128
  3976
			mapCenter->reposition();
insilmaril@128
  3977
			adjustCanvasSize();
insilmaril@128
  3978
			canvas()->update();
insilmaril@128
  3979
		}	
insilmaril@128
  3980
	}	
insilmaril@119
  3981
}
insilmaril@119
  3982
insilmaril@125
  3983
void MapEditor::addFloatImage(const QPixmap &img) 
insilmaril@119
  3984
{
insilmaril@119
  3985
  if (selection && 
insilmaril@119
  3986
      (typeid(*selection) == typeid(BranchObj)) || 
insilmaril@119
  3987
      (typeid(*selection) == typeid(MapCenterObj))  )
insilmaril@119
  3988
  {
insilmaril@163
  3989
    BranchObj *bo=((BranchObj*)selection);
insilmaril@366
  3990
    saveStatePart(selection,QString("Add floatimage to %1").arg(getName(bo)));
insilmaril@119
  3991
    //QString fn=fd->selectedFile();
insilmaril@119
  3992
    //lastImageDir=fn.left(fn.findRev ("/"));
insilmaril@119
  3993
    bo->addFloatImage();
insilmaril@119
  3994
    // FIXME check if load was successful
insilmaril@119
  3995
    bo->getLastFloatImage()->load(img);
insilmaril@119
  3996
    //bo->getLastFloatImage()->setOriginalFilename(fn);
insilmaril@119
  3997
    mapCenter->reposition();
insilmaril@119
  3998
    adjustCanvasSize();
insilmaril@119
  3999
    canvas()->update();
insilmaril@119
  4000
  }
insilmaril@119
  4001
}
insilmaril@119
  4002
insilmaril@119
  4003
insilmaril@366
  4004
void MapEditor::imageDataFetched(const QByteArray &a, Q3NetworkOperation */*nop*/) 
insilmaril@119
  4005
{
insilmaril@119
  4006
  if (!imageBuffer) imageBuffer = new QBuffer();
insilmaril@119
  4007
  if (!imageBuffer->isOpen()) {
insilmaril@366
  4008
    imageBuffer->open(QIODevice::WriteOnly | QIODevice::Append);
insilmaril@119
  4009
  }
insilmaril@119
  4010
  imageBuffer->at(imageBuffer->at()+imageBuffer->writeBlock(a));
insilmaril@119
  4011
}
insilmaril@119
  4012
insilmaril@119
  4013
insilmaril@366
  4014
void MapEditor::imageDataFinished(Q3NetworkOperation *nop) 
insilmaril@119
  4015
{
insilmaril@366
  4016
	if (nop->state()==Q3NetworkProtocol::StDone) {
insilmaril@129
  4017
		QPixmap img(imageBuffer->buffer());
insilmaril@129
  4018
		addFloatImage(img);
insilmaril@129
  4019
	}
insilmaril@129
  4020
insilmaril@129
  4021
	if (imageBuffer) {
insilmaril@129
  4022
		imageBuffer->close();
insilmaril@129
  4023
		if (imageBuffer) {
insilmaril@129
  4024
			imageBuffer->close();
insilmaril@129
  4025
			delete imageBuffer;
insilmaril@129
  4026
			imageBuffer = 0;
insilmaril@129
  4027
		}
insilmaril@129
  4028
	}
insilmaril@119
  4029
}
insilmaril@119
  4030
insilmaril@129
  4031
void MapEditor::fetchImage(const QString &url) 
insilmaril@119
  4032
{
insilmaril@119
  4033
  if (urlOperator) {
insilmaril@119
  4034
    urlOperator->stop();
insilmaril@119
  4035
    disconnect(urlOperator);
insilmaril@119
  4036
    delete urlOperator;
insilmaril@119
  4037
  }
insilmaril@119
  4038
  
insilmaril@366
  4039
  urlOperator = new Q3UrlOperator(url);
insilmaril@366
  4040
  connect(urlOperator, SIGNAL(finished(Q3NetworkOperation *)), 
insilmaril@366
  4041
	  this, SLOT(imageDataFinished(Q3NetworkOperation*)));
insilmaril@366
  4042
insilmaril@366
  4043
  connect(urlOperator, SIGNAL(data(const QByteArray &, Q3NetworkOperation *)),
insilmaril@366
  4044
	  this, SLOT(imageDataFetched(const QByteArray &, Q3NetworkOperation *)));
insilmaril@119
  4045
  urlOperator->get();
insilmaril@119
  4046
}