vymmodel.h
author insilmaril
Wed, 16 Jul 2008 11:56:44 +0000
changeset 722 462d39502273
parent 721 12958f987bcf
child 723 11f9124c1cca
permissions -rw-r--r--
no more segfault when deleting animated part
     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 "xmlobj.h"
    13 
    14 
    15 /*! \brief This will later be divided into Model/View
    16 */
    17 
    18 class VymModel : public QObject,  public XMLObj {		
    19 	Q_OBJECT
    20 
    21 ////////////////////////////////////////////
    22 // General housekeeping
    23 ////////////////////////////////////////////
    24 private:
    25 	QGraphicsScene *mapScene;
    26 	QList <MapCenterObj*> mapCenters;
    27 	QString version;	//!< version string saved in vym file
    28 	QString author;
    29 	QString comment;
    30 	QDate date;
    31 
    32 public:
    33 	VymModel();
    34 	~VymModel ();
    35     void clear();
    36     void init();
    37 	void makeTmpDirectories();		//!< create temporary directories e.g. for history
    38 
    39 	void setMapEditor(MapEditor *me);	// FIXME should not be necessary in Model/View
    40 	MapEditor* getMapEditor();			// FIXME not necessary
    41 
    42 	bool isRepositionBlocked();		//!< While load or undo there is no need to update graphicsview
    43 
    44 	void updateActions();			//!< Update buttons in mainwindow
    45 
    46 
    47 ////////////////////////////////////////////
    48 // Load/save 
    49 ////////////////////////////////////////////
    50 private:
    51 
    52 	bool zipped;				// should map be zipped
    53 	static	int mapNum;			// unique number for model used in save/undo
    54 	FileType fileType;			// type of file, e.g. vym, freemind...
    55 	QString fileName;			// short name of file (for tab)
    56 	QString filePath;			// path to file which will be saved
    57 	QString fileDir;			// dir where file is saved
    58 	QString destPath;			// path to .vym file (needed for vymlinks)
    59 	QString mapName;			// fileName without ".vym"
    60 
    61 	QString tmpMapDir;			// tmp directory with undo history
    62 
    63 	QTimer *autosaveTimer;
    64 	QTimer *fileChangedTimer;
    65 	QDateTime fileChangedTime;
    66 
    67 public:
    68 	/*! This function saves all information of the map to disc.
    69 	    saveToDir also calls the functions for all BranchObj and other objects in the map.
    70 		The structure of the map itself is returned as QString and passed back to Main, 
    71 		where saveToDir is called initially
    72 	*/	
    73     QString saveToDir (const QString &tmpdir, const QString &prefix, bool writeflags, const QPointF &offset, LinkableMapObj *saveSel);
    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 	BranchObj* first();					
   220 	BranchObj* next(BranchObj *bo);		
   221 
   222     LinkableMapObj* findMapObj(QPointF,LinkableMapObj*);	// find MapObj 
   223     LinkableMapObj* findObjBySelect (const QString &s);		// find MapObj by select string
   224     LinkableMapObj* findID (const QString &s);				// find MapObj by previously set ID
   225 
   226 	QString saveToDir (const QString&,const QString&,int, const QPointF&);// Save data recursivly to tempdir
   227 
   228 
   229 ////////////////////////////////////////////
   230 // Interface 
   231 ////////////////////////////////////////////
   232 public:
   233 	void setVersion(const  QString &);
   234 	void setAuthor  (const QString &);
   235 	QString getAuthor ();
   236 	void setComment (const QString &);
   237 	QString getComment ();
   238 	QString getDate();
   239 
   240 public:	
   241 	void setHeading(const QString &);		//!< Set heading of branch	
   242 	QString getHeading (bool &ok,QPoint &p); //!< Get heading, ok if selection is branch
   243 private:
   244 	void setHeadingInt(const QString &);
   245 
   246 private:
   247 	BranchObj* itFind;			// next object in find process
   248 	bool EOFind;				// true, if search failed
   249 public:
   250     BranchObj* findText(QString,bool);		// Find object
   251     void findReset();						// Reset Search
   252 
   253 	void setURL(const QString &url);
   254 	QString getURL();						// returns URL of selection or ""
   255 	QStringList getURLs();					// returns URLs of subtree
   256 
   257 	void linkFloatImageTo(const QString &);
   258 
   259 	void setFrameType(const FrameObj::FrameType &);
   260 	void setFrameType(const QString &);
   261 	void setFramePenColor (const QColor &);
   262 	void setFrameBrushColor (const QColor &);
   263 	void setFramePadding (const int &);
   264 	void setFrameBorderWidth (const int &);
   265 	void setIncludeImagesVer(bool);
   266 	void setIncludeImagesHor(bool);
   267 	void setHideLinkUnselected (bool);
   268 
   269 	/*! Should object be hidden in exports (clouded)? */
   270 	void setHideExport(bool);			
   271 
   272 	/*! Should object be hidden in exports (clouded)? */
   273 	void toggleHideExport();		
   274 
   275     void copy();						//!< Copy to clipboard
   276 private:	
   277     void pasteNoSave(const int &n);		//!< paste clipboard to branch
   278 public:	
   279     void paste();		//!< Paste clipboard to branch and backup
   280     void cut();			//!< Cut to clipboard (and copy)
   281 
   282     void moveBranchUp();	//!< Move branch up
   283     void moveBranchDown();	//!< Move branch down
   284 	void sortChildren();	//!< Sort children lexically
   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 private:	
   307     BranchObj* addNewBranchInt(int);		// 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     BranchObj* addNewBranch(int pos);		
   317     BranchObj* addNewBranchBefore();		//!< Insert branch between selection and its parent
   318     void deleteSelection();					//!< Delete selection
   319 	void deleteKeepChildren();				//!< remove branch, but keep children
   320 	void deleteChildren();					//!< keep branch, but remove children
   321 
   322 private:	
   323 	bool scrollBranch(BranchObj*);
   324 	bool unscrollBranch(BranchObj*);
   325 public:	
   326     void toggleScroll();
   327     void unscrollChildren();
   328 
   329     void addFloatImage(const QPixmap &img);
   330 
   331     void colorBranch(QColor);
   332     void colorSubtree(QColor);
   333 	QColor getCurrentHeadingColor();
   334 
   335 
   336 	void editURL();							// edit URL
   337 	void editLocalURL();					// edit URL to local file
   338 	void editHeading2URL();					// copy heading to URL
   339 	void editBugzilla2URL();				// create URL to Bugzilla
   340 	void editFATE2URL();					// create URL to FATE
   341 	void editVymLink();						// edit link to another map
   342 	void setVymLink (const QString &);	// Set vymLink for selection
   343 	void deleteVymLink();					// delete link to another map
   344 	QString getVymLink();					// return path to map
   345 	QStringList getVymLinks();				// return paths in subtree
   346 	void followXLink (int);
   347 	void editXLink (int);
   348 
   349 
   350 
   351 
   352 ////////////////////////////////////////////
   353 // Scripting
   354 ////////////////////////////////////////////
   355 public:	
   356 
   357 	/* \brief Process one command and its parameters */
   358     void parseAtom (const QString &atom);	
   359 
   360 	/* \brief Runs the script */
   361 	void runScript (QString script);
   362 
   363 private:
   364 	Parser parser;
   365 
   366 ////////////////////////////////////////////
   367 // Exports
   368 ////////////////////////////////////////////
   369 private:
   370 	HideTmpMode hidemode;	// true while exporting to hide some stuff
   371 
   372 public:
   373 	/*! Set or unset temporary hiding of objects during export  */
   374 	void setExportMode (bool);
   375 
   376 	/*! Save as image */
   377     void exportImage (QString fname="",bool askForName=true,QString format="PNG");
   378 
   379 
   380 	/*! Export as XTML to directory */
   381     void exportXML(QString dir="", bool askForName=true);
   382 
   383 	/*! Export as ASCII text to file */
   384 	void exportASCII (QString fname="",bool askForName=true);  
   385 
   386 	/*! Export as XHTML to directory */
   387     void exportXHTML(const QString& dir="", bool askForName=true);	
   388 
   389     /*! Export as OpenOfficeOrg presentation */
   390     void exportOOPresentation(const QString &,const QString &);	
   391 
   392 
   393 ////////////////////////////////////////////
   394 // View related
   395 ////////////////////////////////////////////
   396 public:
   397 	void registerEditor (QWidget *);
   398 	void unregisterEditor (QWidget *);
   399 	void updateNoteFlag();				//!< Signal origination in TextEditor
   400     void updateRelPositions();
   401 
   402 	QRectF getTotalBBox();
   403 	void reposition();					//!< Call reposition for all MCOs
   404 	void setHideTmpMode (HideTmpMode mode);	
   405 
   406 	QPolygonF shape(BranchObj *bo);		//!< Returns arbitrary shape of subtree
   407 	void moveAway (LinkableMapObj *lmo);//!< Autolayout: Move all out of the way
   408 
   409 	//void ensureSelectionVisible();		//!< Show selection in all views
   410 
   411 private:
   412 	MapEditor *mapEditor;
   413 
   414 	QColor defLinkColor;		// default color for links
   415 	QColor defXLinkColor;		// default color for xlinks
   416 	int defXLinkWidth;			// default width for xlinks
   417 	LinkableMapObj::ColorHint linkcolorhint;// use heading color or own color
   418 	LinkableMapObj::Style linkstyle;		// default style for links
   419 
   420 private:
   421     QPixmap getPixmap();
   422 
   423 public:
   424 	void setMapLinkStyle (const QString &);	// Set style of link
   425 	LinkableMapObj::Style getMapLinkStyle ();	// requested in LMO
   426 	void setMapDefLinkColor(QColor);		// default color of links
   427 	void setMapLinkColorHintInt();			// color of links
   428 	void setMapLinkColorHint(LinkableMapObj::ColorHint);// color of links
   429 	void toggleMapLinkColorHint();			// after changing linkStyles
   430     void selectMapBackgroundImage();
   431     void setMapBackgroundImage(const QString &);
   432     void selectMapBackgroundColor();
   433     void setMapBackgroundColor(QColor);
   434     QColor getMapBackgroundColor();
   435 
   436 
   437 	LinkableMapObj::ColorHint getMapLinkColorHint();
   438 	QColor getMapDefLinkColor();
   439 	void setMapDefXLinkColor(QColor);
   440 	QColor getMapDefXLinkColor();
   441 	void setMapDefXLinkWidth (int);
   442 	int getMapDefXLinkWidth();
   443 
   444 	/*!  Move absolutly to (x,y).  */	
   445     void move    (const double &x, const double &y);
   446 
   447 	/*!  Move relativly to (x,y).  */	
   448     void moveRel (const double &x, const double &y);
   449 
   450 ////////////////////////////////////////////
   451 // Animation  **experimental**
   452 ////////////////////////////////////////////
   453 private:	
   454 	QTimer *animationTimer;
   455 	bool animationUse;
   456 	uint animationTicks;
   457 	uint animationInterval;
   458 	int timerId;				// animation timer
   459 	QList <MapObj*> animObjList;// list with animated objects
   460 
   461 private slots:
   462 	void animate();						//!< Called by timer to animate stuff
   463 public:
   464 	void startAnimation(const QPointF &start, const QPointF &dest);
   465 	void stopAnimation(MapObj *mo);
   466 
   467 ////////////////////////////////////////////
   468 // Network related 
   469 ////////////////////////////////////////////
   470 public:
   471     /*! \brief Networking states
   472 		
   473 		In Network modus we want to switch of saveState, autosave, ...
   474 	*/
   475 	enum NetState {
   476 		Offline,			//!< Offline
   477 		Client,				//!< I am the client and connected to server
   478 		Server				//!< I am the server
   479 	};
   480 
   481 private:
   482 	// Network connections **Experimental**
   483 	NetState netstate;			// offline, client, server
   484 	QTcpServer *tcpServer;		// Act as server in conference mode (experimental)
   485 	QList <QTcpSocket*> clientList;		// List of connected clients
   486 	quint16 sendCounter;		// Increased with every sent command
   487 
   488 	QTcpSocket	*clientSocket;	// socket of this client
   489 	QString server;				// server address of this client
   490 	int port;					// server port of this client
   491 
   492 
   493 
   494 protected:
   495 	void sendSelection();
   496 
   497 public:
   498 	void newServer();
   499 	void connectToServer();
   500 
   501 private slots:	
   502 	void newClient();
   503 	void sendData(const QString &s);
   504 	void readData();
   505 	void displayNetworkError (QAbstractSocket::SocketError);
   506 
   507 private:	
   508 	void displayClientError(QAbstractSocket::SocketError socketError);
   509 
   510 
   511 ////////////////////////////////////////////
   512 // Selection related 
   513 ////////////////////////////////////////////
   514 private:
   515 	Selection selection;
   516 	QString latestSelectionString;	// select string of latest added object
   517 
   518 public:
   519 	void setSelectionBlocked(bool);
   520 	bool isSelectionBlocked();
   521 
   522 	bool select(const QString &);			// Select by string
   523 	bool select(LinkableMapObj *lmo);		// Select by pointer
   524 	void unselect();
   525 	void reselect();
   526 
   527 	void ensureSelectionVisible();			//!< Show selection in all views
   528 
   529 	void selectInt(LinkableMapObj*);	
   530 
   531 private:	
   532 	void selectNextBranchInt();		// Increment number of branch
   533 	void selectPrevBranchInt();		// Decrement number of branch
   534 public:	
   535     void selectUpperBranch();
   536     void selectLowerBranch();
   537     void selectLeftBranch();
   538     void selectRightBranch();
   539     void selectFirstBranch();
   540     void selectLastBranch();
   541 
   542 public:
   543 	Selection::Type selectionType();
   544 	LinkableMapObj* getSelection();
   545 	BranchObj* getSelectedBranch();
   546 	FloatImageObj* getSelectedFloatImage();
   547 	QString getSelectString ();
   548 	QString getSelectString (LinkableMapObj *lmo);
   549 	
   550 	void updateSelection();
   551 	void selectMapLinkColor();
   552     void selectMapSelectionColor();
   553 private:	
   554     void setSelectionColorInt(QColor);
   555 public:	
   556     void setSelectionColor(QColor);
   557     QColor getSelectionColor();
   558 
   559 };
   560 
   561 #endif