|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2007 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing various kinds of completers. |
|
8 """ |
|
9 |
|
10 import os |
|
11 |
|
12 from PyQt6.QtCore import QDir, Qt, QStringListModel |
|
13 from PyQt6.QtGui import QFileSystemModel |
|
14 from PyQt6.QtWidgets import QCompleter |
|
15 |
|
16 from Globals import isWindowsPlatform |
|
17 |
|
18 |
|
19 class EricFileCompleter(QCompleter): |
|
20 """ |
|
21 Class implementing a completer for file names. |
|
22 """ |
|
23 def __init__(self, parent=None, |
|
24 completionMode=QCompleter.CompletionMode.PopupCompletion, |
|
25 showHidden=False): |
|
26 """ |
|
27 Constructor |
|
28 |
|
29 @param parent parent widget of the completer (QWidget) |
|
30 @param completionMode completion mode of the |
|
31 completer (QCompleter.CompletionMode) |
|
32 @param showHidden flag indicating to show hidden entries as well |
|
33 (boolean) |
|
34 """ |
|
35 super().__init__(parent) |
|
36 self.__model = QFileSystemModel(self) |
|
37 if showHidden: |
|
38 self.__model.setFilter( |
|
39 QDir.Filter.Dirs | |
|
40 QDir.Filter.Files | |
|
41 QDir.Filter.Drives | |
|
42 QDir.Filter.AllDirs | |
|
43 QDir.Filter.Hidden) |
|
44 else: |
|
45 self.__model.setFilter( |
|
46 QDir.Filter.Dirs | |
|
47 QDir.Filter.Files | |
|
48 QDir.Filter.Drives | |
|
49 QDir.Filter.AllDirs) |
|
50 self.__model.setRootPath("") |
|
51 self.setModel(self.__model) |
|
52 self.setCompletionMode(completionMode) |
|
53 if isWindowsPlatform(): |
|
54 self.setCaseSensitivity(Qt.CaseSensitivity.CaseInsensitive) |
|
55 if parent: |
|
56 parent.setCompleter(self) |
|
57 |
|
58 def setRootPath(self, path): |
|
59 """ |
|
60 Public method to set the root path of the model. |
|
61 |
|
62 @param path root path for the model |
|
63 @type str |
|
64 """ |
|
65 if not os.path.isdir(path): |
|
66 path = os.path.dirname(path) |
|
67 self.__model.setRootPath(path) |
|
68 |
|
69 def rootPath(self): |
|
70 """ |
|
71 Public method to get the root path of the model. |
|
72 |
|
73 @return root path of the model |
|
74 @rtype str |
|
75 """ |
|
76 return self.__model.rootPath() |
|
77 |
|
78 |
|
79 class EricDirCompleter(QCompleter): |
|
80 """ |
|
81 Class implementing a completer for directory names. |
|
82 """ |
|
83 def __init__(self, parent=None, |
|
84 completionMode=QCompleter.CompletionMode.PopupCompletion, |
|
85 showHidden=False): |
|
86 """ |
|
87 Constructor |
|
88 |
|
89 @param parent parent widget of the completer (QWidget) |
|
90 @param completionMode completion mode of the |
|
91 completer (QCompleter.CompletionMode) |
|
92 @param showHidden flag indicating to show hidden entries as well |
|
93 (boolean) |
|
94 """ |
|
95 super().__init__(parent) |
|
96 self.__model = QFileSystemModel(self) |
|
97 if showHidden: |
|
98 self.__model.setFilter( |
|
99 QDir.Filter.Drives | |
|
100 QDir.Filter.AllDirs | |
|
101 QDir.Filter.Hidden) |
|
102 else: |
|
103 self.__model.setFilter( |
|
104 QDir.Filter.Drives | QDir.Filter.AllDirs) |
|
105 self.__model.setRootPath("") |
|
106 self.setModel(self.__model) |
|
107 self.setCompletionMode(completionMode) |
|
108 if isWindowsPlatform(): |
|
109 self.setCaseSensitivity(Qt.CaseSensitivity.CaseInsensitive) |
|
110 if parent: |
|
111 parent.setCompleter(self) |
|
112 |
|
113 def setRootPath(self, path): |
|
114 """ |
|
115 Public method to set the root path of the model. |
|
116 |
|
117 @param path root path for the model |
|
118 @type str |
|
119 """ |
|
120 if not os.path.isdir(path): |
|
121 path = os.path.dirname(path) |
|
122 self.__model.setRootPath(path) |
|
123 |
|
124 def rootPath(self): |
|
125 """ |
|
126 Public method to get the root path of the model. |
|
127 |
|
128 @return root path of the model |
|
129 @rtype str |
|
130 """ |
|
131 return self.__model.rootPath() |
|
132 |
|
133 |
|
134 class EricStringListCompleter(QCompleter): |
|
135 """ |
|
136 Class implementing a completer for string lists. |
|
137 """ |
|
138 def __init__(self, parent=None, strings=None, |
|
139 completionMode=QCompleter.CompletionMode.PopupCompletion): |
|
140 """ |
|
141 Constructor |
|
142 |
|
143 @param parent parent widget of the completer (QWidget) |
|
144 @param strings list of string to load into the completer |
|
145 (list of strings) |
|
146 @param completionMode completion mode of the |
|
147 completer (QCompleter.CompletionMode) |
|
148 """ |
|
149 super().__init__(parent) |
|
150 self.__model = QStringListModel( |
|
151 [] if strings is None else strings[:], |
|
152 parent) |
|
153 self.setModel(self.__model) |
|
154 self.setCompletionMode(completionMode) |
|
155 if parent: |
|
156 parent.setCompleter(self) |