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