Beispiel #1
0
class LayerPanel(SketchPanel):

    title = _("Layers")
    receivers = SketchPanel.receivers[:]

    def __init__(self, master, main_window, doc):
	self.info_list = []
	SketchPanel.__init__(self, master, main_window, doc, name = 'layerdlg')

    def build_dlg(self):
	top = self.top

	frame = Frame(top)
	frame.pack(side = BOTTOM, fill = X)
	button = UpdatedButton(frame, bitmap = pixmaps.LayerNew, name = 'new',
			       command = self.new_layer)
	button.pack(side = LEFT, fill = BOTH, expand = 1)
	button = UpdatedButton(frame, bitmap = pixmaps.LayerUp, name = 'up',
			       command = self.layer_up)
	button.pack(side = LEFT, fill = BOTH, expand = 1)
	button = UpdatedButton(frame, bitmap = pixmaps.LayerDown,
			       name = 'down', command = self.layer_down)
	button.pack(side = LEFT, fill = BOTH, expand = 1)
	button = UpdatedButton(frame, text = _("Close"), name = 'close',
			       command = self.close_dlg)
	button.pack(side = LEFT, fill = BOTH, expand = 1)

	list_frame = Frame(top)
	list_frame.pack(side = LEFT, expand = 1, fill = BOTH)

	sb_vert = Scrollbar(list_frame, takefocus = 0)
	sb_vert.pack(side = RIGHT, fill = Y)

	self.canvas = canvas = Canvas(list_frame)
	canvas.pack(expand = 1, fill = BOTH)

	self.frame = frame = Frame(canvas, name = 'list')
	canvas.create_window(0, 0, window = frame, anchor = NW)
	sb_vert['command'] = (canvas, 'yview')
	canvas['yscrollcommand'] = (sb_vert, 'set')

	self.active_var = IntVar(top)

    def init_from_doc(self):
	self.Update(LAYER_ORDER)

    receivers.append((LAYER, 'Update'))
    def Update(self, detail = '', *args):
	if detail != LAYER_ACTIVE:
	    if detail == LAYER_ORDER:
		self.create_widget_list()
	    else:
		self.update_widgets()
	self.update_active()

    def create_widget_list(self):
	frame = self.frame
	set_active_layer = self.set_active_layer
	set_layer_state = self.set_layer_state
	set_color = self.set_color
	context_menu = self.popup_context_menu
	rows = self.info_list
	layers = self.document.NumLayers()
	if layers > len(rows):
	    for idx in range(len(rows), layers):
		row = LayerInfo(frame, idx, (), self.active_var,
				self.rename_layer, set_active_layer,
				set_layer_state, set_color, context_menu)
		row.PlaceWidgets(idx)
		rows.append(row)
	elif layers < len(rows):
	    for row in rows[layers:]:
		row.Destroy()
	    del rows[layers:]
	self.update_widgets()
	frame.update()
	self.canvas['scrollregion'] = (0, 0, frame.winfo_reqwidth(),
				       frame.winfo_reqheight())
	self.canvas['width'] = frame.winfo_reqwidth()

    def update_widgets(self):
        layers = self.document.layers[:]
        layers.reverse()
	for idx in range(len(layers)):
	    self.info_list[idx].SetInfo(idx, layers[idx])

    def update_active(self):
	idx = self.document.ActiveLayerIdx()
        if idx is None:
            idx = -1
	self.active_var.set(self.document.NumLayers() - 1 - idx)

    def set_active_layer(self):
        idx = self.document.NumLayers() - self.active_var.get() - 1
	self.document.SetActiveLayer(idx)

    def set_layer_state(self, idx):
	idx = int(idx)
	row = self.info_list[idx]
	idx = self.document.NumLayers() - idx - 1
	state = row.State()
	apply(self.document.SetLayerState, (idx,) + state)

    def set_color(self, idx):
	idx = int(idx)
	row = self.info_list[idx]
	idx = self.document.NumLayers() - idx - 1
	self.document.SetLayerColor(idx, row.Color())

    def set_event_context(self, event):
	for idx in range(len(self.info_list)):
	    if self.info_list[idx].button == event.widget:
		break
	else:
	    return
	self.context_idx = self.document.NumLayers() - 1 - idx
	self.context_layer = self.document.Layers()[self.context_idx]

    context_menu = None
    def popup_context_menu(self, event):
	self.set_event_context(event)
	if self.context_menu is None:
            self.context_menu = UpdatedMenu(self.frame, [], tearoff = 0,
                                      auto_rebuild = self.build_context_menu)
        self.context_menu.Popup(event.x_root, event.y_root)

    def build_context_menu(self):
        entries = [
            (_("Rename"), self.rename_layer),
            None,
            (_("Lower Layer"), self.layer_down, 0),
            (_("Raise Layer"), self.layer_up, 0),
            (_("Delete"), self.delete_layer, (), self.can_delete_layer),
            None,
            (_("Move Selection Here"), self.move_selection_here, (),
             self.can_move_selection),
            (_("Select All Children"), self.select_layer, SelectAdd,
             self.is_not_locked),
            (_("Deselect All Children"), self.select_layer, SelectSubtract,
             self.is_not_locked)
            ]
        if self.context_layer.is_GuideLayer:
            entries.append(None)
            entries.append(self.main_window.commands.CreateGuideDialog)
        if self.context_layer.is_GridLayer:
            entries.append(None)
            entries.append(self.main_window.commands.CreateGridDialog)
        return map(MakeCommand, entries)

    def close_dlg(self):
	SketchPanel.close_dlg(self)
	if self.context_menu is not None:
	    self.context_menu.destroy()

    def rename_layer(self, event = None):
	if event is not None:
	    self.set_event_context(event)
	name = GetName(self.top, self.context_layer.Name())
	if name != None:
	    self.document.SetLayerName(self.context_idx, name)

    def delete_layer(self):
	self.document.DeleteLayer(self.context_idx)

    def can_delete_layer(self):
	return self.document.CanDeleteLayer(self.context_idx)

    def new_layer(self):
	self.document.NewLayer()

    def layer_up(self, active_layer = 1):
	if active_layer:
            active_idx = self.active_var.get()
            if active_idx < 0:
                return
	    idx = self.document.NumLayers() - 1 - active_idx
	else:
	    idx = self.context_idx
	self.document.MoveLayerUp(idx)

    def layer_down(self, active_layer = 1):
	if active_layer:
            active_idx = self.active_var.get()
            if active_idx < 0:
                return
	    idx = self.document.NumLayers() - 1 - active_idx
	else:
	    idx = self.context_idx
	self.document.MoveLayerDown(idx)

    def move_selection_here(self):
	self.document.MoveSelectionToLayer(self.context_idx)

    def can_move_selection(self):
	return not self.context_layer.Locked() and self.document.HasSelection()

    def is_not_locked(self):
	return not self.context_layer.Locked()

    def select_layer(self, mode):
	self.document.SelectLayer(self.context_idx, mode)
