src/ldDocument.h

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

mercurial