CmEditorApplication.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #include "CmEditorApplication.h"
  2. #include "CmEditorPrefs.h"
  3. #include "CmProjectPrefs.h"
  4. #include "CmQtEditor.h"
  5. #include "CmQtProjectSelection.h"
  6. #include "CmEditorWindowManager.h"
  7. #include "CmWindowDockManager.h"
  8. #include "CmSceneWindowFactory.h"
  9. #include "CmHierarchyWindowFactory.h"
  10. #include "CmFileSystem.h"
  11. #include "CmException.h"
  12. #include "CmDataStream.h"
  13. #include <QtWidgets/QApplication>
  14. #include <QtCore/QDir>
  15. namespace CamelotEditor
  16. {
  17. const QString EditorApplication::PROJECT_PREFS_FILE_NAME = "CamelotProject.xml";
  18. const QString EditorApplication::EDITOR_PREFS_FILE_NAME = "Editor.xml";
  19. const QString EditorApplication::STYLE_SHEETS_DIRECTORY_NAME = "Styles";
  20. struct EditorApplication::PImpl
  21. {
  22. QApplication* mApp;
  23. QtEditor* mEditor;
  24. };
  25. EditorApplication::EditorApplication()
  26. :p(new PImpl())
  27. {
  28. p->mApp = nullptr;
  29. p->mEditor = nullptr;
  30. }
  31. EditorApplication::~EditorApplication()
  32. {
  33. delete p;
  34. }
  35. void EditorApplication::startUp()
  36. {
  37. EditorPrefs::startUp(new EditorPrefs());
  38. if(FileSystem::fileExists(getEditorPrefsPath().toStdString()))
  39. gEditorPrefs().load(getEditorPrefsPath());
  40. ProjectPrefs::startUp(new ProjectPrefs());
  41. startUpQt();
  42. EditorWindowManager::startUp(new EditorWindowManager());
  43. WindowDockManager::startUp(new WindowDockManager(p->mEditor->getCentralWidget(), p->mEditor->getDockOverlayWidget()));
  44. gEditorWindowManager().registerWindowFactory(new SceneWindowFactory());
  45. gEditorWindowManager().registerWindowFactory(new HierarchyWindowFactory());
  46. vector<QString>::type windowTypes = gEditorWindowManager().getAvailableWindowTypes();
  47. for(auto iter = windowTypes.begin(); iter != windowTypes.end(); ++iter)
  48. {
  49. p->mEditor->addMenuItemCallback("Windows", *iter, gEditorWindowManager().getOpenCallback(*iter));
  50. }
  51. }
  52. void EditorApplication::run()
  53. {
  54. QtProjectSelection projSelection;
  55. projSelection.onProjectSelected.connect(boost::bind(&EditorApplication::loadProject, this, _1));
  56. if(projSelection.exec() == QDialog::Rejected)
  57. return;
  58. p->mEditor->setProjectName(gProjectPrefs().getProjectName());
  59. p->mEditor->show();
  60. p->mApp->exec();
  61. }
  62. void EditorApplication::shutDown()
  63. {
  64. WindowDockManager::shutDown();
  65. EditorWindowManager::shutDown();
  66. shutDownQt();
  67. ProjectPrefs::shutDown();
  68. gEditorPrefs().save(getEditorPrefsPath());
  69. EditorPrefs::shutDown();
  70. }
  71. void EditorApplication::startUpQt()
  72. {
  73. int argc = 0;
  74. p->mApp = new QApplication(argc, nullptr);
  75. p->mEditor = new QtEditor();
  76. loadStyleSheets();
  77. }
  78. void EditorApplication::shutDownQt()
  79. {
  80. delete p->mApp;
  81. }
  82. void EditorApplication::loadStyleSheets()
  83. {
  84. QDir styleSheetsDir = getStyleSheetsDirectoryPath();
  85. if(!styleSheetsDir.exists())
  86. return;
  87. QStringList nameFilters;
  88. nameFilters<<"*.css";
  89. QFileInfoList fileInfos = styleSheetsDir.entryInfoList(nameFilters, QDir::Files | QDir::Readable);
  90. QString styleSheet;
  91. for(auto iter = fileInfos.begin(); iter != fileInfos.end(); ++iter)
  92. {
  93. DataStreamPtr dataStream = FileSystem::open(iter->absoluteFilePath().toStdString());
  94. styleSheet += QString::fromStdString(dataStream->getAsString());
  95. }
  96. p->mApp->setStyleSheet(styleSheet);
  97. }
  98. void EditorApplication::loadProject(const QString& absProjPath)
  99. {
  100. if(!isValidProject(absProjPath))
  101. CM_EXCEPT(InternalErrorException, "Cannot load project at path: " + absProjPath.toStdString());
  102. QString projectPrefsPath = getProjectPrefsPath(absProjPath);
  103. gProjectPrefs().load(projectPrefsPath);
  104. }
  105. void EditorApplication::createProject(const QString& absProjDir, const QString& projName)
  106. {
  107. QString fullProjPath = getFullProjectPath(absProjDir, projName);
  108. String fullProjPathStd = fullProjPath.toStdString();
  109. if(FileSystem::dirExists(fullProjPathStd))
  110. CM_EXCEPT(InternalErrorException, "Directory you have choosen for your project already exists: " + fullProjPathStd);
  111. FileSystem::createDir(fullProjPathStd);
  112. QString projPrefsPath = getProjectPrefsPath(fullProjPath);
  113. gProjectPrefs().setProjectName(projName);
  114. gProjectPrefs().save(projPrefsPath);
  115. }
  116. void EditorApplication::deleteProject(const QString& absProjPath)
  117. {
  118. FileSystem::deleteDir(absProjPath.toStdString());
  119. }
  120. bool EditorApplication::isValidProject(const QString& absProjDir) const
  121. {
  122. return projectExists(absProjDir);
  123. }
  124. bool EditorApplication::isValidProjectName(const QString& name) const
  125. {
  126. return FileSystem::isValidFileName(name.toStdString());
  127. }
  128. QString EditorApplication::getFullProjectPath(const QString& absProjDir, const QString& projName) const
  129. {
  130. QString dir = absProjDir;
  131. return QDir::cleanPath(QDir::toNativeSeparators(absProjDir + QDir::separator() + projName));
  132. }
  133. bool EditorApplication::projectExists(const QString& absProjPath) const
  134. {
  135. QString filePath = getProjectPrefsPath(absProjPath);
  136. if(FileSystem::fileExists(filePath.toStdString()))
  137. return true;
  138. return false;
  139. }
  140. QString EditorApplication::getProjectPrefsPath(const QString& absProjPath) const
  141. {
  142. return QDir::cleanPath(QDir::toNativeSeparators(absProjPath + QDir::separator() + PROJECT_PREFS_FILE_NAME));
  143. }
  144. QString EditorApplication::getEditorRootPath() const
  145. {
  146. return QString::fromStdString(FileSystem::getCurrentPath());
  147. }
  148. QString EditorApplication::getEditorPrefsPath() const
  149. {
  150. return QDir::cleanPath(QDir::toNativeSeparators(getEditorRootPath() + QDir::separator() + EDITOR_PREFS_FILE_NAME));
  151. }
  152. QtEditor* EditorApplication::getMainWindow() const
  153. {
  154. return p->mEditor;
  155. }
  156. QString EditorApplication::getStyleSheetsDirectoryPath() const
  157. {
  158. return QDir::cleanPath(QDir::toNativeSeparators(getEditorRootPath() + QDir::separator() + STYLE_SHEETS_DIRECTORY_NAME));
  159. }
  160. EditorApplication& gEditorApp()
  161. {
  162. static EditorApplication application;
  163. return application;
  164. }
  165. }