Class: Ruber::OpenFileInProjectDlg

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

Overview

This class is a dialog where the user can choose a file to open among the files of the project.

The dialog is made by two widgets: a KDE::LineEdit, where the user can enter a file pattern, and a Qt::ListView, where all files in the project matching the given regexp are shown. The user can choose the file either by pressing the Return key or by activating an item in the list. The pattern is interpreted as a regexp and is checked against the whole path of the file if it contains a pattern separator character (the ‘/’ character on UNIX) and only against the name of the file otherwise. When the user changes the pattern, the file list is changed accordingly. This is achieved using a filter model derived from Qt::SortFilterProxyModel.

Defined Under Namespace

Classes: FilterModel

Instance Method Summary (collapse)

Constructor Details

- (OpenFileInProjectDlg) initialize(prj, parent = nil)

Returns a new OpenFileInProjectDlg. =Arguments parent:: the widget parent of the dialog



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/ruber/main_window/open_file_in_project_dlg.rb', line 113

def initialize prj, parent = nil
  super parent
  files = prj.project_files.to_a
  @base_dir = prj.project_directory
  @ui = Ui::OpenFileInProjectDlg.new
  @ui.setupUi self
  @ui.regexp_error.hide
  filter = FilterModel.new @ui.file_list
  model = Qt::StandardItemModel.new filter
  @ui.file_list.model = filter
  filter.source_model = model
  files.each do |f|
    path = f.sub %r{\A#{Regexp.quote(@base_dir)}/}, ''
    it = Qt::StandardItem.new path
    it.set_data Qt::Variant.new(File.basename(path))
    it.editable = false
    model.append_row it
  end      
  @ui.pattern.install_event_filter self
  connect @ui.pattern, SIGNAL('textChanged(const QString &)'), self, SLOT('change_filter(const QString &)')
  connect @ui.file_list, SIGNAL('activated(const QModelIndex &)'), self, SLOT('item_activated(const QModelIndex &)')
  @ui.file_list.selection_model.select @ui.file_list.model.index(0,0), 
    Qt::ItemSelectionModel::ClearAndSelect|Qt::ItemSelectionModel::Rows
  @ui.file_list.current_index = @ui.file_list.model.index(0,0)
#       @ui.file_list.header.resize_sections Qt::HeaderView::ResizeToContents
end

Instance Method Details

- (Object) change_filter(text) (private)

Changes the pattern used by the filter model applied to the view so that it is equal to the text currently in the pattern widget and selects the first item in the view (if any). If the list is empty, it also disables the Ok button. =Arguments text:: the new pattern



183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/ruber/main_window/open_file_in_project_dlg.rb', line 183

def change_filter text
  begin
    reg = text.empty? ? nil : Regexp.new( text )
    @ui.file_list.model.filter= reg
    @ui.file_list.selection_model.select @ui.file_list.model.index(0,0), 
      Qt::ItemSelectionModel::ClearAndSelect|Qt::ItemSelectionModel::Rows
    @ui.file_list.current_index = @ui.file_list.model.index(0,0)
    @ui.buttons.button(Qt::DialogButtonBox::Ok).enabled = @ui.file_list.model.row_count > 0
    @ui.regexp_error.hide
  rescue RegexpError 
    @ui.regexp_error.show
  end
end

Slot Signature:

change_filter(const QString &)

- (Object) chosen_file

Returns the file chosen by the user or nil if no file has been chosen. The chosen file is the file last selected in the file list.



144
145
146
147
148
149
# File 'lib/ruber/main_window/open_file_in_project_dlg.rb', line 144

def chosen_file
  selection = @ui.file_list.selection_model.selected_indexes
  return nil if selection.empty?
  idx = selection.first
  File.join(@base_dir, idx.data.to_string.gsub(/\A\./,''))
end

- (Object) eventFilter(obj, e)

Reimplements Qt::Object.eventFilter. It blocks the KeyPress events for the up and down keys (but only if there’s no modifier) and redirects them to the file list widget. All other events are allowed to pass. This allows to scroll the list without taking the focus from the pattern widget. =Arguments obj:: the object whose events should be filtered e:: the event



160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/ruber/main_window/open_file_in_project_dlg.rb', line 160

def eventFilter obj, e
  if e.type != Qt::Event::KeyPress then return false
  else
    if (e.key == Qt::Key_Down || e.key == Qt::Key_Up) and e.modifiers == Qt::NoModifier
# TODO: reintroduce the last parameter when it stops giving errors
      new_ev = Qt::KeyEvent.new e.type, e.key, e.modifiers, e.text, 
e.is_auto_repeat, e.count
      Ruber[:app].post_event @ui.file_list, new_ev
      true
    else false
    end
  end
end

- (Object) item_activated(idx) (private)

Closes the dialog with the Qt::Dialog::Accepted status and selects the index passed as argument. =Arguments idx:: the index of the activated item.



203
204
205
206
207
208
# File 'lib/ruber/main_window/open_file_in_project_dlg.rb', line 203

def item_activated idx
  @ui.file_list.selection_model.select idx, Qt::ItemSelectionModel::ClearAndSelect|
      Qt::ItemSelectionModel::Rows
  @ui.file_list.current_index = idx
  accept
end

Slot Signature:

item_activated(const QModelIndex &)