You are here: start » diasetpos
#   Set Position
#   Set Position
#   Copyright (c) 2014, Amos Brocco <amos.brocco@gmail.com>
#
#   Set X,Y position and size (W,H) of selected objects
#   In the dialog insert X or Y position / W or H size, press enter in the corresponding
#   textbox to update.
#
#    This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
import dia
import string
 
class RepositionDialog :
    def __init__(self, d, data) :
        import pygtk
        pygtk.require("2.0")
        import gtk
        self.data = data
        win = gtk.Window()
        win.connect("delete_event", self.on_delete)
        win.set_title("Set position and size")
        self.diagram = d
        self.data = data
        self.win = win
        box1 = gtk.VBox()
        win.add(box1)
        box1.show()
        box2 = gtk.VBox(spacing=10)
        box2.set_border_width(10)
        box1.pack_start(box2)
        self.hlabel = gtk.Label("Enter coordinates/size, then press Enter")
        box2.pack_start(self.hlabel)
        self.hlabel.show()
        box2.show()            
        self.entries = {}
        for e in ["X", "Y", "W", "H"]:
            box = gtk.HBox(spacing=5)
            label = gtk.Label(e+":")
            label.show()
            box.pack_start(label)
            entry = gtk.Entry()
            entry.connect("activate", self.on_entry_activate, e)
            box.pack_start(entry)
            entry.show()
            box2.pack_start(box)
            box.show()
            self.entries[e] = entry
        separator = gtk.HSeparator()
        box1.pack_start(separator, expand=0)
        separator.show()
        box2 = gtk.VBox(spacing=10)
        box2.set_border_width(10)
        box1.pack_start(box2, expand=0)
        box2.show()
        button = gtk.Button("Close")
        button.connect("clicked", self.on_delete)
        box2.pack_start(button)
        button.set_flags(gtk.CAN_DEFAULT)
        button.grab_default()
        button.show()
 
        objs = data.get_sorted_selected()
        if len(objs) == 1:
            pos = objs[0].properties["obj_pos"].value
            width = 0
            height = 0
            if objs[0].properties.has_key("elem_width") :
                width = objs[0].properties["elem_width"].value
            if objs[0].properties.has_key("elem_height") :                
                height  = objs[0].properties["elem_height"].value
            self.entries["X"].set_text(str(pos.x))
            self.entries["Y"].set_text(str(pos.y))
            self.entries["W"].set_text(str(width))
            self.entries["H"].set_text(str(height))
 
        win.show()
 
    def on_entry_activate(self, entry, *args):
        objs = self.data.get_sorted_selected()
        if len(objs) <= 0:
            return
        field = args[0]
        if field == 'X':
            self.update_position(None, 1)
        elif field == 'Y':
            self.update_position(1, None)
        elif field == 'W':
            self.update_size(None, 1)
        elif field == 'H':
            self.update_size(1, None)                            
 
    def update_position(self, x, y):
        nx = 0
        ny = 0
        sx = self.entries["X"].get_text()
        try:
            nx = float(sx)
        except:
            pass
        sy = self.entries["Y"].get_text()
        try:
            ny = float(sy)
        except:
            pass
        objs = self.data.get_sorted_selected()
        if len(objs) == 0 :
            objs = data.active_layer.objects
        for o in objs :
            pos = o.properties["obj_pos"].value
            if (x is None):
                o.move(nx,pos.y)
            elif (y is None):
                o.move(pos.x,ny)
            self.data.update_extents ()
        dia.active_display().diagram.add_update_all()
        dia.active_display().flush()
 
    def resize_handle(self, o, w, h):
        if o.properties.has_key("elem_width") :
            lrh = o.handles[7]
            o.move_handle(lrh, (w, h), 0, 0)
 
    def update_size(self, w, h):
        nh = 0
        nw = 0
        sw = self.entries["W"].get_text()
        try:
            nw = float(sw)
        except:
            pass
        sh = self.entries["H"].get_text()
        try:
            nh = float(sh)        
        except:
            pass    
        objs = self.data.get_sorted_selected()
        if len(objs) == 0 :
            objs = data.active_layer.objects
        for o in objs :
            if o.properties.has_key("elem_width") :
                width = o.properties["elem_width"].value
                height  = o.properties["elem_height"].value
                pos = o.properties["obj_pos"].value
                if (w is None):
                    self.resize_handle(o, pos.x + nw, pos.y + height)
                elif (h is None):
                    self.resize_handle(o, pos.x + width, pos.y + nh)
                self.data.update_extents ()
        dia.active_display().diagram.add_update_all()
        dia.active_display().flush()        
 
    def on_delete (self, *args) :
        self.win.destroy ()
 
def reposition_cb(data, flags) :
    dialog = RepositionDialog(dia.active_display().diagram, data)
 
dia.register_action ("ObjectsSetPositionAndSize", "Set position and size",
             "/DisplayMenu/Objects/ObjectsExtensionStart", 
             reposition_cb)
diasetpos/start.txt · Last modified: 2014/12/01 17:47 by attila
Kleine Websites, die ein Wiki als CMS verwenden.de