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