CmQtNewProject.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include "CmQtNewProject.h"
  2. #include <QtWidgets/QHBoxLayout>
  3. #include <QtWidgets/QVBoxLayout>
  4. #include <QtWidgets/QFileDialog>
  5. #include <QtWidgets/QMessageBox>
  6. #include "CmFileSystem.h"
  7. #include "CmEditorPrefs.h"
  8. #include "CmEditorApplication.h"
  9. namespace CamelotEditor
  10. {
  11. QtNewProject::QtNewProject(QWidget *parent)
  12. :QDialog(parent)
  13. {
  14. setupUi();
  15. }
  16. QtNewProject::~QtNewProject()
  17. { }
  18. void QtNewProject::setupUi()
  19. {
  20. setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint | Qt::MSWindowsFixedSizeDialogHint);
  21. setFixedSize(400, 170);
  22. /************************************************************************/
  23. /* Create controls */
  24. /************************************************************************/
  25. mLblProjectName = new QLabel(this);
  26. mLblProjectPath = new QLabel(this);
  27. mTxtProjectName = new QLineEdit(this);
  28. mTxtProjectPath = new QLineEdit(this);
  29. mBtnCreate = new QPushButton(this);
  30. mBtnBrowse = new QPushButton(this);
  31. /************************************************************************/
  32. /* Name layout */
  33. /************************************************************************/
  34. QHBoxLayout* nameLayout = new QHBoxLayout(this);
  35. nameLayout->setMargin(0);
  36. nameLayout->addWidget(mLblProjectName);
  37. nameLayout->addWidget(mTxtProjectName);
  38. QWidget* nameWidget = new QWidget(this);
  39. nameWidget->setLayout(nameLayout);
  40. /************************************************************************/
  41. /* Path layout */
  42. /************************************************************************/
  43. QHBoxLayout* pathLayout = new QHBoxLayout(this);
  44. pathLayout->setMargin(0);
  45. pathLayout->addWidget(mLblProjectPath);
  46. pathLayout->addWidget(mTxtProjectPath);
  47. pathLayout->addWidget(mBtnBrowse);
  48. QWidget* pathWidget = new QWidget(this);
  49. pathWidget->setLayout(pathLayout);
  50. /************************************************************************/
  51. /* Central layout */
  52. /************************************************************************/
  53. QVBoxLayout* centralLayout = new QVBoxLayout(this);
  54. centralLayout->setMargin(0);
  55. centralLayout->addWidget(nameWidget);
  56. centralLayout->addWidget(pathWidget);
  57. centralLayout->addWidget(mBtnCreate);
  58. setLayout(centralLayout);
  59. retranslateUi();
  60. setupSignals();
  61. }
  62. void QtNewProject::setupSignals()
  63. {
  64. connect(mBtnCreate, &QPushButton::clicked, this, &QtNewProject::createProject);
  65. connect(mBtnBrowse, &QPushButton::clicked, this, &QtNewProject::browseProjectPath);
  66. }
  67. void QtNewProject::retranslateUi()
  68. {
  69. setWindowTitle(tr("New project"));
  70. mLblProjectName->setText(tr("Name:"));
  71. mLblProjectPath->setText(tr("Directory:"));
  72. mBtnCreate->setText(tr("Create"));
  73. mBtnBrowse->setText(tr("Browse"));
  74. QDir lastUsedDir = gEditorPrefs().getLastUsedProjectDirectory();
  75. if(lastUsedDir.exists())
  76. mTxtProjectPath->setText(lastUsedDir.absolutePath());
  77. else
  78. mTxtProjectPath->setText(QString::fromStdString(FileSystem::getCurrentPath()));
  79. }
  80. void QtNewProject::createProject()
  81. {
  82. QString name = mTxtProjectName->text();
  83. if(!gEditorApp().isValidProjectName(name))
  84. {
  85. QMessageBox msgBox;
  86. msgBox.setText("Error");
  87. msgBox.setInformativeText("Provided project name is not valid. Please use only alphanumeric characters.");
  88. msgBox.exec();
  89. return;
  90. }
  91. QString projectDirPath = mTxtProjectPath->text();
  92. QString fullPath = gEditorApp().getFullProjectPath(projectDirPath, name);
  93. if(gEditorApp().projectExists(fullPath))
  94. {
  95. QMessageBox msgBox;
  96. msgBox.setText("Warning");
  97. msgBox.setInformativeText("There is already a project at the specified location. Do you want to overwrite?");
  98. msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
  99. msgBox.setDefaultButton(QMessageBox::No);
  100. int ret = msgBox.exec();
  101. if(ret == QMessageBox::Yes)
  102. gEditorApp().deleteProject(fullPath);
  103. else
  104. return;
  105. }
  106. QDir dir(projectDirPath);
  107. if(!dir.mkpath(projectDirPath))
  108. {
  109. QMessageBox msgBox;
  110. msgBox.setText("Error");
  111. msgBox.setInformativeText("Failed to create project directory.");
  112. return;
  113. }
  114. gEditorPrefs().setLastUsedProjectDirectory(projectDirPath);
  115. gEditorApp().createProject(projectDirPath, name);
  116. gEditorApp().loadProject(fullPath);
  117. accept();
  118. }
  119. void QtNewProject::browseProjectPath()
  120. {
  121. QFileDialog dialog(this, tr("Select project directory"));
  122. dialog.setFileMode(QFileDialog::Directory);
  123. dialog.setOption(QFileDialog::ShowDirsOnly, true);
  124. QDir lastUsedDir = gEditorPrefs().getLastUsedProjectDirectory();
  125. if(lastUsedDir.exists())
  126. dialog.setDirectory(lastUsedDir);
  127. if(dialog.exec())
  128. {
  129. QStringList fileNames = dialog.selectedFiles();
  130. if(fileNames.size() > 0)
  131. {
  132. QString dirPath = QString::fromStdString(FileSystem::getDirectoryPath(fileNames[0].toStdString()));
  133. mTxtProjectPath->setText(dirPath);
  134. gEditorPrefs().setLastUsedProjectDirectory(dirPath);
  135. }
  136. }
  137. }
  138. QString QtNewProject::getProjectPath() const
  139. {
  140. QString name = mTxtProjectName->text();
  141. QString projectDirPath = mTxtProjectPath->text();
  142. return gEditorApp().getFullProjectPath(projectDirPath, name);
  143. }
  144. }