222 Document* MainWindow::currentDocument() |
222 Document* MainWindow::currentDocument() |
223 { |
223 { |
224 return qobject_cast<Document*>(this->ui->tabs->currentWidget()); |
224 return qobject_cast<Document*>(this->ui->tabs->currentWidget()); |
225 } |
225 } |
226 |
226 |
|
227 const Document* MainWindow::currentDocument() const |
|
228 { |
|
229 return qobject_cast<const Document*>(this->ui->tabs->currentWidget()); |
|
230 } |
|
231 |
227 void MainWindow::handleDocumentSplitterChange() |
232 void MainWindow::handleDocumentSplitterChange() |
228 { |
233 { |
229 Document* currentDocument = this->currentDocument(); |
234 Document* currentDocument = this->currentDocument(); |
230 if (currentDocument != nullptr) |
235 if (currentDocument != nullptr) |
231 { |
236 { |
283 */ |
288 */ |
284 void MainWindow::actionSave() |
289 void MainWindow::actionSave() |
285 { |
290 { |
286 if (this->currentDocument() != nullptr) |
291 if (this->currentDocument() != nullptr) |
287 { |
292 { |
288 const ModelId modelId = {0}; |
293 const std::optional<ModelId> modelId = this->findCurrentModelId(); |
289 const QString* path = this->documents.modelPath(modelId); |
294 if (modelId.has_value()) |
290 if (path == nullptr or path->isEmpty()) |
295 { |
291 { |
296 const QString* path = this->documents.modelPath(*modelId); |
292 this->actionSaveAs(); |
297 if (path == nullptr or path->isEmpty()) |
293 } |
|
294 else |
|
295 { |
|
296 QString error; |
|
297 QTextStream errorStream{&error}; |
|
298 const bool succeeded = this->documents.saveModel(modelId, errorStream); |
|
299 if (not succeeded) |
|
300 { |
298 { |
301 QMessageBox::critical(this, tr("Save error"), error); |
299 this->actionSaveAs(); |
302 } |
300 } |
303 else |
301 else |
304 { |
302 { |
305 this->addRecentlyOpenedFile(*path); |
303 QString error; |
|
304 QTextStream errorStream{&error}; |
|
305 const bool succeeded = this->documents.saveModel(*modelId, errorStream); |
|
306 if (not succeeded) |
|
307 { |
|
308 QMessageBox::critical(this, tr("Save error"), error); |
|
309 } |
|
310 else |
|
311 { |
|
312 this->addRecentlyOpenedFile(*path); |
|
313 } |
306 } |
314 } |
307 } |
315 } |
308 } |
316 } |
309 } |
317 } |
310 |
318 |
313 */ |
321 */ |
314 void MainWindow::actionSaveAs() |
322 void MainWindow::actionSaveAs() |
315 { |
323 { |
316 if (this->currentDocument() != nullptr) |
324 if (this->currentDocument() != nullptr) |
317 { |
325 { |
318 const ModelId modelId = {0}; |
326 const std::optional<ModelId> modelId = this->findCurrentModelId(); |
319 const QString* pathPtr = this->documents.modelPath(modelId); |
327 if (modelId.has_value()) |
320 QString defaultPath = (pathPtr != nullptr) ? *pathPtr : ""; |
328 { |
321 const QString newPath = QFileDialog::getSaveFileName( |
329 const QString* pathPtr = this->documents.modelPath(*modelId); |
322 this, |
330 QString defaultPath = (pathPtr != nullptr) ? *pathPtr : ""; |
323 tr("Save as…"), |
331 const QString newPath = QFileDialog::getSaveFileName( |
324 QFileInfo{defaultPath}.absoluteDir().path(), |
332 this, |
325 tr("LDraw files (*.ldr *dat);;All files (*)") |
333 tr("Save as…"), |
326 ); |
334 QFileInfo{defaultPath}.absoluteDir().path(), |
327 if (not newPath.isEmpty()) |
335 tr("LDraw files (*.ldr *dat);;All files (*)") |
328 { |
336 ); |
329 QString error; |
337 if (not newPath.isEmpty()) |
330 QTextStream errorStream{&error}; |
338 { |
331 this->documents.setModelPath(modelId, newPath, this->libraries, errorStream); |
339 QString error; |
332 this->ui->tabs->setTabText(this->ui->tabs->currentIndex(), QFileInfo{newPath}.fileName()); |
340 QTextStream errorStream{&error}; |
333 this->actionSave(); |
341 this->documents.setModelPath(*modelId, newPath, this->libraries, errorStream); |
|
342 this->ui->tabs->setTabText(this->ui->tabs->currentIndex(), QFileInfo{newPath}.fileName()); |
|
343 this->actionSave(); |
|
344 } |
334 } |
345 } |
335 } |
346 } |
336 } |
347 } |
337 |
348 |
338 /** |
349 /** |
392 std::optional<ModelId> modelId = this->documents.findIdForModel(&document->getModel()); |
403 std::optional<ModelId> modelId = this->documents.findIdForModel(&document->getModel()); |
393 if (modelId.has_value()) |
404 if (modelId.has_value()) |
394 { |
405 { |
395 this->documents.closeDocument(modelId.value()); |
406 this->documents.closeDocument(modelId.value()); |
396 delete document; |
407 delete document; |
|
408 } |
|
409 } |
|
410 |
|
411 std::optional<ModelId> MainWindow::findCurrentModelId() const |
|
412 { |
|
413 const Document* document = this->currentDocument(); |
|
414 if (document != nullptr) |
|
415 { |
|
416 return this->documents.findIdForModel(&document->getModel()); |
|
417 } |
|
418 else |
|
419 { |
|
420 return {}; |
397 } |
421 } |
398 } |
422 } |
399 |
423 |
400 void MainWindow::changeEvent(QEvent* event) |
424 void MainWindow::changeEvent(QEvent* event) |
401 { |
425 { |