Guest User

FileView

a guest
Apr 17th, 2015
570
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.71 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. __author__ = 'Ilya'
  3.  
  4. import os
  5. from PyQt4.QtGui import QAbstractItemView, QItemSelectionModel
  6. from PyQt4.QtCore import pyqtSlot, SIGNAL, SLOT, QObject, QDir
  7. from PyQt4 import QtGui, QtCore
  8. import pyfits as pft
  9. from subprocess import *
  10.  
  11. class FileView(QtGui.QListView):
  12.     model = QtGui.QFileSystemModel()
  13.     fts = QtGui.QStandardItemModel()
  14.     list = []
  15.     mx = 0
  16.     current = 0
  17.  
  18.     def __init__(self):
  19.         super(FileView, self).__init__()
  20.         self.acceptDrops()
  21.  
  22.         path = QDir.currentPath()
  23.         flt = ["*.fits"]
  24.         self.model.setFilter(QDir.AllDirs | QDir.NoDotAndDotDot | QDir.AllEntries)
  25.         self.model.setNameFilters(flt)
  26.         self.model.setNameFilterDisables(False)
  27.         root = self.model.setRootPath(path)
  28.         self.setModel(self.model)
  29.         self.setRootIndex(root)
  30.  
  31.         self.setSelectionMode(QAbstractItemView.SingleSelection)
  32.         self.refresh(path)
  33.         self.select(0)
  34.  
  35.         QObject.connect(self, SIGNAL("doubleClicked(QModelIndex)"), self, SLOT("ItemDoubleClicked(QModelIndex)"))
  36.  
  37.     @pyqtSlot("QModelIndex")
  38.     def ItemDoubleClicked(self,index):
  39.         self.setRootIndex(self.model.index(self.model.filePath(index)))
  40.  
  41.     def keyPressEvent(self, event):
  42.         key = event.key()
  43.         if key == QtCore.Qt.Key_Right:
  44.             if len(self.selectedIndexes()) > 0:
  45.                 index = self.selectedIndexes()[0]
  46.                 path = self.model.filePath(index)
  47.  
  48.                 if os.path.isdir(path):
  49.                     index = self.model.setRootPath(path)
  50.                     self.setRootIndex(index)
  51.  
  52.                     self.clearSelection()
  53.  
  54.                     self.refresh(path)
  55.                     self.select(0)
  56.  
  57.                 if os.path.isfile(path):
  58.                     hdulist = pft.open(path)
  59.                     output = ""
  60.                     hdulist.info(output)
  61.                     hdulist.close()
  62.  
  63.  
  64.         if key == QtCore.Qt.Key_Left:
  65.             self.clearSelection()
  66.             dirs = QDir(self.model.filePath(self.rootIndex()))
  67.             if os.path.isfile(dirs.absolutePath()):
  68.                 dirs.cdUp()
  69.             dirs.cdUp()
  70.  
  71.             index = self.model.setRootPath(dirs.absolutePath())
  72.             self.setRootIndex(index)
  73.  
  74.             self.refresh(dirs.absolutePath())
  75.             self.select(0)
  76.  
  77.         if key == QtCore.Qt.Key_Up:
  78.             self.clearSelection()
  79.             self.current -= 1
  80.             if self.current == -1:
  81.                 self.current = self.mx - 1
  82.             self.select(self.current)
  83.  
  84.         if key == QtCore.Qt.Key_Down:
  85.             self.clearSelection()
  86.             self.current += 1
  87.             if self.current == self.mx:
  88.                 self.current = 0
  89.             self.select(self.current)
  90.  
  91.         if key == QtCore.Qt.Key_AsciiTilde:
  92.             path = QDir.homePath()
  93.             self.setRootIndex(self.model.index(path))
  94.             self.refresh(path)
  95.             self.select(0)
  96.  
  97.     def lsdir(self, path):
  98.         ls = os.listdir(path)
  99.         files = []
  100.         dirs = []
  101.         for fl in ls:
  102.             pt = path+'/'+fl
  103.             if os.path.isfile(pt):
  104.                 files.append(fl)
  105.             if os.path.isdir(pt):
  106.                 dirs.append(fl)
  107.         sorted(files)
  108.         sorted(dirs)
  109.         lst = dirs + files
  110.         return lst
  111.  
  112.     def refresh(self, path):
  113.         ls = self.lsdir(path)
  114.         self.list = []
  115.         self.mx = len(ls)
  116.         self.current = 0
  117.         for i in ls:
  118.             self.list.append(path+'/'+i)
  119.  
  120.     def select(self, pos):
  121.         if self.mx > 0:
  122.             self.selectionModel().select(self.model.index(self.list[pos]), QItemSelectionModel.Select)
Advertisement
Add Comment
Please, Sign In to add comment