File: select_by.py

package info (click to toggle)
dia 0.97.3%2Bgit20160930-9
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 54,372 kB
  • sloc: ansic: 155,065; xml: 16,326; python: 6,641; cpp: 4,935; makefile: 3,833; sh: 540; perl: 137; sed: 19
file content (157 lines) | stat: -rw-r--r-- 4,851 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#    Select Objects By Common Properties
#    Copyright (c) 2003, 2013 Hans Breuer <hans@breuer.org>
#
#        different methods (menu entries) to select all objects with given
#    property, that is :
#	- a find dialog to select by name
#	- menu entries for different color types. The common color is read
#	  from already selected objects
#	- a size range given by the current selection

#    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 sys, dia

class CFindDialog :
	def __init__(self, d, data) :
		import pygtk
		pygtk.require("2.0")
		import gtk

		win = gtk.Window()
		win.connect("delete_event", self.on_delete)
		win.set_title("Select by name")

		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)
		box2.show()

		self.entry = gtk.Entry()
		self.entry.set_text("enter name")
		box2.pack_start(self.entry)
		self.entry.show()

		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("find")
		button.connect("clicked", self.on_find)
		box2.pack_start(button)
		button.set_flags(gtk.CAN_DEFAULT)
		button.grab_default()
		button.show()
		win.show()

	def on_find(self, *args) :
		s = self.entry.get_text()
		select_by (self.diagram, self.data, "name", s)
		self.data.update_extents ()
		self.diagram.flush()

	def on_delete (self, *args) :
		self.win.destroy ()

def do_dialog (d, data) :
	# let the dialog do it's work
	dlg = CFindDialog (d, data)

def select_by (diagram, data, name, value) :
	objs = data.active_layer.objects
	for o in objs :
		if o.properties.has_key (name) :
			print "<==", o.properties[name].value
			if value == None or o.properties[name].value == value :
				diagram.select (o)

def select_by_name_cb (data, flags) :
	d = dia.active_display().diagram
	do_dialog (d, data)

def select_by_selected (data, name) :
	d = dia.active_display().diagram
	grp = data.get_sorted_selected()
	bFoundAny = 0
	for o in grp :
		if o.properties.has_key(name) :
			select_by (d, data, name, o.properties[name].value)
			bFoundAny = 1
	if not bFoundAny :
		dia.message(0, "No selected object has the property '%s'." % name)
	data.update_extents ()
	d.flush()

def select_by_fill_color_cb (data, flags) :
	select_by_selected (data, "fill_colour")
def select_by_line_color_cb (data, flags) :
	select_by_selected (data, "line_colour")
def select_by_text_color_cb (data, flags) :
	select_by_selected (data, "text_colour")

def select_by_size_cb (data, flags) :
	""" From the diagrams selection derive the minimum and maximum object size.
	    Select every other object within this range.
	"""
	d = dia.active_display().diagram
	grp = data.get_sorted_selected()
	smin = 100000
	smax = 0
	for o in grp :
		w = o.bounding_box.right - o.bounding_box.left
		h = o.bounding_box.bottom - o.bounding_box.top
		s = w * h
		if s > smax :
			smax = s
		if s < smin :
			smin = s
	objs = data.active_layer.objects
	for o in objs :
		w = o.bounding_box.right - o.bounding_box.left
		h = o.bounding_box.bottom - o.bounding_box.top
		s = w * h
		if s >= smin and s <= smax :
			d.select(o)
	d.flush()


dia.register_action ("SelectByName", "Name", 
                       "/DisplayMenu/Select/SelectBy/SelectByExtensionStart", 
                       select_by_name_cb)
dia.register_action ("SelectByFillcolor", "Fill Color", 
                       "/DisplayMenu/Select/SelectBy/SelectByExtensionStart", 
                       select_by_fill_color_cb)
dia.register_action ("SelectByLinecolor", "Line Color", 
                       "/DisplayMenu/Select/SelectBy/SelectByExtensionStart", 
                       select_by_line_color_cb)
dia.register_action ("SelectByTextcolor", "Text Color", 
                       "/DisplayMenu/Select/SelectBy/SelectByExtensionStart", 
                       select_by_text_color_cb)
dia.register_action ("SelectBySize", "Size", 
                       "/DisplayMenu/Select/SelectBy/SelectByExtensionStart", 
                       select_by_size_cb)