Add action list support to qtonly.
[kaya.git] / lib / toolkits / qt_gui_builder.rb
blob0fb1d3040e046c4c6778df91cbc899103989d62f
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         end
110       end
111       
112       def create_element(window, parent, desc)
113         entry = Entry.new(parent)
114         window.action_list_entries[desc.opts[:name]] << entry
115         parent
116       end
117     end
118     
119     class ToolBar
120       include GuiBuilder
121       
122       def create_element(window, parent, desc)
123         Qt::ToolBar.new(desc.opts[:text].to_s, parent).tap do |tb|
124           tb.object_name = desc.opts[:name].to_s
125           parent.add_tool_bar(Qt::TopToolBarArea, tb)
126         end
127       end
128     end
129     
130     class Layout
131       include GuiBuilder
132       
133       def create_element(window, parent, desc)
134         factory = if desc.opts[:type] == :horizontal
135           Qt::HBoxLayout
136         else
137           Qt::VBoxLayout
138         end
139         factory.new.tap do |layout|
140           layout.margin = desc.opts[:margin] if desc.opts[:margin]
141           parent.add_layout(layout)
142         end
143       end
144     end
145     
146     class Stretch
147       include GuiBuilder
148       
149       def create_element(window, parent, desc)
150         parent.add_stretch
151       end
152     end
153     
154     class Label
155       include GuiBuilder
156       
157       def create_element(window, parent, desc)
158         Qt::Label.new(desc.opts[:text].to_s, window).tap do |label|
159           setup_widget(label, window, parent, desc)
160           if desc.opts[:buddy]
161             window.buddies[label] = desc.opts[:buddy]
162           end
163         end
164       end
165     end
166     
167     class TabWidget
168       include GuiBuilder
169       
170       def create_element(window, parent, desc)
171         KDE::TabWidget.new(window).tap do |widget|
172           setup_widget(widget, window, parent, desc)
173           widget.owner = window.owner
174         end
175       end
176     end
177     
178     class Widget
179       include GuiBuilder
180       
181       def create_element(window, parent, desc)
182         factory(desc).new(window).tap do |widget|
183           setup_widget(widget, window, parent, desc)
184         end
185       end
186       
187       def factory(desc)
188         desc.opts[:factory]
189       end
190     end
191     
192     class Tab
193       include GuiBuilder
194       
195       class Helper
196         def initialize(parent, text)
197           @parent = parent
198           @text = text
199         end
200         
201         def add_widget(widget)
202           @parent.add_tab(widget, @text)
203         end
204       end
205       
206       def build(window, parent, desc)
207         desc.children.each do |child|
208           b = builder(child.name).new
209           b.build(parent, Helper.new(parent, desc.opts[:text]), child)
210         end
211       end
212     end
213     
214     class UrlRequester < Widget
215       def factory(desc)
216         KDE::UrlRequester
217       end
218     end
220     class LineEdit < Widget
221       def factory(desc)
222         Qt::LineEdit
223       end
224     end
225     
226     class ComboBox < Widget
227       def factory(desc)
228         KDE::ComboBox
229       end
230     end
231     
232     class List < Widget
233       def factory(desc)
234         Qt::ListView
235       end
236     end
237     
238     class CheckBox < Widget
239       def factory(desc)
240         Factory.new do |parent|
241           Qt::CheckBox.new(parent).tap do |check|
242             check.text = desc.opts[:text].to_s
243             check.checked = desc.opts[:checked]
244           end
245         end
246       end
247     end
248     
249     class Button < Widget
250       def factory(desc)
251         Factory.new do |parent|
252           KDE::PushButton.new(KDE::Icon.from_theme(desc.opts[:icon]), 
253                               desc.opts[:text], parent)
254         end
255       end
256     end
257   end