CmEditorPrefs.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include "CmEditorPrefs.h"
  2. #include "CmFileSystem.h"
  3. #include <QtCore/QDir>
  4. using namespace pugi;
  5. namespace CamelotEditor
  6. {
  7. UINT32 EditorPrefs::getNumRecentlyUsedProjects() const
  8. {
  9. return (UINT32)mRecentlyUsedProjects.size();
  10. }
  11. const QString& EditorPrefs::getRecentlyUsedProjectPath(UINT32 idx) const
  12. {
  13. return mRecentlyUsedProjects.at(idx);
  14. }
  15. void EditorPrefs::addRecentlyUsedProjectPath(const QString& path)
  16. {
  17. QString cleanPath = QDir::cleanPath(QDir::toNativeSeparators(path));
  18. for(auto iter = mRecentlyUsedProjects.begin(); iter != mRecentlyUsedProjects.end(); ++iter)
  19. {
  20. if(*iter == cleanPath)
  21. return;
  22. }
  23. mRecentlyUsedProjects.push_back(cleanPath);
  24. }
  25. void EditorPrefs::removeRecentlyUsedProjectPath(UINT32 idx)
  26. {
  27. mRecentlyUsedProjects.erase(mRecentlyUsedProjects.begin() + idx);
  28. }
  29. void EditorPrefs::setLastUsedProjectDirectory(const QString& value)
  30. {
  31. mLastUsedProjectDirectory = QDir::cleanPath(QDir::toNativeSeparators(value));
  32. }
  33. const QString& EditorPrefs::getLastUsedProjectDirectory() const
  34. {
  35. return mLastUsedProjectDirectory;
  36. }
  37. void EditorPrefs::setMainWindowLayout(const WindowLayoutDesc& desc)
  38. {
  39. mMainWindowLayout = desc;
  40. mMainWindowLayout.name = "MainWindow";
  41. }
  42. const WindowLayoutDesc& EditorPrefs::getMainWindowLayout() const
  43. {
  44. return mMainWindowLayout;
  45. }
  46. void EditorPrefs::save(const QString& path, bool overwrite) const
  47. {
  48. String stdPath = path.toStdString();
  49. if(FileSystem::fileExists(stdPath))
  50. {
  51. if(!overwrite)
  52. {
  53. CM_EXCEPT(FileNotFoundException, "File already exists at this location.");
  54. }
  55. else
  56. FileSystem::remove(stdPath);
  57. }
  58. xml_document xmldoc;
  59. xml_node camelotEditor = xmldoc.append_child("CamelotEditor");
  60. xml_node recentlyUsedProjects = camelotEditor.append_child("RecentlyUsedProjects");
  61. for(auto iter = mRecentlyUsedProjects.begin(); iter != mRecentlyUsedProjects.end(); ++iter)
  62. {
  63. xml_node recentlyUsedProject = recentlyUsedProjects.append_child("RecentlyUsedProject");
  64. xml_attribute pathAttrib = recentlyUsedProject.append_attribute("path");
  65. pathAttrib.set_value(iter->toStdString().c_str());
  66. }
  67. xml_node lastUsedProjectDirectory = camelotEditor.append_child("LastUsedProjectDir");
  68. xml_attribute pathAttrib = lastUsedProjectDirectory.append_attribute("path");
  69. pathAttrib.set_value(mLastUsedProjectDirectory.toStdString().c_str());
  70. xml_node openWindows = camelotEditor.append_child("OpenWindows");
  71. saveWindowLayout(openWindows, mMainWindowLayout);
  72. // TODO - Add non-main windows
  73. xmldoc.save_file(stdPath.c_str());
  74. }
  75. void EditorPrefs::saveWindowLayout(xml_node parentNode, const WindowLayoutDesc& desc) const
  76. {
  77. xml_node windowLayout = parentNode.append_child("WindowLayout");
  78. windowLayout.append_attribute("name").set_value(desc.name.toStdString().c_str());
  79. xml_node windowGeometry = windowLayout.append_child("Geometry");
  80. windowGeometry.append_attribute("left").set_value(desc.left);
  81. windowGeometry.append_attribute("top").set_value(desc.top);
  82. windowGeometry.append_attribute("width").set_value(desc.width);
  83. windowGeometry.append_attribute("height").set_value(desc.height);
  84. xml_node windowScreen = windowLayout.append_child("Screen");
  85. windowScreen.append_attribute("fullscreen").set_value(desc.maximized);
  86. windowScreen.append_attribute("screenIdx").set_value(desc.screenIdx);
  87. xml_node dockInfo = windowLayout.append_child("DockInfo");
  88. dockInfo.append_attribute("docked").set_value(desc.docked);
  89. }
  90. WindowLayoutDesc EditorPrefs::loadWindowLayout(xml_node node) const
  91. {
  92. WindowLayoutDesc desc;
  93. desc.name = node.attribute("name").value();
  94. desc.left = node.child("Geometry").attribute("left").as_uint();
  95. desc.top = node.child("Geometry").attribute("top").as_uint();
  96. desc.width = node.child("Geometry").attribute("width").as_uint();
  97. desc.height = node.child("Geometry").attribute("height").as_uint();
  98. desc.maximized = node.child("Screen").attribute("fullscreen").as_bool();
  99. desc.screenIdx = node.child("Screen").attribute("screenIdx").as_uint();
  100. desc.docked = node.child("DockInfo").attribute("docked").as_bool();
  101. return desc;
  102. }
  103. void EditorPrefs::load(const QString& path)
  104. {
  105. clear();
  106. String stdPath = path.toStdString();
  107. if(!FileSystem::fileExists(stdPath))
  108. CM_EXCEPT(FileNotFoundException, "Specified file: " + stdPath + " does not exist.");
  109. xml_document xmldoc;
  110. xml_parse_result parseResult = xmldoc.load_file(stdPath.c_str());
  111. if(parseResult.status != status_ok)
  112. CM_EXCEPT(InternalErrorException, "XML parsing failed: " + toString(parseResult.description()));
  113. xml_node camelotEditor = xmldoc.child("CamelotEditor");
  114. xml_node recentlyUsedProjects = camelotEditor.child("RecentlyUsedProjects");
  115. for(auto iter = recentlyUsedProjects.begin(); iter != recentlyUsedProjects.end(); ++iter)
  116. {
  117. xml_attribute pathAttrib = iter->attribute("path");
  118. mRecentlyUsedProjects.push_back(pathAttrib.value());
  119. }
  120. xml_node lastUsedProjectDirectory = camelotEditor.child("LastUsedProjectDir");
  121. xml_attribute pathAttrib = lastUsedProjectDirectory.attribute("path");
  122. mLastUsedProjectDirectory = pathAttrib.value();
  123. xml_node openWindows = camelotEditor.child("OpenWindows");
  124. bool foundMainWindowLayoutDesc = false;
  125. for (xml_node windowLayout = openWindows.child("WindowLayout"); windowLayout; windowLayout = windowLayout.next_sibling("WindowLayout"))
  126. {
  127. WindowLayoutDesc desc = loadWindowLayout(windowLayout);
  128. if(!foundMainWindowLayoutDesc)
  129. {
  130. mMainWindowLayout = desc;
  131. foundMainWindowLayoutDesc = true;
  132. }
  133. }
  134. }
  135. void EditorPrefs::clear()
  136. {
  137. mRecentlyUsedProjects.clear();
  138. mLastUsedProjectDirectory = "";
  139. mMainWindowLayout = WindowLayoutDesc();
  140. }
  141. EditorPrefs& gEditorPrefs()
  142. {
  143. return EditorPrefs::instance();
  144. }
  145. }