1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
|
#!/usr/bin/python
"""
__version__ = "$Revision: 1.42 $"
__date__ = "$Date: 2004/08/15 17:34:58 $"
"""
from PythonCard import dialog, graphic, log, model, timer, util
import wx
import os, sys
from PythonCard import EXIF
import zipfile
from cStringIO import StringIO
def imageFile(path):
if os.path.isfile(path):
ext = os.path.splitext(path)[-1].lower()
if ext != '' and ext[0] == '.':
ext = ext[1:]
if ext in ['bmp', 'gif', 'jpeg', 'jpg', 'pcx',
'png', 'pnm', 'tif', 'tiff', 'xbm', 'xpm']:
return 1
return 0
def htmlFile(path):
if os.path.isfile(path):
ext = os.path.splitext(path)[-1].lower()
if ext != '' and ext[0] == '.':
ext = ext[1:]
if ext in ['htm', 'html']:
return 1
return 0
class SlideShow(model.Background):
def on_initialize(self, event):
self.x = 0
self.y = 0
self.filename = None
self.directory = None
self.zip = None
self.bmp = None
self.fileList = None
self.fileIndex = 0
self.clockTimer = timer.Timer(self.components.bufOff, -1)
self.interval = 1000 * 2 # 5 seconds
self.loop = 0
self.components.bufOff.backgroundColor = 'black'
self.components.bufOff.clear()
if sys.platform.startswith('win'):
del self.components['htmlView']
self.components['htmlView'] = {'type':'IEHtmlWindow', 'name':'htmlView',
'position':(0, 0), 'size':(150, 150), 'visible':0}
# can't disable the component if we want it
# to scroll, so need another way to capture
# key presses so the IE control doesn't get them
# however since slideshow is for displaying HTML
# where you shouldn't need to scroll this is probably fine
# there is still some kind of focus bug with the IE control
# on certain HTML pages
self.components.htmlView.enabled = 0
# this is the code from pictureViewer
# instead of a file argument, slideshow
# should take either a file or directory
# argument
# if given a directory, the slide show would
# be setup to start in that directory
# if given a file argument, the contents
# of the file would contain a list of files to
# display, one file per line
if len(sys.argv) > 1:
# accept a file argument on the command-line
filename = os.path.abspath(sys.argv[1])
log.info('slideshow filename: ' + filename)
if not os.path.exists(filename):
filename = os.path.abspath(os.path.join(self.application.startingDirectory, sys.argv[1]))
#print filename
if os.path.isfile(filename):
#self.openFile(filename)
self.buildFileListFromFile(filename)
self.on_menuSlideshowFirstSlide_select(None)
elif os.path.isdir(filename):
includeSubDirs = self.menuBar.getChecked('menuOptionsIncludeSubDirectories')
self.buildFileListFromDirectory(filename, includeSubDirs)
self.on_menuSlideshowFirstSlide_select(None)
# PythonCard doesn't currently support
# binding key presses to just the background, so this
# is a hack
wx.EVT_KEY_UP(self.components.bufOff, self.on_keyPress)
wx.EVT_KEY_UP(self.components.htmlView, self.on_keyPress)
self.visible = True
self.on_size(None)
def on_timer(self, event):
self.displayNextFile()
# need to enable keyPresses
# for backgrounds without a component that accepts
# keyPresses
def on_keyPress(self, event):
keyCode = event.GetKeyCode()
#print keyCode
if keyCode == wx.WXK_UP or keyCode == wx.WXK_HOME:
self.on_menuSlideshowFirstSlide_select(None)
elif keyCode == wx.WXK_LEFT:
self.on_menuSlideshowPreviousSlide_select(None)
elif keyCode == wx.WXK_RIGHT or keyCode == wx.WXK_SPACE:
self.on_menuSlideshowNextSlide_select(None)
elif keyCode == wx.WXK_DOWN or keyCode == wx.WXK_END:
self.on_menuSlideshowLastSlide_select(None)
def on_size(self, event):
size = self.GetClientSize()
self.panel.SetSize(size)
self.components.bufOff.size = size
self.components.htmlView.size = size
self.displayFile()
# always display the file limited to the current size of buffer
def displayFile(self):
if self.filename is not None:
if htmlFile(self.filename):
self.components.htmlView.text = self.filename
self.components.bufOff.visible = 0
self.components.htmlView.visible = 1
#self.components.htmlView.setFocus()
else:
self.components.htmlView.visible = 0
bufOff = self.components.bufOff
bufOff.autoRefresh = 0
imgSize = self.bmp.getSize()
bufSize = bufOff.size
bufOff.clear()
bufOff.autoRefresh = 1
# the image will be displayed scaled centered
# in the current buffer window
# so we need to know the smallest scale dimension
# and use that for scaling
#newSize = self.sizeScaled(imgSize, bufSize[0], heightScale)
# if the size of the image is <= the width and height of the
# buffer then don't scale the image, just center it
if imgSize[0] <= bufSize[0] and imgSize[1] <= bufSize[1]:
xOffset = (bufSize[0] - imgSize[0]) / 2
yOffset = (bufSize[1] - imgSize[1]) / 2
bufOff.drawBitmap(self.bmp, (xOffset, yOffset))
else:
widthScale = (0.0 + bufSize[0]) / imgSize[0]
heightScale = (0.0 + bufSize[1]) / imgSize[1]
if widthScale > heightScale:
scale = heightScale
newSize = (int(round(imgSize[0] * scale)), int(round((imgSize[1] * scale))))
# center horizontally
offset = (bufSize[0] - newSize[0]) / 2
#print "offset", offset
position = (int(round(offset)), 0)
else:
scale = widthScale
newSize = (int(round(imgSize[0] * scale)), int(round((imgSize[1] * scale))))
# center vertically
offset = (bufSize[1] - newSize[1]) / 2
#print "offset", offset
position = (0, int(round(offset)))
bufOff.drawBitmapScaled(self.bmp, position, newSize)
self.components.bufOff.visible = 1
#self.components.bufOff.setFocus()
def openFile(self, path, slideNumber=None):
if self.zip:
self.openFileInZip(path, slideNumber)
return
if not os.path.exists(path):
return
self.filename = path
if htmlFile(path):
title = os.path.split(self.filename)[-1]
else:
f = open(path, 'rb')
tags = EXIF.process_file(f)
f.close()
try:
# the repr() is something like
# (0x0112) Short=8 @ 54
# but the str() is just 1, 8, etc.
orientation = int(str(tags['Image Orientation']))
#print path
#print 'Image Orientation: %d' % orientation
#print 'Thumbnail Orientation: %s' % tags['Thumbnail Orientation']
except:
orientation = 1
self.bmp = graphic.Bitmap(self.filename)
if orientation == 8:
# need to rotate the image
# defaults to clockwise, 0 means counter-clockwise
#print "rotating"
self.bmp.rotate90(0)
elif orientation == 6:
self.bmp.rotate90(1)
size = self.bmp.getSize()
title = os.path.split(self.filename)[-1] + " %d x %d" % size
if slideNumber is not None:
title = title + " Slide: %d of %d" % (slideNumber + 1, len(self.fileList))
self.title = title
self.displayFile()
def openFileInZip(self, path, slideNumber=None):
self.filename = path
if htmlFile(path):
title = os.path.split(self.filename)[-1]
else:
##info = self.zip.NameToInfo[path]
data = self.zip.read(path)
tags = EXIF.process_file(StringIO(data))
#f = open(path, 'rb')
#tags=EXIF.process_file(f)
#f.close()
try:
# the repr() is something like
# (0x0112) Short=8 @ 54
# but the str() is just 1, 8, etc.
orientation = int(str(tags['Image Orientation']))
#print path
#print 'Image Orientation: %d' % orientation
#print 'Thumbnail Orientation: %s' % tags['Thumbnail Orientation']
except:
orientation = 1
self.bmp = graphic.Bitmap()
self.bmp.setImageBits(wx.ImageFromStream(StringIO(data)))
if orientation == 8:
# need to rotate the image
# defaults to clockwise, 0 means counter-clockwise
#print "rotating"
self.bmp.rotate90(0)
elif orientation == 6:
self.bmp.rotate90(1)
size = self.bmp.getSize()
title = os.path.split(self.filename)[-1] + " %d x %d" % size
if slideNumber is not None:
title = title + " Slide: %d of %d" % (slideNumber + 1, len(self.fileList))
self.title = title
self.displayFile()
def displayNextFile(self):
if self.fileList is None:
return
index = self.fileIndex + 1
if self.loop and index == len(self.fileList):
index = 0
if index < len(self.fileList):
self.fileIndex = index
self.openFile(self.fileList[self.fileIndex], self.fileIndex)
# one shot timer
self.clockTimer.start(self.interval, 1)
def doSlideShow(self):
if self.fileList is not None and self.fileList != []:
self.fileIndex = -1
self.displayNextFile()
def on_menuSlideshowLoop_select(self, event):
self.loop = self.menuBar.getChecked('menuSlideshowLoop')
def on_menuSlideshowContinue_select(self, event):
# act as a toggle
if self.clockTimer.isRunning():
self.clockTimer.stop()
else:
self.displayNextFile()
def on_menuSlideshowShowSlides_select(self, event):
self.doSlideShow()
def on_menuSlideshowSetInterval_select(self, event):
interval = str(self.interval / 1000)
result = dialog.textEntryDialog(self, "Time interval between slides (seconds):", "Slide interval", interval)
if result.accepted:
try:
interval = int(result.text) * 1000
self.interval = interval
except:
pass
def buildFileListFromFile(self, path):
try:
f = open(path)
txt = f.read()
f.close()
self.fileList = txt.splitlines()
except:
pass
def buildFileListFromDirectory(self, path, recurse=True):
self.zip = None
self.directory = path
fileList = util.dirwalk(path, ['*'], recurse)
# self.fileList should be filtered here
self.fileList = []
self.fileIndex = -1
for path in fileList:
if imageFile(path) or htmlFile(path):
#print "path", path
self.fileList.append(path)
self.fileList = util.caseinsensitive_sort(self.fileList)
def buildFileListFromZip(self, path):
#print path
self.zip = zipfile.ZipFile(path)
#print self.zip.namelist()
self.directory = None
fileList = self.zip.namelist()
# self.fileList should be filtered here
self.fileList = []
self.fileIndex = -1
for f in fileList:
ext = os.path.splitext(f)[-1].lower()
if ext != '' and ext[0] == '.':
ext = ext[1:]
if ext in ['bmp', 'gif', 'jpeg', 'jpg', 'pcx',
'png', 'pnm', 'tif', 'tiff', 'xbm', 'xpm']:
#if imageFile(f) or htmlFile(f):
#print "path", path
self.fileList.append(f)
#print self.fileList
def on_menuSlideshowChooseZip_select(self, event):
wildcard = "Zip archives (*.zip)|*.ZIP;*.zip"
if 0:
if self.documentPath is None:
dir = ''
filename = '*.txt'
else:
dir = os.path.dirname(self.documentPath)
filename = os.path.basename(self.documentPath)
result = dialog.openFileDialog(self, 'Choose a zip', wildcard=wildcard)
if result.accepted:
self.buildFileListFromZip(result.paths[0])
self.on_menuSlideshowFirstSlide_select(None)
def on_menuSlideshowChooseDirectory_select(self, event):
if self.directory is not None:
directory = self.directory
else:
directory = ''
result = dialog.directoryDialog(self, 'Choose a directory', directory)
if result.accepted:
includeSubDirs = self.menuBar.getChecked('menuOptionsIncludeSubDirectories')
self.buildFileListFromDirectory(result.path, includeSubDirs)
self.on_menuSlideshowFirstSlide_select(None)
def on_menuSlideshowToggleFullScreen_select(self, event):
self.ShowFullScreen(self.menuBar.getChecked('menuSlideshowToggleFullScreen'))
def on_menuSlideshowStopSlides_select(self, event):
# KEA 2004-07-17
# if the slideshow is running in fullscreen mode then
# people can panic not knowing how to stop the slideshow and get
# control back so I made the ESC key stop the slideshow and
# go back to a normal window size
if self.clockTimer.isRunning():
self.clockTimer.stop()
if self.menuBar.getChecked('menuSlideshowToggleFullScreen'):
self.menuBar.setChecked('menuSlideshowToggleFullScreen', False)
self.ShowFullScreen(False)
def on_menuSlideshowFirstSlide_select(self, event):
if self.fileList is not None and self.fileList != []:
self.fileIndex = 0
self.openFile(self.fileList[self.fileIndex], self.fileIndex)
if self.clockTimer.isRunning():
self.clockTimer.start(self.interval, 1)
def on_menuSlideshowPreviousSlide_select(self, event):
if self.fileList is not None and self.fileList != []:
self.fileIndex -= 1
if self.fileIndex == -1:
self.fileIndex = len(self.fileList) - 1
self.openFile(self.fileList[self.fileIndex], self.fileIndex)
if self.clockTimer.isRunning():
self.clockTimer.start(self.interval, 1)
def on_menuSlideshowNextSlide_select(self, event):
if self.fileList is not None and self.fileList != []:
self.fileIndex += 1
if self.fileIndex == len(self.fileList):
self.fileIndex = 0
self.openFile(self.fileList[self.fileIndex], self.fileIndex)
if self.clockTimer.isRunning():
self.clockTimer.start(self.interval, 1)
def on_menuSlideshowGotoSlide_select(self, event):
if self.fileList is not None and self.fileList != []:
result = dialog.textEntryDialog(self, "Slide number:", "Goto slide", str(self.fileIndex + 1))
# this version doesn't alert the user if the line number is out-of-range
# it just fails quietly
if result.accepted:
try:
n = int(result.text) - 1
if n >= 0 and n < len(self.fileList):
self.fileIndex = n
self.openFile(self.fileList[self.fileIndex], self.fileIndex)
except:
pass
def on_mouseUp(self, event):
self.on_menuSlideshowNextSlide_select(None)
def on_menuSlideshowLastSlide_select(self, event):
if self.fileList is not None and self.fileList != []:
self.fileIndex = len(self.fileList) - 1
self.openFile(self.fileList[self.fileIndex], self.fileIndex)
if self.clockTimer.isRunning():
self.clockTimer.start(self.interval, 1)
def on_menuFileOpen_select(self, event):
result = dialog.openFileDialog()
if result.accepted:
self.openFile(result.paths[0])
def on_menuFileOpenSlide_select(self, event):
if self.fileList is not None and self.fileList != []:
path = self.fileList[self.fileIndex]
if imageFile(path):
#print "image", path
try:
viewer = os.path.abspath(os.path.join('..', 'pictureViewer', 'pictureViewer.py'))
if " " in path:
path = '"' + path + '"'
util.runScript(viewer, path)
except:
pass
elif htmlFile(path):
#print "html", path
import webbrowser
webbrowser.open(path, 1, 1)
def on_close(self, event):
self.clockTimer.stop()
self.bmp = None
event.skip()
if __name__ == '__main__':
app = model.Application(SlideShow)
app.MainLoop()
|