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