vymmodel.h
author insilmaril
Wed, 16 Jul 2008 10:46:14 +0000
changeset 721 12958f987bcf
parent 696 0c2d74acf035
child 722 462d39502273
permissions -rw-r--r--
Started to restructure for later use of Model/View
     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 	void removeSelection ();								// remove selected object
   227 
   228 	QString saveToDir (const QString&,const QString&,int, const QPointF&);// Save data recursivly to tempdir
   229 
   230 
   231 ////////////////////////////////////////////
   232 // Interface 
   233 ////////////////////////////////////////////
   234 public:
   235 	void setVersion(const  QString &);
   236 	void setAuthor  (const QString &);
   237 	QString getAuthor ();
   238 	void setComment (const QString &);
   239 	QString getComment ();
   240 	QString getDate();
   241 
   242 public:	
   243 	void setHeading(const QString &);		//!< Set heading of branch	
   244 	QString getHeading (bool &ok,QPoint &p); //!< Get heading, ok if selection is branch
   245 private:
   246 	void setHeadingInt(const QString &);
   247 
   248 private:
   249 	BranchObj* itFind;			// next object in find process
   250 	bool EOFind;				// true, if search failed
   251 public:
   252     BranchObj* findText(QString,bool);		// Find object
   253     void findReset();						// Reset Search
   254 
   255 	void setURL(const QString &url);
   256 	QString getURL();						// returns URL of selection or ""
   257 	QStringList getURLs();					// returns URLs of subtree
   258 
   259 	void linkFloatImageTo(const QString &);
   260 
   261 	void setFrameType(const FrameObj::FrameType &);
   262 	void setFrameType(const QString &);
   263 	void setFramePenColor (const QColor &);
   264 	void setFrameBrushColor (const QColor &);
   265 	void setFramePadding (const int &);
   266 	void setFrameBorderWidth (const int &);
   267 	void setIncludeImagesVer(bool);
   268 	void setIncludeImagesHor(bool);
   269 	void setHideLinkUnselected (bool);
   270 
   271 	/*! Should object be hidden in exports (clouded)? */
   272 	void setHideExport(bool);			
   273 
   274 	/*! Should object be hidden in exports (clouded)? */
   275 	void toggleHideExport();		
   276 
   277     void copy();						//!< Copy to clipboard
   278 private:	
   279     void pasteNoSave(const int &n);		//!< paste clipboard to branch
   280 public:	
   281     void paste();		//!< Paste clipboard to branch and backup
   282     void cut();			//!< Cut to clipboard (and copy)
   283 
   284     void moveBranchUp();	//!< Move branch up
   285     void moveBranchDown();	//!< Move branch down
   286 	void sortChildren();	//!< Sort children lexically
   287 
   288 	/*! \brief Add new mapcenter
   289 
   290 	    Disclaimer: Still experimental, not fully supported yet.
   291 	*/	
   292 	MapCenterObj* addMapCenter();
   293 private:	
   294 	MapCenterObj* addMapCenter(QPointF absPos);
   295 public:	
   296 	MapCenterObj* removeMapCenter(MapCenterObj *mco);
   297 
   298 	/*! \brief Add new branch
   299 
   300 		Depending on num the new branch is created
   301 
   302 		-3 above selection as child of selections parent
   303 		-2 as child of selection
   304 		-1 below selection as child of selections parent
   305 		0..n	insert at a specific position in selections parent
   306 		(needed for free relinking)
   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 	void updateNoteFlag();				//!< Signal origination in TextEditor
   402     void updateRelPositions();
   403 
   404 	QRectF getTotalBBox();
   405 	void reposition();					//!< Call reposition for all MCOs
   406 	void setHideTmpMode (HideTmpMode mode);	
   407 
   408 	QPolygonF shape(BranchObj *bo);		//!< Returns arbitrary shape of subtree
   409 	void moveAway (LinkableMapObj *lmo);//!< Autolayout: Move all out of the way
   410 
   411 	//void ensureSelectionVisible();		//!< Show selection in all views
   412 
   413 private:
   414 	MapEditor *mapEditor;
   415 
   416 	QColor defLinkColor;		// default color for links
   417 	QColor defXLinkColor;		// default color for xlinks
   418 	int defXLinkWidth;			// default width for xlinks
   419 	LinkableMapObj::ColorHint linkcolorhint;// use heading color or own color
   420 	LinkableMapObj::Style linkstyle;		// default style for links
   421 
   422 private:
   423     QPixmap getPixmap();
   424 
   425 public:
   426 	void setMapLinkStyle (const QString &);	// Set style of link
   427 	LinkableMapObj::Style getMapLinkStyle ();	// requested in LMO
   428 	void setMapDefLinkColor(QColor);		// default color of links
   429 	void setMapLinkColorHintInt();			// color of links
   430 	void setMapLinkColorHint(LinkableMapObj::ColorHint);// color of links
   431 	void toggleMapLinkColorHint();			// after changing linkStyles
   432     void selectMapBackgroundImage();
   433     void setMapBackgroundImage(const QString &);
   434     void selectMapBackgroundColor();
   435     void setMapBackgroundColor(QColor);
   436     QColor getMapBackgroundColor();
   437 
   438 
   439 	LinkableMapObj::ColorHint getMapLinkColorHint();
   440 	QColor getMapDefLinkColor();
   441 	void setMapDefXLinkColor(QColor);
   442 	QColor getMapDefXLinkColor();
   443 	void setMapDefXLinkWidth (int);
   444 	int getMapDefXLinkWidth();
   445 
   446 	/*!  Move absolutly to (x,y).  */	
   447     void move    (const double &x, const double &y);
   448 
   449 	/*!  Move relativly to (x,y).  */	
   450     void moveRel (const double &x, const double &y);
   451 
   452 ////////////////////////////////////////////
   453 // Animation  **experimental**
   454 ////////////////////////////////////////////
   455 private:	
   456 	QTimer *animationTimer;
   457 	bool animationUse;
   458 	uint animationTicks;
   459 	uint animationInterval;
   460 	int timerId;				// animation timer
   461 	QList <MapObj*> animObjList;// list with animated objects
   462 
   463 private slots:
   464 	void animate();						//!< Called by timer to animate stuff
   465 public:
   466 	void startAnimation(const QPointF &start, const QPointF &dest);
   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