message_proxy_model.py
Go to the documentation of this file.
00001 # Software License Agreement (BSD License)
00002 #
00003 # Copyright (c) 2012, Willow Garage, Inc.
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions
00008 # are met:
00009 #
00010 #  * Redistributions of source code must retain the above copyright
00011 #    notice, this list of conditions and the following disclaimer.
00012 #  * Redistributions in binary form must reproduce the above
00013 #    copyright notice, this list of conditions and the following
00014 #    disclaimer in the documentation and/or other materials provided
00015 #    with the distribution.
00016 #  * Neither the name of Willow Garage, Inc. nor the names of its
00017 #    contributors may be used to endorse or promote products derived
00018 #    from this software without specific prior written permission.
00019 #
00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 # POSSIBILITY OF SUCH DAMAGE.
00032 
00033 from python_qt_binding.QtCore import Qt, qWarning
00034 from python_qt_binding.QtGui import QBrush, QColor, QSortFilterProxyModel
00035 
00036 from .filters.filter_collection import FilterCollection
00037 from .message import Message
00038 
00039 
00040 class MessageProxyModel(QSortFilterProxyModel):
00041     """
00042     Provides sorting and filtering capabilities for the MessageDataModel.
00043     Filtering is based on a collection of exclude and highlight filters.
00044     """
00045 
00046     def __init__(self):
00047         super(MessageProxyModel, self).__init__()
00048         self.setDynamicSortFilter(True)
00049         self.setFilterRole(Qt.UserRole)
00050         self.setSortCaseSensitivity(Qt.CaseInsensitive)
00051         self.setSortRole(Qt.UserRole)
00052 
00053         self._exclude_filters = FilterCollection()
00054         self._highlight_filters = FilterCollection()
00055         self._show_highlighted_only = False
00056 
00057         # caching source model locally for better performance of filterAcceptsRow()
00058         self._source_model = None
00059 
00060     def setSourceModel(self, source_model):
00061         super(MessageProxyModel, self).setSourceModel(source_model)
00062         self._source_model = self.sourceModel()
00063 
00064     # BEGIN Required implementations of QSortFilterProxyModel functions
00065 
00066     def filterAcceptsRow(self, sourcerow, sourceparent):
00067         """
00068         returns: True if the row does not match any exclude filter AND (_show_highlighted_only is False OR it matches any highlight filter), ''bool''
00069         """
00070         msg = self._source_model._messages[sourcerow]
00071         if self._exclude_filters.test_message(msg):
00072             # hide excluded message
00073             return False
00074 
00075         highlighted = True
00076         if self._highlight_filters.count_enabled_filters() > 0:
00077             highlighted = self._highlight_filters.test_message(msg, default=True)
00078         if self._show_highlighted_only and not highlighted:
00079             # hide messages which are not highlighted when only highlightes messages should be visible
00080             return False
00081 
00082         # update message state
00083         msg.highlighted = highlighted
00084 
00085         return True
00086 
00087     def data(self, proxy_index, role=None):
00088         """
00089         Set colors of items based on highlight filters.
00090         """
00091         index = self.mapToSource(proxy_index)
00092         if role == Qt.ForegroundRole:
00093             msg = self._source_model._messages[index.row()]
00094             if not msg.highlighted:
00095                 return QBrush(Qt.gray)
00096         return self._source_model.data(index, role)
00097 
00098     # END Required implementations of QSortFilterProxyModel functions
00099 
00100     def handle_exclude_filters_changed(self):
00101         """
00102         Invalidate filters and trigger refiltering.
00103         """
00104         self.invalidateFilter()
00105 
00106     def handle_highlight_filters_changed(self):
00107         """
00108         Invalidate filters and trigger refiltering.
00109         """
00110         if self._show_highlighted_only:
00111             self.invalidateFilter()
00112         else:
00113             self.invalidateFilter()
00114             self.dataChanged.emit(self.index(0, 0), self.index(self.rowCount() - 1, self.columnCount() - 1))
00115 
00116     def add_exclude_filter(self, newfilter):
00117         self._exclude_filters.append(newfilter)
00118 
00119     def add_highlight_filter(self, newfilter):
00120         self._highlight_filters.append(newfilter)
00121 
00122     def delete_exclude_filter(self, index):
00123         del self._exclude_filters[index]
00124         self.handle_exclude_filters_changed()
00125 
00126     def delete_highlight_filter(self, index):
00127         del self._highlight_filters[index]
00128         self.handle_highlight_filters_changed()
00129 
00130     def set_show_highlighted_only(self, show_highlighted_only):
00131         self._show_highlighted_only = show_highlighted_only
00132         self.invalidateFilter()


rqt_console
Author(s): Aaron Blasdel
autogenerated on Mon Oct 6 2014 07:15:09