ProjectButtonWidget.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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 <ProjectButtonWidget.h>
  9. #include <ProjectManagerDefs.h>
  10. #include <ProjectUtils.h>
  11. #include <ProjectManager_Traits_Platform.h>
  12. #include <AzQtComponents/Utilities/DesktopUtilities.h>
  13. #include <AzQtComponents/Components/Widgets/ElidingLabel.h>
  14. #include <AzCore/IO/SystemFile.h>
  15. #include <AzCore/IO/Path/Path.h>
  16. #include <QVBoxLayout>
  17. #include <QHBoxLayout>
  18. #include <QEvent>
  19. #include <QResizeEvent>
  20. #include <QLabel>
  21. #include <QPushButton>
  22. #include <QPixmap>
  23. #include <QMenu>
  24. #include <QSpacerItem>
  25. #include <QProgressBar>
  26. #include <QDir>
  27. #include <QFileInfo>
  28. #include <QDesktopServices>
  29. #include <QMessageBox>
  30. #include <QMouseEvent>
  31. #include <QMovie>
  32. namespace O3DE::ProjectManager
  33. {
  34. LabelButton::LabelButton(QWidget* parent)
  35. : QLabel(parent)
  36. {
  37. // Space for content excluding borders
  38. constexpr int contentSpaceWidth = ProjectPreviewImageWidth - 2;
  39. // The height of a third of the button split into 3 sections, top, middle and bottom
  40. constexpr int threeWaySplitHeight = (ProjectPreviewImageHeight - 2) / 3;
  41. setObjectName("labelButton");
  42. // Use GridLayout so widgets can be overlapped
  43. QGridLayout* overlayLayout = new QGridLayout();
  44. overlayLayout->setContentsMargins(0, 0, 0, 0);
  45. overlayLayout->setSpacing(0);
  46. setLayout(overlayLayout);
  47. m_darkenOverlay = new QLabel(this);
  48. m_darkenOverlay->setObjectName("labelButtonOverlay");
  49. m_darkenOverlay->setVisible(true);
  50. overlayLayout->addWidget(m_darkenOverlay, 0, 0);
  51. m_projectOverlayLayout = new QVBoxLayout();
  52. m_projectOverlayLayout->setContentsMargins(0, 0, 0, 0);
  53. m_projectOverlayLayout->setSpacing(0);
  54. m_projectOverlayLayout->setAlignment(Qt::AlignCenter);
  55. // Split the button into 3 fixed size sections so content alignment is easier to manage
  56. // If widgets in other sections are shown or hidden it will not offset alignment in other sections
  57. QWidget* topWidget = new QWidget();
  58. topWidget->setFixedSize(contentSpaceWidth, threeWaySplitHeight);
  59. QVBoxLayout* verticalMessageLayout = new QVBoxLayout();
  60. verticalMessageLayout->setContentsMargins(0, 0, 0, 0);
  61. verticalMessageLayout->setSpacing(4);
  62. verticalMessageLayout->setAlignment(Qt::AlignCenter);
  63. topWidget->setLayout(verticalMessageLayout);
  64. verticalMessageLayout->addSpacing(10);
  65. QHBoxLayout* horizontalWarningMessageLayout = new QHBoxLayout();
  66. horizontalWarningMessageLayout->setContentsMargins(0, 0, 0, 0);
  67. horizontalWarningMessageLayout->setSpacing(0);
  68. horizontalWarningMessageLayout->setAlignment(Qt::AlignRight | Qt::AlignTop);
  69. m_warningSpacer = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Fixed);
  70. horizontalWarningMessageLayout->addSpacerItem(m_warningSpacer);
  71. horizontalWarningMessageLayout->addSpacing(10);
  72. m_warningIcon = new QLabel(this);
  73. m_warningIcon->setObjectName("projectWarningIconOverlay");
  74. m_warningIcon->setPixmap(QIcon(":/Warning.svg").pixmap(32, 32));
  75. m_warningIcon->setAlignment(Qt::AlignCenter);
  76. m_warningIcon->setVisible(false);
  77. horizontalWarningMessageLayout->addWidget(m_warningIcon);
  78. horizontalWarningMessageLayout->addSpacing(15);
  79. verticalMessageLayout->addLayout(horizontalWarningMessageLayout);
  80. m_messageLabel = new QLabel("", this);
  81. m_messageLabel->setObjectName("projectMessageOverlay");
  82. m_messageLabel->setAlignment(Qt::AlignCenter);
  83. m_messageLabel->setVisible(true);
  84. verticalMessageLayout->addWidget(m_messageLabel);
  85. m_subMessageLabel = new QLabel("", this);
  86. m_subMessageLabel->setObjectName("projectSubMessageOverlay");
  87. m_subMessageLabel->setAlignment(Qt::AlignCenter);
  88. m_subMessageLabel->setVisible(true);
  89. verticalMessageLayout->addWidget(m_subMessageLabel);
  90. verticalMessageLayout->addStretch();
  91. m_projectOverlayLayout->addWidget(topWidget);
  92. QWidget* middleWidget = new QWidget();
  93. middleWidget->setFixedSize(contentSpaceWidth, threeWaySplitHeight);
  94. QVBoxLayout* verticalCenterLayout = new QVBoxLayout();
  95. verticalCenterLayout->setContentsMargins(0, 0, 0, 0);
  96. verticalCenterLayout->setSpacing(0);
  97. verticalCenterLayout->setAlignment(Qt::AlignCenter);
  98. middleWidget->setLayout(verticalCenterLayout);
  99. m_buildingAnimation = new QLabel("", this);
  100. m_buildingAnimation->setObjectName("buildingAnimationOverlay");
  101. m_buildingAnimation->setAlignment(Qt::AlignCenter);
  102. m_buildingAnimation->setVisible(false);
  103. m_buildingAnimation->setMovie(new QMovie(":/SpinningGears.webp"));
  104. m_buildingAnimation->movie()->start();
  105. verticalCenterLayout->addWidget(m_buildingAnimation);
  106. m_projectOverlayLayout->addWidget(middleWidget);
  107. QWidget* bottomWidget = new QWidget();
  108. bottomWidget->setFixedSize(contentSpaceWidth, threeWaySplitHeight);
  109. QVBoxLayout* verticalButtonLayout = new QVBoxLayout();
  110. verticalButtonLayout->setContentsMargins(0, 0, 0, 0);
  111. verticalButtonLayout->setSpacing(5);
  112. verticalButtonLayout->setAlignment(Qt::AlignHCenter | Qt::AlignBottom);
  113. bottomWidget->setLayout(verticalButtonLayout);
  114. m_openEditorButton = new QPushButton(tr("Open Editor"), this);
  115. m_openEditorButton->setObjectName("openEditorButton");
  116. m_openEditorButton->setDefault(true);
  117. m_openEditorButton->setVisible(false);
  118. verticalButtonLayout->addWidget(m_openEditorButton);
  119. m_actionButton = new QPushButton(tr("Project Action"), this);
  120. m_actionButton->setObjectName("projectActionButton");
  121. m_actionButton->setVisible(false);
  122. verticalButtonLayout->addWidget(m_actionButton);
  123. // This button has seperate styling with a red button instead of a blue button as for m_actionButton
  124. // Seperate buttons are used to avoid stutter from reloading style after changing object name
  125. m_actionCancelButton = new QPushButton(tr("Cancel Project Action"), this);
  126. m_actionCancelButton->setObjectName("projectActionCancelButton");
  127. m_actionCancelButton->setVisible(false);
  128. verticalButtonLayout->addWidget(m_actionCancelButton);
  129. m_showLogsButton = new QPushButton(tr("Show logs"), this);
  130. m_showLogsButton->setObjectName("projectShowLogsButton");
  131. m_showLogsButton->setVisible(false);
  132. verticalButtonLayout->addWidget(m_showLogsButton);
  133. verticalButtonLayout->addSpacing(20);
  134. m_projectOverlayLayout->addWidget(bottomWidget);
  135. overlayLayout->addLayout(m_projectOverlayLayout, 0, 0);
  136. }
  137. void LabelButton::mousePressEvent(QMouseEvent* event)
  138. {
  139. emit triggered(event);
  140. }
  141. QLabel* LabelButton::GetMessageLabel()
  142. {
  143. return m_messageLabel;
  144. }
  145. QLabel* LabelButton::GetSubMessageLabel()
  146. {
  147. return m_subMessageLabel;
  148. }
  149. QLabel* LabelButton::GetWarningIcon()
  150. {
  151. return m_warningIcon;
  152. }
  153. QSpacerItem* LabelButton::GetWarningSpacer()
  154. {
  155. return m_warningSpacer;
  156. }
  157. QLabel* LabelButton::GetBuildingAnimationLabel()
  158. {
  159. return m_buildingAnimation;
  160. }
  161. QPushButton* LabelButton::GetOpenEditorButton()
  162. {
  163. return m_openEditorButton;
  164. }
  165. QPushButton* LabelButton::GetActionButton()
  166. {
  167. return m_actionButton;
  168. }
  169. QPushButton* LabelButton::GetActionCancelButton()
  170. {
  171. return m_actionCancelButton;
  172. }
  173. QPushButton* LabelButton::GetShowLogsButton()
  174. {
  175. return m_showLogsButton;
  176. }
  177. QLabel* LabelButton::GetDarkenOverlay()
  178. {
  179. return m_darkenOverlay;
  180. }
  181. ProjectButton::ProjectButton(const ProjectInfo& projectInfo, QWidget* parent)
  182. : QFrame(parent)
  183. , m_projectInfo(projectInfo)
  184. , m_isProjectBuilding(false)
  185. {
  186. setObjectName("projectButton");
  187. QVBoxLayout* vLayout = new QVBoxLayout();
  188. vLayout->setSpacing(0);
  189. vLayout->setContentsMargins(0, 0, 0, 0);
  190. setLayout(vLayout);
  191. m_projectImageLabel = new LabelButton(this);
  192. m_projectImageLabel->setFixedSize(ProjectPreviewImageWidth, ProjectPreviewImageHeight);
  193. m_projectImageLabel->setAlignment(Qt::AlignCenter);
  194. vLayout->addWidget(m_projectImageLabel);
  195. QString projectPreviewPath = QDir(m_projectInfo.m_path).filePath(m_projectInfo.m_iconPath);
  196. QFileInfo doesPreviewExist(projectPreviewPath);
  197. if (!doesPreviewExist.exists() || !doesPreviewExist.isFile())
  198. {
  199. projectPreviewPath = ":/DefaultProjectImage.png";
  200. }
  201. m_projectImageLabel->setPixmap(QPixmap(projectPreviewPath).scaled(m_projectImageLabel->size(), Qt::KeepAspectRatioByExpanding));
  202. QFrame* projectFooter = new QFrame(this);
  203. QHBoxLayout* hLayout = new QHBoxLayout();
  204. hLayout->setContentsMargins(0, 0, 0, 0);
  205. projectFooter->setLayout(hLayout);
  206. {
  207. AzQtComponents::ElidingLabel* projectNameLabel = new AzQtComponents::ElidingLabel(m_projectInfo.GetProjectDisplayName(), this);
  208. projectNameLabel->setToolTip(m_projectInfo.m_path);
  209. hLayout->addWidget(projectNameLabel);
  210. m_projectMenuButton = new QPushButton(this);
  211. m_projectMenuButton->setObjectName("projectMenuButton");
  212. m_projectMenuButton->setMenu(CreateProjectMenu());
  213. hLayout->addWidget(m_projectMenuButton);
  214. }
  215. vLayout->addWidget(projectFooter);
  216. connect(m_projectImageLabel->GetOpenEditorButton(), &QPushButton::clicked, [this]() {
  217. emit OpenProject(m_projectInfo.m_path);
  218. });
  219. connect(m_projectImageLabel, &LabelButton::triggered, [this](QMouseEvent* event) {
  220. if (!m_isProjectBuilding && event->button() == Qt::RightButton)
  221. {
  222. m_projectMenuButton->menu()->move(event->globalPos());
  223. m_projectMenuButton->menu()->show();
  224. }
  225. });
  226. connect(m_projectImageLabel->GetShowLogsButton(), &QPushButton::pressed, this, &ProjectButton::ShowLogs);
  227. SetState(ProjectButtonState::ReadyToLaunch);
  228. }
  229. QMenu* ProjectButton::CreateProjectMenu()
  230. {
  231. QMenu* menu = new QMenu(this);
  232. menu->addAction(tr("Edit Project Settings..."), this, [this]() { emit EditProject(m_projectInfo.m_path); });
  233. menu->addAction(tr("Configure Gems..."), this, [this]() { emit EditProjectGems(m_projectInfo.m_path); });
  234. menu->addAction(tr("Build"), this, [this]() { emit BuildProject(m_projectInfo); });
  235. menu->addAction(tr("Open CMake GUI..."), this, [this]() { emit OpenCMakeGUI(m_projectInfo); });
  236. menu->addSeparator();
  237. menu->addAction(tr("Open Project folder..."), this, [this]()
  238. {
  239. AzQtComponents::ShowFileOnDesktop(m_projectInfo.m_path);
  240. });
  241. #if AZ_TRAIT_PROJECT_MANAGER_CREATE_DESKTOP_SHORTCUT
  242. menu->addAction(tr("Create Editor desktop shortcut..."), this, [this]()
  243. {
  244. AZ::IO::FixedMaxPath editorExecutablePath = ProjectUtils::GetEditorExecutablePath(m_projectInfo.m_path.toUtf8().constData());
  245. const QString shortcutName = QString("%1 Editor").arg(m_projectInfo.m_displayName);
  246. const QString arg = QString("--regset=\"/Amazon/AzCore/Bootstrap/project_path=%1\"").arg(m_projectInfo.m_path);
  247. auto result = ProjectUtils::CreateDesktopShortcut(shortcutName, editorExecutablePath.c_str(), { arg });
  248. if(result.IsSuccess())
  249. {
  250. QMessageBox::information(this, tr("Desktop Shortcut Created"), result.GetValue());
  251. }
  252. else
  253. {
  254. QMessageBox::critical(this, tr("Failed to create shortcut"), result.GetError());
  255. }
  256. });
  257. #endif // AZ_TRAIT_PROJECT_MANAGER_CREATE_DESKTOP_SHORTCUT
  258. menu->addSeparator();
  259. menu->addAction(tr("Duplicate"), this, [this]() { emit CopyProject(m_projectInfo); });
  260. menu->addSeparator();
  261. menu->addAction(tr("Remove from O3DE"), this, [this]() { emit RemoveProject(m_projectInfo.m_path); });
  262. menu->addAction(tr("Delete this Project"), this, [this]() { emit DeleteProject(m_projectInfo.m_path); });
  263. return menu;
  264. }
  265. const ProjectInfo& ProjectButton::GetProjectInfo() const
  266. {
  267. return m_projectInfo;
  268. }
  269. void ProjectButton::ShowLogs()
  270. {
  271. QDesktopServices::openUrl(m_projectInfo.m_logUrl);
  272. }
  273. void ProjectButton::SetState(enum ProjectButtonState state)
  274. {
  275. m_currentState = state;
  276. switch (state)
  277. {
  278. default:
  279. case ProjectButtonState::ReadyToLaunch:
  280. ShowReadyState();
  281. break;
  282. case ProjectButtonState::Launching:
  283. ShowLaunchingState();
  284. break;
  285. case ProjectButtonState::NeedsToBuild:
  286. ShowBuildRequiredState();
  287. break;
  288. case ProjectButtonState::Building:
  289. ShowBuildingState();
  290. break;
  291. case ProjectButtonState::BuildFailed:
  292. ShowBuildFailedState();
  293. break;
  294. }
  295. }
  296. void ProjectButton::ShowReadyState()
  297. {
  298. HideContextualLabelButtonWidgets();
  299. if (m_actionButtonConnection)
  300. {
  301. disconnect(m_actionButtonConnection);
  302. }
  303. m_projectMenuButton->setVisible(true);
  304. SetLaunchingEnabled(true);
  305. SetProjectBuilding(false);
  306. }
  307. void ProjectButton::ShowLaunchingState()
  308. {
  309. HideContextualLabelButtonWidgets();
  310. SetLaunchingEnabled(false);
  311. SetProjectBuilding(false);
  312. // Hide button in-case it is still showing
  313. m_projectImageLabel->GetOpenEditorButton()->hide();
  314. ShowMessage(tr("Opening Editor..."));
  315. }
  316. void ProjectButton::ShowBuildRequiredState()
  317. {
  318. ShowBuildButton();
  319. SetProjectBuilding(false);
  320. ShowWarning(tr("Project build required."));
  321. }
  322. void ProjectButton::ShowBuildingState()
  323. {
  324. m_projectImageLabel->GetShowLogsButton()->show();
  325. // Setting project to building also disables launching
  326. SetProjectBuilding(true);
  327. ShowMessage(tr("Building Project..."));
  328. }
  329. void ProjectButton::ShowBuildFailedState()
  330. {
  331. ShowBuildButton();
  332. SetProjectBuilding(false);
  333. // Show, show logs button if avaliable
  334. m_projectImageLabel->GetShowLogsButton()->setVisible(!m_projectInfo.m_logUrl.isEmpty());
  335. ShowWarning(tr("Failed to build"));
  336. }
  337. void ProjectButton::SetProjectButtonAction(const QString& text, AZStd::function<void()> lambda)
  338. {
  339. QPushButton* projectActionButton;
  340. QPushButton* projectOtherActionButton;
  341. if (text.contains("Cancel", Qt::CaseInsensitive))
  342. {
  343. // Use red button is action involves cancelling
  344. projectActionButton = m_projectImageLabel->GetActionCancelButton();
  345. projectOtherActionButton = m_projectImageLabel->GetActionButton();
  346. }
  347. else
  348. {
  349. projectActionButton = m_projectImageLabel->GetActionButton();
  350. projectOtherActionButton = m_projectImageLabel->GetActionCancelButton();
  351. }
  352. if (m_actionButtonConnection)
  353. {
  354. disconnect(m_actionButtonConnection);
  355. }
  356. projectActionButton->setVisible(true);
  357. projectOtherActionButton->setVisible(false);
  358. projectActionButton->setText(text);
  359. projectActionButton->setMenu(nullptr);
  360. m_actionButtonConnection = connect(projectActionButton, &QPushButton::clicked, lambda);
  361. }
  362. void ProjectButton::SetBuildLogsLink(const QUrl& logUrl)
  363. {
  364. m_projectInfo.m_logUrl = logUrl;
  365. }
  366. void ProjectButton::SetContextualText(const QString& text)
  367. {
  368. if (m_currentState == ProjectButtonState::Building)
  369. {
  370. // Don't update for empty build progress messages
  371. if (!text.isEmpty())
  372. {
  373. // Show info about what's currently building
  374. ShowMessage({}, text);
  375. }
  376. }
  377. else
  378. {
  379. ShowMessage(text);
  380. }
  381. }
  382. void ProjectButton::ShowBuildButton()
  383. {
  384. QPushButton* projectActionButton = m_projectImageLabel->GetActionButton();
  385. projectActionButton->setVisible(true);
  386. projectActionButton->setText(tr("Build Project"));
  387. disconnect(m_actionButtonConnection);
  388. QMenu* menu = new QMenu(this);
  389. QAction* autoBuildAction = menu->addAction(tr("Build Now"));
  390. connect( autoBuildAction, &QAction::triggered, this, [this](){ emit BuildProject(m_projectInfo); });
  391. QAction* openCMakeAction = menu->addAction(tr("Open CMake GUI..."));
  392. connect( openCMakeAction, &QAction::triggered, this, [this](){ emit OpenCMakeGUI(m_projectInfo); });
  393. projectActionButton->setMenu(menu);
  394. }
  395. // Only setting message without setting submessage will hide submessage
  396. void ProjectButton::ShowMessage(const QString& message, const QString& submessage)
  397. {
  398. bool showMessage = !message.isEmpty();
  399. bool showSubmessage = !submessage.isEmpty();
  400. QLabel* messageLabel = m_projectImageLabel->GetMessageLabel();
  401. QLabel* submessageLabel = m_projectImageLabel->GetSubMessageLabel();
  402. if (showMessage || showSubmessage)
  403. {
  404. // Hide any warning text, we cannot show the warning and a message at the same time
  405. ShowWarning();
  406. }
  407. // Keep main message if only submessage is set
  408. if (showMessage || showMessage == showSubmessage)
  409. {
  410. messageLabel->setText(message);
  411. }
  412. submessageLabel->setText(submessage);
  413. // Darken background if there is a message to make it easier to read
  414. m_projectImageLabel->GetDarkenOverlay()->setVisible(showMessage || showSubmessage);
  415. messageLabel->setVisible(showMessage || showSubmessage);
  416. submessageLabel->setVisible(showSubmessage);
  417. }
  418. void ProjectButton::ShowWarning(const QString& warning)
  419. {
  420. bool show = !warning.isEmpty();
  421. QLabel* warningIcon = m_projectImageLabel->GetWarningIcon();
  422. if (show)
  423. {
  424. // Hide any message text, we cannot show the warning and a message at the same time
  425. ShowMessage();
  426. m_projectImageLabel->GetWarningSpacer()->changeSize(0, 0, QSizePolicy::Expanding, QSizePolicy::Fixed);
  427. }
  428. else
  429. {
  430. m_projectImageLabel->GetWarningSpacer()->changeSize(0, 0, QSizePolicy::Fixed, QSizePolicy::Fixed);
  431. }
  432. warningIcon->setToolTip(warning);
  433. warningIcon->setVisible(show);
  434. }
  435. void ProjectButton::SetLaunchingEnabled(bool enabled)
  436. {
  437. m_canLaunch = enabled;
  438. }
  439. void ProjectButton::SetProjectBuilding(bool isBuilding)
  440. {
  441. m_isProjectBuilding = isBuilding;
  442. QLabel* buildingAnimation = m_projectImageLabel->GetBuildingAnimationLabel();
  443. if (isBuilding)
  444. {
  445. SetLaunchingEnabled(false);
  446. }
  447. buildingAnimation->movie()->setPaused(!isBuilding);
  448. buildingAnimation->setVisible(isBuilding);
  449. m_projectMenuButton->setVisible(!isBuilding);
  450. }
  451. void ProjectButton::HideContextualLabelButtonWidgets()
  452. {
  453. ShowMessage();
  454. ShowWarning();
  455. m_projectImageLabel->GetActionButton()->hide();
  456. m_projectImageLabel->GetActionCancelButton()->hide();
  457. m_projectImageLabel->GetShowLogsButton()->hide();
  458. }
  459. void ProjectButton::enterEvent(QEvent* /*event*/)
  460. {
  461. if (m_canLaunch)
  462. {
  463. m_projectImageLabel->GetOpenEditorButton()->setVisible(true);
  464. }
  465. }
  466. void ProjectButton::leaveEvent(QEvent* /*event*/)
  467. {
  468. m_projectImageLabel->GetOpenEditorButton()->setVisible(false);
  469. }
  470. LabelButton* ProjectButton::GetLabelButton()
  471. {
  472. return m_projectImageLabel;
  473. }
  474. } // namespace O3DE::ProjectManager