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