RecentFiles.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 "CommonSettingsConfigurations.h"
  10. #include <AzCore/std/algorithm.h>
  11. #include <QDir>
  12. #include <QSettings>
  13. #define SCRIPTCANVASEDITOR_SETTINGS_RECENT_FILES_KEY (QString("Recent Files") + " " + QString::fromLocal8Bit(ScriptCanvasEditor::GetEditingGameDataFolder().c_str()) + "/")
  14. #define SCRIPTCANVASEDITOR_SETTINGS_RECENT_FILES_PATH_KEY (QString("path"))
  15. namespace ScriptCanvasEditor
  16. {
  17. QStringList ReadRecentFiles()
  18. {
  19. QSettings settings(QSettings::IniFormat, QSettings::UserScope, SCRIPTCANVASEDITOR_AZ_QCOREAPPLICATION_SETTINGS_ORGANIZATION_NAME);
  20. settings.beginGroup(SCRIPTCANVASEDITOR_NAME_SHORT);
  21. int count = AZStd::min(settings.beginReadArray(SCRIPTCANVASEDITOR_SETTINGS_RECENT_FILES_KEY),
  22. static_cast<int>(c_scriptCanvasEditorSettingsRecentFilesCountMax));
  23. // QSettings -> QStringList.
  24. QStringList recentFiles;
  25. {
  26. for (int i = 0; i < count; ++i)
  27. {
  28. settings.setArrayIndex(i);
  29. QFile f(QDir::toNativeSeparators(settings.value(SCRIPTCANVASEDITOR_SETTINGS_RECENT_FILES_PATH_KEY).toString()));
  30. if(f.exists())
  31. {
  32. recentFiles.append(f.fileName());
  33. }
  34. }
  35. }
  36. settings.endArray();
  37. settings.endGroup();
  38. return recentFiles;
  39. }
  40. void WriteRecentFiles(const QStringList& recentFiles)
  41. {
  42. QSettings settings(QSettings::IniFormat, QSettings::UserScope, SCRIPTCANVASEDITOR_AZ_QCOREAPPLICATION_SETTINGS_ORGANIZATION_NAME);
  43. settings.beginGroup(SCRIPTCANVASEDITOR_NAME_SHORT);
  44. settings.beginWriteArray(SCRIPTCANVASEDITOR_SETTINGS_RECENT_FILES_KEY);
  45. int count = AZStd::min(recentFiles.size(),
  46. static_cast<int>(c_scriptCanvasEditorSettingsRecentFilesCountMax));
  47. // QSettings -> QStringList.
  48. {
  49. for (int i = 0; i < count; ++i)
  50. {
  51. settings.setArrayIndex(i);
  52. QFile f(recentFiles.at(i));
  53. if(f.exists())
  54. {
  55. settings.setValue(SCRIPTCANVASEDITOR_SETTINGS_RECENT_FILES_PATH_KEY, f.fileName());
  56. }
  57. }
  58. }
  59. settings.endArray();
  60. settings.endGroup();
  61. }
  62. void AddRecentFile(const QString& filename)
  63. {
  64. QSettings settings(QSettings::IniFormat, QSettings::UserScope, SCRIPTCANVASEDITOR_AZ_QCOREAPPLICATION_SETTINGS_ORGANIZATION_NAME);
  65. // QSettings -> QStringList.
  66. QStringList recentFiles = ReadRecentFiles();
  67. QFile f(QDir::toNativeSeparators(filename));
  68. if (f.exists())
  69. {
  70. recentFiles.prepend(f.fileName());
  71. recentFiles.removeDuplicates();
  72. }
  73. WriteRecentFiles(recentFiles);
  74. }
  75. void ClearRecentFile()
  76. {
  77. QStringList empty;
  78. WriteRecentFiles(QStringList());
  79. }
  80. }