Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # PaintApp.py
- # ZZZ ... still in repair
- # 3rd-party edited to work on the Nokia 500
- # Copyright (c) 2009 Nokia Corporation
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- def draw(rect):
- try: canvas.blit(img)
- except: pass
- s60 = 1
- try:
- import appuifw
- except: s60 = 0
- if s60:
- import e32
- import os
- import time
- import operator
- from graphics import *
- from key_codes import *
- if not appuifw.touch_enabled():
- appuifw.note(u"This application only works on devices that support " +
- u"touch input")
- def draw_buttons():
- cv.x_max = canvas.size[0]
- cv.y_max = canvas.size[1]
- cv.box_width = canvas.size[0] / 4
- cv.box_height = canvas.size[1] - 40
- img.rectangle((0, cv.box_height, cv.x_max, cv.y_max),
- fill=cv.BG_COLOR)
- # Draw the buttons at the bottom and the respective text at an offset
- # of 22 pixels
- buttons = [options_button, clear_button, eraser,
- quit_button]
- options = [u'Options', u'Clear', u'Erase', u'Quit']
- for button, text in zip(buttons, options):
- if cv.erase_mode and text == u'Erase':
- img.rectangle(eraser, fill=cv.ERASER_ACTIVE,
- outline=cv.BORDER_COLOR, width=5)
- else:
- img.rectangle(button, outline=cv.BORDER_COLOR, width=5)
- img.text((button[0][0]+22, button[0][1]+25), text,
- fill=cv.BORDER_COLOR)
- draw_palette()
- def bind_buttons():
- canvas.bind(EButton1Down, reset_canvas, clear_button)
- canvas.bind(EButton1Down, options_callback,
- options_button)
- canvas.bind(EButton1Down, set_exit, quit_button)
- canvas.bind(EButton1Down, eraser_callback, eraser)
- def clear_button_bindings():
- canvas.bind(EButton1Down, None, clear_button)
- canvas.bind(EButton1Down, None, options_button)
- canvas.bind(EButton1Down, None, quit_button)
- canvas.bind(EButton1Down, None, eraser)
- canvas.bind(EButton1Down, None)
- def focus_monitor(value):
- if value:
- draw(())
- draw_buttons()
- def set_exit(pos):
- appuifw.app.body = old_body
- canvas.bind(EButton1Down, None)
- canvas = None
- img = None
- appuifw.app.focus = None
- cv.running = False
- def options_callback(pos):
- option = appuifw.popup_menu([u'Save', u'Point/Line Width', u'About'],
- u'Options')
- if option == 0:
- save_callback()
- elif option == 1:
- cv.dot_width = appuifw.query(u'Point/Line Width', 'number',
- cv.dot_width)
- if cv.dot_width is not None:
- if type(cv.dot_width) == int and cv.dot_width < 150:
- cv.dot_width = cv.dot_width
- else:
- appuifw.note(u"Invalid width")
- elif option == 2:
- show_about()
- return
- draw(())
- draw_buttons()
- def show_about():
- img_path = cv.drive + u':\data\python\about.png'
- if cv.orientation == 'landscape' or not os.path.exists(img_path):
- appuifw.note(u"Scribble is Copyright (c) 2009 Nokia Corporation")
- draw(())
- draw_buttons()
- else:
- about_window = Image.open(img_path)
- cv.about_active = True
- clear_button_bindings()
- canvas.blit(about_window)
- canvas.bind(EButton1Up, clear_about_screen, ((0, 0),
- (cv.x_max, cv.y_max)))
- cv.about_timer = e32.Ao_timer()
- cv.about_timer.after(5, clear_about_screen)
- def clear_about_screen(pos=(0, 0)):
- if cv.about_timer is not None:
- cv.about_timer.cancel()
- cv.about_timer = None
- canvas.bind(EButton1Up, None, ((0, 0), (cv.x_max, cv.y_max)))
- draw(())
- cv.bind_palette = True
- draw_buttons()
- bind_buttons()
- cv.about_active = False
- def eraser_callback(pos):
- # The cv.dot_width and fill_color change in event_callback when cv.erase_mode
- # changes
- cv.erase_mode = not cv.erase_mode
- draw_buttons()
- def save_callback():
- pass ###ZZZ
- '''
- if not cv.saving_file:
- cv.saving_file = True
- save_dir = cv.drive + u":\data\python\"
- if not os.path.exists(save_dir):
- os.mkdir(save_dir)
- filename = save_dir + unicode(time.strftime("%d%m%Y%H%M%S", time.localtime())) + u".jpg"
- img.save(filename, quality=100)
- appuifw.note(u"Saved :" + unicode(filename))
- draw_blit(())
- cv.saving_file = False
- draw_buttons()'''
- def set_BRUSH_COLOR(pos):
- cv.BRUSH_COLOR = cv.color
- def draw_and_bind_color(nul):
- if cv.no_of_colors == 7:
- # Draw all colors after this in another row
- cv.top_left_x = 0
- cv.bottom_right_x = cv.color_box_width
- cv.bottom_right_y -= 20
- cv.top_left_y -= 20
- cv.top_left = (cv.top_left_x, cv.top_left_y)
- cv.bottom_right = (cv.bottom_right_x, cv.bottom_right_y)
- # Draw the color rectangle and bind a function which sets the brush
- # color
- img.rectangle((cv.top_left, cv.bottom_right),
- fill=colors[cv.color])
- if cv.bind_palette:
- canvas.bind(EButton1Down,
- lambda pos: set_cv.BRUSH_COLOR(pos, colors[cv.color]),
- (cv.top_left, cv.bottom_right))
- cv.top_left_x += cv.color_box_width
- cv.bottom_right_x += cv.color_box_width
- cv.no_of_colors += 1
- def draw_palette():
- cv.no_of_colors = 0
- cv.color_box_width = canvas.size[0] / 7
- cv.top_left_x = 0
- cv.top_left_y = canvas.size[1] - 60
- cv.bottom_right_x = cv.color_box_width
- cv.bottom_right_y = cv.box_height - 1
- map(draw_and_bind_color, sorted(colors))
- if cv.bind_palette:
- cv.bind_palette = False
- def reset_canvas(pos=(0, 0)):
- img.clear(cv.BG_COLOR)
- prev_x = 0
- prev_y = 0
- cv.erase_mode = False
- canvas.clear(cv.BG_COLOR)
- draw_buttons()
- def check_orientation():
- if not cv.orientation_changed:
- cv.orientation_changed = True
- else:
- cv.orientation_changed = False
- cv.x_max = canvas.size[0]
- cv.y_max = canvas.size[1]
- if cv.erase_mode:
- dot_size = cv.dot_width * 2
- outline_color = cv.BG_COLOR
- fill_color = cv.BG_COLOR
- else:
- dot_size = cv.dot_width
- outline_color = cv.BRUSH_COLOR
- fill_color = cv.BRUSH_COLOR
- # Ignore the touch events in the region where buttons are drawn or if
- # about screen is active
- if event['pos'][1] > (cv.y_max - 83 - (dot_size / 2)) or cv.about_active:
- return
- if event['type'] in [EButton1Down, EButton1Up]:
- img.point((event['pos'][0], event['pos'][1]),
- outline=outline_color, width=dot_size, fill=fill_color)
- img.point((event['pos'][0], event['pos'][1]),
- outline=outline_color, width=dot_size, fill=fill_color)
- elif event['type'] == EDrag:
- rect = (prev_x, prev_y, event['pos'][0], event['pos'][1])
- img.line(rect, outline=outline_color, width=dot_size,
- fill=fill_color)
- img.line(rect, outline=outline_color, width=dot_size,
- fill=fill_color)
- prev_x = event['pos'][0]
- prev_y = event['pos'][1]
- def run():
- while cv.running:
- if cv.orientation_changed:
- if cv.orientation == 'landscape':
- img.transpose(ROTATE_90)
- elif cv.orientation == 'portrait':
- img.transpose(ROTATE_270)
- clear_button_bindings()
- if cv.about_active:
- clear_about_screen()
- cv.bind_palette = True
- draw_buttons()
- bind_buttons()
- cv.orientation_changed = False
- draw(())
- e32.ao_sleep(0.02)
- quit()
- def quit():
- appuifw.app.exit_key_handler = None
- cv.running = False
- class Cv():
- BG_COLOR = 0xffffff # white
- BORDER_COLOR = 0x000000 # black
- BRUSH_COLOR = 0x000000 # black
- ERASER_ACTIVE = 0xFF0000 # Red
- running = True
- erase_mode = False
- saving_file = False
- orientation_changed = False
- bind_palette = True
- about_active = False
- about_timer = None
- dot_width = 4
- x_max = 0
- y_max = 0
- drive = unicode(os.getcwd()[0])
- no_of_colors = 0
- color = 'Green'
- #
- def redraw_callback(rect):
- try:
- if cv.about_active:
- canvas.blit(about_window)
- except: pass
- if rect == (0, 0, cv.y_max, cv.x_max) and cv.orientation == 'portrait':
- cv.orientation = 'landscape'
- check_orientation()
- elif rect == (0, 0, cv.y_max, cv.x_max) and cv.orientation == 'landscape':
- cv.orientation = 'portrait'
- check_orientation()
- def event_callback(event):
- if not event['type'] in [EButton1Up, EButton1Down, EDrag]:
- return
- #
- appuifw.app.exit_key_handler = quit
- canvas = appuifw.Canvas(event_callback=event_callback,
- redraw_callback=draw)
- old_body = appuifw.app.body
- appuifw.app.body = canvas
- appuifw.app.directional_pad=False
- appuifw.app.screen = 'large'
- appuifw.app.orientation='portrait'
- # appuifw.app.focus = focus_monitor
- colors = {'Black': 0x000000, 'Blue': 0x0000FF, 'Brown': 0xA52A2A,
- 'Gray': 0x808080, 'Green': 0x008000, 'Maroon': 0x800000,
- 'Orange': 0xFFA500, 'Pink': 0xFFC0CB, 'Purple': 0x800080,
- 'Silver': 0xC0C0C0, 'Violet': 0xEE82EE, 'Yellow': 0xFFFF00,
- 'Red': 0xFF0000, 'Lime': 0x00FF00}
- cv=Cv()
- cv.box_width = canvas.size[0] / 4
- cv.box_height = canvas.size[1] - 40
- cv.color_box_width = canvas.size[0] / 7
- cv.bottom_right_x = cv.color_box_width
- cv.bottom_right_y = cv.box_height - 1
- cv.top_left_x = 0
- cv.top_left_y = canvas.size[1] - 60
- cv.top_left = (cv.top_left_x, cv.top_left_y)
- cv.bottom_right = (cv.bottom_right_x, cv.bottom_right_y)
- options_button = ((0, cv.box_height),
- (cv.box_width, cv.y_max))
- clear_button = ((cv.box_width, cv.box_height),
- (cv.box_width*2, cv.y_max))
- eraser = ((cv.box_width*2, cv.box_height),
- (cv.box_width*3, cv.y_max))
- quit_button = ((cv.box_width*3, cv.box_height),
- (cv.box_width*4, cv.y_max))
- if canvas.size[0] > canvas.size[1]:
- cv.orientation = 'landscape'
- else:
- cv.orientation = 'portrait'
- img = Image.new(canvas.size)
- draw_buttons()
- bind_buttons()
- run()
- rpl='''
- '''.strip().splitlines()
- if not s60:
- for z in rpl: zzz=zzz.replace(z,'cv.'+z)
- print zzz
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement