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