File indexing completed on 2024-12-08 04:34:21

0001 /*
0002    SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "explorepermissionswidget.h"
0008 #include "explorepermissionswidgetfilterproxymodel.h"
0009 #include "model/permissionsmodel.h"
0010 #include <KLineEditEventHandler>
0011 #include <KLocalizedString>
0012 #include <QHeaderView>
0013 #include <QLabel>
0014 #include <QLineEdit>
0015 #include <QSortFilterProxyModel>
0016 #include <QTreeView>
0017 #include <QVBoxLayout>
0018 
0019 ExplorePermissionsWidget::ExplorePermissionsWidget(QWidget *parent)
0020     : QWidget{parent}
0021     , mTreeView(new QTreeView(this))
0022     , mSearchLineWidget(new QLineEdit(this))
0023     , mAdminPermissionsModel(new PermissionsModel(this))
0024     , mPermissionFilterProxyModel(new ExplorePermissionsWidgetFilterProxyModel(this))
0025     , mOwnRoles(new QLabel(this))
0026 {
0027     mPermissionFilterProxyModel->setObjectName(QStringLiteral("permissionFilterProxyModel"));
0028     mTreeView->setRootIsDecorated(false);
0029     mTreeView->setSortingEnabled(true);
0030     mTreeView->sortByColumn(0, Qt::AscendingOrder);
0031     mTreeView->header()->setSectionsClickable(true);
0032 
0033     auto mainLayout = new QVBoxLayout(this);
0034     mainLayout->setObjectName(QStringLiteral("mainLayout"));
0035     mainLayout->setContentsMargins({});
0036 
0037     mOwnRoles->setObjectName(QStringLiteral("mOwnRoles"));
0038     mSearchLineWidget->setObjectName(QStringLiteral("mSearchLineWidget"));
0039     mSearchLineWidget->setPlaceholderText(i18n("Search permissions..."));
0040     KLineEditEventHandler::catchReturnKey(mSearchLineWidget);
0041     mSearchLineWidget->setClearButtonEnabled(true);
0042     mainLayout->addWidget(mSearchLineWidget);
0043     mTreeView->setObjectName(QStringLiteral("mTreeView"));
0044     mainLayout->addWidget(mTreeView);
0045     mPermissionFilterProxyModel->setSourceModel(mAdminPermissionsModel);
0046     mTreeView->setModel(mPermissionFilterProxyModel);
0047     mTreeView->setColumnHidden(PermissionsModel::RolesRole, true);
0048     connect(mSearchLineWidget, &QLineEdit::textChanged, this, &ExplorePermissionsWidget::slotFilterTextChanged);
0049 
0050     mOwnRoles->setTextInteractionFlags(Qt::TextBrowserInteraction);
0051     mainLayout->addWidget(mOwnRoles);
0052 }
0053 
0054 ExplorePermissionsWidget::~ExplorePermissionsWidget() = default;
0055 
0056 void ExplorePermissionsWidget::slotFilterTextChanged(const QString &str)
0057 {
0058     mPermissionFilterProxyModel->setFilterString(str);
0059 }
0060 
0061 void ExplorePermissionsWidget::setPermissions(const QVector<Permission> &permissions)
0062 {
0063     Permissions perms;
0064     perms.setPermissions(permissions);
0065     mAdminPermissionsModel->setPermissions(perms);
0066     mTreeView->header()->resizeSections(QHeaderView::ResizeToContents);
0067 }
0068 
0069 void ExplorePermissionsWidget::setOWnRoles(const QStringList &roleStr)
0070 {
0071     mOwnRoles->setText(QStringLiteral("<qt><b>Own Roles:</b> %1</qt>").arg(roleStr.join(QLatin1Char(','))));
0072 }
0073 
0074 #include "moc_explorepermissionswidget.cpp"