Implement ActionList::Entry#clear.
[kaya.git] / lib / theme_prefs.rb
blob031f0c3e83e2cfd04890fa5a5d215abf644b6c03
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 require 'toolkit'
10 class ThemePrefs < KDE::Dialog
11   include Observable
12   
13   def initialize(loader, theme_loader, parent)
14     super(parent)
15     
16     @loader = loader
17     @theme_loader = theme_loader
18     @combos = [
19       [:pieces, KDE::i18n("&Pieces:")],
20       [:board, KDE::i18n("&Board:")],
21       [:layout, KDE::i18n("&Layout:")],
22       [:clock, KDE::i18n("&Clock:")]]
23     @lists = {
24       :game => Factory.new {|p| Game.new_list(p) },
25       :category => Factory.new {|p| Qt::ListWidget.from_a(p, Game.categories) }
26     }
27     
28     @gui = KDE::autogui(:themes, 
29                         :caption => KDE::i18n("Configure themes")) do |g|
30       g.layout(:type => :horizontal) do |l|
31         l.tab_widget(:tabs) do |tabs|
32           tabs.tab(:text => KDE::i18n("&Games")) do |t|
33             t.widget(:game, :factory => @lists[:game])
34           end
35           tabs.tab(:text => KDE::i18n("&Categories")) do |t|
36             t.widget(:category, :factory => @lists[:category])
37           end
38         end
39         l.layout(:type => :vertical) do |info|
40           @combos.each do |name, text|
41             info.label(:text => text, :buddy => name)
42             info.widget(name, :factory => combo_factory(name))
43           end
44           info.stretch
45         end
46       end
47     end
48     setGUI(@gui)
49     
50     tabs.current_index = 0
51     game.current_index = 0
52     
53     update
54     tabs.on(:current_changed) { update }
55     @lists.each_key {|name| send(name).on(:item_selection_changed) { update(name) } }
56     on(:ok_clicked) do
57       @theme_loader.save
58       fire :ok
59     end
60   end
61   
62   private
63   
64   def current_type
65     if tabs.current_widget == game
66       :game
67     else
68       :category
69     end
70   end
71   
72   def item_name(type, data)
73     type == :game ? data.class.data(:id) : data
74   end
75   
76   def combo_factory(name)
77     Factory.new do |parent|
78       themes = @loader.get_all_matching(name).map do |plugin|
79         [plugin.plugin_name, plugin]
80       end
81       themes = [['', nil]] + themes
82       KDE::ComboBox.from_a(parent, themes).tap do |combo|
83         combo.on(:current_index_changed, ["int"]) do |i|
84           type = current_type
85           item = send(type).current_item
86           @theme_loader.set(type, item_name(type, item.get), 
87                             name, combo.current_item.get) if item
88         end
89       end
90     end
91   end
93   def update(type = nil)
94     type ||= current_type
95     item = send(type).current_item
96     
97     @combos.each do |component, _|
98       send(component).enabled = !!item
99     end
100     
101     if item
102       theme = @theme_loader.load_spec(type => item.get)
103       @combos.each do |component, _|
104         klass = theme[component]
105         send(component).select_item do |data|
106           data == klass
107         end
108       end
109     end
110   end