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