src/eric7/EricWidgets/EricFileSaveConfirmDialog.py

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

eric ide

mercurial