CmEditorPrefs.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. }
  41. const WindowLayoutDesc& EditorPrefs::getMainWindowLayout() const
  42. {
  43. return mMainWindowLayout;
  44. }
  45. void EditorPrefs::save(const QString& path, bool overwrite) const
  46. {
  47. String stdPath = path.toStdString();
  48. if(FileSystem::fileExists(stdPath))
  49. {
  50. if(!overwrite)
  51. {
  52. CM_EXCEPT(FileNotFoundException, "File already exists at this location.");
  53. }
  54. else
  55. FileSystem::remove(stdPath);
  56. }
  57. xml_document xmldoc;
  58. xml_node camelotEditor = xmldoc.append_child("CamelotEditor");
  59. xml_node recentlyUsedProjects = camelotEditor.append_child("RecentlyUsedProjects");
  60. for(auto iter = mRecentlyUsedProjects.begin(); iter != mRecentlyUsedProjects.end(); ++iter)
  61. {
  62. xml_node recentlyUsedProject = recentlyUsedProjects.append_child("RecentlyUsedProject");
  63. xml_attribute pathAttrib = recentlyUsedProject.append_attribute("path");
  64. pathAttrib.set_value(iter->toStdString().c_str());
  65. }
  66. xml_node lastUsedProjectDirectory = camelotEditor.append_child("LastUsedProjectDir");
  67. xml_attribute pathAttrib = lastUsedProjectDirectory.append_attribute("path");
  68. pathAttrib.set_value(mLastUsedProjectDirectory.toStdString().c_str());
  69. xml_node openWindows = camelotEditor.append_child("OpenWindows");
  70. saveWindowLayout(openWindows, mMainWindowLayout);
  71. for(auto iter = mWindowLayouts.begin(); iter != mWindowLayouts.end(); ++iter)
  72. {
  73. saveWindowLayout(openWindows, *iter);
  74. }
  75. xmldoc.save_file(stdPath.c_str());
  76. }
  77. void EditorPrefs::saveWindowLayout(xml_node parentNode, const WindowLayoutDesc& desc) const
  78. {
  79. xml_node windowLayout = parentNode.append_child("WindowLayout");
  80. parentNode.append_attribute("id").set_value(desc.id);
  81. xml_node windowGeometry = windowLayout.append_child("Geometry");
  82. windowGeometry.append_attribute("left").set_value(desc.left);
  83. windowGeometry.append_attribute("top").set_value(desc.top);
  84. windowGeometry.append_attribute("width").set_value(desc.width);
  85. windowGeometry.append_attribute("height").set_value(desc.height);
  86. xml_node windowScreen = windowLayout.append_child("Screen");
  87. windowScreen.append_attribute("fullscreen").set_value(desc.maximized);
  88. windowScreen.append_attribute("screenIdx").set_value(desc.screenIdx);
  89. xml_node dockInfo = windowLayout.append_child("DockInfo");
  90. dockInfo.append_attribute("state").set_value((UINT32)desc.dockState);
  91. dockInfo.append_attribute("parentId").set_value(desc.dockParentId);
  92. xml_node childWidgets = windowLayout.append_child("ChildWidgets");
  93. childWidgets.append_attribute("activeWidgetIdx").set_value(desc.activeWidget);
  94. for(auto iter = desc.childWidgetNames.begin(); iter != desc.childWidgetNames.end(); ++iter)
  95. {
  96. childWidgets.append_child("ChildWidget").append_attribute("name").set_value(iter->toStdString().c_str());
  97. }
  98. }
  99. WindowLayoutDesc EditorPrefs::loadWindowLayout(xml_node node) const
  100. {
  101. WindowLayoutDesc desc;
  102. desc.id = node.attribute("id").as_int();
  103. desc.left = node.child("Geometry").attribute("left").as_uint();
  104. desc.top = node.child("Geometry").attribute("top").as_uint();
  105. desc.width = node.child("Geometry").attribute("width").as_uint();
  106. desc.height = node.child("Geometry").attribute("height").as_uint();
  107. desc.maximized = node.child("Screen").attribute("fullscreen").as_bool();
  108. desc.screenIdx = node.child("Screen").attribute("screenIdx").as_uint();
  109. desc.dockState = (WindowDockState)node.child("DockInfo").attribute("state").as_uint();
  110. desc.dockParentId = node.child("DockInfo").attribute("parentId").as_int();
  111. desc.activeWidget = node.child("ChildWidgets").attribute("activeWidgetIdx").as_uint();
  112. xml_node childWidgets = node.child("ChildWidgets");
  113. for (xml_node childWidget = childWidgets.child("ChildWidget"); childWidget; childWidget = childWidget.next_sibling("ChildWidget"))
  114. {
  115. desc.childWidgetNames.push_back(childWidget.attribute("name").as_string());
  116. }
  117. return desc;
  118. }
  119. void EditorPrefs::setWindowLayouts(const Vector<WindowLayoutDesc>::type& descs)
  120. {
  121. mWindowLayouts = descs;
  122. }
  123. const Vector<WindowLayoutDesc>::type& EditorPrefs::getWindowLayouts() const
  124. {
  125. return mWindowLayouts;
  126. }
  127. void EditorPrefs::load(const QString& path)
  128. {
  129. clear();
  130. String stdPath = path.toStdString();
  131. if(!FileSystem::fileExists(stdPath))
  132. CM_EXCEPT(FileNotFoundException, "Specified file: " + stdPath + " does not exist.");
  133. xml_document xmldoc;
  134. xml_parse_result parseResult = xmldoc.load_file(stdPath.c_str());
  135. if(parseResult.status != status_ok)
  136. CM_EXCEPT(InternalErrorException, "XML parsing failed: " + toString(parseResult.description()));
  137. xml_node camelotEditor = xmldoc.child("CamelotEditor");
  138. xml_node recentlyUsedProjects = camelotEditor.child("RecentlyUsedProjects");
  139. for(auto iter = recentlyUsedProjects.begin(); iter != recentlyUsedProjects.end(); ++iter)
  140. {
  141. xml_attribute pathAttrib = iter->attribute("path");
  142. mRecentlyUsedProjects.push_back(pathAttrib.value());
  143. }
  144. xml_node lastUsedProjectDirectory = camelotEditor.child("LastUsedProjectDir");
  145. xml_attribute pathAttrib = lastUsedProjectDirectory.attribute("path");
  146. mLastUsedProjectDirectory = pathAttrib.value();
  147. xml_node openWindows = camelotEditor.child("OpenWindows");
  148. bool foundMainWindowLayoutDesc = false;
  149. for (xml_node windowLayout = openWindows.child("WindowLayout"); windowLayout; windowLayout = windowLayout.next_sibling("WindowLayout"))
  150. {
  151. WindowLayoutDesc desc = loadWindowLayout(windowLayout);
  152. if(!foundMainWindowLayoutDesc)
  153. {
  154. mMainWindowLayout = desc;
  155. foundMainWindowLayoutDesc = true;
  156. }
  157. else
  158. {
  159. mWindowLayouts.push_back(desc);
  160. }
  161. }
  162. }
  163. void EditorPrefs::clear()
  164. {
  165. mRecentlyUsedProjects.clear();
  166. mLastUsedProjectDirectory = "";
  167. mMainWindowLayout = WindowLayoutDesc();
  168. mWindowLayouts.clear();
  169. }
  170. EditorPrefs& gEditorPrefs()
  171. {
  172. return EditorPrefs::instance();
  173. }
  174. }