vymmodel.h
author insilmaril
Mon, 04 Jan 2010 20:36:06 +0000
changeset 819 8f987e376035
parent 813 82ebfc46233b
child 820 735c7ea1d2a9
permissions -rw-r--r--
various fixes
     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&,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 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 	QString getVersion();
   241 	void setAuthor  (const QString &);
   242 	QString getAuthor ();
   243 	void setComment (const QString &);
   244 	QString getComment ();
   245 	QString getDate();
   246 	int branchCount();
   247 
   248 	void setSortFilter (const QString &);
   249 	QString getSortFilter ();
   250 protected:	
   251 	QString sortFilter;
   252 signals:
   253 	void sortFilterChanged (QString );		//!< Notify editors of new filter
   254 
   255 public:
   256 	void setHeading(const QString &);		//!< Set heading of item	
   257 	QString getHeading ();					//!< Get heading of item
   258 	void setNote(const QString &s);			//!< Set note text
   259 	QString getNote();						//!< Get note text
   260 
   261 private:
   262 	BranchItem* findCurrent;				// next object in find process
   263 	BranchItem* findPrevious;				// next object in find process
   264 	bool EOFind;							// true, if search failed
   265 
   266 public:
   267     BranchItem* findText(QString,bool);		// Find object
   268     void findReset();						// Reset Search
   269 	void emitShowFindWidget();				// Tell views to show FindWidget
   270 private:	
   271 	QString findString;
   272 signals:
   273 	void showFindWidget();				
   274 
   275 public:
   276 	void setURL(const QString &url);
   277 	QString getURL();						// returns URL of selection or ""
   278 	QStringList getURLs();					// returns URLs of subtree
   279 
   280 
   281 	void setFrameType(const FrameObj::FrameType &);
   282 	void setFrameType(const QString &);
   283 	void setFramePenColor (const QColor &);
   284 	void setFrameBrushColor (const QColor &);
   285 	void setFramePadding (const int &);
   286 	void setFrameBorderWidth (const int &);
   287 	void setIncludeImagesVer(bool);
   288 	void setIncludeImagesHor(bool);
   289 	void setHideLinkUnselected (bool);
   290 
   291 	/*! Should object be hidden in exports (clouded)? */
   292 	void setHideExport(bool);			
   293 
   294 	/*! Should object be hidden in exports (clouded)? */
   295 	void toggleHideExport();		
   296 
   297 	void addTimestamp();		
   298 
   299     void copy();						//!< Copy to clipboard
   300 private:	
   301     void pasteNoSave(const int &n);		//!< paste clipboard to branch
   302 public:	
   303     void paste();			//!< Paste clipboard to branch and backup
   304     void cut();				//!< Cut to clipboard (and copy)
   305 
   306     bool moveUp(BranchItem *bi);	//!< Move branch up without saving state
   307     void moveUp();					//!< Move branch up with saving state
   308     bool moveDown(BranchItem *bi);	//!< Move branch down without saving state
   309     void moveDown();		//!< Move branch down
   310 	void detach();					//!< Detach branch and use as new mapcenter
   311 	void sortChildren();	//!< Sort children lexically
   312 
   313 	// The create methods are used to quickly parse a XML file
   314 	BranchItem* createMapCenter();				//!< Create MapCenter 
   315 	BranchItem* createBranch(BranchItem *dst);	//!< Create Branch
   316 	ImageItem* createImage(BranchItem *dst);	//!< Create image
   317 	XLinkItem* createXLink(BranchItem *dst,bool createMO=false);	//!< Create XLink starting at dst
   318 
   319 	AttributeItem* addAttribute();
   320 	AttributeItem* addAttribute(AttributeItem* );
   321 
   322 	/*! \brief Add new mapcenter
   323 
   324 	    Disclaimer: Still experimental, not fully supported yet.
   325 	*/	
   326 	BranchItem* addMapCenter();
   327 private:	
   328 	BranchItem* addMapCenter(QPointF absPos);
   329 
   330 	/*! \brief Add new branch
   331 
   332 		Depending on num the new branch is created
   333 
   334 		-3 above selection as child of selections parent
   335 		-2 as child of selection
   336 		-1 below selection as child of selections parent
   337 		0..n	insert at a specific position in selections parent
   338 		(needed for free relinking)
   339 	*/	
   340 
   341 private:	
   342     BranchItem* addNewBranchInt(BranchItem *dst, int pos);	// pos allows to add above/below selection
   343 public:	
   344 	/*! \Add new branch
   345 		
   346 		Depending on num the new branch is created
   347 		-1 above selection
   348 		 0 as child of selection
   349 		 1 below selection
   350 	*/
   351     BranchItem* addNewBranch(int pos=0);		
   352     BranchItem* addNewBranchBefore();		//!< Insert branch between selection and its parent
   353 	/*! \brief Relink a branch to a new destination dst 
   354 	    Relinks branch to dst at branch position pos. There is no saveState
   355 		here, as for example moveUp or moving in MapEditor have
   356 		different needs to call saveState
   357 		Returns true if relinking was successful.
   358 	*/	
   359 	bool relinkBranch (BranchItem* branch, BranchItem* dst, int pos =-1);	
   360 	bool relinkImage  (ImageItem* image, BranchItem* dst);	
   361 
   362     void deleteSelection();				//!< Delete selection
   363 	void deleteKeepChildren();			//!< remove branch, but keep children
   364 	void deleteChildren();				//!< keep branch, but remove children
   365 
   366 	TreeItem* deleteItem(TreeItem*);	//!< Delete item and return parent (if parent!= rootItem)
   367 	void clearItem (TreeItem* ti);		//!< Remove all children of TreeItem ti
   368 	bool scrollBranch(BranchItem *);
   369 	bool unscrollBranch(BranchItem *);
   370 public:	
   371     void toggleScroll();
   372     void unscrollChildren();
   373 	void emitExpandAll();
   374 	void emitExpandOneLevel();
   375 	void emitCollapseOneLevel();
   376 signals:	
   377 	void expandAll();
   378 	void expandOneLevel();
   379 	void collapseOneLevel();
   380 
   381 public:	
   382 	void toggleStandardFlag (const QString &name, FlagRow *master=NULL);
   383     void addFloatImage(const QPixmap &img);
   384 
   385     void colorBranch(QColor);
   386     void colorSubtree(QColor);
   387 	QColor getCurrentHeadingColor();
   388 
   389 
   390 	void editURL();							// edit URL
   391 	void editLocalURL();					// edit URL to local file
   392 	void editHeading2URL();					// copy heading to URL
   393 	void editBugzilla2URL();				// create URL to Bugzilla
   394 	void editFATE2URL();					// create URL to FATE
   395 	void editVymLink();						// edit link to another map
   396 	void setVymLink (const QString &);	// Set vymLink for selection
   397 	void deleteVymLink();					// delete link to another map
   398 	QString getVymLink();					// return path to map
   399 	QStringList getVymLinks();				// return paths in subtree
   400 	void followXLink (int);
   401 	void editXLink (int);
   402 
   403 
   404 
   405 
   406 ////////////////////////////////////////////
   407 // Scripting
   408 ////////////////////////////////////////////
   409 public:	
   410 
   411 	/* \brief Process one command and its parameters */
   412     QVariant parseAtom (const QString &atom, bool &noError, QString &errorMsg);	
   413 
   414 	/* \brief Runs the script */
   415 	QVariant runScript (const QString &script);
   416 
   417 private:
   418 	Parser parser;
   419 
   420 ////////////////////////////////////////////
   421 // Exports
   422 ////////////////////////////////////////////
   423 private:
   424 	TreeItem::HideTmpMode hidemode;	// true while exporting to hide some stuff
   425 
   426 public:
   427 	/*! Set or unset temporary hiding of objects during export  */
   428 	void setExportMode (bool);
   429 
   430 	/*! Save as image */
   431     void exportImage (QString fname="",bool askForName=true,QString format="PNG");
   432 
   433 
   434 	/*! Export as XTML to directory */
   435     void exportXML(QString dir="", bool askForName=true);
   436 
   437 	/*! Export as A&O report text to file */
   438 	void exportAO (QString fname="",bool askForName=true);  
   439 
   440 	/*! Export as ASCII text to file */
   441 	void exportASCII (QString fname="",bool askForName=true);  
   442 
   443 	/*! Export as HTML to directory */
   444     void exportHTML(const QString& dir="", bool askForName=true);	
   445     void exportXHTML(const QString& dir="", bool askForName=true);	
   446 
   447     /*! Export as OpenOfficeOrg presentation */
   448     void exportOOPresentation(const QString &,const QString &);	
   449 
   450 
   451 ////////////////////////////////////////////
   452 // View related
   453 ////////////////////////////////////////////
   454 public:
   455 	void registerEditor (QWidget *);
   456 	void unregisterEditor (QWidget *);
   457 
   458 private: 
   459 	QPointF contextPos;					//!< local position during context menu
   460 public:
   461 	void setContextPos (QPointF);		//!< local position during context menu
   462 	void unsetContextPos ();			//!< forget local position after context menu
   463 
   464 	void updateNoteFlag();				//!< Signal origination in TextEditor
   465 	void reposition();					//!< Call reposition for all MCOs
   466 	void setHideTmpMode (TreeItem::HideTmpMode mode);	
   467 
   468 	void emitNoteHasChanged (TreeItem *ti);
   469 	void emitDataHasChanged (TreeItem *ti);
   470 
   471 signals:
   472 	void noteHasChanged (QModelIndex ix);
   473 	void newChildObject(QModelIndex ix);
   474 
   475 private:
   476 	MapEditor *mapEditor;
   477 
   478 	QColor defLinkColor;		// default color for links
   479 	QColor defXLinkColor;		// default color for xlinks
   480 	int defXLinkWidth;			// default width for xlinks
   481 	LinkableMapObj::ColorHint linkcolorhint;// use heading color or own color
   482 	LinkableMapObj::Style linkstyle;		// default style for links
   483 
   484 public:
   485 	void setMapLinkStyle (const QString &);	// Set style of link
   486 	LinkableMapObj::Style getMapLinkStyle ();	// requested in LMO
   487 	void setMapDefLinkColor(QColor);		// default color of links
   488 	void setMapLinkColorHintInt();			// color of links
   489 	void setMapLinkColorHint(LinkableMapObj::ColorHint);// color of links
   490 	void toggleMapLinkColorHint();			// after changing linkStyles
   491     void selectMapBackgroundImage();
   492     void setMapBackgroundImage(const QString &);
   493     void selectMapBackgroundColor();
   494     void setMapBackgroundColor(QColor);
   495     QColor getMapBackgroundColor();
   496 
   497 
   498 	LinkableMapObj::ColorHint getMapLinkColorHint();
   499 	QColor getMapDefLinkColor();
   500 	void setMapDefXLinkColor(QColor);
   501 	QColor getMapDefXLinkColor();
   502 	void setMapDefXLinkWidth (int);
   503 	int getMapDefXLinkWidth();
   504 
   505 	/*!  Move absolutly to (x,y).  */	
   506     void move    (const double &x, const double &y);
   507 
   508 	/*!  Move relativly to (x,y).  */	
   509     void moveRel (const double &x, const double &y);
   510 
   511 ////////////////////////////////////////////
   512 // Animation  **experimental**
   513 ////////////////////////////////////////////
   514 private:	
   515 	QTimer *animationTimer;
   516 	bool animationUse;
   517 	uint animationTicks;
   518 	uint animationInterval;
   519 	int timerId;				// animation timer
   520 	QList <MapObj*> animObjList;// list with animated objects //FIXME-2 should go to MapEditor
   521 
   522 private slots:
   523 	void animate();						//!< Called by timer to animate stuff
   524 public:
   525 	void startAnimation(BranchObj *bo, const QPointF &start, const QPointF &dest);
   526 	void stopAnimation(MapObj *mo);
   527 
   528 ////////////////////////////////////////////
   529 // Network related 
   530 ////////////////////////////////////////////
   531 public:
   532     /*! \brief Networking states
   533 		
   534 		In Network modus we want to switch of saveState, autosave, ...
   535 	*/
   536 	enum NetState {
   537 		Offline,			//!< Offline
   538 		Client,				//!< I am the client and connected to server
   539 		Server				//!< I am the server
   540 	};
   541 
   542 private:
   543 	// Network connections **Experimental**
   544 	NetState netstate;			// offline, client, server
   545 	QTcpServer *tcpServer;		// Act as server in conference mode (experimental)
   546 	QList <QTcpSocket*> clientList;		// List of connected clients
   547 	quint16 sendCounter;		// Increased with every sent command
   548 
   549 	QTcpSocket	*clientSocket;	// socket of this client
   550 	QString server;				// server address of this client
   551 	int port;					// server port of this client
   552 
   553 
   554 
   555 protected:
   556 	void sendSelection();
   557 
   558 public:
   559 	void newServer();
   560 	void connectToServer();
   561 
   562 private slots:	
   563 	void newClient();
   564 	void sendData(const QString &s);
   565 	void readData();
   566 	void displayNetworkError (QAbstractSocket::SocketError);
   567 
   568 ////////////////////////////////////////////
   569 // DBUS related 
   570 ////////////////////////////////////////////
   571 
   572 private:
   573 AdaptorModel *adaptorModel; //FIXME-3
   574 
   575 public slots:
   576 	void testslot();
   577 
   578 ////////////////////////////////////////////
   579 // Selection related 
   580 ////////////////////////////////////////////
   581 private:
   582 	TreeItem *latestAddedItem;				// latest added object, reset on setChanged()
   583 
   584 public:
   585 	void setSelectionModel(QItemSelectionModel *);		// Set common selectionModel
   586 	QItemSelectionModel* getSelectionModel();
   587 
   588 	void setSelectionBlocked(bool);
   589 	bool isSelectionBlocked();
   590 
   591 	bool select ();							//! select by using common QItemSelectionModel
   592 	bool select (const QString &);			//! Select by string
   593 	bool select (LinkableMapObj *lmo);		//! Select by pointer to LMO
   594 	bool select (TreeItem *ti );			//! Select by pointer to TreeItem
   595 	bool select (const QModelIndex &index);	//! Select by ModelIndex
   596 	void unselect();
   597 	bool reselect();
   598 
   599 	void emitShowSelection();				//!< Show selection in all views
   600 signals:
   601 	void showSelection();
   602 
   603 public:	
   604     bool selectFirstBranch();
   605     bool selectLastBranch();
   606 	bool selectLastSelectedBranch();
   607 	bool selectParent();
   608 
   609 public:
   610 	TreeItem::Type selectionType();
   611 	LinkableMapObj* getSelectedLMO();
   612 	BranchObj* getSelectedBranchObj();	// FIXME-3 replace by item...
   613 	BranchItem* getSelectedBranch();
   614 	ImageItem* getSelectedImage();
   615 	AttributeItem* getSelectedAttribute();
   616 	TreeItem* getSelectedItem();
   617 	QModelIndex getSelectedIndex();
   618 	QString getSelectString ();
   619 	QString getSelectString (LinkableMapObj *lmo);
   620 	QString getSelectString (TreeItem *item);
   621 	
   622 	
   623 signals:
   624 	void selectionChanged(const QItemSelection &newsel, const QItemSelection &oldsel);
   625 
   626 public:
   627 	void emitSelectionChanged(const QItemSelection &oldsel);
   628 	void emitSelectionChanged();
   629 	void selectMapLinkColor();
   630     void selectMapSelectionColor();
   631 private:	
   632     void setSelectionColorInt(QColor);
   633 	QItemSelectionModel *selModel;
   634 	QString lastSelectString;
   635 	bool selectionBlocked;		//! Used to block changes of selection while editing a heading
   636 
   637 public:	
   638     void setSelectionColor(QColor);
   639     QColor getSelectionColor();
   640 };
   641 
   642 #endif