Class: JsonView::ApplicationWindow
- Inherits:
-
Gtk::ApplicationWindow
- Object
- Gtk::ApplicationWindow
- JsonView::ApplicationWindow
- Defined in:
- lib/jsonview/application_window.rb
Class Method Summary collapse
Instance Method Summary collapse
- #create_model(data, parent = nil, level = 0) ⇒ Object
-
#initialize(application, argv) ⇒ ApplicationWindow
constructor
A new instance of ApplicationWindow.
- #on_open_clicked ⇒ Object
- #open_json(filename) ⇒ Object
Constructor Details
#initialize(application, argv) ⇒ ApplicationWindow
Returns a new instance of ApplicationWindow.
20 21 22 23 24 25 26 27 |
# File 'lib/jsonview/application_window.rb', line 20 def initialize(application, argv) super application: application set_title 'JsonView' set_icon GdkPixbuf::Pixbuf.new( resource: '/com/github/kojix2/jsonview/ruby.png' ) argv[0] && open_json(argv[0]) end |
Class Method Details
.init ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'lib/jsonview/application_window.rb', line 10 def self.init set_template resource: '/com/github/kojix2/jsonview/jsonview.ui' bind_template_child 'treeview' set_connect_func do |handler_name| lambda do JsonView.application.active_window.__send__(handler_name) end end end |
Instance Method Details
#create_model(data, parent = nil, level = 0) ⇒ Object
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 127 |
# File 'lib/jsonview/application_window.rb', line 75 def create_model(data, parent = nil, level = 0) parent ||= @treemodel.append(nil) case data when Hash data.each do |key, value| i = @treemodel.append(parent) i[0] = key.to_s i[1] = 1.0 # drate ** level create_model(value, i, level + 1) end when Array data.each do |value| i = @treemodel.append(parent) i[0] = '' i[1] = 1.0 # drate ** level create_model(value, i, level + 1) end when String level += 1 @treemodel.append(parent).tap do |j| j[0] = data j[1] = 1.0 # drate ** level end when Numeric, Integer level += 1 @treemodel.append(parent).tap do |j| j[0] = data.to_s j[1] = 1.0 # drate ** level j[2] = 'yellow' end when true level += 1 @treemodel.append(parent).tap do |j| j[0] = data.to_s j[1] = 1.0 # drate ** level j[2] = 'magenta' end when false level += 1 @treemodel.append(parent).tap do |j| j[0] = data.to_s j[1] = 1.0 # drate ** level j[2] = 'cyan' end else level += 1 @treemodel.append(parent).tap do |j| j[0] = data.to_s j[1] = 1.0 # drate ** level j[2] = 'lime' end end end |
#on_open_clicked ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/jsonview/application_window.rb', line 29 def on_open_clicked dialog = Gtk::FileChooserDialog.new( title: 'Gtk::FileChooser sample', action: :open, flags: :modal, buttons: [[Gtk::Stock::OPEN, :accept], [Gtk::Stock::CANCEL, :cancel]] ) Gtk::FileFilter.new.tap do |f| f.name = 'Json' f.add_pattern('*.json') f.add_pattern('*.JSON') dialog.add_filter f end Gtk::FileFilter.new.tap do |f| f.name = 'All' f.add_pattern('*') dialog.add_filter f end case dialog.run when Gtk::ResponseType::ACCEPT open_json(dialog.filename) dialog.destroy when Gtk::ResponseType::CANCEL dialog.destroy end end |
#open_json(filename) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/jsonview/application_window.rb', line 58 def open_json(filename) data = File.open(filename) do |file| JSON.load(file) end @treemodel&.destroy @treemodel = Gtk::TreeStore.new(String, Float, String) create_model(data) treeview.model = @treemodel column = Gtk::TreeViewColumn.new( File.basename(filename), Gtk::CellRendererText.new, { text: 0, scale: 1, foreground: 2 } ) treeview.append_column(column) end |