File indexing completed on 2024-05-12 05:43:34
0001 /* 0002 Copyright (C) 2015 Volker Krause <vkrause@kde.org> 0003 0004 This program is free software; you can redistribute it and/or modify it 0005 under the terms of the GNU Library General Public License as published by 0006 the Free Software Foundation; either version 2 of the License, or (at your 0007 option) any later version. 0008 0009 This program is distributed in the hope that it will be useful, but WITHOUT 0010 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0011 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 0012 License for more details. 0013 0014 You should have received a copy of the GNU General Public License 0015 along with this program. If not, see <https://www.gnu.org/licenses/>. 0016 */ 0017 0018 #include "sidepane.h" 0019 0020 #include <QStyledItemDelegate> 0021 0022 class Delegate : public QStyledItemDelegate 0023 { 0024 Q_OBJECT 0025 public: 0026 explicit Delegate(QObject *parent = nullptr) : 0027 QStyledItemDelegate(parent) 0028 { 0029 } 0030 0031 QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override 0032 { 0033 auto size = QStyledItemDelegate::sizeHint(option, index); 0034 if (!option.widget || !index.isValid()) 0035 return size; 0036 size.setWidth(std::max(size.width(), option.widget->minimumWidth())); 0037 return size; 0038 } 0039 }; 0040 0041 class NonEmptySelectionModel : public QItemSelectionModel 0042 { 0043 Q_OBJECT 0044 public: 0045 explicit NonEmptySelectionModel(QAbstractItemModel* model) : 0046 QItemSelectionModel(model) 0047 { 0048 } 0049 0050 void select(const QItemSelection& selection, SelectionFlags command) override 0051 { 0052 if (selection.isEmpty() && (command & QItemSelectionModel::Clear)) 0053 return; 0054 QItemSelectionModel::select(selection, command); 0055 } 0056 0057 void select(const QModelIndex& index, SelectionFlags command) override 0058 { 0059 if (!index.isValid() && (command & QItemSelectionModel::Clear)) 0060 return; 0061 QItemSelectionModel::select(index, command); 0062 } 0063 }; 0064 0065 0066 SidePane::SidePane(QWidget* parent): 0067 QListView(parent) 0068 { 0069 viewport()->setAutoFillBackground(false); 0070 setItemDelegate(new Delegate(this)); 0071 } 0072 0073 SidePane::~SidePane() = default; 0074 0075 QSize SidePane::sizeHint() const 0076 { 0077 if (!model()) { 0078 return QSize(0, 0); 0079 } 0080 0081 const int width = sizeHintForColumn(0); 0082 const int height = QListView::sizeHint().height(); 0083 0084 return QSize(width, height); 0085 } 0086 0087 void SidePane::setModel(QAbstractItemModel* model) 0088 { 0089 QAbstractItemView::setModel(model); 0090 setMinimumWidth(sizeHint().width()); 0091 if (model) { 0092 auto selModel = new NonEmptySelectionModel(model); 0093 setSelectionModel(selModel); 0094 connect(selModel, &QItemSelectionModel::selectionChanged, this, [this]() { 0095 const auto selection = selectionModel()->selectedRows(); 0096 if (selection.isEmpty()) 0097 return; 0098 emit currentIndexChanged(selection.first().row()); 0099 }); 0100 } 0101 } 0102 0103 #include "sidepane.moc"