vymmodel.h
author insilmaril
Thu, 03 Sep 2009 08:52:00 +0000
changeset 790 133e2ed6b9c5
parent 788 78ba80b54bc4
child 791 f1006de05c54
permissions -rw-r--r--
More work on xLinks
     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(BranchObj *,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 saveStateChangingPart(
   194 		TreeItem *undoSelection, 
   195 		TreeItem* redoSelection, 
   196 		const QString &redoCommand, 
   197 		const QString &comment);
   198 	/*! Overloaded for convenience */
   199     void saveStateRemovingPart(
   200 		TreeItem *redoSelection, 
   201 		const QString &comment);
   202 	/*! Overloaded for convenience */
   203     void saveState(
   204 		TreeItem *undoSelection, 
   205 		const QString &undoCommand, 
   206 		TreeItem *redoSelection, 
   207 		const QString &redoCommand, 
   208 		const QString &comment); 
   209 	/*! Overloaded for convenience */
   210     void saveState(
   211 		const QString &undoSelection, 
   212 		const QString &undoCommand, 
   213 		const QString &redoSelection, 
   214 		const QString &redoCommand, 
   215 		const QString &comment) ;
   216     void saveState(
   217 		const QString &undoCommand, 
   218 		const QString &redoCommand, 
   219 		const QString &comment) ;
   220 
   221 
   222 ////////////////////////////////////////////
   223 // unsorted so far
   224 ////////////////////////////////////////////
   225 private:
   226 	QGraphicsScene *mapScene;
   227 public:
   228 	void setScene(QGraphicsScene *s);
   229 	QGraphicsScene *getScene();
   230 
   231     TreeItem* findBySelectString (QString s);		
   232     TreeItem* findID (const QString &s);				// find MapObj by previously set ID
   233 
   234 
   235 ////////////////////////////////////////////
   236 // Interface 
   237 ////////////////////////////////////////////
   238 public:
   239 	void setVersion(const  QString &);
   240 	void setAuthor  (const QString &);
   241 	QString getAuthor ();
   242 	void setComment (const QString &);
   243 	QString getComment ();
   244 	QString getDate();
   245 	int branchCount();
   246 
   247 public:	
   248 	void setHeading(const QString &);		//!< Set heading of item	
   249 	QString getHeading ();					//!< Get heading of item
   250 
   251 private:
   252 	BranchItem* findCurrent;				// next object in find process
   253 	BranchItem* findPrevious;				// next object in find process
   254 	bool EOFind;							// true, if search failed
   255 public:
   256     BranchItem* findText(QString,bool);		// Find object
   257     void findReset();						// Reset Search
   258 
   259 	void setURL(const QString &url);
   260 	QString getURL();						// returns URL of selection or ""
   261 	QStringList getURLs();					// returns URLs of subtree
   262 
   263 
   264 	void setFrameType(const FrameObj::FrameType &);
   265 	void setFrameType(const QString &);
   266 	void setFramePenColor (const QColor &);
   267 	void setFrameBrushColor (const QColor &);
   268 	void setFramePadding (const int &);
   269 	void setFrameBorderWidth (const int &);
   270 	void setIncludeImagesVer(bool);
   271 	void setIncludeImagesHor(bool);
   272 	void setHideLinkUnselected (bool);
   273 
   274 	/*! Should object be hidden in exports (clouded)? */
   275 	void setHideExport(bool);			
   276 
   277 	/*! Should object be hidden in exports (clouded)? */
   278 	void toggleHideExport();		
   279 
   280     void copy();						//!< Copy to clipboard
   281 private:	
   282     void pasteNoSave(const int &n);		//!< paste clipboard to branch
   283 public:	
   284     void paste();			//!< Paste clipboard to branch and backup
   285     void cut();				//!< Cut to clipboard (and copy)
   286 
   287     void moveUp();			//!< Move branch up
   288     void moveDown();		//!< Move branch down
   289 	void sortChildren();	//!< Sort children lexically
   290 
   291 	// The create methods are used to quickly parse a XML file
   292 	BranchItem* createMapCenter();				//!< Create MapCenter 
   293 	BranchItem* createBranch(BranchItem *dst);	//!< Create Branch
   294 	ImageItem* createImage(BranchItem *dst);	//!< Create image
   295 	XLinkItem* createXLink(BranchItem *dst,bool createMO=false);	//!< Create XLink starting at dst
   296 
   297 	AttributeItem* addAttribute();
   298 
   299 	/*! \brief Add new mapcenter
   300 
   301 	    Disclaimer: Still experimental, not fully supported yet.
   302 	*/	
   303 	BranchItem* addMapCenter();
   304 private:	
   305 	BranchItem* addMapCenter(QPointF absPos);
   306 
   307 	/*! \brief Add new branch
   308 
   309 		Depending on num the new branch is created
   310 
   311 		-3 above selection as child of selections parent
   312 		-2 as child of selection
   313 		-1 below selection as child of selections parent
   314 		0..n	insert at a specific position in selections parent
   315 		(needed for free relinking)
   316 	*/	
   317 
   318 private:	
   319     BranchItem* addNewBranchInt(BranchItem *dst, int pos);	// pos allows to add above/below selection
   320 public:	
   321 	/*! \Add new branch
   322 		
   323 		Depending on num the new branch is created
   324 		-1 above selection
   325 		 0 as child of selection
   326 		 1 below selection
   327 	*/
   328     BranchItem* addNewBranch(int pos=0);		
   329     BranchItem* addNewBranchBefore();		//!< Insert branch between selection and its parent
   330 	/*! \brief Relink a branch to a new destination dst 
   331 	    Relinks branch to dst at branch position pos. There is no saveState
   332 		here, as for example moveUp or moving in MapEditor have
   333 		different needs to call saveState
   334 		Returns true if relinking was successful.
   335 	*/	
   336 	bool relinkBranch (BranchItem* branch, BranchItem* dst, int pos =-1);	
   337 	bool relinkImage  (ImageItem* image, BranchItem* dst);	
   338 
   339     void deleteSelection();				//!< Delete selection
   340 	void deleteKeepChildren();			//!< remove branch, but keep children
   341 	void deleteChildren();				//!< keep branch, but remove children
   342 
   343 	TreeItem* deleteItem(TreeItem*);	//!< Delete item and return parent (if parent!= rootItem)
   344 	bool scrollBranch(BranchItem *);
   345 	bool unscrollBranch(BranchItem *);
   346 public:	
   347     void toggleScroll();
   348     void unscrollChildren();
   349 	void emitExpandAll();
   350 signals:	
   351 	void expandAll();
   352 
   353 public:	
   354 	void toggleStandardFlag (const QString &name, FlagRow *master=NULL);
   355     void addFloatImage(const QPixmap &img);
   356 
   357     void colorBranch(QColor);
   358     void colorSubtree(QColor);
   359 	QColor getCurrentHeadingColor();
   360 
   361 
   362 	void editURL();							// edit URL
   363 	void editLocalURL();					// edit URL to local file
   364 	void editHeading2URL();					// copy heading to URL
   365 	void editBugzilla2URL();				// create URL to Bugzilla
   366 	void editFATE2URL();					// create URL to FATE
   367 	void editVymLink();						// edit link to another map
   368 	void setVymLink (const QString &);	// Set vymLink for selection
   369 	void deleteVymLink();					// delete link to another map
   370 	QString getVymLink();					// return path to map
   371 	QStringList getVymLinks();				// return paths in subtree
   372 	void followXLink (int);
   373 	void editXLink (int);
   374 
   375 
   376 
   377 
   378 ////////////////////////////////////////////
   379 // Scripting
   380 ////////////////////////////////////////////
   381 public:	
   382 
   383 	/* \brief Process one command and its parameters */
   384     void parseAtom (const QString &atom);	
   385 
   386 	/* \brief Runs the script */
   387 	void runScript (QString script);
   388 
   389 private:
   390 	Parser parser;
   391 
   392 ////////////////////////////////////////////
   393 // Exports
   394 ////////////////////////////////////////////
   395 private:
   396 	TreeItem::HideTmpMode hidemode;	// true while exporting to hide some stuff
   397 
   398 public:
   399 	/*! Set or unset temporary hiding of objects during export  */
   400 	void setExportMode (bool);
   401 
   402 	/*! Save as image */
   403     void exportImage (QString fname="",bool askForName=true,QString format="PNG");
   404 
   405 
   406 	/*! Export as XTML to directory */
   407     void exportXML(QString dir="", bool askForName=true);
   408 
   409 	/*! Export as ASCII text to file */
   410 	void exportASCII (QString fname="",bool askForName=true);  
   411 
   412 	/*! Export as XHTML to directory */
   413     void exportXHTML(const QString& dir="", bool askForName=true);	
   414 
   415     /*! Export as OpenOfficeOrg presentation */
   416     void exportOOPresentation(const QString &,const QString &);	
   417 
   418 
   419 ////////////////////////////////////////////
   420 // View related
   421 ////////////////////////////////////////////
   422 public:
   423 	void registerEditor (QWidget *);
   424 	void unregisterEditor (QWidget *);
   425 
   426 private: 
   427 	QPointF contextPos;					//!< local position during context menu
   428 public:
   429 	void setContextPos (QPointF);		//!< local position during context menu
   430 	void unsetContextPos ();			//!< forget local position after context menu
   431 
   432 	void updateNoteFlag();				//!< Signal origination in TextEditor
   433     void updateRelPositions();
   434 
   435 	void reposition();					//!< Call reposition for all MCOs
   436 	void setHideTmpMode (TreeItem::HideTmpMode mode);	
   437 
   438 	//FIXME-5 QPolygonF shape(BranchObj *bo);		//!< Returns arbitrary shape of subtree
   439 	//FIXME-5 void moveAway (LinkableMapObj *lmo);//!< Autolayout: Move all out of the way
   440 
   441 	void emitNoteHasChanged (TreeItem *ti);
   442 	void emitDataHasChanged (TreeItem *ti);
   443 
   444 signals:
   445 	void noteHasChanged (QModelIndex ix);
   446 	void newChildObject(QModelIndex ix);
   447 
   448 private:
   449 	MapEditor *mapEditor;
   450 
   451 	QColor defLinkColor;		// default color for links
   452 	QColor defXLinkColor;		// default color for xlinks
   453 	int defXLinkWidth;			// default width for xlinks
   454 	LinkableMapObj::ColorHint linkcolorhint;// use heading color or own color
   455 	LinkableMapObj::Style linkstyle;		// default style for links
   456 
   457 public:
   458 	void setMapLinkStyle (const QString &);	// Set style of link
   459 	LinkableMapObj::Style getMapLinkStyle ();	// requested in LMO
   460 	void setMapDefLinkColor(QColor);		// default color of links
   461 	void setMapLinkColorHintInt();			// color of links
   462 	void setMapLinkColorHint(LinkableMapObj::ColorHint);// color of links
   463 	void toggleMapLinkColorHint();			// after changing linkStyles
   464     void selectMapBackgroundImage();
   465     void setMapBackgroundImage(const QString &);
   466     void selectMapBackgroundColor();
   467     void setMapBackgroundColor(QColor);
   468     QColor getMapBackgroundColor();
   469 
   470 
   471 	LinkableMapObj::ColorHint getMapLinkColorHint();
   472 	QColor getMapDefLinkColor();
   473 	void setMapDefXLinkColor(QColor);
   474 	QColor getMapDefXLinkColor();
   475 	void setMapDefXLinkWidth (int);
   476 	int getMapDefXLinkWidth();
   477 
   478 	/*!  Move absolutly to (x,y).  */	
   479     void move    (const double &x, const double &y);
   480 
   481 	/*!  Move relativly to (x,y).  */	
   482     void moveRel (const double &x, const double &y);
   483 
   484 ////////////////////////////////////////////
   485 // Animation  **experimental**
   486 ////////////////////////////////////////////
   487 private:	
   488 	QTimer *animationTimer;
   489 	bool animationUse;
   490 	uint animationTicks;
   491 	uint animationInterval;
   492 	int timerId;				// animation timer
   493 	QList <MapObj*> animObjList;// list with animated objects //FIXME-2 should go to MapEditor
   494 
   495 private slots:
   496 	void animate();						//!< Called by timer to animate stuff
   497 public:
   498 	void startAnimation(BranchObj *bo, const QPointF &start, const QPointF &dest);
   499 	void stopAnimation(MapObj *mo);
   500 
   501 ////////////////////////////////////////////
   502 // Network related 
   503 ////////////////////////////////////////////
   504 public:
   505     /*! \brief Networking states
   506 		
   507 		In Network modus we want to switch of saveState, autosave, ...
   508 	*/
   509 	enum NetState {
   510 		Offline,			//!< Offline
   511 		Client,				//!< I am the client and connected to server
   512 		Server				//!< I am the server
   513 	};
   514 
   515 private:
   516 	// Network connections **Experimental**
   517 	NetState netstate;			// offline, client, server
   518 	QTcpServer *tcpServer;		// Act as server in conference mode (experimental)
   519 	QList <QTcpSocket*> clientList;		// List of connected clients
   520 	quint16 sendCounter;		// Increased with every sent command
   521 
   522 	QTcpSocket	*clientSocket;	// socket of this client
   523 	QString server;				// server address of this client
   524 	int port;					// server port of this client
   525 
   526 
   527 
   528 protected:
   529 	void sendSelection();
   530 
   531 public:
   532 	void newServer();
   533 	void connectToServer();
   534 
   535 private slots:	
   536 	void newClient();
   537 	void sendData(const QString &s);
   538 	void readData();
   539 	void displayNetworkError (QAbstractSocket::SocketError);
   540 
   541 ////////////////////////////////////////////
   542 // DBUS related 
   543 ////////////////////////////////////////////
   544 
   545 private:
   546 AdaptorModel *adaptorModel; //FIXME-3
   547 
   548 public slots:
   549 	void testslot();
   550 
   551 ////////////////////////////////////////////
   552 // Selection related 
   553 ////////////////////////////////////////////
   554 private:
   555 	TreeItem *latestAddedItem;				// latest added object, reset on setChanged()
   556 
   557 public:
   558 	void setSelectionModel(QItemSelectionModel *);		// Set common selectionModel
   559 	QItemSelectionModel* getSelectionModel();
   560 
   561 	void setSelectionBlocked(bool);
   562 	bool isSelectionBlocked();
   563 
   564 	bool select ();							//! select by using common QItemSelectionModel
   565 	bool select (const QString &);			//! Select by string
   566 	bool select (LinkableMapObj *lmo);		//! Select by pointer to LMO
   567 	bool select (TreeItem *ti );			//! Select by pointer to TreeItem
   568 	bool select (const QModelIndex &index);	//! Select by ModelIndex
   569 	void unselect();
   570 	bool reselect();
   571 
   572 	void emitShowSelection();				//!< Show selection in all views
   573 signals:
   574 	void showSelection();
   575 
   576 //	bool selectInt(LinkableMapObj*);	//FIXME-4
   577 
   578 public:	
   579     bool selectFirstBranch();
   580     bool selectLastBranch();
   581 	bool selectLastSelectedBranch();
   582 	bool selectParent();
   583 
   584 public:
   585 	TreeItem::Type selectionType();
   586 	LinkableMapObj* getSelectedLMO();
   587 	BranchObj* getSelectedBranchObj();	// FIXME-3 replace by item...
   588 	BranchItem* getSelectedBranch();
   589 	ImageItem* getSelectedImage();
   590 	AttributeItem* getSelectedAttribute();
   591 	TreeItem* getSelectedItem();
   592 	QModelIndex getSelectedIndex();
   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