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