eric6/E5Gui/E5FileSaveConfirmDialog.py

branch
micropython
changeset 7083
217862c28319
child 7089
9f9816b19aa4
equal deleted inserted replaced
7082:ec199ef0cfc6 7083:217862c28319
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2018 - 2019 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 from __future__ import unicode_literals
11
12 import os
13
14 from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QVBoxLayout, QLabel
15
16 from .E5PathPicker import E5PathPicker, E5PathPickerModes
17 from .E5LineEdit import E5ClearableLineEdit
18
19
20 class E5FileSaveConfirmDialog(QDialog):
21 """
22 Class implementing a dialog to enter a file system path using a file
23 picker.
24 """
25 def __init__(self, filename, title, message="", picker=True, parent=None):
26 """
27 Constructor
28
29 @param filename file name to be shown
30 @type str
31 @param message message to be shown
32 @type str
33 @param title title for the dialog
34 @type str
35 @param picker flag indicating to use a path picker
36 @type bool
37 @param parent reference to the parent widget
38 @type QWidget
39 """
40 super(E5FileSaveConfirmDialog, self).__init__(parent)
41
42 self.setMinimumWidth(400)
43
44 self.__selectedAction = "cancel"
45 self.__filename = filename
46
47 self.__layout = QVBoxLayout(self)
48
49 self.__label = QLabel(self)
50 self.__label.setWordWrap(True)
51 if message:
52 self.__label.setText(message)
53 else:
54 self.__label.setText(self.tr("The given file exists already."))
55
56 if picker:
57 self.__pathPicker = E5PathPicker(self)
58 self.__pathPicker.setMode(E5PathPickerModes.SaveFileMode)
59 else:
60 self.__pathPicker = E5ClearableLineEdit(self)
61
62 self.__buttonBox = QDialogButtonBox(self)
63 self.__cancelButton = self.__buttonBox.addButton(
64 QDialogButtonBox.Cancel)
65 self.__overwriteButton = self.__buttonBox.addButton(
66 self.tr("Overwrite"), QDialogButtonBox.AcceptRole)
67 self.__renameButton = self.__buttonBox.addButton(
68 self.tr("Rename"), QDialogButtonBox.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 message message to be shown
134 @type str
135 @param title title for the dialog
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 = E5FileSaveConfirmDialog(filename, title, message=message,
146 picker=picker, parent=parent)
147 dlg.exec_()
148 return dlg.selectedAction()

eric ide

mercurial