Class: ActiveWindow::FilteredActiveTreeStore

Inherits:
Gtk::TreeModelFilter
  • Object
show all
Includes:
TreeStoreExtentions
Defined in:
lib/active_window/filtered_active_tree_store.rb

Overview

This creates a TreeModel which supports filtering. Please give a block that “returns” boolean

filtered_model = ActiveTreeStoreFilter.new model do |filter_string, model, path, iter|
  !iter[23].index(filter_string).nil?
end

Direct Known Subclasses

FilteredFileTreeStore

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TreeStoreExtentions

#add, #apply_to_tree, #get_object, included, #populate, #refresh

Constructor Details

#initialize(child_model) ⇒ FilteredActiveTreeStore

Returns a new instance of FilteredActiveTreeStore.

[View source]

39
40
41
42
# File 'lib/active_window/filtered_active_tree_store.rb', line 39

def initialize(child_model)
  super(child_model)
  setup_filter
end

Instance Attribute Details

#filter_stringObject Also known as: filter

Returns the value of attribute filter_string.


10
11
12
# File 'lib/active_window/filtered_active_tree_store.rb', line 10

def filter_string
  @filter_string
end

#found_countObject (readonly)

Returns the value of attribute found_count.


10
11
12
# File 'lib/active_window/filtered_active_tree_store.rb', line 10

def found_count
  @found_count
end

#unfiltered_storeObject (readonly)

Returns the value of attribute unfiltered_store.


11
12
13
# File 'lib/active_window/filtered_active_tree_store.rb', line 11

def unfiltered_store
  @unfiltered_store
end

Class Method Details

.inherited(child_class) ⇒ Object

[View source]

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/active_window/filtered_active_tree_store.rb', line 15

def self.inherited(child_class)
  unfiltered_class_name = child_class.name.sub(/^Filtered/,'')
  unfiltered_class = unfiltered_class_name.constantize
  child_class.columns = unfiltered_class.columns
  child_class.column_id = unfiltered_class.column_id
  child_class.setup_column_id_constants
  unfiltered_class.class_eval <<-EOCODE
    def add_with_filter_visibility(file, *args)
      iter = add_without_filter_visibility(file, *args)
      filtered_model.set_visibility_for(iter)
      filtered_model.refilter unless initial_add_in_progress?
      iter
    end
    alias_method_chain :add, :filter_visibility
    attr_accessor :filtered_model
  EOCODE
rescue NameError => e
  if e.message =~ /uninitialized constant #{unfiltered_class}/
    raise "there is no class named #{unfiltered_class} to filter from"
  else
    raise
  end
end

Instance Method Details

#apply_filterObject

Iterate over child model and set visible column according to #iter_visible?

[View source]

68
69
70
71
72
73
74
75
76
# File 'lib/active_window/filtered_active_tree_store.rb', line 68

def apply_filter
  run_callbacks :before_filter_applied
  @found_count = 0
  child_model.each do |model,path,iter|
    set_visibility_for(iter)
  end
  refilter
  run_callbacks :after_filter_applied
end

#clear_filterObject

[View source]

54
55
56
57
58
59
60
# File 'lib/active_window/filtered_active_tree_store.rb', line 54

def clear_filter
  run_callbacks :before_clear_filter
  @filter_string = ''
  @found_count = -1
  refilter
  run_callbacks :after_clear_filter
end

#filtered?Boolean Also known as: filtering?

Returns:

  • (Boolean)
[View source]

78
79
80
# File 'lib/active_window/filtered_active_tree_store.rb', line 78

def filtered?
  !filter_string.blank?
end

#iter_visible?(iter) ⇒ Boolean

implement this to oyur own needs

Returns:

  • (Boolean)
[View source]

63
64
65
# File 'lib/active_window/filtered_active_tree_store.rb', line 63

def iter_visible?(iter)
  true
end

#set_visibility_for(iter) ⇒ Object

[View source]

85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/active_window/filtered_active_tree_store.rb', line 85

def set_visibility_for(iter)
  visid = self.class.column_id[:visible]
  unless filtering?
    iter[ visid ] = true
  else
    if iter_visible?(iter) # iter matches - mark it and all parents as visible
      @found_count += 1
      iter[ visid ] = true
      i = iter
      while i = i.parent
        i[ visid ] = true
      end
    else
      iter[ visid ] = false
    end
  end
end