CreateDocumentDialog.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <Atom/RPI.Edit/Common/AssetUtils.h>
  9. #include <Atom/RPI.Reflect/Asset/AssetUtils.h>
  10. #include <AtomToolsFramework/Document/CreateDocumentDialog.h>
  11. #include <AtomToolsFramework/Util/Util.h>
  12. #include <AzCore/Utils/Utils.h>
  13. #include <AzFramework/Application/Application.h>
  14. #include <AzFramework/StringFunc/StringFunc.h>
  15. #include <AzQtComponents/Components/Widgets/FileDialog.h>
  16. #include <QDialogButtonBox>
  17. #include <QGridLayout>
  18. #include <QLabel>
  19. #include <QVBoxLayout>
  20. namespace AtomToolsFramework
  21. {
  22. CreateDocumentDialog::CreateDocumentDialog(
  23. const QString& title,
  24. const QString& sourceLabel,
  25. const QString& targetLabel,
  26. const QString& initialPath,
  27. const QStringList& supportedExtensions,
  28. const QString& defaultSourcePath,
  29. const FilterFn& filterFn,
  30. QWidget* parent)
  31. : QDialog(parent)
  32. , m_sourceLabel(sourceLabel)
  33. , m_targetLabel(targetLabel)
  34. , m_initialPath(initialPath)
  35. {
  36. setModal(true);
  37. setMinimumWidth(600);
  38. resize(500, 128);
  39. setWindowTitle(title);
  40. // Create the layout for all the widgets to be stacked vertically.
  41. auto verticalLayout = new QVBoxLayout();
  42. // The source selection combo box is used to pick from a set of source files or templates that can be used as a starting point or
  43. // parent for a new document. If there is no filter then no source selection widgets or connections will be made.
  44. if (filterFn)
  45. {
  46. auto sourceSelectionComboBoxLabel = new QLabel(this);
  47. sourceSelectionComboBoxLabel->setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed));
  48. sourceSelectionComboBoxLabel->setText(sourceLabel);
  49. verticalLayout->addWidget(sourceSelectionComboBoxLabel);
  50. m_sourceSelectionComboBox = new AssetSelectionComboBox(filterFn, this);
  51. m_sourceSelectionComboBox->setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed));
  52. m_sourceSelectionComboBox->Populate();
  53. m_sourceSelectionComboBox->SelectPath(defaultSourcePath.toUtf8().constData());
  54. m_sourcePath = m_sourceSelectionComboBox->GetSelectedPath().c_str();
  55. QObject::connect(m_sourceSelectionComboBox, &AssetSelectionComboBox::PathSelected, this, [this](const AZStd::string& path) {
  56. m_sourcePath = QString::fromUtf8(path.data(), static_cast<int>(path.size()));
  57. });
  58. verticalLayout->addWidget(m_sourceSelectionComboBox);
  59. }
  60. // Select a default location and unique name for the new document
  61. AZStd::string filename = GetUniqueFilePath(
  62. AZStd::string::format("%s/untitled.%s", m_initialPath.toUtf8().constData(), supportedExtensions.front().toUtf8().constData()));
  63. if (targetLabel.size())
  64. {
  65. auto targetSelectionBrowserLabel = new QLabel(this);
  66. targetSelectionBrowserLabel->setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed));
  67. targetSelectionBrowserLabel->setText(targetLabel);
  68. verticalLayout->addWidget(targetSelectionBrowserLabel);
  69. m_targetSelectionBrowser = new AzQtComponents::BrowseEdit(this);
  70. m_targetSelectionBrowser->setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed));
  71. m_targetSelectionBrowser->setLineEditReadOnly(true);
  72. verticalLayout->addWidget(m_targetSelectionBrowser);
  73. UpdateTargetPath(QFileInfo(filename.c_str()));
  74. // When the file selection button is pressed, open a file dialog to select where the document will be saved
  75. QObject::connect(
  76. m_targetSelectionBrowser,
  77. &AzQtComponents::BrowseEdit::attachedButtonTriggered,
  78. m_targetSelectionBrowser,
  79. [this, supportedExtensions]()
  80. {
  81. UpdateTargetPath(AzQtComponents::FileDialog::GetSaveFileName(
  82. this, m_targetLabel, m_targetPath, QString("(*.%1)").arg(supportedExtensions.join(");;(*."))));
  83. });
  84. }
  85. else
  86. {
  87. m_targetPath = filename.c_str();
  88. }
  89. // Connect ok and cancel buttons
  90. auto buttonBox = new QDialogButtonBox(this);
  91. buttonBox->setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed));
  92. buttonBox->setOrientation(Qt::Horizontal);
  93. buttonBox->setStandardButtons(QDialogButtonBox::Cancel | QDialogButtonBox::Ok);
  94. QObject::connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
  95. QObject::connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
  96. verticalLayout->addWidget(buttonBox);
  97. auto gridLayout = new QGridLayout(this);
  98. gridLayout->addLayout(verticalLayout, 0, 0, 1, 1);
  99. }
  100. CreateDocumentDialog::CreateDocumentDialog(const DocumentTypeInfo& documentType, const QString& initialPath, QWidget* parent)
  101. : CreateDocumentDialog(
  102. tr("Create %1 Document").arg(documentType.m_documentTypeName.c_str()),
  103. tr("Select source file, type, or template to create %1 document").arg(documentType.m_documentTypeName.c_str()),
  104. tr("Select target path to save %1 document").arg(documentType.m_documentTypeName.c_str()),
  105. initialPath,
  106. { documentType.GetDefaultExtensionToSave().c_str() },
  107. documentType.m_defaultDocumentTemplate.c_str(),
  108. documentType.m_supportedExtensionsToCreate.empty() ?
  109. FilterFn():
  110. [documentType](const AZStd::string& path)
  111. {
  112. // Only add source files with extensions supported by the document types creation rules
  113. // Ignore any files that are marked as non editable in the registry
  114. return documentType.IsSupportedExtensionToCreate(path) && !documentType.IsSupportedExtensionToSave(path) && IsDocumentPathEditable(path);
  115. },
  116. parent)
  117. {
  118. }
  119. void CreateDocumentDialog::UpdateTargetPath(const QFileInfo& fileInfo)
  120. {
  121. if (!fileInfo.absoluteFilePath().isEmpty())
  122. {
  123. m_targetPath = fileInfo.absoluteFilePath();
  124. m_targetSelectionBrowser->setText(m_targetPath);
  125. }
  126. }
  127. } // namespace AtomToolsFramework
  128. #include <AtomToolsFramework/Document/moc_CreateDocumentDialog.cpp>