from PyQt4 import QtGui, QtCore from PyQt4.QtCore import QVariant import os from ..clPlugin import Plugin from ..misc import ORGNAME, APPNAME class Filebrowser(Plugin): o = None def _load(self): self.o = wgFilebrowser(self) def _unload(self): self.o = None def getInfo(self): return 'A file browser that allows adding files not in collection.' def _get_dock_widget(self): return self._create_dock(self.o) class wgFilebrowser(QtGui.QWidget): view = None model = None path = None plugin = None def __init__(self, plugin): QtGui.QWidget.__init__(self) self.plugin = plugin self.model = QtGui.QDirModel() self.model.setFilter(QtCore.QDir.AllDirs|QtCore.QDir.AllEntries) self.model.setSorting(QtCore.QDir.DirsFirst) self.view = QtGui.QListView() self.view.setModel(self.model) self.view.setRootIndex(self.model.index(os.path.expanduser('~'))) self.view.setSelectionMode(QtGui.QTreeWidget.ExtendedSelection) self.connect(self.view, QtCore.SIGNAL('activated(const QModelIndex&)'), self.item_activated) self.path = QtGui.QLineEdit(self.model.filePath(self.view.rootIndex())) self.connect(self.path, QtCore.SIGNAL('returnPressed()'), self.path_changed) self.setLayout(QtGui.QVBoxLayout()) self.layout().setSpacing(0) self.layout().setMargin(0) self.layout().addWidget(self.path) self.layout().addWidget(self.view) def item_activated(self, index): if self.model.hasChildren(index): self.view.setRootIndex(index) self.path.setText(self.model.filePath(index)) else: if not 'file://' in self.plugin.mpclient()().urlhandlers(): self.set_status('file:// handler not available. Connect via unix domain sockets.') return paths = [] for index in self.view.selectedIndexes(): paths.append(u'file://' + unicode(self.model.filePath(index))) self.plugin.mpclient()().add(paths) def path_changed(self): if os.path.isdir(self.path.text()): self.view.setRootIndex(self.model.index(self.path.text()))