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, QSortFilterProxyModel
00035 
00036 from .filters.filter_collection import FilterCollection
00037 
00038 
00039 class MessageProxyModel(QSortFilterProxyModel):
00040     """
00041     Provides sorting and filtering capabilities for the MessageDataModel.
00042     Filtering is based on subclassed Filters stored in the FilterCollections.
00043     """
00044     def __init__(self):
00045         super(MessageProxyModel, self).__init__()
00046         self._filters = []
00047         self.setDynamicSortFilter(True)
00048         self._show_highlighted_only = False
00049 
00050         self._exclude_filters = FilterCollection(self)
00051         self._highlight_filters = FilterCollection(self)
00052 
00053     # BEGIN Required implementations of QSortFilterProxyModel functions
00054     def filterAcceptsRow(self, sourcerow, sourceparent):
00055         """
00056         returns: True if the row does not match the exclude filters AND (_show_highlighted_only is False OR it matches the _highlight_filters, ''bool''
00057         OR
00058         returns: False if the row matches the exclude filters OR (_show_highlighted_only is True and it doesn't match the _highlight_filters, ''bool''
00059         """
00060         rowdata = []
00061         for index in range(self.sourceModel().columnCount()):
00062             rowdata.append(self.sourceModel().index(sourcerow, index, sourceparent).data())
00063 
00064         if self._exclude_filters.test_message_array(rowdata):
00065             return False
00066         if self._highlight_filters.count_enabled_filters() == 0:
00067             return True
00068         match = self._highlight_filters.test_message_array(rowdata)
00069 
00070         return not self._show_highlighted_only or match
00071 
00072     def data(self, index, role=None):
00073         """
00074         Sets colors of items based on highlight filters and severity type
00075         """
00076         messagelist = self.sourceModel()._messages.get_message_list()
00077         index = self.mapToSource(index)
00078         if index.row() >= 0 or index.row() < len(messagelist):
00079             if index.column() >= 0 or index.column() < messagelist[index.row()].count():
00080                 if role == Qt.ForegroundRole:
00081                     if index.column() == 1:
00082                         data = index.data()
00083                         severity_levels = {'Debug': QBrush(Qt.cyan), \
00084                                            'Info': QBrush(Qt.darkCyan), \
00085                                            'Warn': QBrush(Qt.darkYellow), \
00086                                            'Error': QBrush(Qt.darkRed), \
00087                                            'Fatal': QBrush(Qt.red)}
00088                         if data in severity_levels.keys():
00089                             return severity_levels[data]
00090                         else:
00091                             raise KeyError('Unknown severity type: %s' % data)
00092                     if self._highlight_filters.count_enabled_filters() > 0:
00093                         if not self._highlight_filters.test_message(messagelist[index.row()]):
00094                             return QBrush(Qt.gray)
00095         return self.sourceModel().data(index, role)
00096     # END Required implementations of QSortFilterProxyModel functions
00097 
00098     def handle_filters_changed(self):
00099         self.invalidate()
00100 
00101     def add_exclude_filter(self, newfilter):
00102         self._exclude_filters.append(newfilter)
00103 
00104     def add_highlight_filter(self, newfilter):
00105         self._highlight_filters.append(newfilter)
00106 
00107     def delete_exclude_filter(self, index):
00108         if index < self._exclude_filters.count() and index >= 0:
00109             del self._exclude_filters[index]
00110             self.reset()
00111             return True
00112         return False
00113 
00114     def delete_highlight_filter(self, index):
00115         if index < self._highlight_filters.count() and index >= 0:
00116             del self._highlight_filters[index]
00117             self.reset()
00118             return True
00119         return False
00120 
00121     def set_show_highlighted_only(self, show_highlighted_only):
00122         self._show_highlighted_only = show_highlighted_only
00123         self.reset()
00124 
00125     def save_to_file(self, filehandle):
00126         """
00127         Saves to an already open filehandle.
00128         :return: True if file write is successful, ''bool''
00129         OR
00130         :return: False if file write fails, ''bool''
00131         """
00132         try:
00133             filehandle.write(self.sourceModel()._messages.header_print())
00134             for index in range(self.rowCount()):
00135                 row = self.mapToSource(self.index(index, 0)).row()
00136                 filehandle.write(self.sourceModel()._messages.get_message_list()[row].file_print())
00137         except Exception as e:
00138             qWarning('File save failed: %s' % str(e))
00139             return False
00140         return True


rqt_console
Author(s): Aaron Blasdel
autogenerated on Fri Jan 3 2014 11:54:30