From 49d09217f1171dd9060ee41f17b3bfb5f0aaf778 Mon Sep 17 00:00:00 2001 From: tianyaaz Date: Sat, 6 Feb 2021 22:43:36 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=96=B0=E5=BB=BA=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E6=97=B6=E7=94=A8=E6=A8=A1=E6=9D=BF=E6=96=B0=E5=BB=BA=EF=BC=8C?= =?UTF-8?q?=E6=A8=A1=E6=9D=BF=E4=B8=8D=E5=AD=98=E5=9C=A8=E6=98=AF=E4=BB=A5?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E6=96=87=E4=BB=B6=E6=96=B0=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pyminer2/features/base.py | 171 +++++++++++++++----------- pyminer2/template/Basic-Template.py | 14 +++ pyminer2/template/Empty-Template.py | 9 ++ pyminer2/template/Plot-Template.py | 36 ++++++ pyminer2/template/PyQt-Template.py | 16 +++ pyminer2/template/PySide2-Template.py | 46 +++++++ pyminer2/template/PySide2-Template.ui | 42 +++++++ pyminer2/ui/base/project_wizard.py | 20 ++- pyminer2/ui/base/project_wizard.ui | 28 ++++- 9 files changed, 302 insertions(+), 80 deletions(-) create mode 100644 pyminer2/template/Basic-Template.py create mode 100644 pyminer2/template/Empty-Template.py create mode 100644 pyminer2/template/Plot-Template.py create mode 100644 pyminer2/template/PyQt-Template.py create mode 100644 pyminer2/template/PySide2-Template.py create mode 100644 pyminer2/template/PySide2-Template.ui diff --git a/pyminer2/features/base.py b/pyminer2/features/base.py index 56c10358..cc2f90b7 100644 --- a/pyminer2/features/base.py +++ b/pyminer2/features/base.py @@ -255,19 +255,33 @@ class ProjectWizardForm(QWizard, Project_Ui_Form): 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: - self.absoluteDirectoryEditLine.setText(directory_name) + 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) def finishWizard(self): """ @@ -275,78 +289,78 @@ 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() + if os.path.exists(project_path): + pathlib.Path(file_path).touch() # 创建空文件 else: + os.mkdir(project_path) current_project_type = self.file_list.item(self.file_list.currentRow()).text() - if current_project_type == "Python-Empty": # 创建空项目 + pathlib.Path(file_path).touch() + from shutil import copyfile + if current_project_type == "Python-Empty": # 创建空项目 + template_dir = "pyminer2/template/Empty-Template.py" + print(1111111) + print(os.path.exists(template_dir)) + 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("\t\t\t\t# Create your codes here") + f.write("\t\t\t\tpass") + 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项目 + 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("\t\t\t\tx = np.linspace(0, 5, 200)\n") + f.write("\t\t\t\ty1 = x + 1\n") + f.write("\t\t\t\ty2 = x - 1\n") + f.write("\t\t\t\tplt.figure()\n") + f.write("\t\t\t\tax = plt.axes()\n") + f.write("\t\t\t\tax.spines['top'].set_visible(False)\n") + f.write("\t\t\t\tax.spines['right'].set_visible(False)\n") + f.write("\t\t\t\tplt.grid(axis='both', linestyle='-.', c='b')\n") + f.write("\t\t\t\tplt.plot(x, y1, 'c--')\n") + f.write("\t\t\t\tplt.plot(x, y2, 'r-.')\n") + f.write("\t\t\t\tplt.text(1, 0.5, 'text')\n") + f.write("\t\t\t\tplt.legend(['y1', 'y2'])\n") + f.write("\t\t\t\tplt.xlabel('xlabel)\n") + f.write("\t\t\t\tplt.ylabel('ylabel')\n") + f.write("\t\t\t\tplt.title('title')\n") + f.write("\t\t\t\tplt.show()\n") + f.write("\n") + f.write("\n") + f.write("if __name__ == '__main__':\n") + f.write("\t\t\t\tdemoTemplate()\n") + elif current_project_type == "Python-Template-PyQt": # 创建pyqt 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 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() + project_path = self.projectDirectoryEditLine.text() + print(project_path) + extension_lib.Program.set_work_dir(project_path) # 在文件树区域打开新建项目 def projectNameLineEditTextChange(self): """ @@ -357,11 +371,15 @@ 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") def fileListItemClicked(self): current_project_type = self.file_list.item(self.file_list.currentRow()).text() @@ -371,8 +389,11 @@ 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 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 00000000..6788f5a1 --- /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 00000000..7532010d --- /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 00000000..000c5ab1 --- /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 00000000..e920801d --- /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/PySide2-Template.py b/pyminer2/template/PySide2-Template.py new file mode 100644 index 00000000..5126c251 --- /dev/null +++ b/pyminer2/template/PySide2-Template.py @@ -0,0 +1,46 @@ +# -*- 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_Form(object): + def setupUi(self, Form): + if not Form.objectName(): + Form.setObjectName(u"Form") + Form.resize(402, 307) + self.verticalLayoutWidget = QWidget(Form) + 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(Form) + + QMetaObject.connectSlotsByName(Form) + # setupUi + + def retranslateUi(self, Form): + Form.setWindowTitle(QCoreApplication.translate("Form", u"Form", None)) + self.textBrowser.setHtml(QCoreApplication.translate("Form", u"\n" +"\n" +"

