|
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 |
|
15 from EricWidgets import EricFileDialog |
|
16 |
|
17 from .Ui_AddEntryPointDialog import Ui_AddEntryPointDialog |
|
18 |
|
19 import UI.PixmapCache |
|
20 |
|
21 |
|
22 class AddEntryPointDialog(QDialog, Ui_AddEntryPointDialog): |
|
23 """ |
|
24 Class implementing a dialog to enter the data for an entry point. |
|
25 """ |
|
26 def __init__(self, rootDirectory, epType="", name="", script="", parent=None): |
|
27 """ |
|
28 Constructor |
|
29 |
|
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) |
|
44 |
|
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) |
|
51 |
|
52 self.scriptButton.setIcon(UI.PixmapCache.getIcon("open")) |
|
53 |
|
54 self.buttonBox.button(QDialogButtonBox.StandardButton.Ok).setEnabled(False) |
|
55 |
|
56 self.__rootDirectory = rootDirectory |
|
57 |
|
58 self.typeComboBox.currentTextChanged.connect(self.__updateOK) |
|
59 self.nameEdit.textChanged.connect(self.__updateOK) |
|
60 self.scriptEdit.textChanged.connect(self.__updateOK) |
|
61 |
|
62 self.typeComboBox.setCurrentText(epType) |
|
63 self.nameEdit.setText(name) |
|
64 self.scriptEdit.setText(script) |
|
65 |
|
66 msh = self.minimumSizeHint() |
|
67 self.resize(max(self.width(), msh.width()), msh.height()) |
|
68 |
|
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( |
|
75 bool(self.typeComboBox.currentText()) and |
|
76 bool(self.nameEdit.text()) and |
|
77 bool(self.scriptEdit.text()) |
|
78 ) |
|
79 |
|
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" |
|
96 |
|
97 script = EricFileDialog.getOpenFileName( |
|
98 self, |
|
99 self.tr("Select Script File"), |
|
100 root, |
|
101 self.tr("Python Files (*.py);;All Files (*)") |
|
102 ) |
|
103 |
|
104 if script: |
|
105 scriptPath = pathlib.Path(script) |
|
106 try: |
|
107 relativeScriptPath = scriptPath.relative_to(self.__rootDirectory) |
|
108 except ValueError: |
|
109 relativeScriptPath = scriptPath |
|
110 self.scriptEdit.setText("{0}:{1}".format( |
|
111 str(relativeScriptPath.with_suffix("")) |
|
112 .replace("/", ".") |
|
113 .replace("\\", "."), |
|
114 scriptFunction |
|
115 )) |
|
116 |
|
117 def getEntryPoint(self): |
|
118 """ |
|
119 Public method to get the data for the entry point. |
|
120 |
|
121 @return tuple containing the entry point type, category, name and |
|
122 script function |
|
123 @rtype tuple of (str, str, str) |
|
124 """ |
|
125 return ( |
|
126 self.typeComboBox.currentText(), |
|
127 self.typeComboBox.currentData(), |
|
128 self.nameEdit.text(), |
|
129 self.scriptEdit.text(), |
|
130 ) |