Beispiel #2
0
class LayerPanel(SketchPanel):

    title = _("Layers")
    receivers = SketchPanel.receivers[:]

    def __init__(self, master, main_window, doc):
        self.info_list = []
        SketchPanel.__init__(self, master, main_window, doc, name='layerdlg')

    def build_dlg(self):
        top = self.top

        frame = Frame(top)
        frame.pack(side=BOTTOM, fill=X)
        button = UpdatedButton(frame,
                               bitmap=pixmaps.LayerNew,
                               name='new',
                               command=self.new_layer)
        button.pack(side=LEFT, fill=BOTH, expand=1)
        button = UpdatedButton(frame,
                               bitmap=pixmaps.LayerUp,
                               name='up',
                               command=self.layer_up)
        button.pack(side=LEFT, fill=BOTH, expand=1)
        button = UpdatedButton(frame,
                               bitmap=pixmaps.LayerDown,
                               name='down',
                               command=self.layer_down)
        button.pack(side=LEFT, fill=BOTH, expand=1)
        button = UpdatedButton(frame,
                               text=_("Close"),
                               name='close',
                               command=self.close_dlg)
        button.pack(side=LEFT, fill=BOTH, expand=1)

        list_frame = Frame(top)
        list_frame.pack(side=LEFT, expand=1, fill=BOTH)

        sb_vert = Scrollbar(list_frame, takefocus=0)
        sb_vert.pack(side=RIGHT, fill=Y)

        self.canvas = canvas = Canvas(list_frame)
        canvas.pack(expand=1, fill=BOTH)

        self.frame = frame = Frame(canvas, name='list')
        canvas.create_window(0, 0, window=frame, anchor=NW)
        sb_vert['command'] = (canvas, 'yview')
        canvas['yscrollcommand'] = (sb_vert, 'set')

        self.active_var = IntVar(top)

    def init_from_doc(self):
        self.Update(LAYER_ORDER)

    receivers.append((LAYER, 'Update'))

    def Update(self, detail='', *args):
        if detail != LAYER_ACTIVE:
            if detail == LAYER_ORDER:
                self.create_widget_list()
            else:
                self.update_widgets()
        self.update_active()

    def create_widget_list(self):
        frame = self.frame
        set_active_layer = self.set_active_layer
        set_layer_state = self.set_layer_state
        set_color = self.set_color
        context_menu = self.popup_context_menu
        rows = self.info_list
        layers = self.document.NumLayers()
        if layers > len(rows):
            for idx in range(len(rows), layers):
                row = LayerInfo(frame, idx, (), self.active_var,
                                self.rename_layer, set_active_layer,
                                set_layer_state, set_color, context_menu)
                row.PlaceWidgets(idx)
                rows.append(row)
        elif layers < len(rows):
            for row in rows[layers:]:
                row.Destroy()
            del rows[layers:]
        self.update_widgets()
        frame.update()
        self.canvas['scrollregion'] = (0, 0, frame.winfo_reqwidth(),
                                       frame.winfo_reqheight())
        self.canvas['width'] = frame.winfo_reqwidth()

    def update_widgets(self):
        layers = self.document.layers[:]
        layers.reverse()
        for idx in range(len(layers)):
            self.info_list[idx].SetInfo(idx, layers[idx])

    def update_active(self):
        idx = self.document.ActiveLayerIdx()
        if idx is None:
            idx = -1
        self.active_var.set(self.document.NumLayers() - 1 - idx)

    def set_active_layer(self):
        idx = self.document.NumLayers() - self.active_var.get() - 1
        self.document.SetActiveLayer(idx)

    def set_layer_state(self, idx):
        idx = int(idx)
        row = self.info_list[idx]
        idx = self.document.NumLayers() - idx - 1
        state = row.State()
        apply(self.document.SetLayerState, (idx, ) + state)

    def set_color(self, idx):
        idx = int(idx)
        row = self.info_list[idx]
        idx = self.document.NumLayers() - idx - 1
        self.document.SetLayerColor(idx, row.Color())

    def set_event_context(self, event):
        for idx in range(len(self.info_list)):
            if self.info_list[idx].button == event.widget:
                break
        else:
            return
        self.context_idx = self.document.NumLayers() - 1 - idx
        self.context_layer = self.document.Layers()[self.context_idx]

    context_menu = None

    def popup_context_menu(self, event):
        self.set_event_context(event)
        if self.context_menu is None:
            self.context_menu = UpdatedMenu(
                self.frame, [],
                tearoff=0,
                auto_rebuild=self.build_context_menu)
        self.context_menu.Popup(event.x_root, event.y_root)

    def build_context_menu(self):
        entries = [(_("Rename"), self.rename_layer), None,
                   (_("Lower Layer"), self.layer_down, 0),
                   (_("Raise Layer"), self.layer_up, 0),
                   (_("Delete"), self.delete_layer, (), self.can_delete_layer),
                   None,
                   (_("Move Selection Here"), self.move_selection_here, (),
                    self.can_move_selection),
                   (_("Select All Children"), self.select_layer, SelectAdd,
                    self.is_not_locked),
                   (_("Deselect All Children"), self.select_layer,
                    SelectSubtract, self.is_not_locked)]
        if self.context_layer.is_GuideLayer:
            entries.append(None)
            entries.append(self.main_window.commands.CreateGuideDialog)
        if self.context_layer.is_GridLayer:
            entries.append(None)
            entries.append(self.main_window.commands.CreateGridDialog)
        return map(MakeCommand, entries)

    def close_dlg(self):
        SketchPanel.close_dlg(self)
        if self.context_menu is not None:
            self.context_menu.destroy()

    def rename_layer(self, event=None):
        if event is not None:
            self.set_event_context(event)
        name = GetName(self.top, self.context_layer.Name())
        if name != None:
            self.document.SetLayerName(self.context_idx, name)

    def delete_layer(self):
        self.document.DeleteLayer(self.context_idx)

    def can_delete_layer(self):
        return self.document.CanDeleteLayer(self.context_idx)

    def new_layer(self):
        self.document.NewLayer()

    def layer_up(self, active_layer=1):
        if active_layer:
            active_idx = self.active_var.get()
            if active_idx < 0:
                return
            idx = self.document.NumLayers() - 1 - active_idx
        else:
            idx = self.context_idx
        self.document.MoveLayerUp(idx)

    def layer_down(self, active_layer=1):
        if active_layer:
            active_idx = self.active_var.get()
            if active_idx < 0:
                return
            idx = self.document.NumLayers() - 1 - active_idx
        else:
            idx = self.context_idx
        self.document.MoveLayerDown(idx)

    def move_selection_here(self):
        self.document.MoveSelectionToLayer(self.context_idx)

    def can_move_selection(self):
        return not self.context_layer.Locked() and self.document.HasSelection()

    def is_not_locked(self):
        return not self.context_layer.Locked()

    def select_layer(self, mode):
        self.document.SelectLayer(self.context_idx, mode)