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