Class: PDFWalker::PDFTree

Inherits:
TreeView
  • Object
show all
Includes:
PopupMenu
Defined in:
lib/pdfwalker/treeview.rb

Constant Summary collapse

OBJCOL =
0
TEXTCOL =
1
WEIGHTCOL =
2
STYLECOL =
3
FGCOL =
4
BGCOL =
5
LOADCOL =
6
@@appearance =
Hash.new(Weight: :normal, Style: :normal)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PopupMenu

#popup_menu

Constructor Details

#initialize(parent) ⇒ PDFTree

Returns a new instance of PDFTree.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/pdfwalker/treeview.rb', line 58

def initialize(parent)
    @parent = parent

    reset_appearance

    @treestore = TreeStore.new(Object::Object, String, Pango::Weight, Pango::Style, String, String, Integer)
    super(@treestore)

    signal_connect('cursor-changed') {
        iter = selection.selected
        if iter
            obj = @treestore.get_value(iter, OBJCOL)

            parent.hexview.load(obj)
            parent.objectview.load(obj)
        end
    }

    signal_connect('row-activated') { |_tree, path,_column|
        if selection.selected
            obj = @treestore.get_value(selection.selected, OBJCOL)

            if row_expanded?(path)
                collapse_row(path)
            else
                expand_row(path, false)
            end

            goto(obj) if obj.is_a?(Origami::Reference)
        end
    }

    signal_connect('row-expanded') { |_tree, iter, _path|
        obj = @treestore.get_value(iter, OBJCOL)

        if obj.is_a?(Origami::Stream) and iter.n_children == 1

            # Processing with an XRef or Object Stream
            if obj.is_a?(Origami::ObjectStream)
                obj.each { |embeddedobj|
                    load_object(iter, embeddedobj)
                }

            elsif obj.is_a?(Origami::XRefStream)
                obj.each { |xref|
                    load_xrefstm(iter, xref)
                }
            end
        end

        for i in 0...iter.n_children
            subiter = iter.nth_child(i)
            subobj = @treestore.get_value(subiter, OBJCOL)

            load_sub_objects(subiter, subobj)
        end
    }

    add_events(Gdk::Event::BUTTON_PRESS_MASK)
    signal_connect('button_press_event') { |_widget, event|
        if event.button == 3 && parent.opened
            path = get_path(event.x,event.y).first
            set_cursor(path, nil, false)

            obj = @treestore.get_value(@treestore.get_iter(path), OBJCOL)
            popup_menu(obj, event, path)
        end
    }
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



56
57
58
# File 'lib/pdfwalker/treeview.rb', line 56

def parent
  @parent
end

Instance Method Details

#clearObject



128
129
130
# File 'lib/pdfwalker/treeview.rb', line 128

def clear
    @treestore.clear
end

#goto(obj, follow_references: true) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/pdfwalker/treeview.rb', line 132

def goto(obj, follow_references: true)
    if obj.is_a?(TreePath)
        set_cursor(obj, nil, false)
    else
        if obj.is_a?(Origami::Name) and obj.parent.is_a?(Origami::Dictionary) and obj.parent.has_key?(obj)
            obj = obj.parent[obj]
        elsif obj.is_a?(Origami::Reference) and follow_references
            obj =
                begin
                    obj.solve
                rescue Origami::InvalidReferenceError
                    @parent.error("Object not found : #{obj}")
                    return
                end
        end

        _, path = object_to_tree_pos(obj)
        if path.nil?
            @parent.error("Object not found : #{obj.type}")
            return
        end

        expand_to_path(path) unless row_expanded?(path)
        @parent.explorer_history << cursor.first if cursor.first
        set_cursor(path, nil, false)
    end
end

#highlight(obj, color) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/pdfwalker/treeview.rb', line 160

def highlight(obj, color)
    if obj.is_a?(Origami::Name) and obj.parent.is_a?(Origami::Dictionary) and obj.parent.has_key?(obj)
        obj = obj.parent[obj]
    end

    iter, path = object_to_tree_pos(obj)
    if iter.nil? or path.nil?
        @parent.error("Object not found : #{obj.type}")
        return
    end

    @treestore.set_value(iter, BGCOL, color)
    expand_to_path(path) unless row_expanded?(path)
end

#load(pdf) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/pdfwalker/treeview.rb', line 175

def load(pdf)
    return unless pdf

    self.clear

    begin
        #
        # Create root entry
        #
        root = @treestore.append(nil)
        @treestore.set_value(root, OBJCOL, pdf)

        set_node(root, :Filename, @parent.filename)

        #
        # Create header entry
        #
        header = @treestore.append(root)
        @treestore.set_value(header, OBJCOL, pdf.header)

        set_node(header, :Header,
                 "Header (version #{pdf.header.major_version}.#{pdf.header.minor_version})")

        no = 1
        pdf.revisions.each { |revision|
            load_revision(root, no, revision)
            no = no + 1
        }

        set_model(@treestore)

    ensure
        expand(@treestore.iter_first, 3)
        set_cursor(@treestore.iter_first.path, nil, false)
    end
end

#object_by_path(path) ⇒ Object



212
213
214
215
216
# File 'lib/pdfwalker/treeview.rb', line 212

def object_by_path(path)
    iter = @treestore.get_iter(path)

    @treestore.get_value(iter, OBJCOL)
end