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