CmEditorPrefs.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. for(auto iter = mWindowLayouts.begin(); iter != mWindowLayouts.end(); ++iter)
  73. {
  74. saveWindowLayout(openWindows, *iter);
  75. }
  76. xmldoc.save_file(stdPath.c_str());
  77. }
  78. void EditorPrefs::saveWindowLayout(xml_node parentNode, const WindowLayoutDesc& desc) const
  79. {
  80. xml_node windowLayout = parentNode.append_child("WindowLayout");
  81. windowLayout.append_attribute("name").set_value(desc.name.toStdString().c_str());
  82. xml_node windowGeometry = windowLayout.append_child("Geometry");
  83. windowGeometry.append_attribute("left").set_value(desc.left);
  84. windowGeometry.append_attribute("top").set_value(desc.top);
  85. windowGeometry.append_attribute("width").set_value(desc.width);
  86. windowGeometry.append_attribute("height").set_value(desc.height);
  87. xml_node windowScreen = windowLayout.append_child("Screen");
  88. windowScreen.append_attribute("fullscreen").set_value(desc.maximized);
  89. windowScreen.append_attribute("screenIdx").set_value(desc.screenIdx);
  90. xml_node dockInfo = windowLayout.append_child("DockInfo");
  91. dockInfo.append_attribute("state").set_value((UINT32)desc.dockState);
  92. dockInfo.append_attribute("parentName").set_value(desc.dockParentName.toStdString().c_str());
  93. }
  94. WindowLayoutDesc EditorPrefs::loadWindowLayout(xml_node node) const
  95. {
  96. WindowLayoutDesc desc;
  97. desc.name = node.attribute("name").value();
  98. desc.left = node.child("Geometry").attribute("left").as_uint();
  99. desc.top = node.child("Geometry").attribute("top").as_uint();
  100. desc.width = node.child("Geometry").attribute("width").as_uint();
  101. desc.height = node.child("Geometry").attribute("height").as_uint();
  102. desc.maximized = node.child("Screen").attribute("fullscreen").as_bool();
  103. desc.screenIdx = node.child("Screen").attribute("screenIdx").as_uint();
  104. desc.dockState = (WindowDockState)node.child("DockInfo").attribute("state").as_uint();
  105. desc.dockParentName = node.child("DockInfo").attribute("parentName").as_string();
  106. return desc;
  107. }
  108. void EditorPrefs::setWindowLayouts(const vector<WindowLayoutDesc>::type& descs)
  109. {
  110. mWindowLayouts = descs;
  111. }
  112. const vector<WindowLayoutDesc>::type& EditorPrefs::getWindowLayouts() const
  113. {
  114. return mWindowLayouts;
  115. }
  116. void EditorPrefs::load(const QString& path)
  117. {
  118. clear();
  119. String stdPath = path.toStdString();
  120. if(!FileSystem::fileExists(stdPath))
  121. CM_EXCEPT(FileNotFoundException, "Specified file: " + stdPath + " does not exist.");
  122. xml_document xmldoc;
  123. xml_parse_result parseResult = xmldoc.load_file(stdPath.c_str());
  124. if(parseResult.status != status_ok)
  125. CM_EXCEPT(InternalErrorException, "XML parsing failed: " + toString(parseResult.description()));
  126. xml_node camelotEditor = xmldoc.child("CamelotEditor");
  127. xml_node recentlyUsedProjects = camelotEditor.child("RecentlyUsedProjects");
  128. for(auto iter = recentlyUsedProjects.begin(); iter != recentlyUsedProjects.end(); ++iter)
  129. {
  130. xml_attribute pathAttrib = iter->attribute("path");
  131. mRecentlyUsedProjects.push_back(pathAttrib.value());
  132. }
  133. xml_node lastUsedProjectDirectory = camelotEditor.child("LastUsedProjectDir");
  134. xml_attribute pathAttrib = lastUsedProjectDirectory.attribute("path");
  135. mLastUsedProjectDirectory = pathAttrib.value();
  136. xml_node openWindows = camelotEditor.child("OpenWindows");
  137. bool foundMainWindowLayoutDesc = false;
  138. for (xml_node windowLayout = openWindows.child("WindowLayout"); windowLayout; windowLayout = windowLayout.next_sibling("WindowLayout"))
  139. {
  140. WindowLayoutDesc desc = loadWindowLayout(windowLayout);
  141. if(!foundMainWindowLayoutDesc)
  142. {
  143. mMainWindowLayout = desc;
  144. foundMainWindowLayoutDesc = true;
  145. }
  146. else
  147. {
  148. mWindowLayouts.push_back(desc);
  149. }
  150. }
  151. }
  152. void EditorPrefs::clear()
  153. {
  154. mRecentlyUsedProjects.clear();
  155. mLastUsedProjectDirectory = "";
  156. mMainWindowLayout = WindowLayoutDesc();
  157. mWindowLayouts.clear();
  158. }
  159. EditorPrefs& gEditorPrefs()
  160. {
  161. return EditorPrefs::instance();
  162. }
  163. }