|
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 #pragma once |
|
20 #include <QObject> |
|
21 #include "main.h" |
|
22 #include "ldObject.h" |
|
23 #include "editHistory.h" |
|
24 |
|
25 class History; |
|
26 class OpenProgressDialog; |
|
27 class LDDocumentPointer; |
|
28 struct LDGLData; |
|
29 |
|
30 namespace LDPaths |
|
31 { |
|
32 void initPaths(); |
|
33 bool tryConfigure (QString path); |
|
34 |
|
35 QString ldconfig(); |
|
36 QString prims(); |
|
37 QString parts(); |
|
38 QString getError(); |
|
39 } |
|
40 |
|
41 // ============================================================================= |
|
42 // |
|
43 // This class stores a document either as a editable file for the user or for |
|
44 // subfile caching. Its methods handle file input and output. |
|
45 // |
|
46 // A file is implicit when they are opened automatically for caching purposes |
|
47 // and are hidden from the user. User-opened files are explicit (not implicit). |
|
48 // |
|
49 // The default name is a placeholder, initially suggested name for a file. The |
|
50 // primitive generator uses this to give initial names to primitives. |
|
51 // |
|
52 class LDDocument : public QObject |
|
53 { |
|
54 public: |
|
55 using ReferenceList = QList<LDDocumentPointer*>; |
|
56 |
|
57 Q_OBJECT |
|
58 PROPERTY (public, QString, name, setName, STOCK_WRITE) |
|
59 PROPERTY (private, LDObjectList, objects, setObjects, STOCK_WRITE) |
|
60 PROPERTY (private, LDObjectList, cache, setCache, STOCK_WRITE) |
|
61 PROPERTY (private, History*, history, setHistory, STOCK_WRITE) |
|
62 PROPERTY (private, LDObjectList, vertices, setVertices, STOCK_WRITE) |
|
63 PROPERTY (private, ReferenceList, references, setReferences, STOCK_WRITE) |
|
64 PROPERTY (public, QString, fullPath, setFullPath, STOCK_WRITE) |
|
65 PROPERTY (public, QString, defaultName, setDefaultName, STOCK_WRITE) |
|
66 PROPERTY (public, bool, isImplicit, setImplicit, STOCK_WRITE) |
|
67 PROPERTY (public, long, savePosition, setSavePosition, STOCK_WRITE) |
|
68 PROPERTY (public, int, tabIndex, setTabIndex, STOCK_WRITE) |
|
69 |
|
70 public: |
|
71 LDDocument(); |
|
72 ~LDDocument(); |
|
73 |
|
74 int addObject (LDObject* obj); // Adds an object to this file at the end of the file. |
|
75 void addObjects (const LDObjectList objs); |
|
76 void clearSelection(); |
|
77 void forgetObject (LDObject* obj); // Deletes the given object from the object chain. |
|
78 QString getDisplayName(); |
|
79 const LDObjectList& getSelection() const; |
|
80 bool hasUnsavedChanges() const; // Does this document have unsaved changes? |
|
81 LDObjectList inlineContents (LDSubfile::InlineFlags flags); |
|
82 void insertObj (int pos, LDObject* obj); |
|
83 int getObjectCount() const; |
|
84 LDObject* getObject (int pos) const; |
|
85 bool save (QString path = ""); // Saves this file to disk. |
|
86 void swapObjects (LDObject* one, LDObject* other); |
|
87 bool isSafeToClose(); // Perform safety checks. Do this before closing any files! |
|
88 void setObject (int idx, LDObject* obj); |
|
89 void addReference (LDDocumentPointer* ptr); |
|
90 void removeReference (LDDocumentPointer* ptr); |
|
91 |
|
92 inline LDDocument& operator<< (LDObject* obj) |
|
93 { |
|
94 addObject (obj); |
|
95 return *this; |
|
96 } |
|
97 |
|
98 inline void addHistoryStep() |
|
99 { |
|
100 history()->addStep(); |
|
101 } |
|
102 |
|
103 inline void undo() |
|
104 { |
|
105 history()->undo(); |
|
106 } |
|
107 |
|
108 inline void redo() |
|
109 { |
|
110 history()->redo(); |
|
111 } |
|
112 |
|
113 inline void clearHistory() |
|
114 { |
|
115 history()->clear(); |
|
116 } |
|
117 |
|
118 inline void addToHistory (AbstractHistoryEntry* entry) |
|
119 { |
|
120 *history() << entry; |
|
121 } |
|
122 |
|
123 static void closeUnused(); |
|
124 static LDDocument* current(); |
|
125 static void setCurrent (LDDocument* f); |
|
126 static void closeInitialFile(); |
|
127 static int countExplicitFiles(); |
|
128 |
|
129 // Turns a full path into a relative path |
|
130 static QString shortenName (QString a); |
|
131 |
|
132 protected: |
|
133 void addToSelection (LDObject* obj); |
|
134 void removeFromSelection (LDObject* obj); |
|
135 |
|
136 LDGLData* getGLData() |
|
137 { |
|
138 return m_gldata; |
|
139 } |
|
140 |
|
141 friend class LDObject; |
|
142 friend class GLRenderer; |
|
143 |
|
144 private: |
|
145 LDObjectList m_sel; |
|
146 LDGLData* m_gldata; |
|
147 |
|
148 // If set to true, next inline of this document discards the cache and |
|
149 // re-builds it. |
|
150 bool m_needsCache; |
|
151 |
|
152 static LDDocument* m_curdoc; |
|
153 }; |
|
154 |
|
155 inline LDDocument* getCurrentDocument() |
|
156 { |
|
157 return LDDocument::current(); |
|
158 } |
|
159 |
|
160 // Close all current loaded files and start off blank. |
|
161 void newFile(); |
|
162 |
|
163 // Opens the given file as the main file. Everything is closed first. |
|
164 void openMainFile (QString path); |
|
165 |
|
166 // Finds an OpenFile by name or null if not open |
|
167 LDDocument* findDocument (QString name); |
|
168 |
|
169 // Opens the given file and parses the LDraw code within. Returns a pointer |
|
170 // to the opened file or null on error. |
|
171 LDDocument* openDocument (QString path, bool search); |
|
172 |
|
173 // Opens the given file and returns a pointer to it, potentially looking in /parts and /p |
|
174 QFile* openLDrawFile (QString relpath, bool subdirs, QString* pathpointer = null); |
|
175 |
|
176 // Close all open files, whether user-opened or subfile caches. |
|
177 void closeAll(); |
|
178 |
|
179 // Parses a string line containing an LDraw object and returns the object parsed. |
|
180 LDObject* parseLine (QString line); |
|
181 |
|
182 // Retrieves the pointer to the given document by file name. Document is loaded |
|
183 // from file if necessary. Can return null if neither succeeds. |
|
184 LDDocument* getDocument (QString filename); |
|
185 |
|
186 // Re-caches all subfiles. |
|
187 void reloadAllSubfiles(); |
|
188 |
|
189 // Is it safe to close all files? |
|
190 bool safeToCloseAll(); |
|
191 |
|
192 LDObjectList loadFileContents (QFile* f, int* numWarnings, bool* ok = null); |
|
193 |
|
194 extern QList<LDDocument*> g_loadedFiles; |
|
195 |
|
196 inline const LDObjectList& selection() |
|
197 { |
|
198 return getCurrentDocument()->getSelection(); |
|
199 } |
|
200 |
|
201 void addRecentFile (QString path); |
|
202 void loadLogoedStuds(); |
|
203 QString basename (QString path); |
|
204 QString dirname (QString path); |
|
205 |
|
206 extern QList<LDDocument*> g_loadedFiles; // Vector of all currently opened files. |
|
207 |
|
208 // ============================================================================= |
|
209 // |
|
210 // LDFileLoader |
|
211 // |
|
212 // Loads the given file and parses it to LDObjects using parseLine. It's a |
|
213 // separate class so as to be able to do the work progressively through the |
|
214 // event loop, allowing the program to maintain responsivity during loading. |
|
215 // |
|
216 class LDFileLoader : public QObject |
|
217 { |
|
218 Q_OBJECT |
|
219 PROPERTY (private, LDObjectList, objects, setObjects, STOCK_WRITE) |
|
220 PROPERTY (private, bool, isDone, setDone, STOCK_WRITE) |
|
221 PROPERTY (private, int, progress, setProgress, STOCK_WRITE) |
|
222 PROPERTY (private, bool, isAborted, setAborted, STOCK_WRITE) |
|
223 PROPERTY (public, QStringList, lines, setLines, STOCK_WRITE) |
|
224 PROPERTY (public, int*, warnings, setWarnings, STOCK_WRITE) |
|
225 PROPERTY (public, bool, isOnForeground, setOnForeground, STOCK_WRITE) |
|
226 |
|
227 public slots: |
|
228 void start(); |
|
229 void abort(); |
|
230 |
|
231 private: |
|
232 OpenProgressDialog* dlg; |
|
233 |
|
234 private slots: |
|
235 void work (int i); |
|
236 |
|
237 signals: |
|
238 void progressUpdate (int progress); |
|
239 void workDone(); |
|
240 }; |