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