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 stoporse 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 # Author: Isaac Saito 00034 00035 from python_qt_binding.QtCore import QRegExp, Qt 00036 from rqt_console.filters.message_filter import MessageFilter 00037 00038 00039 class TextFilter(MessageFilter): 00040 """ 00041 Provides a filtering feature for text set by set_text. 00042 00043 Inheriting rqt_console.filters.MessageFilter, this class provides timeout 00044 effect to the input widget (eg. QLineEdit) that contains this class. 00045 """ 00046 00047 def __init__(self, qregexp=None): 00048 super(TextFilter, self).__init__() 00049 self._regexp = qregexp 00050 00051 def test_message(self, text): 00052 """ 00053 Overridden. 00054 00055 :param message: the message to be tested against the filters. 00056 :type message: str. 00057 :rtype: bool 00058 """ 00059 00060 _hit = False 00061 00062 if (self.is_enabled() and 00063 self._text != '' and 00064 not self._qregexp == None # If None, init process isn't done yet 00065 # and we can ignore the call to this 00066 # method. 00067 ): 00068 pos_hit = self._qregexp.indexIn(text) 00069 if pos_hit >= 0: 00070 _hit = True 00071 else: 00072 _hit = False 00073 return _hit 00074 00075 # def set_regexp(self, qregexp): 00076 # """ 00077 # Setter for self._qregexp. Need not be used if _qregexp is already set 00078 # via __init__. Calling this method overwrites the existing regex 00079 # instance. 00080 # 00081 # Do not mix up self._qregexp with MessageFilter._regex that is boolean. 00082 # Instead, this method sets QRegExp instance, that test_message method 00083 # uses. 00084 # 00085 # :type qregexp: QRegExp 00086 # """ 00087 # self._qregexp = qregexp 00088 00089 def get_regexp(self): 00090 return self._regex 00091 00092 def set_text(self, text): 00093 """ 00094 Setter for _text 00095 :param text: text to set ''str'' 00096 :emits filter_changed_signal: If _enabled is true 00097 """ 00098 super(TextFilter, self).set_text(text) 00099 00100 syntax_nr = QRegExp.RegExp 00101 syntax = QRegExp.PatternSyntax(syntax_nr) 00102 self.regex = QRegExp(text, Qt.CaseInsensitive, syntax) 00103 self.set_regex(self.regex) 00104 00105 def get_text(self): 00106 return self._text