Class: Ruber::State::DocumentExtension

Inherits:
Qt::Object
  • Object
show all
Includes:
Extension
Defined in:
plugins/state/state.rb

Overview

Extension for documents needed by the State plugin

The scope of this extension is to move the cursor of all newly created views associated with the document to the position it was in the last used view. The cursor position for the first view is read from the document’s own project, where it is saved whenever the document is closed.

The cursor position for a view is moved in response to the Document#view_created signal.

Instance Attribute Summary

Attributes included from Extension

plugin

Instance Method Summary (collapse)

Methods included from Extension

#query_close, #remove_from_project, #shutdown

Constructor Details

- (DocumentExtension) initialize(prj)

A new instance of DocumentExtension

Parameters:



374
375
376
377
378
379
380
381
# File 'plugins/state/state.rb', line 374

def initialize prj
  super
  @last_view = nil
  @project = prj
  @document = prj.document
  connect @document, SIGNAL('view_created(QObject*, QObject*)'), self, SLOT('auto_restore(QObject*)')
  connect @document, SIGNAL('closing_view(QWidget*, QObject*)'), self, SLOT('view_closing(QWidget*)')
end

Instance Method Details

- (nil) auto_restore(view) (private)

Restores the cursor position for a view if the user choosed to do so

It does nothing if the user choosed not to restore the cursor position when a view is created

Parameters:

  • the (EditorView)

    view to restore the cursor position for

Returns:

  • (nil)


426
427
428
429
430
# File 'plugins/state/state.rb', line 426

def auto_restore view
  restore view if Ruber[:config][:state, :restore_cursor_position] #.restore_cursor_position?
  connect view, SIGNAL('focus_in(QWidget*)'), self, SLOT('view_received_focus(QWidget*)')
  nil
end

Slot Signature:

auto_restore(QObject*)

- (nil) restore(view)

Moves the cursor of a view to the position it was in the last used view

If there are no other views associated with the document, the position of the cursor is read from the document’s own project

Parameters:

  • the (EditorView)

    view to move the cursor for

Returns:

  • (nil)


391
392
393
394
395
396
397
398
# File 'plugins/state/state.rb', line 391

def restore view
  if @last_view then view.cursor_position = @last_view.cursor_position
  else
    pos = @document.own_project[:state, :cursor_position]
    view.go_to *pos
  end
  nil
end

- (nil) save_settings

Saves the position of the cursor in the document’s own project

It does nothing if the document isn’t associated with a view

Returns:

  • (nil)


406
407
408
409
410
411
412
413
# File 'plugins/state/state.rb', line 406

def save_settings
  if @last_view
    cur = @last_view.cursor_position
    pos = [cur.line, cur.column]
    @project[:state, :cursor_position] = pos
  end        
  nil
end

- (nil) view_closing(view) (private)

Method called whenever a view associated with the document is closed

If the closed view is the one which last got focus, its cursor position is saved in the document’s own project. Otherwise nothing is done.

Parameters:

Returns:

  • (nil)


454
455
456
457
458
459
# File 'plugins/state/state.rb', line 454

def view_closing view
  if view == @last_view
    save_settings
    @last_view = nil
  end
end

Slot Signature:

view_closing(QWidget*)

- (nil) view_received_focus(view) (private)

Memorizes which view has last received focus

This information is used to decide which view to ask for the cursor position when a new view is created or the cursor position needs to be saved to the project

Parameters:

  • view (EditorView)

    the view which has received focus

Returns:

  • (nil)


440
441
442
443
# File 'plugins/state/state.rb', line 440

def view_received_focus view
  @last_view = view
  nil
end

Slot Signature:

view_received_focus(QWidget*)