Class: Ruber::OpenFileInProjectDlg
- 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
-
#chosen_file ⇒ Object
Returns the file chosen by the user or
nilif no file has been chosen. -
#eventFilter(obj, e) ⇒ Object
Reimplements Qt::Object.eventFilter.
-
#initialize(prj, parent = nil) ⇒ OpenFileInProjectDlg
constructor
Returns a new
OpenFileInProjectDlg.
Constructor Details
#initialize(prj, parent = nil) ⇒ OpenFileInProjectDlg
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
#chosen_file ⇒ Object
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 |
#eventFilter(obj, e) ⇒ Object
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 |