KPeople

personactionsmodel.cpp
1 /*
2  SPDX-FileCopyrightText: 2013 Martin Klapetek <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.1-or-later
5 */
6 
7 #include "kpeople_debug.h"
8 #include "personactionsmodel_p.h"
9 #include "persondata.h"
10 #include "widgets/actions.h"
11 #include <QAction>
12 
13 namespace KPeople
14 {
15 class PersonActionsPrivate
16 {
17 public:
18  PersonActionsPrivate()
19  : person(nullptr)
20  {
21  }
22 
23  QList<QAction *> actions;
24  QString id;
25  KPeople::PersonData *person;
26 };
27 }
28 
29 using namespace KPeople;
30 
31 PersonActionsModel::PersonActionsModel(QObject *parent)
32  : QAbstractListModel(parent)
33  , d_ptr(new PersonActionsPrivate)
34 {
35 }
36 
37 PersonActionsModel::~PersonActionsModel()
38 {
39  delete d_ptr;
40 }
41 
42 QHash<int, QByteArray> PersonActionsModel::roleNames() const
43 {
45  roles[IconNameRole] = "iconName";
46  roles[ActionRole] = "action";
47  roles[ActionTypeRole] = "actionType";
48  return roles;
49 }
50 
51 void 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 
76 void PersonActionsModel::resetActions()
77 {
78  Q_D(PersonActions);
79 
80  beginResetModel();
81  d->actions = KPeople::actionsForPerson(d->id, this);
82  endResetModel();
83 }
84 
85 QString PersonActionsModel::personUri() const
86 {
87  Q_D(const PersonActions);
88  return d->id;
89 }
90 
91 QVariant 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();
102  case Qt::DecorationRole:
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 
117 int PersonActionsModel::rowCount(const QModelIndex &parent) const
118 {
119  Q_D(const PersonActions);
120 
121  return parent.isValid() ? 0 : d->actions.size();
122 }
123 
124 void 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 
134 QList<QAction *> PersonActionsModel::actions() const
135 {
136  Q_D(const PersonActions);
137  return d->actions;
138 }
DisplayRole
Allows to query the information about a given person.
Definition: persondata.h:34
void dataChanged()
One of the contact sources has changed.
virtual QHash< int, QByteArray > roleNames() const const
bool isValid() const const
int row() const const
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sun Aug 14 2022 04:09:08 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.