Pimcommon

collectionaclwidget.cpp
1/*
2 SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "collectionaclwidget.h"
8#include "acllistview.h"
9#include "aclmanager.h"
10
11#include <KLocalizedString>
12
13#include <QAction>
14#include <QActionEvent>
15#include <QCheckBox>
16#include <QHBoxLayout>
17#include <QPushButton>
18#include <QVBoxLayout>
19
20using namespace PimCommon;
21/**
22 * Unfortunately QPushButton doesn't support to plug in
23 * a QAction like QToolButton does, so we have to reimplement it :(
24 */
25class ActionButton : public QPushButton
26{
27public:
28 ActionButton(QWidget *parent = nullptr)
30 {
31 }
32
33 void setDefaultAction(QAction *action)
34 {
35 if (!actions().contains(action)) {
36 addAction(action);
38 }
39
40 setText(action->text());
41 setEnabled(action->isEnabled());
42
43 mDefaultAction = action;
44 }
45
46protected:
47 void actionEvent(QActionEvent *event) override
48 {
49 QAction *action = event->action();
50 switch (event->type()) {
52 if (action == mDefaultAction) {
53 setDefaultAction(mDefaultAction);
54 }
55 return;
56 break;
57 default:
58 break;
59 }
60
62 }
63
64private:
65 QAction *mDefaultAction = nullptr;
66};
67
68CollectionAclWidget::CollectionAclWidget(QWidget *parent)
69 : QWidget(parent)
70 , mAclManager(new PimCommon::AclManager(this))
71 , mRecursiveChk(new QCheckBox(i18n("Apply permissions on all &subfolders."), this))
72{
73 auto layout = new QHBoxLayout(this);
74 auto listViewLayout = new QVBoxLayout;
75 layout->addLayout(listViewLayout);
76
77 auto view = new AclListView;
78 view->setObjectName(QLatin1StringView("list_view"));
79 listViewLayout->addWidget(view);
80 listViewLayout->addWidget(mRecursiveChk);
81 connect(mRecursiveChk, &QCheckBox::clicked, this, &CollectionAclWidget::slotRecursivePermissionChanged);
82
83 view->setAlternatingRowColors(true);
84 view->setModel(mAclManager->model());
85 view->setSelectionModel(mAclManager->selectionModel());
86
87 auto buttonBox = new QWidget;
88 auto buttonBoxVBoxLayout = new QVBoxLayout(buttonBox);
89 buttonBoxVBoxLayout->setContentsMargins({});
90 layout->addWidget(buttonBox);
91
92 auto button = new ActionButton(buttonBox);
93 buttonBoxVBoxLayout->addWidget(button);
94 button->setObjectName(QLatin1StringView("add"));
95 button->setDefaultAction(mAclManager->addAction());
96
97 button = new ActionButton(buttonBox);
98 buttonBoxVBoxLayout->addWidget(button);
99 button->setObjectName(QLatin1StringView("edit"));
100 button->setDefaultAction(mAclManager->editAction());
101
102 button = new ActionButton(buttonBox);
103 buttonBoxVBoxLayout->addWidget(button);
104 button->setDefaultAction(mAclManager->deleteAction());
105 button->setObjectName(QLatin1StringView("delete"));
106
107 auto spacer = new QWidget(buttonBox);
108 buttonBoxVBoxLayout->addWidget(spacer);
109 spacer->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding);
110 connect(view, SIGNAL(doubleClicked(QModelIndex)), mAclManager->editAction(), SIGNAL(triggered()));
111 connect(mAclManager, &AclManager::collectionCanBeAdministrated, this, &CollectionAclWidget::slotCollectionCanBeAdministrated);
112 connect(mAclManager, &AclManager::collectionCanBeAdministrated, view, &AclListView::slotCollectionCanBeAdministrated);
113}
114
115CollectionAclWidget::~CollectionAclWidget() = default;
116
117void CollectionAclWidget::slotCollectionCanBeAdministrated(bool b)
118{
119 if (!b) {
120 mRecursiveChk->setChecked(false);
121 }
122 mRecursiveChk->setEnabled(b);
123}
124
125AclManager *CollectionAclWidget::aclManager() const
126{
127 return mAclManager;
128}
129
130bool CollectionAclWidget::recursive() const
131{
132 return mRecursiveChk->isChecked();
133}
134
135void CollectionAclWidget::setEnableRecursiveCheckBox(bool enable)
136{
137 if (!enable) {
138 mRecursiveChk->setChecked(false);
139 }
140 mRecursiveChk->setEnabled(enable);
141}
142
143void CollectionAclWidget::slotRecursivePermissionChanged()
144{
145 mAclManager->setChanged(true);
146}
147
148#include "moc_collectionaclwidget.cpp"
QString i18n(const char *text, const TYPE &arg...)
folderdialogacltab.h
void setChecked(bool)
void clicked(bool checked)
void setText(const QString &text)
bool isEnabled() const const
void trigger()
void addLayout(QLayout *layout, int stretch)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
virtual bool event(QEvent *e) override
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
virtual void actionEvent(QActionEvent *event)
QList< QAction * > actions() const const
QAction * addAction(const QIcon &icon, const QString &text)
void setEnabled(bool)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:17:23 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.