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