|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2018 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to enter a file system path using a file picker. |
|
8 """ |
|
9 |
|
10 import os |
|
11 |
|
12 from PyQt6.QtWidgets import ( |
|
13 QDialog, QDialogButtonBox, QVBoxLayout, QLabel, QLineEdit |
|
14 ) |
|
15 |
|
16 from .EricPathPicker import EricPathPicker, EricPathPickerModes |
|
17 |
|
18 |
|
19 class EricFileSaveConfirmDialog(QDialog): |
|
20 """ |
|
21 Class implementing a dialog to enter a file system path using a file |
|
22 picker. |
|
23 """ |
|
24 def __init__(self, filename, title, message="", picker=True, parent=None): |
|
25 """ |
|
26 Constructor |
|
27 |
|
28 @param filename file name to be shown |
|
29 @type str |
|
30 @param title title for the dialog |
|
31 @type str |
|
32 @param message message to be shown |
|
33 @type str |
|
34 @param picker flag indicating to use a path picker |
|
35 @type bool |
|
36 @param parent reference to the parent widget |
|
37 @type QWidget |
|
38 """ |
|
39 super().__init__(parent) |
|
40 |
|
41 self.setMinimumWidth(400) |
|
42 |
|
43 self.__selectedAction = "cancel" |
|
44 self.__filename = filename |
|
45 |
|
46 self.__layout = QVBoxLayout(self) |
|
47 |
|
48 self.__label = QLabel(self) |
|
49 self.__label.setWordWrap(True) |
|
50 if message: |
|
51 self.__label.setText(message) |
|
52 else: |
|
53 self.__label.setText(self.tr("The given file exists already.")) |
|
54 |
|
55 if picker: |
|
56 self.__pathPicker = EricPathPicker(self) |
|
57 self.__pathPicker.setMode(EricPathPickerModes.SAVE_FILE_MODE) |
|
58 else: |
|
59 self.__pathPicker = QLineEdit(self) |
|
60 self.__pathPicker.setClearButtonEnabled(True) |
|
61 |
|
62 self.__buttonBox = QDialogButtonBox(self) |
|
63 self.__cancelButton = self.__buttonBox.addButton( |
|
64 QDialogButtonBox.StandardButton.Cancel) |
|
65 self.__overwriteButton = self.__buttonBox.addButton( |
|
66 self.tr("Overwrite"), QDialogButtonBox.ButtonRole.AcceptRole) |
|
67 self.__renameButton = self.__buttonBox.addButton( |
|
68 self.tr("Rename"), QDialogButtonBox.ButtonRole.AcceptRole) |
|
69 |
|
70 self.__layout.addWidget(self.__label) |
|
71 self.__layout.addWidget(self.__pathPicker) |
|
72 self.__layout.addWidget(self.__buttonBox) |
|
73 |
|
74 # set values and states |
|
75 self.__pathPicker.setText(filename) |
|
76 if picker: |
|
77 self.__pathPicker.setDefaultDirectory(os.path.dirname(filename)) |
|
78 self.__renameButton.setEnabled(False) |
|
79 self.__cancelButton.setDefault(True) |
|
80 |
|
81 self.__buttonBox.clicked.connect(self.__buttonBoxClicked) |
|
82 self.__pathPicker.textChanged.connect(self.__filenameChanged) |
|
83 |
|
84 def __buttonBoxClicked(self, button): |
|
85 """ |
|
86 Private slot to handle the user clicking a button. |
|
87 |
|
88 @param button reference to the clicked button |
|
89 @type QAbstractButton |
|
90 """ |
|
91 if button == self.__cancelButton: |
|
92 self.__selectedAction = "cancel" |
|
93 self.reject() |
|
94 elif button == self.__renameButton: |
|
95 self.__selectedAction = "rename" |
|
96 self.accept() |
|
97 elif button == self.__overwriteButton: |
|
98 self.__selectedAction = "overwrite" |
|
99 self.accept() |
|
100 |
|
101 def __filenameChanged(self, text): |
|
102 """ |
|
103 Private slot to handle a change of the file name. |
|
104 |
|
105 @param text new file name |
|
106 @type str |
|
107 """ |
|
108 self.__renameButton.setEnabled(text != self.__filename) |
|
109 |
|
110 def selectedAction(self): |
|
111 """ |
|
112 Public method to get the selected action and associated data. |
|
113 |
|
114 @return tuple containing the selected action (cancel, rename, |
|
115 overwrite) and the filename (in case of 'rename' or 'overwrite') |
|
116 @rtype tuple of (str, str) |
|
117 """ |
|
118 if self.__selectedAction == "rename": |
|
119 filename = self.__pathPicker.text() |
|
120 elif self.__selectedAction == "overwrite": |
|
121 filename = self.__filename |
|
122 else: |
|
123 filename = "" |
|
124 return self.__selectedAction, filename |
|
125 |
|
126 |
|
127 def confirmOverwrite(filename, title, message="", picker=True, parent=None): |
|
128 """ |
|
129 Function to confirm that a file shall be overwritten. |
|
130 |
|
131 @param filename file name to be shown |
|
132 @type str |
|
133 @param title title for the dialog |
|
134 @type str |
|
135 @param message message to be shown |
|
136 @type str |
|
137 @param picker flag indicating to use a path picker |
|
138 @type bool |
|
139 @param parent reference to the parent widget |
|
140 @type QWidget |
|
141 @return tuple containing the selected action (cancel, rename, |
|
142 overwrite) and the filename (in case of 'rename' or 'overwrite') |
|
143 @rtype tuple of (str, str) |
|
144 """ |
|
145 dlg = EricFileSaveConfirmDialog(filename, title, message=message, |
|
146 picker=picker, parent=parent) |
|
147 dlg.exec() |
|
148 return dlg.selectedAction() |