Class: VR::FileTreeView

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

Defined Under Namespace

Classes: IconHash

Instance Attribute Summary collapse

Attributes included from ViewCommon

#column_keys, #vr_cols, #vr_column, #vr_id, #vr_renderer

Instance Method Summary collapse

Methods inherited from TreeView

#add_row

Methods included from ViewCommon

#add_column, #add_renderer_to_col, #col_attr, #column, #delete_selected, #each_cell_method, #each_renderer, #each_row, #flatten_hash, #get_iter, #id, #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)



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

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: GdkPixbuf::Pixbuf, file_name: String}, empty: TrueClass, path: String, sort_on: String)
  self.col_visible(path: false, sort_on: false, empty: false, file: false)
  self.headers_visible = false
  @icons = File.directory?(icon_path) ? IconHash.new(icon_path) : nil
#      parse_signals()  #fix this!  Subclasses may call twice!
  self.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)]
  } 
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.



6
7
8
# File 'lib/treeview/FileTreeView.rb', line 6

def glob
  @glob
end

#rootObject

Returns the value of attribute root.



6
7
8
# File 'lib/treeview/FileTreeView.rb', line 6

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…



10
11
12
# File 'lib/treeview/FileTreeView.rb', line 10

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.



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/treeview/FileTreeView.rb', line 102

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



70
71
72
73
74
75
76
77
# File 'lib/treeview/FileTreeView.rb', line 70

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.



82
83
84
85
86
# File 'lib/treeview/FileTreeView.rb', line 82

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.



133
134
135
# File 'lib/treeview/FileTreeView.rb', line 133

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.



90
91
92
93
94
95
96
97
98
# File 'lib/treeview/FileTreeView.rb', line 90

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.



41
42
43
44
45
46
47
48
# File 'lib/treeview/FileTreeView.rb', line 41

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.



126
127
128
129
# File 'lib/treeview/FileTreeView.rb', line 126

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