CmEditorApplication.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include "CmEditorApplication.h"
  2. #include "CmLayoutManager.h"
  3. #include "CmEditorPrefs.h"
  4. #include "CmProjectPrefs.h"
  5. #include "CmQtEditor.h"
  6. #include "CmQtProjectSelection.h"
  7. #include "CmFileSystem.h"
  8. #include "CmException.h"
  9. #include <QtWidgets/QApplication>
  10. #include <QtCore/QDir>
  11. namespace CamelotEditor
  12. {
  13. const QString EditorApplication::PROJECT_PREFS_FILE_NAME = "CamelotProject.xml";
  14. const QString EditorApplication::EDITOR_PREFS_FILE_NAME = "Editor.xml";
  15. struct EditorApplication::PImpl
  16. {
  17. };
  18. EditorApplication::EditorApplication()
  19. :p(new PImpl())
  20. {
  21. }
  22. EditorApplication::~EditorApplication()
  23. {
  24. delete p;
  25. }
  26. void EditorApplication::startUp()
  27. {
  28. EditorPrefs::startUp(new EditorPrefs());
  29. if(FileSystem::fileExists(getEditorPrefsPath().toStdString()))
  30. gEditorPrefs().load(getEditorPrefsPath());
  31. ProjectPrefs::startUp(new ProjectPrefs());
  32. LayoutManager::startUp(new LayoutManager());
  33. }
  34. void EditorApplication::run()
  35. {
  36. int argc = 0;
  37. QApplication a(argc, nullptr);
  38. QtProjectSelection projSelection;
  39. projSelection.onProjectSelected.connect(boost::bind(&EditorApplication::loadProject, this, _1));
  40. if(projSelection.exec() == QDialog::Rejected)
  41. return;
  42. QtEditor w;
  43. w.show();
  44. //WindowDockManager::startUp(new WindowDockManager())
  45. a.exec();
  46. }
  47. void EditorApplication::shutDown()
  48. {
  49. LayoutManager::shutDown();
  50. ProjectPrefs::shutDown();
  51. gEditorPrefs().save(getEditorPrefsPath());
  52. EditorPrefs::shutDown();
  53. }
  54. void EditorApplication::loadProject(const QString& absProjPath)
  55. {
  56. if(!isValidProject(absProjPath))
  57. CM_EXCEPT(InternalErrorException, "Cannot load project at path: " + absProjPath.toStdString());
  58. QString projectPrefsPath = getProjectPrefsPath(absProjPath);
  59. gProjectPrefs().load(projectPrefsPath);
  60. }
  61. void EditorApplication::createProject(const QString& absProjDir, const QString& projName)
  62. {
  63. QString fullProjPath = getFullProjectPath(absProjDir, projName);
  64. String fullProjPathStd = fullProjPath.toStdString();
  65. if(FileSystem::dirExists(fullProjPathStd))
  66. CM_EXCEPT(InternalErrorException, "Directory you have choosen for your project already exists: " + fullProjPathStd);
  67. FileSystem::createDir(fullProjPathStd);
  68. QString projPrefsPath = getProjectPrefsPath(fullProjPath);
  69. gProjectPrefs().setProjectName(projName);
  70. gProjectPrefs().save(projPrefsPath);
  71. }
  72. void EditorApplication::deleteProject(const QString& absProjPath)
  73. {
  74. FileSystem::deleteDir(absProjPath.toStdString());
  75. }
  76. bool EditorApplication::isValidProject(const QString& absProjDir) const
  77. {
  78. return projectExists(absProjDir);
  79. }
  80. bool EditorApplication::isValidProjectName(const QString& name) const
  81. {
  82. return FileSystem::isValidFileName(name.toStdString());
  83. }
  84. QString EditorApplication::getFullProjectPath(const QString& absProjDir, const QString& projName) const
  85. {
  86. QString dir = absProjDir;
  87. return QDir::cleanPath(QDir::toNativeSeparators(absProjDir + QDir::separator() + projName));
  88. }
  89. bool EditorApplication::projectExists(const QString& absProjPath) const
  90. {
  91. QString filePath = getProjectPrefsPath(absProjPath);
  92. if(FileSystem::fileExists(filePath.toStdString()))
  93. return true;
  94. return false;
  95. }
  96. QString EditorApplication::getProjectPrefsPath(const QString& absProjPath) const
  97. {
  98. return QDir::cleanPath(QDir::toNativeSeparators(absProjPath + QDir::separator() + PROJECT_PREFS_FILE_NAME));
  99. }
  100. QString EditorApplication::getEditorRootPath() const
  101. {
  102. return QString::fromStdString(FileSystem::getCurrentPath());
  103. }
  104. QString EditorApplication::getEditorPrefsPath() const
  105. {
  106. return QDir::cleanPath(QDir::toNativeSeparators(getEditorRootPath() + QDir::separator() + EDITOR_PREFS_FILE_NAME));
  107. }
  108. EditorApplication& gEditorApp()
  109. {
  110. static EditorApplication application;
  111. return application;
  112. }
  113. }