Class: Alexandria::UI::SmartLibraryRuleBox

Inherits:
Object
  • Object
show all
Defined in:
lib/alexandria/ui/smart_library_rule_box.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent) ⇒ SmartLibraryRuleBox

Returns a new instance of SmartLibraryRuleBox.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/alexandria/ui/smart_library_rule_box.rb', line 15

def initialize(parent)
  @parent = parent

  self.rule_box = Gtk::Box.new :horizontal
  rule_box.spacing = 8

  self.left_operand_combo = Gtk::ComboBoxText.new
  self.operator_combo = Gtk::ComboBoxText.new

  self.value_entry = Gtk::Entry.new

  self.date_entry = Gtk::Entry.new.tap do |entry|
    entry.primary_icon_name = Gtk::Stock::EDIT

    entry.primary_icon_activatable = true
    entry.signal_connect("icon-press") do |widget, primary, icon|
      @parent.handle_date_icon_press(widget, primary, icon)
    end
  end

  self. = Gtk::Label.new("")

  self.add_button = Gtk::Button.new(label: "").tap do |widget|
    widget.remove(widget.children.first)
    widget << Gtk::Image.new(stock: Gtk::Stock::ADD,
                             size: Gtk::IconSize::BUTTON)

    widget.signal_connect("clicked") { @parent.handle_add_rule_clicked }
  end

  self.remove_button = Gtk::Button.new(label: "")
  remove_button.remove(remove_button.children.first)
  remove_button << Gtk::Image.new(stock: Gtk::Stock::REMOVE,
                                  size: Gtk::IconSize::BUTTON)

  remove_button.signal_connect("clicked") do |_button|
    @parent.handle_remove_rule_clicked(self)
  end

  operands.each do |operand|
    left_operand_combo.append_text(operand.name)
  end

  operator_combo.signal_connect("changed") do
    handle_operator_changed
  end

  left_operand_combo.signal_connect("changed") do
    handle_left_operand_changed
  end

  rule_box.pack_start(left_operand_combo, expand: false, fill: false)
  rule_box.pack_start(operator_combo, expand: false, fill: false)
  rule_box.pack_start(value_entry)
  rule_box.pack_start(date_entry)
  rule_box.pack_start(, expand: false, fill: false)
  rule_box.pack_end(remove_button, expand: false, fill: false)
  rule_box.pack_end(add_button, expand: false, fill: false)

  value_entry.visible = date_entry.visible = .visible = false
end

Instance Attribute Details

#add_buttonObject

Returns the value of attribute add_button.



10
11
12
# File 'lib/alexandria/ui/smart_library_rule_box.rb', line 10

def add_button
  @add_button
end

#date_entryObject

Returns the value of attribute date_entry.



10
11
12
# File 'lib/alexandria/ui/smart_library_rule_box.rb', line 10

def date_entry
  @date_entry
end

#entry_labelObject

Returns the value of attribute entry_label.



10
11
12
# File 'lib/alexandria/ui/smart_library_rule_box.rb', line 10

def 
  @entry_label
end

#left_operand_comboObject

Returns the value of attribute left_operand_combo.



10
11
12
# File 'lib/alexandria/ui/smart_library_rule_box.rb', line 10

def left_operand_combo
  @left_operand_combo
end

#operator_comboObject

Returns the value of attribute operator_combo.



10
11
12
# File 'lib/alexandria/ui/smart_library_rule_box.rb', line 10

def operator_combo
  @operator_combo
end

#remove_buttonObject

Returns the value of attribute remove_button.



10
11
12
# File 'lib/alexandria/ui/smart_library_rule_box.rb', line 10

def remove_button
  @remove_button
end

#rule_boxObject

Returns the value of attribute rule_box.



10
11
12
# File 'lib/alexandria/ui/smart_library_rule_box.rb', line 10

def rule_box
  @rule_box
end

#value_entryObject

Returns the value of attribute value_entry.



10
11
12
# File 'lib/alexandria/ui/smart_library_rule_box.rb', line 10

def value_entry
  @value_entry
end

Instance Method Details

#handle_left_operand_changedObject



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/alexandria/ui/smart_library_rule_box.rb', line 105

def handle_left_operand_changed
  operand = operands[left_operand_combo.active]
  operator_combo.freeze_notify do
    operator_combo.remove_all
    operations = SmartLibrary::Rule.operations_for_operand(operand)
    operations.each do |operation|
      operator = operation.first
      operator_combo.append_text(operator.name)
    end
    operator_combo.active = 0
  end
end

#handle_operator_changedObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/alexandria/ui/smart_library_rule_box.rb', line 81

def handle_operator_changed
  operand = operands[left_operand_combo.active]
  operations = SmartLibrary::Rule.operations_for_operand(operand)
  operation = operations[operator_combo.active]

  value_entry.visible = date_entry.visible = .visible = false
  right_operand = operation.last
  unless right_operand.nil?
    entry = case right_operand.klass.name
            when "Time"
              date_entry
            else
              value_entry
            end
    entry.visible = true
    unless right_operand.name.nil?
      .text = right_operand.name
      .visible = true
    end
  end

  @parent.apply_smart_rule_for_rule_box(rule_box, operand, operation)
end

#operandsObject



77
78
79
# File 'lib/alexandria/ui/smart_library_rule_box.rb', line 77

def operands
  SmartLibrary::Rule::Operands::LEFT
end