Class: Ruber::MainWindow::ViewManager

Inherits:
Qt::Object
  • Object
show all
Defined in:
lib/ruber/main_window/view_manager.rb

Overview

Note:

this class is only for internal use by Ruber::MainWindow. Its API is likely to change and the whole class may disappear in the future. So, *do not* use it outside of Ruber::MainWindow

Class which manages the views in the tab widget

It takes care of activating a view when it gets focus, removing a tab when the last view in it is closed, and so on.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tabs, parent) ⇒ ViewManager

Returns a new instance of ViewManager.

Parameters:



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ruber/main_window/view_manager.rb', line 81

def initialize tabs, parent
  super parent
  @tabs = tabs
  @focus_editors = []
  @part_manager = KParts::PartManager.new parent, self
  @activation_order = []
  @active_editor = nil
  @solver = MainWindow::HintSolver.new @tabs, @part_manager, @activation_order
  @auto_activate_editors = true
  connect @tabs, SIGNAL('currentChanged(int)'), self, SLOT('current_tab_changed(int)')
end

Instance Attribute Details

#activation_orderArray<EditorView> (readonly)

Returns a list of all the editors in all tabs, in activation order, from more recently activated to less recently activated.

Returns:

  • (Array<EditorView>)

    a list of all the editors in all tabs, in activation order, from more recently activated to less recently activated



55
56
57
# File 'lib/ruber/main_window/view_manager.rb', line 55

def activation_order
  @activation_order
end

#active_editorEditorView? (readonly)

Returns the active editor (that is, the editor whose GUI is merged with the main window GUI) or nil if no main window exists.

Returns:

  • (EditorView, nil)

    the active editor (that is, the editor whose GUI is merged with the main window GUI) or nil if no main window exists



49
50
51
# File 'lib/ruber/main_window/view_manager.rb', line 49

def active_editor
  @active_editor
end

#auto_activate_editorsBoolean

Returns whether to automatically activate an editor when it is created.

Returns:

  • (Boolean)

    whether to automatically activate an editor when it is created



60
61
62
# File 'lib/ruber/main_window/view_manager.rb', line 60

def auto_activate_editors
  @auto_activate_editors
end

#part_managerKDE::PartManager (readonly)

Returns the part manager which manages the document parts.

Returns:

  • (KDE::PartManager)

    the part manager which manages the document parts



43
44
45
# File 'lib/ruber/main_window/view_manager.rb', line 43

def part_manager
  @part_manager
end

Instance Method Details

#deactivate_editor(view) ⇒ nil

Deactivates the given editor

To deactivate the editor, its GUI is removed from the main window’s and the corresponding document is deactivated.

If the given editor wasn’t the active one, nothing is done

Parameters:

Returns:

  • (nil)


173
174
175
176
177
178
179
# File 'lib/ruber/main_window/view_manager.rb', line 173

def deactivate_editor view
  return unless @active_editor and view == @active_editor
  @active_editor.document.deactivate
  parent.gui_factory.remove_client view.send(:internal)
  @active_editor = nil
  nil
end

#editor_for(doc, hints) ⇒ EditorView

Returns an editor associated with the given document, creating it if needed

It works as Ruber::MainWindow#editor_for!.

hints may contain the same values as in Ruber::MainWindow#editor_for!. It may also contain another value, @:close_starting_document@

Parameters:

  • doc (Document)

    the document to retrieve an editor for

  • hints (Hash)

    options to tailor the algorithm used to retrieve or create the editor

Options Hash (hints):

  • :close_starting_document (Boolean) — default: false

    if given, specifies that the default document created by ruber at startup exists and it’s in the first tab. If a new view is created in a new tab, this document is closed

Returns:

  • (EditorView)

    the view to use for the document



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/ruber/main_window/view_manager.rb', line 107

def editor_for doc, hints
  editor = @solver.find_editor doc, hints
  if !editor and hints[:create_if_needed]
    editor = create_editor doc
    if hints[:show]
      @activation_order << editor
      view = @solver.place_editor hints
      if view 
        dir = Qt.const_get hints[:split].to_s.capitalize
#               tab(view).split view, editor, dir
        view.parent.split view, editor, dir
      else 
        if hints[:close_starting_document]
          if @tabs.count == 1 and @tabs.widget(0).single_view?
            doc_to_close = @tabs.widget(0).view.document
          end
        end
        add_tab editor, doc.icon, doc.document_name
        doc_to_close.close if doc_to_close
      end
      editor.parent.label = label_for_editor editor
    end
  end
  editor
end

#focus_editor?(editor) ⇒ Boolean #focus_editor?(editor, tab) ⇒ Boolean

Whether the given view is a focus editor

