Class: Tk::DoubleSlider

Inherits:
Object
  • Object
show all
Defined in:
lib/tk-double-slider/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, *args) ⇒ DoubleSlider

Returns a new instance of DoubleSlider.



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
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/tk-double-slider/base.rb', line 131

def initialize(parent,*args)
	@height = 36.0
	@width = 360.0
	@min = @max = @low = @high = 0.0
	@ballsize = 5
	@snap = false
	@logbase = false
	@colors = {
		:background => 'grey20',
		:line => 'grey75',
		:low_head => '#996666',
		:high_head => '#996666',
		:text => 'white',
		:delta => 'white',
	}
	@left_margin = 10.0
	@right_margin = 20.0
	@top_margin = 6.0
	@bottom_margin = 4.0
	@selection = nil
	@change_cb = nil
	@valuefmt = proc { |x| sprintf "%d", x }
	@deltafmt = nil
	@label = nil
	if args.length > 0
		args[0].each do |k,v|
			if instance_variable_defined?("@#{k}")
				if v =~ /^[\d\.]+$/
					instance_variable_set("@#{k}", v.to_f)
				else
					instance_variable_set("@#{k}", v)
				end
			end
		end
	end
	@canvas = TkCanvas.new(parent, :width => @width, :height => @height, :scrollregion => [0, 0, @width, @height])
	@canvas.configure('background', @colors[:background]) if @colors[:background]
	redraw
end

Instance Attribute Details

#change_cbObject

Returns the value of attribute change_cb.



130
131
132
# File 'lib/tk-double-slider/base.rb', line 130

def change_cb
  @change_cb
end

#colorsObject

Returns the value of attribute colors.



130
131
132
# File 'lib/tk-double-slider/base.rb', line 130

def colors
  @colors
end

#valuefmtObject

Returns the value of attribute valuefmt.



130
131
132
# File 'lib/tk-double-slider/base.rb', line 130

def valuefmt
  @valuefmt
end

Instance Method Details

#move(tag, x, y) ⇒ Object



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
# File 'lib/tk-double-slider/base.rb', line 47

def move(tag, x, y)
	return unless @sel
	@canvas.raise @sel, 'all'
	x = @left_margin if x < @left_margin
	x = @width - @right_margin if x > @width - @right_margin
	x = value2x(snap(x2value(x)))
	y = @height/2
	# don't update the text and deltas unless they need to be updated
	return if instance_variable_get("@#{@sel}") == x2value(x)
	instance_variable_set("@#{@sel}",x2value(x))
	@canvas.coords(@sel, x - @ballsize, y - @ballsize, x + @ballsize, y + @ballsize)
	if @sel == 'low' and @low > @high
		@high = @low
		@canvas.coords('high', x - @ballsize, y - @ballsize, x + @ballsize, y + @ballsize)
	elsif @sel == 'high' and @high < @low
		@low = @high
		@canvas.coords('low', x - @ballsize, y - @ballsize, x + @ballsize, y + @ballsize)
	end
	lx = value2x(@low)
	hx = value2x(@high)
	@canvas.coords('selection', lx, y, hx, y)
	if @valuefmt
		@canvas.coords('lowvalue', lx, @top_margin)
		@canvas.coords('highvalue', hx, @height - @bottom_margin)
		@canvas.coords('delta', (lx+hx)/2, y)
		@canvas.itemconfigure('lowvalue', :text => @valuefmt.call(@low))
		@canvas.itemconfigure('highvalue', :text => @valuefmt.call(@high))
		fmt = @deltafmt || @valuefmt  # use delta format instead of value format if it is defined
		@canvas.itemconfigure('delta', :text => fmt.call(@high - @low))
		coords = @canvas.bbox('delta')
		w1 = coords[2] - coords[0]
		if coords[0] < lx +5
			if lx < @width/2
				coords2 = @canvas.bbox('lowvalue')
				ox = coords2[2] + 10 + (w1/2)
				oy = @top_margin
				@canvas.coords('delta',ox,oy)
			else
				coords2 = @canvas.bbox('highvalue')
				ox = coords2[0] - 10 - (w1/2)
				oy = @height - @bottom_margin
				@canvas.coords('delta',ox,oy)
			end
		end
		coords = @canvas.bbox('delta')
		@canvas.coords('deltabg', coords[0], coords[1], coords[2], coords[3])
	end
	@change_cb.call(@low,@high) if @change_cb
