vymmodel.h
author insilmaril
Mon, 29 Jun 2009 10:27:42 +0000
changeset 777 8acac4fade1b
parent 776 25e634a7e1dc
child 781 6cbf3c9cbd21
permissions -rw-r--r--
more fixes
     1 #ifndef VYMMODEL_H
     2 #define VYMMODEL_H
     3 
     4 #include <QGraphicsScene>
     5 #include <QtNetwork>
     6 
     7 #include "file.h"
     8 #include "imageitem.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 	ImageItem* loadFloatImageInt (BranchItem *dst,QString);
   111 	void saveFloatImageInt (ImageItem*, 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(LinkableMapObj*);	
   155 	QString getObjectName(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 
   254 	void setFrameType(const FrameObj::FrameType &);
   255 	void setFrameType(const QString &);
   256 	void setFramePenColor (const QColor &);
   257 	void setFrameBrushColor (const QColor &);
   258 	void setFramePadding (const int &);
   259 	void setFrameBorderWidth (const int &);
   260 	void setIncludeImagesVer(bool);
   261 	void setIncludeImagesHor(bool);
   262 	void setHideLinkUnselected (bool);
   263 
   264 	/*! Should object be hidden in exports (clouded)? */
   265 	void setHideExport(bool);			
   266 
   267 	/*! Should object be hidden in exports (clouded)? */
   268 	void toggleHideExport();		
   269 
   270     void copy();						//!< Copy to clipboard
   271 private:	
   272     void pasteNoSave(const int &n);		//!< paste clipboard to branch
   273 public:	
   274     void paste();		//!< Paste clipboard to branch and backup
   275     void cut();			//!< Cut to clipboard (and copy)
   276 
   277     void moveUp();	//!< Move branch up
   278     void moveDown();	//!< Move branch down
   279 	void sortChildren();	//!< Sort children lexically
   280 
   281 	// The create methods are used to quickly parse a XML file
   282 	BranchItem* createMapCenter();				//!< Create MapCenter 
   283 	BranchItem* createBranch(BranchItem *dst);	//!< Create Branch
   284 	ImageItem* createImage(BranchItem *dst);		//!< Create image
   285 
   286 	/*! \brief Add new mapcenter
   287 
   288 	    Disclaimer: Still experimental, not fully supported yet.
   289 	*/	
   290 	BranchItem* addMapCenter();
   291 private:	
   292 	BranchItem* addMapCenter(QPointF absPos);
   293 
   294 	/*! \brief Add new branch
   295 
   296 		Depending on num the new branch is created
   297 
   298 		-3 above selection as child of selections parent
   299 		-2 as child of selection
   300 		-1 below selection as child of selections parent
   301 		0..n	insert at a specific position in selections parent
   302 		(needed for free relinking)
   303 	*/	
   304 
   305 private:	
   306     BranchItem* addNewBranchInt(BranchItem *dst, int pos);	// pos allows to add above/below selection
   307 public:	
   308 	/*! \Add new branch
   309 		
   310 		Depending on num the new branch is created
   311 		-1 above selection
   312 		 0 as child of selection
   313 		 1 below selection
   314 	*/
   315     BranchItem* addNewBranch(int pos=0);		
   316     BranchItem* addNewBranchBefore();		//!< Insert branch between selection and its parent
   317 	/*! \brief Relink a branch to a new destination dst 
   318 	    Relinks branch to dst at branch position pos. There is no saveState
   319 		here, as for example moveUp or moving in MapEditor have
   320 		different needs to call saveState
   321 		Returns true if relinking was successful.
   322 	*/	
   323 	bool relinkBranch (BranchItem* branch, BranchItem* dst, int pos =-1);	
   324 	bool relinkImage  (ImageItem* image, BranchItem* dst);	
   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 	TreeItem* deleteItem(TreeItem*);	//!< Delete item and return parent (if parent!= rootItem)
   332 	bool scrollBranch(BranchItem *);
   333 	bool unscrollBranch(BranchItem *);
   334 public:	
   335     void toggleScroll();
   336     void unscrollChildren();
   337 	void emitExpandAll();
   338 signals:	
   339 	void expandAll();
   340 
   341 public:	
   342 	void toggleStandardFlag (const QString &name, FlagRow *master=NULL);
   343     void addFloatImage(const QPixmap &img);
   344 
   345     void colorBranch(QColor);
   346     void colorSubtree(QColor);
   347 	QColor getCurrentHeadingColor();
   348 
   349 
   350 	void editURL();							// edit URL
   351 	void editLocalURL();					// edit URL to local file
   352 	void editHeading2URL();					// copy heading to URL
   353 	void editBugzilla2URL();				// create URL to Bugzilla
   354 	void editFATE2URL();					// create URL to FATE
   355 	void editVymLink();						// edit link to another map
   356 	void setVymLink (const QString &);	// Set vymLink for selection
   357 	void deleteVymLink();					// delete link to another map
   358 	QString getVymLink();					// return path to map
   359 	QStringList getVymLinks();				// return paths in subtree
   360 	void followXLink (int);
   361 	void editXLink (int);
   362 
   363 
   364 
   365 
   366 ////////////////////////////////////////////
   367 // Scripting
   368 ////////////////////////////////////////////
   369 public:	
   370 
   371 	/* \brief Process one command and its parameters */
   372     void parseAtom (const QString &atom);	
   373 
   374 	/* \brief Runs the script */
   375 	void runScript (QString script);
   376 
   377 private:
   378 	Parser parser;
   379 
   380 ////////////////////////////////////////////
   381 // Exports
   382 ////////////////////////////////////////////
   383 private:
   384 	TreeItem::HideTmpMode hidemode;	// true while exporting to hide some stuff
   385 
   386 public:
   387 	/*! Set or unset temporary hiding of objects during export  */
   388 	void setExportMode (bool);
   389 
   390 	/*! Save as image */
   391     void exportImage (QString fname="",bool askForName=true,QString format="PNG");
   392 
   393 
   394 	/*! Export as XTML to directory */
   395     void exportXML(QString dir="", bool askForName=true);
   396 
   397 	/*! Export as ASCII text to file */
   398 	void exportASCII (QString fname="",bool askForName=true);  
   399 
   400 	/*! Export as XHTML to directory */
   401     void exportXHTML(const QString& dir="", bool askForName=true);	
   402 
   403     /*! Export as OpenOfficeOrg presentation */
   404     void exportOOPresentation(const QString &,const QString &);	
   405 
   406 
   407 ////////////////////////////////////////////
   408 // View related
   409 ////////////////////////////////////////////
   410 public:
   411 	void registerEditor (QWidget *);
   412 	void unregisterEditor (QWidget *);
   413 
   414 private: 
   415 	QPointF contextPos;					//!< local position during context menu
   416 public:
   417 	void setContextPos (QPointF);		//!< local position during context menu
   418 	void unsetContextPos ();			//!< forget local position after context menu
   419 
   420 	void updateNoteFlag();				//!< Signal origination in TextEditor
   421     void updateRelPositions();
   422 
   423 	QRectF getTotalBBox();
   424 	void reposition();					//!< Call reposition for all MCOs
   425 	void setHideTmpMode (TreeItem::HideTmpMode mode);	
   426 
   427 	//FIXME-5 QPolygonF shape(BranchObj *bo);		//!< Returns arbitrary shape of subtree
   428 	//FIXME-5 void moveAway (LinkableMapObj *lmo);//!< Autolayout: Move all out of the way
   429 
   430 	void emitNoteHasChanged (TreeItem *ti);
   431 	void emitDataHasChanged (TreeItem *ti);
   432 
   433 signals:
   434 	void noteHasChanged (QModelIndex ix);
   435 	void newChildObject(QModelIndex ix);
   436 
   437 private:
   438 	MapEditor *mapEditor;
   439 
   440 	QColor defLinkColor;		// default color for links
   441 	QColor defXLinkColor;		// default color for xlinks
   442 	int defXLinkWidth;			// default width for xlinks
   443 	LinkableMapObj::ColorHint linkcolorhint;// use heading color or own color
   444 	LinkableMapObj::Style linkstyle;		// default style for links
   445 
   446 private:
   447     QPixmap getPixmap();
   448 
   449 public:
   450 	void setMapLinkStyle (const QString &);	// Set style of link
   451 	LinkableMapObj::Style getMapLinkStyle ();	// requested in LMO
   452 	void setMapDefLinkColor(QColor);		// default color of links
   453 	void setMapLinkColorHintInt();			// color of links
   454 	void setMapLinkColorHint(LinkableMapObj::ColorHint);// color of links
   455 	void toggleMapLinkColorHint();			// after changing linkStyles
   456     void selectMapBackgroundImage();
   457     void setMapBackgroundImage(const QString &);
   458     void selectMapBackgroundColor();
   459     void setMapBackgroundColor(QColor);
   460     QColor getMapBackgroundColor();
   461 
   462 
   463 	LinkableMapObj::ColorHint getMapLinkColorHint();
   464 	QColor getMapDefLinkColor();
   465 	void setMapDefXLinkColor(QColor);
   466 	QColor getMapDefXLinkColor();
   467 	void setMapDefXLinkWidth (int);
   468 	int getMapDefXLinkWidth();
   469 
   470 	/*!  Move absolutly to (x,y).  */	
   471     void move    (const double &x, const double &y);
   472 
   473 	/*!  Move relativly to (x,y).  */	
   474     void moveRel (const double &x, const double &y);
   475 
   476 ////////////////////////////////////////////
   477 // Animation  **experimental**
   478 ////////////////////////////////////////////
   479 private:	
   480 	QTimer *animationTimer;
   481 	bool animationUse;
   482 	uint animationTicks;
   483 	uint animationInterval;
   484 	int timerId;				// animation timer
   485 	QList <MapObj*> animObjList;// list with animated objects //FIXME-2 should go to MapEditor
   486 
   487 private slots:
   488 	void animate();						//!< Called by timer to animate stuff
   489 public:
   490 	void startAnimation(BranchObj *bo, const QPointF &start, const QPointF &dest);
   491 	void stopAnimation(MapObj *mo);
   492 
   493 ////////////////////////////////////////////
   494 // Network related 
   495 ////////////////////////////////////////////
   496 public:
   497     /*! \brief Networking states
   498 		
   499 		In Network modus we want to switch of saveState, autosave, ...
   500 	*/
   501 	enum NetState {
   502 		Offline,			//!< Offline
   503 		Client,				//!< I am the client and connected to server
   504 		Server				//!< I am the server
   505 	};
   506 
   507 private:
   508 	// Network connections **Experimental**
   509 	NetState netstate;			// offline, client, server
   510 	QTcpServer *tcpServer;		// Act as server in conference mode (experimental)
   511 	QList <QTcpSocket*> clientList;		// List of connected clients
   512 	quint16 sendCounter;		// Increased with every sent command
   513 
   514 	QTcpSocket	*clientSocket;	// socket of this client
   515 	QString server;				// server address of this client
   516 	int port;					// server port of this client
   517 
   518 
   519 
   520 protected:
   521 	void sendSelection();
   522 
   523 public:
   524 	void newServer();
   525 	void connectToServer();
   526 
   527 private slots:	
   528 	void newClient();
   529 	void sendData(const QString &s);
   530 	void readData();
   531 	void displayNetworkError (QAbstractSocket::SocketError);
   532 
   533 private:	
   534 	void displayClientError(QAbstractSocket::SocketError socketError);
   535 
   536 
   537 ////////////////////////////////////////////
   538 // Selection related 
   539 ////////////////////////////////////////////
   540 private:
   541 	TreeItem *latestAddedItem;				// latest added object, reset on setChanged()
   542 
   543 public:
   544 	void setSelectionModel(QItemSelectionModel *);		// Set common selectionModel
   545 	QItemSelectionModel* getSelectionModel();
   546 
   547 	void setSelectionBlocked(bool);
   548 	bool isSelectionBlocked();
   549 
   550 	bool select ();							//! select by using common QItemSelectionModel
   551 	bool select (const QString &);			//! Select by string
   552 	bool select (LinkableMapObj *lmo);		//! Select by pointer to LMO
   553 	bool select (TreeItem *ti );			//! Select by pointer to TreeItem
   554 	bool select (const QModelIndex &index);	//! Select by ModelIndex
   555 	void unselect();
   556 	void reselect();
   557 
   558 	void emitShowSelection();				//!< Show selection in all views
   559 signals:
   560 	void showSelection();
   561 
   562 //	void selectInt(LinkableMapObj*);	//FIXME-4
   563 
   564 private:	
   565 	void selectNextBranchInt();		// Increment number of branch
   566 	void selectPrevBranchInt();		//! Select the branch which would be above in vymmap view
   567 	void selectAboveBranchInt();	//! Select the branch which would be above current selection in TreeView
   568 	void selectBelowBranchInt();		// Increment number of branch
   569 public:	
   570     void selectUpperBranch();
   571     void selectLowerBranch();
   572     void selectLeftBranch();
   573     void selectRightBranch();
   574     void selectFirstBranch();
   575     void selectLastBranch();
   576 	void selectLastSelectedBranch();
   577 	void selectParent();
   578 
   579 public:
   580 	TreeItem::Type selectionType();
   581 	LinkableMapObj* getSelectedLMO();
   582 	BranchObj* getSelectedBranchObj();	// FIXME-3 replace by item...
   583 	BranchItem* getSelectedBranchItem();
   584 	TreeItem* getSelectedItem();
   585 	QModelIndex getSelectedIndex();
   586 	ImageItem* getSelectedImageItem();
   587 	QString getSelectString ();
   588 	QString getSelectString (LinkableMapObj *lmo);
   589 	QString getSelectString (TreeItem *item);
   590 	
   591 	
   592 signals:
   593 	void selectionChanged(const QItemSelection &newsel, const QItemSelection &oldsel);
   594 
   595 public slots:
   596 	void updateSelection (const QItemSelection &newSel, const QItemSelection &delSel);
   597 
   598 public:
   599 	void emitSelectionChanged(const QItemSelection &oldsel);
   600 	void emitSelectionChanged();
   601 	void selectMapLinkColor();
   602     void selectMapSelectionColor();
   603 private:	
   604     void setSelectionColorInt(QColor);
   605 	QItemSelectionModel *selModel;
   606 	QString lastSelectString;
   607 	bool selectionBlocked;		//! Used to block changes of selection while editing a heading
   608 
   609 public:	
   610     void setSelectionColor(QColor);
   611     QColor getSelectionColor();
   612 };
   613 
   614 #endif