src/mainwindow.cpp

changeset 148
e1ced2523cad
parent 147
37f936073cac
child 160
536efae7137e
equal deleted inserted replaced
147:37f936073cac 148:e1ced2523cad
118 118
119 void MainWindow::openModelFromPath(const QString& path) 119 void MainWindow::openModelFromPath(const QString& path)
120 { 120 {
121 QString errorString; 121 QString errorString;
122 QTextStream errorStream{&errorString}; 122 QTextStream errorStream{&errorString};
123 QString modelName = this->documents.openModel(path, errorStream); 123 std::optional<ModelId> modelIdOpt = this->documents.openModel(
124 if (not modelName.isEmpty()) 124 path,
125 { 125 errorStream,
126 this->documents.loadDependenciesForModel(modelName, path, this->libraries, errorStream); 126 DocumentManager::OpenType::ManuallyOpened);
127 if (modelIdOpt.has_value())
128 {
129 const ModelId modelId = modelIdOpt.value();
130 this->documents.loadDependenciesForModel(modelId, path, this->libraries, errorStream);
127 if (not errorString.isEmpty()) 131 if (not errorString.isEmpty())
128 { 132 {
129 QMessageBox::warning( 133 QMessageBox::warning(
130 this, 134 this,
131 tr("Problem loading references"), 135 tr("Problem loading references"),
132 errorString); 136 errorString);
133 } 137 }
134 this->openModelForEditing(modelName); 138 this->openModelForEditing(modelId);
135 this->addRecentlyOpenedFile(path); 139 this->addRecentlyOpenedFile(path);
136 } 140 }
137 else 141 else
138 { 142 {
139 QMessageBox::critical( 143 QMessageBox::critical(
179 } 183 }
180 this->saveSettings(); 184 this->saveSettings();
181 this->updateRecentlyOpenedDocumentsMenu(); 185 this->updateRecentlyOpenedDocumentsMenu();
182 } 186 }
183 187
184 void MainWindow::openModelForEditing(const QString& modelName) 188 void MainWindow::openModelForEditing(const ModelId modelId)
185 { 189 {
186 Document* document = new Document{this->documents.findModelByName(modelName), &this->documents, this->colorTable}; 190 Document* document = new Document{this->documents.getModelById(modelId), &this->documents, this->colorTable};
187 document->setRenderPreferences(this->renderPreferences); 191 document->setRenderPreferences(this->renderPreferences);
188 connect(document, &Document::newStatusText, [&](const QString& newStatusText) 192 connect(document, &Document::newStatusText, [&](const QString& newStatusText)
189 { 193 {
190 this->statusBar()->showMessage(newStatusText); 194 this->statusBar()->showMessage(newStatusText);
191 }); 195 });
192 this->ui->tabs->addTab(document, modelName); 196 const QFileInfo fileInfo{*this->documents.modelPath(modelId)};
197 this->ui->tabs->addTab(document, fileInfo.baseName());
193 this->ui->tabs->setCurrentWidget(document); 198 this->ui->tabs->setCurrentWidget(document);
194 document->restoreSplitterState(this->documentSplitterState); 199 document->restoreSplitterState(this->documentSplitterState);
195 } 200 }
196 201
197 void MainWindow::runSettingsEditor() 202 void MainWindow::runSettingsEditor()
207 Document* MainWindow::currentDocument() 212 Document* MainWindow::currentDocument()
208 { 213 {
209 return qobject_cast<Document*>(this->ui->tabs->currentWidget()); 214 return qobject_cast<Document*>(this->ui->tabs->currentWidget());
210 } 215 }
211 216
217 /*
212 void MainWindow::closeDocument(Document *document) 218 void MainWindow::closeDocument(Document *document)
213 { 219 {
214 const int tabIndex = this->ui->tabs->indexOf(document); 220 const int tabIndex = this->ui->tabs->indexOf(document);
215 if (tabIndex != -1) 221 if (tabIndex != -1)
216 { 222 {
226 { 232 {
227 openedDocumentNames.insert(pathToName(document->modelPath())); 233 openedDocumentNames.insert(pathToName(document->modelPath()));
228 } 234 }
229 } 235 }
230 } 236 }
237 */
231 238
232 void MainWindow::handleDocumentSplitterChange() 239 void MainWindow::handleDocumentSplitterChange()
233 { 240 {
234 Document* currentDocument = this->currentDocument(); 241 Document* currentDocument = this->currentDocument();
235 if (currentDocument != nullptr) 242 if (currentDocument != nullptr)
281 */ 288 */
282 void MainWindow::actionSave() 289 void MainWindow::actionSave()
283 { 290 {
284 if (this->currentDocument() != nullptr) 291 if (this->currentDocument() != nullptr)
285 { 292 {
286 if (this->currentDocument()->modelPath().isEmpty()) 293 const ModelId modelId = {0};
294 const QString* path = this->documents.modelPath(modelId);
295 if (path == nullptr or path->isEmpty())
287 { 296 {
288 this->actionSaveAs(); 297 this->actionSaveAs();
289 } 298 }
290 else 299 else
291 { 300 {
292 QString error; 301 QString error;
293 QTextStream errorStream{&error}; 302 QTextStream errorStream{&error};
294 const bool succeeded = this->currentDocument()->save(errorStream); 303 const bool succeeded = this->documents.saveModel(modelId, errorStream);
295 if (not succeeded) 304 if (not succeeded)
296 { 305 {
297 QMessageBox::critical(this, tr("Save error"), error); 306 QMessageBox::critical(this, tr("Save error"), error);
298 } 307 }
299 else 308 else
300 { 309 {
301 this->addRecentlyOpenedFile(this->currentDocument()->modelPath()); 310 this->addRecentlyOpenedFile(*path);
302 } 311 }
303 } 312 }
304 } 313 }
305 } 314 }
306 315
309 */ 318 */
310 void MainWindow::actionSaveAs() 319 void MainWindow::actionSaveAs()
311 { 320 {
312 if (this->currentDocument() != nullptr) 321 if (this->currentDocument() != nullptr)
313 { 322 {
314 const QString dir = QFileInfo{this->currentDocument()->modelPath()}.absoluteDir().path(); 323 const ModelId modelId = {0};
324 const QString* pathPtr = this->documents.modelPath(modelId);
325 QString defaultPath = (pathPtr != nullptr) ? *pathPtr : "";
315 const QString newPath = QFileDialog::getSaveFileName( 326 const QString newPath = QFileDialog::getSaveFileName(
316 this, 327 this,
317 tr("Save as…"), 328 tr("Save as…"),
318 dir, 329 QFileInfo{defaultPath}.absoluteDir().path(),
319 tr("LDraw files (*.ldr *dat);;All files (*)") 330 tr("LDraw files (*.ldr *dat);;All files (*)")
320 ); 331 );
321 if (not newPath.isEmpty()) 332 if (not newPath.isEmpty())
322 { 333 {
323 this->currentDocument()->setModelPath(newPath); 334 QString error;
335 QTextStream errorStream{&error};
336 this->documents.setModelPath(modelId, newPath, this->libraries, errorStream);
324 this->ui->tabs->setTabText(this->ui->tabs->currentIndex(), QFileInfo{newPath}.fileName()); 337 this->ui->tabs->setTabText(this->ui->tabs->currentIndex(), QFileInfo{newPath}.fileName());
325 this->actionSave(); 338 this->actionSave();
326 } 339 }
327 } 340 }
328 } 341 }

mercurial