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