SIP4 python bindings for TQt
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

453 lines
13KB

  1. #!/usr/bin/env python
  2. """**************************************************************************
  3. ** $Id: dirview.py,v 1.7 2005/11/14 17:21:26 phil Exp $
  4. **
  5. ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
  6. ** Some corrections by M. Biermaier (http://www.office-m.at)
  7. **
  8. ** This file is part of an example program for TQt. This example
  9. ** program may be used, distributed and modified without limitation.
  10. **
  11. ***************************************************************************"""
  12. import sys
  13. from python_tqt.qt import *
  14. folder_closed_xpm = [
  15. "16 16 9 1",
  16. "g c #808080",
  17. "b c #c0c000",
  18. "e c #c0c0c0",
  19. "# c #000000",
  20. "c c #ffff00",
  21. ". c None",
  22. "a c #585858",
  23. "f c #a0a0a4",
  24. "d c #ffffff",
  25. "..###...........",
  26. ".#abc##.........",
  27. ".#daabc#####....",
  28. ".#ddeaabbccc#...",
  29. ".#dedeeabbbba...",
  30. ".#edeeeeaaaab#..",
  31. ".#deeeeeeefe#ba.",
  32. ".#eeeeeeefef#ba.",
  33. ".#eeeeeefeff#ba.",
  34. ".#eeeeefefff#ba.",
  35. ".##geefeffff#ba.",
  36. "...##gefffff#ba.",
  37. ".....##fffff#ba.",
  38. ".......##fff#b##",
  39. ".........##f#b##",
  40. "...........####."
  41. ]
  42. folder_open_xpm = [
  43. "16 16 11 1",
  44. "# c #000000",
  45. "g c #c0c0c0",
  46. "e c #303030",
  47. "a c #ffa858",
  48. "b c #808080",
  49. "d c #a0a0a4",
  50. "f c #585858",
  51. "c c #ffdca8",
  52. "h c #dcdcdc",
  53. "i c #ffffff",
  54. ". c None",
  55. "....###.........",
  56. "....#ab##.......",
  57. "....#acab####...",
  58. "###.#acccccca#..",
  59. "#ddefaaaccccca#.",
  60. "#bdddbaaaacccab#",
  61. ".eddddbbaaaacab#",
  62. ".#bddggdbbaaaab#",
  63. "..edgdggggbbaab#",
  64. "..#bgggghghdaab#",
  65. "...ebhggghicfab#",
  66. "....#edhhiiidab#",
  67. "......#egiiicfb#",
  68. "........#egiibb#",
  69. "..........#egib#",
  70. "............#ee#"
  71. ]
  72. folder_locked = [
  73. "16 16 10 1",
  74. "h c #808080",
  75. "b c #ffa858",
  76. "f c #c0c0c0",
  77. "e c #c05800",
  78. "# c #000000",
  79. "c c #ffdca8",
  80. ". c None",
  81. "a c #585858",
  82. "g c #a0a0a4",
  83. "d c #ffffff",
  84. "..#a#...........",
  85. ".#abc####.......",
  86. ".#daa#eee#......",
  87. ".#ddf#e##b#.....",
  88. ".#dfd#e#bcb##...",
  89. ".#fdccc#daaab#..",
  90. ".#dfbbbccgfg#ba.",
  91. ".#ffb#ebbfgg#ba.",
  92. ".#ffbbe#bggg#ba.",
  93. ".#fffbbebggg#ba.",
  94. ".##hf#ebbggg#ba.",
  95. "...###e#gggg#ba.",
  96. ".....#e#gggg#ba.",
  97. "......###ggg#b##",
  98. ".........##g#b##",
  99. "...........####."
  100. ]
  101. pix_file = [
  102. "16 16 7 1",
  103. "# c #000000",
  104. "b c #ffffff",
  105. "e c #000000",
  106. "d c #404000",
  107. "c c #c0c000",
  108. "a c #ffffc0",
  109. ". c None",
  110. "................",
  111. ".........#......",
  112. "......#.#a##....",
  113. ".....#b#bbba##..",
  114. "....#b#bbbabbb#.",
  115. "...#b#bba##bb#..",
  116. "..#b#abb#bb##...",
  117. ".#a#aab#bbbab##.",
  118. "#a#aaa#bcbbbbbb#",
  119. "#ccdc#bcbbcbbb#.",
  120. ".##c#bcbbcabb#..",
  121. "...#acbacbbbe...",
  122. "..#aaaacaba#....",
  123. "...##aaaaa#.....",
  124. ".....##aa#......",
  125. ".......##......."
  126. ]
  127. folderLocked = None
  128. folderClosed = None
  129. folderOpened = None
  130. fileNormal = None
  131. class FileItem( TQListViewItem ) :
  132. def __init__( self, parent=None, s1=TQString.null, s2=TQString.null ):
  133. TQListViewItem.__init__( self, parent, s1, s2 )
  134. self.pix = None
  135. def setPixmap( self, p ) :
  136. self.pix = p
  137. self.setup()
  138. self.widthChanged( 0 )
  139. self.invalidateHeight()
  140. self.repaint()
  141. def pixmap( self, i ) :
  142. if i : return None
  143. else : return self.pix;
  144. # Class Directory
  145. class Directory( TQListViewItem ) :
  146. def __init__( self, parent=None, filename=TQString.null, col2=TQString.null ):
  147. TQListViewItem.__init__( self, parent, filename, col2 )
  148. self.pix = None
  149. global folderLocked, folderClosed, folderOpened, fileNormal
  150. self.showDirsOnly = False
  151. if isinstance(parent, TQListView):
  152. self.p = None
  153. self.showDirsOnly = parent.showDirsOnly()
  154. self.f = TQFile( TQString('/') )
  155. else:
  156. self.p = parent
  157. self.showDirsOnly = parent.showDirsOnly
  158. self.f = TQFile( TQString(filename) )
  159. self.readable = TQDir( self.fullName() ).isReadable()
  160. if not self.readable : self.setPixmap( folderLocked )
  161. else : self.setPixmap( folderClosed )
  162. def setPixmap( self, px ) :
  163. self.pix = px
  164. self.setup()
  165. self.widthChanged( 0 )
  166. self.invalidateHeight()
  167. self.repaint()
  168. def pixmap( self, i ) :
  169. if i : return None
  170. else : return self.pix
  171. def setOpen( self, o ) :
  172. if o : self.setPixmap( folderOpened )
  173. else : self.setPixmap( folderClosed )
  174. if o and not self.childCount() :
  175. s = TQString( self.fullName() )
  176. thisDir = TQDir( s )
  177. if not thisDir.isReadable() :
  178. self.readable = False
  179. self.setExpandable( False )
  180. return
  181. self.listView().setUpdatesEnabled( False )
  182. files = thisDir.entryInfoList()
  183. if files :
  184. fi = TQFileInfo()
  185. for it in files:
  186. fi = it
  187. if str(fi.fileName()) == "." or str(fi.fileName()) == ".." :
  188. continue # nothing
  189. elif fi.isSymLink() and not self.showDirsOnly :
  190. item = FileItem( self, fi.fileName(), "Symbolic Link" )
  191. item.setPixmap( fileNormal )
  192. elif fi.isDir() :
  193. Directory( self, fi.fileName() )
  194. elif not self.showDirsOnly :
  195. if fi.isFile() : item = FileItem( self, fi.fileName(), "File" )
  196. else : item = FileItem( self, fi.fileName(), "Special" )
  197. item.setPixmap( fileNormal )
  198. self.listView().setUpdatesEnabled( True )
  199. TQListViewItem.setOpen( self, o )
  200. def setup( self ):
  201. self.setExpandable( True )
  202. TQListViewItem.setup( self )
  203. def fullName( self ):
  204. s = TQString()
  205. if self.p :
  206. s = self.p.fullName()
  207. s.append( self.f.name() )
  208. s.append( "/" )
  209. else :
  210. s = self.f.name()
  211. return s
  212. def text( self, column ) :
  213. if column == 0 : return self.f.name()
  214. elif self.readable : return "Directory"
  215. else : return "Unreadable Directory"
  216. # Class DirectoryView
  217. class DirectoryView( TQListView ):
  218. def __init__( self, parent=None, name=None, sdo=False ):
  219. TQListView.__init__( self, parent, name )
  220. self.dirsOnly = sdo
  221. self.oldCurrent = 0
  222. self.dropItem = 0
  223. self.presspos = TQPoint(0,0)
  224. self.mousePressed = False
  225. global folderLocked, folderClosed, folderOpened, fileNormal #= TQPixmap()
  226. self.autoopenTime = 750
  227. self.autoopen_timer = TQTimer( self )
  228. if not folderLocked :
  229. folderLocked = TQPixmap( folder_locked )
  230. folderClosed = TQPixmap( folder_closed_xpm )
  231. folderOpened = TQPixmap( folder_open_xpm )
  232. fileNormal = TQPixmap( pix_file )
  233. self.connect( self, SIGNAL("doubleClicked(TQListViewItem *)"),
  234. self.slotFolderSelected )
  235. self.connect( self, SIGNAL("returnPressed(TQListViewItem *)"),
  236. self.slotFolderSelected )
  237. self.setAcceptDrops( True )
  238. self.viewport().setAcceptDrops( True )
  239. self.connect( self.autoopen_timer, SIGNAL("timeout()"), self.openFolder )
  240. def showDirsOnly( self ):
  241. return self.dirsOnly
  242. def slotFolderSelected( self, i ):
  243. if not i or not self.showDirsOnly():
  244. return
  245. dir = i
  246. self.emit( PYSIGNAL("folderSelected(const TQString &)"), (dir.fullName(), ) )
  247. def openFolder( self ):
  248. self.autoopen_timer.stop()
  249. if self.dropItem and not self.dropItem.isOpen():
  250. self.dropItem.setOpen( True )
  251. self.dropItem.repaint()
  252. def contentsDragEnterEvent( self, e ):
  253. if not TQUriDrag.canDecode(e) :
  254. e.ignore()
  255. return
  256. self.oldCurrent = self.currentItem()
  257. i = self.itemAt( self.contentsToViewport(e.pos()) )
  258. if i :
  259. self.dropItem = i
  260. self.autoopen_timer.start( self.autoopenTime )
  261. def contentsDragMoveEvent( self, e ) :
  262. if not TQUriDrag.canDecode(e) :
  263. e.ignore()
  264. return
  265. vp = self.contentsToViewport( e.pos() )
  266. i = self.itemAt( vp )
  267. if i :
  268. self.setSelected( i, True )
  269. e.accept()
  270. if i != self.dropItem :
  271. self.autoopen_timer.stop()
  272. self.dropItem = i
  273. self.autoopen_timer.start( self.autoopenTime )
  274. if e.action() == TQDropEvent.Copy:
  275. pass
  276. elif e.action() == TQDropEvent.Move:
  277. e.acceptAction()
  278. elif e.action() == TQDropEvent.Link:
  279. e.acceptAction()
  280. else:
  281. pass
  282. else :
  283. e.ignore()
  284. self.autoopen_timer.stop()
  285. self.dropItem = 0
  286. def contentsDragLeaveEvent( self, TQDragLeaveEvent ) :
  287. self.autoopen_timer.stop()
  288. self.dropItem = 0
  289. self.setCurrentItem( self.oldCurrent )
  290. self.setSelected( self.oldCurrent, True )
  291. def contentsDropEvent( self, e ):
  292. self.autoopen_timer.stop()
  293. if not TQUriDrag.canDecode(e) :
  294. e.ignore()
  295. return
  296. item = self.itemAt( self.contentsToViewport(e.pos()) )
  297. if item :
  298. lst = TQStringList()
  299. TQUriDrag.decodeLocalFiles( e, lst )
  300. if e.action() == TQDropEvent.Copy:
  301. s = TQString("Copy")
  302. elif e.action() == TQDropEvent.Move:
  303. s = TQString("Move")
  304. e.acceptAction()
  305. elif e.action() == TQDropEvent.Link:
  306. s = TQString("Link")
  307. e.acceptAction()
  308. else:
  309. s = TQString("Unknown")
  310. s += "\n\n"
  311. e.accept()
  312. for filename in lst:
  313. s += filename
  314. s += "\n"
  315. s += "\nTo\n\n "
  316. s += self.fullPath(item)
  317. TQMessageBox.information( self, "Drop target", s, "Not implemented" )
  318. else :
  319. e.ignore()
  320. def fullPath( self, item ):
  321. fullpath = TQString( item.text(0) )
  322. item = item.parent()
  323. while item:
  324. newpath = item.text(0)
  325. if item.parent():
  326. newpath += "/"
  327. newpath += fullpath
  328. fullpath = newpath
  329. item = item.parent()
  330. return fullpath
  331. def contentsMousePressEvent( self, e ) :
  332. TQListView.contentsMousePressEvent( self, e )
  333. p = TQPoint( self.contentsToViewport( e.pos() ) )
  334. i = self.itemAt( p )
  335. if i :
  336. # if the user clicked into the root decoration of the item, don't try to start a drag!
  337. if self.rootIsDecorated(): isdecorated = 1
  338. else : isdecorated = 0
  339. if p.x() > self.header().sectionPos( self.header().mapToIndex( 0 )) + self.treeStepSize() * ( i.depth() + isdecorated + self.itemMargin() or
  340. p.x() < self.header().sectionPos( self.header().mapToIndex( 0 ) ) ) :
  341. self.presspos.setX(e.pos().x())
  342. self.presspos.setY(e.pos().y())
  343. self.mousePressed = True
  344. def contentsMouseMoveEvent( self, e ):
  345. offset = TQPoint( self.presspos.x() - e.pos().x(),
  346. self.presspos.y() - e.pos().y() )
  347. if self.mousePressed and (offset).manhattanLength() > TQApplication.startDragDistance() :
  348. self.mousePressed = False
  349. item = self.itemAt( self.contentsToViewport(self.presspos) )
  350. if item :
  351. source = self.fullPath(item)
  352. if TQFile.exists(source) :
  353. ud = TQUriDrag(self.viewport())
  354. ud.setFileNames( TQStringList (source) )
  355. if ud.drag() :
  356. TQMessageBox.information( self, "Drag source",
  357. TQString("Delete ")+source, "Not implemented" )
  358. def contentsMouseReleaseEvent( self, TQMouseEvent ):
  359. self.mousePressed = False
  360. def setDir( self, s ):
  361. it = TQListViewItem( self )
  362. it.setOpen(False)
  363. thisDir = TQDir( s )
  364. it = TQListViewItem( self )
  365. it.setOpen(False)
  366. lst = TQStringList( TQStringList.split( "/", s ) )
  367. listview2 = []
  368. listview2.extend( lst )
  369. item = self.firstChild()
  370. for it2 in listview2 :
  371. while item :
  372. if item.text( 0 ) == it2 :
  373. item.setOpen( True )
  374. break
  375. item = item.itemBelow()
  376. if item:
  377. self.setCurrentItem( item )
  378. def main( args ):
  379. a = TQApplication( args )
  380. mw = DirectoryView()
  381. mw.addColumn( "Name" )
  382. mw.addColumn( "Type" )
  383. mw.setTreeStepSize( 20 )
  384. roots = TQDir.drives()
  385. if roots :
  386. for it in roots:
  387. fi = it
  388. root = Directory( mw, fi.filePath() )
  389. if roots.count(it) <= 1 :
  390. root.setOpen( True ) # be interesting
  391. mw.resize( 400, 400 )
  392. mw.setCaption( "TQt Example - Directory Browser" )
  393. mw.setAllColumnsShowFocus( True )
  394. a.setMainWidget( mw )
  395. mw.show()
  396. a.exec_loop()
  397. if __name__=="__main__":
  398. main(sys.argv)