src/toolsets/filetoolset.cpp

changeset 1052
6fd1597b688e
parent 1051
28ab6cad2c32
child 1053
2a48c0fff107
equal deleted inserted replaced
1051:28ab6cad2c32 1052:6fd1597b688e
30 #include "../dialogs/generateprimitivedialog.h" 30 #include "../dialogs/generateprimitivedialog.h"
31 #include "../documentmanager.h" 31 #include "../documentmanager.h"
32 #include "filetoolset.h" 32 #include "filetoolset.h"
33 #include "ui_about.h" 33 #include "ui_about.h"
34 34
35 FileToolset::FileToolset (MainWindow* parent) : 35 FileToolset::FileToolset(MainWindow* parent)
36 Toolset (parent) {} 36 : Toolset(parent) {}
37 37
38 void FileToolset::newPart() 38 void FileToolset::newPart()
39 { 39 {
40 NewPartDialog* dlg = new NewPartDialog (m_window); 40 NewPartDialog* dialog = new NewPartDialog {m_window};
41 41
42 if (dlg->exec() == QDialog::Accepted) 42 if (dialog->exec() == QDialog::Accepted)
43 { 43 {
44 m_window->createBlankDocument(); 44 m_window->createBlankDocument();
45 dlg->fillHeader (currentDocument()); 45 dialog->fillHeader(currentDocument());
46 m_window->doFullRefresh(); 46 m_window->doFullRefresh();
47 } 47 }
48 } 48 }
49 49
50 void FileToolset::newFile() 50 void FileToolset::newFile()
52 m_window->createBlankDocument(); 52 m_window->createBlankDocument();
53 } 53 }
54 54
55 void FileToolset::open() 55 void FileToolset::open()
56 { 56 {
57 QString name = QFileDialog::getOpenFileName (m_window, "Open File", "", "LDraw files (*.dat *.ldr)"); 57 QString name = QFileDialog::getOpenFileName(m_window, "Open File", "", "LDraw files (*.dat *.ldr)");
58 58
59 if (name.isEmpty()) 59 if (not name.isEmpty())
60 return; 60 m_documents->openMainModel (name);
61
62 m_documents->openMainModel (name);
63 } 61 }
64 62
65 void FileToolset::save() 63 void FileToolset::save()
66 { 64 {
67 m_window->save (currentDocument(), false); 65 m_window->save(currentDocument(), false);
68 } 66 }
69 67
70 void FileToolset::saveAs() 68 void FileToolset::saveAs()
71 { 69 {
72 m_window->save (currentDocument(), true); 70 m_window->save(currentDocument(), true);
73 } 71 }
74 72
75 void FileToolset::saveAll() 73 void FileToolset::saveAll()
76 { 74 {
77 for (LDDocument* file : m_documents->allDocuments()) 75 for (LDDocument* document : m_documents->allDocuments())
78 m_window->save (file, false); 76 m_window->save(document, false);
79 } 77 }
80 78
81 void FileToolset::close() 79 void FileToolset::close()
82 { 80 {
83 if (not currentDocument()->isSafeToClose()) 81 if (currentDocument()->isSafeToClose())
84 return; 82 currentDocument()->close();
85
86 currentDocument()->close();
87 } 83 }
88 84
89 void FileToolset::closeAll() 85 void FileToolset::closeAll()
90 { 86 {
91 if (m_documents->isSafeToCloseAll()) 87 if (m_documents->isSafeToCloseAll())
92 m_documents->clear(); 88 m_documents->clear();
93 } 89 }
94 90
95 void FileToolset::settings() 91 void FileToolset::settings()
96 { 92 {
97 (new ConfigDialog (m_window))->exec(); 93 (new ConfigDialog {m_window})->exec();
98 } 94 }
99 95
100 void FileToolset::setLDrawPath() 96 void FileToolset::setLDrawPath()
101 { 97 {
102 LDrawPathDialog* dialog = new LDrawPathDialog (m_config->lDrawPath(), true); 98 LDrawPathDialog* dialog = new LDrawPathDialog {m_config->lDrawPath(), true};
103 99
104 if (dialog->exec()) 100 if (dialog->exec())
105 m_config->setLDrawPath (dialog->path()); 101 m_config->setLDrawPath (dialog->path());
106 } 102 }
107 103
108 void FileToolset::exit() 104 void FileToolset::exit()
109 { 105 {
110 ::exit (EXIT_SUCCESS); 106 ::exit(EXIT_SUCCESS);
111 } 107 }
112 108
113 void FileToolset::insertFrom() 109 void FileToolset::insertFrom()
114 { 110 {
115 QString fname = QFileDialog::getOpenFileName(); 111 QString filePath = QFileDialog::getOpenFileName();
116 int idx = m_window->suggestInsertPoint(); 112 int position = m_window->suggestInsertPoint();
117 113
118 if (not fname.length()) 114 if (not filePath.isEmpty())
119 return; 115 {
120 116 QFile file = {filePath};
121 QFile f (fname); 117
122 118 if (file.open(QIODevice::ReadOnly))
123 if (not f.open (QIODevice::ReadOnly)) 119 {
124 { 120 // TODO: shouldn't need to go to the document manager to parse a file
125 Critical (format ("Couldn't open %1 (%2)", fname, f.errorString())); 121 LDObjectList objects = m_documents->loadFileContents(&file, nullptr, nullptr);
126 return; 122
127 } 123 currentDocument()->clearSelection();
128 124
129 // TODO: shouldn't need to go to the document manager to parse a file 125 for (LDObject* object : objects)
130 LDObjectList objs = m_documents->loadFileContents (&f, nullptr, nullptr); 126 {
131 127 currentDocument()->insertObject (position, object);
132 currentDocument()->clearSelection(); 128 object->select();
133 129 m_window->renderer()->compileObject (object);
134 for (LDObject* obj : objs) 130 position++;
135 { 131 }
136 currentDocument()->insertObject (idx, obj); 132
137 obj->select(); 133 m_window->refresh();
138 m_window->renderer()->compileObject (obj); 134 m_window->scrollToSelection();
139 135 }
140 idx++; 136 else
141 } 137 {
142 138 Critical(format("Couldn't open %1 (%2)", filePath, file.errorString()));
143 m_window->refresh(); 139 }
144 m_window->scrollToSelection(); 140 }
145 } 141 }
146 142
147 void FileToolset::exportTo() 143 void FileToolset::exportTo()
148 { 144 {
149 if (selectedObjects().isEmpty()) 145 if (selectedObjects().isEmpty())
150 return; 146 return;
151 147
152 QString fname = QFileDialog::getSaveFileName(); 148 QString filePath = QFileDialog::getSaveFileName();
153 149
154 if (fname.length() == 0) 150 if (filePath.length() == 0)
155 return; 151 return;
156 152
157 QFile file (fname); 153 QFile file = {filePath};
158 154
159 if (not file.open (QIODevice::WriteOnly | QIODevice::Text)) 155 if (file.open(QIODevice::WriteOnly | QIODevice::Text))
160 { 156 {
161 Critical (format ("Unable to open %1 for writing (%2)", fname, file.errorString())); 157 for (LDObject* obj : selectedObjects())
162 return; 158 {
163 } 159 QString contents = obj->asText();
164 160 QByteArray data = contents.toUtf8();
165 for (LDObject* obj : selectedObjects()) 161 file.write(data, data.size());
166 { 162 file.write("\r\n", 2);
167 QString contents = obj->asText(); 163 }
168 QByteArray data = contents.toUtf8(); 164 }
169 file.write (data, data.size()); 165 else
170 file.write ("\r\n", 2); 166 {
167 Critical(format("Unable to open %1 for writing (%2)", filePath, file.errorString()));
171 } 168 }
172 } 169 }
173 170
174 void FileToolset::scanPrimitives() 171 void FileToolset::scanPrimitives()
175 { 172 {
176 primitives()->startScan(); 173 primitives()->startScan();
177 } 174 }
178 175
179 void FileToolset::openSubfiles() 176 void FileToolset::openSubfiles()
180 { 177 {
181 for (LDObject* obj : selectedObjects()) 178 for (LDObject* object : selectedObjects())
182 { 179 {
183 LDSubfileReference* ref = dynamic_cast<LDSubfileReference*> (obj); 180 LDSubfileReference* reference = dynamic_cast<LDSubfileReference*>(object);
184 181
185 if (ref and ref->fileInfo()->isCache()) 182 if (reference and reference->fileInfo()->isCache())
186 ref->fileInfo()->openForEditing(); 183 reference->fileInfo()->openForEditing();
187 } 184 }
188 } 185 }
189 186
190 void FileToolset::downloadFrom() 187 void FileToolset::downloadFrom()
191 { 188 {
192 PartDownloader* dialog = new PartDownloader (m_window); 189 PartDownloader* dialog = new PartDownloader (m_window);
193 connect (dialog, &PartDownloader::primaryFileDownloaded, [&]() 190 connect(dialog, &PartDownloader::primaryFileDownloaded, [&]()
194 { 191 {
195 m_window->changeDocument (dialog->primaryFile()); 192 m_window->changeDocument (dialog->primaryFile());
196 m_window->doFullRefresh(); 193 m_window->doFullRefresh();
197 m_window->renderer()->resetAngles(); 194 m_window->renderer()->resetAngles();
198 }); 195 });
201 198
202 void FileToolset::makePrimitive() 199 void FileToolset::makePrimitive()
203 { 200 {
204 GeneratePrimitiveDialog* dialog = new GeneratePrimitiveDialog(m_window); 201 GeneratePrimitiveDialog* dialog = new GeneratePrimitiveDialog(m_window);
205 202
206 if (not dialog->exec()) 203 if (dialog->exec())
207 return; 204 {
208 205 LDDocument* primitive = primitives()->generatePrimitive(dialog->spec());
209 LDDocument* primitive = primitives()->generatePrimitive(dialog->spec()); 206 primitive->openForEditing();
210 primitive->openForEditing(); 207 m_window->save(primitive, false);
211 m_window->save(primitive, false); 208 }
212 } 209 }
213 210
214 // These are not exactly file tools but I don't want to make another toolset just for 3 very small actions 211 // These are not exactly file tools but I don't want to make another toolset just for 3 very small actions
215 void FileToolset::help() 212 void FileToolset::help()
216 { 213 {

mercurial