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