class QdCompletionThread
(QtCore.
QThread): def __init__(self, parent=None):
QtCore.
QThread.__init__
(self, parent
) self.
directory = QtCore.
QDir.
root() self._dir_expr = re.compile(unicode("^" + self.directory.path() + ".*"))
self.text = ""
self.directory_history = []
self.bookmarks = {}
def match(self, text):
self.text = text
self.start()
#
# Pre-Populating the Gatherer
#
def setDirectoryHistory(self, directories):
"""Sets the history the completer can pull from"""
self.directory_history = set(directories)
def setBookmarks(self, bookmark_dict):
"""Sets the bookmarks the completer can pull from, names and directories"""
self.bookmarks = bookmark_dict.copy()
#
# Synchronous Matching Methods
#
def getDirectoryMatches(self, text):
"""Returns the directory matches based on text"""
text = unicode(text)
if text.endswith(os.sep):
clean_path = text
parent_dir = text
match_name = ""
else:
clean_path
= unicode
(QtCore.
QDir.
cleanPath(text
)) parent_dir = os.path.dirname(clean_path)
match_name = os.path.basename(clean_path)
# check that the directory exists or is readable
if not self.directory.cd(parent_dir):
return []
matches = self.directory.entryList([match_name + "*"],
QtCore.
QDir.
Dirs | QtCore.
QDir.
NoDotAndDotDot) return [os.path.join(parent_dir, unicode(match)) for match in matches]
def getMatches(self, text):
"""Returns all of the matches based on the history, bookmarks and directories"""
matches = set()
# check if it matches any directories
if self._dir_expr.match(text):
matches.update(self.getDirectoryMatches(text))
directories = self.directory_history.copy()
directories.update(self.bookmarks.values())
# check known directory names and parts
for directory in directories:
if text in directory:
matches.add(directory)
# check bookmark names
for bookmark_name in self.bookmarks:
if text in bookmark_name:
matches.add(bookmark_name)
return list(sorted(matches))
#
# Overloaded Qt Methods
#
def run(self):
text = unicode(self.text)
matches = self.getMatches(text)
if text != self.text:
# the text has changed since the thread has started
self.run()
return
self.emit(SIGNAL("matched(QString, QStringList)"), text, matches)
class QdCompletionThread(QtCore.QThread):
def __init__(self, parent=None):
QtCore.QThread.__init__(self, parent)
self.directory = QtCore.QDir.root()
self._dir_expr = re.compile(unicode("^" + self.directory.path() + ".*"))
self.text = ""
self.directory_history = []
self.bookmarks = {}
def match(self, text):
self.text = text
self.start()
#
# Pre-Populating the Gatherer
#
def setDirectoryHistory(self, directories):
"""Sets the history the completer can pull from"""
self.directory_history = set(directories)
def setBookmarks(self, bookmark_dict):
"""Sets the bookmarks the completer can pull from, names and directories"""
self.bookmarks = bookmark_dict.copy()
#
# Synchronous Matching Methods
#
def getDirectoryMatches(self, text):
"""Returns the directory matches based on text"""
text = unicode(text)
if text.endswith(os.sep):
clean_path = text
parent_dir = text
match_name = ""
else:
clean_path = unicode(QtCore.QDir.cleanPath(text))
parent_dir = os.path.dirname(clean_path)
match_name = os.path.basename(clean_path)
# check that the directory exists or is readable
if not self.directory.cd(parent_dir):
return []
matches = self.directory.entryList([match_name + "*"],
QtCore.QDir.Dirs | QtCore.QDir.NoDotAndDotDot)
return [os.path.join(parent_dir, unicode(match)) for match in matches]
def getMatches(self, text):
"""Returns all of the matches based on the history, bookmarks and directories"""
matches = set()
# check if it matches any directories
if self._dir_expr.match(text):
matches.update(self.getDirectoryMatches(text))
directories = self.directory_history.copy()
directories.update(self.bookmarks.values())
# check known directory names and parts
for directory in directories:
if text in directory:
matches.add(directory)
# check bookmark names
for bookmark_name in self.bookmarks:
if text in bookmark_name:
matches.add(bookmark_name)
return list(sorted(matches))
#
# Overloaded Qt Methods
#
def run(self):
text = unicode(self.text)
matches = self.getMatches(text)
if text != self.text:
# the text has changed since the thread has started
self.run()
return
self.emit(SIGNAL("matched(QString, QStringList)"), text, matches)
To copy to clipboard, switch view to plain text mode
Bookmarks