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