Overloads:

  • #focus_editor?(editor) ⇒ Boolean

    Whether the given view is the focus editor for any tab

    Parameters:

    Returns:

    • (Boolean)

      true if there’s a tab which has editor as focus editor and false otherwise

  • #focus_editor?(editor, tab) ⇒ Boolean
    Note:

    this method doesn’t test whether tab actually is a toplevel pane

    Whether the given view is the focus editor for the given tab

    Parameters:

    • editor (EditorView)

      the view to test

    • tab (Pane, Integer)

      the tab the view must be focus editor for. If a Pane it must be the toplevel pane of the tab; if an @Integer@, it must be the index of the pane to test

    Returns:

    • (Boolean)

      true if editor is the focus widget for tab and false otherwise



249
250
251
252
253
254
255
# File 'lib/ruber/main_window/view_manager.rb', line 249

def focus_editor? editor, tab = nil
  if tab 
    tab = @tabs.index_of(tab) unless tab.is_a? Integer
    @focus_editors[tab] == editor
  else @focus_editors.any?{|k, v| v == editor}
  end
end

#label_for_editor(ed) ⇒ String

The label to use for an editor

Parameters:

  • ed (EditorView)

    the editor to return the label for

Returns:

  • (String)

    the text to show below the given editor



263
264
265
266
267
268
# File 'lib/ruber/main_window/view_manager.rb', line 263

def label_for_editor ed
  url = ed.document.url
  if url.valid? then url.local_file? ? url.path : url.pretty_url
  else ed.document.document_name
  end
end

#make_editor_active(editor) ⇒ EditorView

Makes the given editor active

Besides merging the editor’s GUI with the main window’s, this method also deactivates the previously active editor, updates the activation order, activates the document associated with the new active editor, updates the tab where the new editor is and emits the #active_editor_changed signal.

Nothing is done if the given editor was already active

Parameters:

Returns:



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/ruber/main_window/view_manager.rb', line 145

def make_editor_active editor
  return if editor and @active_editor == editor
  deactivate_editor @active_editor
  @active_editor = editor
  if editor
    parent.gui_factory.add_client editor.send(:internal)
    @activation_order.delete editor
    @activation_order.insert 0, editor
    editor_tab = tab(editor)
    tab_idx = @tabs.index_of editor_tab 
    @focus_editors[tab_idx] = editor
    update_tab editor_tab
    editor.document.activate
  end
  emit active_editor_changed @active_editor
  @active_editor
end

#tab(idx) ⇒ Pane #tab(editor) ⇒ Pane? #tab(pane) ⇒ Pane?

The toplevel pane corresponding to the given index or widget

Overloads:

  • #tab(idx) ⇒ Pane

    Returns the toplevel pane in the tab number idx.

    Parameters:

    • idx (Integer)

      the index of the tab

    Returns:

    • (Pane)

      the toplevel pane in the tab number idx

  • #tab(editor) ⇒ Pane?

    Returns the toplevel pane containing the given editor or nil if the view isn’t contained in a pane.

    Parameters:

    • editor (EditorView)

      the editor to retrieve the pane for

    Returns:

    • (Pane, nil)

      the toplevel pane containing the given editor or nil if the view isn’t contained in a pane

  • #tab(pane) ⇒ Pane?

    Returns the toplevel pane containing the given pane or nil if the pane isn’t contained in another pane.

    Parameters:

    • pane (Pane)

      the pane to retrieve the toplevel pane for

    Returns:

    • (Pane, nil)

      the toplevel pane containing the given pane or nil if the pane isn’t contained in another pane



221
222
223
224
225
226
227
228
229
# File 'lib/ruber/main_window/view_manager.rb', line 221

def tab arg
  if arg.is_a? Integer then @tabs.widget arg
  else
    pane = arg.is_a?(Pane) ? arg : arg.parent
    return unless pane
    pane = pane.parent_pane while pane.parent_pane
    pane
  end
end

#without_activating { ... } ⇒ Object

Executes a block without automatically activating editors

Usually, whenever a tab becomes active, one of the editors it contains becomes active. Sometimes, you don’t want this to happen (for example, when opening multiple documents in sequence). To do so, do what you need from a block passed to this method.

After the block has been executed, the last activated method in the current tab becomes active.

Yields:

  • the block to call without automatically activating editors

Returns:

  • (Object)

    the value returned by the block



194
195
196
197
198
199
200
201
202
203
204
# File 'lib/ruber/main_window/view_manager.rb', line 194

def without_activating
  begin
    @auto_activate_editors = false
    yield
  ensure
    @auto_activate_editors = true
    if @tabs.current_index < 0 then make_editor_active nil
    else make_editor_active @focus_editors[@tabs.current_index]
    end
  end
end