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