File indexing completed on 2024-11-24 04:42:32
0001 /* 0002 * resourceselectdialog.cpp - Resource selection dialog 0003 * Program: kalarm 0004 * SPDX-FileCopyrightText: 2019-2020 David Jarvie <djarvie@kde.org> 0005 * 0006 * SPDX-License-Identifier: GPL-2.0-or-later 0007 */ 0008 0009 #include "resourceselectdialog.h" 0010 0011 #include "resourcemodel.h" 0012 #include "lib/config.h" 0013 0014 #include <KLocalizedString> 0015 0016 #include <QVBoxLayout> 0017 #include <QDialogButtonBox> 0018 #include <QLineEdit> 0019 #include <QListView> 0020 #include <QPushButton> 0021 0022 namespace 0023 { 0024 static const char DialogName[] = "ResourceSelectDialog"; 0025 } 0026 0027 0028 ResourceSelectDialog::ResourceSelectDialog(ResourceListModel* model, QWidget* parent) 0029 : QDialog(parent) 0030 , mModel(model) 0031 { 0032 auto layout = new QVBoxLayout(this); 0033 0034 auto filterEdit = new QLineEdit(this); 0035 filterEdit->setClearButtonEnabled(true); 0036 filterEdit->setPlaceholderText(i18nc("@info A prompt for user to enter what to search for", "Search")); 0037 layout->addWidget(filterEdit); 0038 0039 mResourceList = new QListView(this); 0040 mResourceList->setModel(model); 0041 layout->addWidget(mResourceList); 0042 0043 mButtonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this); 0044 connect(mButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); 0045 connect(mButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); 0046 layout->addWidget(mButtonBox); 0047 mButtonBox->button(QDialogButtonBox::Ok)->setEnabled(false); 0048 0049 connect(filterEdit, &QLineEdit::textChanged, this, &ResourceSelectDialog::slotFilterText); 0050 connect(mResourceList->selectionModel(), &QItemSelectionModel::selectionChanged, this, &ResourceSelectDialog::slotSelectionChanged); 0051 connect(mResourceList, &QAbstractItemView::doubleClicked, this, &ResourceSelectDialog::slotDoubleClicked); 0052 0053 QSize s; 0054 if (Config::readWindowSize(DialogName, s)) 0055 resize(s); 0056 } 0057 0058 ResourceSelectDialog::~ResourceSelectDialog() 0059 { 0060 Config::writeWindowSize(DialogName, size()); 0061 } 0062 0063 void ResourceSelectDialog::setDefaultResource(const Resource& resource) 0064 { 0065 const QModelIndex index = mModel->resourceIndex(resource); 0066 mResourceList->selectionModel()->select(index, QItemSelectionModel::SelectCurrent); 0067 } 0068 0069 Resource ResourceSelectDialog::selectedResource() const 0070 { 0071 const QModelIndexList indexes = mResourceList->selectionModel()->selectedRows(); //clazy:exclude=inefficient-qlist 0072 if (indexes.isEmpty()) 0073 return {}; 0074 return mModel->resource(indexes[0].row()); 0075 } 0076 0077 void ResourceSelectDialog::slotFilterText(const QString& text) 0078 { 0079 mModel->setFilterText(text); 0080 } 0081 0082 void ResourceSelectDialog::slotDoubleClicked() 0083 { 0084 if (!mResourceList->selectionModel()->selectedIndexes().isEmpty()) 0085 accept(); 0086 } 0087 0088 void ResourceSelectDialog::slotSelectionChanged() 0089 { 0090 mButtonBox->button(QDialogButtonBox::Ok)->setEnabled(!mResourceList->selectionModel()->selectedIndexes().isEmpty()); 0091 } 0092 0093 #include "moc_resourceselectdialog.cpp" 0094 0095 // vim: et sw=4: