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