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