File indexing completed on 2024-12-01 05:13:01
0001 /*************************************************************************** 0002 * Copyright (C) 2005 by David Saxton * 0003 * david@bluehaze.org * 0004 * * 0005 * This program is free software; you can redistribute it and/or modify * 0006 * it under the terms of the GNU General Public License as published by * 0007 * the Free Software Foundation; either version 2 of the License, or * 0008 * (at your option) any later version. * 0009 ***************************************************************************/ 0010 0011 #ifndef DOCUMENT_H 0012 #define DOCUMENT_H 0013 0014 #include "view.h" 0015 0016 #include <QPointer> 0017 #include <QUrl> 0018 0019 class DCOPObject; 0020 class Document; 0021 class DocumentIface; 0022 class KTechlab; 0023 class View; 0024 class ViewContainer; 0025 0026 typedef QList<QPointer<View>> ViewList; 0027 0028 /** 0029 @author David Saxton 0030 */ 0031 class Document : public QObject 0032 { 0033 Q_OBJECT 0034 public: 0035 enum DocumentType { 0036 dt_none, // Used to denote document type not known / specified / etc, when appropriate 0037 dt_flowcode, 0038 dt_circuit, 0039 dt_mechanics, 0040 dt_text, 0041 dt_pinMapEditor 0042 }; 0043 Document(const QString &caption); 0044 ~Document() override; 0045 /** 0046 * If the user has created a new document from the new file dialog, and 0047 * wants to add it to the project, then this must wait until this file is 0048 * given a url. Set this to true to add the file to the active project when 0049 * it is first saved. 0050 */ 0051 void setAddToProjectOnSave(bool add) 0052 { 0053 m_bAddToProjectOnSave = add; 0054 } 0055 /** 0056 * Caption of document, e.g. "Untitled 2" 0057 */ 0058 QString caption() const 0059 { 0060 return m_caption; 0061 } 0062 /** 0063 * Set the caption of the document, to be displayed in the tab bar when 0064 * active 0065 */ 0066 void setCaption(const QString &caption); 0067 /** 0068 * Return the dcop object for this document 0069 */ 0070 DCOPObject *dcopObject() const; 0071 /** 0072 * Returns the dcop suffix for this document - a unique ID for the current 0073 * app session. DCOP name will be "Document#dcopID()" 0074 */ 0075 unsigned dcopID() const 0076 { 0077 return m_dcopID; 0078 } 0079 /** 0080 * Sets the dcop suffix. The DCOP object for this document will be renamed. 0081 * @see dcopID 0082 */ 0083 void setDCOPID(unsigned id); 0084 /** 0085 * Returns the active view, which is the last view to be used to edit in 0086 */ 0087 View *activeView() const 0088 { 0089 return m_pActiveView; 0090 } 0091 ViewList viewList() const 0092 { 0093 return m_viewList; 0094 } 0095 /** 0096 * Returns the type of document. 0097 * @see Document::DocumentType 0098 */ 0099 DocumentType type() const 0100 { 0101 return m_type; 0102 } 0103 /** 0104 * Returns the number of open views. 0105 */ 0106 uint numberOfViews() const 0107 { 0108 return m_viewList.size(); 0109 } 0110 /** 0111 * Create a view that will display the document data. In all reimplemented 0112 * functions, you must call handleNewView after creating the view, so that 0113 * the appropriate slots, pointers, etc can all be initialised. 0114 */ 0115 virtual View *createView(ViewContainer *viewContainer, uint viewAreaId) = 0; 0116 /** 0117 * Returns the url of the file that the Document refers to 0118 */ 0119 const QUrl &url() const 0120 { 0121 return m_url; 0122 } 0123 /** 0124 * Prompts the user for a url, with the given types for the filter. 0125 * If user accepts, returns true, and set the url to the new url. 0126 */ 0127 bool getURL(const QString &types, const QString &fileExtToEnforce); 0128 /** 0129 * Attempts to open a url, and returns true if succesful. 0130 * You must reinherit this function. 0131 */ 0132 virtual bool openURL(const QUrl &url) = 0; 0133 /** 0134 * Sets the url of the file that this Document refers to 0135 */ 0136 void setURL(const QUrl &url); 0137 /** 0138 * Sets whether the file is modified or not. Will emit modifiedStateChanged 0139 * if state changes. You must emit this signal if you reinherit this 0140 */ 0141 virtual void setModified(bool modified); 0142 /** 0143 * Returns the modification state since last-save. 0144 */ 0145 virtual bool isModified() const 0146 { 0147 return b_modified; 0148 } 0149 /** 0150 * Returns true if undo is avilable. 0151 */ 0152 virtual bool isUndoAvailable() const 0153 { 0154 return false; 0155 } 0156 /** 0157 * Returns true if redo is avilable. 0158 */ 0159 virtual bool isRedoAvailable() const 0160 { 0161 return false; 0162 } 0163 /** 0164 * Saves the file to a new name. 0165 */ 0166 virtual void fileSaveAs() = 0; 0167 /** 0168 * Attempts to close the file without saving, prompting the user if the 0169 * file has been modified. If succesful, calls QObject::deleteLater(), and 0170 * returns true (otherwise returns false). 0171 */ 0172 virtual bool fileClose(); 0173 /** 0174 * Saves the file. 0175 */ 0176 virtual void fileSave() = 0; 0177 /** 0178 * Prints the file. 0179 */ 0180 virtual void print() {}; 0181 /** 0182 * Cuts whatever is selected. 0183 */ 0184 virtual void cut() {}; 0185 /** 0186 * Copies whatever is selected. 0187 */ 0188 virtual void copy() {}; 0189 /** 0190 * Attempts to paste whatever is in the clipboard. 0191 */ 0192 virtual void paste() {}; 0193 /** 0194 * Undo the last operation. You should reinherit this function. 0195 */ 0196 virtual void undo() {}; 0197 /** 0198 * Redo the undone last operation. You should reinherit this function. 0199 */ 0200 virtual void redo() {}; 0201 /** 0202 * Selects everything in the view. 0203 */ 0204 virtual void selectAll() {}; 0205 0206 virtual void convertToMicrobe() {}; 0207 virtual void convertToHex() {}; 0208 virtual void convertToPIC() {}; 0209 virtual void convertToAssembly() {}; 0210 virtual void debugRun() {}; 0211 virtual void debugInterrupt() {}; 0212 virtual void debugStop() {}; 0213 virtual void debugStep() {}; 0214 bool isDeleted() const 0215 { 0216 return m_bDeleted; 0217 } 0218 0219 protected slots: 0220 /** 0221 * Called when the user changes the configuration. 0222 */ 0223 virtual void slotUpdateConfiguration() {}; 0224 0225 #define protected public 0226 signals: 0227 /** 0228 * Emitted when an operation has been performed that 0229 * has caused the stack of available undo/redo operations to 0230 * have changed 0231 */ 0232 void undoRedoStateChanged(); 0233 #undef protected 0234 0235 signals: 0236 /** 0237 * Emitted when the Document goes from modified to unmodified, 0238 * or vice-versa 0239 */ 0240 void modifiedStateChanged(); 0241 /** 0242 * Emitted when the name of the file that the Document refers to 0243 * is changed. 0244 */ 0245 void fileNameChanged(const QUrl &url); 0246 0247 void viewFocused(View *view); 0248 void viewUnfocused(); 0249 0250 private slots: 0251 void slotViewDestroyed(QObject *obj); 0252 void slotViewFocused(View *view); 0253 0254 protected: 0255 /** 0256 * You must call this function after creating a new view 0257 */ 0258 virtual void handleNewView(View *view); 0259 0260 bool b_modified; 0261 0262 // TODO: refactor this out. 0263 DocumentType m_type; 0264 // XXXX 0265 0266 ViewList m_viewList; 0267 DocumentIface *m_pDocumentIface; 0268 0269 // Set to true by the document et subclasses destructors, used to avoid 0270 // doing stuff that might lead to crash when being deleted. 0271 bool m_bDeleted; 0272 0273 private: 0274 QUrl m_url; 0275 QPointer<View> m_pActiveView; 0276 QString m_caption; 0277 bool m_bAddToProjectOnSave; 0278 unsigned m_dcopID; 0279 unsigned m_nextViewID; 0280 }; 0281 0282 #endif