FAQ
Hi

My first hack at a Patch Panel application is progressing. To
facilitate rapid screen redraw I planned to use
the Pixmap class. However I cannot understand how the same objects
that are absolutely referenced to my
main Window and also written to the Pixmap, appear row inverted when
retrieved from the Pixmap and
displayed alongside the main Window-written set? The graphics
positioning is upside down but the text
drawing in the Pixmap is rightside up.

Cheers
Colin Brown

Code on Mac OSX:


from GUI import Application, Document, Window, FileType, View, Frame,
Pixmap
from GUI.StdColors import black, yellow, green
from GUI.Geometry import pt_in_rect

class myApp(Application):
def __init__(self, title=None):
Application.__init__(self, title=title)
self.item_type = FileType(name="Item Document", suffix = 'patch')
self.file_type = self.item_type
self.pm = Pixmap(450,800)
def open_app(self):
self.new_cmd()
def make_document(self, fileref):
return ItemDoc(file_type = self.item_type)
def make_window(self, document):
win = Window(size=(950,800), title='Patch Panel',
document=document)
view = ItemView(model=document)
win.place(view, left=0, top=0, right=0, bottom=0, sticky='nsew')
win.show()
def zero_windows_allowed(self):
return

class ItemView(View, Frame):
def __init__(self,*args, **kargs):
View.__init__(self,*args, **kargs)
for n in range(15):
for m in range(8):
self.model.add_item(Item((50*(m+1),50*(n+1)), '%03i' % (1+m
+8*n,)))
def draw(self, canvas, update_rect):
canvas.fillcolor = yellow
canvas.pencolor = black
for item in self.model.items:
item.draw(canvas)
A.pm.draw(canvas,(0,0,450,800),(500,0,950,800))
def mouse_move(self, ev):
print self.model.onItem(ev.position)

class ItemDoc(Document):
def new_contents(self):
self.items = []
def add_item(self, item):
self.changed()
self.notify_views('item_changed', item)
self.items.append(item)
item.setpm(1)
A.pm.with_canvas(item.draw)
item.setpm(0)
def onItem(self, xy):
for n in range(len(self.items)):
if self.items[n].contains(xy):
return n+1
return 0

class Item:
def __init__(self, c, t):
self.centre = c
self.rect, self.text = (c[0]-20,c[1]-20,c[0]+20,c[1]+20), t
self.pm = 0
def contains(self, xy):
return pt_in_rect(xy, self.rect)
def setpm(self,state):
self.pm = state
def draw(self, canvas):
if self.pm:
canvas.fillcolor = green
canvas.pencolor = black
else:
canvas.fillcolor = yellow
l,t,r,b = self.rect
canvas.newpath()
canvas.moveto(l,t)
canvas.lineto(r,t)
canvas.lineto(r,b)
canvas.lineto((r+l)/2,b+3) # Help debug pixmap drawing problem
canvas.lineto(l,b)
canvas.closepath()
canvas.fill_stroke()
canvas.moveto(l+9,t+24)
canvas.show_text(self.text)

A = myApp('Patch Panel')
A.run()

Search Discussions

  • Greg Ewing at Dec 15, 2010 at 10:37 pm

    Colin Brown wrote:
    I cannot understand how the same objects that
    are absolutely referenced to my
    main Window and also written to the Pixmap, appear row inverted when
    retrieved from the Pixmap
    It's because Cocoa's default coordinate system for graphics
    ports has the origin in the lower left with X upwards, like
    Postscript. I need to invert the coordinate system to make
    it match the window coordinates.
    The graphics
    positioning is upside down but the text
    drawing in the Pixmap is rightside up.
    NSGraphicsContext has an isFlipped property that Cocoa uses
    to flip the text. I need to get that set to True, although
    that turns out not to be so straightforward for a graphics
    context attached to a bitmap.

    I'm making progress, though -- I've got both the text *and*
    the graphics upside down now. :-)

    --
    Greg
  • Greg Ewing at Dec 17, 2010 at 10:02 pm

    Colin Brown wrote:
    The graphics
    positioning is upside down but the text
    drawing in the Pixmap is rightside up.
    I have fully solved this problem now and posted a new release
    to fix it.

    --
    Greg

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppygui @
categoriespython
postedDec 14, '10 at 8:08a
activeDec 17, '10 at 10:02p
posts3
users2
websitecosc.canterbury.ac.nz...

2 users in discussion

Greg Ewing: 2 posts Colin Brown: 1 post

People

Translate

site design / logo © 2019 Grokbase