* @brief Prompts for a save path, writes the imported project, then opens it; the * work is queued so the UI dialog that launched the import (and the macOS * NSSavePanel KVO callback) can unwind before the model re-enters. */
| 2495 | * NSSavePanel KVO callback) can unwind before the model re-enters. |
| 2496 | */ |
| 2497 | void DataModel::ProjectModel::importProjectFromJson(const QJsonObject& project, |
| 2498 | const QString& suggestedFileName) |
| 2499 | { |
| 2500 | QMetaObject::invokeMethod( |
| 2501 | this, |
| 2502 | [this, project, suggestedFileName]() { |
| 2503 | QString suggested = suggestedFileName.isEmpty() ? tr("Untitled Project") : suggestedFileName; |
| 2504 | if (!suggested.endsWith(QStringLiteral(".ssproj"), Qt::CaseInsensitive)) |
| 2505 | suggested += QStringLiteral(".ssproj"); |
| 2506 | |
| 2507 | const QString defaultPath = jsonProjectsPath() + QStringLiteral("/") + suggested; |
| 2508 | |
| 2509 | auto* dialog = new QFileDialog(qApp->activeWindow(), |
| 2510 | tr("Save Imported Project"), |
| 2511 | defaultPath, |
| 2512 | tr("Serial Studio Project Files (*.ssproj)")); |
| 2513 | |
| 2514 | dialog->setAcceptMode(QFileDialog::AcceptSave); |
| 2515 | dialog->setFileMode(QFileDialog::AnyFile); |
| 2516 | dialog->setAttribute(Qt::WA_DeleteOnClose); |
| 2517 | |
| 2518 | auto accepted = std::make_shared<bool>(false); |
| 2519 | auto chosenPath = std::make_shared<QString>(); |
| 2520 | connect( |
| 2521 | dialog, &QFileDialog::fileSelected, this, [accepted, chosenPath](const QString& path) { |
| 2522 | if (path.isEmpty()) |
| 2523 | return; |
| 2524 | |
| 2525 | *accepted = true; |
| 2526 | *chosenPath = path; |
| 2527 | if (!chosenPath->endsWith(QStringLiteral(".ssproj"), Qt::CaseInsensitive)) |
| 2528 | *chosenPath += QStringLiteral(".ssproj"); |
| 2529 | }); |
| 2530 | |
| 2531 | connect(dialog, &QFileDialog::finished, this, [this, accepted, chosenPath, project](int) { |
| 2532 | if (!*accepted) { |
| 2533 | Q_EMIT importCompleted(false, QString()); |
| 2534 | return; |
| 2535 | } |
| 2536 | |
| 2537 | QMetaObject::invokeMethod( |
| 2538 | this, |
| 2539 | [this, chosenPath, project]() { |
| 2540 | QFile file(*chosenPath); |
| 2541 | if (!file.open(QFile::WriteOnly)) { |
| 2542 | if (m_suppressMessageBoxes) |
| 2543 | qWarning() << "[ProjectModel] Import save error:" << file.errorString(); |
| 2544 | else |
| 2545 | Misc::Utilities::showMessageBox( |
| 2546 | tr("File open error"), file.errorString(), QMessageBox::Critical); |
| 2547 | |
| 2548 | Q_EMIT importCompleted(false, QString()); |
| 2549 | return; |
| 2550 | } |
| 2551 | |
| 2552 | file.write(QJsonDocument(project).toJson(QJsonDocument::Indented)); |
| 2553 | file.close(); |
| 2554 |
no test coverage detected