Pimcommon

configureactivitieswidget.cpp
1/*
2 SPDX-FileCopyrightText: 2024 Laurent Montel <montel.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "configureactivitieswidget.h"
8#include <KLocalizedString>
9#include <PlasmaActivities/ActivitiesModel>
10#include <QCheckBox>
11#include <QItemDelegate>
12#include <QLabel>
13#include <QListView>
14#include <QVBoxLayout>
15
16using namespace Qt::Literals::StringLiterals;
17using namespace PimCommonActivities;
18namespace
19{
20// Code stolen from plasma-vault
21class CheckboxDelegate : public QItemDelegate
22{
23public:
24 CheckboxDelegate(QObject *parent)
25 : QItemDelegate(parent)
26 {
27 }
28
29 void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
30 {
31 auto wholeRect = option.rect;
32
33 // Drawing the checkbox
34 auto checkRect = wholeRect;
35 checkRect.setWidth(checkRect.height());
36 drawCheck(painter, option, checkRect, option.state & QStyle::State_Selected ? Qt::Checked : Qt::Unchecked);
37
38 // Drawing the text
39 auto textRect = wholeRect;
40 textRect.setLeft(textRect.left() + 8 + textRect.height());
41 drawDisplay(painter, option, textRect, index.data(Qt::DisplayRole).toString());
42 }
43
44 [[nodiscard]] QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
45 {
46 Q_UNUSED(option);
47 Q_UNUSED(index);
48 return QSize(100, 22);
49 }
50};
51}
52
53ConfigureActivitiesWidget::ConfigureActivitiesWidget(QWidget *parent)
54 : QWidget{parent}
55 , mListView(new QListView(this))
56 , mEnableActivitiesSupport(new QCheckBox(i18nc("@option:check", "Limit to the selected activities:"), this))
57{
58 auto mainLayout = new QVBoxLayout(this);
59 mainLayout->setObjectName("mainLayout"_L1);
60
61 auto label = new QLabel(i18nc("@label:textbox",
62 "If you limit this account to activity, it will be shown only when you are in those activities. Furthermore, when "
63 "you switch to an activity it should not be available in, it will automatically be hidden."),
64 this);
65 label->setWordWrap(true);
66 label->setObjectName("label"_L1);
67 mainLayout->addWidget(label);
68
69 mEnableActivitiesSupport->setObjectName("mEnableActivitiesSupport"_L1);
70 mainLayout->addWidget(mEnableActivitiesSupport);
71 mEnableActivitiesSupport->setChecked(false);
72
73 mListView->setObjectName("mListView"_L1);
74 mListView->setModel(new KActivities::ActivitiesModel(this));
75 mListView->setItemDelegate(new CheckboxDelegate(this));
76 mainLayout->addWidget(mListView);
77 mListView->setSelectionMode(QAbstractItemView::MultiSelection);
78 mListView->setEnabled(false);
79 connect(mEnableActivitiesSupport, &QCheckBox::clicked, mListView, &QListView::setEnabled);
80}
81
82ConfigureActivitiesWidget::~ConfigureActivitiesWidget() = default;
83
84ActivitiesBaseManager::ActivitySettings ConfigureActivitiesWidget::activitiesSettings() const
85{
86 if (!mEnableActivitiesSupport->isChecked()) {
87 return {};
88 }
89 const auto selection = mListView->selectionModel();
90 const auto selected = selection->selectedIndexes();
91 QStringList selectedActivities;
92 selectedActivities.reserve(selected.count());
93 for (const auto &selectedIndex : selected) {
94 selectedActivities << selectedIndex.data(KActivities::ActivitiesModel::ActivityId).toString();
95 }
96 const ActivitiesBaseManager::ActivitySettings activities{selectedActivities, true};
97 return activities;
98}
99
100void ConfigureActivitiesWidget::setActivitiesSettings(const ActivitiesBaseManager::ActivitySettings &activitySettings)
101{
102 auto model = mListView->model();
103 auto selection = mListView->selectionModel();
104 selection->clearSelection();
105
106 bool listIsEmpty{activitySettings.activities.isEmpty()};
107 mListView->setEnabled(activitySettings.enabled);
108 mEnableActivitiesSupport->setChecked(activitySettings.enabled);
109 bool hasFoundActivities = false;
110 if (!listIsEmpty) {
111 for (int row = 0; row < model->rowCount(); ++row) {
112 const auto index = model->index(row, 0);
113 const auto activity = model->data(index, KActivities::ActivitiesModel::ActivityId).toString();
114
115 if (activitySettings.contains(activity)) {
116 selection->select(index, QItemSelectionModel::Select);
117 hasFoundActivities = true;
118 }
119 }
120 // Make sure to disable it if we don't find activities => it was removed
121 if (!hasFoundActivities) {
122 mListView->setEnabled(false);
123 mEnableActivitiesSupport->setChecked(false);
124 }
125 }
126}
127
128#include "moc_configureactivitieswidget.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString label(StandardShortcut id)
bool isChecked() const const
void clicked(bool checked)
QAbstractItemModel * model() const const
QItemSelectionModel * selectionModel() const const
pointer data()
bool isEmpty() const const
void reserve(qsizetype size)
QVariant data(int role) const const
DisplayRole
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QString toString() const const
void setEnabled(bool)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:14:35 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.