src/extras/imgviewer.py
changeset 0 ca70ae20a155
equal deleted inserted replaced
-1:000000000000 0:ca70ae20a155
       
     1 # Copyright (c) 2005 Nokia Corporation
       
     2 #
       
     3 # Licensed under the Apache License, Version 2.0 (the "License");
       
     4 # you may not use this file except in compliance with the License.
       
     5 # You may obtain a copy of the License at
       
     6 #
       
     7 #     http://www.apache.org/licenses/LICENSE-2.0
       
     8 #
       
     9 # Unless required by applicable law or agreed to in writing, software
       
    10 # distributed under the License is distributed on an "AS IS" BASIS,
       
    11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    12 # See the License for the specific language governing permissions and
       
    13 # limitations under the License.
       
    14 
       
    15 
       
    16 import appuifw, e32, os
       
    17 from graphics import *
       
    18 import key_codes
       
    19 
       
    20 appuifw.app.screen='full'
       
    21 appuifw.app.body=canvas=appuifw.Canvas()
       
    22 backup_image=Image.new(canvas.size)
       
    23 canvas.clear(0)
       
    24 center=[0,0]
       
    25 zoom=1
       
    26 zoomstepindex=0
       
    27 screensize=canvas.size
       
    28 screenrect=(0,0,screensize[0],screensize[1])
       
    29 step=30
       
    30 textloc=(screensize[0]*0.3,screensize[1]*.5)
       
    31 
       
    32 if e32.in_emulator():
       
    33     imagedir=u'c:\\images'
       
    34 else:
       
    35     imagedir=u'e:\\images'
       
    36 files=map(unicode,os.listdir(imagedir))
       
    37 
       
    38 index=appuifw.selection_list(files)
       
    39 
       
    40 lock=e32.Ao_lock()
       
    41 
       
    42 def fullupdate():
       
    43     backup_image.clear(bgcolor)
       
    44     update()
       
    45     
       
    46 def nextpic():
       
    47     global index,finished
       
    48     index=(index+1)%len(files)
       
    49     finished=1
       
    50     loaded_image.stop()
       
    51     lock.signal()
       
    52 
       
    53 def prevpic():
       
    54     global index,finished
       
    55     index=(index-1)%len(files)
       
    56     finished=1
       
    57     loaded_image.stop()
       
    58     lock.signal()
       
    59 
       
    60 def zoomin():
       
    61     global zoomstepindex,zoom
       
    62     if zoomstepindex < (len(zoomsteps)-1):
       
    63         zoomstepindex+=1
       
    64     zoom=zoomsteps[zoomstepindex]
       
    65     fullupdate()
       
    66 
       
    67 def zoomout():
       
    68     global zoomstepindex
       
    69     if zoomstepindex > 0:
       
    70         zoomstepindex-=1
       
    71     zoom=zoomsteps[zoomstepindex]
       
    72     backup_image.clear(bgcolor)
       
    73     fullupdate()
       
    74 
       
    75 
       
    76 def isvalidcenter(c):
       
    77     iw,ih=(loaded_image.size[0],loaded_image.size[1])
       
    78     srcsize=(int(screensize[0]/zoom),int(screensize[1]/zoom))
       
    79     vw,vh=(srcsize[0]/2,srcsize[1]/2)    
       
    80     return (c[0]+vw<iw and c[0]-vw>=0 and
       
    81             c[1]+vh<ih and c[1]-vh>=0)
       
    82 def move(delta):
       
    83     global center
       
    84     c=center
       
    85     for k in range(1,4):
       
    86         t=[c[0]+int(delta[0]*k*20/zoom),
       
    87            c[1]+int(delta[1]*k*20/zoom)]
       
    88         center=t
       
    89         update()
       
    90 
       
    91 bgcolor=0
       
    92 
       
    93 canvas.bind(key_codes.EKey3,nextpic)
       
    94 canvas.bind(key_codes.EKey1,prevpic)
       
    95 canvas.bind(key_codes.EKey5,zoomin)
       
    96 canvas.bind(key_codes.EKey0,zoomout)
       
    97 canvas.bind(key_codes.EKeyLeftArrow,lambda:move((-1,0)))
       
    98 canvas.bind(key_codes.EKeyRightArrow,lambda:move((1,0)))
       
    99 canvas.bind(key_codes.EKeyUpArrow,lambda:move((0,-1)))
       
   100 canvas.bind(key_codes.EKeyDownArrow,lambda:move((0,1)))
       
   101 
       
   102 def rect_intersection(r1,r2):
       
   103     return (max(r1[0],r2[0]),max(r1[1],r2[1]),
       
   104             min(r1[2],r2[2]),min(r1[3],r2[3]))
       
   105     
       
   106 def update():
       
   107     global zoom
       
   108     zoom=zoomsteps[zoomstepindex]
       
   109     # We convert the screen rect into image coordinates, compute its
       
   110     # intersection with the image rect and transform it back to screen
       
   111     # coordinates.
       
   112     imgrect=(0,0,loaded_image.size[0],loaded_image.size[1])    
       
   113     ss=(int(screensize[0]/zoom),int(screensize[1]/zoom))
       
   114     screenrect_imgcoords=(center[0]-ss[0]/2,center[1]-ss[1]/2,
       
   115                           center[0]+ss[0]/2,center[1]+ss[1]/2)
       
   116     sourcerect=rect_intersection(screenrect_imgcoords,imgrect)
       
   117     targetrect=(int((sourcerect[0]-center[0])*zoom+screensize[0]/2),
       
   118                 int((sourcerect[1]-center[1])*zoom+screensize[1]/2),
       
   119                 int((sourcerect[2]-center[0])*zoom+screensize[0]/2),
       
   120                 int((sourcerect[3]-center[1])*zoom+screensize[1]/2))
       
   121     backup_image.clear(bgcolor)
       
   122     backup_image.blit(loaded_image,source=sourcerect,target=targetrect,scale=1)
       
   123     if not finished:
       
   124         backup_image.text(textloc,u'Loading....',(255,255,0))
       
   125     backup_image.text((0,10),files[index],(0,255,0))
       
   126     canvas.blit(backup_image)
       
   127 
       
   128 global finished
       
   129 finished=0
       
   130 def finishload(err):
       
   131     global finished
       
   132     finished=1
       
   133 
       
   134 running=1
       
   135 def quit():
       
   136     global running,lock
       
   137     running=0
       
   138     lock.signal()
       
   139     
       
   140 appuifw.app.exit_key_handler=quit
       
   141 backup_image.clear(bgcolor)
       
   142 
       
   143 selected_file=imagedir+"\\"+files[index]
       
   144 imginfo=Image.inspect(selected_file)
       
   145 imgsize=imginfo['size']
       
   146 loaded_image=Image.new(imgsize)
       
   147 
       
   148 im=None
       
   149 while running:
       
   150     selected_file=imagedir+"\\"+files[index]
       
   151     imgsize=Image.inspect(selected_file)['size']
       
   152     backup_image.text(textloc,u'Loading.',(255,255,0))
       
   153     finished=0
       
   154     if imgsize != loaded_image.size:
       
   155         loaded_image=Image.new(imgsize)
       
   156     loaded_image.load(selected_file, callback=finishload)
       
   157     backup_image.text(textloc,u'Loading..',(255,255,0))
       
   158     zoomsteps=[1.*screensize[0]/loaded_image.size[0],.25,.5,1]
       
   159     zoomstepindex=0
       
   160     center=[loaded_image.size[0]/2,loaded_image.size[1]/2]    
       
   161     backup_image.text(textloc,u'Loading...',(255,255,0))
       
   162     while not finished:
       
   163         update()
       
   164         e32.ao_sleep(0.5)
       
   165     fullupdate()
       
   166     lock.wait()
       
   167 loaded_image=None
       
   168 backup_image=None