예제 #1
0
    def __init__(self, application):
        BaseCanvas.__init__(self, application)
        self.origin = Origin()
        self.grid = Grid()
        self.guides = Guides()
        self.selection = Selection()

        #        self.gradients = []
        #        grad = Gradient(type=LINEAR, name="1", x=0, y=0, x1=0, y1=0)
        #        grad.clear()
        #        grad.add_new_color(GradientColor(1.0, 0.0, 0.0, 1.0, 0.142))
        #        grad.add_new_color(GradientColor(1.0, 1.0, 0.0, 1.0, 0.285))
        #        grad.add_new_color(GradientColor(0.0, 1.0, 0.0, 1.0, 0.428))
        #        grad.add_new_color(GradientColor(0.0, 1.0, 1.0, 1.0, 0.571))
        #        grad.add_new_color(GradientColor(0.0, 0.0, 1.0, 1.0, 0.714))
        #        grad.add_new_color(GradientColor(1.0, 0.0, 1.0, 1.0, 0.857))
        #        grad.update()
        #        self.gradients.append(grad)

        self.document = Document()
        #self.document.pages[0].children = list()
        #self.document.pages = list()

        page = Page()
        self.total = Size()

        self.document.pages.append(page)
        self.zoom = 1.0
        self.origin.x = 0 # XXX
        self.origin.y = 0 # XXX
        self.border = 25

        self.pick = False
        self.updated = False
        #self.get_child() = None
        self.child = None
        self.stop_cursor_change = False

        self.horizontal_ruler = None
        self.vertical_ruler = None
        self.clipboard = None

        self.hints = False

        self.motions = 0
예제 #2
0
    def __init__(self, application):
        BaseCanvas.__init__(self, application)
        self.origin = Origin()
        self.grid = Grid()
        self.guides = Guides()
        self.selection = Selection()

        #        self.gradients = []
        #        grad = Gradient(type=LINEAR, name="1", x=0, y=0, x1=0, y1=0)
        #        grad.clear()
        #        grad.add_new_color(GradientColor(1.0, 0.0, 0.0, 1.0, 0.142))
        #        grad.add_new_color(GradientColor(1.0, 1.0, 0.0, 1.0, 0.285))
        #        grad.add_new_color(GradientColor(0.0, 1.0, 0.0, 1.0, 0.428))
        #        grad.add_new_color(GradientColor(0.0, 1.0, 1.0, 1.0, 0.571))
        #        grad.add_new_color(GradientColor(0.0, 0.0, 1.0, 1.0, 0.714))
        #        grad.add_new_color(GradientColor(1.0, 0.0, 1.0, 1.0, 0.857))
        #        grad.update()
        #        self.gradients.append(grad)

        self.document = Document()
        #self.document.pages[0].children = list()
        #self.document.pages = list()

        page = Page()
        self.total = Size()

        self.document.pages.append(page)
        self.zoom = 1.0
        self.origin.x = 0 # XXX
        self.origin.y = 0 # XXX
        self.border = 25

        self.pick = False
        self.updated = False
        self.child = None
        self.stop_cursor_change = False

        self.horizontal_ruler = None
        self.vertical_ruler = None
        self.clipboard = None

        self.hints = False

        self.motions = 0
