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