File indexing completed on 2025-01-19 05:18:32
0001 /* 0002 * Copyright (c) 2018 Sune Vuorela <sune@vuorela.dk> 0003 * 0004 * Permission is hereby granted, free of charge, to any person 0005 * obtaining a copy of this software and associated documentation 0006 * files (the "Software"), to deal in the Software without 0007 * restriction, including without limitation the rights to use, 0008 * copy, modify, merge, publish, distribute, sublicense, and/or sell 0009 * copies of the Software, and to permit persons to whom the 0010 * Software is furnished to do so, subject to the following 0011 * conditions: 0012 * 0013 * The above copyright notice and this permission notice shall be 0014 * included in all copies or substantial portions of the Software. 0015 * 0016 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 0017 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 0018 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 0019 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 0020 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 0021 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 0022 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 0023 * OTHER DEALINGS IN THE SOFTWARE. 0024 */ 0025 #include "listpane.h" 0026 #include <QListView> 0027 #include <QHBoxLayout> 0028 #include <QLineEdit> 0029 #include <memory> 0030 #include <QSortFilterProxyModel> 0031 #include <QAction> 0032 0033 ListPane::ListPane(QWidget* parent) : QWidget(parent){ 0034 m_view = new QListView(this); 0035 m_filter = std::make_unique<QSortFilterProxyModel>(); 0036 m_filter->setFilterCaseSensitivity(Qt::CaseInsensitive); 0037 setLayout(new QVBoxLayout()); 0038 0039 auto edit = std::make_unique<QLineEdit>(); 0040 edit->setPlaceholderText("Search"); 0041 auto action = edit->addAction(QIcon::fromTheme("edit-clear"),QLineEdit::TrailingPosition); 0042 connect(action, &QAction::triggered, edit.get(), &QLineEdit::clear); 0043 connect(edit.get(), &QLineEdit::textChanged, this, &ListPane::setFilterString); 0044 0045 layout()->addWidget(edit.release()); 0046 layout()->addWidget(m_view); 0047 connect(m_view,&QListView::clicked, this, [this](const QModelIndex& idx) { 0048 QString data = idx.data(Qt::UserRole + 1).toString(); 0049 if (!data.isEmpty()) { 0050 Q_EMIT this->fileSelected(data); 0051 } 0052 }); 0053 m_view->setModel(m_filter.get()); 0054 } 0055 0056 ListPane::~ListPane() 0057 { 0058 // for smart pointers 0059 } 0060 0061 0062 void ListPane::setModel(const std::shared_ptr<QAbstractItemModel>& model) 0063 { 0064 m_model = model; 0065 m_filter->setSourceModel(model.get()); 0066 } 0067 0068 void ListPane::setFilterString(const QString& filter) { 0069 m_filter->setFilterFixedString(filter); 0070 }