vymmodel.h
author insilmaril
Mon, 04 Aug 2008 10:43:06 +0000
changeset 723 11f9124c1cca
parent 722 462d39502273
child 725 7ea31701156e
permissions -rw-r--r--
Adding/moving MCO works again
     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 	MapEditor* getMapEditor();			// FIXME not necessary
    40 
    41 	bool isRepositionBlocked();		//!< While load or undo there is no need to update graphicsview
    42 
    43 	void updateActions();			//!< Update buttons in mainwindow
    44 
    45 
    46 ////////////////////////////////////////////
    47 // Load/save 
    48 ////////////////////////////////////////////
    49 private:
    50 
    51 	bool zipped;				// should map be zipped
    52 	static	int mapNum;			// unique number for model used in save/undo
    53 	FileType fileType;			// type of file, e.g. vym, freemind...
    54 	QString fileName;			// short name of file (for tab)
    55 	QString filePath;			// path to file which will be saved
    56 	QString fileDir;			// dir where file is saved
    57 	QString destPath;			// path to .vym file (needed for vymlinks)
    58 	QString mapName;			// fileName without ".vym"
    59 
    60 	QString tmpMapDir;			// tmp directory with undo history
    61 
    62 	QTimer *autosaveTimer;
    63 	QTimer *fileChangedTimer;
    64 	QDateTime fileChangedTime;
    65 
    66 public:
    67 	/*! This function saves all information of the map to disc.
    68 	    saveToDir also calls the functions for all BranchObj and other objects in the map.
    69 		The structure of the map itself is returned as QString and passed back to Main, 
    70 		where saveToDir is called initially
    71 	*/	
    72     QString saveToDir (const QString &tmpdir, const QString &prefix, bool writeflags, const QPointF &offset, LinkableMapObj *saveSel);
    73 
    74 	/*! \brief Sets filepath, filename and mapname
    75 
    76 	     If the filepath is "/home/tux/map.xml", then the filename will be set
    77 		 to map.xml. The destname is needed for vymLinks, pointing to another map. 
    78 		 The destname holds the real name of the file, after it has been compressed, e.g. "map.vym"
    79 	*/	 
    80 
    81 
    82 	/*! \brief Set File path
    83 
    84 	     The destname is needed to construct the references between maps
    85 	*/	 
    86 	void setFilePath (QString filepath,QString destname);	
    87 	void setFilePath (QString);	//!< Overloaded for convenience
    88 	QString getFilePath ();	//!< Full path e.g. "/home/tux/map.xml"
    89 	QString getFileName ();	//!< e.g. "map.xml"
    90 	QString getMapName ();	//!< e.g. "map"
    91 	QString getDestPath (); //!< e.g. "/home/tux/map.vym"
    92 
    93 	/*! \brief Load map
    94 
    95 		The data is read from file. Depending on LoadMode the current
    96 		selection gets replaced by data or the data is appended.
    97 	*/	
    98     ErrorCode load (QString, const LoadMode &, const FileType& );	// newmap, import/replace selection
    99 
   100 public:
   101 	/*! \brief Save the map to file */
   102     ErrorCode save(const SaveMode &);	
   103 
   104 private:
   105     void addMapReplaceInt(const QString & undoSel, const QString & path);
   106     void addMapInsertInt (const QString & path, int pos);
   107 
   108 	FloatImageObj* loadFloatImageInt (QString);
   109 	void saveFloatImageInt (FloatImageObj*, const QString &, const QString &);
   110 public:	
   111 	void loadFloatImage ();
   112 	void saveFloatImage ();
   113 
   114 private:	
   115     void importDirInt(BranchObj *,QDir);
   116     void importDirInt(const QString&);
   117 public:	
   118     void importDir();
   119 
   120 private slots:
   121 	void autosave ();
   122 	void fileChanged();
   123 
   124 ////////////////////////////////////////////
   125 // history (undo/redo)
   126 ////////////////////////////////////////////
   127 private:
   128     bool mapDefault;			//!< Flag if map is untouched
   129     bool mapChanged;			//!< Flag if undo is possible
   130 	bool mapUnsaved;			//!< Flag if map should be saved
   131 
   132 	QString histPath;			//!< Path to history file
   133 	SimpleSettings undoSet;		//!< undo/redo commands, saved in histPath
   134 	int stepsTotal;				//!< total number of steps (undos+redos) 
   135 	int curStep;				//!< Current step in history (ring buffer)
   136 	int curClipboard;			//!< number of history step, which is the current clipboard
   137 	int redosAvail;				//!< Available number of redo steps
   138 	int undosAvail;				//!< Available number of undo steps
   139 	bool blockReposition;		//!< block while load or undo
   140 	bool blockSaveState;		//!< block while load or undo
   141 public:
   142 	bool isDefault();			//!< true, if map is still the empty default map
   143 	void makeDefault();			//!< Reset changelog, declare this as default map
   144     bool hasChanged()	;		//!< true, if something has changed and is not saved yet
   145 	void setChanged();			//!< called from TextEditor via LinkableMapObj
   146 
   147 	/*! \brief Get name of object
   148 	  
   149 	  Returns heading of a branch or name of an object for use in comment
   150 	  of undo/redo history
   151 	*/ 
   152 	QString getObjectName(const LinkableMapObj*);	
   153 
   154     void redo();						//!< Redo last action
   155 	bool isRedoAvailable();				//!< True, if redo is available
   156     void undo();						//!< Undo last action
   157 	bool isUndoAvailable();				//!< True, if undo is available
   158 	void gotoHistoryStep (int);			//!< Goto a specifig step in history
   159 
   160 
   161 	QString getHistoryPath();			//!< Path to directory containing the history
   162 
   163 	/*! \brief Save the current changes in map 
   164 
   165 		Two commands and selections are saved:
   166 
   167 			- undocommand and undoselection to undo the change
   168 			- redocommand and redoselection to redo the action after an undo
   169 
   170 		Additionally a comment is logged. 
   171 
   172 	*/	
   173     void saveState(
   174 		const SaveMode& savemode, 
   175 		const QString &undoSelection, 
   176 		const QString &undoCommand, 
   177 		const QString &redoSelection, 
   178 		const QString &redoCommand, 
   179 		const QString &comment, 
   180 		LinkableMapObj *saveSelection);
   181 	/*! Overloaded for convenience */
   182     void saveStateChangingPart(
   183 		LinkableMapObj *undoSelection, 
   184 		LinkableMapObj* redoSelection, 
   185 		const QString &redoCommand, 
   186 		const QString &comment);
   187 	/*! Overloaded for convenience */
   188     void saveStateRemovingPart(
   189 		LinkableMapObj *redoSelection, 
   190 		const QString &comment);
   191 	/*! Overloaded for convenience */
   192     void saveState(
   193 		LinkableMapObj *undoSelection, 
   194 		const QString &undoCommand, 
   195 		LinkableMapObj *redoSelection, 
   196 		const QString &redoCommand, 
   197 		const QString &comment); 
   198 	/*! Overloaded for convenience */
   199     void saveState(
   200 		const QString &undoSelection, 
   201 		const QString &undoCommand, 
   202 		const QString &redoSelection, 
   203 		const QString &redoCommand, 
   204 		const QString &comment) ;
   205     void saveState(
   206 		const QString &undoCommand, 
   207 		const QString &redoCommand, 
   208 		const QString &comment) ;
   209 
   210 
   211 ////////////////////////////////////////////
   212 // unsorted so far
   213 ////////////////////////////////////////////
   214 public:
   215 	void setScene(QGraphicsScene *s);
   216 	QGraphicsScene *getScene();
   217 
   218 	BranchObj* first();					
   219 	BranchObj* next(BranchObj *bo);		
   220 
   221     LinkableMapObj* findMapObj(QPointF,LinkableMapObj*);	// find MapObj 
   222     LinkableMapObj* findObjBySelect (const QString &s);		// find MapObj by select string
   223     LinkableMapObj* findID (const QString &s);				// find MapObj by previously set ID
   224 
   225 	QString saveToDir (const QString&,const QString&,int, const QPointF&);// Save data recursivly to tempdir
   226 
   227 
   228 ////////////////////////////////////////////
   229 // Interface 
   230 ////////////////////////////////////////////
   231 public:
   232 	void setVersion(const  QString &);
   233 	void setAuthor  (const QString &);
   234 	QString getAuthor ();
   235 	void setComment (const QString &);
   236 	QString getComment ();
   237 	QString getDate();
   238 
   239 public:	
   240 	void setHeading(const QString &);		//!< Set heading of branch	
   241 	QString getHeading (bool &ok,QPoint &p); //!< Get heading, ok if selection is branch
   242 private:
   243 	void setHeadingInt(const QString &);
   244 
   245 private:
   246 	BranchObj* itFind;			// next object in find process
   247 	bool EOFind;				// true, if search failed
   248 public:
   249     BranchObj* findText(QString,bool);		// Find object
   250     void findReset();						// Reset Search
   251 
   252 	void setURL(const QString &url);
   253 	QString getURL();						// returns URL of selection or ""
   254 	QStringList getURLs();					// returns URLs of subtree
   255 
   256 	void linkFloatImageTo(const QString &);
   257 
   258 	void setFrameType(const FrameObj::FrameType &);
   259 	void setFrameType(const QString &);
   260 	void setFramePenColor (const QColor &);
   261 	void setFrameBrushColor (const QColor &);
   262 	void setFramePadding (const int &);
   263 	void setFrameBorderWidth (const int &);
   264 	void setIncludeImagesVer(bool);
   265 	void setIncludeImagesHor(bool);
   266 	void setHideLinkUnselected (bool);
   267 
   268 	/*! Should object be hidden in exports (clouded)? */
   269 	void setHideExport(bool);			
   270 
   271 	/*! Should object be hidden in exports (clouded)? */
   272 	void toggleHideExport();		
   273 
   274     void copy();						//!< Copy to clipboard
   275 private:	
   276     void pasteNoSave(const int &n);		//!< paste clipboard to branch
   277 public:	
   278     void paste();		//!< Paste clipboard to branch and backup
   279     void cut();			//!< Cut to clipboard (and copy)
   280 
   281     void moveBranchUp();	//!< Move branch up
   282     void moveBranchDown();	//!< Move branch down
   283 	void sortChildren();	//!< Sort children lexically
   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 public:
   527 	void setSelectionBlocked(bool);
   528 	bool isSelectionBlocked();
   529 
   530 	bool select(const QString &);			// Select by string
   531 	bool select(LinkableMapObj *lmo);		// Select by pointer
   532 	void unselect();
   533 	void reselect();
   534 
   535 	void ensureSelectionVisible();			//!< Show selection in all views
   536 
   537 	void selectInt(LinkableMapObj*);	
   538 
   539 private:	
   540 	void selectNextBranchInt();		// Increment number of branch
   541 	void selectPrevBranchInt();		// Decrement number of branch
   542 public:	
   543     void selectUpperBranch();
   544     void selectLowerBranch();
   545     void selectLeftBranch();
   546     void selectRightBranch();
   547     void selectFirstBranch();
   548     void selectLastBranch();
   549 
   550 public:
   551 	Selection::Type selectionType();
   552 	LinkableMapObj* getSelection();
   553 	BranchObj* getSelectedBranch();
   554 	FloatImageObj* getSelectedFloatImage();
   555 	QString getSelectString ();
   556 	QString getSelectString (LinkableMapObj *lmo);
   557 	
   558 	void updateSelection();
   559 	void selectMapLinkColor();
   560     void selectMapSelectionColor();
   561 private:	
   562     void setSelectionColorInt(QColor);
   563 public:	
   564     void setSelectionColor(QColor);
   565     QColor getSelectionColor();
   566 
   567 };
   568 
   569 #endif