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, qVersion, qWarning
00034 try:
00035     from python_qt_binding.QtCore import QSortFilterProxyModel  # Qt 5
00036 except ImportError:
00037     from python_qt_binding.QtGui import QSortFilterProxyModel  # Qt 4
00038 from python_qt_binding.QtGui import QBrush, QColor
00039 
00040 from .filters.filter_collection import FilterCollection
00041 from .message import Message
00042 
00043 
00044 class MessageProxyModel(QSortFilterProxyModel):
00045     """
00046     Provides sorting and filtering capabilities for the MessageDataModel.
00047     Filtering is based on a collection of exclude and highlight filters.
00048     """
00049 
00050     def __init__(self):
00051         super(MessageProxyModel, self).__init__()
00052         self.setDynamicSortFilter(True)
00053         self.setFilterRole(Qt.UserRole)
00054         self.setSortCaseSensitivity(Qt.CaseInsensitive)
00055         self.setSortRole(Qt.UserRole)
00056 
00057         self._exclude_filters = FilterCollection()
00058         self._highlight_filters = FilterCollection()
00059         self._show_highlighted_only = False
00060 
00061         # caching source model locally for better performance of filterAcceptsRow()
00062         self._source_model = None
00063 
00064     def setSourceModel(self, source_model):
00065         super(MessageProxyModel, self).setSourceModel(source_model)
00066         self._source_model = self.sourceModel()
00067 
00068     # BEGIN Required implementations of QSortFilterProxyModel functions
00069 
00070     def filterAcceptsRow(self, sourcerow, sourceparent):
00071         """
00072         returns: True if the row does not match any exclude filter AND (_show_highlighted_only is False OR it matches any highlight filter), ''bool''
00073         """
00074         msg = self._source_model._messages[sourcerow]
00075         if self._exclude_filters.test_message(msg):
00076             # hide excluded message
00077             return False
00078 
00079         highlighted = True
00080         if self._highlight_filters.count_enabled_filters() > 0:
00081             highlighted = self._highlight_filters.test_message(msg, default=True)
00082         if self._show_highlighted_only and not highlighted:
00083             # hide messages which are not highlighted when only highlightes messages should be visible
00084             return False
00085 
00086         # update message state
00087         msg.highlighted = highlighted
00088 
00089         return True
00090 
00091     def data(self, proxy_index, role=None):
00092         """
00093         Set colors of items based on highlight filters.
00094         """
00095         index = self.mapToSource(proxy_index)
00096         if role == Qt.ForegroundRole:
00097             msg = self._source_model._messages[index.row()]
00098             if not msg.highlighted:
00099                 return QBrush(Qt.gray)
00100         return self._source_model.data(index, role)
00101 
00102     # END Required implementations of QSortFilterProxyModel functions
00103 
00104     def handle_exclude_filters_changed(self):
00105         """
00106         Invalidate filters and trigger refiltering.
00107         """
00108         self.invalidateFilter()
00109 
00110     def handle_highlight_filters_changed(self):
00111         """
00112         Invalidate filters and trigger refiltering.
00113         """
00114         if self._show_highlighted_only:
00115             self.invalidateFilter()
00116         else:
00117             self.invalidateFilter()
00118             if qVersion().startswith('4.'):
00119                 self.dataChanged.emit(self.index(0, 0), self.index(self.rowCount() - 1, self.columnCount() - 1))
00120             else:
00121                 self.dataChanged.emit(self.index(0, 0), self.index(self.rowCount() - 1, self.columnCount() - 1), [])
00122 
00123     def add_exclude_filter(self, newfilter):
00124         self._exclude_filters.append(newfilter)
00125 
00126     def add_highlight_filter(self, newfilter):
00127         self._highlight_filters.append(newfilter)
00128 
00129     def delete_exclude_filter(self, index):
00130         del self._exclude_filters[index]
00131         self.handle_exclude_filters_changed()
00132 
00133     def delete_highlight_filter(self, index):
00134         del self._highlight_filters[index]
00135         self.handle_highlight_filters_changed()
00136 
00137     def set_show_highlighted_only(self, show_highlighted_only):
00138         self._show_highlighted_only = show_highlighted_only
00139         self.invalidateFilter()


rqt_console
Author(s): Aaron Blasdel
autogenerated on Wed May 3 2017 02:48:27