* @brief Shows a dialog to pick and load a built-in painter template, then * offers to add the datasets the chosen template expects. */
| 378 | * offers to add the datasets the chosen template expects. |
| 379 | */ |
| 380 | void DataModel::PainterCodeEditor::selectTemplate() |
| 381 | { |
| 382 | if (m_templateNames.isEmpty()) |
| 383 | return; |
| 384 | |
| 385 | bool ok; |
| 386 | const auto name = QInputDialog::getItem(nullptr, |
| 387 | tr("Select Painter Widget Template"), |
| 388 | tr("Choose a template to load:"), |
| 389 | m_templateNames, |
| 390 | 0, |
| 391 | false, |
| 392 | &ok); |
| 393 | |
| 394 | if (!ok) |
| 395 | return; |
| 396 | |
| 397 | const int idx = m_templateNames.indexOf(name); |
| 398 | if (idx < 0 || idx >= m_templateFiles.size()) |
| 399 | return; |
| 400 | |
| 401 | QFile file(m_templateFiles.at(idx)); |
| 402 | if (file.open(QFile::ReadOnly)) { |
| 403 | m_widget.setPlainText(QString::fromUtf8(file.readAll())); |
| 404 | m_widget.document()->clearUndoRedoStacks(); |
| 405 | m_widget.document()->setModified(false); |
| 406 | Q_EMIT modifiedChanged(); |
| 407 | file.close(); |
| 408 | } |
| 409 | |
| 410 | const QString templateFile = QFileInfo(m_templateFiles.at(idx)).completeBaseName(); |
| 411 | const auto specs = templateDatasetSpecs(templateFile); |
| 412 | if (specs.isEmpty()) |
| 413 | return; |
| 414 | |
| 415 | auto& editor = DataModel::ProjectEditor::instance(); |
| 416 | if (!editor.currentGroupIsPainter()) |
| 417 | return; |
| 418 | |
| 419 | auto& pm = DataModel::ProjectModel::instance(); |
| 420 | const auto& groups = pm.groups(); |
| 421 | const int gid = editor.currentGroupId(); |
| 422 | if (gid < 0 || static_cast<size_t>(gid) >= groups.size()) |
| 423 | return; |
| 424 | |
| 425 | const int existing = static_cast<int>(groups[gid].datasets.size()); |
| 426 | const int missing = qMax(0, specs.size() - existing); |
| 427 | if (missing == 0) |
| 428 | return; |
| 429 | |
| 430 | const auto choice = |
| 431 | QMessageBox::question(nullptr, |
| 432 | tr("Add datasets for this template?"), |
| 433 | tr("\"%1\" expects %2 dataset(s); the current group has %3.\n\n" |
| 434 | "Add %4 dataset(s) using the template's defaults?") |
| 435 | .arg(name) |
| 436 | .arg(specs.size()) |
| 437 | .arg(existing) |
nothing calls this directly
no test coverage detected