vymmodel.h
author insilmaril
Mon Jul 27 12:53:17 2009 +0000 (2009-07-27)
changeset 782 ffb02a9bb508
parent 777 8acac4fade1b
child 784 9db215a4ad53
permissions -rw-r--r--
Playing with dbus, fixed name of TW translation file
     1 #ifndef VYMMODEL_H
     2 #define VYMMODEL_H
     3 
     4 #include <QGraphicsScene>
     5 #include <QtNetwork>
     6 #include <QtDBus/QDBusConnection>
     7 
     8 #include "file.h"
     9 #include "imageitem.h"
    10 #include "mapeditor.h"
    11 #include "parser.h"
    12 #include "treeitem.h"
    13 #include "treemodel.h"
    14 
    15 class BranchItem;
    16 class MapEditor;
    17 
    18 class VymModel : public TreeModel {		
    19 	Q_OBJECT
    20 
    21 ////////////////////////////////////////////
    22 // General housekeeping
    23 ////////////////////////////////////////////
    24 private:
    25 	QGraphicsScene *mapScene;
    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-2 still 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, TreeItem *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 	ImageItem* loadFloatImageInt (BranchItem *dst,QString);
   112 	void saveFloatImageInt (ImageItem*, 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(LinkableMapObj*);	
   156 	QString getObjectName(TreeItem*);	
   157 
   158     void redo();						//!< Redo last action
   159 	bool isRedoAvailable();				//!< True, if redo is available
   160     void undo();						//!< Undo last action
   161 	bool isUndoAvailable();				//!< True, if undo is available
   162 	void gotoHistoryStep (int);			//!< Goto a specifig step in history
   163 
   164 
   165 	QString getHistoryPath();			//!< Path to directory containing the history
   166 
   167 	/*! \brief Save the current changes in map 
   168 
   169 		Two commands and selections are saved:
   170 
   171 			- undocommand and undoselection to undo the change
   172 			- redocommand and redoselection to redo the action after an undo
   173 
   174 		Additionally a comment is logged. 
   175 
   176 	*/	
   177     void saveState(
   178 		const SaveMode& savemode, 
   179 		const QString &undoSelection, 
   180 		const QString &undoCommand, 
   181 		const QString &redoSelection, 
   182 		const QString &redoCommand, 
   183 		const QString &comment, 
   184 		TreeItem *saveSelection);
   185 	/*! Overloaded for convenience */
   186     void saveStateChangingPart(
   187 		TreeItem *undoSelection, 
   188 		TreeItem* redoSelection, 
   189 		const QString &redoCommand, 
   190 		const QString &comment);
   191 	/*! Overloaded for convenience */
   192     void saveStateRemovingPart(
   193 		TreeItem *redoSelection, 
   194 		const QString &comment);
   195 	/*! Overloaded for convenience */
   196     void saveState(
   197 		TreeItem *undoSelection, 
   198 		const QString &undoCommand, 
   199 		TreeItem *redoSelection, 
   200 		const QString &redoCommand, 
   201 		const QString &comment); 
   202 	/*! Overloaded for convenience */
   203     void saveState(
   204 		const QString &undoSelection, 
   205 		const QString &undoCommand, 
   206 		const QString &redoSelection, 
   207 		const QString &redoCommand, 
   208 		const QString &comment) ;
   209     void saveState(
   210 		const QString &undoCommand, 
   211 		const QString &redoCommand, 
   212 		const QString &comment) ;
   213 
   214 
   215 ////////////////////////////////////////////
   216 // unsorted so far
   217 ////////////////////////////////////////////
   218 public:
   219 	void setScene(QGraphicsScene *s);
   220 	QGraphicsScene *getScene();
   221 
   222     TreeItem* findBySelectString (QString s);		
   223     TreeItem* 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     BranchItem* 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 
   255 	void setFrameType(const FrameObj::FrameType &);
   256 	void setFrameType(const QString &);
   257 	void setFramePenColor (const QColor &);
   258 	void setFrameBrushColor (const QColor &);
   259 	void setFramePadding (const int &);
   260 	void setFrameBorderWidth (const int &);
   261 	void setIncludeImagesVer(bool);
   262 	void setIncludeImagesHor(bool);
   263 	void setHideLinkUnselected (bool);
   264 
   265 	/*! Should object be hidden in exports (clouded)? */
   266 	void setHideExport(bool);			
   267 
   268 	/*! Should object be hidden in exports (clouded)? */
   269 	void toggleHideExport();		
   270 
   271     void copy();						//!< Copy to clipboard
   272 private:	
   273     void pasteNoSave(const int &n);		//!< paste clipboard to branch
   274 public:	
   275     void paste();		//!< Paste clipboard to branch and backup
   276     void cut();			//!< Cut to clipboard (and copy)
   277 
   278     void moveUp();	//!< Move branch up
   279     void moveDown();	//!< Move branch down
   280 	void sortChildren();	//!< Sort children lexically
   281 
   282 	// The create methods are used to quickly parse a XML file
   283 	BranchItem* createMapCenter();				//!< Create MapCenter 
   284 	BranchItem* createBranch(BranchItem *dst);	//!< Create Branch
   285 	ImageItem* createImage(BranchItem *dst);		//!< Create image
   286 
   287 	/*! \brief Add new mapcenter
   288 
   289 	    Disclaimer: Still experimental, not fully supported yet.
   290 	*/	
   291 	BranchItem* addMapCenter();
   292 private:	
   293 	BranchItem* addMapCenter(QPointF absPos);
   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 
   306 private:	
   307     BranchItem* addNewBranchInt(BranchItem *dst, int pos);	// pos allows to add above/below selection
   308 public:	
   309 	/*! \Add new branch
   310 		
   311 		Depending on num the new branch is created
   312 		-1 above selection
   313 		 0 as child of selection
   314 		 1 below selection
   315 	*/
   316     BranchItem* addNewBranch(int pos=0);		
   317     BranchItem* addNewBranchBefore();		//!< Insert branch between selection and its parent
   318 	/*! \brief Relink a branch to a new destination dst 
   319 	    Relinks branch to dst at branch position pos. There is no saveState
   320 		here, as for example moveUp or moving in MapEditor have
   321 		different needs to call saveState
   322 		Returns true if relinking was successful.
   323 	*/	
   324 	bool relinkBranch (BranchItem* branch, BranchItem* dst, int pos =-1);	
   325 	bool relinkImage  (ImageItem* image, BranchItem* dst);	
   326 
   327     void deleteSelection();				//!< Delete selection
   328 	void deleteKeepChildren();			//!< remove branch, but keep children
   329 	void deleteChildren();				//!< keep branch, but remove children
   330 
   331 private:	
   332 	TreeItem* deleteItem(TreeItem*);	//!< Delete item and return parent (if parent!= rootItem)
   333 	bool scrollBranch(BranchItem *);
   334 	bool unscrollBranch(BranchItem *);
   335 public:	
   336     void toggleScroll();
   337     void unscrollChildren();
   338 	void emitExpandAll();
   339 signals:	
   340 	void expandAll();
   341 
   342 public:	
   343 	void toggleStandardFlag (const QString &name, FlagRow *master=NULL);
   344     void addFloatImage(const QPixmap &img);
   345 
   346     void colorBranch(QColor);
   347     void colorSubtree(QColor);
   348 	QColor getCurrentHeadingColor();
   349 
   350 
   351 	void editURL();							// edit URL
   352 	void editLocalURL();					// edit URL to local file
   353 	void editHeading2URL();					// copy heading to URL
   354 	void editBugzilla2URL();				// create URL to Bugzilla
   355 	void editFATE2URL();					// create URL to FATE
   356 	void editVymLink();						// edit link to another map
   357 	void setVymLink (const QString &);	// Set vymLink for selection
   358 	void deleteVymLink();					// delete link to another map
   359 	QString getVymLink();					// return path to map
   360 	QStringList getVymLinks();				// return paths in subtree
   361 	void followXLink (int);
   362 	void editXLink (int);
   363 
   364 
   365 
   366 
   367 ////////////////////////////////////////////
   368 // Scripting
   369 ////////////////////////////////////////////
   370 public:	
   371 
   372 	/* \brief Process one command and its parameters */
   373     void parseAtom (const QString &atom);	
   374 
   375 	/* \brief Runs the script */
   376 	void runScript (QString script);
   377 
   378 private:
   379 	Parser parser;
   380 
   381 ////////////////////////////////////////////
   382 // Exports
   383 ////////////////////////////////////////////
   384 private:
   385 	TreeItem::HideTmpMode hidemode;	// true while exporting to hide some stuff
   386 
   387 public:
   388 	/*! Set or unset temporary hiding of objects during export  */
   389 	void setExportMode (bool);
   390 
   391 	/*! Save as image */
   392     void exportImage (QString fname="",bool askForName=true,QString format="PNG");
   393 
   394 
   395 	/*! Export as XTML to directory */
   396     void exportXML(QString dir="", bool askForName=true);
   397 
   398 	/*! Export as ASCII text to file */
   399 	void exportASCII (QString fname="",bool askForName=true);  
   400 
   401 	/*! Export as XHTML to directory */
   402     void exportXHTML(const QString& dir="", bool askForName=true);	
   403 
   404     /*! Export as OpenOfficeOrg presentation */
   405     void exportOOPresentation(const QString &,const QString &);	
   406 
   407 
   408 ////////////////////////////////////////////
   409 // View related
   410 ////////////////////////////////////////////
   411 public:
   412 	void registerEditor (QWidget *);
   413 	void unregisterEditor (QWidget *);
   414 
   415 private: 
   416 	QPointF contextPos;					//!< local position during context menu
   417 public:
   418 	void setContextPos (QPointF);		//!< local position during context menu
   419 	void unsetContextPos ();			//!< forget local position after context menu
   420 
   421 	void updateNoteFlag();				//!< Signal origination in TextEditor
   422     void updateRelPositions();
   423 
   424 	QRectF getTotalBBox();
   425 	void reposition();					//!< Call reposition for all MCOs
   426 	void setHideTmpMode (TreeItem::HideTmpMode mode);	
   427 
   428 	//FIXME-5 QPolygonF shape(BranchObj *bo);		//!< Returns arbitrary shape of subtree
   429 	//FIXME-5 void moveAway (LinkableMapObj *lmo);//!< Autolayout: Move all out of the way
   430 
   431 	void emitNoteHasChanged (TreeItem *ti);
   432 	void emitDataHasChanged (TreeItem *ti);
   433 
   434 signals:
   435 	void noteHasChanged (QModelIndex ix);
   436 	void newChildObject(QModelIndex ix);
   437 
   438 private:
   439 	MapEditor *mapEditor;
   440 
   441 	QColor defLinkColor;		// default color for links
   442 	QColor defXLinkColor;		// default color for xlinks
   443 	int defXLinkWidth;			// default width for xlinks
   444 	LinkableMapObj::ColorHint linkcolorhint;// use heading color or own color
   445 	LinkableMapObj::Style linkstyle;		// default style for links
   446 
   447 private:
   448     QPixmap getPixmap();
   449 
   450 public:
   451 	void setMapLinkStyle (const QString &);	// Set style of link
   452 	LinkableMapObj::Style getMapLinkStyle ();	// requested in LMO
   453 	void setMapDefLinkColor(QColor);		// default color of links
   454 	void setMapLinkColorHintInt();			// color of links
   455 	void setMapLinkColorHint(LinkableMapObj::ColorHint);// color of links
   456 	void toggleMapLinkColorHint();			// after changing linkStyles
   457     void selectMapBackgroundImage();
   458     void setMapBackgroundImage(const QString &);
   459     void selectMapBackgroundColor();
   460     void setMapBackgroundColor(QColor);
   461     QColor getMapBackgroundColor();
   462 
   463 
   464 	LinkableMapObj::ColorHint getMapLinkColorHint();
   465 	QColor getMapDefLinkColor();
   466 	void setMapDefXLinkColor(QColor);
   467 	QColor getMapDefXLinkColor();
   468 	void setMapDefXLinkWidth (int);
   469 	int getMapDefXLinkWidth();
   470 
   471 	/*!  Move absolutly to (x,y).  */	
   472     void move    (const double &x, const double &y);
   473 
   474 	/*!  Move relativly to (x,y).  */	
   475     void moveRel (const double &x, const double &y);
   476 
   477 ////////////////////////////////////////////
   478 // Animation  **experimental**
   479 ////////////////////////////////////////////
   480 private:	
   481 	QTimer *animationTimer;
   482 	bool animationUse;
   483 	uint animationTicks;
   484 	uint animationInterval;
   485 	int timerId;				// animation timer
   486 	QList <MapObj*> animObjList;// list with animated objects //FIXME-2 should go to MapEditor
   487 
   488 private slots:
   489 	void animate();						//!< Called by timer to animate stuff
   490 public:
   491 	void startAnimation(BranchObj *bo, const QPointF &start, const QPointF &dest);
   492 	void stopAnimation(MapObj *mo);
   493 
   494 ////////////////////////////////////////////
   495 // Network related 
   496 ////////////////////////////////////////////
   497 public:
   498     /*! \brief Networking states
   499 		
   500 		In Network modus we want to switch of saveState, autosave, ...
   501 	*/
   502 	enum NetState {
   503 		Offline,			//!< Offline
   504 		Client,				//!< I am the client and connected to server
   505 		Server				//!< I am the server
   506 	};
   507 
   508 private:
   509 	// Network connections **Experimental**
   510 	NetState netstate;			// offline, client, server
   511 	QTcpServer *tcpServer;		// Act as server in conference mode (experimental)
   512 	QList <QTcpSocket*> clientList;		// List of connected clients
   513 	quint16 sendCounter;		// Increased with every sent command
   514 
   515 	QTcpSocket	*clientSocket;	// socket of this client
   516 	QString server;				// server address of this client
   517 	int port;					// server port of this client
   518 
   519 
   520 
   521 protected:
   522 	void sendSelection();
   523 
   524 public:
   525 	void newServer();
   526 	void connectToServer();
   527 
   528 private slots:	
   529 	void newClient();
   530 	void sendData(const QString &s);
   531 	void readData();
   532 	void displayNetworkError (QAbstractSocket::SocketError);
   533 
   534 private:	
   535 	void displayClientError(QAbstractSocket::SocketError socketError);
   536 
   537 
   538 ////////////////////////////////////////////
   539 // Selection related 
   540 ////////////////////////////////////////////
   541 private:
   542 	TreeItem *latestAddedItem;				// latest added object, reset on setChanged()
   543 
   544 public:
   545 	void setSelectionModel(QItemSelectionModel *);		// Set common selectionModel
   546 	QItemSelectionModel* getSelectionModel();
   547 
   548 	void setSelectionBlocked(bool);
   549 	bool isSelectionBlocked();
   550 
   551 	bool select ();							//! select by using common QItemSelectionModel
   552 	bool select (const QString &);			//! Select by string
   553 	bool select (LinkableMapObj *lmo);		//! Select by pointer to LMO
   554 	bool select (TreeItem *ti );			//! Select by pointer to TreeItem
   555 	bool select (const QModelIndex &index);	//! Select by ModelIndex
   556 	void unselect();
   557 	void reselect();
   558 
   559 	void emitShowSelection();				//!< Show selection in all views
   560 signals:
   561 	void showSelection();
   562 
   563 //	void selectInt(LinkableMapObj*);	//FIXME-4
   564 
   565 private:	
   566 	void selectNextBranchInt();		// Increment number of branch
   567 	void selectPrevBranchInt();		//! Select the branch which would be above in vymmap view
   568 	void selectAboveBranchInt();	//! Select the branch which would be above current selection in TreeView
   569 	void selectBelowBranchInt();		// Increment number of branch
   570 public:	
   571     void selectUpperBranch();
   572     void selectLowerBranch();
   573     void selectLeftBranch();
   574     void selectRightBranch();
   575     void selectFirstBranch();
   576     void selectLastBranch();
   577 	void selectLastSelectedBranch();
   578 	void selectParent();
   579 
   580 public:
   581 	TreeItem::Type selectionType();
   582 	LinkableMapObj* getSelectedLMO();
   583 	BranchObj* getSelectedBranchObj();	// FIXME-3 replace by item...
   584 	BranchItem* getSelectedBranchItem();
   585 	TreeItem* getSelectedItem();
   586 	QModelIndex getSelectedIndex();
   587 	ImageItem* getSelectedImageItem();
   588 	QString getSelectString ();
   589 	QString getSelectString (LinkableMapObj *lmo);
   590 	QString getSelectString (TreeItem *item);
   591 	
   592 	
   593 signals:
   594 	void selectionChanged(const QItemSelection &newsel, const QItemSelection &oldsel);
   595 
   596 public slots:
   597 	void updateSelection (const QItemSelection &newSel, const QItemSelection &delSel);
   598 
   599 public:
   600 	void emitSelectionChanged(const QItemSelection &oldsel);
   601 	void emitSelectionChanged();
   602 	void selectMapLinkColor();
   603     void selectMapSelectionColor();
   604 private:	
   605     void setSelectionColorInt(QColor);
   606 	QItemSelectionModel *selModel;
   607 	QString lastSelectString;
   608 	bool selectionBlocked;		//! Used to block changes of selection while editing a heading
   609 
   610 public:	
   611     void setSelectionColor(QColor);
   612     QColor getSelectionColor();
   613 };
   614 
   615 #endif