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