RecentFiles.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 <QSettings>
  9. #define LUAEDITOR_SETTINGS_RECENT_FILES_KEY (QString("Recent Files"))
  10. #define LUAEDITOR_SETTINGS_RECENT_FILES_PATH_KEY (QString("path"))
  11. #define LUAEDITOR_SETTINGS_RECENT_FILES_COUNT_MAX (10)
  12. #define LUAEDITOR_GROUPNAME "Lua Editor"
  13. #define AZ_QCOREAPPLICATION_SETTINGS_ORGANIZATION_NAME "O3DE"
  14. #define AZ_QCOREAPPLICATION_SETTINGS_APPLICATION_NAME "O3DE"
  15. QStringList ReadRecentFiles()
  16. {
  17. QSettings settings(QSettings::IniFormat, QSettings::UserScope, AZ_QCOREAPPLICATION_SETTINGS_ORGANIZATION_NAME);
  18. settings.beginGroup(LUAEDITOR_GROUPNAME);
  19. int count = std::min(settings.beginReadArray(LUAEDITOR_SETTINGS_RECENT_FILES_KEY), LUAEDITOR_SETTINGS_RECENT_FILES_COUNT_MAX);
  20. // QSettings -> QStringList.
  21. QStringList recentFiles;
  22. {
  23. for (int i = 0; i < count; ++i)
  24. {
  25. settings.setArrayIndex(i);
  26. recentFiles.append(settings.value(LUAEDITOR_SETTINGS_RECENT_FILES_PATH_KEY).toString());
  27. }
  28. }
  29. settings.endArray();
  30. settings.endGroup();
  31. return recentFiles;
  32. }
  33. void WriteRecentFiles(const QStringList& recentFiles)
  34. {
  35. QSettings settings(QSettings::IniFormat, QSettings::UserScope, AZ_QCOREAPPLICATION_SETTINGS_ORGANIZATION_NAME);
  36. settings.beginGroup(LUAEDITOR_GROUPNAME);
  37. settings.beginWriteArray(LUAEDITOR_SETTINGS_RECENT_FILES_KEY);
  38. int count = std::min(recentFiles.size(),
  39. LUAEDITOR_SETTINGS_RECENT_FILES_COUNT_MAX);
  40. // QSettings -> QStringList.
  41. {
  42. for (int i = 0; i < count; ++i)
  43. {
  44. settings.setArrayIndex(i);
  45. settings.setValue(LUAEDITOR_SETTINGS_RECENT_FILES_PATH_KEY, recentFiles.at(i));
  46. }
  47. }
  48. settings.endArray();
  49. settings.endGroup();
  50. }
  51. void AddRecentFile(const QString& filename)
  52. {
  53. // QSettings -> QStringList.
  54. QStringList recentFiles = ReadRecentFiles();
  55. recentFiles.prepend(filename);
  56. recentFiles.removeDuplicates();
  57. WriteRecentFiles(recentFiles);
  58. }
  59. void ClearRecentFile()
  60. {
  61. WriteRecentFiles(QStringList());
  62. }