File indexing completed on 2024-05-19 05:42:14

0001 // ct_lvtwdg_treefiltermodel.h                                       -*-C++-*-
0002 
0003 /*
0004 // Copyright 2023 Codethink Ltd <codethink@codethink.co.uk>
0005 // SPDX-License-Identifier: Apache-2.0
0006 //
0007 // Licensed under the Apache License, Version 2.0 (the "License");
0008 // you may not use this file except in compliance with the License.
0009 // You may obtain a copy of the License at
0010 //
0011 //     http://www.apache.org/licenses/LICENSE-2.0
0012 //
0013 // Unless required by applicable law or agreed to in writing, software
0014 // distributed under the License is distributed on an "AS IS" BASIS,
0015 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0016 // See the License for the specific language governing permissions and
0017 // limitations under the License.
0018 */
0019 
0020 #ifndef INCLUDED_LVTMDL_TREEFILTERMODEL
0021 #define INCLUDED_LVTMDL_TREEFILTERMODEL
0022 
0023 #include <lvtmdl_export.h>
0024 
0025 #include <memory>
0026 #include <string>
0027 
0028 //@PURPOSE: Filters the data from the original model
0029 //
0030 //@CLASSES:
0031 //  lvtmdl::TreeFilterModel:
0032 //
0033 //@DESCRIPTION: This component allows filtering of the.
0034 // class model by a string.
0035 //
0036 /// Usage:
0037 ///------
0038 ///  auto sort_model = std::make_unique<TreeFilterModel>()
0039 ///  sort_model->setSourceModel(originalModel);
0040 ///  treeView->setModel(sort_model);
0041 ///  lineEdit->changed().connect(sort_model, [sort_model]{
0042 ///     sort_model->setFilter(lineEdit->text());
0043 ///  });
0044 
0045 #include <QSortFilterProxyModel>
0046 using FilterProxyModel = QSortFilterProxyModel;
0047 
0048 namespace Codethink::lvtmdl {
0049 
0050 class LVTMDL_EXPORT TreeFilterModel : public QSortFilterProxyModel
0051 // This class filters data from the TreeFilterModel
0052 {
0053     // DATA TYPES
0054     struct TreeFilterModelPrivate;
0055 
0056   public:
0057     // CREATORS
0058     TreeFilterModel();
0059     // Constructor
0060     ~TreeFilterModel() noexcept override;
0061     // Destructor
0062 
0063     void setFilter(const QString& filter);
0064     // Sets the filter to act on filterAcceptRow
0065 
0066     [[nodiscard]] bool filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const override;
0067 
0068   private:
0069     std::unique_ptr<TreeFilterModelPrivate> d;
0070 };
0071 
0072 } // end namespace Codethink::lvtmdl
0073 
0074 #endif