| 471 | } |
| 472 | |
| 473 | bool Template::execSwitchTemplateFileDialog(QWidget* dialog_parent) |
| 474 | { |
| 475 | QString new_path = FileDialog::getOpenFileName(dialog_parent, |
| 476 | tr("Find the moved template file"), |
| 477 | QString(), |
| 478 | tr("All files (*.*)") ); |
| 479 | new_path = QFileInfo(new_path).canonicalFilePath(); |
| 480 | if (new_path.isEmpty()) |
| 481 | return false; |
| 482 | |
| 483 | const State old_state = getTemplateState(); |
| 484 | const QString old_path = getTemplatePath(); |
| 485 | |
| 486 | if (auto* placeholder = qobject_cast<TemplatePlaceholder*>(this)) |
| 487 | { |
| 488 | setTemplatePath(new_path); |
| 489 | auto new_temp = placeholder->makeActualTemplate(); |
| 490 | if (new_temp) |
| 491 | { |
| 492 | auto pos = map->findTemplateIndex(this); |
| 493 | if (pos >= 0) |
| 494 | { |
| 495 | auto self = map->setTemplate(pos, std::move(new_temp)); |
| 496 | if (map->getTemplate(pos)->loadTemplateFile()) |
| 497 | { |
| 498 | // Loading succeeded. This object must be destroyed. |
| 499 | self.release()->deleteLater(); |
| 500 | return true; |
| 501 | } |
| 502 | // Loading failed. This object must be put back. |
| 503 | map->setTemplate(pos, std::move(self)); |
| 504 | return false; |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | else |
| 509 | { |
| 510 | switchTemplateFile(new_path, true); |
| 511 | } |
| 512 | if (getTemplateState() != Loaded) |
| 513 | { |
| 514 | QString error_template = QCoreApplication::translate("OpenOrienteering::TemplateListWidget", "Cannot open template\n%1:\n%2").arg(new_path); |
| 515 | QString error = errorString(); |
| 516 | Q_ASSERT(!error.isEmpty()); |
| 517 | |
| 518 | // Revert change |
| 519 | switchTemplateFile(old_path, old_state == Loaded); |
| 520 | if (old_state == Invalid) |
| 521 | { |
| 522 | template_state = Invalid; |
| 523 | } |
| 524 | |
| 525 | QMessageBox::warning(dialog_parent, tr("Error"), error_template.arg(error)); |
| 526 | return false; |
| 527 | } |
| 528 | |
| 529 | return true; |
| 530 | } |
no test coverage detected