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