Qt Quick文字列マッチングを実現

2699 ワード

原理は簡単で、リストのDelegateで文字列がパターンに一致するかどうかを検出する関数を定義し、アルゴリズムも暴力アルゴリズムであり、一致しなければdelegateのstateを定義された状態(この状態にはいくつかの項目を隠すpropertyとtransitionがある)に設定する.
コアコード:
import QtQuick 2.0

Item {
    id: root
    height: 50

    property alias text: content.text
    property string pattern: "" //       

    states: [
        State {
            name: "normal"
            PropertyChanges { target: root; height: 50; opacity: 1 }
        },
        State {
            name: "hidden"
            PropertyChanges { target: root; height: 0; opacity: 0 }
        }
    ]

    state: "normal"

    transitions: [
        Transition {
            from: "normal"
            to: "hidden"
            ParallelAnimation {
                NumberAnimation { property: "opacity"; duration: 200 }
                SequentialAnimation {
                    PauseAnimation { duration: 100 }
                    NumberAnimation { property: "height"; duration: 400; easing.type: Easing.OutQuint }
                }
            }
        },
        Transition {
            from: "hidden"
            to: "normal"
            ParallelAnimation {
                NumberAnimation { property: "height"; duration: 400; easing.type: Easing.OutQuint }
                SequentialAnimation {
                    PauseAnimation { duration: 100 }
                    NumberAnimation { property: "opacity"; duration: 200 }
                }
            }
        }
    ]

    Text {
        id: content

        anchors.verticalCenter: parent.verticalCenter

        font.family: "Arial"
        font.pointSize: 14
    }

    onPatternChanged: {
        //        
        if (pattern.length == 0) {
            state = "normal"; //          ,    
        }
        else {
            if (pattern.length > text.length) {
                state = "hidden"; //         ,    
            }
            else {
                var i,j;
                j = 0;
                state = "normal";
                for (i = 0; i < pattern.length; i++) {
                    if (j > text.length - 1) {
                        state = "hidden";
                        break;
                    }
                    while (text.toLocaleLowerCase()[j] !== pattern.toLocaleLowerCase()[i]) {
                        if (j > text.length - 1) {
                            state = "hidden";
                            break;
                        }
                        j++;
                    }
                    j++;
                }
            }
        }
    }
}