Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[Issue 16] Use FilteredOutputWidget as tool widget

Before this, the tool widget tried to mimic an OutputWidget behaviour
regarding handling the meta modifier and middle button click. However,
this was hard to mantain, since it meant to replicate much of the code.
  • Loading branch information...
commit f65999662fbf07d8d3e7ce23b869dbf69fa7ff33 1 parent c0b82b2
@stcrocco authored
Showing with 91 additions and 88 deletions.
  1. +91 −88 plugins/project_browser/project_browser.rb
View
179 plugins/project_browser/project_browser.rb
@@ -18,6 +18,8 @@
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
=end
+require 'ruber/filtered_output_widget'
+
module Ruber
=begin rdoc
@@ -30,13 +32,17 @@ module Ruber
The view is updated whenever the contents of the project directory change or whenever
the @general/project_files@ project option changes.
+
+Although it uses a {FilteredOutputWidget} for view, it doesn't provide a way to
+filter files basing on filenames and the standard menu entries {FilteredOutputWidget}
+usually have.
=end
module ProjectBrowser
=begin rdoc
The tool widget displaying the project directory
=end
- class ToolWidget < Qt::Widget
+ class ToolWidget < FilteredOutputWidget
=begin rdoc
Filter used by the tree view to hide non-project files. It also allow to turn off
@@ -55,11 +61,70 @@ class FilterModel < KDE::DirSortFilterProxyModel
def initialize parent = nil
super
@project = nil
- @do_filtering = true
+ @ignore = false
self.dynamic_sort_filter = true
end
=begin rdoc
+Disables filtering
+@return [nil]
+=end
+ def ignore_filter
+ @ignore = true
+ nil
+ end
+
+=begin rdoc
+Tells whether to to accept all items or exclude files not belonging to the project.
+
+If this changes from *true* to *false* or vice-versa, the filter is invalidated
+@param [Boolean] val whether or not to accept all files
+@return [Boolean] _val_
+=end
+ def ignore_filter= val
+ old = @ignore
+ @ignore = val.to_bool
+ invalidate if old != @ignore
+ nil
+ end
+
+=begin rdoc
+Whether files not belonging to the project are being kept or filtered out
+@return [Boolean] *true* if all files are being kept and *false* if files not belonging
+ to the project are being filtered out
+=end
+ def filter_ignored?
+ @ignore
+ end
+
+=begin rdoc
+Needed to satisfy {FilteredOutputWidget::FilterModel} API
+
+If does nothing
+@return [nil]
+=end
+ def filter_reg_exp= val
+ end
+
+=begin rdoc
+Needed to satisfy {FilteredOutputWidget::FilterModel} API
+
+If does nothing
+@return [nil]
+=end
+ def exclude
+ end
+
+=begin rdoc
+Needed to satisfy {FilteredOutputWidget::FilterModel} API
+
+If does nothing
+@return [nil]
+=end
+ def exclude= val
+ end
+
+=begin rdoc
Override of @KDE::DirSortFilterProxyModel#filterAcceptsRow@ which rejects all files
not belonging to the project, unless filtering has been turned off.
@@ -71,7 +136,7 @@ def initialize parent = nil
otherwise
=end
def filterAcceptsRow row, parent
- return true if @project.nil? or !@do_filtering
+ return true if @project.nil? or @ignore
it = source_model.item_for_index source_model.index(row,0,parent)
return true if it.dir?
@project.file_in_project? it.local_path
@@ -89,16 +154,6 @@ def project= prj
end
=begin rdoc
-Tells whether to exclude files not belonging to the project or to accept all items
-@param [Boolean] val whether or not to accept all files
-@return [Boolean] _val_
-=end
- def do_filtering= val
- @do_filtering = val
- invalidate_filter
- end
-
-=begin rdoc
Override of @KDE::DirSortFilterProxyModel#filterAcceptsRow@ which works as the
parent method but is public
@return [nil]
@@ -110,43 +165,6 @@ def invalidate_filter
end
=begin rdoc
-The view used by the plugin
-
-The only scope of this class is to provide the context menu
-=end
- class View < Qt::TreeView
-
-=begin rdoc
-Signal emitted whenever the user toggles the "Show only project files" action
-@param [Boolean] *true* if the user checked the action and *false* if he unchecked
-it
-=end
- signals 'only_project_files_triggered(bool)'
-
-=begin rdoc
-@param [Qt::Widget,nil] parent the parent widget
-=end
- def initialize parent = nil
- super
- @menu = Qt::Menu.new self
- @toggle_filter_action = KDE::ToggleAction.new 'Show only project files', @menu
- @toggle_filter_action.checked = true
- @menu.add_action @toggle_filter_action
- connect @toggle_filter_action, SIGNAL('toggled(bool)'), self, SIGNAL('only_project_files_triggered(bool)')
- end
-
-=begin rdoc
-Override of @Qt::AbstractScrollArea#contextMenuEvent@ which displays a menu containing
-the action
-@param [Qt::ContextMenuEvent] e the event object
-=end
- def contextMenuEvent e
- @menu.popup e.global_pos
- end
-
- end
-
-=begin rdoc
Creates a new instance
The view of the new instance displays the contents of the directory of the current
@@ -154,23 +172,23 @@ def contextMenuEvent e
@param [Qt::Widget,nil] parent the parent widget
=end
def initialize parent = nil
- super
+ super parent, :view => :tree, :model => KDE::DirModel.new, :use_default_font => true,
+ :filter => FilterModel.new
+ action_list.clear
+ actions.clear
+ action = KDE::ToggleAction.new KDE.i18n('&Show only project files'), self
+ action.object_name = 'only_project_files'
+ actions['only_project_files'] = action
+ action.connect(SIGNAL('toggled(bool)')){|val| filter_model.ignore_filter = !val}
+ action_list << 'only_project_files'
connect Ruber[:world], SIGNAL('active_project_changed(QObject*)'), self, SLOT('current_project_changed(QObject*)')
- self.layout = Qt::VBoxLayout.new self
- @view = View.new self
- @model = KDE::DirModel.new @view
- @model.dir_lister.open_url KDE::Url.new('/')
- @filter = FilterModel.new @view
- @filter.source_model = @model
- @view.model = @filter
- @view.edit_triggers = Qt::AbstractItemView::NoEditTriggers
- 1.upto(@model.column_count-1){|i| @view.hide_column i}
- @view.header_hidden = true
- layout.add_widget @view
+ action.checked = true
+ model.dir_lister.open_url KDE::Url.new('/')
+ view.edit_triggers = Qt::AbstractItemView::NoEditTriggers
+ 1.upto(model.column_count-1){|i| view.hide_column i}
+ view.header_hidden = true
@project = nil
current_project_changed Ruber[:world].active_project
- @view.connect(SIGNAL('only_project_files_triggered(bool)')){|val| @filter.do_filtering = val}
- connect @view, SIGNAL('activated(QModelIndex)'), self, SLOT('open_file_in_editor(QModelIndex)')
end
private
@@ -190,11 +208,11 @@ def current_project_changed prj
if prj
@project = prj
connect @project, SIGNAL('option_changed(QString, QString)'), self, SLOT('project_option_changed(QString, QString)')
- @model.dir_lister.open_url KDE::Url.new(prj.project_directory)
- @view.enabled = true
- else @view.enabled = false
+ model.dir_lister.open_url KDE::Url.new(prj.project_directory)
+ view.enabled = true
+ else view.enabled = false
end
- @filter.project = prj
+ filter_model.project = prj
nil
end
slots 'current_project_changed(QObject*)'
@@ -209,34 +227,19 @@ def current_project_changed prj
@return [nil]
=end
def project_option_changed group, name
- @filter.invalidate_filter if group == 'general' and name == 'project_files'
+ filter.invalidate_filter if group == 'general' and name == 'project_files'
nil
end
slots 'project_option_changed(QString, QString)'
-=begin rdoc
-Slot called whenever the user activates an item in the view
-
-If the item corresponds to a file, it will be opened in an editor, otherwise nothing
-will be done. The tool widget will be closed unless the Meta key is pressed
-@param [Qt::ModelIndex] idx the activated index (referred to the filter model)
-@return [nil]
-=end
- def open_file_in_editor idx
- #Currently, only the name column is supported. However, in the future,
- #other columns can be supported
+ def find_filename_in_index idx
unless idx.column == KDE::DirModel::Name
- idx = @filter.index(KDE::DirModel::Name, idx.row, idx.parent)
+ idx = filter.index(KDE::DirModel::Name, idx.row, idx.parent)
end
- item = @model.item_for_index @filter.map_to_source(idx)
+ item = @model.item_for_index idx
return if item.dir?
- file = item.local_path
- modifiers = Ruber[:app].keyboard_modifiers
- Ruber[:main_window].display_document file
- Ruber[:main_window].hide_tool self if (Qt::MetaModifier & modifiers) == 0
- nil
+ [item.local_path, 0]
end
- slots 'open_file_in_editor(QModelIndex)'
end
Please sign in to comment.
Something went wrong with that request. Please try again.