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