MainWindow.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 <source/ui/MainWindow.h>
  9. #include <source/ui/ui_MainWindow.h>
  10. #include <source/utils/GUIApplicationManager.h>
  11. #include <AzCore/std/utils.h>
  12. #include <AzToolsFramework/UI/Logging/LogTableItemDelegate.h>
  13. #include <AzQtComponents/Utilities/QtWindowUtilities.h>
  14. #include <QAction>
  15. #include <QApplication>
  16. #include <QClipboard>
  17. #include <QCloseEvent>
  18. #include <QDateTime>
  19. #include <QDesktopServices>
  20. namespace AssetBundler
  21. {
  22. MainWindow::MainWindow(AssetBundler::GUIApplicationManager* guiApplicationManager, QWidget* parent)
  23. : QMainWindow(parent)
  24. , m_guiApplicationManager(guiApplicationManager)
  25. {
  26. m_ui.reset(new Ui::MainWindow);
  27. m_ui->setupUi(this);
  28. m_ui->verticalLayout->setContentsMargins(0, 0, 0, 0);
  29. // Set up Log
  30. m_logModel.reset(new AzToolsFramework::Logging::LogTableModel(this));
  31. m_ui->logTableView->setModel(m_logModel.data());
  32. m_ui->logTableView->setIndentation(0);
  33. m_ui->logTableView->setContextMenuPolicy(Qt::CustomContextMenu);
  34. connect(m_ui->logTableView, &QWidget::customContextMenuRequested, this, &MainWindow::ShowLogContextMenu);
  35. AZ::Debug::TraceMessageBus::Handler::BusConnect();
  36. // Create the Unsaved Changes Popup
  37. m_unsavedChangesMsgBox.reset(new QMessageBox(this));
  38. m_unsavedChangesMsgBox->setText(tr("There are unsaved changes."));
  39. m_unsavedChangesMsgBox->setInformativeText(tr("Would you like to save all changes before quitting?"));
  40. m_unsavedChangesMsgBox->setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
  41. m_unsavedChangesMsgBox->setDefaultButton(QMessageBox::Save);
  42. // Set up Quit functionality
  43. m_ui->actionClose->setShortcut(0x0 | Qt::Key_Q | Qt::CTRL);
  44. m_ui->actionClose->setMenuRole(QAction::QuitRole);
  45. connect(m_ui->actionClose, &QAction::triggered, this, &MainWindow::close);
  46. this->addAction(m_ui->actionClose);
  47. // Set up Tabs
  48. AssetBundlerTabWidget::InitAssetBundlerSettings(m_guiApplicationManager->GetCurrentProjectFolder().c_str());
  49. m_seedListTab.reset(new SeedTabWidget(
  50. this,
  51. m_guiApplicationManager,
  52. QString(m_guiApplicationManager->GetAssetBundlingFolder().c_str())));
  53. m_ui->tabWidget->addTab(m_seedListTab.data(), m_seedListTab->GetTabTitle());
  54. m_assetListTab.reset(new AssetListTabWidget(this, m_guiApplicationManager));
  55. m_ui->tabWidget->addTab(m_assetListTab.data(), m_assetListTab->GetTabTitle());
  56. m_rulesTab.reset(new RulesTabWidget(this, m_guiApplicationManager));
  57. m_ui->tabWidget->addTab(m_rulesTab.data(), m_rulesTab->GetTabTitle());
  58. m_bundleListTab.reset(new BundleListTabWidget(this, m_guiApplicationManager));
  59. m_ui->tabWidget->addTab(m_bundleListTab.data(), m_bundleListTab->GetTabTitle());
  60. // Set up link to documentation
  61. QAction* supportAction = new QAction(QIcon(":/stylesheet/img/help.svg"), "", this);
  62. connect(supportAction, &QAction::triggered, this, &MainWindow::OnSupportClicked);
  63. connect(m_ui->actionDocumentation, &QAction::triggered, this, &MainWindow::OnSupportClicked);
  64. m_ui->tabWidget->setActionToolBarVisible();
  65. m_ui->tabWidget->addAction(supportAction);
  66. // Set up Save functionality
  67. m_ui->actionSave->setShortcut(0x0 | Qt::Key_S | Qt::CTRL);
  68. connect(m_ui->actionSave, &QAction::triggered, this, &MainWindow::SaveCurrentSelection);
  69. m_ui->actionSaveAll->setShortcut(0x0 | Qt::Key_S | Qt::CTRL | Qt::SHIFT);
  70. connect(m_ui->actionSaveAll, &QAction::triggered, this, &MainWindow::SaveAll);
  71. }
  72. MainWindow::~MainWindow()
  73. {
  74. AZ::Debug::TraceMessageBus::Handler::BusDisconnect();
  75. m_guiApplicationManager = nullptr;
  76. }
  77. void MainWindow::Activate()
  78. {
  79. m_seedListTab->Activate();
  80. m_rulesTab->Activate();
  81. m_assetListTab->Activate();
  82. m_bundleListTab->Activate();
  83. }
  84. void MainWindow::ApplyConfig()
  85. {
  86. const GUIApplicationManager::Config& config = m_guiApplicationManager->GetConfig();
  87. // Event Log Details
  88. m_ui->logTableView->header()->resizeSection(AzToolsFramework::Logging::LogTableModel::ColumnType, config.logTypeColumnWidth);
  89. m_ui->logTableView->header()->resizeSection(AzToolsFramework::Logging::LogTableModel::ColumnWindow, config.logSourceColumnWidth);
  90. m_seedListTab->ApplyConfig();
  91. m_assetListTab->ApplyConfig();
  92. m_rulesTab->ApplyConfig();
  93. m_bundleListTab->ApplyConfig();
  94. }
  95. void MainWindow::WriteToLog(const AZStd::string& message, AzToolsFramework::Logging::LogLine::LogType logType)
  96. {
  97. WriteToLog(message.c_str(), logType);
  98. }
  99. void MainWindow::WriteToLog(const QString& message, AzToolsFramework::Logging::LogLine::LogType logType)
  100. {
  101. WriteToLog(message.toUtf8().data(), logType);
  102. }
  103. void MainWindow::WriteToLog(const char* message, AzToolsFramework::Logging::LogLine::LogType logType)
  104. {
  105. WriteToLog(message, "AssetBundler", logType);
  106. }
  107. void MainWindow::WriteToLog(const char* message, const char* window, AzToolsFramework::Logging::LogLine::LogType logType)
  108. {
  109. m_logModel->AppendLine(
  110. AzToolsFramework::Logging::LogLine(
  111. message,
  112. window,
  113. logType,
  114. QDateTime::currentMSecsSinceEpoch()));
  115. m_ui->logTableView->scrollToBottom();
  116. }
  117. bool MainWindow::OnPreError(const char* window, const char* /*fileName*/, int /*line*/, const char* /*func*/, const char* message)
  118. {
  119. WriteToLog(message, window, AzToolsFramework::Logging::LogLine::LogType::TYPE_ERROR);
  120. return true;
  121. }
  122. bool MainWindow::OnPreWarning(const char* window, const char* /*fileName*/, int /*line*/, const char* /*func*/, const char* message)
  123. {
  124. WriteToLog(message, window, AzToolsFramework::Logging::LogLine::LogType::TYPE_WARNING);
  125. return true;
  126. }
  127. bool MainWindow::OnPrintf(const char* /*window*/, const char* /*message*/)
  128. {
  129. return true;
  130. }
  131. void MainWindow::ShowWindow()
  132. {
  133. AzQtComponents::bringWindowToTop(this);
  134. }
  135. void MainWindow::closeEvent(QCloseEvent* event)
  136. {
  137. if (!HasUnsavedChanges())
  138. {
  139. // No need to ask the user if they want to quit when there are no unsaved changes
  140. event->accept();
  141. return;
  142. }
  143. int unsavedChangesResult = m_unsavedChangesMsgBox->exec();
  144. switch (unsavedChangesResult)
  145. {
  146. case QMessageBox::Save:
  147. // Save All was clicked
  148. SaveAll();
  149. event->accept();
  150. break;
  151. case QMessageBox::Discard:
  152. // Don't Save was clicked
  153. event->accept();
  154. break;
  155. case QMessageBox::Cancel:
  156. // Cancel was clicked
  157. event->ignore();
  158. break;
  159. default:
  160. // should never be reached
  161. AZ_Assert(false, "No result was returned by the Unsaved Changes Message Box!");
  162. break;
  163. }
  164. }
  165. void MainWindow::OnSupportClicked()
  166. {
  167. QDesktopServices::openUrl(
  168. QStringLiteral("https://o3de.org/docs/user-guide/packaging/asset-bundler/"));
  169. }
  170. void MainWindow::ShowLogContextMenu(const QPoint& pos)
  171. {
  172. QModelIndex index = m_ui->logTableView->indexAt(pos);
  173. QMenu menu;
  174. QAction* action = menu.addAction(tr("Copy line"), this, [&]()
  175. {
  176. QApplication::clipboard()->setText(index.data(AzToolsFramework::Logging::LogTableModel::LogLineTextRole).toString());
  177. });
  178. action->setEnabled(index.isValid());
  179. menu.exec(m_ui->logTableView->mapToGlobal(pos));
  180. }
  181. bool MainWindow::HasUnsavedChanges()
  182. {
  183. return m_seedListTab->HasUnsavedChanges() || m_rulesTab->HasUnsavedChanges();
  184. }
  185. void MainWindow::SaveCurrentSelection()
  186. {
  187. switch (m_ui->tabWidget->currentIndex())
  188. {
  189. case TabIndex::Seeds:
  190. m_seedListTab->SaveCurrentSelection();
  191. break;
  192. case TabIndex::Rules:
  193. m_rulesTab->SaveCurrentSelection();
  194. break;
  195. default:
  196. break;
  197. }
  198. }
  199. void MainWindow::SaveAll()
  200. {
  201. m_seedListTab->SaveAll();
  202. m_rulesTab->SaveAll();
  203. }
  204. } // namespace AssetBundler
  205. #include <source/ui/moc_MainWindow.cpp>