| 29 #include "../dialogs/newpartdialog.h" |
29 #include "../dialogs/newpartdialog.h" |
| 30 #include "../documentmanager.h" |
30 #include "../documentmanager.h" |
| 31 #include "filetoolset.h" |
31 #include "filetoolset.h" |
| 32 #include "ui_makeprim.h" |
32 #include "ui_makeprim.h" |
| 33 |
33 |
| 34 FileToolset::FileToolset (MainWindow* parent) : |
34 FileToolset::FileToolset(MainWindow* parent) : |
| 35 Toolset (parent) {} |
35 Toolset(parent) {} |
| 36 |
36 |
| 37 void FileToolset::newPart() |
37 void FileToolset::newPart() |
| 38 { |
38 { |
| 39 NewPartDialog* dlg = new NewPartDialog (m_window); |
39 NewPartDialog* dlg = new NewPartDialog(m_window); |
| 40 |
40 |
| 41 if (dlg->exec() == QDialog::Accepted) |
41 if (dlg->exec() == QDialog::Accepted) |
| 42 { |
42 { |
| 43 m_window->createBlankDocument(); |
43 m_window->createBlankDocument(); |
| 44 dlg->fillHeader (currentDocument()); |
44 dlg->fillHeader(currentDocument()); |
| 45 m_window->doFullRefresh(); |
45 m_window->doFullRefresh(); |
| 46 } |
46 } |
| 47 } |
47 } |
| 48 |
48 |
| 49 void FileToolset::newFile() |
49 void FileToolset::newFile() |
| 51 m_window->createBlankDocument(); |
51 m_window->createBlankDocument(); |
| 52 } |
52 } |
| 53 |
53 |
| 54 void FileToolset::open() |
54 void FileToolset::open() |
| 55 { |
55 { |
| 56 QString name = QFileDialog::getOpenFileName (m_window, "Open File", "", "LDraw files (*.dat *.ldr)"); |
56 QString name = QFileDialog::getOpenFileName(m_window, "Open File", "", "LDraw files(*.dat *.ldr)"); |
| 57 |
57 |
| 58 if (name.isEmpty()) |
58 if (name.isEmpty()) |
| 59 return; |
59 return; |
| 60 |
60 |
| 61 m_documents->openMainModel (name); |
61 m_documents->openMainModel(name); |
| 62 } |
62 } |
| 63 |
63 |
| 64 void FileToolset::save() |
64 void FileToolset::save() |
| 65 { |
65 { |
| 66 m_window->save (currentDocument(), false); |
66 m_window->save(currentDocument(), false); |
| 67 } |
67 } |
| 68 |
68 |
| 69 void FileToolset::saveAs() |
69 void FileToolset::saveAs() |
| 70 { |
70 { |
| 71 m_window->save (currentDocument(), true); |
71 m_window->save(currentDocument(), true); |
| 72 } |
72 } |
| 73 |
73 |
| 74 void FileToolset::saveAll() |
74 void FileToolset::saveAll() |
| 75 { |
75 { |
| 76 for (LDDocument* file : m_documents->allDocuments()) |
76 for (LDDocument* file : m_documents->allDocuments()) |
| 77 m_window->save (file, false); |
77 m_window->save(file, false); |
| 78 } |
78 } |
| 79 |
79 |
| 80 void FileToolset::close() |
80 void FileToolset::close() |
| 81 { |
81 { |
| 82 if (not currentDocument()->isSafeToClose()) |
82 if (not currentDocument()->isSafeToClose()) |
| 91 m_documents->clear(); |
91 m_documents->clear(); |
| 92 } |
92 } |
| 93 |
93 |
| 94 void FileToolset::settings() |
94 void FileToolset::settings() |
| 95 { |
95 { |
| 96 (new ConfigDialog (m_window))->exec(); |
96 (new ConfigDialog(m_window))->exec(); |
| 97 } |
97 } |
| 98 |
98 |
| 99 void FileToolset::setLDrawPath() |
99 void FileToolset::setLDrawPath() |
| 100 { |
100 { |
| 101 LDrawPathDialog* dialog = new LDrawPathDialog (config.lDrawPath(), true); |
101 LDrawPathDialog* dialog = new LDrawPathDialog(config.lDrawPath(), true); |
| 102 |
102 |
| 103 if (dialog->exec()) |
103 if (dialog->exec()) |
| 104 config.setLDrawPath (dialog->path()); |
104 config.setLDrawPath(dialog->path()); |
| 105 } |
105 } |
| 106 |
106 |
| 107 void FileToolset::exit() |
107 void FileToolset::exit() |
| 108 { |
108 { |
| 109 ::exit (EXIT_SUCCESS); |
109 ::exit(EXIT_SUCCESS); |
| 110 } |
110 } |
| 111 |
111 |
| 112 void FileToolset::insertFrom() |
112 void FileToolset::insertFrom() |
| 113 { |
113 { |
| 114 QString fname = QFileDialog::getOpenFileName(); |
114 QString fname = QFileDialog::getOpenFileName(); |
| 115 int idx = m_window->suggestInsertPoint(); |
115 int idx = m_window->suggestInsertPoint(); |
| 116 |
116 |
| 117 if (not fname.length()) |
117 if (not fname.length()) |
| 118 return; |
118 return; |
| 119 |
119 |
| 120 QFile f (fname); |
120 QFile f(fname); |
| 121 |
121 |
| 122 if (not f.open (QIODevice::ReadOnly)) |
122 if (not f.open(QIODevice::ReadOnly)) |
| 123 { |
123 { |
| 124 Critical (format ("Couldn't open %1 (%2)", fname, f.errorString())); |
124 Critical(format("Couldn't open %1(%2)", fname, f.errorString())); |
| 125 return; |
125 return; |
| 126 } |
126 } |
| 127 |
127 |
| 128 // TODO: shouldn't need to go to the document manager to parse a file |
128 // TODO: shouldn't need to go to the document manager to parse a file |
| 129 LDObjectList objs = m_documents->loadFileContents (&f, nullptr, nullptr); |
129 LDObjectList objs = m_documents->loadFileContents(&f, nullptr, nullptr); |
| 130 |
130 |
| 131 currentDocument()->clearSelection(); |
131 currentDocument()->clearSelection(); |
| 132 |
132 |
| 133 for (LDObject* obj : objs) |
133 for (LDObject* obj : objs) |
| 134 { |
134 { |
| 135 currentDocument()->insertObj (idx, obj); |
135 currentDocument()->insertObj(idx, obj); |
| 136 obj->select(); |
136 obj->select(); |
| 137 m_window->renderer()->compileObject (obj); |
137 m_window->renderer()->compileObject(obj); |
| 138 |
138 |
| 139 idx++; |
139 idx++; |
| 140 } |
140 } |
| 141 |
141 |
| 142 m_window->refresh(); |
142 m_window->refresh(); |
| 151 QString fname = QFileDialog::getSaveFileName(); |
151 QString fname = QFileDialog::getSaveFileName(); |
| 152 |
152 |
| 153 if (fname.length() == 0) |
153 if (fname.length() == 0) |
| 154 return; |
154 return; |
| 155 |
155 |
| 156 QFile file (fname); |
156 QFile file(fname); |
| 157 |
157 |
| 158 if (not file.open (QIODevice::WriteOnly | QIODevice::Text)) |
158 if (not file.open(QIODevice::WriteOnly | QIODevice::Text)) |
| 159 { |
159 { |
| 160 Critical (format ("Unable to open %1 for writing (%2)", fname, file.errorString())); |
160 Critical(format("Unable to open %1 for writing(%2)", fname, file.errorString())); |
| 161 return; |
161 return; |
| 162 } |
162 } |
| 163 |
163 |
| 164 for (LDObject* obj : selectedObjects()) |
164 for (LDObject* obj : selectedObjects()) |
| 165 { |
165 { |
| 166 QString contents = obj->asText(); |
166 QString contents = obj->asText(); |
| 167 QByteArray data = contents.toUtf8(); |
167 QByteArray data = contents.toUtf8(); |
| 168 file.write (data, data.size()); |
168 file.write(data, data.size()); |
| 169 file.write ("\r\n", 2); |
169 file.write("\r\n", 2); |
| 170 } |
170 } |
| 171 } |
171 } |
| 172 |
172 |
| 173 void FileToolset::scanPrimitives() |
173 void FileToolset::scanPrimitives() |
| 174 { |
174 { |
| 177 |
177 |
| 178 void FileToolset::openSubfiles() |
178 void FileToolset::openSubfiles() |
| 179 { |
179 { |
| 180 for (LDObject* obj : selectedObjects()) |
180 for (LDObject* obj : selectedObjects()) |
| 181 { |
181 { |
| 182 LDSubfileReference* ref = dynamic_cast<LDSubfileReference*> (obj); |
182 LDSubfileReference* ref = dynamic_cast<LDSubfileReference*>(obj); |
| 183 |
183 |
| 184 if (ref and ref->fileInfo()->isCache()) |
184 if (ref and ref->fileInfo()->isCache()) |
| 185 ref->fileInfo()->openForEditing(); |
185 ref->fileInfo()->openForEditing(); |
| 186 } |
186 } |
| 187 } |
187 } |
| 188 |
188 |
| 189 void FileToolset::downloadFrom() |
189 void FileToolset::downloadFrom() |
| 190 { |
190 { |
| 191 PartDownloader* dialog = new PartDownloader (m_window); |
191 PartDownloader* dialog = new PartDownloader(m_window); |
| 192 connect (dialog, &PartDownloader::primaryFileDownloaded, [&]() |
192 connect(dialog, &PartDownloader::primaryFileDownloaded, [&]() |
| 193 { |
193 { |
| 194 m_window->changeDocument (dialog->primaryFile()); |
194 m_window->changeDocument(dialog->primaryFile()); |
| 195 m_window->doFullRefresh(); |
195 m_window->doFullRefresh(); |
| 196 m_window->renderer()->resetAngles(); |
196 m_window->renderer()->resetAngles(); |
| 197 }); |
197 }); |
| 198 dialog->exec(); |
198 dialog->exec(); |
| 199 } |
199 } |
| 200 |
200 |
| 201 void FileToolset::makePrimitive() |
201 void FileToolset::makePrimitive() |
| 202 { |
202 { |
| 203 PrimitivePrompt* dlg = new PrimitivePrompt (m_window); |
203 PrimitivePrompt* dlg = new PrimitivePrompt(m_window); |
| 204 |
204 |
| 205 if (not dlg->exec()) |
205 if (not dlg->exec()) |
| 206 return; |
206 return; |
| 207 |
207 |
| 208 int segs = dlg->ui->sb_segs->value(); |
208 int segs = dlg->ui->sb_segs->value(); |
| 213 dlg->ui->rb_cylinder->isChecked() ? Cylinder : |
213 dlg->ui->rb_cylinder->isChecked() ? Cylinder : |
| 214 dlg->ui->rb_disc->isChecked() ? Disc : |
214 dlg->ui->rb_disc->isChecked() ? Disc : |
| 215 dlg->ui->rb_ndisc->isChecked() ? DiscNeg : |
215 dlg->ui->rb_ndisc->isChecked() ? DiscNeg : |
| 216 dlg->ui->rb_ring->isChecked() ? Ring : Cone; |
216 dlg->ui->rb_ring->isChecked() ? Ring : Cone; |
| 217 |
217 |
| 218 LDDocument* f = GeneratePrimitive (type, segs, divs, num); |
218 LDDocument* f = GeneratePrimitive(type, segs, divs, num); |
| 219 f->openForEditing(); |
219 f->openForEditing(); |
| 220 m_window->save (f, false); |
220 m_window->save(f, false); |
| 221 } |
221 } |
| 222 |
222 |
| 223 // These are not exactly file tools but I don't want to make another toolset just for 3 very small actions |
223 // These are not exactly file tools but I don't want to make another toolset just for 3 very small actions |
| 224 void FileToolset::help() |
224 void FileToolset::help() |
| 225 { |
225 { |