mapeditor.h
author insilmaril
Mon, 30 Jul 2007 09:47:08 +0000
changeset 570 ab83f030285e
parent 537 16d3598f611f
child 572 a97aabc21136
permissions -rw-r--r--
1.9.4 New base class for XML based file parsing (vym & Freemind)
     1 #ifndef MAPEDITOR_H
     2 #define MAPEDITOR_H
     3 
     4 #include <QGraphicsView>
     5 #include <QtNetwork>
     6 
     7 #include "mapcenterobj.h"
     8 #include "file.h"
     9 #include "misc.h"
    10 #include "parser.h"
    11 #include "ornamentedobj.h"
    12 #include "selection.h"
    13 #include "settings.h"
    14 
    15 
    16 /*! \brief Main widget in vym to display and edit a map */
    17 
    18 class MapEditor : public QGraphicsView, public xmlObj {
    19     Q_OBJECT
    20 
    21 public:
    22     /*! \brief State of the mapeditor 
    23 		
    24 		While a heading is edited, the state has to change, so
    25 		that e.g. no other object might get selected. This is done
    26 		in Main by calling setStateEditHeading
    27 	*/
    28 	enum State {
    29 		Idle,			//!< Idle, waiting for user event
    30 		EditHeading		//!< Editing heading (dont't select another branch now)
    31 	};
    32     /*! \brief Networking states
    33 		
    34 		In Network modus we want to switch of saveState, autosave, ...
    35 	*/
    36 	enum NetState {
    37 		Offline,			//!< Offline
    38 		Client,				//!< I am the client and connected to server
    39 		Server				//!< I am the server
    40 	};
    41     MapEditor(QWidget* parent=0);
    42 	~MapEditor();
    43     MapCenterObj* getMapCenter();		//!< Return center of map
    44 	QGraphicsScene * getScene();
    45 	State getState();					//!< Return State of MapEditor
    46 	void setStateEditHeading (bool);	//!< If set to true, State will change to EditHeading
    47 	bool isRepositionBlocked(); //!< While load or undo there is no need to update graphicsview
    48 	bool isSaveStateBlocked();			//!< block while undo/redo or while running scripts
    49 	void setSaveStateBlocked(bool);		//!< block saving the undo/redo state
    50 	bool isSelectBlocked();		//!< true, if no change of selection is possible, e.g. while editing the heading of abranch
    51 	
    52 protected:
    53 	/*! \brief Get name of object
    54 	  
    55 	  Returns heading of a branch or name of an object for use in comment
    56 	  of undo/redo history
    57 	*/ 
    58 	QString getName(const LinkableMapObj*);	// Get e.g. heading or filename
    59 	void makeTmpDirs();		// create temporary directories
    60 
    61 	/*! This function saves all information of the map to disc.
    62 	    saveToDir also calls the functions for all BranchObj and other objects in the map.
    63 		The structure of the map itself is returned as QString and passed back to Main, 
    64 		where saveToDir is called initially
    65 	*/	
    66     QString saveToDir (const QString &tmpdir, const QString &prefix, bool writeflags, const QPointF &offset, LinkableMapObj *saveSel);
    67 
    68 	/*! \brief Get directory, where current step in history is save
    69 
    70 		saveState creates a directory for each step in history. This function returns the
    71 		path of the current directory
    72 	*/
    73 	QString getHistoryDir();
    74 
    75 	/*! \brief Save the current changes in map 
    76 
    77 		Two commands and selections are saved:
    78 
    79 			- undocommand and undoselection to undo the change
    80 			- redocommand and redoselection to redo the action after an undo
    81 
    82 		Additionally a comment is logged. 
    83 
    84 	*/	
    85     void saveState(const SaveMode& savemode, const QString &undoSelection, const QString &undoCommand, const QString &redoSelection, const QString &redoCommand, const QString &comment, LinkableMapObj *saveSelection);
    86 	/*! Overloaded for convenience */
    87     void saveStateChangingPart(LinkableMapObj *undoSelection, LinkableMapObj* redoSelection, const QString &redoCommand, const QString &comment);
    88 	/*! Overloaded for convenience */
    89     void saveStateRemovingPart(LinkableMapObj *redoSelection, const QString &comment);
    90 	/*! Overloaded for convenience */
    91     void saveState(LinkableMapObj *undoSelection, const QString &undoCommand, LinkableMapObj *redoSelection, const QString &redoCommand, const QString &comment); 
    92 	/*! Overloaded for convenience */
    93     void saveState(const QString &undoSelection, const QString &undoCommand, const QString &redoSelection, const QString &redoCommand, const QString &comment) ;
    94 
    95 public:	
    96 
    97 	/* \brief Process one command and its parameters */
    98     void parseAtom (const QString &atom);	
    99 
   100 	/* \brief Runs the script */
   101 	void runScript (QString script);
   102 private:
   103     void addFloatImageInt(const QPixmap &img);
   104 
   105 public:
   106 	bool isDefault();		//!< true, if map is still the empty default map
   107     bool hasChanged();		//!< true, if something has changed and is not saved yet
   108 	void setChanged();		//!< called from TextEditor via LinkableMapObj
   109 	void closeMap();		//!< Closes the map
   110 
   111 	/*! \brief Sets filepath, filename and mapname
   112 
   113 	     If the filepath is "/home/tux/map.xml", then the filename will be set
   114 		 to map.xml. The destname is needed for vymLinks, pointing to another map. 
   115 		 The destname holds the real name of the file, after it has been compressed, e.g. "map.vym"
   116 	*/	 
   117 
   118 	/*! \brief Set File path
   119 
   120 	     The destname is needed to construct the references between maps
   121 	*/	 
   122 	void setFilePath (QString filepath,QString destname);	
   123 	void setFilePath (QString);	//!< Overloaded for convenience
   124 	QString getFilePath ();	//!< Full path e.g. "/home/tux/map.xml"
   125 	QString getFileName ();	//!< e.g. "map.xml"
   126 	QString getMapName ();	//!< e.g. "map"
   127 	QString getDestPath (); //!< e.g. "/home/tux/map.vym"
   128 
   129 	/*! \brief Load map
   130 
   131 		The data is read from file. Depending on LoadMode the current
   132 		selection gets replaced by data or the data is appended.
   133 	*/	
   134     ErrorCode load (QString, LoadMode );	// newmap, import/replace selection
   135 public:
   136 	/*! \brief Save the map to file */
   137     int save(const SaveMode &);	
   138 	void setZipped(bool);		//!< Set or unset compression of map with zip save map zipped
   139 	bool saveZipped();			//!< True, if file will be saved zipped
   140     void print();				//!< Print the map
   141 	void setAntiAlias (bool);	//!< Set or unset antialiasing
   142 	void setSmoothPixmap(bool); //!< Set or unset smoothing of pixmaps
   143 private:
   144     QPixmap getPixmap();
   145 	void setHideTmpMode (BranchObj::HideTmpMode);	// temporary hide stuff
   146 	BranchObj::HideTmpMode getHideTmpMode();		// temporary hide stuff
   147 public:
   148 	/*! Set or unset temporary hiding of objects during export  */
   149 	void setExportMode (bool);
   150 
   151 	/*! Export as ASCII text to file */
   152 	void exportASCII (QString fname="",bool askForName=true);  
   153 
   154 	/*! Save as image */
   155     void exportImage (QString fname="",bool askForName=true,QString format="PNG");
   156 
   157     /*! Export as OpenOfficeOrg presentation */
   158     void exportOOPresentation(const QString &,const QString &);	
   159 
   160 	/*! Export as XHTML to directory */
   161     void exportXHTML(const QString& dir="", bool askForName=true);	
   162 
   163 	/*! Export as XTML to directory */
   164     void exportXML(QString dir="", bool askForName=true);
   165 
   166     void clear();						//!< Clear map
   167     void copy();						//!< Copy to clipboard
   168     void redo();						//!< Redo last action
   169 	bool isRedoAvailable();				//!< True, if redo is available
   170     void undo();						//!< Undo last action
   171 	bool isUndoAvailable();				//!< True, if undo is available
   172 	void gotoHistoryStep (int);			//!< Goto a specifig step in history
   173 private:	
   174     void addMapReplaceInt(const QString & undoSel, const QString & path);
   175     void addMapInsertInt (const QString & path, int pos);
   176     void pasteNoSave(const int &n);		//!< paste clipboard to branch
   177 public:	
   178     void paste();		//!< Paste clipboard to branch and backup
   179     void cut();			//!< Cut to clipboard (and copy)
   180 	/*! \brief Move absolutly
   181 
   182 		Move absolutly to (x,y).
   183 	*/	
   184     void move    (const double &x, const double &y);
   185 	/*! \brief Move relativly
   186 
   187 		Move relativly to (x,y).
   188 	*/	
   189     void moveRel (const double &x, const double &y);
   190     void moveBranchUp();	//!< Move branch up
   191     void moveBranchDown();	//!< Move branch down
   192 private:	
   193 	void linkTo(const QString &);
   194 public:	
   195 	QString getHeading (bool &ok,QPoint &p); //!< Get heading, ok if selection is branch
   196 	void setHeading(const QString &);		//!< Set heading of branch	
   197 private:
   198 	void setHeadingInt(const QString &);
   199 	void setVymLinkInt(const QString &);	// Set vymLink for selection
   200 	/*! \brief Add new branch
   201 
   202 		Depending on num the new branch is created
   203 
   204 		-3 above selection as child of selections parent
   205 		-2 as child of selection
   206 		-1 below selection as child of selections parent
   207 		0..n	insert at a specific position in selections parent
   208 		(needed for free relinking)
   209 	*/	
   210     BranchObj* addNewBranchInt(int);		// pos allows to add above/below selection
   211 public:	
   212 	/*! \Add new branch
   213 		
   214 		Depending on num the new branch is created
   215 		-1 above selection
   216 		 0 as child of selection
   217 		 1 below selection
   218 	*/
   219     BranchObj* addNewBranch(int pos);		
   220     BranchObj* addNewBranchBefore();		//!< Insert branch between selection and its parent
   221     void deleteSelection();					//!< Delete selection
   222 	LinkableMapObj* getSelection();			//!< Returns selection
   223 	BranchObj* getSelectedBranch();			// returns selected branch or NULL
   224 	FloatImageObj* getSelectedFloatImage();	// returns selected branch or NULL
   225 	void unselect();						// before changing current noteedit
   226 	void reselect();						// after  changing current noteedit
   227 	bool select(const QString &);			// Select by string
   228 	QString getSelectString();
   229 private:	
   230 	void selectInt(LinkableMapObj*);	
   231 	void selectNextBranchInt();		// Increment number of branch
   232 	void selectPrevBranchInt();		// Decrement number of branch
   233 public:	
   234     void selectUpperBranch();
   235     void selectLowerBranch();
   236     void selectLeftBranch();
   237     void selectRightBranch();
   238     void selectFirstBranch();
   239     void selectLastBranch();
   240     void selectMapBackgroundImage();
   241     void setMapBackgroundImage(const QString &);
   242     void selectMapBackgroundColor();
   243     void setMapBackgroundColor(QColor);
   244     QColor getMapBackgroundColor();
   245     QColor getCurrentHeadingColor();
   246     void colorBranch(QColor);
   247     void colorSubtree(QColor);
   248 	void toggleStandardFlag(QString);
   249     BranchObj* findText(QString,bool);		// Find object
   250     void findReset();						// Reset Find 
   251 	void setURL(const QString &);			// set  URL
   252 	void editURL();							// edit URL
   253 	void editLocalURL();					// edit URL to local file
   254 	QString getURL();						// returns URL of selection or ""
   255 	QStringList getURLs();					// returns URLs of subtree
   256 	void editHeading2URL();					// copy heading to URL
   257 	void editBugzilla2URL();				// create URL to Bugzilla
   258 	void editFATE2URL();					// create URL to FATE
   259 	void editVymLink();						// edit link to another map
   260 	void deleteVymLink();					// delete link to another map
   261 	QString getVymLink();					// return path to map
   262 	QStringList getVymLinks();				// return paths in subtree
   263 	void setHideExport(bool);				// toggle the export flag
   264 	void toggleHideExport();				// toggle the export flag
   265 	void deleteKeepChilds();				// remove but keep childs
   266 	void deleteChilds();					// remove childs
   267 	void editMapInfo();						// dialog to enter author, ...
   268 	void ensureSelectionVisible();		
   269 	void updateSelection();					// update geometry of selection
   270 	void updateActions();					// update e.g. format buttons
   271 	void updateNoteFlag();					// when TextEditor changes
   272 	void setMapAuthor (const QString &);
   273 	void setMapComment(const QString &);
   274 	void setMapLinkStyle (const QString &);	// Set style of link
   275 	LinkableMapObj::Style getMapLinkStyle ();	// requested in LMO
   276 	void setMapDefLinkColor(QColor);		// default color of links
   277 	void setMapLinkColorHintInt();			// color of links
   278 	void setMapLinkColorHint(LinkableMapObj::ColorHint);// color of links
   279 	LinkableMapObj::ColorHint getMapLinkColorHint();
   280 	QColor getMapDefLinkColor();
   281 	void setMapDefXLinkColor(QColor);
   282 	QColor getMapDefXLinkColor();
   283 	void setMapDefXLinkWidth (int);
   284 	int getMapDefXLinkWidth();
   285 	void toggleMapLinkColorHint();			// after changing linkStyles
   286     void selectMapLinkColor();
   287     void selectMapSelectionColor();
   288 private:	
   289     void setSelectionColorInt(QColor);
   290 public:	
   291     void setSelectionColor(QColor);
   292     QColor getSelectionColor();
   293 	bool scrollBranch(BranchObj*);
   294 	bool unscrollBranch(BranchObj*);
   295     void toggleScroll();
   296     void unscrollChilds();
   297 private:	
   298 	FloatImageObj* loadFloatImageInt (QString);
   299 public:	
   300 	void loadFloatImage ();
   301 private:	
   302 	void saveFloatImageInt (FloatImageObj*, const QString &, const QString &);
   303 public:	
   304 	void saveFloatImage ();
   305 	void setFrameType(const FrameObj::FrameType &);
   306 	void setFrameType(const QString &);
   307 	void setFramePenColor (const QColor &);
   308 	void setFrameBrushColor (const QColor &);
   309 	void setFramePadding (const int &);
   310 	void setFrameBorderWidth (const int &);
   311 	void setIncludeImagesVer(bool);
   312 	void setIncludeImagesHor(bool);
   313 	void setHideLinkUnselected (bool);
   314 	bool getHideLinkUnselected ();
   315 private:	
   316     void importDirInt(BranchObj *,QDir);
   317     void importDirInt(const QString&);
   318 public:	
   319     void importDir();
   320 	void followXLink (int);
   321 	void editXLink (int);
   322     void testFunction1();					// just testing new stuff
   323     void testFunction2();					// just testing new stuff
   324 											// set /mainwindo/showTestMenu=true...
   325 
   326 protected:
   327 	virtual void contextMenuEvent ( QContextMenuEvent *e );
   328     virtual void keyPressEvent(QKeyEvent*);
   329     virtual void keyReleaseEvent(QKeyEvent*);
   330     virtual void mousePressEvent(QMouseEvent*);
   331     virtual void mouseMoveEvent(QMouseEvent*);
   332     virtual void mouseReleaseEvent(QMouseEvent*);
   333     virtual void mouseDoubleClickEvent(QMouseEvent*);
   334     virtual void resizeEvent( QResizeEvent * );
   335 
   336 	void dragEnterEvent (QDragEnterEvent *);
   337 	void dragMoveEvent (QDragMoveEvent *);
   338 	void dragLeaveEvent (QDragLeaveEvent *);
   339 	void dropEvent (QDropEvent *);
   340 
   341 	void timerEvent(QTimerEvent *event);
   342 
   343 protected:
   344 	void sendSelection();
   345 
   346 public:
   347 	void newServer();
   348 	void connectToServer();
   349 
   350 private slots:	
   351 	void newClient();
   352 	void sendData(const QString &s);
   353 	void readData();
   354 	void displayNetworkError (QAbstractSocket::SocketError);
   355 
   356 	void autosave ();
   357 
   358 private:
   359 	State state;				// State of MapEditor
   360 	QGraphicsScene *mapScene;
   361     MapCenterObj* mapCenter;
   362 	QTimer *autosaveTimer;
   363 
   364 	bool adjustCanvasRequested;	// collect requests until end of user event
   365 	BranchObj *editingBO;		// entering Text into BO
   366 
   367 	QColor defLinkColor;		// default color for links
   368 	QColor defXLinkColor;		// default color for xlinks
   369 	int defXLinkWidth;			// default width for xlinks
   370 	LinkableMapObj::ColorHint linkcolorhint;// use heading color or own color
   371 	LinkableMapObj::Style linkstyle;		// default style for links
   372 
   373     QCursor HandOpenCursor;		// cursor while moving canvas view
   374 	QCursor PickColorCursor;	// cursor while picking color 
   375 	QCursor CopyCursor;			// cursor while picking color 
   376 	QCursor XLinkCursor;		// cursor while picking color 
   377 	bool pickingColor;
   378 	bool drawingLink;			// true while creating a link
   379 	bool copyingObj;			// true while creating a link
   380 	XLinkObj* tmpXLink;
   381 
   382 	Selection xelection;	
   383 
   384 	QString latestSelection;		// select string of latest added object
   385 
   386     MapObj* movingObj;				// moving a MapObj
   387 	MapObj* linkingObj_src;			// part of a link
   388     QPointF movingObj_orgPos;		// org. pos of mouse before move
   389     QPointF movingObj_orgRelPos;	// org. relative pos of mouse before move
   390     QPointF movingObj_start;		// rel. pos of mouse to absPos 
   391     QPointF movingCont_start;		// inital pos of moving Content or
   392     QPointF movingVec;				// how far has Content moved
   393     QPrinter* printer;				// Printing
   394 
   395     bool mapDefault;				// Flag if map is untouched
   396     bool mapChanged;				// Flag if undo is possible
   397 	bool mapUnsaved;				// Flag if map should be saved
   398 
   399 	Parser parser;				// Parser stuff for scripting
   400 
   401 	bool printFrame;			// Print frame around map
   402 	bool printFooter;			// Print footer below map
   403 
   404 	bool zipped;				// should map be zipped
   405 	static	int mapNum;			// unique number for Editor
   406 	QString fileName;			// short name of file (for tab)
   407 	QString filePath;			// path to file which will be saved
   408 	QString fileDir;			// dir where file is saved
   409 	QString destPath;			// path to .vym file (needed for vymlinks)
   410 	QString mapName;			// fileName without ".vym"
   411 
   412 	QString tmpMapDir;			// tmp directory with undo history
   413 	QString histPath;			// Path to history file
   414 	SimpleSettings undoSet;		// undo/redo commands, saved in histPath
   415 	int stepsTotal;				// total number of steps (undos+redos) 
   416 	int curStep;				// Current step in history (ring buffer)
   417 	int curClipboard;			// number of history step, which is the current clipboard
   418 	int redosAvail;				// Available number of redo steps
   419 	int undosAvail;				// Available number of undo steps
   420 	bool blockReposition;		// block while load or undo
   421 	bool blockSaveState;		// block while load or undo
   422 
   423 	BranchObj* itFind;			// next object in find process
   424 	bool EOFind;				// true, if search failed
   425 
   426 	QPoint exportOffset;		// set before export, used in save
   427 	BranchObj::HideTmpMode hidemode;	// true while exporting to hide some stuff
   428 
   429 	NetState netstate;			// offline, client, server
   430 	QTcpServer *tcpServer;		// Act as server in conference mode (experimental)
   431 	QList <QTcpSocket*> clientList;		// List of connected clients
   432 	quint16 sendCounter;		// Increased with every sent command
   433 
   434 	QTcpSocket	*clientSocket;	// socket of this client
   435 	QString server;				// server address of this client
   436 	int port;					// server port of this client
   437 	void displayClientError(QAbstractSocket::SocketError socketError);
   438 
   439 
   440 	int timerId;				// animation timer
   441 	QList <MapObj*> animObjList;// list with animated objects
   442 };
   443 #endif
   444