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.



49
50
51
52
# File 'lib/active_window/filtered_active_tree_store.rb', line 49

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

BETTER:

class FilteredFileStore < ActiveTreeStore.filter

 returns new anonymous class which inherits from Gtk::TreeModelFilter 
we can inherit from -- like Struct.new
.filter is a class method the ActiveTreeStore got extended with

end the #add stuff down there we could replace with GTK’s signals

=>


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/active_window/filtered_active_tree_store.rb', line 25

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?



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/active_window/filtered_active_tree_store.rb', line 78

def apply_filter
  @filtering = true
  run_callbacks :before_filter_applied
  @found_count = 0
  # we could traverse the tree for ourself with #first_iter and TreeIter#next! and #parent,
  # setting visid to true/false accordingly => less method calls, better control
  child_model.each do |model,path,iter|
    set_visibility_for(iter)
  end
  refilter
  run_callbacks :after_filter_applied
end

#clear_filterObject



63
64
65
66
67
68
69
70
# File 'lib/active_window/filtered_active_tree_store.rb', line 63

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

#filtered?Boolean Also known as: filtering?

Returns:

  • (Boolean)


91
92
93
# File 'lib/active_window/filtered_active_tree_store.rb', line 91

def filtered?
  @filtering
end

#iter_visible?(iter) ⇒ Boolean

implement this to oyur own needs

Returns:

  • (Boolean)


73
74
75
# File 'lib/active_window/filtered_active_tree_store.rb', line 73

def iter_visible?(iter)
  true
end

#set_visibility_for(iter) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/active_window/filtered_active_tree_store.rb', line 98

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

#visibility_columnObject



117
118
119
# File 'lib/active_window/filtered_active_tree_store.rb', line 117

def visibility_column
  self.class.column_id[:visible]
end