tk-double-slider 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ j�w�BoolP(��@L��ם&��JT#��d*�<�c A�j�e�9���IB3R�� (Or�M�
2
+ ������+����_�>�� �U?��O���?#){'��X�� 5R
3
+ � "%�E-0W֯��S!7{0,p^|�&����k�,����O��� �^�g_~J�� ����
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Chris Lee, PhD
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,19 @@
1
+ = tk-double-slider
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to tk-double-slider
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Chris Lee, PhD. See LICENSE.txt for
18
+ further details.
19
+
@@ -0,0 +1 @@
1
+ require 'tk-double-slider/base'
@@ -0,0 +1,171 @@
1
+ # DESCRIPTION: provides a TK widget with two "heads", a high and a low.
2
+ # Copyright (C) 2007 Christopher Lee
3
+ #
4
+ # This program is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
+ module Tk
17
+ class DoubleSlider
18
+ def pack(*args)
19
+ @canvas.pack(*args)
20
+ end
21
+
22
+ def redraw
23
+ @canvas.delete('all');
24
+ x1 = value2x(@low)
25
+ x2 = value2x(@high)
26
+ y = @height/2
27
+ TkcText.new(@canvas, 2, @height - 2, :text=>@label, :anchor=>'sw', :fill => @colors[:text]) if @label
28
+ TkcLine.new(@canvas, @left_margin, y, @width - @right_margin, y, :fill => @colors[:line], :width => 1, :tags => ['track'])
29
+ TkcLine.new(@canvas, @left_margin, y, @width - @right_margin, y, :fill => @colors[:line], :width => 4, :tags => ['selection'])
30
+ TkcOval.new(@canvas, x1 - @ballsize, y - @ballsize, x1 + @ballsize, y + @ballsize, :fill => @colors[:low_head], :width => 1, :tags => ['low'])
31
+ TkcOval.new(@canvas, x2 - @ballsize, y - @ballsize, x2 + @ballsize, y + @ballsize, :fill => @colors[:high_head], :width => 1, :tags => ['high'])
32
+ if @valuefmt
33
+ TkcText.new(@canvas, x1, @top_margin, :text => @valuefmt.call(@low), :tags => ['lowvalue'], :fill => @colors[:text])
34
+ TkcText.new(@canvas, x2, @height - @bottom_margin, :text => @valuefmt.call(@high), :tags => ['highvalue'], :fill => @colors[:text])
35
+ fmt = @deltafmt || @valuefmt # use delta format instead of value format if it is defined
36
+ TkcText.new(@canvas, (x1+x2)/2, y, :text => fmt.call(@high - @low), :tags => ['delta'], :fill => @colors[:delta])
37
+ coords = @canvas.bbox('delta')
38
+ TkcRectangle.new(@canvas,coords[0], coords[1], coords[2], coords[3], :fill => @colors[:background], :tags => ['deltabg'], :outline => @colors[:background]) if coords.length > 0
39
+ @canvas.raise('delta','all')
40
+ end
41
+ @canvas.itembind('low', 'ButtonPress-1', proc { @sel = 'low' })
42
+ @canvas.itembind('high', 'ButtonPress-1', proc { @sel = 'high' })
43
+ @canvas.itembind('all', 'ButtonRelease-1', proc { @sel = nil })
44
+ @canvas.itembind('low||high', 'Button1-Motion', proc { |x,y| move(@sel,x,y) }, "%x %y")
45
+ end
46
+
47
+ def move(tag, x, y)
48
+ return unless @sel
49
+ @canvas.raise @sel, 'all'
50
+ x = @left_margin if x < @left_margin
51
+ x = @width - @right_margin if x > @width - @right_margin
52
+ x = value2x(snap(x2value(x)))
53
+ y = @height/2
54
+ # don't update the text and deltas unless they need to be updated
55
+ return if instance_variable_get("@#{@sel}") == x2value(x)
56
+ instance_variable_set("@#{@sel}",x2value(x))
57
+ @canvas.coords(@sel, x - @ballsize, y - @ballsize, x + @ballsize, y + @ballsize)
58
+ if @sel == 'low' and @low > @high
59
+ @high = @low
60
+ @canvas.coords('high', x - @ballsize, y - @ballsize, x + @ballsize, y + @ballsize)
61
+ elsif @sel == 'high' and @high < @low
62
+ @low = @high
63
+ @canvas.coords('low', x - @ballsize, y - @ballsize, x + @ballsize, y + @ballsize)
64
+ end
65
+ lx = value2x(@low)
66
+ hx = value2x(@high)
67
+ @canvas.coords('selection', lx, y, hx, y)
68
+ if @valuefmt
69
+ @canvas.coords('lowvalue', lx, @top_margin)
70
+ @canvas.coords('highvalue', hx, @height - @bottom_margin)
71
+ @canvas.coords('delta', (lx+hx)/2, y)
72
+ @canvas.itemconfigure('lowvalue', :text => @valuefmt.call(@low))
73
+ @canvas.itemconfigure('highvalue', :text => @valuefmt.call(@high))
74
+ fmt = @deltafmt || @valuefmt # use delta format instead of value format if it is defined
75
+ @canvas.itemconfigure('delta', :text => fmt.call(@high - @low))
76
+ coords = @canvas.bbox('delta')
77
+ w1 = coords[2] - coords[0]
78
+ if coords[0] < lx +5
79
+ if lx < @width/2
80
+ coords2 = @canvas.bbox('lowvalue')
81
+ ox = coords2[2] + 10 + (w1/2)
82
+ oy = @top_margin
83
+ @canvas.coords('delta',ox,oy)
84
+ else
85
+ coords2 = @canvas.bbox('highvalue')
86
+ ox = coords2[0] - 10 - (w1/2)
87
+ oy = @height - @bottom_margin
88
+ @canvas.coords('delta',ox,oy)
89
+ end
90
+ end
91
+ coords = @canvas.bbox('delta')
92
+ @canvas.coords('deltabg', coords[0], coords[1], coords[2], coords[3])
93
+ end
94
+ @change_cb.call(@low,@high) if @change_cb
95
+ end
96
+
97
+ def snap(x)
98
+ return x unless @snap
99
+ return @max if x > @max - @snap
100
+ x = (x - (x % @snap)).to_i
101
+ return x
102
+ end
103
+
104
+ def x2value(x)
105
+ x = @left_margin if x < @left_margin
106
+ r = @width - @right_margin
107
+ x = r if x > r
108
+ l = @left_margin
109
+ o = (x - l).to_f
110
+ dx = (r - l).to_f
111
+ frac = o/dx
112
+ if @logbase
113
+ return @max**(o/dx)
114
+ else
115
+ return @min+((o/dx)*(@max - @min))
116
+ end
117
+ end
118
+
119
+ def value2x(v)
120
+ v = @max if v > @max
121
+ v = @min if v < @min
122
+ w = @width - @left_margin - @right_margin
123
+ if @logbase
124
+ return @left_margin + (Math.log(v)*w/Math.log(@max))
125
+ else
126
+ return @left_margin + (((v - @min).to_f/(@max - @min).to_f)*w)
127
+ end
128
+ end
129
+
130
+ attr_accessor :change_cb, :valuefmt, :colors
131
+ def initialize(parent,*args)
132
+ @height = 36.0
133
+ @width = 360.0
134
+ @min = @max = @low = @high = 0.0
135
+ @ballsize = 5
136
+ @snap = false
137
+ @logbase = false
138
+ @colors = {
139
+ :background => 'grey20',
140
+ :line => 'grey75',
141
+ :low_head => '#996666',
142
+ :high_head => '#996666',
143
+ :text => 'white',
144
+ :delta => 'white',
145
+ }
146
+ @left_margin = 10.0
147
+ @right_margin = 20.0
148
+ @top_margin = 6.0
149
+ @bottom_margin = 4.0
150
+ @selection = nil
151
+ @change_cb = nil
152
+ @valuefmt = proc { |x| sprintf "%d", x }
153
+ @deltafmt = nil
154
+ @label = nil
155
+ if args.length > 0
156
+ args[0].each do |k,v|
157
+ if instance_variable_defined?("@#{k}")
158
+ if v =~ /^[\d\.]+$/
159
+ instance_variable_set("@#{k}", v.to_f)
160
+ else
161
+ instance_variable_set("@#{k}", v)
162
+ end
163
+ end
164
+ end
165
+ end
166
+ @canvas = TkCanvas.new(parent, :width => @width, :height => @height, :scrollregion => [0, 0, @width, @height])
167
+ @canvas.configure('background', @colors[:background]) if @colors[:background]
168
+ redraw
169
+ end
170
+ end
171
+ end
@@ -0,0 +1,20 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ require 'tk'
14
+
15
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
16
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
17
+ require 'tk-double-slider'
18
+
19
+ class Test::Unit::TestCase
20
+ end
@@ -0,0 +1,18 @@
1
+ require 'helper'
2
+
3
+ class TestTkDoubleSlider < Test::Unit::TestCase
4
+ should "instantiate and display a double-headed slider with linear scale and custom value and delta formatters" do
5
+ root = TkRoot.new() {
6
+ title "Tk::DoubleSlider Test"
7
+ protocol('WM_DELETE_WINDOW', proc{ exit })
8
+ }
9
+ root.bind('Control-c', proc{ exit })
10
+ left_frame = TkFrame.new(root)
11
+ left_frame.grid(:row=>0,:column=>0,:sticky=>'new')
12
+ time_min = Time.now.to_i - (7*24*60*60)
13
+ time_max = Time.now.to_i
14
+ timeslide = Tk::DoubleSlider.new(left_frame, :min=>time_min, :max=>time_max, :low=>time_min, :high=>time_max, :snap => 300, :label=>'Time', :valuefmt => proc { |x| Time.at(x).strftime("%H:%M") }, :deltafmt => proc { |x| sprintf("%0.2f hours", (x/3600.0)) })
15
+ timeslide.pack()
16
+ Tk.mainloop()
17
+ end
18
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tk-double-slider
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Chris Lee
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain:
17
+ - |
18
+ -----BEGIN CERTIFICATE-----
19
+ MIIDYjCCAkqgAwIBAgIBADANBgkqhkiG9w0BAQUFADBXMREwDwYDVQQDDAhydWJ5
20
+ Z2VtczEYMBYGCgmSJomT8ixkARkWCGNocmlzbGVlMRMwEQYKCZImiZPyLGQBGRYD
21
+ ZGhzMRMwEQYKCZImiZPyLGQBGRYDb3JnMB4XDTExMDIyNzE1MzAxOVoXDTEyMDIy
22
+ NzE1MzAxOVowVzERMA8GA1UEAwwIcnVieWdlbXMxGDAWBgoJkiaJk/IsZAEZFghj
23
+ aHJpc2xlZTETMBEGCgmSJomT8ixkARkWA2RoczETMBEGCgmSJomT8ixkARkWA29y
24
+ ZzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALNM1Hjs6q58sf7Jp64A
25
+ vEY2cnRWDdFpD8UWpwaJK5kgSHOVgs+0mtszn+YlYjmx8kpmuYpyU4g9mNMImMQe
26
+ ow8pVsL4QBBK/1Ozgdxrsptk3IiTozMYA+g2I/+WvZSEDu9uHkKe8pvMBEMrg7RJ
27
+ IN7+jWaPnSzg3DbFwxwOdi+QRw33DjK7oFWcOaaBqWTUpI4epdi/c/FE1I6UWULJ
28
+ ZF/Uso0Sc2Pp/YuVhuMHGrUbn7zrWWo76nnK4DTLfXFDbZF5lIXT1w6BtIiN6Ho9
29
+ Rdr/W6663hYUo3WMsUSa3I5+PJXEBKmGHIZ2TNFnoFIRHha2fmm1HC9+BTaKwcO9
30
+ PLcCAwEAAaM5MDcwCQYDVR0TBAIwADAdBgNVHQ4EFgQURzsNkZo2rv86Ftc+hVww
31
+ RNICMrwwCwYDVR0PBAQDAgSwMA0GCSqGSIb3DQEBBQUAA4IBAQBRRw/iNA/PdnvW
32
+ OBoNCSr/IiHOGZqMHgPJwyWs68FhThnLc2EyIkuLTQf98ms1/D3p0XX9JsxazvKT
33
+ W/in8Mm/R2fkVziSdzqChtw/4Z4bW3c+RF7TgX6SP5cKxNAfKmAPuItcs2Y+7bdS
34
+ hr/FktVtT2iAmISRnlEbdaTpfl6N2ZWNT83khV6iOs5xRkX/+0e+GgAv9mE6nqr1
35
+ AkuDXMhposxcnFZUrZ3UtMPEe/JnyP7Vv6pvr3qtZm8FidFZU91+rX/fwdyBU8RP
36
+ /5l8uLWXXNt1wEbtu4N1I66LwTK2iRrQZE8XtlgZGbxYDFUkiurq3OafF2YwRs6W
37
+ 6yhklP75
38
+ -----END CERTIFICATE-----
39
+
40
+ date: 2011-03-07 00:00:00 -05:00
41
+ default_executable:
42
+ dependencies:
43
+ - !ruby/object:Gem::Dependency
44
+ version_requirements: &id001 !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ hash: 3
50
+ segments:
51
+ - 0
52
+ version: "0"
53
+ requirement: *id001
54
+ prerelease: false
55
+ name: shoulda
56
+ type: :development
57
+ - !ruby/object:Gem::Dependency
58
+ version_requirements: &id002 !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ~>
62
+ - !ruby/object:Gem::Version
63
+ hash: 23
64
+ segments:
65
+ - 1
66
+ - 0
67
+ - 0
68
+ version: 1.0.0
69
+ requirement: *id002
70
+ prerelease: false
71
+ name: bundler
72
+ type: :development
73
+ - !ruby/object:Gem::Dependency
74
+ version_requirements: &id003 !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ~>
78
+ - !ruby/object:Gem::Version
79
+ hash: 7
80
+ segments:
81
+ - 1
82
+ - 5
83
+ - 2
84
+ version: 1.5.2
85
+ requirement: *id003
86
+ prerelease: false
87
+ name: jeweler
88
+ type: :development
89
+ - !ruby/object:Gem::Dependency
90
+ version_requirements: &id004 !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ hash: 3
96
+ segments:
97
+ - 0
98
+ version: "0"
99
+ requirement: *id004
100
+ prerelease: false
101
+ name: rcov
102
+ type: :development
103
+ description: This provides an advanced, double-headed slider widget that allows selection of a range of values on a linear, log, square root, or cube root axis.
104
+ email: rubygems@chrislee.dhs.org
105
+ executables: []
106
+
107
+ extensions: []
108
+
109
+ extra_rdoc_files:
110
+ - LICENSE.txt
111
+ - README.rdoc
112
+ files:
113
+ - lib/tk-double-slider.rb
114
+ - lib/tk-double-slider/base.rb
115
+ - LICENSE.txt
116
+ - README.rdoc
117
+ - test/helper.rb
118
+ - test/test_tk-double-slider.rb
119
+ has_rdoc: true
120
+ homepage: https://rubygems.org/gems/tk-double-slider
121
+ licenses:
122
+ - MIT
123
+ post_install_message:
124
+ rdoc_options: []
125
+
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ hash: 3
134
+ segments:
135
+ - 0
136
+ version: "0"
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ hash: 3
143
+ segments:
144
+ - 0
145
+ version: "0"
146
+ requirements: []
147
+
148
+ rubyforge_project:
149
+ rubygems_version: 1.6.1
150
+ signing_key:
151
+ specification_version: 3
152
+ summary: A double headed slider widget for Tk
153
+ test_files:
154
+ - test/helper.rb
155
+ - test/test_tk-double-slider.rb
Binary file