CmQtNewProject.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. setObjectNames();
  60. retranslateUi();
  61. setupSignals();
  62. }
  63. void QtNewProject::setupSignals()
  64. {
  65. connect(mBtnCreate, &QPushButton::clicked, this, &QtNewProject::createProject);
  66. connect(mBtnBrowse, &QPushButton::clicked, this, &QtNewProject::browseProjectPath);
  67. }
  68. void QtNewProject::retranslateUi()
  69. {
  70. setWindowTitle(tr("New project"));
  71. mLblProjectName->setText(tr("Name:"));
  72. mLblProjectPath->setText(tr("Directory:"));
  73. mBtnCreate->setText(tr("Create"));
  74. mBtnBrowse->setText(tr("Browse"));
  75. QDir lastUsedDir = gEditorPrefs().getLastUsedProjectDirectory();
  76. if(lastUsedDir.exists())
  77. mTxtProjectPath->setText(lastUsedDir.absolutePath());
  78. else
  79. mTxtProjectPath->setText(QString::fromStdString(FileSystem::getWorkingDirectoryPath()));
  80. }
  81. void QtNewProject::setObjectNames()
  82. {
  83. mLblProjectName->setObjectName(QStringLiteral("LblProjectName"));
  84. mTxtProjectName->setObjectName(QStringLiteral("TxtProjectName"));
  85. mBtnCreate->setObjectName(QStringLiteral("BtnCreate"));
  86. mLblProjectPath->setObjectName(QStringLiteral("LblProjectPath"));
  87. mTxtProjectPath->setObjectName(QStringLiteral("TxtProjectPath"));
  88. mBtnBrowse->setObjectName(QStringLiteral("BtnBrowse"));
  89. }
  90. void QtNewProject::createProject()
  91. {
  92. QString name = mTxtProjectName->text();
  93. if(!gEditorApp().isValidProjectName(name))
  94. {
  95. QMessageBox msgBox;
  96. msgBox.setText("Error");
  97. msgBox.setInformativeText("Provided project name is not valid. Please use only alphanumeric characters.");
  98. msgBox.exec();
  99. return;
  100. }
  101. QString projectDirPath = mTxtProjectPath->text();
  102. QString fullPath = gEditorApp().getFullProjectPath(projectDirPath, name);
  103. if(gEditorApp().projectExists(fullPath))
  104. {
  105. QMessageBox msgBox;
  106. msgBox.setText("Warning");
  107. msgBox.setInformativeText("There is already a project at the specified location. Do you want to overwrite?");
  108. msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
  109. msgBox.setDefaultButton(QMessageBox::No);
  110. int ret = msgBox.exec();
  111. if(ret == QMessageBox::Yes)
  112. gEditorApp().deleteProject(fullPath);
  113. else
  114. return;
  115. }
  116. QDir dir(projectDirPath);
  117. if(!dir.mkpath(projectDirPath))
  118. {
  119. QMessageBox msgBox;
  120. msgBox.setText("Error");
  121. msgBox.setInformativeText("Failed to create project directory.");
  122. return;
  123. }
  124. gEditorPrefs().setLastUsedProjectDirectory(projectDirPath);
  125. gEditorApp().createProject(projectDirPath, name);
  126. gEditorApp().loadProject(fullPath);
  127. accept();
  128. }
  129. void QtNewProject::browseProjectPath()
  130. {
  131. QFileDialog dialog(this, tr("Select project directory"));
  132. dialog.setFileMode(QFileDialog::Directory);
  133. dialog.setOption(QFileDialog::ShowDirsOnly, true);
  134. QDir lastUsedDir = gEditorPrefs().getLastUsedProjectDirectory();
  135. if(lastUsedDir.exists())
  136. dialog.setDirectory(lastUsedDir);
  137. if(dialog.exec())
  138. {
  139. QStringList fileNames = dialog.selectedFiles();
  140. if(fileNames.size() > 0)
  141. {
  142. QString dirPath = QString::fromStdString(FileSystem::getParentDirectory(fileNames[0].toStdString()));
  143. mTxtProjectPath->setText(dirPath);
  144. gEditorPrefs().setLastUsedProjectDirectory(dirPath);
  145. }
  146. }
  147. }
  148. QString QtNewProject::getProjectPath() const
  149. {
  150. QString name = mTxtProjectName->text();
  151. QString projectDirPath = mTxtProjectPath->text();
  152. return gEditorApp().getFullProjectPath(projectDirPath, name);
  153. }
  154. }