KPeople

personactionsmodel.cpp
1/*
2 SPDX-FileCopyrightText: 2013 Martin Klapetek <mklapetek@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.1-or-later
5*/
6
7#include "actions.h"
8#include "kpeople_debug.h"
9#include "personactionsmodel_p.h"
10#include "persondata.h"
11#include <QAction>
12
13namespace KPeople
14{
15class PersonActionsPrivate
16{
17public:
18 PersonActionsPrivate()
19 : person(nullptr)
20 {
21 }
22
23 QList<QAction *> actions;
24 QString id;
25 KPeople::PersonData *person;
26};
27}
28
29using namespace KPeople;
30
31PersonActionsModel::PersonActionsModel(QObject *parent)
32 : QAbstractListModel(parent)
33 , d_ptr(new PersonActionsPrivate)
34{
35}
36
37PersonActionsModel::~PersonActionsModel()
38{
39 delete d_ptr;
40}
41
42QHash<int, QByteArray> PersonActionsModel::roleNames() const
43{
45 roles[IconNameRole] = "iconName";
46 roles[ActionRole] = "action";
47 roles[ActionTypeRole] = "actionType";
48 return roles;
49}
50
51void PersonActionsModel::setPersonUri(const QString &id)
52{
53 Q_D(PersonActions);
54
55 if (id == d->id) {
56 return;
57 }
58
59 delete d->person;
60 d->id = id;
61
62 if (!id.isEmpty()) {
63 d->person = new PersonData(id, this);
64 connect(d->person, &PersonData::dataChanged, this, &PersonActionsModel::resetActions);
65
66 resetActions();
67 } else {
68 beginResetModel();
69 d->actions.clear();
70 endResetModel();
71 }
72
73 Q_EMIT personChanged();
74}
75
76void PersonActionsModel::resetActions()
77{
78 Q_D(PersonActions);
79
80 beginResetModel();
81 d->actions = KPeople::actionsForPerson(d->id, this);
82 endResetModel();
83}
84
85QString PersonActionsModel::personUri() const
86{
87 Q_D(const PersonActions);
88 return d->id;
89}
90
91QVariant PersonActionsModel::data(const QModelIndex &index, int role) const
92{
93 Q_D(const PersonActions);
94
95 if (!index.isValid()) {
96 return QVariant();
97 }
98
99 switch (role) {
100 case Qt::DisplayRole:
101 return d->actions[index.row()]->text();
103 return d->actions[index.row()]->icon();
104 case Qt::ToolTip:
105 return d->actions[index.row()]->toolTip();
106 case IconNameRole:
107 return d->actions[index.row()]->icon().name();
108 case ActionRole:
109 return QVariant::fromValue<QObject *>(d->actions[index.row()]);
110 case ActionTypeRole:
111 return d->actions[index.row()]->property("actionType");
112 }
113
114 return QVariant();
115}
116
117int PersonActionsModel::rowCount(const QModelIndex &parent) const
118{
119 Q_D(const PersonActions);
120
121 return parent.isValid() ? 0 : d->actions.size();
122}
123
124void PersonActionsModel::triggerAction(int row) const
125{
126 Q_D(const PersonActions);
127 if (d->actions.count() >= row) {
128 qWarning() << "no action in row" << row << ". Actions available:" << d->actions.count();
129 return;
130 }
131 d->actions[row]->trigger();
132}
133
134QList<QAction *> PersonActionsModel::actions() const
135{
136 Q_D(const PersonActions);
137 return d->actions;
138}
139
140#include "moc_personactionsmodel_p.cpp"
Allows to query the information about a given person.
Definition persondata.h:35
void dataChanged()
One of the contact sources has changed.
virtual QHash< int, QByteArray > roleNames() const const
bool isValid() const const
int row() const const
DisplayRole
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:17:45 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.