Advertisement
here2share

# PaintApp.py # ZZZ ... still in repair

Jul 25th, 2017
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # PaintApp.py
  2. # ZZZ ... still in repair
  3.  
  4. # 3rd-party edited to work on the Nokia 500
  5.  
  6. # Copyright (c) 2009 Nokia Corporation
  7. #
  8. # Licensed under the Apache License, Version 2.0 (the "License");
  9. # you may not use this file except in compliance with the License.
  10. # You may obtain a copy of the License at
  11. #
  12. #    http://www.apache.org/licenses/LICENSE-2.0
  13. #
  14. # Unless required by applicable law or agreed to in writing, software
  15. # distributed under the License is distributed on an "AS IS" BASIS,
  16. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. # See the License for the specific language governing permissions and
  18. # limitations under the License.
  19.  
  20. def draw(rect):
  21.     try: canvas.blit(img)
  22.     except: pass
  23.  
  24. s60 = 1
  25. try:
  26.     import appuifw
  27. except: s60 = 0
  28.  
  29. if s60:
  30.     import e32
  31.     import os
  32.     import time
  33.     import operator
  34.     from graphics import *
  35.     from key_codes import *
  36.  
  37.     if not appuifw.touch_enabled():
  38.         appuifw.note(u"This application only works on devices that support " +
  39.                      u"touch input")
  40.  
  41.     def draw_buttons():
  42.         cv.x_max = canvas.size[0]
  43.         cv.y_max = canvas.size[1]
  44.         cv.box_width = canvas.size[0] / 4
  45.         cv.box_height = canvas.size[1] - 40
  46.         img.rectangle((0, cv.box_height, cv.x_max, cv.y_max),
  47.                             fill=cv.BG_COLOR)
  48.  
  49.         # Draw the buttons at the bottom and the respective text at an offset
  50.         # of 22 pixels
  51.         buttons = [options_button, clear_button, eraser,
  52.                    quit_button]
  53.         options = [u'Options', u'Clear', u'Erase', u'Quit']
  54.         for button, text in zip(buttons, options):
  55.             if cv.erase_mode and text == u'Erase':
  56.                 img.rectangle(eraser, fill=cv.ERASER_ACTIVE,
  57.                                 outline=cv.BORDER_COLOR, width=5)
  58.             else:
  59.                 img.rectangle(button, outline=cv.BORDER_COLOR, width=5)
  60.             img.text((button[0][0]+22, button[0][1]+25), text,
  61.                           fill=cv.BORDER_COLOR)
  62.         draw_palette()
  63.  
  64.     def bind_buttons():
  65.         canvas.bind(EButton1Down, reset_canvas, clear_button)
  66.         canvas.bind(EButton1Down, options_callback,
  67.                          options_button)
  68.         canvas.bind(EButton1Down, set_exit, quit_button)
  69.         canvas.bind(EButton1Down, eraser_callback, eraser)
  70.  
  71.     def clear_button_bindings():
  72.         canvas.bind(EButton1Down, None, clear_button)
  73.         canvas.bind(EButton1Down, None, options_button)
  74.         canvas.bind(EButton1Down, None, quit_button)
  75.         canvas.bind(EButton1Down, None, eraser)
  76.         canvas.bind(EButton1Down, None)
  77.  
  78.     def focus_monitor(value):
  79.         if value:
  80.             draw(())
  81.             draw_buttons()
  82.  
  83.     def set_exit(pos):
  84.         appuifw.app.body = old_body
  85.         canvas.bind(EButton1Down, None)
  86.         canvas = None
  87.         img = None
  88.         appuifw.app.focus = None
  89.         cv.running = False
  90.  
  91.     def options_callback(pos):
  92.         option = appuifw.popup_menu([u'Save', u'Point/Line Width', u'About'],
  93.                                     u'Options')
  94.         if option == 0:
  95.             save_callback()
  96.         elif option == 1:
  97.             cv.dot_width = appuifw.query(u'Point/Line Width', 'number',
  98.                                        cv.dot_width)
  99.             if cv.dot_width is not None:
  100.                 if type(cv.dot_width) == int and cv.dot_width < 150:
  101.                     cv.dot_width = cv.dot_width
  102.                 else:
  103.                     appuifw.note(u"Invalid width")
  104.         elif option == 2:
  105.             show_about()
  106.             return
  107.         draw(())
  108.         draw_buttons()
  109.  
  110.     def show_about():
  111.         img_path = cv.drive + u':\data\python\about.png'
  112.         if cv.orientation == 'landscape' or not os.path.exists(img_path):
  113.             appuifw.note(u"Scribble is Copyright (c) 2009 Nokia Corporation")
  114.             draw(())
  115.             draw_buttons()
  116.         else:
  117.             about_window = Image.open(img_path)
  118.             cv.about_active = True
  119.             clear_button_bindings()
  120.             canvas.blit(about_window)
  121.             canvas.bind(EButton1Up, clear_about_screen, ((0, 0),
  122.                              (cv.x_max, cv.y_max)))
  123.             cv.about_timer = e32.Ao_timer()
  124.             cv.about_timer.after(5, clear_about_screen)
  125.  
  126.     def clear_about_screen(pos=(0, 0)):
  127.         if cv.about_timer is not None:
  128.             cv.about_timer.cancel()
  129.             cv.about_timer = None
  130.         canvas.bind(EButton1Up, None, ((0, 0), (cv.x_max, cv.y_max)))
  131.         draw(())
  132.         cv.bind_palette = True
  133.         draw_buttons()
  134.         bind_buttons()
  135.         cv.about_active = False
  136.  
  137.     def eraser_callback(pos):
  138.         # The cv.dot_width and fill_color change in event_callback when cv.erase_mode
  139.         # changes
  140.         cv.erase_mode = not cv.erase_mode
  141.         draw_buttons()
  142.  
  143.     def save_callback():
  144.         pass ###ZZZ
  145.         '''
  146.         if not cv.saving_file:
  147.             cv.saving_file = True
  148.             save_dir = cv.drive + u":\data\python\"
  149.             if not os.path.exists(save_dir):
  150.                 os.mkdir(save_dir)
  151.             filename = save_dir + unicode(time.strftime("%d%m%Y%H%M%S", time.localtime())) + u".jpg"
  152.             img.save(filename, quality=100)
  153.             appuifw.note(u"Saved :" + unicode(filename))
  154.             draw_blit(())
  155.             cv.saving_file = False
  156.             draw_buttons()'''
  157.  
  158.     def set_BRUSH_COLOR(pos):
  159.         cv.BRUSH_COLOR = cv.color
  160.  
  161.     def draw_and_bind_color(nul):
  162.         if cv.no_of_colors == 7:
  163.             # Draw all colors after this in another row
  164.             cv.top_left_x = 0
  165.             cv.bottom_right_x = cv.color_box_width
  166.             cv.bottom_right_y -= 20
  167.             cv.top_left_y -= 20
  168.         cv.top_left = (cv.top_left_x, cv.top_left_y)
  169.         cv.bottom_right = (cv.bottom_right_x, cv.bottom_right_y)
  170.         # Draw the color rectangle and bind a function which sets the brush
  171.         # color
  172.         img.rectangle((cv.top_left, cv.bottom_right),
  173.                             fill=colors[cv.color])
  174.         if cv.bind_palette:
  175.             canvas.bind(EButton1Down,
  176.                      lambda pos: set_cv.BRUSH_COLOR(pos, colors[cv.color]),
  177.                      (cv.top_left, cv.bottom_right))
  178.         cv.top_left_x += cv.color_box_width
  179.         cv.bottom_right_x += cv.color_box_width
  180.         cv.no_of_colors += 1
  181.  
  182.     def draw_palette():
  183.         cv.no_of_colors = 0
  184.         cv.color_box_width = canvas.size[0] / 7
  185.         cv.top_left_x = 0
  186.         cv.top_left_y = canvas.size[1] - 60
  187.         cv.bottom_right_x = cv.color_box_width
  188.         cv.bottom_right_y = cv.box_height - 1
  189.        
  190.         map(draw_and_bind_color, sorted(colors))
  191.         if cv.bind_palette:
  192.             cv.bind_palette = False
  193.  
  194.     def reset_canvas(pos=(0, 0)):
  195.         img.clear(cv.BG_COLOR)
  196.         prev_x = 0
  197.         prev_y = 0
  198.         cv.erase_mode = False
  199.         canvas.clear(cv.BG_COLOR)
  200.         draw_buttons()
  201.  
  202.     def check_orientation():
  203.         if not cv.orientation_changed:
  204.             cv.orientation_changed = True
  205.         else:
  206.             cv.orientation_changed = False
  207.         cv.x_max = canvas.size[0]
  208.         cv.y_max = canvas.size[1]
  209.  
  210.         if cv.erase_mode:
  211.             dot_size = cv.dot_width * 2
  212.             outline_color = cv.BG_COLOR
  213.             fill_color = cv.BG_COLOR
  214.         else:
  215.             dot_size = cv.dot_width
  216.             outline_color = cv.BRUSH_COLOR
  217.             fill_color = cv.BRUSH_COLOR
  218.  
  219.         # Ignore the touch events in the region where buttons are drawn or if
  220.         # about screen is active
  221.         if event['pos'][1] > (cv.y_max - 83 - (dot_size / 2)) or cv.about_active:
  222.             return
  223.  
  224.         if event['type'] in [EButton1Down, EButton1Up]:
  225.             img.point((event['pos'][0], event['pos'][1]),
  226.                     outline=outline_color, width=dot_size, fill=fill_color)
  227.             img.point((event['pos'][0], event['pos'][1]),
  228.                     outline=outline_color, width=dot_size, fill=fill_color)
  229.         elif event['type'] == EDrag:
  230.             rect = (prev_x, prev_y, event['pos'][0], event['pos'][1])
  231.             img.line(rect, outline=outline_color, width=dot_size,
  232.                            fill=fill_color)
  233.             img.line(rect, outline=outline_color, width=dot_size,
  234.                            fill=fill_color)
  235.         prev_x = event['pos'][0]
  236.         prev_y = event['pos'][1]
  237.  
  238.     def run():
  239.         while cv.running:
  240.             if cv.orientation_changed:
  241.                 if cv.orientation == 'landscape':
  242.                     img.transpose(ROTATE_90)
  243.                 elif cv.orientation == 'portrait':
  244.                     img.transpose(ROTATE_270)
  245.                 clear_button_bindings()
  246.             if cv.about_active:
  247.                 clear_about_screen()
  248.             cv.bind_palette = True
  249.             draw_buttons()
  250.             bind_buttons()
  251.             cv.orientation_changed = False
  252.             draw(())
  253.             e32.ao_sleep(0.02)
  254.  
  255.         quit()
  256.  
  257.     def quit():
  258.         appuifw.app.exit_key_handler = None
  259.         cv.running = False
  260.  
  261.     class Cv():
  262.         BG_COLOR = 0xffffff # white
  263.         BORDER_COLOR = 0x000000 # black
  264.         BRUSH_COLOR = 0x000000 # black
  265.         ERASER_ACTIVE = 0xFF0000 # Red
  266.  
  267.         running = True
  268.         erase_mode = False
  269.         saving_file = False
  270.         orientation_changed = False
  271.         bind_palette = True
  272.         about_active = False
  273.         about_timer = None
  274.         dot_width = 4
  275.         x_max = 0
  276.         y_max = 0
  277.         drive = unicode(os.getcwd()[0])
  278.         no_of_colors = 0
  279.         color = 'Green'
  280.     #
  281.  
  282.     def redraw_callback(rect):
  283.         try:
  284.             if cv.about_active:
  285.                 canvas.blit(about_window)
  286.         except: pass
  287.  
  288.         if rect == (0, 0, cv.y_max, cv.x_max) and                                           cv.orientation == 'portrait':
  289.             cv.orientation = 'landscape'
  290.             check_orientation()
  291.         elif rect == (0, 0, cv.y_max, cv.x_max) and                                            cv.orientation == 'landscape':
  292.             cv.orientation = 'portrait'
  293.             check_orientation()
  294.  
  295.     def event_callback(event):
  296.         if not event['type'] in [EButton1Up, EButton1Down, EDrag]:
  297.             return
  298.     #
  299.  
  300.     appuifw.app.exit_key_handler = quit
  301.     canvas = appuifw.Canvas(event_callback=event_callback,
  302.                                  redraw_callback=draw)
  303.     old_body = appuifw.app.body
  304.     appuifw.app.body = canvas
  305.     appuifw.app.directional_pad=False
  306.     appuifw.app.screen = 'large'
  307.     appuifw.app.orientation='portrait'
  308.     # appuifw.app.focus = focus_monitor
  309.  
  310.     colors = {'Black': 0x000000, 'Blue': 0x0000FF, 'Brown': 0xA52A2A,
  311.               'Gray': 0x808080, 'Green': 0x008000, 'Maroon': 0x800000,
  312.               'Orange': 0xFFA500, 'Pink': 0xFFC0CB, 'Purple': 0x800080,
  313.               'Silver': 0xC0C0C0, 'Violet': 0xEE82EE, 'Yellow': 0xFFFF00,
  314.               'Red': 0xFF0000, 'Lime': 0x00FF00}
  315.  
  316.     cv=Cv()
  317.     cv.box_width = canvas.size[0] / 4
  318.     cv.box_height = canvas.size[1] - 40
  319.     cv.color_box_width = canvas.size[0] / 7
  320.     cv.bottom_right_x = cv.color_box_width
  321.     cv.bottom_right_y = cv.box_height - 1
  322.     cv.top_left_x = 0
  323.     cv.top_left_y = canvas.size[1] - 60
  324.     cv.top_left = (cv.top_left_x, cv.top_left_y)
  325.     cv.bottom_right = (cv.bottom_right_x, cv.bottom_right_y)
  326.     options_button = ((0, cv.box_height),
  327.                            (cv.box_width, cv.y_max))
  328.     clear_button = ((cv.box_width, cv.box_height),
  329.                          (cv.box_width*2, cv.y_max))
  330.     eraser = ((cv.box_width*2, cv.box_height),
  331.                    (cv.box_width*3, cv.y_max))
  332.     quit_button = ((cv.box_width*3, cv.box_height),
  333.                         (cv.box_width*4, cv.y_max))
  334.  
  335.     if canvas.size[0] > canvas.size[1]:
  336.         cv.orientation = 'landscape'
  337.     else:
  338.         cv.orientation = 'portrait'
  339.    
  340.     img = Image.new(canvas.size)
  341.        
  342.     draw_buttons()
  343.     bind_buttons()
  344.     run()
  345.  
  346. rpl='''
  347. '''.strip().splitlines()
  348. if not s60:
  349.     for z in rpl: zzz=zzz.replace(z,'cv.'+z)
  350.     print zzz
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement