Advertisement
sanfx

nslideshow.py

Nov 16th, 2013
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.21 KB | None | 0 0
  1. from PyQt4 import QtGui,QtCore
  2. import sys
  3. import os
  4.  
  5.  
  6. class SlideShowPics(QtGui.QWidget):
  7.         """docstring for SlideShowPics"""
  8.         def __init__(self, path):
  9.                 super(SlideShowPics, self).__init__()
  10.                 # Centre UI
  11.                 screen = QtGui.QDesktopWidget().screenGeometry(self)
  12.                 size =  self.geometry()
  13.                 self.move((screen.width()-size.width())/2, (screen.height()-size.height())/2)
  14.                 QtGui.QWidget.__init__(self, None, QtCore.Qt.WindowStaysOnTopHint)
  15.                 self._path = path
  16.                 self.setStyleSheet("QWidget{background-color: #000000;}")
  17.                 self.animFlag = True
  18.                 self.pause = False
  19.                 self.buildUi()
  20.                 self.showFullScreen()
  21.                 self.count = 0
  22.                 self.nextImage()
  23.                 self.updateTimer = QtCore.QTimer()
  24.                 self.connect(self.updateTimer, QtCore.SIGNAL("timeout()"), self.nextImage)
  25.                 self.playPause()
  26.  
  27.         def allImages(self):
  28.                 return  tuple(os.path.join(self._path,each) for each in os.listdir(self._path)
  29.                         if os.path.isfile(os.path.join(self._path,each)) and each.endswith('png') or each.endswith('jpg'))
  30.  
  31.         def nextImage(self):
  32.                 if self.allImages():
  33.                         if self.count != len(self.allImages()):
  34.                                 image = QtGui.QImage(self.allImages()[self.count])
  35.                                 pp = QtGui.QPixmap.fromImage(image)
  36.                                 self.label.setPixmap(pp.scaled(self.label.size(), QtCore.Qt.KeepAspectRatio, QtCore.Qt.SmoothTransformation))
  37.                         else:
  38.                                 self.count = 0
  39.                         if self.animFlag:
  40.                                 self.count += 1
  41.                         else:
  42.                                 self.count -= 1
  43.                 else:
  44.                         message = "No Image found in %s" % os.getcwd()
  45.                         print message
  46.                         self.close()
  47.  
  48.         def keyPressEvent(self, keyevent):
  49.                 """        Capture key to exit, next image, previous image,
  50.                        on Escape , Key Right and key left respectively.
  51.                """
  52.                 if keyevent.key() == QtCore.Qt.Key_Escape:
  53.                         self.close()
  54.                 if keyevent.key() == QtCore.Qt.Key_Left:
  55.                         self.animFlag = False
  56.                         self.nextImage()
  57.                 if keyevent.key() == QtCore.Qt.Key_Right:
  58.                         self.animFlag = True
  59.                         self.nextImage()
  60.                 if keyevent.key() == 32:
  61.                         self.pause = self.playPause()
  62.  
  63.         def playPause(self):
  64.                         if not self.pause:
  65.                                 self.pause = True
  66.                                 self.updateTimer.start(2500)
  67.                                 return self.pause
  68.                         else:
  69.                                 self.pause = False
  70.                                 self.updateTimer.stop()
  71.  
  72.         def _openFolder(self):
  73.                 selectedDir = str(QtGui.QFileDialog.getExistingDirectory(
  74.                         self,"Select Directory to SlideShow",os.path.expanduser("~")))
  75.                 if selectedDir:
  76.                         return selectedDir
  77.  
  78.         def buildUi(self):
  79.                 filename = self._path
  80.                 self.layout = QtGui.QHBoxLayout()
  81.                 self.label = QtGui.QLabel()
  82.                 self.label.setAlignment(QtCore.Qt.AlignCenter)
  83.                 self.layout.addWidget(self.label)
  84.                 self.setLayout(self.layout)
  85.  
  86.  
  87. def main():
  88.         curntPath = os.getcwd()
  89.         if any(each.endswith('png') or each.endswith('jpg') for each in os.listdir(curntPath)):        
  90.                 app = QtGui.QApplication(sys.argv)
  91.                 window =  SlideShowPics(curntPath)
  92.                 window.show()
  93.                 window.raise_()
  94.                 app.exec_()
  95.         else:
  96.                 print "No Image found in %s" % os.getcwd()
  97.  
  98. if __name__ == '__main__':
  99.         main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement