Implement ActionList::Entry#clear.
[kaya.git] / lib / toolkits / qt_gui_builder.rb
blob17e7a7fd27c802eec13c78f5f94830649a15a581
1 # Copyright (c) 2009 Paolo Capriotti <p.capriotti@gmail.com>
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 module Qt
9   module GuiBuilder    
10     def self.build(window, gui)
11       Gui.new.build(window, nil, gui)
12     end
13     
14     def build(window, parent, desc)
15       element = create_element(window, parent, desc)
16       desc.children.each do |child|
17         b = builder(child.name).new
18         b.build(window, element, child)
19       end
20       element
21     end
22     
23     def setup_widget(widget, parent, layout, desc)
24       layout.add_widget(widget)
25       if desc.opts[:name]
26         parent.add_accessor(desc.opts[:name], widget)
27       end
28       if desc.opts[:properties]
29         desc.opts[:properties].each do |k, v|
30           widget.send("#{k}=", v)
31         end
32       end
33     end
34     
35     def builder(name)
36       GuiBuilder.const_get(name.to_s.capitalize.camelize)
37     end
38     
39     class Gui
40       include GuiBuilder
41       def create_element(window, parent, desc)
42         window
43       end
44     end
45     
46     class MenuBar
47       include GuiBuilder
48       
49       def create_element(window, parent, desc)
50         window.menu_bar
51       end
52     end
53     
54     class Menu
55       include GuiBuilder
56       
57       def create_element(window, parent, desc)
58         Qt::Menu.new(desc.opts[:text].to_s, window).tap do |menu|
59           parent.add_menu(menu)
60         end
61       end
62     end
63     
64     class Action
65       include GuiBuilder
66       
67       def create_element(window, parent, desc)
68         action = window.action_collection[desc.opts[:name]]
69         if action
70           parent.add_action(action)
71         end
72         action
73       end
74     end
75     
76     class Separator
77       include GuiBuilder
78       
79       def create_element(window, parent, desc)
80         parent.add_separator
81       end
82     end
83     
84     class Group
85       include GuiBuilder
86       
87       def create_element(window, parent, desc)
88         parent
89       end
90     end
91     
92     class ActionList
93       include GuiBuilder
95       class Entry
96         attr_reader :parent
98         def initialize(parent)
99           @parent = parent
100           @actions = []
101         end
103         def add_action(action)
104           @parent.add_action(action)
105           @actions << action
106         end
108         def clear
109           @actions.each do |action|
110             action.dispose
111           end
112           @actions = []
113         end
114       end
115       
116       def create_element(window, parent, desc)
117         entry = Entry.new(parent)
118         window.action_list_entries[desc.opts[:name]] << entry
119         parent
120       end
121     end
122     
123     class ToolBar
124       include GuiBuilder
125       
126       def create_element(window, parent, desc)
127         Qt::ToolBar.new(desc.opts[:text].to_s, parent).tap do |tb|
128           tb.object_name = desc.opts[:name].to_s
129           parent.add_tool_bar(Qt::TopToolBarArea, tb)
130         end
131       end
132     end
133     
134     class Layout
135       include GuiBuilder
136       
137       def create_element(window, parent, desc)
138         factory = if desc.opts[:type] == :horizontal
139           Qt::HBoxLayout
140         else
141           Qt::VBoxLayout
142         end
143         factory.new.tap do |layout|
144           layout.margin = desc.opts[:margin] if desc.opts[:margin]
145           parent.add_layout(layout)
146         end
147       end
148     end
149     
150     class Stretch
151       include GuiBuilder
152       
153       def create_element(window, parent, desc)
154         parent.add_stretch
155       end
156     end
157     
158     class Label
159       include GuiBuilder
160       
161       def create_element(window, parent, desc)
162         Qt::Label.new(desc.opts[:text].to_s, window).tap do |label|
163           setup_widget(label, window, parent, desc)
164           if desc.opts[:buddy]
165             window.buddies[label] = desc.opts[:buddy]
166           end
167         end
168       end
169     end
170     
171     class TabWidget
172       include GuiBuilder
173       
174       def create_element(window, parent, desc)
175         KDE::TabWidget.new(window).tap do |widget|
176           setup_widget(widget, window, parent, desc)
177           widget.owner = window.owner
178         end
179       end
180     end
181     
182     class Widget
183       include GuiBuilder
184       
185       def create_element(window, parent, desc)
186         factory(desc).new(window).tap do |widget|
187           setup_widget(widget, window, parent, desc)
188         end
189       end
190       
191       def factory(desc)
192         desc.opts[:factory]
193       end
194     end
195     
196     class Tab
197       include GuiBuilder
198       
199       class Helper
200         def initialize(parent, text)
201           @parent = parent
202           @text = text
203         end
204         
205         def add_widget(widget)
206           @parent.add_tab(widget, @text)
207         end
208       end
209       
210       def build(window, parent, desc)
211         desc.children.each do |child|
212           b = builder(child.name).new
213           b.build(parent, Helper.new(parent, desc.opts[:text]), child)
214         end
215       end
216     end
217     
218     class UrlRequester < Widget
219       def factory(desc)
220         KDE::UrlRequester
221       end
222     end
224     class LineEdit < Widget
225       def factory(desc)
226         Qt::LineEdit
227       end
228     end
229     
230     class ComboBox < Widget
231       def factory(desc)
232         KDE::ComboBox
233       end
234     end
235     
236     class List < Widget
237       def factory(desc)
238         Qt::ListView
239       end
240     end
241     
242     class CheckBox < Widget
243       def factory(desc)
244         Factory.new do |parent|
245           Qt::CheckBox.new(parent).tap do |check|
246             check.text = desc.opts[:text].to_s
247             check.checked = desc.opts[:checked]
248           end
249         end
250       end
251     end
252     
253     class Button < Widget
254       def factory(desc)
255         Factory.new do |parent|
256           KDE::PushButton.new(KDE::Icon.from_theme(desc.opts[:icon]), 
257                               desc.opts[:text], parent)
258         end
259       end
260     end
261   end