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