NewProject.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. * Copyright (c) 2011-2018 by Andrew Mustun. All rights reserved.
  3. *
  4. * This file is part of the QCAD project.
  5. *
  6. * QCAD is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * QCAD is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with QCAD.
  18. */
  19. include("scripts/File/File.js");
  20. include("scripts/File/Save/Save.js");
  21. include("scripts/Widgets/ViewportWidget/ViewportWidget.js");
  22. include("scripts/File/AutoSave/AutoSave.js");
  23. include("scripts/Reset/Reset.js");
  24. /**
  25. * \class NewFile
  26. * \brief Creates a new document.
  27. * \ingroup ecma_file
  28. */
  29. function NewProject(guiAction) {
  30. File.call(this, guiAction);
  31. }
  32. NewProject.prototype = new File();
  33. NewProject.includeBasePath = includeBasePath;
  34. /**
  35. * \param isOpen True if new is called from a file open function.
  36. */
  37. NewProject.prototype.beginEvent = function() {
  38. var appWin = EAction.getMainWindow();
  39. var dialog = WidgetFactory.createDialog(NewProject.includeBasePath, "NewProjectDialog.ui");
  40. var label1 = dialog.findChild("Text1");
  41. var label2 = dialog.findChild("Text2");
  42. var fileBtn = dialog.findChild("fileBtn");
  43. // 选择文件夹
  44. fileBtn.clicked.connect(function(){
  45. var appWin = EAction.getMainWindow();
  46. var fileDialog = new QFileDialog(appWin);
  47. fileDialog.fileMode = QFileDialog.Directory;
  48. if (!fileDialog.exec()) {
  49. destr(fileDialog);
  50. EAction.activateMainWindow();
  51. return;
  52. }
  53. var selectDir = fileDialog.selectedFiles();
  54. label2.setText(selectDir[0]);
  55. });
  56. // 1、首先检查路径是否存在,如果不存在提示修改路径
  57. // 2、检查文件夹下是否已经存在项目名称的文件夹,如果存在提示修改名称
  58. // 3、验证通过后,创建文件夹,关闭对话框,进入项目主界面。
  59. var buttonBox = dialog.findChild("ButtonBox");
  60. buttonBox.clicked.connect(function(button) {
  61. var standardButton = buttonBox.standardButton(button);
  62. switch (standardButton) {
  63. // cancel:
  64. case QDialogButtonBox.Cancel:
  65. dialog.reject();
  66. break;
  67. // save:
  68. case QDialogButtonBox.Save:
  69. var dirTxt = label2.text;
  70. var pjText = label1.text;
  71. // EAction.handleUserMessage(label1.text);
  72. if(dirTxt == ""){
  73. QMessageBox.critical(appWin,qsTr("错误"),qsTr("请选择或输入工程路径!"));
  74. break;
  75. }else if(pjText == ""){
  76. QMessageBox.critical(appWin,qsTr("错误"),qsTr("请输入工程名称!"));
  77. break;
  78. }
  79. var dir = new QDir(dirTxt);
  80. if(!dir.exists()){
  81. QMessageBox.critical(appWin,qsTr("错误"),qsTr("工程路径找不到,请选择或输入正确的路径!"));
  82. break;
  83. }
  84. var pjPath = dirTxt + "/" + pjText;
  85. var path = new QDir(pjPath);
  86. if(path.exists())
  87. {
  88. QMessageBox.critical(appWin,qsTr("错误"),qsTr(pjText + "工程已存在,请重新输入工程名称!"));
  89. break;
  90. }
  91. var ismkdir = dir.mkdir(pjText);
  92. if(!ismkdir)
  93. {
  94. EAction.handleUserMessage("Create project fail");
  95. }
  96. else
  97. {
  98. EAction.handleUserMessage("Create project success");
  99. // 拷贝参数配置文件
  100. var srcFilePath = NewProject.includeBasePath + "/para.ini";
  101. var dstFilePath = pjPath + "/para.ini";
  102. QFile.copy(srcFilePath, dstFilePath);
  103. var buttons = makeQMessageBoxStandardButtons(QMessageBox.Yes, QMessageBox.No);
  104. var ret = QMessageBox.warning(null,
  105. qsTr("提示"),
  106. qsTr("\"%1\" 工程创建成功. \n "+
  107. "是否加载新的工程?").arg(pjText), buttons);
  108. if (ret===QMessageBox.Yes) {
  109. RSettings.setValue("OpenProject/Current", pjPath);
  110. }
  111. }
  112. if (dialog.exec()) {
  113. }
  114. else {
  115. }
  116. destr(dialog);
  117. break;
  118. }
  119. });
  120. if (dialog.exec()) {
  121. }
  122. else {
  123. }
  124. destr(dialog);
  125. EAction.activateMainWindow();
  126. };
  127. // 弹出选择文件夹对话框
  128. NewProject.prototype.selectDir = function() {
  129. var appWin = EAction.getMainWindow();
  130. var fileDialog = new QFileDialog(appWin);
  131. fileDialog.fileMode = QFileDialog.Directory;
  132. if (!fileDialog.exec()) {
  133. destr(fileDialog);
  134. EAction.activateMainWindow();
  135. return;
  136. }
  137. var selectDir = fileDialog.selectedFiles();
  138. EAction.handleUserMessage("Starting to load file: " + selectDir[0]);
  139. return selectDir[0];
  140. };