Esempio n. 1
0
    def apply_filter(self, text=None):
        """filter table to only show tests matching text"""
        if text is None:
            text = self.filter.text()

        if text:
            text = text.replace("(", "\(")
            text = text.replace(")", "\)")
            m = QRegularExpression(text)
            m.setPatternOptions(QRegularExpression.CaseInsensitiveOption)
            if m.isValid():
                m.optimize()
                filter = lambda row_num: m.match(
                    self.table.item(row_num, 0).text()).hasMatch()
            else:
                return

        else:
            filter = lambda row: True

        for i in range(0, self.table.rowCount()):
            self.table.setRowHidden(i, not filter(i))
 def testMatch(self):
     re = QRegularExpression('^.*(word2).*$')
     self.assertTrue(re.isValid())
     match = re.match('word1 word2  word3')
     self.assertTrue(match.isValid())
     self.assertEqual(match.captured(1), 'word2')
Esempio n. 3
0
class ProxyModelJson(QSortFilterProxyModel):
    def __init__(self, parent=None):
        QSortFilterProxyModel.__init__(self, parent)
        #        char* m_filterRoles;
        self.m_filterRoles = ""
        #        char* m_sortData;
        self.m_sortData = ""
        self.m_typeSort = 0
        self.m_boolSignalReset = False
        self.m_reguexp = QRegularExpression()
        self.m_strexp = ""
        self.m_boolstrexp = True
        self.m_boolVacio = True

    def setFilterRoles(self, filterRoles):
        if self.m_filterRoles == filterRoles:
            return
        self.m_filterRoles = filterRoles

    # self.filterChanged()

    def setSortData(self, sortData):
        pass

    def setTypeSort(self, typeSort):
        if self.m_typeSort == typeSort:
            return
        self.m_typeSort = typeSort
        self.sortOrder()

    def reNumaelis(self, text):
        if text == "":
            return text
        lista = text.split(QRegularExpression("\\s+"))
        tem2 = ""
        for item in lista:
            tem2 += "(?=.*" + item.strip() + ")"
        return tem2

    @Slot(bool)
    def setAscendingOrder(self, bao):
        if self.sortOrder() == Qt.AscendingOrder and bao == False:
            self.sort(0, Qt.DescendingOrder)

        if self.sortOrder() == Qt.DescendingOrder and bao == True:
            self.sort(0, Qt.AscendingOrder)

    @Slot(bool)
    def setEmitSignalReset(self, bes):
        #        if(m_boolSignalReset!=bes){
        #            m_boolSignalReset=bes;
        #        }
        pass

    def lessThan(self, left, right):
        rolis = self.sortRole()
        l = left.model().data(left, rolis) if left.model() == True else None
        r = right.model().data(right, rolis) if right.model() == True else None
        if l.__class__() == None:
            return None
        #return (r.type() != QVariant::Invalid);
        #if l.__class__() == '':
#        if self.isSortLocaleAware():
        return (l.property(self.m_sortData) <
                (r.property(self.m_sortData))) < 0

    def filterAcceptsRow(self, source_row, source_parent):
        if self.m_boolVacio:
            return True
        filteris = self.filterRole()

        source_index = self.sourceModel().index(source_row, 0, source_parent)
        if source_index.isValid() == False:
            return True

        key = self.sourceModel().data(source_index,
                                      filteris).property(self.m_filterRoles)

        if self.m_boolstrexp:
            return key.__contains__(
                self.m_strexp
            )  #;//, Qt::CaseInsensitive); el metadata esta en minuscula
        else:
            return self.m_reguexp.match(key).hasMatch()
            #return QString!!!key.contains(self.m_reguexp)

    @Slot(int, result=int)
    def getIndexParent(self, index):
        if index != -1:
            return (self.mapToSource(self.index(index, 0,
                                                QModelIndex()))).row()
        return -1

    @Slot(int, result=int)
    def getIndexProxy(self, index):
        if index != -1:
            return (self.mapFromSource(self.sourceModel().index(
                index, 0, QModelIndex()))).row()
        return -1

    @Slot(str)
    def findElis(self, text):
        textt = text.strip().lower()
        if textt == "":
            self.m_boolVacio = True
        else:
            self.m_boolVacio = False
        if textt.__contains__(" "):
            self.m_boolstrexp = False
            self.m_reguexp.setPattern(self.reNumaelis(textt))
        else:
            self.m_boolstrexp = True
            self.m_strexp = textt

    #//setFilter no es usado al filtra, solo se invoca para que comienze a hacerlo
        self.setFilterRegExp(QRegExp(text))

    @Signal
    def signalReset(self):
        pass

    @Slot()
    def endFilterOrder(self):
        #        if(m_boolSignalReset){
        #            //qDebug()<<"termino Filtering";
        #            m_boolSignalReset=false;
        #            emit signalReset();
        #        }
        pass
