|
1 /* |
|
2 * LDForge: LDraw parts authoring CAD |
|
3 * Copyright (C) 2013 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 "main.h" |
|
23 #include "ldtypes.h" |
|
24 #include "history.h" |
|
25 #include <QObject> |
|
26 |
|
27 class History; |
|
28 class OpenProgressDialog; |
|
29 |
|
30 namespace LDPaths |
|
31 { void initPaths(); |
|
32 bool tryConfigure (str path); |
|
33 |
|
34 str ldconfig(); |
|
35 str prims(); |
|
36 str parts(); |
|
37 str getError(); |
|
38 } |
|
39 |
|
40 // ============================================================================= |
|
41 // LDDocument |
|
42 // |
|
43 // The LDDocument class stores a document opened in LDForge either as a editable |
|
44 // file for the user or for subfile caching. Its methods handle file input and |
|
45 // output. |
|
46 // |
|
47 // A file is implicit when they are opened automatically for caching purposes |
|
48 // and are hidden from the user. User-opened files are explicit (not implicit). |
|
49 // |
|
50 // The default name is a placeholder, initially suggested name for a file. The |
|
51 // primitive generator uses this to give initial names to primitives. |
|
52 // ============================================================================= |
|
53 class LDDocument : public QObject |
|
54 { properties: |
|
55 Q_OBJECT |
|
56 PROPERTY (private, QList<LDObject*>, Objects, NO_OPS, STOCK_WRITE) |
|
57 PROPERTY (private, History*, History, NO_OPS, STOCK_WRITE) |
|
58 PROPERTY (private, QList<LDObject*>, Vertices, NO_OPS, STOCK_WRITE) |
|
59 PROPERTY (public, str, Name, STR_OPS, STOCK_WRITE) |
|
60 PROPERTY (public, str, DefaultName, STR_OPS, STOCK_WRITE) |
|
61 PROPERTY (public, bool, Implicit, BOOL_OPS, STOCK_WRITE) |
|
62 PROPERTY (public, QList<LDObject*>, Cache, NO_OPS, STOCK_WRITE) |
|
63 PROPERTY (public, long, SavePosition, NUM_OPS, STOCK_WRITE) |
|
64 PROPERTY (public, QListWidgetItem*, ListItem, NO_OPS, STOCK_WRITE) |
|
65 |
|
66 public: |
|
67 LDDocument(); |
|
68 ~LDDocument(); |
|
69 |
|
70 int addObject (LDObject* obj); // Adds an object to this file at the end of the file. |
|
71 void addObjects (const QList<LDObject*> objs); |
|
72 void clearSelection(); |
|
73 void forgetObject (LDObject* obj); // Deletes the given object from the object chain. |
|
74 str getShortName(); |
|
75 const QList<LDObject*>& getSelection() const; |
|
76 bool hasUnsavedChanges() const; // Does this document.have unsaved changes? |
|
77 QList<LDObject*> inlineContents (LDSubfile::InlineFlags flags); |
|
78 void insertObj (int pos, LDObject* obj); |
|
79 int getObjectCount() const; |
|
80 LDObject* getObject (int pos) const; |
|
81 bool save (str path = ""); // Saves this file to disk. |
|
82 bool isSafeToClose(); // Perform safety checks. Do this before closing any files! |
|
83 void setObject (int idx, LDObject* obj); |
|
84 |
|
85 inline LDDocument& operator<< (LDObject* obj) |
|
86 { addObject (obj); |
|
87 return *this; |
|
88 } |
|
89 |
|
90 inline void addHistoryStep() |
|
91 { m_History->addStep(); |
|
92 } |
|
93 |
|
94 inline void undo() |
|
95 { m_History->undo(); |
|
96 } |
|
97 |
|
98 inline void redo() |
|
99 { m_History->redo(); |
|
100 } |
|
101 |
|
102 inline void clearHistory() |
|
103 { m_History->clear(); |
|
104 } |
|
105 |
|
106 inline void addToHistory (AbstractHistoryEntry* entry) |
|
107 { *m_History << entry; |
|
108 } |
|
109 |
|
110 static void closeUnused(); |
|
111 static LDDocument* current(); |
|
112 static void setCurrent (LDDocument* f); |
|
113 static void closeInitialFile(); |
|
114 static int countExplicitFiles(); |
|
115 |
|
116 protected: |
|
117 void addToSelection (LDObject* obj); |
|
118 void removeFromSelection (LDObject* obj); |
|
119 friend class LDObject; |
|
120 |
|
121 private: |
|
122 QList<LDObject*> m_sel; |
|
123 |
|
124 static LDDocument* m_curdoc; |
|
125 }; |
|
126 |
|
127 inline LDDocument* getCurrentDocument() |
|
128 { return LDDocument::current(); |
|
129 } |
|
130 |
|
131 // Close all current loaded files and start off blank. |
|
132 void newFile(); |
|
133 |
|
134 // Opens the given file as the main file. Everything is closed first. |
|
135 void openMainFile (str path); |
|
136 |
|
137 // Finds an OpenFile by name or null if not open |
|
138 LDDocument* findDocument (str name); |
|
139 |
|
140 // Opens the given file and parses the LDraw code within. Returns a pointer |
|
141 // to the opened file or null on error. |
|
142 LDDocument* openDocument (str path, bool search); |
|
143 |
|
144 // Opens the given file and returns a pointer to it, potentially looking in /parts and /p |
|
145 File* openLDrawFile (str relpath, bool subdirs); |
|
146 |
|
147 // Close all open files, whether user-opened or subfile caches. |
|
148 void closeAll(); |
|
149 |
|
150 // Parses a string line containing an LDraw object and returns the object parsed. |
|
151 LDObject* parseLine (str line); |
|
152 |
|
153 // Retrieves the pointer to the given document by file name. Document is loaded |
|
154 // from file if necessary. Can return null if neither succeeds. |
|
155 LDDocument* getDocument (str filename); |
|
156 |
|
157 // Re-caches all subfiles. |
|
158 void reloadAllSubfiles(); |
|
159 |
|
160 // Is it safe to close all files? |
|
161 bool safeToCloseAll(); |
|
162 |
|
163 QList<LDObject*> loadFileContents (File* f, int* numWarnings, bool* ok = null); |
|
164 |
|
165 extern QList<LDDocument*> g_loadedFiles; |
|
166 |
|
167 inline const QList<LDObject*>& selection() |
|
168 { return getCurrentDocument()->getSelection(); |
|
169 } |
|
170 |
|
171 void addRecentFile (str path); |
|
172 void loadLogoedStuds(); |
|
173 str basename (str path); |
|
174 str dirname (str path); |
|
175 |
|
176 extern QList<LDDocument*> g_loadedFiles; // Vector of all currently opened files. |
|
177 |
|
178 // ============================================================================= |
|
179 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
|
180 // ============================================================================= |
|
181 // FileLoader |
|
182 // |
|
183 // Loads the given file and parses it to LDObjects using parseLine. It's a |
|
184 // separate class so as to be able to do the work progressively through the |
|
185 // event loop, allowing the program to maintain responsivity during loading. |
|
186 // ============================================================================= |
|
187 class LDFileLoader : public QObject |
|
188 { Q_OBJECT |
|
189 PROPERTY (private, QList<LDObject*>, Objects, NO_OPS, STOCK_WRITE) |
|
190 PROPERTY (private, bool, Done, BOOL_OPS, STOCK_WRITE) |
|
191 PROPERTY (private, int, Progress, NUM_OPS, STOCK_WRITE) |
|
192 PROPERTY (private, bool, Aborted, BOOL_OPS, STOCK_WRITE) |
|
193 PROPERTY (public, QStringList, Lines, NO_OPS, STOCK_WRITE) |
|
194 PROPERTY (public, int*, Warnings, NO_OPS, STOCK_WRITE) |
|
195 PROPERTY (public, bool, OnForeground, BOOL_OPS, STOCK_WRITE) |
|
196 |
|
197 public slots: |
|
198 void start(); |
|
199 void abort(); |
|
200 |
|
201 private: |
|
202 OpenProgressDialog* dlg; |
|
203 |
|
204 private slots: |
|
205 void work (int i); |
|
206 |
|
207 signals: |
|
208 void progressUpdate (int progress); |
|
209 void workDone(); |
|
210 }; |
|
211 |
|
212 #endif // LDFORGE_DOCUMENT_H |