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