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