예제 #3
0
class CanvasImplementation(BaseCanvas):
    """This class represents a high level canvas implementation"""

    #    def initialize(self):
    #        BaseCanvas.initialize(self)
    def __init__(self, application):
        BaseCanvas.__init__(self, application)
        self.origin = Origin()
        self.grid = Grid()
        self.guides = Guides()
        self.selection = Selection()

        #        self.gradients = []
        #        grad = Gradient(type=LINEAR, name="1", x=0, y=0, x1=0, y1=0)
        #        grad.clear()
        #        grad.add_new_color(GradientColor(1.0, 0.0, 0.0, 1.0, 0.142))
        #        grad.add_new_color(GradientColor(1.0, 1.0, 0.0, 1.0, 0.285))
        #        grad.add_new_color(GradientColor(0.0, 1.0, 0.0, 1.0, 0.428))
        #        grad.add_new_color(GradientColor(0.0, 1.0, 1.0, 1.0, 0.571))
        #        grad.add_new_color(GradientColor(0.0, 0.0, 1.0, 1.0, 0.714))
        #        grad.add_new_color(GradientColor(1.0, 0.0, 1.0, 1.0, 0.857))
        #        grad.update()
        #        self.gradients.append(grad)

        self.document = Document()
        #self.document.pages[0].children = list()
        #self.document.pages = list()

        page = Page()
        self.total = Size()

        self.document.pages.append(page)
        self.zoom = 1.0
        self.origin.x = 0 # XXX
        self.origin.y = 0 # XXX
        self.border = 25

        self.pick = False
        self.updated = False
        #self.get_child() = None
        self.child = None
        self.stop_cursor_change = False

        self.horizontal_ruler = None
        self.vertical_ruler = None
        self.clipboard = None

        self.hints = False

        self.motions = 0

    def release(self, widget, event):
        """
        This code is executed when you release the mouse button
        """
        if self.selection.active:
            self.unselect_all()
            for child in self.document.pages[0].children:
                if child.in_selection(self.selection):
                    child.selected = True
                    self.emit("select", child)
                    #elif child.resizing:
                    #    child.resizing ^= 1
            self.selection.active = False
        else:
            for child in self.document.pages[0].children:
                if child.selected:
                    self.emit("finalize", child)
                if child.resizing:
                    child.resizing ^= 1
                    child.direction = NONE
                    child.handler.pivot.active = False
                    self.emit("finalize", child)

        self.pick = False
        #widget.bin_window.set_cursor(Gdk.Cursor.new(Gdk.CursorType.ARROW))
        widget.get_window().set_cursor(Gdk.Cursor.new(Gdk.CursorType.ARROW))

        self.updated = False
        self.update()
        self.stop_cursor_change = False
        return True

    def motion(self, widget, event):
        """
        This code is executed when move the mouse pointer
        """
        self.statics.motion += 1
        #self.consume(Gdk.MOTION_NOTIFY, event.get_state())
        self.disconnect(self.motion_id)
        if self.horizontal_ruler:
            self.horizontal_ruler.motion(self.horizontal_ruler, event, True)
        if self.vertical_ruler:
            self.vertical_ruler.motion(self.vertical_ruler, event, True)
        x = event.x / self.zoom
        y = event.y / self.zoom

        if not self.stop_cursor_change:
            def get_direction_for_child_at_position(x, y, children):
                for child in children:
                    if child.selected and child.handler.at_position(x, y):
                        direction = child.handler.get_direction(x, y)
                        #widget.bin_window.set_cursor(child.get_cursor(direction))
                        widget.get_window().set_cursor(child.get_cursor(direction))
                        return direction
                return NONE

            direction = get_direction_for_child_at_position(x, y, self.document.pages[0].children)

            if direction == NONE:
                if self.pick:
                    #widget.bin_window.set_cursor(Gdk.Cursor.new(Gdk.PENCIL))
                    widget.get_window().set_cursor(Gdk.Cursor.new(Gdk.CursorType.PENCIL))
                else:
                    #widget.bin_window.set_cursor(Gdk.Cursor.new(Gdk.CursorType.ARROW))
                    widget.get_window().set_cursor(Gdk.Cursor.new(Gdk.CursorType.ARROW))

        if self.selection.active:
            self.selection.width = x - self.selection.x
            self.selection.height = y - self.selection.y
            self.updated = False
            self.update() # XXX
        elif event.get_state() & Gdk.ModifierType.BUTTON1_MASK:
            for child in self.document.pages[0].children: # TODO
                if child.selected:
                    target = Point()
                    if child.resizing:
                        target.x = self.grid.nearest(x)
                        target.y = self.grid.nearest(y)
                        target = self.guides.nearest(target)
                        if child.direction < ANONIMOUS:
                            child.resize(target.x, target.y)
                        else:
                            child.transform(target.x, target.y)
                    else:
                        #widget.bin_window.set_cursor(Gdk.Cursor.new(Gdk.FLEUR))
                        widget.get_window().set_cursor(Gdk.Cursor.new(Gdk.CursorType.FLEUR))
                        target.x = self.grid.nearest(x - child.offset.x)
                        target.y = self.grid.nearest(y - child.offset.y)
                        target = self.guides.nearest(target)
                        # XXX-TEST
                        if self.is_testing:
                            def magnetize(target, children):
                                for child in children:
                                    if not child.selected and child.magnetos.is_magnetized(target):
                                        return child.magnetos.get_magnetized(target)
                                return target

                            target = magnetize(target, self.document.pages[0].children)
                        # XXX-TEST
                        child.move(target.x, target.y)
                    self.emit("edit-child", child)
                    self.update()
        self.motion_id = self.connect("motion-notify-event", self.motion)
        return True

    def press(self, widget, event):
        """
        This code is executed when you press the mouse button
        """
        self.emit("focus", Gtk.DirectionType.TAB_FORWARD)

        x = event.x / self.zoom
        y = event.y / self.zoom

        def start_resize(child):
            self.unselect_all()
            child.selected = True
            child.resizing = True
            if child.direction < ANONIMOUS:
                control = child.handler.control[opposite(child.direction)]
                child.pivot.x = self.grid.nearest(control.x)
                child.pivot.y = self.grid.nearest(control.y)
                child.pivot = self.guides.nearest(child.pivot)
                child.handler.pivot.x = control.x
                child.handler.pivot.y = control.y
                child.handler.pivot.active = True

        if self.pick:
            self.unselect_all()
            #x, y = self.get_pointer()
            target = Point()
            #child = self.get_child()
            child = self.child
            self.add(child)
            child.selected = True
            target.x = self.grid.nearest(x)
            target.y = self.grid.nearest(y)
            target = self.guides.nearest(target)
            child.x = target.x
            child.y = target.y
            child.width = 0
            child.height = 0
            child.direction = SOUTHEAST
            child.handler.control[opposite(child.direction)].y = child.y
            child.handler.control[opposite(child.direction)].x = child.x
            start_resize(child)
            #widget.bin_window.set_cursor(Gdk.Cursor.new(Gdk.BOTTOM_RIGHT_CORNER))
            widget.get_window().set_cursor(Gdk.Cursor.new(Gdk.CursorType.BOTTOM_RIGHT_CORNER))
            self.emit("select", child)
            return True

        selection = True

        def start_move(child, x, y):
            child.offset.x = x - child.x
            child.offset.y = y - child.y
            child.press(x, y)

        def select(child):
            if not event.get_state() & Gdk.ModifierType.CONTROL_MASK:
                self.unselect_all()
            child.selected = True

        for child in sorted(self.document.pages[0].children, key=lambda child: child.z):
            if child.selected:
                if child.handler.at_position(x, y):
                    child.direction = child.handler.get_direction(x, y)
                    selection = False
                    start_resize(child)
                elif child.at_position(x, y):
                    #start_move(child, x, y)
                    start_move(child, x, y)
                    selection = False
                else:
                    continue
            elif child.at_position(x, y):
                selection = False
                select(child)
                start_move(child, x, y)
            else:
                continue

        if selection:
            self.selection.x = x
            self.selection.y = y
            self.selection.width = 0
            self.selection.height = 0
            self.selection.active = True
            #widget.bin_window.set_cursor(Gdk.Cursor.new(Gdk.CursorType.CROSSHAIR))
            widget.get_window().set_cursor(Gdk.Cursor.new(Gdk.CursorType.CROSSHAIR))
        else:
            self.stop_cursor_change = True
            #self.updated = False
        self.update() # XXX

        return True

    def expose(self, widget, context):
        self.statics.expose += 1
        self.disconnect(self.expose_id)
        #context = widget.bin_window.cairo_create()
        #context.rectangle(event.area.x, event.area.y, event.area.width, event.area.height)
        #context.clip()
        context.scale(self.zoom, self.zoom)
        self.total.width = int(self.document.pages[0].width * self.zoom + 2 * self.border)
        self.total.height = int(
            len(self.document.pages) * self.document.pages[0].height * self.zoom +
            (len(self.document.pages) + 1) * self.border)
        self.set_size_request(self.total.width, self.total.height)
        context.set_source_rgb(0.55, 0.55, 0.55) #background
        context.paint()

        page = self.document.pages[0]

        self.document.draw(context, self.border, self.zoom, self.hints)

        self.origin.x = page.x + page.left # + self.border
        self.origin.y = page.y + page.top # + self.border

        if self.grid.active:
            self.grid.x = self.origin.x
            self.grid.y = self.origin.y
            self.grid.width = page.width - page.left - page.right
            self.grid.height = page.height - page.top - page.bottom
            self.grid.draw(context)

        if self.guides.active:
            self.guides.x = self.origin.x
            self.guides.y = self.origin.y
            self.guides.width = page.width - page.left - page.right
            self.guides.height = page.height - page.top - page.bottom
            self.guides.draw(context)

        if self.selection.active:
            self.selection.draw(context)
        self.updated = True
        self.expose_id = self.connect("draw", self.expose)
        return True

    add = lambda self, child: self.document.pages[0].children.append(child)

    #update = lambda self: self.queue_draw()
    def update(self):
        if not self.updated:
            pass
        self.queue_draw()
