Image

Image124um wrote in Imagewxpython

Вопрос.

этом графическом Контексте Rectangl передвигается без проблем. Можно ли так же передвигать Arc ??? (Я пока не знаю как настроить ЖЖ под правильное расставление пробелов.)

import wx
import random
import time

W = 10000
H = 10000
SW = 1000for i in range(1):
            id = wx.NewId()
            dc.SetId(id)
            x = 145
            y = 290
            x2 = 175
            y2 = 260
            xc = 145
            yc = 210
            pen = self.pen
            dc.SetPen(pen)
            dc.SetBrush(wx.Brush((200,2,200)))
            dc.DrawArc( x, y, x2, y2, xc, yc)
            r = wx.Rect( x , y2, x2, y)
            r.Inflate(pen.GetWidth(),pen.GetWidth())
            dc.SetIdBounds(id,r)
            self.objids.append(id)
SH = 1000
klv = 0
colours = [
    "BLACK",
    "BLUE",
    "BLUE VIOLET",
    "BROWN",
    "CYAN",
    "DARK GREY",
    "DARK GREEN",
    "GOLD",
    "GREY",
    "GREEN",
    "MAGENTA",
    "NAVY",
    "PINK",
    "RED",
    "SKY BLUE",
    "VIOLET",
    "YELLOW",
    ]



class MyFrame( wx.Frame ):
    def __init__( self, parent ):
        wx.Frame.__init__( self, parent, size = ( 900 , 450 ) )
        self.canvas = MyPanel( self )

class MyPanel( wx.ScrolledWindow ):
    def __init__( self, parent ):
        wx.ScrolledWindow.__init__( self, parent )
        self.SetVirtualSize((H , W))
        self.SetScrollRate(20,20)
        self.pdc = wx.PseudoDC()
        self.parent = parent
        self.color = "blue"
        self.thickness = 1
        self.pen = wx.Pen(self.color, self.thickness, wx.STIPPLE)
        self.DoDrawing(self.pdc)
#        self.Refresh()
        self.dragid = -1
        self.Bind( wx.EVT_PAINT, self.OnPaint )
        self.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouse)

    def OnPaint(self, event):
        dc = wx.BufferedPaintDC(self)
        self.PrepareDC(dc)
        # we need to clear the dc BEFORE calling PrepareDC
        bg = wx.Brush(self.GetBackgroundColour())
        dc.SetBackground(bg)
        dc.Clear()
        # create a clipping rect from our position and size
        # and the Update Region
        xv, yv = self.GetViewStart()
        dx, dy = self.GetScrollPixelsPerUnit()
        x, y   = (xv * dx, yv * dy)
        rgn = self.GetUpdateRegion()
        rgn.Offset(x,y)
        r = rgn.GetBox()
#        print r
        # draw to the dc using the calculated clipping rect
        self.pdc.DrawToDCClipped(dc,r)

    def DoDrawing(self, dc):
        random.seed()
        self.objids = []
        self.boundsdict = {}

        dc.BeginDrawing()
        for i in range(1):
            id = wx.NewId()
            dc.SetId(id)
            x = 145
            y = 290
            x2 = 175
            y2 = 260
            xc = 145
            yc = 210
            pen = self.pen
            dc.SetPen(pen)
            dc.SetBrush(wx.Brush((200,2,200)))
            dc.DrawArc( x, y, x2, y2, xc, yc)
            r = wx.Rect( x , y2, x2, y)
            r.Inflate(pen.GetWidth(),pen.GetWidth())
            dc.SetIdBounds(id,r)
            self.objids.append(id)
        for i in range(1):
            id = wx.NewId()
            dc.SetId(id)
            x = 445
            y = 190
            x2 = 175
            y2 = 260
            pen = self.pen
            dc.SetPen(pen)
            dc.SetBrush(wx.Brush((200,2,200)))
            dc.DrawRectangle( x, y, x2, y2)
            r = wx.Rect( x , y, x2, y2)
            r.Inflate(pen.GetWidth(),pen.GetWidth())
            dc.SetIdBounds(id,r)
            self.objids.append(id)
        dc.EndDrawing()

    def ConvertEventCoords(self, event):
        xView, yView = self.GetViewStart()
        xDelta, yDelta = self.GetScrollPixelsPerUnit()
        return (event.GetX() + (xView * xDelta),
            event.GetY() + (yView * yDelta))

    def OffsetRect(self, r):
        xView, yView = self.GetViewStart()
        xDelta, yDelta = self.GetScrollPixelsPerUnit()
        r.OffsetXY(-(xView*xDelta),-(yView*yDelta))



    def OnMouse(self, event):
        hitradius = 5
        if event.LeftDown():
            x,y = self.ConvertEventCoords(event)
            l = self.pdc.FindObjectsByBBox(x, y)
#            l = self.pdc.FindObjects(x, y, hitradius)
            for id in l:
                if not self.pdc.GetIdGreyedOut(id):
                    self.dragid = id
 
                    
                    self.lastpos = (event.GetX(),event.GetY())
                    break
        elif event.RightDown():
            x,y = self.ConvertEventCoords(event)
            #l = self.pdc.FindObjectsByBBox(x, y)
            l = self.pdc.FindObjects(x, y, hitradius)
            if l:
                self.pdc.SetIdGreyedOut(l[0], not self.pdc.GetIdGreyedOut(l[0]))
                r = self.pdc.GetIdBounds(l[0])
                r.Inflate(4,4)
                self.OffsetRect(r)
                self.RefreshRect(r, False)
        elif event.Dragging() or event.LeftUp():
            if self.dragid != -1:
                x,y = self.lastpos
                dx = event.GetX() - x
                dy = event.GetY() - y
                r = self.pdc.GetIdBounds(self.dragid)
                self.pdc.TranslateId(self.dragid, dx, dy)
                r2 = self.pdc.GetIdBounds(self.dragid)
                r = r.Union(r2)
                r.Inflate(4,4)
                self.OffsetRect(r)
                self.RefreshRect(r, False)
                self.lastpos = (event.GetX(),event.GetY())

            if event.LeftUp():
                self.dragid = -1

if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = MyFrame( None )
    frame.Show(True)
    app.MainLoop()