message_proxy_model.py
Go to the documentation of this file.
1 # Software License Agreement (BSD License)
2 #
3 # Copyright (c) 2012, Willow Garage, Inc.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following
14 # disclaimer in the documentation and/or other materials provided
15 # with the distribution.
16 # * Neither the name of Willow Garage, Inc. nor the names of its
17 # contributors may be used to endorse or promote products derived
18 # from this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 # POSSIBILITY OF SUCH DAMAGE.
32 
33 from python_qt_binding.QtCore import Qt, qVersion, qWarning
34 try:
35  from python_qt_binding.QtCore import QSortFilterProxyModel # Qt 5
36 except ImportError:
37  from python_qt_binding.QtGui import QSortFilterProxyModel # Qt 4
38 from python_qt_binding.QtGui import QBrush, QColor
39 
40 from .filters.filter_collection import FilterCollection
41 from .message import Message
42 
43 
44 class MessageProxyModel(QSortFilterProxyModel):
45 
46  """
47  Provides sorting and filtering capabilities for the MessageDataModel.
48  Filtering is based on a collection of exclude and highlight filters.
49  """
50 
51  def __init__(self):
52  super(MessageProxyModel, self).__init__()
53  self.setDynamicSortFilter(True)
54  self.setFilterRole(Qt.UserRole)
55  self.setSortCaseSensitivity(Qt.CaseInsensitive)
56  self.setSortRole(Qt.UserRole)
57 
58  self._exclude_filters = FilterCollection()
59  self._highlight_filters = FilterCollection()
61 
62  # caching source model locally for better performance of filterAcceptsRow()
63  self._source_model = None
64 
65  def setSourceModel(self, source_model):
66  super(MessageProxyModel, self).setSourceModel(source_model)
67  self._source_model = self.sourceModel()
68 
69  # BEGIN Required implementations of QSortFilterProxyModel functions
70 
71  def filterAcceptsRow(self, sourcerow, sourceparent):
72  """
73  returns: True if the row does not match any exclude filter AND (_show_highlighted_only is
74  False OR it matches any highlight filter), ''bool''
75  """
76  msg = self._source_model._messages[sourcerow]
77  if self._exclude_filters.test_message(msg):
78  # hide excluded message
79  return False
80 
81  highlighted = True
82  if self._highlight_filters.count_enabled_filters() > 0:
83  highlighted = self._highlight_filters.test_message(msg, default=True)
84  if self._show_highlighted_only and not highlighted:
85  # hide messages which are not highlighted when only highlightes messages
86  # should be visible
87  return False
88 
89  # update message state
90  msg.highlighted = highlighted
91 
92  return True
93 
94  def data(self, proxy_index, role=None):
95  """
96  Set colors of items based on highlight filters.
97  """
98  index = self.mapToSource(proxy_index)
99  if role == Qt.ForegroundRole:
100  msg = self._source_model._messages[index.row()]
101  if not msg.highlighted:
102  return QBrush(Qt.gray)
103  return self._source_model.data(index, role)
104 
105  # END Required implementations of QSortFilterProxyModel functions
106 
108  """
109  Invalidate filters and trigger refiltering.
110  """
111  self.invalidateFilter()
112 
114  """
115  Invalidate filters and trigger refiltering.
116  """
117  if self._show_highlighted_only:
118  self.invalidateFilter()
119  else:
120  self.invalidateFilter()
121  if qVersion().startswith('4.'):
122  self.dataChanged.emit(
123  self.index(0, 0), self.index(self.rowCount() - 1, self.columnCount() - 1))
124  else:
125  self.dataChanged.emit(
126  self.index(0, 0), self.index(self.rowCount() - 1, self.columnCount() - 1), [])
127 
128  def add_exclude_filter(self, newfilter):
129  self._exclude_filters.append(newfilter)
130 
131  def add_highlight_filter(self, newfilter):
132  self._highlight_filters.append(newfilter)
133 
134  def delete_exclude_filter(self, index):
135  del self._exclude_filters[index]
137 
138  def delete_highlight_filter(self, index):
139  del self._highlight_filters[index]
141 
142  def set_show_highlighted_only(self, show_highlighted_only):
143  self._show_highlighted_only = show_highlighted_only
144  self.invalidateFilter()
def set_show_highlighted_only(self, show_highlighted_only)
def filterAcceptsRow(self, sourcerow, sourceparent)


rqt_console
Author(s): Aaron Blasdel
autogenerated on Sun May 24 2020 03:23:49