Fri, 04 Nov 2022 13:52:26 +0100
Resorted the import statements using isort.
9201 | 1 | # -*- coding: utf-8 -*- |
2 | ||
3 | # Copyright (c) 2022 Detlev Offenbach <detlev@die-offenbachs.de> | |
4 | # | |
5 | ||
6 | """ | |
7 | Module implementing a dialog to enter the data for an entry point. | |
8 | """ | |
9 | ||
10 | import pathlib | |
11 | ||
12 | from PyQt6.QtCore import pyqtSlot | |
13 | from PyQt6.QtWidgets import QDialog, QDialogButtonBox | |
14 | ||
9473
3f23dbf37dbe
Resorted the import statements using isort.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9413
diff
changeset
|
15 | from eric7.EricGui import EricPixmapCache |
9413
80c06d472826
Changed the eric7 import statements to include the package name (i.e. eric7) in order to not fiddle with sys.path.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
16 | from eric7.EricWidgets import EricFileDialog |
9201 | 17 | |
18 | from .Ui_AddEntryPointDialog import Ui_AddEntryPointDialog | |
19 | ||
20 | ||
21 | class AddEntryPointDialog(QDialog, Ui_AddEntryPointDialog): | |
22 | """ | |
23 | Class implementing a dialog to enter the data for an entry point. | |
24 | """ | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
25 | |
9201 | 26 | def __init__(self, rootDirectory, epType="", name="", script="", parent=None): |
27 | """ | |
28 | Constructor | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
29 | |
9201 | 30 | @param rootDirectory root directory for selecting script modules via |
31 | a file selection dialog | |
32 | @type str | |
33 | @param epType type of the entry point (defaults to "") | |
34 | @type str (optional) | |
35 | @param name name of the entry point (defaults to "") | |
36 | @type str (optional) | |
37 | @param script script function of the entry point (defaults to "") | |
38 | @type str (optional) | |
39 | @param parent reference to the parent widget (defaults to None) | |
40 | @type QWidget (optional) | |
41 | """ | |
42 | super().__init__(parent) | |
43 | self.setupUi(self) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
44 | |
9201 | 45 | for typeStr, category in ( |
46 | ("", ""), | |
47 | (self.tr("Console"), "console_scripts"), | |
48 | (self.tr("GUI"), "gui_scripts"), | |
49 | ): | |
50 | self.typeComboBox.addItem(typeStr, category) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
51 | |
9413
80c06d472826
Changed the eric7 import statements to include the package name (i.e. eric7) in order to not fiddle with sys.path.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
52 | self.scriptButton.setIcon(EricPixmapCache.getIcon("open")) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
53 | |
9201 | 54 | self.buttonBox.button(QDialogButtonBox.StandardButton.Ok).setEnabled(False) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
55 | |
9201 | 56 | self.__rootDirectory = rootDirectory |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
57 | |
9201 | 58 | self.typeComboBox.currentTextChanged.connect(self.__updateOK) |
59 | self.nameEdit.textChanged.connect(self.__updateOK) | |
60 | self.scriptEdit.textChanged.connect(self.__updateOK) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
61 | |
9201 | 62 | self.typeComboBox.setCurrentText(epType) |
63 | self.nameEdit.setText(name) | |
64 | self.scriptEdit.setText(script) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
65 | |
9201 | 66 | msh = self.minimumSizeHint() |
67 | self.resize(max(self.width(), msh.width()), msh.height()) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
68 | |
9201 | 69 | @pyqtSlot() |
70 | def __updateOK(self): | |
71 | """ | |
72 | Private slot to update the enabled state of the OK button. | |
73 | """ | |
74 | self.buttonBox.button(QDialogButtonBox.StandardButton.Ok).setEnabled( | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
75 | bool(self.typeComboBox.currentText()) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
76 | and bool(self.nameEdit.text()) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
77 | and bool(self.scriptEdit.text()) |
9201 | 78 | ) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
79 | |
9201 | 80 | @pyqtSlot() |
81 | def on_scriptButton_clicked(self): | |
82 | """ | |
83 | Private slot to select a script via a file selection dialog. | |
84 | """ | |
85 | script = self.scriptEdit.text() | |
86 | if script: | |
87 | if ":" in script: | |
88 | scriptFile, scriptFunction = script.rsplit(":", 1) | |
89 | else: | |
90 | scriptFile, scriptFunction = script, "main" | |
91 | scriptFile = scriptFile.replace(".", "/") + ".py" | |
92 | root = str(pathlib.Path(self.__rootDirectory) / scriptFile) | |
93 | else: | |
94 | root = self.__rootDirectory | |
95 | scriptFunction = "main" | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
96 | |
9201 | 97 | script = EricFileDialog.getOpenFileName( |
98 | self, | |
99 | self.tr("Select Script File"), | |
100 | root, | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
101 | self.tr("Python Files (*.py);;All Files (*)"), |
9201 | 102 | ) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
103 | |
9201 | 104 | if script: |
105 | scriptPath = pathlib.Path(script) | |
106 | try: | |
107 | relativeScriptPath = scriptPath.relative_to(self.__rootDirectory) | |
108 | except ValueError: | |
109 | relativeScriptPath = scriptPath | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
110 | self.scriptEdit.setText( |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
111 | "{0}:{1}".format( |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
112 | str(relativeScriptPath.with_suffix("")) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
113 | .replace("/", ".") |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
114 | .replace("\\", "."), |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
115 | scriptFunction, |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
116 | ) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
117 | ) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
118 | |
9201 | 119 | def getEntryPoint(self): |
120 | """ | |
121 | Public method to get the data for the entry point. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
122 | |
9201 | 123 | @return tuple containing the entry point type, category, name and |
124 | script function | |
125 | @rtype tuple of (str, str, str) | |
126 | """ | |
127 | return ( | |
128 | self.typeComboBox.currentText(), | |
129 | self.typeComboBox.currentData(), | |
130 | self.nameEdit.text(), | |
131 | self.scriptEdit.text(), | |
132 | ) |