Class: Ruber::FilteredOutputWidget

Inherits:
OutputWidget show all
Defined in:
lib/ruber/filtered_output_widget.rb

Overview

OutputWidget which allows the user to filter the contents, displaying only the items which match a given regexp.

This widget provides a line edit to enter the regexp, which is shown using a ‘Create filter’ action from the context menu. When the user presses the Return key from within the line edit, a filter is created from the current text and applied (if the line edit is empty, any existing filter is removed). If the user doesn’t want to create the filter, the line edit is hidden by pressing ESC while within it.

The filter is implemented using a FilteredOutputWidget::FilterModel, which is a slightly modified Qt::SortFilterProxyModel. Most of the times, however, you don’t need to worry about this, as all the methods provided by this class still use indexes referred to the source model. This means that, most of the times, adding filtering capability to an output widget is as simple as having it inherit from FilteredOutputWidget rather than from OutputWidget.

Besides the ‘Create Filter’, this class adds two other entries to the context menu: ‘Clear Filter’, which removes the existring filter, and a ‘Ignore Filter’ toggle action, which allows to temporarily disable the filter without removing it. Also, a new UI state, called ‘no_filter’ is defined: it’s true if no filter is applied and false otherwise

Slots

  • create_filter_from_editor

  • clear_filter

  • ignore_filter(bool)

  • show_editor

Defined Under Namespace

Classes: FilterModel

Constant Summary

Constants inherited from OutputWidget

OutputWidget::IsTitleRole, OutputWidget::OutputTypeRole

Instance Attribute Summary collapse

Attributes inherited from OutputWidget

#auto_scroll, #ignore_word_wrap_option, #model, #skip_first_file_in_title, #view, #working_dir

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from OutputWidget

#clear_output, #has_title?, #load_settings, #pinned_down?, #set_color_for, #set_output_type, #title=, #with_auto_scrolling

Methods included from GuiStatesHandler

#change_state, included, #register_action_handler, #remove_action_handler_for, #state

Constructor Details

#initialize(parent = nil, opts = {}) ⇒ FilteredOutputWidget

Creates a new FilteredOutputWidget.

The arguments have the same meaning as in OutputWidget.new. The only difference is that opts can also contain a :filter entry. If given, it is the filter model to use (which should be derived from FilteredOutputWidget::FilterModel or provide the same api); if this entry is missing, a new FilteredOutputWidget::FilterModel will be used



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ruber/filtered_output_widget.rb', line 75

def initialize parent = nil, opts = {}
  super parent, opts
  @filter_model = opts[:filter] || FilterModel.new
  @filter_model.parent = self
  @filter_model.dynamic_sort_filter = true
  @filter_model.source_model = model
  view.model = @filter_model
  disconnect @model, SIGNAL('rowsInserted(QModelIndex, int, int)'), self, SLOT('do_auto_scroll(QModelIndex, int, int)')
  connect @filter_model, SIGNAL('rowsInserted(QModelIndex, int, int)'), self, SLOT('do_auto_scroll(QModelIndex, int, int)')
  connect view.selection_model, SIGNAL('selectionChanged(QItemSelection, QItemSelection)'), self, SLOT('selection_changed(QItemSelection, QItemSelection)')
  @editor = KDE::LineEdit.new self
  connect @editor, SIGNAL('returnPressed(QString)'), self, SLOT(:create_filter_from_editor)
  layout.add_widget @editor, 2, 0
  def @editor.keyReleaseEvent e
    super
    hide if e.key == Qt::Key_Escape and e.modifiers == 0
  end
  @editor.hide
  @editor.completion_object = KDE::Completion.new
  @action_list.insert_before 1, nil, 'create_filter', 'ignore_filter', 'clear_filter'
  create_standard_actions
  set_state 'no_filter', true
end

Instance Attribute Details

#filter_modelObject (readonly) Also known as: filter

The model used to filter the contents of the output widget



59
60
61
# File 'lib/ruber/filtered_output_widget.rb', line 59

def filter_model
  @filter_model
end

Class Method Details

.keyReleaseEvent(e) ⇒ Object



88
89
90
91
# File 'lib/ruber/filtered_output_widget.rb', line 88

def @editor.keyReleaseEvent e
  super
  hide if e.key == Qt::Key_Escape and e.modifiers == 0
end

Instance Method Details

#clear_filterObject

Removes the existing filter (if any).

This means that, after calling this method, all items will be accepted



112
113
114
115
# File 'lib/ruber/filtered_output_widget.rb', line 112

def clear_filter
  @filter_model.filter_reg_exp = ''
  set_state 'no_filter', true
end

#scroll_to(idx) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/ruber/filtered_output_widget.rb', line 117

def scroll_to idx
  case idx
  when Numeric
    rc = @filter_model.row_count
    if idx >= rc then idx = rc -1
    elsif idx < 0 and idx.abs < rc then idx = rc + idx
    elsif idx < 0 then idx = 0
    end
    mod_idx = @filter_model.index idx, 0
    @view.scroll_to mod_idx
  when Qt::ModelIndex
    idx = @filter_model.map_from_source idx unless idx.model == @filter_model
    idx = @filter_model.index(@filter_model.row_count - 1, 0) unless idx.valid?
    @view.scroll_to idx
  when nil
    @view.scroll_to @filter_model.index(@filter_model.row_count - 1, 0)
  end
end

#show_editorObject

Shows the line edit where the user can enter the filter regexp and gives it focus



102
103
104
105
# File 'lib/ruber/filtered_output_widget.rb', line 102

def show_editor
  @editor.show
  @editor.set_focus
end