end

#pack(*args) ⇒ Object



18
19
20
# File 'lib/tk-double-slider/base.rb', line 18

def pack(*args)
	@canvas.pack(*args)
end

#redrawObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/tk-double-slider/base.rb', line 22

def redraw
	@canvas.delete('all');
	x1 = value2x(@low)
	x2 = value2x(@high)
	y = @height/2
	TkcText.new(@canvas, 2, @height - 2, :text=>@label, :anchor=>'sw', :fill => @colors[:text]) if @label
	TkcLine.new(@canvas, @left_margin, y, @width - @right_margin, y, :fill => @colors[:line], :width => 1, :tags => ['track'])
	TkcLine.new(@canvas, @left_margin, y, @width - @right_margin, y, :fill => @colors[:line], :width => 4, :tags => ['selection'])
	TkcOval.new(@canvas, x1 - @ballsize, y - @ballsize, x1 + @ballsize, y + @ballsize, :fill => @colors[:low_head], :width => 1, :tags => ['low'])
	TkcOval.new(@canvas, x2 - @ballsize, y - @ballsize, x2 + @ballsize, y + @ballsize, :fill => @colors[:high_head], :width => 1, :tags => ['high'])
	if @valuefmt
		TkcText.new(@canvas, x1, @top_margin, :text => @valuefmt.call(@low), :tags => ['lowvalue'], :fill => @colors[:text])
		TkcText.new(@canvas, x2, @height - @bottom_margin, :text => @valuefmt.call(@high), :tags => ['highvalue'], :fill => @colors[:text])
		fmt = @deltafmt || @valuefmt  # use delta format instead of value format if it is defined
		TkcText.new(@canvas, (x1+x2)/2, y, :text => fmt.call(@high - @low), :tags => ['delta'], :fill => @colors[:delta])
		coords = @canvas.bbox('delta')
		TkcRectangle.new(@canvas,coords[0], coords[1], coords[2], coords[3], :fill => @colors[:background], :tags => ['deltabg'], :outline => @colors[:background]) if coords.length > 0
		@canvas.raise('delta','all')
	end
	@canvas.itembind('low', 'ButtonPress-1', proc { @sel = 'low' })
	@canvas.itembind('high', 'ButtonPress-1', proc { @sel = 'high' })
	@canvas.itembind('all', 'ButtonRelease-1', proc { @sel = nil })
	@canvas.itembind('low||high', 'Button1-Motion', proc { |x,y| move(@sel,x,y) }, "%x %y")  
end

#snap(x) ⇒ Object



97
98
99
100
101
102
# File 'lib/tk-double-slider/base.rb', line 97

def snap(x)
	return x unless @snap
	return @max if x > @max - @snap
	x = (x - (x % @snap)).to_i
	return x
end

#value2x(v) ⇒ Object



119
120
121
122
123
124
125
126
127
128
# File 'lib/tk-double-slider/base.rb', line 119

def value2x(v)
	v = @max if v > @max
	v = @min if v < @min
	w = @width - @left_margin - @right_margin
	if @logbase
		return @left_margin + (Math.log(v)*w/Math.log(@max))
	else
		return @left_margin + (((v - @min).to_f/(@max - @min).to_f)*w)
	end
end

#x2value(x) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/tk-double-slider/base.rb', line 104

def x2value(x)
	x = @left_margin if x < @left_margin
	r = @width - @right_margin
	x = r if x > r
	l = @left_margin
	o = (x - l).to_f
	dx = (r - l).to_f
	frac = o/dx
	if @logbase
		return @max**(o/dx)
	else
		return @min+((o/dx)*(@max - @min))
	end
end