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