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