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