qtext 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,156 @@
1
+ require 'Qt4'
2
+ require 'qtext/extensions.rb'
3
+
4
+ =begin rdoc
5
+ This module can be used to make the construction of collections of actions
6
+ more rubyish. Menus are generally made up of a collection of actions.
7
+
8
+ Once included, it's intended to be called as follows, where self is a descendant of Qt::Widget:
9
+ build_actions do
10
+ list( :edit ) do
11
+ #~ new_action :action_cut, 'Cu&t', :shortcut => Qt::KeySequence::Cut
12
+ action :action_copy, '&Copy', :shortcut => Qt::KeySequence::Copy, :method => :copy_current_selection
13
+ action :action_paste, '&Paste', :shortcut => Qt::KeySequence::Paste, :method => :paste
14
+ separator
15
+ action :action_ditto, '&Ditto', :shortcut => 'Ctrl+\'', :method => :ditto, :tool_tip => 'Copy same field from previous record'
16
+ action :action_ditto_right, 'Ditto R&ight', :shortcut => 'Ctrl+]', :method => :ditto_right, :tool_tip => 'Copy field one to right from previous record'
17
+ action :action_ditto_left, '&Ditto L&eft', :shortcut => 'Ctrl+[', :method => :ditto_left, :tool_tip => 'Copy field one to left from previous record'
18
+ action :action_insert_date, 'Insert Date', :shortcut => 'Ctrl+;', :method => :insert_current_date
19
+ action :action_open_editor, '&Open Editor', :shortcut => 'F4', :method => :open_editor
20
+ separator
21
+ action :action_row, 'New Ro&w', :shortcut => 'Ctrl+N', :method => :row
22
+ action :action_refresh, '&Refresh', :shortcut => 'Ctrl+R', :method => :refresh
23
+ action :action_delete_rows, 'Delete Rows', :shortcut => 'Ctrl+Delete', :method => :delete_rows
24
+
25
+ if $options[:debug]
26
+ action :action_dump, 'D&ump', :shortcut => 'Ctrl+Shift+D' do
27
+ puts model.collection[current_index.row].inspect
28
+ end
29
+ end
30
+ end
31
+
32
+ separator
33
+ end
34
+
35
+ You can also do something like this:
36
+
37
+ build_actions do |ab|
38
+ ab.list( :edit ) do
39
+ #~ new_action :action_cut, 'Cu&t', :shortcut => Qt::KeySequence::Cut
40
+ ab.action :action_copy, '&Copy', :shortcut => Qt::KeySequence::Copy, :method => :copy_current_selection
41
+ end
42
+ end
43
+
44
+ If the including class defines a method called action_triggered( &block ),
45
+ it can be used to wrap the code triggered by actions. That way, the
46
+ including class
47
+ can catch exceptions and things like that. If this method is not
48
+ defined, it will be defined as an empty wrapper.
49
+ =end
50
+ module ActionBuilder
51
+ def build_actions( &block )
52
+ yield( self )
53
+ end
54
+
55
+ def collect_actions
56
+ @collect_actions ||= []
57
+ end
58
+
59
+ def collect_actions=( arr )
60
+ @collect_actions = arr
61
+ end
62
+
63
+ # create a new separator
64
+ def separator
65
+ Qt::Action.construct( parent ) do |action|
66
+ action.separator = true
67
+ add_action action
68
+ collect_actions << action
69
+ end
70
+ end
71
+
72
+ # create and return a list of actions. The actions are grouped together, but not
73
+ # as strongly as with Qt::ActionGroup
74
+ # a method called "#{group_name}_actions" will be added to self, which will return the
75
+ # set of Qt::Action instances created in the block
76
+ def list( group_name, &block )
77
+ unless respond_to?( "#{group_name.to_s}_actions" )
78
+ self.class.send( :define_method, "#{group_name.to_s}_actions" ) do
79
+ eval "@#{group_name.to_s}_actions"
80
+ end
81
+ end
82
+ self.collect_actions = []
83
+ yield( self )
84
+ # copy actions to the right instance variable
85
+ eval "@#{group_name.to_s}_actions = collect_actions"
86
+ end
87
+
88
+ # Create an action and add_action
89
+ # block takes predence over options[:method], which is a method
90
+ # on self to be called.
91
+ # option keys can be any method in Qt::Action, ie :tool_tip, :shortcut, :status_tip etc
92
+ # a value for :shortcut is automatically passed to Qt::KeySequence.new
93
+ def action( name_or_action, text = nil, options = {}, &block )
94
+ if name_or_action.class == Qt::Action
95
+ add_action( name_or_action )
96
+ else
97
+ name = name_or_action
98
+ if options.has_key?( :method ) && !block.nil?
99
+ raise "you can't specify both :method and a block"
100
+ end
101
+
102
+ Qt::Action.construct( parent ) do |action|
103
+ action.object_name = name.to_s
104
+ action.text = text
105
+ options.each do |k,v|
106
+ next if k == :method
107
+ if k == :shortcut
108
+ action.shortcut = Qt::KeySequence.new( v )
109
+ else
110
+ action.send( "#{k.to_s}=", v )
111
+ end
112
+ end
113
+
114
+ # add action for Qt
115
+ add_action action
116
+
117
+ # add actions for list. Yes, it's a side-effect. Is there a better way?
118
+ collect_actions << action
119
+
120
+ action_method_or_block( action, options, &block )
121
+ end
122
+ end
123
+ end
124
+
125
+ # if parent doesn't define this, add it so that
126
+ # our action_method_or_block will work.
127
+ unless instance_methods.include?( :action_triggered )
128
+ def action_triggered( &someblock )
129
+ yield
130
+ end
131
+ end
132
+
133
+ # set up the code to be executed when an action is triggered,
134
+ def action_method_or_block( qt_action, options, &block )
135
+ signal_name = "triggered(#{options.has_key?( :checkable ) ? 'bool' : ''})"
136
+
137
+ # connect the action to some code
138
+ if options.has_key?( :method )
139
+ qt_action.connect SIGNAL( signal_name ) do |active|
140
+ action_triggered do
141
+ send_args = [ options[:method], options.has_key?( :checkable ) ? active : nil ].compact
142
+ send( *send_args )
143
+ end
144
+ end
145
+ else
146
+ unless block.nil?
147
+ action_triggered do
148
+ qt_action.connect SIGNAL( signal_name ) do |active|
149
+ yield( active )
150
+ end
151
+ end
152
+ end
153
+ end
154
+ end
155
+
156
+ end
@@ -18,6 +18,9 @@ class Object
18
18
  end
19
19
 
20
20
  class Class
21
+ # TODO Qt bindings will accept a block for new, which
22
+ # makes this somewhat irrelevant. Unless it's really
23
+ # necessary to pass args in
21
24
  def construct_exec( *args, &block )
22
25
  raise "block is nil" if block.nil?
23
26
  inst = self.new( *args )
@@ -27,6 +30,7 @@ class Class
27
30
  inst
28
31
  end
29
32
 
33
+ # see self.construct
30
34
  def construct( *args, &block )
31
35
  raise "block is nil" if block.nil?
32
36
  inst = self.new( *args )
data/lib/qtext/version.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  module Qtext #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
- MINOR = 4
5
- TINY = 1
4
+ MINOR = 5
5
+ TINY = 0
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qtext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - FIXME full name
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-07-23 00:00:00 +02:00
12
+ date: 2008-08-21 00:00:00 +02:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -34,6 +34,7 @@ extra_rdoc_files:
34
34
  files:
35
35
  - README.txt
36
36
  - lib/qtext.rb
37
+ - lib/qtext/action_builder.rb
37
38
  - lib/qtext/version.rb
38
39
  - lib/qtext/flags.rb
39
40
  - lib/qtext/extensions.rb