Skip to content

Commit

Permalink
[Issue 16] Use FilteredOutputWidget as tool widget
Browse files Browse the repository at this point in the history
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
stcrocco committed Nov 27, 2011
1 parent c0b82b2 commit f659996
Showing 1 changed file with 91 additions and 88 deletions.
179 changes: 91 additions & 88 deletions plugins/project_browser/project_browser.rb
Expand Up @@ -18,6 +18,8 @@
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
=end =end


require 'ruber/filtered_output_widget'

module Ruber module Ruber


=begin rdoc =begin rdoc
Expand All @@ -30,13 +32,17 @@ module Ruber
The view is updated whenever the contents of the project directory change or whenever The view is updated whenever the contents of the project directory change or whenever
the @general/project_files@ project option changes. 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 =end
module ProjectBrowser module ProjectBrowser


=begin rdoc =begin rdoc
The tool widget displaying the project directory The tool widget displaying the project directory
=end =end
class ToolWidget < Qt::Widget class ToolWidget < FilteredOutputWidget


=begin rdoc =begin rdoc
Filter used by the tree view to hide non-project files. It also allow to turn off Filter used by the tree view to hide non-project files. It also allow to turn off
Expand All @@ -55,10 +61,69 @@ class FilterModel < KDE::DirSortFilterProxyModel
def initialize parent = nil def initialize parent = nil
super super
@project = nil @project = nil
@do_filtering = true @ignore = false
self.dynamic_sort_filter = true self.dynamic_sort_filter = true
end 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 =begin rdoc
Override of @KDE::DirSortFilterProxyModel#filterAcceptsRow@ which rejects all files Override of @KDE::DirSortFilterProxyModel#filterAcceptsRow@ which rejects all files
not belonging to the project, unless filtering has been turned off. not belonging to the project, unless filtering has been turned off.
Expand All @@ -71,7 +136,7 @@ def initialize parent = nil
otherwise otherwise
=end =end
def filterAcceptsRow row, parent 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) it = source_model.item_for_index source_model.index(row,0,parent)
return true if it.dir? return true if it.dir?
@project.file_in_project? it.local_path @project.file_in_project? it.local_path
Expand All @@ -88,16 +153,6 @@ def project= prj
invalidate_filter invalidate_filter
end 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 =begin rdoc
Override of @KDE::DirSortFilterProxyModel#filterAcceptsRow@ which works as the Override of @KDE::DirSortFilterProxyModel#filterAcceptsRow@ which works as the
parent method but is public parent method but is public
Expand All @@ -109,43 +164,6 @@ def invalidate_filter


end 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 =begin rdoc
Creates a new instance Creates a new instance
Expand All @@ -154,23 +172,23 @@ def contextMenuEvent e
@param [Qt::Widget,nil] parent the parent widget @param [Qt::Widget,nil] parent the parent widget
=end =end
def initialize parent = nil 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*)') connect Ruber[:world], SIGNAL('active_project_changed(QObject*)'), self, SLOT('current_project_changed(QObject*)')
self.layout = Qt::VBoxLayout.new self action.checked = true
@view = View.new self model.dir_lister.open_url KDE::Url.new('/')
@model = KDE::DirModel.new @view view.edit_triggers = Qt::AbstractItemView::NoEditTriggers
@model.dir_lister.open_url KDE::Url.new('/') 1.upto(model.column_count-1){|i| view.hide_column i}
@filter = FilterModel.new @view view.header_hidden = true
@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
@project = nil @project = nil
current_project_changed Ruber[:world].active_project 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 end


private private
Expand All @@ -190,11 +208,11 @@ def current_project_changed prj
if prj if prj
@project = prj @project = prj
connect @project, SIGNAL('option_changed(QString, QString)'), self, SLOT('project_option_changed(QString, QString)') 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) model.dir_lister.open_url KDE::Url.new(prj.project_directory)
@view.enabled = true view.enabled = true
else @view.enabled = false else view.enabled = false
end end
@filter.project = prj filter_model.project = prj
nil nil
end end
slots 'current_project_changed(QObject*)' slots 'current_project_changed(QObject*)'
Expand All @@ -209,34 +227,19 @@ def current_project_changed prj
@return [nil] @return [nil]
=end =end
def project_option_changed group, name 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 nil
end end
slots 'project_option_changed(QString, QString)' slots 'project_option_changed(QString, QString)'


=begin rdoc def find_filename_in_index idx
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
unless idx.column == KDE::DirModel::Name 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 end
item = @model.item_for_index @filter.map_to_source(idx) item = @model.item_for_index idx
return if item.dir? return if item.dir?
file = item.local_path [item.local_path, 0]
modifiers = Ruber[:app].keyboard_modifiers
Ruber[:main_window].display_document file
Ruber[:main_window].hide_tool self if (Qt::MetaModifier & modifiers) == 0
nil
end end
slots 'open_file_in_editor(QModelIndex)'


end end


Expand Down

0 comments on commit f659996

Please sign in to comment.