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