|
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 from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QVBoxLayout, QLabel |
|
13 |
|
14 from .E5PathPicker import E5PathPicker, E5PathPickerModes |
|
15 |
|
16 |
|
17 class E5PathPickerDialog(QDialog): |
|
18 """ |
|
19 Class implementing a dialog to enter a file system path using a file |
|
20 picker. |
|
21 """ |
|
22 def __init__(self, parent=None): |
|
23 """ |
|
24 Constructor |
|
25 |
|
26 @param parent reference to the parent widget |
|
27 @type QWidget |
|
28 """ |
|
29 super(E5PathPickerDialog, self).__init__(parent) |
|
30 |
|
31 self.setMinimumWidth(400) |
|
32 |
|
33 self.__layout = QVBoxLayout(self) |
|
34 |
|
35 self.__label = QLabel(self) |
|
36 self.__label.setWordWrap(True) |
|
37 |
|
38 self.__pathPicker = E5PathPicker(self) |
|
39 self.__buttonBox = QDialogButtonBox( |
|
40 QDialogButtonBox.Cancel | QDialogButtonBox.Ok, self) |
|
41 |
|
42 self.__layout.addWidget(self.__label) |
|
43 self.__layout.addWidget(self.__pathPicker) |
|
44 self.__layout.addWidget(self.__buttonBox) |
|
45 |
|
46 self.__buttonBox.accepted.connect(self.accept) |
|
47 self.__buttonBox.rejected.connect(self.reject) |
|
48 |
|
49 def setLabelText(self, text): |
|
50 """ |
|
51 Public method to set the label text. |
|
52 |
|
53 @param text label text |
|
54 @type str |
|
55 """ |
|
56 self.__label.setText(text) |
|
57 |
|
58 def setTitle(self, title): |
|
59 """ |
|
60 Public method to set the window title. |
|
61 |
|
62 @param title window title |
|
63 @type str |
|
64 """ |
|
65 self.setWindowTitle(title) |
|
66 self.__pathPicker.setWindowTitle(title) |
|
67 |
|
68 def setPickerMode(self, mode): |
|
69 """ |
|
70 Public method to set the mode of the path picker. |
|
71 |
|
72 @param mode picker mode |
|
73 @type E5PathPickerModes |
|
74 """ |
|
75 self.__pathPicker.setMode(mode) |
|
76 |
|
77 def setPickerPath(self, path): |
|
78 """ |
|
79 Public method to set the path of the path picker. |
|
80 |
|
81 @param path path to be set |
|
82 @type str |
|
83 """ |
|
84 self.__pathPicker.setPath(path) |
|
85 |
|
86 def setDefaultDirectory(self, directory): |
|
87 """ |
|
88 Public method to set the default directory of the path picker. |
|
89 |
|
90 @param directory default directory |
|
91 @type str |
|
92 """ |
|
93 self.__pathPicker.setDefaultDirectory(directory) |
|
94 |
|
95 def setPickerFilters(self, filters): |
|
96 """ |
|
97 Public method to set the filters of the path picker. |
|
98 |
|
99 Note: Multiple filters must be separated by ';;'. |
|
100 |
|
101 @param filters string containing the file filters |
|
102 @type str |
|
103 """ |
|
104 self.__pathPicker.setFilters(filters) |
|
105 |
|
106 def getPath(self): |
|
107 """ |
|
108 Public method to get the current path. |
|
109 |
|
110 @return current path |
|
111 @rtype str |
|
112 """ |
|
113 return self.__pathPicker.path() |
|
114 |
|
115 |
|
116 def getPath(parent, title, label, mode=E5PathPickerModes.OpenFileMode, |
|
117 path="", defaultDirectory="", filters=None): |
|
118 """ |
|
119 Function to get a file or directory path from the user. |
|
120 |
|
121 @param parent reference to the parent widget |
|
122 @type QWidget |
|
123 @param title title of the dialog |
|
124 @type str |
|
125 @param label text to be shown above the path picker |
|
126 @type str |
|
127 @param mode mode of the path picker |
|
128 @type E5PathPickerModes |
|
129 @param path initial path to be shown |
|
130 @type str |
|
131 @param defaultDirectory default directory of the path picker selection |
|
132 dialog |
|
133 @type str |
|
134 @param filters list of file filters |
|
135 @type list of str |
|
136 @return tuple containing the entered path and a flag indicating that the |
|
137 user pressed the OK button |
|
138 @rtype tuple of (str, bool) |
|
139 """ |
|
140 # step 1: setup of the dialog |
|
141 dlg = E5PathPickerDialog(parent) |
|
142 if title: |
|
143 dlg.setTitle(title) |
|
144 if label: |
|
145 dlg.setLabelText(label) |
|
146 dlg.setPickerMode(mode) |
|
147 if path: |
|
148 dlg.setPickerPath(path) |
|
149 if defaultDirectory: |
|
150 dlg.setDefaultDirectory(defaultDirectory) |
|
151 if filters is not None and len(filters) > 0: |
|
152 dlg.setPickerFilters(";;".join(filters)) |
|
153 |
|
154 # step 2: show the dialog and get the result |
|
155 if dlg.exec_() == QDialog.Accepted: |
|
156 ok = True |
|
157 path = dlg.getPath().strip() |
|
158 else: |
|
159 ok = False |
|
160 path = "" |
|
161 |
|
162 # step 3: return the result |
|
163 return path, ok |