diff --git a/pyminer2/features/base.py b/pyminer2/features/base.py index 19b442078a7317cf796bb61c40617e0ecfa9196d..cc416844762ef4ac6ceb18e99a6526c867ce337e 100644 --- a/pyminer2/features/base.py +++ b/pyminer2/features/base.py @@ -6,13 +6,13 @@ import webbrowser from typing import Tuple, List import qdarkstyle -from qtpy.QtCore import QPoint, QRectF -from qtpy.QtGui import QMouseEvent, QPainter, QLinearGradient -from qtpy.QtGui import QCloseEvent -from qtpy.QtCore import Signal, Qt, QUrl, QPropertyAnimation -from qtpy.QtWidgets import QApplication, QListWidgetItem, QWizard, QHeaderView, QMessageBox -from qtpy.QtWebEngineWidgets import * -from qtpy.QtWidgets import QWidget, QDesktopWidget, QFileDialog, QApplication, QDialog +from PySide2.QtCore import QPoint, QRectF +from PySide2.QtGui import QMouseEvent, QPainter, QLinearGradient +from PySide2.QtGui import QCloseEvent +from PySide2.QtCore import Signal, Qt, QUrl, QPropertyAnimation +from PySide2.QtWidgets import QApplication, QListWidgetItem, QWizard, QHeaderView, QMessageBox +from PySide2.QtWebEngineWidgets import * +from PySide2.QtWidgets import QWidget, QDesktopWidget, QFileDialog, QApplication, QDialog from pmgwidgets import PMGPanel from pyminer2.ui.base.option import Ui_Form as Option_Ui_Form @@ -238,7 +238,7 @@ class ProjectWizardForm(QWizard, Project_Ui_Form): 新建项目引导窗口 """ - def __init__(self,parent=None): + def __init__(self, parent=None): super(ProjectWizardForm, self).__init__(parent=None) self.setupUi(self) self.center() @@ -257,23 +257,44 @@ class ProjectWizardForm(QWizard, Project_Ui_Form): self.toolButton.clicked.connect(self.getProjectDirectory) # 向导界面finish按钮按下后触发的事件 self.button(QWizard.FinishButton).clicked.connect(self.finishWizard) + # 向导界面next按钮按下后触发的事件 + self.button(QWizard.NextButton).clicked.connect(self.nextButtonCheck) # 项目名称框text发生改变时触发的事件 self.projectNameLineEdit.textChanged.connect(self.projectNameLineEditTextChange) # 选择不同项目类型时下方展示不同的类型描述 self.file_list.itemClicked.connect(self.fileListItemClicked) + # 自定义模板按钮按下后触发的事件 + self.customTemplate.clicked.connect(self.customTemplateClicked) def getProjectDirectory(self): """ 浏览按钮触发的事件,选择文件夹 :return: """ - directory_name = QFileDialog.getExistingDirectory(None, "请选择文件夹路径", "./") + absolute_directory = self.absoluteDirectoryEditLine.text() + project_directory = self.projectDirectoryEditLine.text() + directory_name = QFileDialog.getExistingDirectory(None, "请选择文件夹路径", "./").replace("/", "\\") self.projectDirectoryEditLine.setText(directory_name) project_name = self.projectNameLineEdit.text() - if project_name != "": - self.absoluteDirectoryEditLine.setText(directory_name + "/" + project_name) + if len(project_name) != 0: + if len(directory_name) != 0: + self.absoluteDirectoryEditLine.setText(directory_name + "\\" + project_name + '\\main.py') + self.projectDirectoryEditLine.setText(directory_name + "\\" + project_name) + else: + self.absoluteDirectoryEditLine.setText(absolute_directory) + self.projectDirectoryEditLine.setText(project_directory) + else: + if len(directory_name) != 0: + self.absoluteDirectoryEditLine.setText(directory_name + "\\" + project_name + '\\main.py') + self.projectDirectoryEditLine.setText(directory_name + "\\" + project_name) + else: + self.absoluteDirectoryEditLine.setText(absolute_directory) + self.projectDirectoryEditLine.setText(project_directory) + project_directory = self.projectDirectoryEditLine.text() + if project_directory != "" and os.path.exists(project_directory): + self.warningLabel.setText("警告:该项目已存在") else: - self.absoluteDirectoryEditLine.setText(directory_name) + self.warningLabel.setText("") def finishWizard(self): """ @@ -281,78 +302,85 @@ class ProjectWizardForm(QWizard, Project_Ui_Form): :return: """ import os + import pathlib file_path = self.absoluteDirectoryEditLine.text() - if os.path.exists(file_path): - print('文件已存在') # 创建空文件 + project_path = self.projectDirectoryEditLine.text() + current_project_type = self.file_list.item(self.file_list.currentRow()).text() + if os.path.exists(project_path): + # result = QMessageBox.warning(self, "警告", "该项目已存在,是否重新创建并覆盖?", QMessageBox.Yes, QMessageBox.No) + # if result == QMessageBox.Yes: + # print("Yes") + # else: + # print("No") + if current_project_type != "Python-Template-PySide2": + pathlib.Path(file_path).touch() # 创建空文件 + else: + from shutil import rmtree + rmtree(project_path) else: - current_project_type = self.file_list.item(self.file_list.currentRow()).text() - if current_project_type == "Python-Empty": # 创建空项目 + if current_project_type != "Python-Template-PySide2": + os.mkdir(project_path) + pathlib.Path(file_path).touch() + from shutil import copyfile + if current_project_type == "Python-Empty": # 创建空项目 + template_dir = "pyminer2/template/Empty-Template.py" + if os.path.exists(template_dir): + copyfile(template_dir, file_path) + else: # 若模板文件不存在,默认新建空main.py with open(file_path, "w") as f: - f.write('') - elif current_project_type == "Python-Template-Basic": # 创建base template项目 + f.write("") + elif current_project_type == "Python-Template-Basic": # 创建base template项目 + template_dir = "pyminer2/template/Basic-Template.py" + if os.path.exists(template_dir): + copyfile(template_dir, file_path) + else: # 若模板文件不存在,默认将以下内容写入main.py with open(file_path, "w") as f: - f.write("""#--coding:utf-8-- - - if __name__ == '__main__': - # Create your codes here - """) - elif current_project_type == "Python-Template-Plot": # 创建plot template项目 + f.write("# --coding:utf-8--\n") + f.write("if __name__ == '__main__':\n") + f.write(" # Create your codes here") + f.write(" pass") + elif current_project_type == "Python-Template-Plot": # 创建plot template项目 + template_dir = "pyminer2/template/Plot-Template.py" + if os.path.exists(template_dir): + copyfile(template_dir, file_path) + else: # 若模板文件不存在,默认将以下内容写入main.py with open(file_path, "w") as f: - f.write("""#--coding:utf-8-- - import matplotlib.pyplot as plt - import numpy as np - - def demoTemplate(): - x = np.linspace(0,5,200) - y1 = x + 1 - y2 = x -1 - plt.figure() - #### 去边框 - ax = plt.axes() - ax.spines['top'].set_visible(False) - ax.spines['right'].set_visible(False) - #### 关闭坐标轴//就剩下中间的绘图区,坐标轴连带标签都去掉了 - #### 网格设置 - plt.grid(axis="both",linestyle='-.',c='b') - #### 绘图 - plt.plot(x,y1,'c--') - plt.plot(x,y2,'r-.') - plt.text(1,0.5,"text") - #### legend - plt.legend(["y1","y2"]) - #### 标签 - plt.xlabel("xlabel") - plt.ylabel("ylabel") - plt.title("title") - plt.show() - - if __name__ == '__main__': - demoTemplate() - """) - elif current_project_type == "Python-Template-PyQt": # 创建pyqt template项目 - with open(file_path, "w") as f: - f.write("""#--coding:utf-8-- - import sys - from PyQt5.QtWidgets import QApplication, QDialog - - from pyqt_template import Ui_PyQtTemplate - - - class CallPyQtTemplate(QDialog, Ui_PyQtTemplate): - def __init__(self): - super(CallPyQtTemplate, self).__init__() - self.setupUi(self) - - - if __name__ == '__main__': - app = QApplication(sys.argv) - form = CallPyQtTemplate() - form.show() - sys.exit(app.exec()) - """) - project_path=self.projectDirectoryEditLine.text() - extension_lib.Program.set_work_dir(project_path) # 在文件树区域打开新建项目 - + f.write("# --coding:utf-8--\n") + f.write("\n") + f.write("import matplotlib.pyplot as plt\n") + f.write("import numpy as np\n") + f.write("\n") + f.write("\n") + f.write("def demoTemplate():\n") + f.write(" x = np.linspace(0, 5, 200)\n") + f.write(" y1 = x + 1\n") + f.write(" y2 = x - 1\n") + f.write(" plt.figure()\n") + f.write(" ax = plt.axes()\n") + f.write(" ax.spines['top'].set_visible(False)\n") + f.write(" ax.spines['right'].set_visible(False)\n") + f.write(" plt.grid(axis='both', linestyle='-.', c='b')\n") + f.write(" plt.plot(x, y1, 'c--')\n") + f.write(" plt.plot(x, y2, 'r-.')\n") + f.write(" plt.text(1, 0.5, 'text')\n") + f.write(" plt.legend(['y1', 'y2'])\n") + f.write(" plt.xlabel('xlabel')\n") + f.write(" plt.ylabel('ylabel')\n") + f.write(" plt.title('title')\n") + f.write(" plt.show()\n") + f.write("\n") + f.write("\n") + f.write("if __name__ == '__main__':\n") + f.write(" demoTemplate()\n") + elif current_project_type == "Python-Template-PySide2": # 创建pyqt template项目 + template_dir = "pyminer2/template/PySide2Template" + if os.path.exists(template_dir): + from shutil import copytree + copytree(template_dir, project_path) + else: # 若模板文件不存在,默认将以下内容写入main.py + QMessageBox.warning(self, "警告", "模板路径不存在,请确保模板在程序根目录下的pyminer2/template/PySide2Template", + QMessageBox.Ok) + extension_lib.Program.set_work_dir(project_path) # 在文件树区域打开新建项目,将当前工作路径切换为新建的项目 def projectNameLineEditTextChange(self): """ @@ -363,11 +391,20 @@ class ProjectWizardForm(QWizard, Project_Ui_Form): """ project_name = self.projectNameLineEdit.text() absolute_directory = self.absoluteDirectoryEditLine.text() - # 将文件路径按照/分割成列表,然后把最后一个元素也就是项目名称pop()移出列表,最后再拼接成完整的路径 - absolute_directory_list = absolute_directory.split("/") - absolute_directory_list.pop() + # 将文件路径按照\分割成列表,然后把右边2个元素也就是main.py与项目名称pop()移出列表,最后再拼接成完整的路径 + absolute_directory_list = absolute_directory.split("\\") + absolute_directory_list.pop() # 移除最右边的元素"main.py" + absolute_directory_list.pop() # 移除右边的项目名称元素 if absolute_directory != "": - self.absoluteDirectoryEditLine.setText("/".join(absolute_directory_list) + "/" + project_name) + # 将新项目名称和main.py与浏览按钮选择的路径进行拼接组合成新的绝对路径和项目路径 + self.projectDirectoryEditLine.setText("\\".join(absolute_directory_list) + "\\" + project_name) + self.absoluteDirectoryEditLine.setText("\\".join(absolute_directory_list) + "\\" + project_name + + "\\main.py") + project_dir = self.projectDirectoryEditLine.text() + if os.path.exists(project_dir): + self.warningLabel.setText("警告:该项目已存在") + else: + self.warningLabel.setText("") def fileListItemClicked(self): current_project_type = self.file_list.item(self.file_list.currentRow()).text() @@ -377,8 +414,18 @@ class ProjectWizardForm(QWizard, Project_Ui_Form): self.plainTextEdit.setPlainText("Create a Python Project containing a Base Template main.py.") elif current_project_type == "Python-Template-Plot": self.plainTextEdit.setPlainText("Create a Python Project containing a Plot Template main.py.") - elif current_project_type == "Python-Template-PyQt": - self.plainTextEdit.setPlainText("Create a Python Project containing a PyQt Template main.py.") + elif current_project_type == "Python-Template-PySide2": + self.plainTextEdit.setPlainText("Create a Python Project containing a PySide2 Template main.py.") + + def nextButtonCheck(self): + """ + 进行项目类型选择控制,必须选中一行才能进行下一步 + """ + # QMessageBox.warning(self, "警告", "请选择一行") + pass + + def customTemplateClicked(self): + pass def keyPressEvent(self, e): """ diff --git a/pyminer2/template/Basic-Template.py b/pyminer2/template/Basic-Template.py new file mode 100644 index 0000000000000000000000000000000000000000..6788f5a14368d20291240c5fc7836cb2276b8c8f --- /dev/null +++ b/pyminer2/template/Basic-Template.py @@ -0,0 +1,14 @@ +# --coding:utf-8-- + +""" +Please make sure the content of the file is complete. +If you customize the template, please make sure it can be executed correctly +by the interpreter after modification. +Do not delete the file and the directory where the file is located. +请确保文件内容完整。 +若自定义模板,请在修改后确保能够被解释器正确执行。 +【请勿删除】该文件以及文件所在目录。 +""" +if __name__ == '__main__': + # Create your codes here + pass diff --git a/pyminer2/template/Empty-Template.py b/pyminer2/template/Empty-Template.py new file mode 100644 index 0000000000000000000000000000000000000000..7532010d52681088bc245077b16225b06a1310df --- /dev/null +++ b/pyminer2/template/Empty-Template.py @@ -0,0 +1,9 @@ +""" +Please make sure the content of the file is complete. +If you customize the template, please make sure it can be executed correctly +by the interpreter after modification. +Do not delete the file and the directory where the file is located. +请确保文件内容完整。 +若自定义模板,请在修改后确保能够被解释器正确执行。 +【请勿删除】该文件以及文件所在目录 +""" \ No newline at end of file diff --git a/pyminer2/template/Plot-Template.py b/pyminer2/template/Plot-Template.py new file mode 100644 index 0000000000000000000000000000000000000000..000c5ab169cd4daba858ea52e011046d54880bf2 --- /dev/null +++ b/pyminer2/template/Plot-Template.py @@ -0,0 +1,36 @@ +# --coding:utf-8-- + +""" +Please make sure the content of the file is complete. +If you customize the template, please make sure it can be executed correctly +by the interpreter after modification. +Do not delete the file and the directory where the file is located. +请确保文件内容完整。 +若自定义模板,请在修改后确保能够被解释器正确执行。 +【请勿删除】该文件以及文件所在目录 +""" +import matplotlib.pyplot as plt +import numpy as np + + +def demoTemplate(): + x = np.linspace(0, 5, 200) + y1 = x + 1 + y2 = x - 1 + plt.figure() + ax = plt.axes() + ax.spines['top'].set_visible(False) + ax.spines['right'].set_visible(False) + plt.grid(axis="both", linestyle='-.', c='b') + plt.plot(x, y1, 'c--') + plt.plot(x, y2, 'r-.') + plt.text(1, 0.5, "text") + plt.legend(["y1", "y2"]) + plt.xlabel("xlabel") + plt.ylabel("ylabel") + plt.title("title") + plt.show() + + +if __name__ == '__main__': + demoTemplate() diff --git a/pyminer2/template/PyQt-Template.py b/pyminer2/template/PyQt-Template.py new file mode 100644 index 0000000000000000000000000000000000000000..e920801d5bd7eb0671ca452af912570e5a3756be --- /dev/null +++ b/pyminer2/template/PyQt-Template.py @@ -0,0 +1,16 @@ +# --coding:utf-8-- +import sys +from PySide2.QtWidgets import QApplication, QDialog + + +class CallPyQtTemplate(object): + def __init__(self): + super(CallPyQtTemplate, self).__init__() + self.setupUi(self) + + +if __name__ == '__main__': + app = QApplication(sys.argv) + form = CallPyQtTemplate() + form.show() + sys.exit(app.exec()) diff --git a/pyminer2/template/PySide2Template/PySide2_Template.py b/pyminer2/template/PySide2Template/PySide2_Template.py new file mode 100644 index 0000000000000000000000000000000000000000..7b9215ba81393a15168c1a1d728e3bf1b07916e6 --- /dev/null +++ b/pyminer2/template/PySide2Template/PySide2_Template.py @@ -0,0 +1,49 @@ +# -*- coding: utf-8 -*- + +################################################################################ +## Form generated from reading UI file 'PySide2_Template.ui' +## +## Created by: Qt User Interface Compiler version 5.15.2 +## +## WARNING! All changes made in this file will be lost when recompiling UI file! +################################################################################ + +from PySide2.QtCore import * +from PySide2.QtGui import * +from PySide2.QtWidgets import * + + +class Ui_PySide2Template(object): + def setupUi(self, PySide2Template): + if not PySide2Template.objectName(): + PySide2Template.setObjectName(u"PySide2Template") + PySide2Template.resize(402, 307) + icon = QIcon() + icon.addFile(u"../../ui/source/icons/logo.ico", QSize(), QIcon.Normal, QIcon.Off) + PySide2Template.setWindowIcon(icon) + self.verticalLayoutWidget = QWidget(PySide2Template) + self.verticalLayoutWidget.setObjectName(u"verticalLayoutWidget") + self.verticalLayoutWidget.setGeometry(QRect(9, 19, 381, 281)) + self.verticalLayout = QVBoxLayout(self.verticalLayoutWidget) + self.verticalLayout.setObjectName(u"verticalLayout") + self.verticalLayout.setContentsMargins(0, 0, 0, 0) + self.textBrowser = QTextBrowser(self.verticalLayoutWidget) + self.textBrowser.setObjectName(u"textBrowser") + + self.verticalLayout.addWidget(self.textBrowser) + + + self.retranslateUi(PySide2Template) + + QMetaObject.connectSlotsByName(PySide2Template) + # setupUi + + def retranslateUi(self, PySide2Template): + PySide2Template.setWindowTitle(QCoreApplication.translate("PySide2Template", u"PySide2Template", None)) + self.textBrowser.setHtml(QCoreApplication.translate("PySide2Template", u"\n" +"
\n" +"This is a PySide2 Template.
", None)) + # retranslateUi + diff --git a/pyminer2/template/PySide2Template/PySide2_Template.ui b/pyminer2/template/PySide2Template/PySide2_Template.ui new file mode 100644 index 0000000000000000000000000000000000000000..40ee0698acdade5e1ef9fb3c76b26505fc646e63 --- /dev/null +++ b/pyminer2/template/PySide2Template/PySide2_Template.ui @@ -0,0 +1,46 @@ + +