File indexing completed on 2025-01-19 04:46:52
0001 /* 0002 SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "folderconfiguretreewidget.h" 0008 #include "foldersettingfilterproxymodel.h" 0009 #include <KCheckableProxyModel> 0010 #include <KLocalizedString> 0011 #include <MailCommon/FolderTreeView> 0012 #include <MailCommon/FolderTreeWidget> 0013 #include <QPushButton> 0014 #include <QVBoxLayout> 0015 0016 FolderConfigureTreeWidget::FolderConfigureTreeWidget(QWidget *parent) 0017 : QWidget(parent) 0018 , mFolderSettingFilterProxyModel(new FolderSettingFilterProxyModel(this)) 0019 , mFolderTreeWidget(new MailCommon::FolderTreeWidget(this, 0020 nullptr, 0021 MailCommon::FolderTreeWidget::TreeViewOptions(MailCommon::FolderTreeWidget::UseDistinctSelectionModel 0022 | MailCommon::FolderTreeWidget::HideStatistics 0023 | MailCommon::FolderTreeWidget::HideHeaderViewMenu))) 0024 { 0025 auto mainLayout = new QVBoxLayout(this); 0026 mainLayout->setObjectName(QLatin1StringView("mainLayout")); 0027 mainLayout->setContentsMargins({}); 0028 mFolderTreeWidget->setObjectName(QLatin1StringView("foldertreewidget")); 0029 mFolderTreeWidget->folderTreeView()->setDragEnabled(false); 0030 mFolderTreeWidget->folderTreeView()->setSelectionMode(QAbstractItemView::ExtendedSelection); 0031 0032 auto ftv = mFolderTreeWidget->folderTreeView(); 0033 auto sourceModel = ftv->model(); 0034 auto selectionModel = mFolderTreeWidget->selectionModel(); 0035 0036 auto checkable = new KCheckableProxyModel(this); 0037 checkable->setObjectName(QLatin1StringView("checkable")); 0038 checkable->setSourceModel(sourceModel); 0039 checkable->setSelectionModel(selectionModel); 0040 0041 mFolderSettingFilterProxyModel->setObjectName(QLatin1StringView("folderSettingFilterProxyModel")); 0042 mFolderSettingFilterProxyModel->setSourceModel(checkable); 0043 0044 ftv->setModel(mFolderSettingFilterProxyModel); 0045 ftv->expandAll(); 0046 mainLayout->addWidget(mFolderTreeWidget); 0047 0048 auto buttonLayout = new QHBoxLayout; 0049 buttonLayout->setObjectName(QLatin1StringView("buttonLayout")); 0050 mainLayout->addLayout(buttonLayout); 0051 0052 mSelectFolder = new QPushButton(i18n("Select"), this); 0053 mSelectFolder->setObjectName(QLatin1StringView("selectFolder")); 0054 buttonLayout->addWidget(mSelectFolder); 0055 mSelectFolder->setEnabled(false); 0056 connect(mSelectFolder, &QPushButton::clicked, this, [this]() { 0057 changeFolderSelection(true); 0058 }); 0059 0060 mUnSelectFolder = new QPushButton(i18n("Unselect"), this); 0061 mUnSelectFolder->setObjectName(QLatin1StringView("unSelectFolder")); 0062 mUnSelectFolder->setEnabled(false); 0063 buttonLayout->addWidget(mUnSelectFolder); 0064 connect(mUnSelectFolder, &QPushButton::clicked, this, [this]() { 0065 changeFolderSelection(false); 0066 }); 0067 connect(mFolderTreeWidget->folderTreeView()->selectionModel(), 0068 &QItemSelectionModel::currentChanged, 0069 this, 0070 &FolderConfigureTreeWidget::slotSelectionChanged); 0071 } 0072 0073 FolderConfigureTreeWidget::~FolderConfigureTreeWidget() = default; 0074 0075 void FolderConfigureTreeWidget::slotSelectionChanged() 0076 { 0077 const QModelIndexList indexes = mFolderTreeWidget->folderTreeView()->selectionModel()->selection().indexes(); 0078 bool checkedFolder = false; 0079 bool uncheckedFolder = false; 0080 for (const QModelIndex &selectedIndex : indexes) { 0081 bool b = mFolderSettingFilterProxyModel->data(selectedIndex, Qt::CheckStateRole).toBool(); 0082 if (b) { 0083 checkedFolder = true; 0084 } else { 0085 uncheckedFolder = true; 0086 } 0087 } 0088 mSelectFolder->setEnabled(uncheckedFolder); 0089 mUnSelectFolder->setEnabled(checkedFolder); 0090 } 0091 0092 void FolderConfigureTreeWidget::changeFolderSelection(bool select) 0093 { 0094 const QModelIndexList indexes = mFolderTreeWidget->folderTreeView()->selectionModel()->selectedIndexes(); 0095 for (const QModelIndex &selectedIndex : indexes) { 0096 mFolderSettingFilterProxyModel->setData(selectedIndex, select ? Qt::Checked : Qt::Unchecked, Qt::CheckStateRole); 0097 } 0098 slotSelectionChanged(); 0099 } 0100 0101 Akonadi::Collection::List FolderConfigureTreeWidget::listCollections() const 0102 { 0103 return mFolderSettingFilterProxyModel->listCollections(); 0104 } 0105 0106 #include "moc_folderconfiguretreewidget.cpp"