RecentFiles.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 "RecentFiles.h"
  9. #include "MysticQtManager.h"
  10. #include <AzCore/IO/Path/Path.h>
  11. #include <AzFramework/API/ApplicationAPI.h>
  12. #include <AzFramework/StringFunc/StringFunc.h>
  13. #include <AzToolsFramework/API/EditorAssetSystemAPI.h>
  14. #include <EMotionFX/Source/EMotionFXManager.h>
  15. #include <QtCore/QFileInfo>
  16. #include <QtCore/QSettings>
  17. #include <QtGui/QHelpEvent>
  18. #include <QtWidgets/QToolTip>
  19. namespace MysticQt
  20. {
  21. class ToolTipMenu
  22. : public QMenu
  23. {
  24. public:
  25. ToolTipMenu(const QString title, QWidget* parent)
  26. : QMenu(title, parent)
  27. {
  28. m_parent = parent;
  29. }
  30. bool event(QEvent* e) override
  31. {
  32. bool result = QMenu::event(e);
  33. const QHelpEvent* helpEvent = static_cast<QHelpEvent*>(e);
  34. if (helpEvent->type() == QEvent::ToolTip)
  35. {
  36. QAction* action = activeAction();
  37. if (action)
  38. {
  39. QToolTip::showText(helpEvent->globalPos(), action->toolTip(), m_parent);
  40. }
  41. }
  42. else
  43. {
  44. QToolTip::hideText();
  45. }
  46. return result;
  47. }
  48. private:
  49. QWidget* m_parent;
  50. };
  51. RecentFiles::RecentFiles()
  52. : QObject()
  53. {
  54. m_maxNumRecentFiles = 10;
  55. }
  56. void RecentFiles::Init(QMenu* parentMenu, size_t numRecentFiles, const char* subMenuName, const char* configStringName)
  57. {
  58. m_configStringName = configStringName;
  59. m_maxNumRecentFiles = numRecentFiles;
  60. Load();
  61. m_resetRecentFilesAction = nullptr;
  62. m_recentFilesMenu = new ToolTipMenu(subMenuName, parentMenu);
  63. m_recentFilesMenu->setObjectName("EMFX.MainWindow.RecentFilesMenu");
  64. parentMenu->addMenu(m_recentFilesMenu);
  65. SetMaxRecentFiles(numRecentFiles);
  66. UpdateMenu();
  67. }
  68. void RecentFiles::OnClearRecentFiles()
  69. {
  70. m_recentFiles.clear();
  71. Save();
  72. UpdateMenu();
  73. }
  74. void RecentFiles::Save()
  75. {
  76. QSettings settings(this);
  77. settings.beginGroup("EMotionFX");
  78. settings.setValue(m_configStringName, m_recentFiles);
  79. settings.endGroup();
  80. }
  81. void RecentFiles::Load()
  82. {
  83. QSettings settings(this);
  84. settings.beginGroup("EMotionFX");
  85. m_recentFiles = settings.value(m_configStringName).toStringList();
  86. settings.endGroup();
  87. // Remove deleted or non-existing files from the recent files.
  88. QString filename;
  89. for (int i = 0; i < m_recentFiles.size(); )
  90. {
  91. if (!QFile::exists(m_recentFiles[i]))
  92. {
  93. m_recentFiles.removeAt(i);
  94. }
  95. else
  96. {
  97. i++;
  98. }
  99. }
  100. // Remove legacy duplicates or duplicated filenames with inconsistent casing.
  101. RemoveDuplicates();
  102. }
  103. void RecentFiles::UpdateMenu()
  104. {
  105. m_recentFilesMenu->clear();
  106. AZStd::string cacheFolder = EMotionFX::GetEMotionFX().GetAssetCacheFolder();
  107. AzFramework::StringFunc::Path::Normalize(cacheFolder);
  108. AzFramework::StringFunc::Strip(cacheFolder, AZ_CORRECT_FILESYSTEM_SEPARATOR, true, false, true);
  109. int recentFilesAdded = 0;
  110. const int recentFileCount = m_recentFiles.size();
  111. for (int i = 0; i < recentFileCount; ++i)
  112. {
  113. const QString recentFilePath = m_recentFiles[i];
  114. if (!QFile::exists(recentFilePath))
  115. {
  116. continue;
  117. }
  118. AZStd::string normalizedPath = recentFilePath.toUtf8().data();
  119. AzFramework::StringFunc::Path::Normalize(normalizedPath);
  120. // Check if the file can be found in any of the scan folders (Project asset paths, gem asset paths, etc.)
  121. bool foundInScanFolders = false;
  122. {
  123. bool getScanFoldersSuccess = false;
  124. AZStd::vector<AZStd::string> scanFolders;
  125. AzToolsFramework::AssetSystemRequestBus::BroadcastResult(getScanFoldersSuccess, &AzToolsFramework::AssetSystemRequestBus::Events::GetScanFolders, scanFolders);
  126. if (getScanFoldersSuccess)
  127. {
  128. for (AZStd::string& scanFolder : scanFolders)
  129. {
  130. AzFramework::StringFunc::Path::Normalize(scanFolder);
  131. if (AzFramework::StringFunc::Contains(normalizedPath, scanFolder))
  132. {
  133. foundInScanFolders = true;
  134. }
  135. }
  136. }
  137. // Is the file part of the asset cache folder?
  138. if (AzFramework::StringFunc::Contains(normalizedPath, cacheFolder))
  139. {
  140. foundInScanFolders = true;
  141. }
  142. }
  143. if (foundInScanFolders)
  144. {
  145. const QFileInfo fileInfo(m_recentFiles[i]);
  146. const QString menuItemText = QString("&%1 %2").arg(i + 1).arg(fileInfo.fileName());
  147. QAction* action = new QAction(m_recentFilesMenu);
  148. action->setText(menuItemText);
  149. action->setData(m_recentFiles[i]);
  150. action->setToolTip(m_recentFiles[i]);
  151. m_recentFilesMenu->addAction(action);
  152. recentFilesAdded++;
  153. connect(action, &QAction::triggered, this, &MysticQt::RecentFiles::OnRecentFileSlot);
  154. }
  155. }
  156. if (recentFilesAdded > 0)
  157. {
  158. m_recentFilesMenu->addSeparator();
  159. m_resetRecentFilesAction = m_recentFilesMenu->addAction("Reset Recent Files", this, &RecentFiles::OnClearRecentFiles);
  160. m_resetRecentFilesAction->setObjectName("EMFX.RecentFiles.ResetRecentFilesAction");
  161. }
  162. }
  163. void RecentFiles::SetMaxRecentFiles(size_t numRecentFiles)
  164. {
  165. m_maxNumRecentFiles = numRecentFiles;
  166. if (m_recentFiles.size() > m_maxNumRecentFiles)
  167. {
  168. while (m_recentFiles.size() > m_maxNumRecentFiles)
  169. {
  170. m_recentFiles.removeLast();
  171. }
  172. Save();
  173. UpdateMenu();
  174. }
  175. }
  176. AZStd::string RecentFiles::GetLastRecentFileName() const
  177. {
  178. if (m_recentFiles.empty())
  179. {
  180. return "";
  181. }
  182. return m_recentFiles[0].toUtf8().data();
  183. }
  184. void RecentFiles::AddRecentFile(AZStd::string filename)
  185. {
  186. AzFramework::ApplicationRequests::Bus::Broadcast(&AzFramework::ApplicationRequests::Bus::Events::NormalizePathKeepCase, filename);
  187. // Remove filename duplicates and add the new filename to the top of the recent files.
  188. if (!filename.empty())
  189. {
  190. m_recentFiles.removeAll(filename.c_str());
  191. m_recentFiles.prepend(filename.c_str());
  192. }
  193. // Remove the oldest filenames.
  194. const int maxRecentFiles = static_cast<int>(m_maxNumRecentFiles);
  195. while (m_recentFiles.size() > maxRecentFiles)
  196. {
  197. m_recentFiles.removeLast();
  198. }
  199. Save();
  200. UpdateMenu();
  201. }
  202. void RecentFiles::OnRecentFileSlot()
  203. {
  204. QAction* action = qobject_cast<QAction*>(sender());
  205. if (action == nullptr)
  206. {
  207. return;
  208. }
  209. emit OnRecentFile(action);
  210. }
  211. void RecentFiles::RemoveDuplicates()
  212. {
  213. for (int i = 0; i < m_recentFiles.size(); i++)
  214. {
  215. for (int j = i + 1; j < m_recentFiles.size(); )
  216. {
  217. if (m_recentFiles[i].compare(m_recentFiles[j], Qt::CaseSensitivity::CaseInsensitive) == 0)
  218. {
  219. m_recentFiles.removeAt(j);
  220. }
  221. else
  222. {
  223. j++;
  224. }
  225. }
  226. }
  227. }
  228. } // namespace MysticQt
  229. #include <MysticQt/Source/moc_RecentFiles.cpp>