Application.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
  3. * its licensors.
  4. *
  5. * For complete copyright and license terms please see the LICENSE at the root of this
  6. * distribution (the "License"). All use of this software is governed by the License,
  7. * or, if provided, by the license below or the license accompanying this file. Do not
  8. * remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
  9. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. *
  11. */
  12. #include <Application.h>
  13. #include <ProjectUtils.h>
  14. #include <AzCore/IO/FileIO.h>
  15. #include <AzCore/Utils/Utils.h>
  16. #include <AzCore/Settings/SettingsRegistryMergeUtils.h>
  17. #include <AzFramework/Logging/LoggingComponent.h>
  18. #include <AzQtComponents/Utilities/HandleDpiAwareness.h>
  19. #include <AzQtComponents/Components/StyleManager.h>
  20. #include <AzQtComponents/Components/WindowDecorationWrapper.h>
  21. #include <QApplication>
  22. #include <QDir>
  23. #include <QMessageBox>
  24. namespace O3DE::ProjectManager
  25. {
  26. Application::~Application()
  27. {
  28. TearDown();
  29. }
  30. bool Application::Init(bool interactive)
  31. {
  32. constexpr const char* applicationName { "O3DE" };
  33. QApplication::setOrganizationName(applicationName);
  34. QApplication::setOrganizationDomain("o3de.org");
  35. QCoreApplication::setApplicationName(applicationName);
  36. QCoreApplication::setApplicationVersion("1.0");
  37. // Use the LogComponent for non-dev logging log
  38. RegisterComponentDescriptor(AzFramework::LogComponent::CreateDescriptor());
  39. // set the log alias to .o3de/Logs instead of the default user/logs
  40. AZ::IO::FixedMaxPath path = AZ::Utils::GetO3deLogsDirectory();
  41. // DevWriteStorage is where the event log is written during development
  42. m_settingsRegistry->Set(AZ::SettingsRegistryMergeUtils::FilePathKey_DevWriteStorage, path.LexicallyNormal().Native());
  43. // Save event logs to .o3de/Logs/eventlogger/EventLogO3DE.azsl
  44. m_settingsRegistry->Set(AZ::SettingsRegistryMergeUtils::BuildTargetNameKey, applicationName);
  45. Start(AzFramework::Application::Descriptor());
  46. QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
  47. QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
  48. QCoreApplication::setAttribute(Qt::AA_DontCreateNativeWidgetSiblings);
  49. QLocale::setDefault(QLocale(QLocale::English, QLocale::UnitedStates));
  50. QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
  51. AzQtComponents::Utilities::HandleDpiAwareness(AzQtComponents::Utilities::SystemDpiAware);
  52. // Create the actual Qt Application - this needs to happen before using QMessageBox
  53. m_app.reset(new QApplication(*GetArgC(), *GetArgV()));
  54. if(!InitLog(applicationName))
  55. {
  56. AZ_Warning("ProjectManager", false, "Failed to init logging");
  57. }
  58. m_pythonBindings = AZStd::make_unique<PythonBindings>(GetEngineRoot());
  59. if (!m_pythonBindings || !m_pythonBindings->PythonStarted())
  60. {
  61. if (interactive)
  62. {
  63. QMessageBox::critical(nullptr, QObject::tr("Failed to start Python"),
  64. QObject::tr("This tool requires an O3DE engine with a Python runtime, "
  65. "but either Python is missing or mis-configured. Please rename "
  66. "your python/runtime folder to python/runtime_bak, then run "
  67. "python/get_python.bat to restore the Python runtime folder."));
  68. }
  69. return false;
  70. }
  71. const AZ::CommandLine* commandLine = GetCommandLine();
  72. AZ_Assert(commandLine, "Failed to get command line");
  73. ProjectManagerScreen startScreen = ProjectManagerScreen::Projects;
  74. if (size_t screenSwitchCount = commandLine->GetNumSwitchValues("screen"); screenSwitchCount > 0)
  75. {
  76. QString screenOption = commandLine->GetSwitchValue("screen", screenSwitchCount - 1).c_str();
  77. ProjectManagerScreen screen = ProjectUtils::GetProjectManagerScreen(screenOption);
  78. if (screen != ProjectManagerScreen::Invalid)
  79. {
  80. startScreen = screen;
  81. }
  82. }
  83. AZ::IO::FixedMaxPath projectPath;
  84. if (size_t projectSwitchCount = commandLine->GetNumSwitchValues("project-path"); projectSwitchCount > 0)
  85. {
  86. projectPath = commandLine->GetSwitchValue("project-path", projectSwitchCount - 1).c_str();
  87. }
  88. m_mainWindow.reset(new ProjectManagerWindow(nullptr, projectPath, startScreen));
  89. return true;
  90. }
  91. bool Application::InitLog(const char* logName)
  92. {
  93. if (!m_entity)
  94. {
  95. // override the log alias to the O3de Logs directory instead of the default project user/Logs folder
  96. AZ::IO::FixedMaxPath path = AZ::Utils::GetO3deLogsDirectory();
  97. AZ::IO::FileIOBase* fileIO = AZ::IO::FileIOBase::GetInstance();
  98. AZ_Assert(fileIO, "Failed to get FileIOBase instance");
  99. fileIO->SetAlias("@log@", path.LexicallyNormal().Native().c_str());
  100. // this entity exists because we need a home for LogComponent
  101. // and cannot use the system entity because we need to be able to call SetLogFileBaseName
  102. // so the log will be named O3DE.log
  103. m_entity = aznew AZ::Entity("Application Entity");
  104. if (m_entity)
  105. {
  106. AzFramework::LogComponent* logger = aznew AzFramework::LogComponent();
  107. AZ_Assert(logger, "Failed to create LogComponent");
  108. logger->SetLogFileBaseName(logName);
  109. m_entity->AddComponent(logger);
  110. m_entity->Init();
  111. m_entity->Activate();
  112. }
  113. }
  114. return m_entity != nullptr;
  115. }
  116. void Application::TearDown()
  117. {
  118. if (m_entity)
  119. {
  120. m_entity->Deactivate();
  121. delete m_entity;
  122. m_entity = nullptr;
  123. }
  124. m_pythonBindings.reset();
  125. m_mainWindow.reset();
  126. m_app.reset();
  127. }
  128. bool Application::Run()
  129. {
  130. // Set up the Style Manager
  131. AzQtComponents::StyleManager styleManager(qApp);
  132. styleManager.initialize(qApp, GetEngineRoot());
  133. // setup stylesheets and hot reloading
  134. AZ::IO::FixedMaxPath engineRoot(GetEngineRoot());
  135. QDir rootDir(engineRoot.c_str());
  136. const auto pathOnDisk = rootDir.absoluteFilePath("Code/Tools/ProjectManager/Resources");
  137. const auto qrcPath = QStringLiteral(":/ProjectManager/style");
  138. AzQtComponents::StyleManager::addSearchPaths("style", pathOnDisk, qrcPath, engineRoot);
  139. // set stylesheet after creating the main window or their styles won't get updated
  140. AzQtComponents::StyleManager::setStyleSheet(m_mainWindow.data(), QStringLiteral("style:ProjectManager.qss"));
  141. // the decoration wrapper is intended to remember window positioning and sizing
  142. auto wrapper = new AzQtComponents::WindowDecorationWrapper();
  143. wrapper->setGuest(m_mainWindow.data());
  144. wrapper->show();
  145. m_mainWindow->show();
  146. qApp->setQuitOnLastWindowClosed(true);
  147. // Run the application
  148. return qApp->exec();
  149. }
  150. }