1 /***************************************************************************
2  *   Copyright (C) 2005 by David Saxton                                    *
3  *   david@bluehaze.org                                                    *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  ***************************************************************************/
10 
11 #ifndef DOCUMENT_H
12 #define DOCUMENT_H
13 
14 #include "view.h"
15 
16 #include <QUrl>
17 #include <QPointer>
18 
19 class DCOPObject;
20 class Document;
21 class DocumentIface;
22 class KTechlab;
23 class View;
24 class ViewContainer;
25 
26 typedef QList<QPointer<View> > ViewList;
27 
28 /**
29 @author David Saxton
30 */
31 class Document : public QObject
32 {
33 Q_OBJECT
34 public:
35 	enum DocumentType
36 	{
37 		dt_none, // Used to denote document type not known / specified / etc, when appropriate
38 		dt_flowcode,
39 		dt_circuit,
40 		dt_mechanics,
41 		dt_text,
42 		dt_pinMapEditor
43 	};
44 	Document( const QString &caption, const char *name = nullptr );
45 	~Document() override;
46 	/**
47 	 * If the user has created a new document from the new file dialog, and
48 	 * wants to add it to the project, then this must wait until this file is
49 	 * given a url. Set this to true to add the file to the active project when
50 	 * it is first saved.
51 	 */
setAddToProjectOnSave(bool add)52 	void setAddToProjectOnSave( bool add ) { m_bAddToProjectOnSave = add; }
53 	/**
54 	 * Caption of document, e.g. "Untitled 2"
55 	 */
caption()56 	QString caption() const { return m_caption; }
57 	/**
58 	 * Set the caption of the document, to be displayed in the tab bar when
59 	 * active
60 	 */
61 	void setCaption( const QString &caption );
62 	/**
63 	 * Return the dcop object for this document
64 	 */
65 	DCOPObject * dcopObject() const;
66 	/**
67 	 * Returns the dcop suffix for this document - a unique ID for the current
68 	 * app session. DCOP name will be "Document#dcopID()"
69 	 */
dcopID()70 	unsigned dcopID() const { return m_dcopID; }
71 	/**
72 	 * Sets the dcop suffix. The DCOP object for this document will be renamed.
73 	 * @see dcopID
74 	 */
75 	void setDCOPID( unsigned id );
76 	/**
77 	 * Returns the active view, which is the last view to be used to edit in
78 	 */
activeView()79 	View *activeView() const { return m_pActiveView; }
viewList()80 	ViewList viewList() const { return m_viewList; }
81 	/**
82 	 * Returns the type of document.
83 	 * @see Document::DocumentType
84 	 */
type()85 	DocumentType type() const { return m_type; }
86 	/**
87 	 * Returns the number of open views.
88 	 */
numberOfViews()89 	uint numberOfViews() const { return m_viewList.size(); }
90 	/**
91 	 * Create a view that will display the document data. In all reimplemented
92 	 * functions, you must call handleNewView after creating the view, so that
93 	 * the appropriate slots, pointers, etc can all be initialised.
94 	 */
95 	virtual View *createView( ViewContainer *viewContainer, uint viewAreaId, const char *name = nullptr ) = 0;
96 	/**
97 	 * Returns the url of the file that the Document refers to
98 	 */
url()99 	const QUrl& url() const { return m_url; }
100 	/**
101 	 * Prompts the user for a url, with the given types for the filter.
102 	 * If user accepts, returns true, and set the url to the new url.
103 	 */
104 	bool getURL( const QString &types );
105 	/**
106 	 * Attempts to open a url, and returns true if succesful.
107 	 * You must reinherit this function.
108 	 */
109 	virtual bool openURL( const QUrl &url ) = 0;
110 	/**
111 	 * Sets the url of the file that this Document refers to
112 	 */
113 	void setURL( const QUrl &url );
114 	/**
115 	 * Sets whether the file is modified or not. Will emit modifiedStateChanged
116 	 * if state changes. You must emit this signal if you reinherit this
117 	 */
118 	virtual void setModified( bool modified );
119 	/**
120 	 * Returns the modification state since last-save.
121 	 */
isModified()122 	virtual bool isModified() const { return b_modified; }
123 	/**
124 	 * Returns true if undo is avilable.
125 	 */
isUndoAvailable()126 	virtual bool isUndoAvailable() const { return false; }
127 	/**
128 	 * Returns true if redo is avilable.
129 	 */
isRedoAvailable()130 	virtual bool isRedoAvailable() const { return false; }
131 	/**
132 	 * Saves the file to a new name.
133 	 */
134 	virtual void fileSaveAs() = 0;
135 	/**
136 	 * Attempts to close the file without saving, prompting the user if the
137 	 * file has been modified. If succesful, calls QObject::deleteLater(), and
138 	 * returns true (otherwise returns false).
139 	 */
140 	virtual bool fileClose();
141 	/**
142 	 * Saves the file.
143 	 */
144 	virtual void fileSave() = 0;
145 	/**
146 	 * Prints the file.
147 	 */
print()148 	virtual void print() {};
149 	/**
150 	 * Cuts whatever is selected.
151 	 */
cut()152 	virtual void cut() {};
153 	/**
154 	 * Copies whatever is selected.
155 	 */
copy()156 	virtual void copy() {};
157 	/**
158 	 * Attempts to paste whatever is in the clipboard.
159 	 */
paste()160 	virtual void paste() {};
161 	/**
162 	 * Undo the last operation. You should reinherit this function.
163 	 */
undo()164 	virtual void undo() {};
165 	/**
166 	 * Redo the undone last operation. You should reinherit this function.
167 	 */
redo()168 	virtual void redo() {};
169 	/**
170 	 * Selects everything in the view.
171 	 */
selectAll()172 	virtual void selectAll() {};
173 
convertToMicrobe()174 	virtual void convertToMicrobe() {};
convertToHex()175 	virtual void convertToHex() {};
convertToPIC()176 	virtual void convertToPIC() {};
convertToAssembly()177 	virtual void convertToAssembly() {};
debugRun()178 	virtual void debugRun() {};
debugInterrupt()179 	virtual void debugInterrupt() {};
debugStop()180 	virtual void debugStop() {};
debugStep()181 	virtual void debugStep() {};
isDeleted()182 	bool isDeleted() const { return m_bDeleted; }
183 
184 protected slots:
185 	/**
186 	 * Called when the user changes the configuration.
187 	 */
slotUpdateConfiguration()188 	virtual void slotUpdateConfiguration() {};
189 
190 #define protected public
191 signals:
192 	/**
193 	 * Emitted when an operation has been performed that
194 	 * has caused the stack of available undo/redo operations to
195 	 * have changed
196 	 */
197 	void undoRedoStateChanged();
198 #undef protected
199 
200 signals:
201 	/**
202 	 * Emitted when the Document goes from modified to unmodified,
203 	 * or vice-versa
204 	 */
205 	void modifiedStateChanged();
206 	/**
207 	 * Emitted when the name of the file that the Document refers to
208 	 * is changed.
209 	 */
210 	void fileNameChanged( const QUrl &url );
211 
212 	void viewFocused( View *view );
213 	void viewUnfocused();
214 
215 private slots:
216 	void slotViewDestroyed( QObject *obj );
217 	void slotViewFocused( View *view );
218 
219 protected:
220 	/**
221 	 * You must call this function after creating a new view
222 	 */
223 	virtual void handleNewView( View *view );
224 
225 	bool		  b_modified;
226 
227 // TODO: refactor this out.
228 	DocumentType	  m_type;
229 // XXXX
230 
231 	ViewList	  m_viewList;
232 	DocumentIface    *m_pDocumentIface;
233 
234 	// Set to true by the document et subclasses destructors, used to avoid
235 	// doing stuff that might lead to crash when being deleted.
236 	bool m_bDeleted;
237 
238 private:
239 
240 	QUrl		  m_url;
241 	QPointer<View> m_pActiveView;
242 	QString		  m_caption;
243 	bool		  m_bAddToProjectOnSave;
244 	unsigned 	  m_dcopID;
245 	unsigned 	  m_nextViewID;
246 };
247 
248 #endif
249