src/file.h

changeset 667
31540c1f22ea
parent 666
c595cfb4791c
parent 626
f3c01a033e9e
child 668
6a05c4969074
equal deleted inserted replaced
666:c595cfb4791c 667:31540c1f22ea
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 FILE_H
20 #define FILE_H
21
22 #include "common.h"
23 #include "ldtypes.h"
24 #include "history.h"
25 #include <QObject>
26
27 #define curfile LDFile::current()
28
29 class History;
30 class OpenProgressDialog;
31
32 namespace LDPaths
33 { void initPaths();
34 bool tryConfigure (str path);
35
36 str ldconfig();
37 str prims();
38 str parts();
39 str getError();
40 }
41
42 // =============================================================================
43 // LDFile
44 //
45 // The LDFile class stores a file opened in LDForge either as a editable file
46 // for the user or for 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 LDFile : public QObject
55 { Q_OBJECT
56 READ_PROPERTY (QList<LDObject*>, objects, setObjects)
57 READ_PROPERTY (History, history, setHistory)
58 READ_PROPERTY (QList<LDObject*>, vertices, setVertices)
59 PROPERTY (str, name, setName)
60 PROPERTY (str, defaultName, setDefaultName)
61 PROPERTY (bool, implicit, setImplicit)
62 PROPERTY (QList<LDObject*>, cache, setCache)
63 PROPERTY (long, savePos, setSavePos)
64 DECLARE_PROPERTY (QListWidgetItem*, listItem, setListItem)
65
66 public:
67 LDFile();
68 ~LDFile();
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*>& selection() const;
76 bool hasUnsavedChanges() const; // Does this file have unsaved changes?
77 QList<LDObject*> inlineContents (LDSubfile::InlineFlags flags);
78 void insertObj (int pos, LDObject* obj);
79 int numObjs() const;
80 LDObject* object (int pos) const;
81 LDObject* obj (int pos) const;
82 bool save (str path = ""); // Saves this file to disk.
83 bool safeToClose(); // Perform safety checks. Do this before closing any files!
84 void setObject (int idx, LDObject* obj);
85
86 inline LDFile& operator<< (LDObject* obj)
87 { addObject (obj);
88 return *this;
89 }
90
91 inline void openHistory()
92 { m_history.open();
93 }
94
95 inline void closeHistory()
96 { m_history.close();
97 }
98
99 inline void undo()
100 { m_history.undo();
101 }
102
103 inline void redo()
104 { m_history.redo();
105 }
106
107 inline void clearHistory()
108 { m_history.clear();
109 }
110
111 inline void addToHistory (AbstractHistoryEntry* entry)
112 { m_history << entry;
113 }
114
115 static void closeUnused();
116 static LDFile* current();
117 static void setCurrent (LDFile* f);
118 static void closeInitialFile();
119 static int countExplicitFiles();
120
121 protected:
122 void addToSelection (LDObject* obj);
123 void removeFromSelection (LDObject* obj);
124 friend class LDObject;
125
126 private:
127 QList<LDObject*> m_sel;
128
129 static LDFile* m_curfile;
130 };
131
132 // Close all current loaded files and start off blank.
133 void newFile();
134
135 // Opens the given file as the main file. Everything is closed first.
136 void openMainFile (str path);
137
138 // Finds an OpenFile by name or null if not open
139 LDFile* findLoadedFile (str name);
140
141 // Opens the given file and parses the LDraw code within. Returns a pointer
142 // to the opened file or null on error.
143 LDFile* openDATFile (str path, bool search);
144
145 // Opens the given file and returns a pointer to it, potentially looking in /parts and /p
146 File* openLDrawFile (str relpath, bool subdirs);
147
148 // Close all open files, whether user-opened or subfile caches.
149 void closeAll();
150
151 // Parses a string line containing an LDraw object and returns the object parsed.
152 LDObject* parseLine (str line);
153
154 // Retrieves the pointer to - or loads - the given subfile.
155 LDFile* getFile (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<LDFile*> g_loadedFiles;
166
167 inline const QList<LDObject*>& selection()
168 { return LDFile::current()->selection();
169 }
170
171 void addRecentFile (str path);
172 void loadLogoedStuds();
173 str basename (str path);
174 str dirname (str path);
175
176 extern QList<LDFile*> 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 FileLoader : public QObject
188 { Q_OBJECT
189 READ_PROPERTY (QList<LDObject*>, objs, setObjects)
190 READ_PROPERTY (bool, done, setDone)
191 READ_PROPERTY (int, progress, setProgress)
192 READ_PROPERTY (bool, aborted, setAborted)
193 PROPERTY (QList<str>, lines, setLines)
194 PROPERTY (int*, warningsPointer, setWarningsPointer)
195 PROPERTY (bool, concurrent, setConcurrent)
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 // FILE_H

mercurial