Class: VR::FileTreeView

Inherits:
TreeView
  • Object
show all
Includes:
GladeGUI
Defined in:
lib/treeview/FileTreeView.rb

Defined Under Namespace

Classes: IconHash

Instance Attribute Summary collapse

Attributes included from GladeGUI

#builder

Attributes included from ViewCommon

#vr_renderer

Instance Method Summary collapse

Methods included from GladeGUI

#buttonCancel__clicked, #extract_key, #get_glade_active_record, #get_glade_all, #get_glade_variables, #load_glade, #parse_signals, #set_drag_drop, #set_glade_active_record, #set_glade_all, #set_glade_hash, #set_glade_variables, #show_glade, #try_to_select_text_in_combobox, #window1__destroy, #window1__key_press_event

Methods inherited from TreeView

#add_row

Methods included from ViewCommon

#col_attr, #column, #delete_selected, #each_renderer, #each_row, #flatten_hash, #get_iter, #id, #load_columns, #method_missing, #ren_attr, #renderer, #selected_rows, #turn_on_comboboxes, #vr_row

Constructor Details

#initialize(root = Dir.pwd, icon_path = nil, glob = "*", validate_block = nil) ⇒ FileTreeView

FileTreeView creates a TreeView of files with folders and icons.

Often you should subclass this class for a particular use.

Parameters:

  • root (String) (defaults to: Dir.pwd)

    Root folder of the tree

  • icon_path (String) (defaults to: nil)

    Path to a folder where icons are stored. See VR::IconHash

  • glob (String) (defaults to: "*")

    Glob designating the files to be included. Google: “ruby glob” for more.

  • validate_block (Proc) (defaults to: nil)

    Block that limits files and folders to include. When block returns true file is includud. (false = excluded)



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/treeview/FileTreeView.rb', line 20

def initialize(root = Dir.pwd, icon_path = nil, glob = "*", validate_block = nil)
  @root = File.expand_path(root)
  @glob = glob
  @validate_block = validate_block
  super(:file => {:pix => Gdk::Pixbuf, :file_name => String}, :empty => TrueClass, 
        :path => String, :sort_on => String)
  col_visible( :path => false, :sort_on => false, :empty => false)
  self.headers_visible = false
  @icons = File.directory?(icon_path) ? IconHash.new(icon_path) : nil
#      parse_signals()  #fix this!  Subclasses may call twice!
  model.set_sort_column_id(id(:sort_on), :ascending )
  self.set_enable_search(false)
  refresh
  self.signal_connect("row_expanded") { |view, iter, path| 
    iter = model.get_iter(path) # bug fix
    fill_folder(iter) if iter[id(:empty)]
  } 
  self.visible = true # necessary!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class VR::ViewCommon

Instance Attribute Details

#globObject

Returns the value of attribute glob



8
9
10
# File 'lib/treeview/FileTreeView.rb', line 8

def glob
  @glob
end

#rootObject

Returns the value of attribute root



8
9
10
# File 'lib/treeview/FileTreeView.rb', line 8

def root
  @root
end

#validate_blockObject

A Code block that further excludes files and folders included in the tree. If this block returns false for the entry, it will be excluded. If it returns true, it will be included…



12
13
14
# File 'lib/treeview/FileTreeView.rb', line 12

def validate_block
  @validate_block
end

Instance Method Details

#add_file(filename, parent = @root_iter) ⇒ Object

Adds a file to the tree under a given parent iter.

Parameters:

  • filename (String)

    Full path to file to add.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/treeview/FileTreeView.rb', line 106

def add_file(filename, parent = @root_iter)
  my_path = File.dirname(filename)
  model.each do |model, path, iter|
    return if iter[id(:path)] == filename # duplicate
    parent = iter if iter[id(:path)] == my_path
  end
  fn = filename.gsub("\\", "/")
  parent[id(:empty)] = false unless parent.nil?
  child = add_row(parent)
  child[:pix] = @icons.get_icon(File.directory?(fn) ? "x.folder" : fn)   if @icons
  child[:file_name] = File.basename(fn)
  child[:path] = fn
  if File.directory?(fn)
    child[:sort_on] = "0" + child[:file_name]
    child[:empty] = true
    add_row(child) # dummy record so expander appears  
  else
    child[id(:sort_on)] = "1" + child[id(:file_name)]      
  end
  return child
end

#expand_or_collapse_folderObject

Expands or collapses the currently selected row



74
75
76
77
78
79
80
81
# File 'lib/treeview/FileTreeView.rb', line 74

def expand_or_collapse_folder()
  return unless row = selected_rows.first
  if row_expanded?(row.path)
    collapse_row(row.path)
  else
    expand_row(row.path, false)
  end
end

#get_open_foldersArray

returns an array of open folders. The array of folders can be saved, and then you can pass the array to #open_folders to restore the state of the file tree.

Returns:

  • (Array)

    Returns array of strings with the full expanded paths of the open folders.



86
87
88
89
90
# File 'lib/treeview/FileTreeView.rb', line 86

def get_open_folders()
  expanded = []
  map_expanded_rows {|view, path| expanded << model.get_iter(path)[id(:path)] }
  return expanded
end

#get_selected_pathString?

Returns the full filename with path of the selected file.

Returns:

  • (String, nil)

    Full file path to selected file.



137
138
139
# File 'lib/treeview/FileTreeView.rb', line 137

def get_selected_path() 
  selection.selected ? selection.selected[id(:path)] : nil 
end

#open_folders(folder_paths) ⇒ Object

Opens a list of folders.

Parameters:

  • folder_paths (Array)

    Array of Strings of folder names to expand, possibly from the #get_open_folders method.



94
95
96
97
98
99
100
101
102
# File 'lib/treeview/FileTreeView.rb', line 94

def open_folders(folder_paths)
  model.each do |model, path, iter| 
    if folder_paths.include?(iter[id(:path)])
      fill_folder(iter)
#          expand_row(path, false)
#          self__row_expanded(self, iter, path)    
    end
  end
end

#refresh(flags = {}) ⇒ Object

Refresh the file tree, optionally with a new root folder, and optionally opening an array of folders.

Parameters:

  • flags (Hash) (defaults to: {})

    You can change the root and/or open an array of folders.

Options Hash (flags):

  • :root (String)

    Path to root folder to open

  • :open_folders (Array)

    A list of folders to open, possibly from #get_open_folders. Default: the currently open folders, so the tree doesn't collapse when refreshed.



45
46
47
48
49
50
51
52
# File 'lib/treeview/FileTreeView.rb', line 45

def refresh(flags={}) 
  @root = flags[:root] if flags[:root]
  open_folders = flags[:open_folders] ? flags[:open_folders] : get_open_folders()
  model.clear
  @root_iter = add_file(@root, nil)
  open_folders([@root_iter[:path]])
  open_folders(open_folders)
end

#set_show_expanders(expand = true) ⇒ Object

Sets whether or not the little arrow expanders appear net to the folders.

Parameters:

  • expand (Boolean) (defaults to: true)

    Whether or not to show the expanders next to the folders.



130
131
132
133
# File 'lib/treeview/FileTreeView.rb', line 130

def set_show_expanders(expand = true)
  self.show_expanders = expand
  self.level_indentation  = expand ? 0 : 12
end