eric7/E5Gui/E5FileSaveConfirmDialog.py

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

eric ide

mercurial