|
1 /* |
|
2 * LDForge: LDraw parts authoring CAD |
|
3 * Copyright (C) 2013 - 2015 Teemu 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 #include <QFileDialog> |
|
20 #include <QMessageBox> |
|
21 #include "../configDialog.h" |
|
22 #include "../dialogs.h" |
|
23 #include "../glRenderer.h" |
|
24 #include "../ldDocument.h" |
|
25 #include "../mainwindow.h" |
|
26 #include "../partDownloader.h" |
|
27 #include "../primitives.h" |
|
28 #include "../dialogs/ldrawpathdialog.h" |
|
29 #include "../dialogs/newpartdialog.h" |
|
30 #include "filetoolset.h" |
|
31 #include "ui_makeprim.h" |
|
32 |
|
33 EXTERN_CFGENTRY (String, LDrawPath) |
|
34 |
|
35 FileToolset::FileToolset (MainWindow* parent) : |
|
36 Toolset (parent) {} |
|
37 |
|
38 void FileToolset::newPart() |
|
39 { |
|
40 NewPartDialog* dlg = new NewPartDialog (m_window); |
|
41 |
|
42 if (dlg->exec() == QDialog::Accepted) |
|
43 { |
|
44 newFile(); |
|
45 dlg->fillHeader (CurrentDocument()); |
|
46 m_window->doFullRefresh(); |
|
47 } |
|
48 } |
|
49 |
|
50 void FileToolset::newFile() |
|
51 { |
|
52 newFile(); |
|
53 } |
|
54 |
|
55 void FileToolset::open() |
|
56 { |
|
57 QString name = QFileDialog::getOpenFileName (m_window, "Open File", "", "LDraw files (*.dat *.ldr)"); |
|
58 |
|
59 if (name.isEmpty()) |
|
60 return; |
|
61 |
|
62 OpenMainModel (name); |
|
63 } |
|
64 |
|
65 void FileToolset::save() |
|
66 { |
|
67 m_window->save (CurrentDocument(), false); |
|
68 } |
|
69 |
|
70 void FileToolset::saveAs() |
|
71 { |
|
72 m_window->save (CurrentDocument(), true); |
|
73 } |
|
74 |
|
75 void FileToolset::saveAll() |
|
76 { |
|
77 for (LDDocument* file : LDDocument::explicitDocuments()) |
|
78 m_window->save (file, false); |
|
79 } |
|
80 |
|
81 void FileToolset::close() |
|
82 { |
|
83 if (not CurrentDocument()->isSafeToClose()) |
|
84 return; |
|
85 |
|
86 CurrentDocument()->dismiss(); |
|
87 } |
|
88 |
|
89 void FileToolset::closeAll() |
|
90 { |
|
91 if (not IsSafeToCloseAll()) |
|
92 return; |
|
93 |
|
94 CloseAllDocuments(); |
|
95 } |
|
96 |
|
97 void FileToolset::settings() |
|
98 { |
|
99 (new ConfigDialog)->exec(); |
|
100 } |
|
101 |
|
102 void FileToolset::setLDrawPath() |
|
103 { |
|
104 (new LDrawPathDialog (cfg::LDrawPath, true))->exec(); |
|
105 } |
|
106 |
|
107 void FileToolset::exit() |
|
108 { |
|
109 Exit(); |
|
110 } |
|
111 |
|
112 void FileToolset::insertFrom() |
|
113 { |
|
114 QString fname = QFileDialog::getOpenFileName(); |
|
115 int idx = m_window->getInsertionPoint(); |
|
116 |
|
117 if (not fname.length()) |
|
118 return; |
|
119 |
|
120 QFile f (fname); |
|
121 |
|
122 if (not f.open (QIODevice::ReadOnly)) |
|
123 { |
|
124 Critical (format ("Couldn't open %1 (%2)", fname, f.errorString())); |
|
125 return; |
|
126 } |
|
127 |
|
128 LDObjectList objs = LoadFileContents (&f, null); |
|
129 |
|
130 CurrentDocument()->clearSelection(); |
|
131 |
|
132 for (LDObject* obj : objs) |
|
133 { |
|
134 CurrentDocument()->insertObj (idx, obj); |
|
135 obj->select(); |
|
136 m_window->R()->compileObject (obj); |
|
137 |
|
138 idx++; |
|
139 } |
|
140 |
|
141 m_window->refresh(); |
|
142 m_window->scrollToSelection(); |
|
143 } |
|
144 |
|
145 void FileToolset::exportTo() |
|
146 { |
|
147 if (Selection().isEmpty()) |
|
148 return; |
|
149 |
|
150 QString fname = QFileDialog::getSaveFileName(); |
|
151 |
|
152 if (fname.length() == 0) |
|
153 return; |
|
154 |
|
155 QFile file (fname); |
|
156 |
|
157 if (not file.open (QIODevice::WriteOnly | QIODevice::Text)) |
|
158 { |
|
159 Critical (format ("Unable to open %1 for writing (%2)", fname, file.errorString())); |
|
160 return; |
|
161 } |
|
162 |
|
163 for (LDObject* obj : Selection()) |
|
164 { |
|
165 QString contents = obj->asText(); |
|
166 QByteArray data = contents.toUtf8(); |
|
167 file.write (data, data.size()); |
|
168 file.write ("\r\n", 2); |
|
169 } |
|
170 } |
|
171 |
|
172 void FileToolset::scanPrimitives() |
|
173 { |
|
174 PrimitiveScanner::start(); |
|
175 } |
|
176 |
|
177 void FileToolset::openSubfiles() |
|
178 { |
|
179 for (LDObject* obj : Selection()) |
|
180 { |
|
181 LDSubfile* ref = dynamic_cast<LDSubfile*> (obj); |
|
182 |
|
183 if (ref == null or not ref->fileInfo()->isImplicit()) |
|
184 continue; |
|
185 |
|
186 ref->fileInfo()->setImplicit (false); |
|
187 } |
|
188 } |
|
189 |
|
190 void FileToolset::downloadFrom() |
|
191 { |
|
192 PartDownloader::staticBegin(); |
|
193 } |
|
194 |
|
195 void FileToolset::makePrimitive() |
|
196 { |
|
197 PrimitivePrompt* dlg = new PrimitivePrompt (g_win); |
|
198 |
|
199 if (not dlg->exec()) |
|
200 return; |
|
201 |
|
202 int segs = dlg->ui->sb_segs->value(); |
|
203 int divs = dlg->ui->cb_hires->isChecked() ? HighResolution : LowResolution; |
|
204 int num = dlg->ui->sb_ringnum->value(); |
|
205 PrimitiveType type = |
|
206 dlg->ui->rb_circle->isChecked() ? Circle : |
|
207 dlg->ui->rb_cylinder->isChecked() ? Cylinder : |
|
208 dlg->ui->rb_disc->isChecked() ? Disc : |
|
209 dlg->ui->rb_ndisc->isChecked() ? DiscNeg : |
|
210 dlg->ui->rb_ring->isChecked() ? Ring : Cone; |
|
211 |
|
212 LDDocument* f = GeneratePrimitive (type, segs, divs, num); |
|
213 f->setImplicit (false); |
|
214 m_window->save (f, false); |
|
215 } |
|
216 |
|
217 // These are not exactly file tools but I don't want to make another toolset just for 3 very small actions |
|
218 void FileToolset::help() |
|
219 { |
|
220 // Not yet implemented |
|
221 } |
|
222 |
|
223 void FileToolset::about() |
|
224 { |
|
225 AboutDialog().exec(); |
|
226 } |
|
227 |
|
228 void FileToolset::aboutQt() |
|
229 { |
|
230 QMessageBox::aboutQt (m_window); |
|
231 } |