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