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


rqt_console
Author(s): Aaron Blasdel
autogenerated on Sat Jun 8 2019 20:58:08