vymmodel.h
author insilmaril
Wed, 29 Apr 2009 21:40:37 +0000
changeset 761 0301e6109702
parent 760 59614eaf5fbb
child 763 8c028a5d9083
permissions -rw-r--r--
speedup
     1 #ifndef VYMMODEL_H
     2 #define VYMMODEL_H
     3 
     4 #include <QGraphicsScene>
     5 #include <QtNetwork>
     6 
     7 #include "file.h"
     8 #include "mapcenterobj.h"
     9 #include "mapeditor.h"
    10 #include "parser.h"
    11 #include "selection.h"
    12 #include "treeitem.h"
    13 #include "treemodel.h"
    14 
    15 class BranchItem;
    16 class MapCenterItem;
    17 class MapEditor;
    18 
    19 class VymModel : public TreeModel {		
    20 	Q_OBJECT
    21 
    22 ////////////////////////////////////////////
    23 // General housekeeping
    24 ////////////////////////////////////////////
    25 private:
    26 	QGraphicsScene *mapScene;
    27 	QString version;	//!< version string saved in vym file
    28 	QString author;
    29 	QString comment;
    30 	QDate date;
    31 
    32 public:
    33 	VymModel();
    34 	~VymModel ();
    35     void clear();
    36     void init();
    37 	void makeTmpDirectories();		//!< create temporary directories e.g. for history
    38 
    39 	MapEditor* getMapEditor();			// FIXME-2 still necessary?
    40 
    41 	bool isRepositionBlocked();		//!< While load or undo there is no need to update graphicsview
    42 
    43 	void updateActions();			//!< Update buttons in mainwindow
    44 
    45 
    46 ////////////////////////////////////////////
    47 // Load/save 
    48 ////////////////////////////////////////////
    49 private:
    50 
    51 	bool zipped;				// should map be zipped
    52 	static	int mapNum;			// unique number for model used in save/undo
    53 	FileType fileType;			// type of file, e.g. vym, freemind...
    54 	QString fileName;			// short name of file (for tab)
    55 	QString filePath;			// path to file which will be saved
    56 	QString fileDir;			// dir where file is saved
    57 	QString destPath;			// path to .vym file (needed for vymlinks)
    58 	QString mapName;			// fileName without ".vym"
    59 
    60 	QString tmpMapDir;			// tmp directory with undo history
    61 
    62 	QTimer *autosaveTimer;
    63 	QTimer *fileChangedTimer;
    64 	QDateTime fileChangedTime;
    65 
    66 public:
    67 	/*! This function saves all information of the map to disc.
    68 	    saveToDir also calls the functions for all BranchObj and other objects in the map.
    69 		The structure of the map itself is returned as QString and passed back to Main, 
    70 		where saveToDir is called initially
    71 	*/	
    72     QString saveToDir (const QString &tmpdir, const QString &prefix, bool writeflags, const QPointF &offset, TreeItem *saveSel);
    73 
    74 	/*! Save all data in tree*/
    75 	QString saveTreeToDir (const QString&,const QString&,int, const QPointF&);// Save data recursivly to tempdir
    76 
    77 
    78 	/*! \brief Sets filepath, filename and mapname
    79 
    80 	     If the filepath is "/home/tux/map.xml", then the filename will be set
    81 		 to map.xml. The destname is needed for vymLinks, pointing to another map. 
    82 		 The destname holds the real name of the file, after it has been compressed, e.g. "map.vym"
    83 	*/	 
    84 
    85 
    86 	/*! \brief Set File path
    87 
    88 	     The destname is needed to construct the references between maps
    89 	*/	 
    90 	void setFilePath (QString filepath,QString destname);	
    91 	void setFilePath (QString);	//!< Overloaded for convenience
    92 	QString getFilePath ();	//!< Full path e.g. "/home/tux/map.xml"
    93 	QString getFileName ();	//!< e.g. "map.xml"
    94 	QString getMapName ();	//!< e.g. "map"
    95 	QString getDestPath (); //!< e.g. "/home/tux/map.vym"
    96 
    97 	/*! \brief Load map
    98 
    99 		The data is read from file. Depending on LoadMode the current
   100 		selection gets replaced by data or the data is appended.
   101 	*/	
   102     ErrorCode load (QString, const LoadMode &, const FileType& );	// newmap, import/replace selection
   103 
   104 public:
   105 	/*! \brief Save the map to file */
   106     ErrorCode save(const SaveMode &);	
   107 
   108 private:
   109     void addMapReplaceInt(const QString & undoSel, const QString & path);
   110     void addMapInsertInt (const QString & path, int pos);
   111 
   112 	FloatImageObj* loadFloatImageInt (QString);
   113 	void saveFloatImageInt (FloatImageObj*, const QString &, const QString &);
   114 public:	
   115 	void loadFloatImage ();
   116 	void saveFloatImage ();
   117 
   118 private:	
   119     void importDirInt(BranchObj *,QDir);
   120     void importDirInt(const QString&);
   121 public:	
   122     void importDir();
   123 
   124 private slots:
   125 	void autosave ();
   126 	void fileChanged();
   127 
   128 ////////////////////////////////////////////
   129 // history (undo/redo)
   130 ////////////////////////////////////////////
   131 private:
   132     bool mapDefault;			//!< Flag if map is untouched
   133     bool mapChanged;			//!< Flag if undo is possible
   134 	bool mapUnsaved;			//!< Flag if map should be saved
   135 
   136 	QString histPath;			//!< Path to history file
   137 	SimpleSettings undoSet;		//!< undo/redo commands, saved in histPath
   138 	int stepsTotal;				//!< total number of steps (undos+redos) 
   139 	int curStep;				//!< Current step in history (ring buffer)
   140 	int curClipboard;			//!< number of history step, which is the current clipboard
   141 	int redosAvail;				//!< Available number of redo steps
   142 	int undosAvail;				//!< Available number of undo steps
   143 	bool blockReposition;		//!< block while load or undo
   144 	bool blockSaveState;		//!< block while load or undo
   145 public:
   146 	bool isDefault();			//!< true, if map is still the empty default map
   147 	void makeDefault();			//!< Reset changelog, declare this as default map
   148     bool hasChanged()	;		//!< true, if something has changed and is not saved yet
   149 	void setChanged();			//!< called from TextEditor via LinkableMapObj
   150 
   151 	/*! \brief Get name of object
   152 	  
   153 	  Returns heading of a branch or name of an object for use in comment
   154 	  of undo/redo history
   155 	*/ 
   156 	QString getObjectName(const LinkableMapObj*);	
   157 	QString getObjectName(const TreeItem*);	
   158 
   159     void redo();						//!< Redo last action
   160 	bool isRedoAvailable();				//!< True, if redo is available
   161     void undo();						//!< Undo last action
   162 	bool isUndoAvailable();				//!< True, if undo is available
   163 	void gotoHistoryStep (int);			//!< Goto a specifig step in history
   164 
   165 
   166 	QString getHistoryPath();			//!< Path to directory containing the history
   167 
   168 	/*! \brief Save the current changes in map 
   169 
   170 		Two commands and selections are saved:
   171 
   172 			- undocommand and undoselection to undo the change
   173 			- redocommand and redoselection to redo the action after an undo
   174 
   175 		Additionally a comment is logged. 
   176 
   177 	*/	
   178     void saveState(
   179 		const SaveMode& savemode, 
   180 		const QString &undoSelection, 
   181 		const QString &undoCommand, 
   182 		const QString &redoSelection, 
   183 		const QString &redoCommand, 
   184 		const QString &comment, 
   185 		TreeItem *saveSelection);
   186 	/*! Overloaded for convenience */
   187     void saveStateChangingPart(
   188 		TreeItem *undoSelection, 
   189 		TreeItem* redoSelection, 
   190 		const QString &redoCommand, 
   191 		const QString &comment);
   192 	/*! Overloaded for convenience */
   193     void saveStateRemovingPart(
   194 		TreeItem *redoSelection, 
   195 		const QString &comment);
   196 	/*! Overloaded for convenience */
   197     void saveState(
   198 		TreeItem *undoSelection, 
   199 		const QString &undoCommand, 
   200 		TreeItem *redoSelection, 
   201 		const QString &redoCommand, 
   202 		const QString &comment); 
   203 	/*! Overloaded for convenience */
   204     void saveState(
   205 		const QString &undoSelection, 
   206 		const QString &undoCommand, 
   207 		const QString &redoSelection, 
   208 		const QString &redoCommand, 
   209 		const QString &comment) ;
   210     void saveState(
   211 		const QString &undoCommand, 
   212 		const QString &redoCommand, 
   213 		const QString &comment) ;
   214 
   215 
   216 ////////////////////////////////////////////
   217 // unsorted so far
   218 ////////////////////////////////////////////
   219 public:
   220 	void setScene(QGraphicsScene *s);
   221 	QGraphicsScene *getScene();
   222 
   223     TreeItem* findBySelectString (QString s);		
   224     TreeItem* findID (const QString &s);				// find MapObj by previously set ID
   225 
   226 
   227 ////////////////////////////////////////////
   228 // Interface 
   229 ////////////////////////////////////////////
   230 public:
   231 	void setVersion(const  QString &);
   232 	void setAuthor  (const QString &);
   233 	QString getAuthor ();
   234 	void setComment (const QString &);
   235 	QString getComment ();
   236 	QString getDate();
   237 	int branchCount();
   238 
   239 public:	
   240 	void setHeading(const QString &);		//!< Set heading of branch	
   241 //	QString getHeading (bool &ok,QPoint &p); //!< Get heading, ok if selection is branch
   242 
   243 private:
   244 	BranchItem* findCurrent;		// next object in find process
   245 	BranchItem* findPrevious;		// next object in find process
   246 	bool EOFind;				// true, if search failed
   247 public:
   248     BranchItem* findText(QString,bool);		// Find object
   249     void findReset();						// Reset Search
   250 
   251 	void setURL(const QString &url);
   252 	QString getURL();						// returns URL of selection or ""
   253 	QStringList getURLs();					// returns URLs of subtree
   254 
   255 	void linkFloatImageTo(const QString &);
   256 
   257 	void setFrameType(const FrameObj::FrameType &);
   258 	void setFrameType(const QString &);
   259 	void setFramePenColor (const QColor &);
   260 	void setFrameBrushColor (const QColor &);
   261 	void setFramePadding (const int &);
   262 	void setFrameBorderWidth (const int &);
   263 	void setIncludeImagesVer(bool);
   264 	void setIncludeImagesHor(bool);
   265 	void setHideLinkUnselected (bool);
   266 
   267 	/*! Should object be hidden in exports (clouded)? */
   268 	void setHideExport(bool);			
   269 
   270 	/*! Should object be hidden in exports (clouded)? */
   271 	void toggleHideExport();		
   272 
   273     void copy();						//!< Copy to clipboard
   274 private:	
   275     void pasteNoSave(const int &n);		//!< paste clipboard to branch
   276 public:	
   277     void paste();		//!< Paste clipboard to branch and backup
   278     void cut();			//!< Cut to clipboard (and copy)
   279 
   280     void moveBranchUp();	//!< Move branch up
   281     void moveBranchDown();	//!< Move branch down
   282 	void sortChildren();	//!< Sort children lexically
   283 
   284 	// The create methods are used to quickly parse a XML file
   285 	MapCenterItem* createMapCenter();	//!< Create and select MapCenter
   286 	BranchItem* createBranch();			//!< Create and select Branch
   287 	TreeItem* createImage();			//!< Create and select image
   288 
   289 	/*! \brief Add new mapcenter
   290 
   291 	    Disclaimer: Still experimental, not fully supported yet.
   292 	*/	
   293 	MapCenterItem* addMapCenter();
   294 private:	
   295 	MapCenterItem* addMapCenter(QPointF absPos);
   296 
   297 	/*! \brief Add new branch
   298 
   299 		Depending on num the new branch is created
   300 
   301 		-3 above selection as child of selections parent
   302 		-2 as child of selection
   303 		-1 below selection as child of selections parent
   304 		0..n	insert at a specific position in selections parent
   305 		(needed for free relinking)
   306 	*/	
   307 
   308 private:	
   309     BranchItem* addNewBranchInt(int);		// pos allows to add above/below selection
   310 public:	
   311 	/*! \Add new branch
   312 		
   313 		Depending on num the new branch is created
   314 		-1 above selection
   315 		 0 as child of selection
   316 		 1 below selection
   317 	*/
   318     BranchItem* addNewBranch(int pos);		
   319     BranchItem* addNewBranchBefore();		//!< Insert branch between selection and its parent
   320 	BranchItem* relinkBranch (BranchItem* branch, BranchItem* dst, int pos =-1);	//! Relink branch to dst at position pos
   321     void deleteSelection();					//!< Delete selection
   322 	void deleteKeepChildren();				//!< remove branch, but keep children
   323 	void deleteChildren();					//!< keep branch, but remove children
   324 
   325 private:	
   326 	bool scrollBranch(BranchItem *);
   327 	bool unscrollBranch(BranchItem *);
   328 public:	
   329     void toggleScroll();
   330     void unscrollChildren();
   331 
   332     void addFloatImage(const QPixmap &img);
   333 
   334     void colorBranch(QColor);
   335     void colorSubtree(QColor);
   336 	QColor getCurrentHeadingColor();
   337 
   338 
   339 	void editURL();							// edit URL
   340 	void editLocalURL();					// edit URL to local file
   341 	void editHeading2URL();					// copy heading to URL
   342 	void editBugzilla2URL();				// create URL to Bugzilla
   343 	void editFATE2URL();					// create URL to FATE
   344 	void editVymLink();						// edit link to another map
   345 	void setVymLink (const QString &);	// Set vymLink for selection
   346 	void deleteVymLink();					// delete link to another map
   347 	QString getVymLink();					// return path to map
   348 	QStringList getVymLinks();				// return paths in subtree
   349 	void followXLink (int);
   350 	void editXLink (int);
   351 
   352 
   353 
   354 
   355 ////////////////////////////////////////////
   356 // Scripting
   357 ////////////////////////////////////////////
   358 public:	
   359 
   360 	/* \brief Process one command and its parameters */
   361     void parseAtom (const QString &atom);	
   362 
   363 	/* \brief Runs the script */
   364 	void runScript (QString script);
   365 
   366 private:
   367 	Parser parser;
   368 
   369 ////////////////////////////////////////////
   370 // Exports
   371 ////////////////////////////////////////////
   372 private:
   373 	TreeItem::HideTmpMode hidemode;	// true while exporting to hide some stuff
   374 
   375 public:
   376 	/*! Set or unset temporary hiding of objects during export  */
   377 	void setExportMode (bool);
   378 
   379 	/*! Save as image */
   380     void exportImage (QString fname="",bool askForName=true,QString format="PNG");
   381 
   382 
   383 	/*! Export as XTML to directory */
   384     void exportXML(QString dir="", bool askForName=true);
   385 
   386 	/*! Export as ASCII text to file */
   387 	void exportASCII (QString fname="",bool askForName=true);  
   388 
   389 	/*! Export as XHTML to directory */
   390     void exportXHTML(const QString& dir="", bool askForName=true);	
   391 
   392     /*! Export as OpenOfficeOrg presentation */
   393     void exportOOPresentation(const QString &,const QString &);	
   394 
   395 
   396 ////////////////////////////////////////////
   397 // View related
   398 ////////////////////////////////////////////
   399 public:
   400 	void registerEditor (QWidget *);
   401 	void unregisterEditor (QWidget *);
   402 
   403 private: 
   404 	QPointF contextPos;					//!< local position during context menu
   405 public:
   406 	void setContextPos (QPointF);		//!< local position during context menu
   407 	void unsetContextPos ();			//!< forget local position after context menu
   408 
   409 	void updateNoteFlag();				//!< Signal origination in TextEditor
   410     void updateRelPositions();
   411 
   412 	QRectF getTotalBBox();
   413 	void reposition();					//!< Call reposition for all MCOs
   414 	void setHideTmpMode (TreeItem::HideTmpMode mode);	
   415 
   416 	QPolygonF shape(BranchObj *bo);		//!< Returns arbitrary shape of subtree
   417 	void moveAway (LinkableMapObj *lmo);//!< Autolayout: Move all out of the way
   418 
   419 	//void ensureSelectionVisible();		//!< Show selection in all views
   420 
   421 	void emitNoteHasChanged (TreeItem *ti);
   422 	void emitDataHasChanged (TreeItem *ti);
   423 
   424 signals:
   425 	void noteHasChanged (QModelIndex ix);
   426 	void dataHasChanged (QModelIndex ix);	//FIXME-3 necessary? There is dataChanged in AbstractModel
   427 	void newChildObject(QModelIndex ix);
   428 
   429 private:
   430 	MapEditor *mapEditor;
   431 
   432 	QColor defLinkColor;		// default color for links
   433 	QColor defXLinkColor;		// default color for xlinks
   434 	int defXLinkWidth;			// default width for xlinks
   435 	LinkableMapObj::ColorHint linkcolorhint;// use heading color or own color
   436 	LinkableMapObj::Style linkstyle;		// default style for links
   437 
   438 private:
   439     QPixmap getPixmap();
   440 
   441 public:
   442 	void setMapLinkStyle (const QString &);	// Set style of link
   443 	LinkableMapObj::Style getMapLinkStyle ();	// requested in LMO
   444 	void setMapDefLinkColor(QColor);		// default color of links
   445 	void setMapLinkColorHintInt();			// color of links
   446 	void setMapLinkColorHint(LinkableMapObj::ColorHint);// color of links
   447 	void toggleMapLinkColorHint();			// after changing linkStyles
   448     void selectMapBackgroundImage();
   449     void setMapBackgroundImage(const QString &);
   450     void selectMapBackgroundColor();
   451     void setMapBackgroundColor(QColor);
   452     QColor getMapBackgroundColor();
   453 
   454 
   455 	LinkableMapObj::ColorHint getMapLinkColorHint();
   456 	QColor getMapDefLinkColor();
   457 	void setMapDefXLinkColor(QColor);
   458 	QColor getMapDefXLinkColor();
   459 	void setMapDefXLinkWidth (int);
   460 	int getMapDefXLinkWidth();
   461 
   462 	/*!  Move absolutly to (x,y).  */	
   463     void move    (const double &x, const double &y);
   464 
   465 	/*!  Move relativly to (x,y).  */	
   466     void moveRel (const double &x, const double &y);
   467 
   468 ////////////////////////////////////////////
   469 // Animation  **experimental**
   470 ////////////////////////////////////////////
   471 private:	
   472 	QTimer *animationTimer;
   473 	bool animationUse;
   474 	uint animationTicks;
   475 	uint animationInterval;
   476 	int timerId;				// animation timer
   477 	QList <MapObj*> animObjList;// list with animated objects
   478 
   479 private slots:
   480 	void animate();						//!< Called by timer to animate stuff
   481 public:
   482 	void startAnimation(BranchObj *bo, const QPointF &start, const QPointF &dest);
   483 	void stopAnimation(MapObj *mo);
   484 
   485 ////////////////////////////////////////////
   486 // Network related 
   487 ////////////////////////////////////////////
   488 public:
   489     /*! \brief Networking states
   490 		
   491 		In Network modus we want to switch of saveState, autosave, ...
   492 	*/
   493 	enum NetState {
   494 		Offline,			//!< Offline
   495 		Client,				//!< I am the client and connected to server
   496 		Server				//!< I am the server
   497 	};
   498 
   499 private:
   500 	// Network connections **Experimental**
   501 	NetState netstate;			// offline, client, server
   502 	QTcpServer *tcpServer;		// Act as server in conference mode (experimental)
   503 	QList <QTcpSocket*> clientList;		// List of connected clients
   504 	quint16 sendCounter;		// Increased with every sent command
   505 
   506 	QTcpSocket	*clientSocket;	// socket of this client
   507 	QString server;				// server address of this client
   508 	int port;					// server port of this client
   509 
   510 
   511 
   512 protected:
   513 	void sendSelection();
   514 
   515 public:
   516 	void newServer();
   517 	void connectToServer();
   518 
   519 private slots:	
   520 	void newClient();
   521 	void sendData(const QString &s);
   522 	void readData();
   523 	void displayNetworkError (QAbstractSocket::SocketError);
   524 
   525 private:	
   526 	void displayClientError(QAbstractSocket::SocketError socketError);
   527 
   528 
   529 ////////////////////////////////////////////
   530 // Selection related 
   531 ////////////////////////////////////////////
   532 private:
   533 	Selection selection;
   534 	TreeItem *latestAddedItem;				// latest added object, reset on setChanged()
   535 
   536 signals:
   537 	void selectionChanged(const QItemSelection &, const QItemSelection &);
   538 
   539 public:
   540 	void setSelectionModel(QItemSelectionModel *);		// Set common selectionModel
   541 	QItemSelectionModel* getSelectionModel();
   542 
   543 	void setSelectionBlocked(bool);
   544 	bool isSelectionBlocked();
   545 
   546 	bool select ();							// select by using common QItemSlectionModel
   547 	bool select (const QString &);			// Select by string
   548 	bool select (LinkableMapObj *lmo);		// Select by pointer to LMO
   549 	bool select (TreeItem *ti );			// Select by point to TreeItem
   550 	void unselect();
   551 	void reselect();
   552 
   553 	void ensureSelectionVisible();			//!< Show selection in all views
   554 
   555 //	void selectInt(LinkableMapObj*);	//FIXME-4
   556 
   557 private:	
   558 	void selectNextBranchInt();		// Increment number of branch
   559 	void selectPrevBranchInt();		//! Select the branch which would be above in vymmap view
   560 	void selectAboveBranchInt();	//! Select the branch which would be above current selection in TreeView
   561 	void selectBelowBranchInt();		// Increment number of branch
   562 public:	
   563     void selectUpperBranch();
   564     void selectLowerBranch();
   565     void selectLeftBranch();
   566     void selectRightBranch();
   567     void selectFirstBranch();
   568     void selectLastBranch();
   569 	void selectLastSelectedBranch();
   570 	void selectParent();
   571 
   572 public:
   573 	TreeItem::Type selectionType();
   574 	LinkableMapObj* getSelectedLMO();
   575 	BranchObj* getSelectedBranchObj();	// FIXME-2 replace by item...
   576 	BranchItem* getSelectedBranchItem();
   577 	TreeItem* getSelectedItem();
   578 	QModelIndex getSelectedIndex();
   579 	FloatImageObj* getSelectedFloatImage();
   580 	QString getSelectString ();
   581 	QString getSelectString (LinkableMapObj *lmo);
   582 	QString getSelectString (TreeItem *item);
   583 	
   584 	
   585 	/*
   586 public slots:
   587 	void changeSelection (const QItemSelection &newSel, const QItemSelection &delSel);
   588 	*/
   589 
   590 public:
   591 	void updateSelection(const QItemSelection &oldsel);
   592 	void updateSelection();
   593 	void selectMapLinkColor();
   594     void selectMapSelectionColor();
   595 private:	
   596     void setSelectionColorInt(QColor);
   597 	QItemSelectionModel *selModel;
   598 
   599 public:	
   600     void setSelectionColor(QColor);
   601     QColor getSelectionColor();
   602 };
   603 
   604 #endif