Warning, /utilities/mycroft-plasmoid/plasmoid/contents/ui/Logic.qml is written in an unsupported language. File is not indexed.

0001 /* Copyright 2019 Aditya Mehra <aix.m@outlook.com>                            
0002 
0003     This library is free software; you can redistribute it and/or
0004     modify it under the terms of the GNU Lesser General Public
0005     License as published by the Free Software Foundation; either
0006     version 2.1 of the License, or (at your option) version 3, or any
0007     later version accepted by the membership of KDE e.V. (or its
0008     successor approved by the membership of KDE e.V.), which shall
0009     act as a proxy defined in Section 6 of version 3 of the license.
0010     
0011     This library is distributed in the hope that it will be useful,
0012     but WITHOUT ANY WARRANTY; without even the implied warranty of
0013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014     Lesser General Public License for more details.
0015     
0016     You should have received a copy of the GNU Lesser General Public
0017     License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0018 */
0019 
0020 import QtQuick 2.9
0021 
0022 Item {
0023     id: component
0024     property alias model: filterModel
0025 
0026     property QtObject sourceModel: undefined
0027     property string filter: ""
0028     property string property: ""
0029 
0030     Connections {
0031         onFilterChanged: invalidateFilter()
0032         onPropertyChanged: invalidateFilter()
0033         onSourceModelChanged: invalidateFilter()
0034     }
0035 
0036     Component.onCompleted: invalidateFilter()
0037 
0038     ListModel {
0039         id: filterModel
0040     }
0041 
0042     function invalidateFilter() {
0043         if (sourceModel === undefined)
0044             return;
0045 
0046         filterModel.clear();
0047 
0048         if (!isFilteringPropertyOk())
0049             return
0050 
0051         var length = sourceModel.count
0052         for (var i = 0; i < length; ++i) {
0053             var item = sourceModel.get(i);
0054             if (isAcceptedItem(item)) {
0055                 filterModel.append(item)
0056             }
0057         }
0058     }
0059 
0060     function isAcceptedItem(item) {
0061         if (item[this.property] === undefined)
0062             return false
0063 
0064         if (item[this.property].match(this.filter) === null) {
0065             return false
0066         }
0067 
0068         return true
0069     }
0070 
0071     function isFilteringPropertyOk() {
0072         if(this.property === undefined || this.property === "") {
0073             return false
0074         }
0075         return true
0076     }
0077 }
0078