|
1 /* |
|
2 * LDForge: LDraw parts authoring CAD |
|
3 * Copyright (C) 2013, 2014 Santeri Piippo |
|
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 3 of the License, or |
|
8 * (at your option) any later version. |
|
9 * |
|
10 * This program is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 * GNU General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License |
|
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 */ |
|
18 |
|
19 #ifndef LDFORGE_DOCUMENT_H |
|
20 #define LDFORGE_DOCUMENT_H |
|
21 |
|
22 #include <QObject> |
|
23 #include "Main.h" |
|
24 #include "LDObject.h" |
|
25 #include "EditHistory.h" |
|
26 |
|
27 class History; |
|
28 class OpenProgressDialog; |
|
29 class LDDocumentPointer; |
|
30 struct LDGLData; |
|
31 |
|
32 namespace LDPaths |
|
33 { |
|
34 void initPaths(); |
|
35 bool tryConfigure (QString path); |
|
36 |
|
37 QString ldconfig(); |
|
38 QString prims(); |
|
39 QString parts(); |
|
40 QString getError(); |
|
41 } |
|
42 |
|
43 // ============================================================================= |
|
44 // LDDocument |
|
45 // |
|
46 // The LDDocument class stores a document opened in LDForge either as a editable |
|
47 // file for the user or for subfile caching. Its methods handle file input and |
|
48 // output. |
|
49 // |
|
50 // A file is implicit when they are opened automatically for caching purposes |
|
51 // and are hidden from the user. User-opened files are explicit (not implicit). |
|
52 // |
|
53 // The default name is a placeholder, initially suggested name for a file. The |
|
54 // primitive generator uses this to give initial names to primitives. |
|
55 // ============================================================================= |
|
56 class LDDocument : public QObject |
|
57 { |
|
58 properties: |
|
59 Q_OBJECT |
|
60 PROPERTY (private, LDObjectList, Objects, LIST_OPS, STOCK_WRITE) |
|
61 PROPERTY (private, History*, History, NO_OPS, STOCK_WRITE) |
|
62 PROPERTY (private, LDObjectList, Vertices, LIST_OPS, STOCK_WRITE) |
|
63 PROPERTY (private, QList<LDDocumentPointer*>, References, LIST_OPS, STOCK_WRITE) |
|
64 PROPERTY (public, QString, Name, STR_OPS, STOCK_WRITE) |
|
65 PROPERTY (public, QString, FullPath, STR_OPS, STOCK_WRITE) |
|
66 PROPERTY (public, QString, DefaultName, STR_OPS, STOCK_WRITE) |
|
67 PROPERTY (public, bool, Implicit, BOOL_OPS, STOCK_WRITE) |
|
68 PROPERTY (public, LDObjectList, Cache, LIST_OPS, STOCK_WRITE) |
|
69 PROPERTY (public, long, SavePosition, NUM_OPS, STOCK_WRITE) |
|
70 PROPERTY (public, QListWidgetItem*, ListItem, NO_OPS, STOCK_WRITE) |
|
71 |
|
72 public: |
|
73 LDDocument(); |
|
74 ~LDDocument(); |
|
75 |
|
76 int addObject (LDObject* obj); // Adds an object to this file at the end of the file. |
|
77 void addObjects (const LDObjectList objs); |
|
78 void clearSelection(); |
|
79 void forgetObject (LDObject* obj); // Deletes the given object from the object chain. |
|
80 QString getDisplayName(); |
|
81 const LDObjectList& getSelection() const; |
|
82 bool hasUnsavedChanges() const; // Does this Document.have unsaved changes? |
|
83 LDObjectList inlineContents (LDSubfile::InlineFlags flags); |
|
84 void insertObj (int pos, LDObject* obj); |
|
85 int getObjectCount() const; |
|
86 LDObject* getObject (int pos) const; |
|
87 bool save (QString path = ""); // Saves this file to disk. |
|
88 void swapObjects (LDObject* one, LDObject* other); |
|
89 bool isSafeToClose(); // Perform safety checks. Do this before closing any files! |
|
90 void setObject (int idx, LDObject* obj); |
|
91 void addReference (LDDocumentPointer* ptr); |
|
92 void removeReference (LDDocumentPointer* ptr); |
|
93 |
|
94 inline LDDocument& operator<< (LDObject* obj) |
|
95 { |
|
96 addObject (obj); |
|
97 return *this; |
|
98 } |
|
99 |
|
100 inline void addHistoryStep() |
|
101 { |
|
102 m_History->addStep(); |
|
103 } |
|
104 |
|
105 inline void undo() |
|
106 { |
|
107 m_History->undo(); |
|
108 } |
|
109 |
|
110 inline void redo() |
|
111 { |
|
112 m_History->redo(); |
|
113 } |
|
114 |
|
115 inline void clearHistory() |
|
116 { |
|
117 m_History->clear(); |
|
118 } |
|
119 |
|
120 inline void addToHistory (AbstractHistoryEntry* entry) |
|
121 { |
|
122 *m_History << entry; |
|
123 } |
|
124 |
|
125 static void closeUnused(); |
|
126 static LDDocument* current(); |
|
127 static void setCurrent (LDDocument* f); |
|
128 static void closeInitialFile(); |
|
129 static int countExplicitFiles(); |
|
130 |
|
131 // Turns a full path into a relative path |
|
132 static QString shortenName (QString a); |
|
133 |
|
134 protected: |
|
135 void addToSelection (LDObject* obj); |
|
136 void removeFromSelection (LDObject* obj); |
|
137 |
|
138 LDGLData* getGLData() |
|
139 { |
|
140 return m_gldata; |
|
141 } |
|
142 |
|
143 friend class LDObject; |
|
144 friend class GLRenderer; |
|
145 |
|
146 private: |
|
147 LDObjectList m_sel; |
|
148 LDGLData* m_gldata; |
|
149 |
|
150 // If set to true, next inline of this document discards the cache and |
|
151 // re-builds it. |
|
152 bool m_needsCache; |
|
153 |
|
154 static LDDocument* m_curdoc; |
|
155 }; |
|
156 |
|
157 inline LDDocument* getCurrentDocument() |
|
158 { |
|
159 return LDDocument::current(); |
|
160 } |
|
161 |
|
162 // Close all current loaded files and start off blank. |
|
163 void newFile(); |
|
164 |
|
165 // Opens the given file as the main file. Everything is closed first. |
|
166 void openMainFile (QString path); |
|
167 |
|
168 // Finds an OpenFile by name or null if not open |
|
169 LDDocument* findDocument (QString name); |
|
170 |
|
171 // Opens the given file and parses the LDraw code within. Returns a pointer |
|
172 // to the opened file or null on error. |
|
173 LDDocument* openDocument (QString path, bool search); |
|
174 |
|
175 // Opens the given file and returns a pointer to it, potentially looking in /parts and /p |
|
176 QFile* openLDrawFile (QString relpath, bool subdirs, QString* pathpointer = null); |
|
177 |
|
178 // Close all open files, whether user-opened or subfile caches. |
|
179 void closeAll(); |
|
180 |
|
181 // Parses a string line containing an LDraw object and returns the object parsed. |
|
182 LDObject* parseLine (QString line); |
|
183 |
|
184 // Retrieves the pointer to the given document by file name. Document is loaded |
|
185 // from file if necessary. Can return null if neither succeeds. |
|
186 LDDocument* getDocument (QString filename); |
|
187 |
|
188 // Re-caches all subfiles. |
|
189 void reloadAllSubfiles(); |
|
190 |
|
191 // Is it safe to close all files? |
|
192 bool safeToCloseAll(); |
|
193 |
|
194 LDObjectList loadFileContents (QFile* f, int* numWarnings, bool* ok = null); |
|
195 |
|
196 extern QList<LDDocument*> g_loadedFiles; |
|
197 |
|
198 inline const LDObjectList& selection() |
|
199 { |
|
200 return getCurrentDocument()->getSelection(); |
|
201 } |
|
202 |
|
203 void addRecentFile (QString path); |
|
204 void loadLogoedStuds(); |
|
205 QString basename (QString path); |
|
206 QString dirname (QString path); |
|
207 |
|
208 extern QList<LDDocument*> g_loadedFiles; // Vector of all currently opened files. |
|
209 |
|
210 // ============================================================================= |
|
211 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
|
212 // ============================================================================= |
|
213 // FileLoader |
|
214 // |
|
215 // Loads the given file and parses it to LDObjects using parseLine. It's a |
|
216 // separate class so as to be able to do the work progressively through the |
|
217 // event loop, allowing the program to maintain responsivity during loading. |
|
218 // ============================================================================= |
|
219 class LDFileLoader : public QObject |
|
220 { |
|
221 Q_OBJECT |
|
222 PROPERTY (private, LDObjectList, Objects, NO_OPS, STOCK_WRITE) |
|
223 PROPERTY (private, bool, Done, BOOL_OPS, STOCK_WRITE) |
|
224 PROPERTY (private, int, Progress, NUM_OPS, STOCK_WRITE) |
|
225 PROPERTY (private, bool, Aborted, BOOL_OPS, STOCK_WRITE) |
|
226 PROPERTY (public, QStringList, Lines, NO_OPS, STOCK_WRITE) |
|
227 PROPERTY (public, int*, Warnings, NO_OPS, STOCK_WRITE) |
|
228 PROPERTY (public, bool, OnForeground, BOOL_OPS, STOCK_WRITE) |
|
229 |
|
230 public slots: |
|
231 void start(); |
|
232 void abort(); |
|
233 |
|
234 private: |
|
235 OpenProgressDialog* dlg; |
|
236 |
|
237 private slots: |
|
238 void work (int i); |
|
239 |
|
240 signals: |
|
241 void progressUpdate (int progress); |
|
242 void workDone(); |
|
243 }; |
|
244 |
|
245 #endif // LDFORGE_DOCUMENT_H |