This is a PySide2 Template.

", None)) + # retranslateUi + diff --git a/pyminer2/template/PySide2-Template.ui b/pyminer2/template/PySide2-Template.ui new file mode 100644 index 00000000..dd617f5f --- /dev/null +++ b/pyminer2/template/PySide2-Template.ui @@ -0,0 +1,42 @@ + + + Form + + + + 0 + 0 + 402 + 307 + + + + Form + + + + + 9 + 19 + 381 + 281 + + + + + + + <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> +<html><head><meta name="qrichtext" content="1" /><style type="text/css"> +p, li { white-space: pre-wrap; } +</style></head><body style=" font-family:'SimSun'; font-size:9pt; font-weight:400; font-style:normal;"> +<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:14pt; font-weight:600; color:#ff0000;">This is a PySide2 Template.</span></p></body></html> + + + + + + + + + diff --git a/pyminer2/ui/base/project_wizard.py b/pyminer2/ui/base/project_wizard.py index 8a51090c..7e68015c 100644 --- a/pyminer2/ui/base/project_wizard.py +++ b/pyminer2/ui/base/project_wizard.py @@ -12,7 +12,6 @@ from PySide2.QtCore import * from PySide2.QtGui import * from PySide2.QtWidgets import * -import pyqtsource_rc class Ui_Wizard(object): def setupUi(self, Wizard): @@ -161,6 +160,20 @@ class Ui_Wizard(object): self.verticalLayout_4.addWidget(self.plainTextEdit) + self.horizontalLayout_8 = QHBoxLayout() + self.horizontalLayout_8.setObjectName(u"horizontalLayout_8") + self.horizontalSpacer = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum) + + self.horizontalLayout_8.addItem(self.horizontalSpacer) + + self.customTemplate = QPushButton(self.widget_right) + self.customTemplate.setObjectName(u"customTemplate") + + self.horizontalLayout_8.addWidget(self.customTemplate) + + + self.verticalLayout_4.addLayout(self.horizontalLayout_8) + self.horizontalLayout.addWidget(self.widget_right) @@ -238,7 +251,7 @@ class Ui_Wizard(object): self.projectNameLineEdit = QLineEdit(self.widget_right_2) self.projectNameLineEdit.setObjectName(u"projectNameLineEdit") - self.projectNameLineEdit.setClearButtonEnabled(True) + self.projectNameLineEdit.setClearButtonEnabled(False) self.formLayout_2.setWidget(0, QFormLayout.FieldRole, self.projectNameLineEdit) @@ -323,10 +336,11 @@ class Ui_Wizard(object): ___qlistwidgetitem4 = self.file_list.item(2) ___qlistwidgetitem4.setText(QCoreApplication.translate("Wizard", u"Python-Template-Plot", None)); ___qlistwidgetitem5 = self.file_list.item(3) - ___qlistwidgetitem5.setText(QCoreApplication.translate("Wizard", u"Python-Template-PyQt", None)); + ___qlistwidgetitem5.setText(QCoreApplication.translate("Wizard", u"Python-Template-PySide2", None)); self.file_list.setSortingEnabled(__sortingEnabled2) self.plainTextEdit.setPlainText(QCoreApplication.translate("Wizard", u"Create a Python Project containing an empty main.py file.", None)) + self.customTemplate.setText(QCoreApplication.translate("Wizard", u"\u81ea\u5b9a\u4e49\u6a21\u677f", None)) self.label_5.setText(QCoreApplication.translate("Wizard", u"Steps", None)) __sortingEnabled3 = self.listWidget_2.isSortingEnabled() diff --git a/pyminer2/ui/base/project_wizard.ui b/pyminer2/ui/base/project_wizard.ui index 284c55bc..bff94ee5 100644 --- a/pyminer2/ui/base/project_wizard.ui +++ b/pyminer2/ui/base/project_wizard.ui @@ -283,7 +283,7 @@ - Python-Template-PyQt + Python-Template-PySide2 @@ -316,6 +316,30 @@ + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + 自定义模板 + + + + + @@ -448,7 +472,7 @@ PyMinerProject - true + false -- Gitee From ebbb0322c040f2fd069f6588911d97c7e875f8e8 Mon Sep 17 00:00:00 2001 From: tianyaaz Date: Wed, 10 Feb 2021 23:12:31 +0800 Subject: [PATCH 2/2] =?UTF-8?q?pyminer2=E7=9B=AE=E5=BD=95=E4=B8=8B?= =?UTF-8?q?=E6=96=B0=E5=A2=9Etemplate=E6=A8=A1=E6=9D=BF=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=EF=BC=8C=E9=87=8C=E9=9D=A2=E5=AD=98=E6=94=BE=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=EF=BC=8C=E5=8F=AF=E4=BB=A5=E8=87=AA=E5=AE=9A=E4=B9=89=EF=BC=8C?= =?UTF-8?q?=E4=BD=86=E4=B8=8D=E8=83=BD=E5=88=A0=E9=99=A4=E3=80=82=20?= =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=96=B0=E5=BB=BA=E9=A1=B9=E7=9B=AE=EF=BC=8C?= =?UTF-8?q?=E5=B0=86pyqt=E5=88=87=E6=8D=A2=E4=B8=BApyside2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pyminer2/features/base.py | 108 ++++++++++------- .../PySide2_Template.py} | 27 +++-- .../PySide2_Template.ui} | 10 +- pyminer2/template/PySide2Template/__init__.py | 0 pyminer2/template/PySide2Template/main.py | 26 ++++ pyminer2/template/__init__.py | 0 pyminer2/ui/base/project_wizard.py | 68 +++++------ pyminer2/ui/base/project_wizard.ui | 112 +++++++++--------- 8 files changed, 202 insertions(+), 149 deletions(-) rename pyminer2/template/{PySide2-Template.py => PySide2Template/PySide2_Template.py} (60%) rename pyminer2/template/{PySide2-Template.ui => PySide2Template/PySide2_Template.ui} (84%) create mode 100644 pyminer2/template/PySide2Template/__init__.py create mode 100644 pyminer2/template/PySide2Template/main.py create mode 100644 pyminer2/template/__init__.py diff --git a/pyminer2/features/base.py b/pyminer2/features/base.py index 80bfb9f8..cc416844 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,6 +257,8 @@ 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) # 选择不同项目类型时下方展示不同的类型描述 @@ -288,6 +290,11 @@ class ProjectWizardForm(QWizard, Project_Ui_Form): 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.warningLabel.setText("") def finishWizard(self): """ @@ -298,17 +305,25 @@ class ProjectWizardForm(QWizard, Project_Ui_Form): import pathlib file_path = self.absoluteDirectoryEditLine.text() project_path = self.projectDirectoryEditLine.text() + current_project_type = self.file_list.item(self.file_list.currentRow()).text() if os.path.exists(project_path): - pathlib.Path(file_path).touch() # 创建空文件 + # 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: - os.mkdir(project_path) - current_project_type = self.file_list.item(self.file_list.currentRow()).text() - pathlib.Path(file_path).touch() + 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" - print(1111111) - print(os.path.exists(template_dir)) if os.path.exists(template_dir): copyfile(template_dir, file_path) else: # 若模板文件不存在,默认新建空main.py @@ -322,8 +337,8 @@ class ProjectWizardForm(QWizard, Project_Ui_Form): with open(file_path, "w") as f: f.write("# --coding:utf-8--\n") f.write("if __name__ == '__main__':\n") - f.write("\t\t\t\t# Create your codes here") - f.write("\t\t\t\tpass") + 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): @@ -337,36 +352,35 @@ class ProjectWizardForm(QWizard, Project_Ui_Form): f.write("\n") f.write("\n") f.write("def demoTemplate():\n") - f.write("\t\t\t\tx = np.linspace(0, 5, 200)\n") - f.write("\t\t\t\ty1 = x + 1\n") - f.write("\t\t\t\ty2 = x - 1\n") - f.write("\t\t\t\tplt.figure()\n") - f.write("\t\t\t\tax = plt.axes()\n") - f.write("\t\t\t\tax.spines['top'].set_visible(False)\n") - f.write("\t\t\t\tax.spines['right'].set_visible(False)\n") - f.write("\t\t\t\tplt.grid(axis='both', linestyle='-.', c='b')\n") - f.write("\t\t\t\tplt.plot(x, y1, 'c--')\n") - f.write("\t\t\t\tplt.plot(x, y2, 'r-.')\n") - f.write("\t\t\t\tplt.text(1, 0.5, 'text')\n") - f.write("\t\t\t\tplt.legend(['y1', 'y2'])\n") - f.write("\t\t\t\tplt.xlabel('xlabel)\n") - f.write("\t\t\t\tplt.ylabel('ylabel')\n") - f.write("\t\t\t\tplt.title('title')\n") - f.write("\t\t\t\tplt.show()\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("\t\t\t\tdemoTemplate()\n") - elif current_project_type == "Python-Template-PyQt": # 创建pyqt template项目 - template_dir = "pyminer2/template/Plot-Template.py" + f.write(" demoTemplate()\n") + elif current_project_type == "Python-Template-PySide2": # 创建pyqt template项目 + template_dir = "pyminer2/template/PySide2Template" if os.path.exists(template_dir): - copyfile(template_dir, file_path) + from shutil import copytree + copytree(template_dir, project_path) else: # 若模板文件不存在,默认将以下内容写入main.py - with open(file_path, "w") as f: - f.write() - project_path = self.projectDirectoryEditLine.text() - print(project_path) - extension_lib.Program.set_work_dir(project_path) # 在文件树区域打开新建项目 + QMessageBox.warning(self, "警告", "模板路径不存在,请确保模板在程序根目录下的pyminer2/template/PySide2Template", + QMessageBox.Ok) + extension_lib.Program.set_work_dir(project_path) # 在文件树区域打开新建项目,将当前工作路径切换为新建的项目 def projectNameLineEditTextChange(self): """ @@ -386,6 +400,11 @@ class ProjectWizardForm(QWizard, Project_Ui_Form): 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() @@ -398,6 +417,13 @@ class ProjectWizardForm(QWizard, Project_Ui_Form): 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 diff --git a/pyminer2/template/PySide2-Template.py b/pyminer2/template/PySide2Template/PySide2_Template.py similarity index 60% rename from pyminer2/template/PySide2-Template.py rename to pyminer2/template/PySide2Template/PySide2_Template.py index 5126c251..7b9215ba 100644 --- a/pyminer2/template/PySide2-Template.py +++ b/pyminer2/template/PySide2Template/PySide2_Template.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- ################################################################################ -## Form generated from reading UI file 'PySide2-Template.ui' +## Form generated from reading UI file 'PySide2_Template.ui' ## ## Created by: Qt User Interface Compiler version 5.15.2 ## @@ -13,12 +13,15 @@ from PySide2.QtGui import * from PySide2.QtWidgets import * -class Ui_Form(object): - def setupUi(self, Form): - if not Form.objectName(): - Form.setObjectName(u"Form") - Form.resize(402, 307) - self.verticalLayoutWidget = QWidget(Form) +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) @@ -30,14 +33,14 @@ class Ui_Form(object): self.verticalLayout.addWidget(self.textBrowser) - self.retranslateUi(Form) + self.retranslateUi(PySide2Template) - QMetaObject.connectSlotsByName(Form) + QMetaObject.connectSlotsByName(PySide2Template) # setupUi - def retranslateUi(self, Form): - Form.setWindowTitle(QCoreApplication.translate("Form", u"Form", None)) - self.textBrowser.setHtml(QCoreApplication.translate("Form", u"\n" + def retranslateUi(self, PySide2Template): + PySide2Template.setWindowTitle(QCoreApplication.translate("PySide2Template", u"PySide2Template", None)) + self.textBrowser.setHtml(QCoreApplication.translate("PySide2Template", u"\n" "\n" diff --git a/pyminer2/template/PySide2-Template.ui b/pyminer2/template/PySide2Template/PySide2_Template.ui similarity index 84% rename from pyminer2/template/PySide2-Template.ui rename to pyminer2/template/PySide2Template/PySide2_Template.ui index dd617f5f..40ee0698 100644 --- a/pyminer2/template/PySide2-Template.ui +++ b/pyminer2/template/PySide2Template/PySide2_Template.ui @@ -1,7 +1,7 @@ - Form - + PySide2Template + 0 @@ -11,7 +11,11 @@ - Form + PySide2Template + + + + ../../ui/source/icons/logo.ico../../ui/source/icons/logo.ico diff --git a/pyminer2/template/PySide2Template/__init__.py b/pyminer2/template/PySide2Template/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/pyminer2/template/PySide2Template/main.py b/pyminer2/template/PySide2Template/main.py new file mode 100644 index 00000000..6c1c6520 --- /dev/null +++ b/pyminer2/template/PySide2Template/main.py @@ -0,0 +1,26 @@ +# --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 sys +from PySide2.QtWidgets import QApplication, QDialog +from PySide2_Template import Ui_PySide2Template + + +class CallPySideTemplate(QDialog, Ui_PySide2Template): + def __init__(self): + super(CallPySideTemplate, self).__init__() + self.setupUi(self) + + +if __name__ == '__main__': + app = QApplication(sys.argv) + form = CallPySideTemplate() + form.show() + sys.exit(app.exec_()) diff --git a/pyminer2/template/__init__.py b/pyminer2/template/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/pyminer2/ui/base/project_wizard.py b/pyminer2/ui/base/project_wizard.py index 7e68015c..aae264a1 100644 --- a/pyminer2/ui/base/project_wizard.py +++ b/pyminer2/ui/base/project_wizard.py @@ -12,6 +12,7 @@ from PySide2.QtCore import * from PySide2.QtGui import * from PySide2.QtWidgets import * +import pyqtsource_rc class Ui_Wizard(object): def setupUi(self, Wizard): @@ -19,6 +20,7 @@ class Ui_Wizard(object): Wizard.setObjectName(u"Wizard") Wizard.resize(736, 505) Wizard.setWizardStyle(QWizard.ModernStyle) + Wizard.setOptions(QWizard.HelpButtonOnRight|QWizard.NoBackButtonOnStartPage) self.wizardPage1 = QWizardPage() self.wizardPage1.setObjectName(u"wizardPage1") self.horizontalLayout = QHBoxLayout(self.wizardPage1) @@ -160,20 +162,6 @@ class Ui_Wizard(object): self.verticalLayout_4.addWidget(self.plainTextEdit) - self.horizontalLayout_8 = QHBoxLayout() - self.horizontalLayout_8.setObjectName(u"horizontalLayout_8") - self.horizontalSpacer = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum) - - self.horizontalLayout_8.addItem(self.horizontalSpacer) - - self.customTemplate = QPushButton(self.widget_right) - self.customTemplate.setObjectName(u"customTemplate") - - self.horizontalLayout_8.addWidget(self.customTemplate) - - - self.verticalLayout_4.addLayout(self.horizontalLayout_8) - self.horizontalLayout.addWidget(self.widget_right) @@ -239,11 +227,6 @@ class Ui_Wizard(object): self.formLayout_2 = QFormLayout() self.formLayout_2.setObjectName(u"formLayout_2") - self.projectDirectory = QLabel(self.widget_right_2) - self.projectDirectory.setObjectName(u"projectDirectory") - - self.formLayout_2.setWidget(1, QFormLayout.LabelRole, self.projectDirectory) - self.projectNameText = QLabel(self.widget_right_2) self.projectNameText.setObjectName(u"projectNameText") @@ -255,21 +238,10 @@ class Ui_Wizard(object): self.formLayout_2.setWidget(0, QFormLayout.FieldRole, self.projectNameLineEdit) - self.absoluteDirectoryEditLine = QLineEdit(self.widget_right_2) - self.absoluteDirectoryEditLine.setObjectName(u"absoluteDirectoryEditLine") - self.absoluteDirectoryEditLine.setStyleSheet(u"") - self.absoluteDirectoryEditLine.setReadOnly(True) - - self.formLayout_2.setWidget(2, QFormLayout.FieldRole, self.absoluteDirectoryEditLine) - - self.absoluteDirectory = QLabel(self.widget_right_2) - self.absoluteDirectory.setObjectName(u"absoluteDirectory") - - self.formLayout_2.setWidget(2, QFormLayout.LabelRole, self.absoluteDirectory) - - self.verticalSpacer_3 = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding) + self.projectDirectory = QLabel(self.widget_right_2) + self.projectDirectory.setObjectName(u"projectDirectory") - self.formLayout_2.setItem(3, QFormLayout.LabelRole, self.verticalSpacer_3) + self.formLayout_2.setWidget(1, QFormLayout.LabelRole, self.projectDirectory) self.horizontalLayout_4 = QHBoxLayout() self.horizontalLayout_4.setObjectName(u"horizontalLayout_4") @@ -288,6 +260,30 @@ class Ui_Wizard(object): self.formLayout_2.setLayout(1, QFormLayout.FieldRole, self.horizontalLayout_4) + self.absoluteDirectory = QLabel(self.widget_right_2) + self.absoluteDirectory.setObjectName(u"absoluteDirectory") + + self.formLayout_2.setWidget(2, QFormLayout.LabelRole, self.absoluteDirectory) + + self.absoluteDirectoryEditLine = QLineEdit(self.widget_right_2) + self.absoluteDirectoryEditLine.setObjectName(u"absoluteDirectoryEditLine") + self.absoluteDirectoryEditLine.setStyleSheet(u"") + self.absoluteDirectoryEditLine.setReadOnly(True) + + self.formLayout_2.setWidget(2, QFormLayout.FieldRole, self.absoluteDirectoryEditLine) + + self.verticalSpacer_3 = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding) + + self.formLayout_2.setItem(4, QFormLayout.LabelRole, self.verticalSpacer_3) + + self.warningLabel = QLabel(self.widget_right_2) + self.warningLabel.setObjectName(u"warningLabel") + self.warningLabel.setFont(font) + self.warningLabel.setStyleSheet(u"color: rgb(255, 102, 0);") + self.warningLabel.setTextFormat(Qt.AutoText) + + self.formLayout_2.setWidget(3, QFormLayout.FieldRole, self.warningLabel) + self.verticalLayout_6.addLayout(self.formLayout_2) @@ -340,7 +336,6 @@ class Ui_Wizard(object): self.file_list.setSortingEnabled(__sortingEnabled2) self.plainTextEdit.setPlainText(QCoreApplication.translate("Wizard", u"Create a Python Project containing an empty main.py file.", None)) - self.customTemplate.setText(QCoreApplication.translate("Wizard", u"\u81ea\u5b9a\u4e49\u6a21\u677f", None)) self.label_5.setText(QCoreApplication.translate("Wizard", u"Steps", None)) __sortingEnabled3 = self.listWidget_2.isSortingEnabled() @@ -352,10 +347,11 @@ class Ui_Wizard(object): self.listWidget_2.setSortingEnabled(__sortingEnabled3) self.label_9.setText(QCoreApplication.translate("Wizard", u"Project Configuration", None)) - self.projectDirectory.setText(QCoreApplication.translate("Wizard", u"Project Directory:", None)) self.projectNameText.setText(QCoreApplication.translate("Wizard", u"Project Name:", None)) self.projectNameLineEdit.setText(QCoreApplication.translate("Wizard", u"PyMinerProject", None)) - self.absoluteDirectory.setText(QCoreApplication.translate("Wizard", u"Absolute Directory:", None)) + self.projectDirectory.setText(QCoreApplication.translate("Wizard", u"Project Directory:", None)) self.toolButton.setText(QCoreApplication.translate("Wizard", u"...", None)) + self.absoluteDirectory.setText(QCoreApplication.translate("Wizard", u"Absolute Directory:", None)) + self.warningLabel.setText("") # retranslateUi diff --git a/pyminer2/ui/base/project_wizard.ui b/pyminer2/ui/base/project_wizard.ui index bff94ee5..2a45d89d 100644 --- a/pyminer2/ui/base/project_wizard.ui +++ b/pyminer2/ui/base/project_wizard.ui @@ -16,6 +16,9 @@ QWizard::ModernStyle + + QWizard::HelpButtonOnRight|QWizard::NoBackButtonOnStartPage + @@ -316,30 +319,6 @@ - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - 自定义模板 - - - - - @@ -452,13 +431,6 @@ - - - - Project Directory: - - - @@ -476,36 +448,13 @@ - - - - - - - true - - - - - + + - Absolute Directory: + Project Directory: - - - - Qt::Vertical - - - - 20 - 40 - - - - @@ -527,6 +476,55 @@ + + + + Absolute Directory: + + + + + + + + + + true + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + 75 + true + + + + color: rgb(255, 102, 0); + + + + + + Qt::AutoText + + + -- Gitee