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