class TypeChecker:
    def __init__(self, parent=None):
        self.bool_exp = QRegularExpression('^(true)|(false)$')
        assert self.bool_exp.isValid()
        self.bool_exp.setPatternOptions(QRegularExpression.CaseInsensitiveOption)

        self.byteArray_exp = QRegularExpression(r'^[\x00-\xff]*$')
        assert self.byteArray_exp.isValid()

        self.char_exp = QRegularExpression('^.$')
        assert self.char_exp.isValid()

        pattern = r'^[+-]?\d+$'
        self.int_exp = QRegularExpression(pattern)
        assert self.int_exp.isValid()

        pattern = r'^\(([0-9]*),([0-9]*),([0-9]*),([0-9]*)\)$'
        self.color_exp = QRegularExpression(pattern)
        assert self.color_exp.isValid()

        pattern = r'^\((-?[0-9]*),(-?[0-9]*)\)$'
        self.point_exp = QRegularExpression(pattern)
        assert self.point_exp.isValid()

        pattern = r'^\((-?[0-9]*),(-?[0-9]*),(-?[0-9]*),(-?[0-9]*)\)$'
        self.rect_exp = QRegularExpression(pattern)
        assert self.rect_exp.isValid()

        self.size_exp = QRegularExpression(self.point_exp)

        date_pattern = '([0-9]{,4})-([0-9]{,2})-([0-9]{,2})'
        self.date_exp = QRegularExpression('^{}$'.format(date_pattern))
        assert self.date_exp.isValid()

        time_pattern = '([0-9]{,2}):([0-9]{,2}):([0-9]{,2})'
        self.time_exp = QRegularExpression('^{}$'.format(time_pattern))
        assert self.time_exp.isValid()

        pattern = '^{}T{}$'.format(date_pattern, time_pattern)
        self.dateTime_exp = QRegularExpression(pattern)
        assert self.dateTime_exp.isValid()

    def type_from_text(self, text):
        if self.bool_exp.match(text).hasMatch():
            return bool
        if self.int_exp.match(text).hasMatch():
            return int
        return None

    def create_validator(self, value, parent):
        if isinstance(value, bool):
            return QRegularExpressionValidator(self.bool_exp, parent)
        if isinstance(value, float):
            return QDoubleValidator(parent)
        if isinstance(value, int):
            return QIntValidator(parent)
        if isinstance(value, QByteArray):
            return QRegularExpressionValidator(self.byteArray_exp, parent)
        if isinstance(value, QColor):
            return QRegularExpressionValidator(self.color_exp, parent)
        if isinstance(value, QDate):
            return QRegularExpressionValidator(self.date_exp, parent)
        if isinstance(value, QDateTime):
            return QRegularExpressionValidator(self.dateTime_exp, parent)
        if isinstance(value, QTime):
            return QRegularExpressionValidator(self.time_exp, parent)
        if isinstance(value, QPoint):
            return QRegularExpressionValidator(self.point_exp, parent)
        if isinstance(value, QRect):
            return QRegularExpressionValidator(self.rect_exp, parent)
        if isinstance(value, QSize):
            return QRegularExpressionValidator(self.size_exp, parent)
        return None

    def from_string(self, text, original_value):
        if isinstance(original_value, QColor):
            match = self.color_exp.match(text)
            return QColor(min(int(match.captured(1)), 255),
                          min(int(match.captured(2)), 255),
                          min(int(match.captured(3)), 255),
                          min(int(match.captured(4)), 255))
        if isinstance(original_value, QDate):
            value = QDate.fromString(text, Qt.ISODate)
            return value if value.isValid() else None
        if isinstance(original_value, QDateTime):
            value = QDateTime.fromString(text, Qt.ISODate)
            return value if value.isValid() else None
        if isinstance(original_value, QTime):
            value = QTime.fromString(text, Qt.ISODate)
            return value if value.isValid() else None
        if isinstance(original_value, QPoint):
            match = self.point_exp.match(text)
            return QPoint(int(match.captured(1)),
                          int(match.captured(2)))
        if isinstance(original_value, QRect):
            match = self.rect_exp.match(text)
            return QRect(int(match.captured(1)),
                         int(match.captured(2)),
                         int(match.captured(3)),
                         int(match.captured(4)))
        if isinstance(original_value, QSize):
            match = self.size_exp.match(text)
            return QSize(int(match.captured(1)),
                         int(match.captured(2)))
        if isinstance(original_value, list):
            return text.split(',')
        return type(original_value)(text)