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