예제 #4
0
class CanvasImplementation(BaseCanvas):
    """This class represents a high level canvas implementation"""

    #    def initialize(self):
    #        BaseCanvas.initialize(self)
    def __init__(self, application):
        BaseCanvas.__init__(self, application)
        self.origin = Origin()
        self.grid = Grid()
        self.guides = Guides()
        self.selection = Selection()

        #        self.gradients = []
        #        grad = Gradient(type=LINEAR, name="1", x=0, y=0, x1=0, y1=0)
        #        grad.clear()
        #        grad.add_new_color(GradientColor(1.0, 0.0, 0.0, 1.0, 0.142))
        #        grad.add_new_color(GradientColor(1.0, 1.0, 0.0, 1.0, 0.285))
        #        grad.add_new_color(GradientColor(0.0, 1.0, 0.0, 1.0, 0.428))
        #        grad.add_new_color(GradientColor(0.0, 1.0, 1.0, 1.0, 0.571))
        #        grad.add_new_color(GradientColor(0.0, 0.0, 1.0, 1.0, 0.714))
        #        grad.add_new_color(GradientColor(1.0, 0.0, 1.0, 1.0, 0.857))
        #        grad.update()
        #        self.gradients.append(grad)

        self.document = Document()
        #self.document.pages[0].children = list()
        #self.document.pages = list()

        page = Page()
        self.total = Size()

        self.document.pages.append(page)
        self.zoom = 1.0
        self.origin.x = 0 # XXX
        self.origin.y = 0 # XXX
        self.border = 25

        self.pick = False
        self.updated = False
        self.child = None
        self.stop_cursor_change = False

        self.horizontal_ruler = None
        self.vertical_ruler = None
        self.clipboard = None

        self.hints = False

        self.motions = 0

    def release(self, widget, event):
        """
        This code is executed when you release the mouse button
        """
        if self.selection.active:
            self.unselect_all()
            for child in self.document.pages[0].children:
                if child.in_selection(self.selection):
                    child.selected = True
                    self.emit("select", child)
                    #elif child.resizing:
                    #    child.resizing ^= 1
            self.selection.active = False
        else:
            for child in self.document.pages[0].children:
                if child.selected:
                    self.emit("finalize", child)
                if child.resizing:
                    child.resizing ^= 1
                    child.direction = NONE
                    child.handler.pivot.active = False
                    self.emit("finalize", child)

        self.pick = False
        widget.bin_window.set_cursor(gtk.gdk.Cursor(gtk.gdk.ARROW))

        self.updated = False
        self.update()
        self.stop_cursor_change = False
        return True

    def motion(self, widget, event):
        """
        This code is executed when move the mouse pointer
        """
        self.statics.motion += 1
        #self.consume(gtk.gdk.MOTION_NOTIFY, event.state)
        self.disconnect(self.motion_id)
        if self.horizontal_ruler:
            self.horizontal_ruler.motion(self.horizontal_ruler, event, True)
        if self.vertical_ruler:
            self.vertical_ruler.motion(self.vertical_ruler, event, True)
        x = event.x / self.zoom
        y = event.y / self.zoom

        if not self.stop_cursor_change:
            def get_direction_for_child_at_position(x, y, children):
                for child in children:
                    if child.selected and child.handler.at_position(x, y):
                        direction = child.handler.get_direction(x, y)
                        widget.bin_window.set_cursor(child.get_cursor(direction))
                        return direction
                return NONE

            direction = get_direction_for_child_at_position(x, y, self.document.pages[0].children)

            if direction == NONE:
                if self.pick:
                    widget.bin_window.set_cursor(gtk.gdk.Cursor(gtk.gdk.PENCIL))
                else:
                    widget.bin_window.set_cursor(gtk.gdk.Cursor(gtk.gdk.ARROW))

        if self.selection.active:
            self.selection.width = x - self.selection.x
            self.selection.height = y - self.selection.y
            self.updated = False
            self.update() # XXX
        elif event.state & gtk.gdk.BUTTON1_MASK:
            for child in self.document.pages[0].children: # TODO
                if child.selected:
                    target = Point()
                    if child.resizing:
                        target.x = self.grid.nearest(x)
                        target.y = self.grid.nearest(y)
                        target = self.guides.nearest(target)
                        if child.direction < ANONIMOUS:
                            child.resize(target.x, target.y)
                        else:
                            child.transform(target.x, target.y)
                    else:
                        widget.bin_window.set_cursor(gtk.gdk.Cursor(gtk.gdk.FLEUR))
                        target.x = self.grid.nearest(x - child.offset.x)
                        target.y = self.grid.nearest(y - child.offset.y)
                        target = self.guides.nearest(target)
                        # XXX-TEST
                        if self.is_testing:
                            def magnetize(target, children):
                                for child in children:
                                    if not child.selected and child.magnetos.is_magnetized(target):
                                        return child.magnetos.get_magnetized(target)
                                return target

                            target = magnetize(target, self.document.pages[0].children)
                        # XXX-TEST
                        child.move(target.x, target.y)
                    self.emit("edit-child", child)
                    self.update()
        self.motion_id = self.connect("motion-notify-event", self.motion)
        return True

    def press(self, widget, event):
        """
        This code is executed when you press the mouse button
        """
        self.emit("focus", gtk.DIR_TAB_FORWARD)

        x = event.x / self.zoom
        y = event.y / self.zoom

        def start_resize(child):
            self.unselect_all()
            child.selected = True
            child.resizing = True
            if child.direction < ANONIMOUS:
                control = child.handler.control[opposite(child.direction)]
                child.pivot.x = self.grid.nearest(control.x)
                child.pivot.y = self.grid.nearest(control.y)
                child.pivot = self.guides.nearest(child.pivot)
                child.handler.pivot.x = control.x
                child.handler.pivot.y = control.y
                child.handler.pivot.active = True

        if self.pick:
            self.unselect_all()
            #x, y = self.get_pointer()
            target = Point()
            child = self.child
            self.add(child)
            child.selected = True
            target.x = self.grid.nearest(x)
            target.y = self.grid.nearest(y)
            target = self.guides.nearest(target)
            child.x = target.x
            child.y = target.y
            child.width = 0
            child.height = 0
            child.direction = SOUTHEAST
            child.handler.control[opposite(child.direction)].y = child.y
            child.handler.control[opposite(child.direction)].x = child.x
            start_resize(child)
            widget.bin_window.set_cursor(gtk.gdk.Cursor(gtk.gdk.BOTTOM_RIGHT_CORNER))
            self.emit("select", child)
            return True

        selection = True

        def start_move(child, x, y):
            child.offset.x = x - child.x
            child.offset.y = y - child.y
            child.press(x, y)

        def select(child):
            if not event.state & gtk.gdk.CONTROL_MASK:
                self.unselect_all()
            child.selected = True

        for child in sorted(self.document.pages[0].children, key=lambda child: child.z):
            if child.selected:
                if child.handler.at_position(x, y):
                    child.direction = child.handler.get_direction(x, y)
                    selection = False
                    start_resize(child)
                elif child.at_position(x, y):
                    #start_move(child, x, y)
                    start_move(child, x, y)
                    selection = False
                else:
                    continue
            elif child.at_position(x, y):
                selection = False
                select(child)
                start_move(child, x, y)
            else:
                continue

        if selection:
            self.selection.x = x
            self.selection.y = y
            self.selection.width = 0
            self.selection.height = 0
            self.selection.active = True
            widget.bin_window.set_cursor(gtk.gdk.Cursor(gtk.gdk.CROSSHAIR))
        else:
            self.stop_cursor_change = True
            #self.updated = False
        self.update() # XXX

        return True

    def expose(self, widget, event):
        self.statics.expose += 1
        self.disconnect(self.expose_id)
        context = widget.bin_window.cairo_create()
        context.rectangle(event.area.x, event.area.y, event.area.width, event.area.height)
        context.clip()
        context.scale(self.zoom, self.zoom)
        self.total.width = int(self.document.pages[0].width * self.zoom + 2 * self.border)
        self.total.height = int(
            len(self.document.pages) * self.document.pages[0].height * self.zoom +
            (len(self.document.pages) + 1) * self.border)
        self.set_size_request(self.total.width, self.total.height)
        context.set_source_rgb(0.55, 0.55, 0.55) #background
        context.paint()

        page = self.document.pages[0]

        self.document.draw(context, self.border, self.zoom, self.hints)

        self.origin.x = page.x + page.left # + self.border
        self.origin.y = page.y + page.top # + self.border

        if self.grid.active:
            self.grid.x = self.origin.x
            self.grid.y = self.origin.y
            self.grid.width = page.width - page.left - page.right
            self.grid.height = page.height - page.top - page.bottom
            self.grid.draw(context)

        if self.guides.active:
            self.guides.x = self.origin.x
            self.guides.y = self.origin.y
            self.guides.width = page.width - page.left - page.right
            self.guides.height = page.height - page.top - page.bottom
            self.guides.draw(context)

        if self.selection.active:
            self.selection.draw(context)
        self.updated = True
        self.expose_id = self.connect("expose-event", self.expose)
        return True

    add = lambda self, child: self.document.pages[0].children.append(child)

    #update = lambda self: self.queue_draw()
    def update(self):
        if not self.updated:
            pass
        self.queue_draw()