WelcomeScreenDialog.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 "EditorDefs.h"
  9. #include "WelcomeScreenDialog.h"
  10. // Qt
  11. #include <QTableWidget>
  12. #include <QTableWidgetItem>
  13. #include <QToolTip>
  14. #include <QMenu>
  15. #include <QDesktopServices>
  16. #include <QFileDialog>
  17. #include <QMessageBox>
  18. #include <QScreen>
  19. #include <QDesktopWidget>
  20. #include <QTimer>
  21. #include <QDateTime>
  22. #include <AzCore/Utils/Utils.h>
  23. // AzToolsFramework
  24. #include <AzToolsFramework/UI/UICore/WidgetHelpers.h>
  25. // AzQtComponents
  26. #include <AzQtComponents/Components/Widgets/CheckBox.h>
  27. #include <AzQtComponents/Components/WindowDecorationWrapper.h>
  28. #include <AzQtComponents/Utilities/PixmapScaleUtilities.h>
  29. // Editor
  30. #include "Settings.h"
  31. #include "MainWindow.h"
  32. #include "CryEdit.h"
  33. #include "LevelFileDialog.h"
  34. #include <WelcomeScreen/ui_WelcomeScreenDialog.h>
  35. using namespace AzQtComponents;
  36. #define WMSEVENTNAME "WMSEvent"
  37. #define WMSEVENTOPERATION "operation"
  38. static int GetSmallestScreenHeight()
  39. {
  40. int smallestHeight = -1;
  41. for (QScreen* screen : QApplication::screens())
  42. {
  43. int screenHeight = screen->availableGeometry().height();
  44. if ((smallestHeight < 0) || (smallestHeight > screenHeight))
  45. {
  46. smallestHeight = screenHeight;
  47. }
  48. }
  49. return smallestHeight;
  50. }
  51. WelcomeScreenDialog::WelcomeScreenDialog(QWidget* pParent)
  52. : QDialog(new WindowDecorationWrapper(WindowDecorationWrapper::OptionAutoAttach | WindowDecorationWrapper::OptionAutoTitleBarButtons, pParent), Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint | Qt::WindowTitleHint)
  53. , ui(new Ui::WelcomeScreenDialog)
  54. , m_pRecentList(nullptr)
  55. {
  56. ui->setupUi(this);
  57. // Set the project preview image
  58. QString projectPreviewPath = QDir(AZ::Utils::GetProjectPath().c_str()).filePath("preview.png");
  59. QFileInfo projectPreviewPathInfo(projectPreviewPath);
  60. if (!projectPreviewPathInfo.exists() || !projectPreviewPathInfo.isFile())
  61. {
  62. projectPreviewPath = ":/WelcomeScreenDialog/DefaultProjectImage.png";
  63. }
  64. ui->activeProjectIcon->setPixmap(
  65. AzQtComponents::ScalePixmapForScreenDpi(
  66. QPixmap(projectPreviewPath),
  67. screen(),
  68. ui->activeProjectIcon->size(),
  69. Qt::KeepAspectRatioByExpanding,
  70. Qt::SmoothTransformation
  71. )
  72. );
  73. ui->recentLevelTable->setColumnCount(3);
  74. ui->recentLevelTable->setMouseTracking(true);
  75. ui->recentLevelTable->setContextMenuPolicy(Qt::CustomContextMenu);
  76. ui->recentLevelTable->horizontalHeader()->hide();
  77. ui->recentLevelTable->verticalHeader()->hide();
  78. ui->recentLevelTable->setSelectionBehavior(QAbstractItemView::SelectRows);
  79. ui->recentLevelTable->setSelectionMode(QAbstractItemView::SingleSelection);
  80. ui->recentLevelTable->setIconSize(QSize(20, 20));
  81. installEventFilter(this);
  82. auto projectDisplayName = AZ::Utils::GetProjectDisplayName();
  83. ui->currentProjectName->setText(projectDisplayName.c_str());
  84. ui->newLevelButton->setDefault(true);
  85. // Hide these buttons until the new functionality is added
  86. ui->gridButton->hide();
  87. ui->objectListButton->hide();
  88. ui->switchProjectButton->hide();
  89. connect(ui->recentLevelTable, &QWidget::customContextMenuRequested, this, &WelcomeScreenDialog::OnShowContextMenu);
  90. connect(ui->recentLevelTable, &QTableWidget::entered, this, &WelcomeScreenDialog::OnShowToolTip);
  91. connect(ui->recentLevelTable, &QTableWidget::clicked, this, &WelcomeScreenDialog::OnRecentLevelTableItemClicked);
  92. connect(ui->newLevelButton, &QPushButton::clicked, this, &WelcomeScreenDialog::OnNewLevelBtnClicked);
  93. connect(ui->levelFileLabel, &QLabel::linkActivated, this, &WelcomeScreenDialog::OnNewLevelLabelClicked);
  94. connect(ui->openLevelButton, &QPushButton::clicked, this, &WelcomeScreenDialog::OnOpenLevelBtnClicked);
  95. // Adjust the height, if need be
  96. // Do it in the constructor so that the WindowDecoratorWrapper handles it correctly
  97. int smallestHeight = GetSmallestScreenHeight();
  98. if (smallestHeight < geometry().height())
  99. {
  100. const int SOME_PADDING_IN_PIXELS = 90;
  101. int difference = geometry().height() - (smallestHeight - SOME_PADDING_IN_PIXELS);
  102. QRect newGeometry = geometry().adjusted(0, difference / 2, 0, -difference / 2);
  103. setMinimumSize(minimumSize().width(), newGeometry.height());
  104. resize(newGeometry.size());
  105. }
  106. m_levelExtension = EditorUtils::LevelFile::GetDefaultFileExtension();
  107. }
  108. WelcomeScreenDialog::~WelcomeScreenDialog()
  109. {
  110. delete ui;
  111. }
  112. void WelcomeScreenDialog::done(int result)
  113. {
  114. QDialog::done(result);
  115. }
  116. const QString& WelcomeScreenDialog::GetLevelPath()
  117. {
  118. return m_levelPath;
  119. }
  120. bool WelcomeScreenDialog::eventFilter(QObject *watched, QEvent *event)
  121. {
  122. if (event->type() == QEvent::Show)
  123. {
  124. ui->recentLevelTable->horizontalHeader()->resizeSection(0, ui->nameLabel->width());
  125. ui->recentLevelTable->horizontalHeader()->resizeSection(1, ui->modifiedLabel->width());
  126. ui->recentLevelTable->horizontalHeader()->resizeSection(2, ui->typeLabel->width());
  127. }
  128. return QDialog::eventFilter(watched, event);
  129. }
  130. bool WelcomeScreenDialog::IsValidLevelName(const QString& path)
  131. {
  132. QStringList pathParts = Path::SplitIntoSegments(path);
  133. QString levelName = pathParts.at(pathParts.size() - 1);
  134. if (levelName.endsWith(".prefab", Qt::CaseInsensitive))
  135. {
  136. // If the level is a prefab, check the container name.
  137. levelName = pathParts.at(pathParts.size() - 2);
  138. }
  139. QRegExpValidator validator(QRegExp("^[a-zA-Z0-9_\\-./]*$"));
  140. int pos = 0;
  141. return validator.validate(levelName, pos);
  142. }
  143. void WelcomeScreenDialog::SetRecentFileList(RecentFileList* pList)
  144. {
  145. if (!pList)
  146. {
  147. return;
  148. }
  149. m_pRecentList = pList;
  150. auto projectPath = AZ::Utils::GetProjectPath();
  151. QString gamePath{projectPath.c_str()};
  152. Path::ConvertSlashToBackSlash(gamePath);
  153. gamePath = Path::ToUnixPath(gamePath.toLower());
  154. gamePath = Path::AddSlash(gamePath);
  155. QString sCurDir = (Path::GetEditingGameDataFolder() + QDir::separator().toLatin1()).c_str();
  156. int nCurDir = sCurDir.length();
  157. int recentListSize = pList->GetSize();
  158. int currentRow = 0;
  159. ui->recentLevelTable->setRowCount(recentListSize);
  160. for (int i = 0; i < recentListSize; ++i)
  161. {
  162. const QString& recentFile = pList->m_arrNames[i];
  163. if (recentFile.endsWith(m_levelExtension) && IsValidLevelName(recentFile))
  164. {
  165. if (CFileUtil::Exists(recentFile, false))
  166. {
  167. QString sCurEntryDir = recentFile.left(nCurDir);
  168. if (sCurEntryDir.compare(sCurDir, Qt::CaseInsensitive) == 0)
  169. {
  170. QString fullPath = recentFile;
  171. const QString name = Path::GetFile(fullPath);
  172. Path::ConvertSlashToBackSlash(fullPath);
  173. fullPath = Path::ToUnixPath(fullPath.toLower());
  174. fullPath = Path::AddSlash(fullPath);
  175. if (fullPath.contains(gamePath))
  176. {
  177. if (gSettings.prefabSystem)
  178. {
  179. QIcon icon;
  180. icon.addFile(QString::fromUtf8(":/Level/level.svg"), QSize(), QIcon::Normal, QIcon::Off);
  181. ui->recentLevelTable->setItem(currentRow, 0, new QTableWidgetItem(icon, name));
  182. }
  183. else
  184. {
  185. ui->recentLevelTable->setItem(currentRow, 0, new QTableWidgetItem(name));
  186. }
  187. QFileInfo file(recentFile);
  188. QDateTime dateTime = file.lastModified();
  189. QString date = QLocale::system().toString(dateTime.date(), QLocale::ShortFormat) + " " +
  190. QLocale::system().toString(dateTime.time(), QLocale::LongFormat);
  191. ui->recentLevelTable->setItem(currentRow, 1, new QTableWidgetItem(date));
  192. ui->recentLevelTable->setItem(currentRow++, 2, new QTableWidgetItem(tr("Level")));
  193. m_levels.push_back(std::make_pair(name, recentFile));
  194. }
  195. }
  196. }
  197. }
  198. }
  199. ui->recentLevelTable->setRowCount(currentRow);
  200. ui->recentLevelTable->setMinimumHeight(currentRow * ui->recentLevelTable->verticalHeader()->defaultSectionSize());
  201. ui->recentLevelTable->setMaximumHeight(currentRow * ui->recentLevelTable->verticalHeader()->defaultSectionSize());
  202. ui->levelFileLabel->setVisible(currentRow ? false : true);
  203. ui->recentLevelTable->setCurrentIndex(QModelIndex());
  204. }
  205. void WelcomeScreenDialog::RemoveLevelEntry(int index)
  206. {
  207. TNamePathPair levelPath = m_levels[index];
  208. ui->recentLevelTable->removeRow(index);
  209. m_levels.erase(m_levels.begin() + index);
  210. if (!m_pRecentList)
  211. {
  212. return;
  213. }
  214. for (int i = 0; i < m_pRecentList->GetSize(); ++i)
  215. {
  216. QString fullPath = m_pRecentList->m_arrNames[i];
  217. QString fullPath2 = levelPath.second;
  218. // path from recent list
  219. Path::ConvertSlashToBackSlash(fullPath);
  220. fullPath = Path::ToUnixPath(fullPath.toLower());
  221. fullPath = Path::AddPathSlash(fullPath);
  222. // path from our dashboard list
  223. Path::ConvertSlashToBackSlash(fullPath2);
  224. fullPath2 = Path::ToUnixPath(fullPath2.toLower());
  225. fullPath2 = Path::AddPathSlash(fullPath2);
  226. if (fullPath == fullPath2)
  227. {
  228. m_pRecentList->Remove(index);
  229. break;
  230. }
  231. }
  232. m_pRecentList->WriteList();
  233. }
  234. void WelcomeScreenDialog::OnShowToolTip(const QModelIndex& index)
  235. {
  236. const QString& fullPath = m_levels[index.row()].second;
  237. QToolTip::showText(QCursor::pos(), QString("Open level: %1").arg(fullPath));
  238. }
  239. void WelcomeScreenDialog::OnShowContextMenu(const QPoint& pos)
  240. {
  241. QModelIndex index = ui->recentLevelTable->indexAt(pos);
  242. if (index.isValid())
  243. {
  244. QString level = ui->recentLevelTable->itemAt(pos)->text();
  245. QPoint globalPos = ui->recentLevelTable->viewport()->mapToGlobal(pos);
  246. QMenu contextMenu;
  247. contextMenu.addAction(QString("Remove " + level + " from recent list"));
  248. QAction* selectedItem = contextMenu.exec(globalPos);
  249. if (selectedItem)
  250. {
  251. RemoveLevelEntry(index.row());
  252. }
  253. }
  254. }
  255. void WelcomeScreenDialog::OnNewLevelBtnClicked([[maybe_unused]] bool checked)
  256. {
  257. m_levelPath = "new";
  258. accept();
  259. }
  260. void WelcomeScreenDialog::OnNewLevelLabelClicked(const QString& path)
  261. {
  262. if (path == "Create")
  263. {
  264. OnNewLevelBtnClicked(true);
  265. }
  266. else
  267. {
  268. OnOpenLevelBtnClicked(true);
  269. }
  270. }
  271. void WelcomeScreenDialog::OnOpenLevelBtnClicked([[maybe_unused]] bool checked)
  272. {
  273. CLevelFileDialog dlg(true, this);
  274. if (dlg.exec() == QDialog::Accepted)
  275. {
  276. m_levelPath = dlg.GetFileName();
  277. accept();
  278. }
  279. }
  280. void WelcomeScreenDialog::OnRecentLevelTableItemClicked(const QModelIndex& modelIndex)
  281. {
  282. int index = modelIndex.row();
  283. if (index >= 0 && index < m_levels.size())
  284. {
  285. m_levelPath = m_levels[index].second;
  286. accept();
  287. }
  288. }
  289. void WelcomeScreenDialog::OnCloseBtnClicked([[maybe_unused]] bool checked)
  290. {
  291. accept();
  292. }
  293. void WelcomeScreenDialog::previewAreaScrolled()
  294. {
  295. //this should only be reported once per session
  296. if (m_messageScrollReported)
  297. {
  298. return;
  299. }
  300. m_messageScrollReported = true;
  301. }
  302. #include <WelcomeScreen/moc_WelcomeScreenDialog.cpp>