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"
8using namespace Qt::Literals::StringLiterals;
9
10#include "acllistview.h"
11#include "aclmanager.h"
12
13#include <KLocalizedString>
14
15#include <QAction>
16#include <QActionEvent>
17#include <QCheckBox>
18#include <QHBoxLayout>
19#include <QPushButton>
20#include <QVBoxLayout>
21
22using namespace PimCommon;
23/**
24 * Unfortunately QPushButton doesn't support to plug in
25 * a QAction like QToolButton does, so we have to reimplement it :(
26 */
27class ActionButton : public QPushButton
28{
29public:
30 ActionButton(QWidget *parent = nullptr)
32 {
33 }
34
35 void setDefaultAction(QAction *action)
36 {
37 if (!actions().contains(action)) {
38 addAction(action);
40 }
41
42 setText(action->text());
43 setEnabled(action->isEnabled());
44
45 mDefaultAction = action;
46 }
47
48protected:
49 void actionEvent(QActionEvent *event) override
50 {
51 QAction *action = event->action();
52 switch (event->type()) {
54 if (action == mDefaultAction) {
55 setDefaultAction(mDefaultAction);
56 }
57 return;
58 break;
59 default:
60 break;
61 }
62
64 }
65
66private:
67 QAction *mDefaultAction = nullptr;
68};
69
70CollectionAclWidget::CollectionAclWidget(QWidget *parent)
71 : QWidget(parent)
72 , mAclManager(new PimCommon::AclManager(this))
73 , mRecursiveChk(new QCheckBox(i18nc("@option:check", "Apply permissions on all &subfolders."), this))
74{
75 auto layout = new QHBoxLayout(this);
76 auto listViewLayout = new QVBoxLayout;
77 layout->addLayout(listViewLayout);
78
79 auto view = new AclListView;
80 view->setObjectName("list_view"_L1);
81 listViewLayout->addWidget(view);
82 listViewLayout->addWidget(mRecursiveChk);
83 connect(mRecursiveChk, &QCheckBox::clicked, this, &CollectionAclWidget::slotRecursivePermissionChanged);
84
85 view->setAlternatingRowColors(true);
86 view->setModel(mAclManager->model());
87 view->setSelectionModel(mAclManager->selectionModel());
88
89 auto buttonBox = new QWidget;
90 auto buttonBoxVBoxLayout = new QVBoxLayout(buttonBox);
91 buttonBoxVBoxLayout->setContentsMargins({});
92 layout->addWidget(buttonBox);
93
94 auto button = new ActionButton(buttonBox);
95 buttonBoxVBoxLayout->addWidget(button);
96 button->setObjectName("add"_L1);
97 button->setDefaultAction(mAclManager->addAction());
98
99 button = new ActionButton(buttonBox);
100 buttonBoxVBoxLayout->addWidget(button);
101 button->setObjectName("edit"_L1);
102 button->setDefaultAction(mAclManager->editAction());
103
104 button = new ActionButton(buttonBox);
105 buttonBoxVBoxLayout->addWidget(button);
106 button->setDefaultAction(mAclManager->deleteAction());
107 button->setObjectName("delete"_L1);
108
109 auto spacer = new QWidget(buttonBox);
110 buttonBoxVBoxLayout->addWidget(spacer);
111 spacer->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding);
112 connect(view, SIGNAL(doubleClicked(QModelIndex)), mAclManager->editAction(), SIGNAL(triggered()));
113 connect(mAclManager, &AclManager::collectionCanBeAdministrated, this, &CollectionAclWidget::slotCollectionCanBeAdministrated);
114 connect(mAclManager, &AclManager::collectionCanBeAdministrated, view, &AclListView::slotCollectionCanBeAdministrated);
115}
116
117CollectionAclWidget::~CollectionAclWidget() = default;
118
119void CollectionAclWidget::slotCollectionCanBeAdministrated(bool b)
120{
121 if (!b) {
122 mRecursiveChk->setChecked(false);
123 }
124 mRecursiveChk->setEnabled(b);
125}
126
127AclManager *CollectionAclWidget::aclManager() const
128{
129 return mAclManager;
130}
131
132bool CollectionAclWidget::recursive() const
133{
134 return mRecursiveChk->isChecked();
135}
136
137void CollectionAclWidget::setEnableRecursiveCheckBox(bool enable)
138{
139 if (!enable) {
140 mRecursiveChk->setChecked(false);
141 }
142 mRecursiveChk->setEnabled(enable);
143}
144
145void CollectionAclWidget::slotRecursivePermissionChanged()
146{
147 mAclManager->setChanged(true);
148}
149
150#include "moc_collectionaclwidget.cpp"
QString i18nc(const char *context, 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 Mon Nov 4 2024 16:39:22 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.