RecentFiles.cpp 2.2 KB

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