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