def setLastUsedDir(filePath): settings = QSettings() fileInfo = QFileInfo(filePath) if fileInfo.isDir(): dirPath = fileInfo.filePath() else: dirPath = fileInfo.path() settings.setValue( "/GdalTools/lastUsedDir", dirPath )
def newFile(self, bookPath): if not bookPath.isNull(): # this was successful File>Open finf = QFileInfo(bookPath) self.pngPath = finf.absolutePath().append(u"/pngs/") finf = QFileInfo(self.pngPath) if finf.exists() and finf.isDir(): # looking good self.ready = True self.newPosition() else: # We could inform the user we couldn't find a pngs folder, # but you know -- the user is probably already aware of that. self.clear() # just put up the gray default image else: # It was a File>New self.clear()
def __workingDirectory(path_): """ Private function to determine working directory for the file dialog. @param path_ path of the intended working directory (string or QString) @return calculated working directory (QString) """ path = QString(path_) if not path.isEmpty(): info = QFileInfo(path) if info.exists() and info.isDir(): return info.absoluteFilePath() return info.absolutePath() return QDir.currentPath()
def getFiles(pattern, directory): files = [] d = QDir(directory) for f in d.entryList(): if f=="." or f=="..": continue fi = QFileInfo(d, f) if fi.isDir(): files.extend( getFiles( pattern, unicode( fi.filePath() ) ) ) elif re.search( pattern, f): files.append( os.path.abspath( unicode( fi.filePath() ) ) ) return files
def getFiles(pattern, directory): files = [] d = QDir(directory) for f in d.entryList(): if f == "." or f == "..": continue fi = QFileInfo(d, f) if fi.isDir(): files.extend(getFiles(pattern, unicode(fi.filePath()))) elif re.search(pattern, f): files.append(os.path.abspath(unicode(fi.filePath()))) return files
class FileSystemItem(QObject): """ An element in the FileSystemModel. """ iconProvider = QFileIconProvider() fileExtensions = ['*.qlr'] xmlSearchableTags = ['title', 'abstract','layername', 'attribution'] def __init__(self, file, recurse = True, recursion_counter = None, namingregex = None): super(FileSystemItem, self).__init__() # Raise exception if root path has too many child elements if recursion_counter: recursion_counter.increment() if isinstance(file, QFileInfo): self.fileinfo = file else: self.fileinfo = QFileInfo(file) self.fullpath = self.fileinfo.absoluteFilePath() self.basename = self.fileinfo.completeBaseName() self.displayname = self.fileinfo.fileName() if self.fileinfo.isDir() else self.fileinfo.completeBaseName() if namingregex: self.displayname = namingregex.match(self.displayname).group(1) self.icon = FileSystemItem.iconProvider.icon(self.fileinfo) self.isdir = self.fileinfo.isDir() self.children = [] if self.isdir else None if self.isdir and recurse: qdir = QDir(self.fullpath) for finfo in qdir.entryInfoList( FileSystemItem.fileExtensions , QDir.Files | QDir.AllDirs | QDir.NoDotAndDotDot,QDir.Name): self.children.append(FileSystemItem(finfo, recurse, recursion_counter, namingregex)) else: # file # Populate this if and when needed self.searchablecontent = None def filtered(self, filter): """ Filters the root path. :filter is a string. Is it contained in the basename or displayname then this item will be rendered. :return: the directory item. If nothing is found returns None. """ if not filter: return self filterlower = filter.lower() namematch = self.name_matches(filter) if self.isdir: if namematch: # Stop searching. Return this dir and all sub items return FileSystemItem(self.fullpath, True) else: # Only return dir if at least one sub item is a filter match diritem = FileSystemItem(self.fullpath, False) for child in self.children: childmatch = child.filtered(filter) if childmatch is not None: diritem.children.append((childmatch)) if len(diritem.children) > 0: return diritem else: if self.searchablecontent is None: self.searchablecontent = self.get_searchable_content().lower() if namematch or self.content_matches(filter): return FileSystemItem(self.fullpath, False) return None def matches(self, searchterm): """Returns true if this item mathces the searchterm""" return self.name_matches(searchterm) or self.content_matches(searchterm) def name_matches(self, searchterm): """Returns true if the searchterm matches the name of this item""" lowered = searchterm.lower() return lowered in self.basename.lower() or lowered in self.displayname.lower() def content_matches(self, searchterm): """Returns True if the searchterm matches content of this item""" if self.isdir: return False lowered = searchterm.lower() if self.searchablecontent is None: self.searchablecontent = self.get_searchable_content().lower() return lowered in self.searchablecontent def get_searchable_content(self): """ Pulls out tags from the object and returns them in order to be used by the filtered() method. """ f = QFile(self.fileinfo.absoluteFilePath()) f.open(QIODevice.ReadOnly) #stream = QTextStream(f) #stream.setCodec("UTF-8") try: doc = QDomDocument() doc.setContent( f.readAll() ) docelt = doc.documentElement() texts = [] for tagName in FileSystemItem.xmlSearchableTags: nodes = docelt.elementsByTagName(tagName) for i in range(nodes.count()): node = nodes.at(i) value = node.firstChild().toText().data() #print value texts.append( value ) # Add keywords nodes = docelt.elementsByTagName("keywordList") for i in range(nodes.count()): kwnode = nodes.at(i) valnodes = kwnode.toElement().elementsByTagName("value") for j in range(valnodes.count()): value = valnodes.at(j).firstChild().toText().data() texts.append(value) return u' '.join(texts) finally: f.close()
def open_path(self, path): qfi = QFileInfo(path) if qfi.isDir(): self.open_dir(path) elif qfi.isFile(): self.open_file(path)