dirWidget.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/python
  2. ''' a widget that shows only a dir listing
  3. '''
  4. import sys,os,argparse
  5. from PyQt4.QtGui import *
  6. from PyQt4 import QtGui, QtCore
  7. class DirWidget(QListWidget):
  8. ''' a simple widget that shows a list of directories, staring
  9. at the directory passed into the constructor
  10. a mouse click or 'enter' key will send a 'selected' signal to
  11. anyone who connects to this.
  12. the .. parent directory is shown for all dirs except /
  13. the most interesting parts of the interface are:
  14. constructor - send in the directory to view
  15. method - currentPath() returns text of current path
  16. signal - selected calls a slot with a single arg: new path
  17. '''
  18. selected = QtCore.pyqtSignal(str)
  19. def __init__(self, directory='.', parent=None):
  20. super(DirWidget,self).__init__(parent)
  21. self.directory = directory
  22. self.refill()
  23. self.itemActivated.connect(self.selectionByLWI)
  24. # it would be nice to pick up single-mouse-click for selection as well
  25. # but that seems to be a system-preferences global
  26. def selectionByLWI(self, li):
  27. self.directory = os.path.abspath(self.directory + '/' + str(li.text()))
  28. self.refill()
  29. self.selected.emit(self.directory)
  30. def refill(self):
  31. current,dirs,files = os.walk(self.directory,followlinks=True).next()
  32. dirs.sort()
  33. if '/' not in dirs:
  34. dirs = ['..'] + dirs
  35. self.clear()
  36. for d in dirs:
  37. li = QListWidgetItem(d,self)
  38. def currentPath(self):
  39. return self.directory
  40. if __name__=="__main__":
  41. def alert(some_text):
  42. os.system('notify-send -t 1500 "file: '+str(some_text) + '"')
  43. a = QApplication([])
  44. qmw = QMainWindow()
  45. dw = DirWidget(parent=qmw)
  46. dw.selected.connect(alert)
  47. qmw.setCentralWidget(dw)
  48. qmw.show()
  49. a.exec_()