Class: MetaRuby::GUI::ModelBrowser
- Inherits:
-
Qt::Widget
- Object
- Qt::Widget
- MetaRuby::GUI::ModelBrowser
- Defined in:
- lib/metaruby/gui/model_browser.rb
Overview
Widget that allows to browse the currently available models and display information about them
It contains a model selection widget, which lists all available models and allows to select them, and a visualization pane in which the corresponding model visualizations are rendered as HTML
The object display itself is delegated to rendering objects. These objects must respond to:
#enable: enable this renderer. This is called so that the rendering
object listens to relevant Qt signals if it has e.g. the ability to
interact with the user through HTML
#disable: disables this renderer. This is called so that the rendering
object can stop listening to relevant Qt signals if it has e.g. the ability to
interact with the user through HTML
#clear: clear existing data
#render(model): render the given model
Defined Under Namespace
Classes: Page
Instance Attribute Summary collapse
-
#central_splitter ⇒ Qt::Splitter
readonly
The horizontal splitter between the model browser and the model view.
-
#display ⇒ Qt::WebView
readonly
The HTML view widget.
-
#exception_view ⇒ ExceptionView
readonly
View that allows to display errors to the user.
-
#history ⇒ Array<Model,[String]>
readonly
The browsing history, as either direct modules or module name path (suitable to be given to #select_by_path).
-
#history_index ⇒ Integer
readonly
The index of the current link in the history.
-
#main_layout ⇒ Qt::BoxLayout
readonly
The main layout.
-
#manager ⇒ RenderingManager
readonly
The object that manages all the rendering objects available.
-
#model_selector ⇒ ModelSelector
readonly
The widget that lists available models and allows to select them.
-
#page ⇒ Page
The page object that handles compositing the results of different rendering objects, as well as the ability to e.g.
-
#registered_exceptions ⇒ Array<Exception>
readonly
Set of exceptions raised during the last rendering step.
-
#vertical_splitter ⇒ Qt::Splitter
readonly
The toplevel splitter (between model browser and exception view).
Instance Method Summary collapse
-
#add_central_widgets(splitter) ⇒ Object
Sets up the widgets that form the central part of the browser.
-
#back ⇒ Object
Go back in the browsing history.
-
#current_selection ⇒ RubyModuleModel::ModuleInfo?
Returns the currently selected item.
-
#forward ⇒ Object
Go forward in the browsing history.
-
#initialize(main = nil, exception_view: nil) ⇒ ModelBrowser
constructor
A new instance of ModelBrowser.
-
#push_to_history(object) ⇒ Object
Pushes one element in the history.
-
#register_type(type, rendering_class, name, priority = 0) ⇒ Object
Registers a certain kind of model as well as the information needed to display it.
-
#reload ⇒ Object
Update the model list.
-
#render_model(mod, options = Hash.new) ⇒ Object
Call to render the given model.
-
#restore_from_settings(settings) ⇒ Object
Restore the state of this widget from settings previously saved with #save_to_settings.
-
#save_to_settings(settings) ⇒ Object
Save the current state of this widget in the given settings.
-
#select_by_history_element(h) ⇒ Object
Selects a given model based on a value in the history.
-
#select_by_module(model) ⇒ Boolean
Selects the given model if it registered in the model list This emits the model_selected signal.
-
#select_by_path(*path) ⇒ Boolean
Selects the given model if it registered in the model list This emits the model_selected signal.
-
#update_exceptions ⇒ Object
Updates #exception_view from the set of registered exceptions.
-
#update_model_selector ⇒ Object
Update the model selector after #register_type got called.
Constructor Details
#initialize(main = nil, exception_view: nil) ⇒ ModelBrowser
Returns a new instance of ModelBrowser.
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 |
# File 'lib/metaruby/gui/model_browser.rb', line 72 def initialize(main = nil, exception_view: nil) super(main) @available_renderers = Hash.new @registered_exceptions = Array.new @history = Array.new @history_index = -1 @manager = RenderingManager.new @main_layout = Qt::VBoxLayout.new(self) @vertical_splitter = Qt::Splitter.new(Qt::Vertical, self) main_layout.(vertical_splitter) @central_splitter = Qt::Splitter.new(vertical_splitter) @exception_view = (exception_view ||= ExceptionView.new) exception_view.parent = vertical_splitter connect(exception_view, SIGNAL('fileOpenClicked(const QUrl&)'), self, SLOT('fileOpenClicked(const QUrl&)')) (central_splitter) vertical_splitter.(central_splitter) vertical_splitter.(exception_view) setTabOrder(model_selector, display) update_exceptions end |
Instance Attribute Details
#central_splitter ⇒ Qt::Splitter (readonly)
Returns the horizontal splitter between the model browser and the model view.
55 56 57 |
# File 'lib/metaruby/gui/model_browser.rb', line 55 def central_splitter @central_splitter end |
#display ⇒ Qt::WebView (readonly)
Returns the HTML view widget.
31 32 33 |
# File 'lib/metaruby/gui/model_browser.rb', line 31 def display @display end |
#exception_view ⇒ ExceptionView (readonly)
Returns view that allows to display errors to the user.
34 35 36 |
# File 'lib/metaruby/gui/model_browser.rb', line 34 def exception_view @exception_view end |
#history ⇒ Array<Model,[String]> (readonly)
Returns the browsing history, as either direct modules or module name path (suitable to be given to #select_by_path).
44 45 46 |
# File 'lib/metaruby/gui/model_browser.rb', line 44 def history @history end |
#history_index ⇒ Integer (readonly)
Returns the index of the current link in the history.
46 47 48 |
# File 'lib/metaruby/gui/model_browser.rb', line 46 def history_index @history_index end |
#main_layout ⇒ Qt::BoxLayout (readonly)
Returns the main layout.
48 49 50 |
# File 'lib/metaruby/gui/model_browser.rb', line 48 def main_layout @main_layout end |
#manager ⇒ RenderingManager (readonly)
Returns the object that manages all the rendering objects available.
37 38 39 |
# File 'lib/metaruby/gui/model_browser.rb', line 37 def manager @manager end |
#model_selector ⇒ ModelSelector (readonly)
Returns the widget that lists available models and allows to select them.
25 26 27 |
# File 'lib/metaruby/gui/model_browser.rb', line 25 def model_selector @model_selector end |
#page ⇒ Page
Returns the page object that handles compositing the results of different rendering objects, as well as the ability to e.g. handle buttons.
29 30 31 |
# File 'lib/metaruby/gui/model_browser.rb', line 29 def page @page end |
#registered_exceptions ⇒ Array<Exception> (readonly)
Returns set of exceptions raised during the last rendering step.
40 41 42 |
# File 'lib/metaruby/gui/model_browser.rb', line 40 def registered_exceptions @registered_exceptions end |
#vertical_splitter ⇒ Qt::Splitter (readonly)
Returns the toplevel splitter (between model browser and exception view).
52 53 54 |
# File 'lib/metaruby/gui/model_browser.rb', line 52 def vertical_splitter @vertical_splitter end |
Instance Method Details
#add_central_widgets(splitter) ⇒ Object
Sets up the widgets that form the central part of the browser
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/metaruby/gui/model_browser.rb', line 160 def (splitter) @model_selector = ModelSelector.new splitter.(model_selector) # Create a central stacked layout display = @display = Qt::WebView.new browser = self display.singleton_class.class_eval do define_method :contextMenuEvent do |event| = Qt::Menu.new(self) act = page.action(Qt::WebPage::Back) act.enabled = true .add_action act connect(act, SIGNAL(:triggered), browser, SLOT(:back)) act = page.action(Qt::WebPage::Forward) act.enabled = true connect(act, SIGNAL(:triggered), browser, SLOT(:forward)) .add_action act .popup(event.globalPos) event.accept end end splitter.(display) splitter.set_stretch_factor(1, 2) self.page = Page.new(display.page) model_selector.connect(SIGNAL('model_selected(QVariant)')) do |mod| mod = mod.to_ruby push_to_history(mod) render_model(mod) end end |
#back ⇒ Object
Go back in the browsing history
280 281 282 283 284 |
# File 'lib/metaruby/gui/model_browser.rb', line 280 def back return if history_index <= 0 @history_index -= 1 select_by_history_element(history[history_index]) end |
#current_selection ⇒ RubyModuleModel::ModuleInfo?
Returns the currently selected item
257 258 259 |
# File 'lib/metaruby/gui/model_browser.rb', line 257 def current_selection model_selector.current_selection end |
#forward ⇒ Object
Go forward in the browsing history
273 274 275 276 277 |
# File 'lib/metaruby/gui/model_browser.rb', line 273 def forward return if history_index == history.size - 1 @history_index += 1 select_by_history_element(history[history_index]) end |
#push_to_history(object) ⇒ Object
Pushes one element in the history
If the history index is not at the end, the remainder is discarded
264 265 266 267 268 269 270 |
# File 'lib/metaruby/gui/model_browser.rb', line 264 def push_to_history(object) return if object == history[history_index] @history = history[0, history_index + 1] history << object @history_index = history.size - 1 end |
#register_type(type, rendering_class, name, priority = 0) ⇒ Object
Registers a certain kind of model as well as the information needed to display it
It registers the given type on the model browser so that it gets displayed there.
You must call #update_model_selector after this call for the modification to have any effect (i.e. for the newly registered models to appear on the selector)
154 155 156 157 |
# File 'lib/metaruby/gui/model_browser.rb', line 154 def register_type(type, rendering_class, name, priority = 0) model_selector.register_type(type, name, priority) manager.register_type(type, rendering_class) end |
#reload ⇒ Object
Update the model list
297 298 299 |
# File 'lib/metaruby/gui/model_browser.rb', line 297 def reload model_selector.reload end |
#render_model(mod, options = Hash.new) ⇒ Object
Call to render the given model
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/metaruby/gui/model_browser.rb', line 218 def render_model(mod, = Hash.new) page.clear @registered_exceptions.clear reference_model, _ = manager.find_renderer(mod) if mod page.title = "#{mod.name} (#{reference_model.name})" begin manager.render(mod, ) rescue ::Exception => e @registered_exceptions << e end else @registered_exceptions << ArgumentError.new("no view available for #{mod} (#{mod.class})") end update_exceptions end |
#restore_from_settings(settings) ⇒ Object
Restore the state of this widget from settings previously saved with #save_to_settings
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/metaruby/gui/model_browser.rb', line 104 def restore_from_settings(settings) %w{central_splitter vertical_splitter}.each do |object_name| sizes = settings.value(object_name) if !sizes.null? sizes = sizes.to_list.map do |obj| obj.to_int end send(object_name).sizes = sizes end end end |
#save_to_settings(settings) ⇒ Object
Save the current state of this widget in the given settings
119 120 121 122 123 124 125 |
# File 'lib/metaruby/gui/model_browser.rb', line 119 def save_to_settings(settings) %w{central_splitter vertical_splitter}.each do |object_name| sizes = send(object_name).sizes sizes = sizes.map { |o| Qt::Variant.new(o) } settings.set_value(object_name, Qt::Variant.new(sizes)) end end |
#select_by_history_element(h) ⇒ Object
Selects a given model based on a value in the history
289 290 291 292 293 294 |
# File 'lib/metaruby/gui/model_browser.rb', line 289 def select_by_history_element(h) if h.respond_to?(:to_ary) select_by_path(*h) else select_by_module(h) end end |
#select_by_module(model) ⇒ Boolean
Selects the given model if it registered in the model list This emits the model_selected signal
250 251 252 253 254 |
# File 'lib/metaruby/gui/model_browser.rb', line 250 def select_by_module(model) if model_selector.select_by_module(model) push_to_history(model) end end |
#select_by_path(*path) ⇒ Boolean
Selects the given model if it registered in the model list This emits the model_selected signal
243 244 245 246 247 |
# File 'lib/metaruby/gui/model_browser.rb', line 243 def select_by_path(*path) if model_selector.select_by_path(*path) push_to_history(path) end end |
#update_exceptions ⇒ Object
Updates #exception_view from the set of registered exceptions
236 237 238 239 |
# File 'lib/metaruby/gui/model_browser.rb', line 236 def update_exceptions exception_view.exceptions = registered_exceptions + manager.registered_exceptions end |
#update_model_selector ⇒ Object
Update the model selector after #register_type got called
128 129 130 |
# File 'lib/metaruby/gui/model_browser.rb', line 128 def update_model_selector model_selector.update end |