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