File indexing completed on 2024-05-05 16:16:35
0001 /* 0002 SPDX-FileCopyrightText: 2013 Martin Klapetek <mklapetek@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.1-or-later 0005 */ 0006 0007 #include "kpeople_debug.h" 0008 #include "personactionsmodel_p.h" 0009 #include "persondata.h" 0010 #include "widgets/actions.h" 0011 #include <QAction> 0012 0013 namespace KPeople 0014 { 0015 class PersonActionsPrivate 0016 { 0017 public: 0018 PersonActionsPrivate() 0019 : person(nullptr) 0020 { 0021 } 0022 0023 QList<QAction *> actions; 0024 QString id; 0025 KPeople::PersonData *person; 0026 }; 0027 } 0028 0029 using namespace KPeople; 0030 0031 PersonActionsModel::PersonActionsModel(QObject *parent) 0032 : QAbstractListModel(parent) 0033 , d_ptr(new PersonActionsPrivate) 0034 { 0035 } 0036 0037 PersonActionsModel::~PersonActionsModel() 0038 { 0039 delete d_ptr; 0040 } 0041 0042 QHash<int, QByteArray> PersonActionsModel::roleNames() const 0043 { 0044 QHash<int, QByteArray> roles = QAbstractListModel::roleNames(); 0045 roles[IconNameRole] = "iconName"; 0046 roles[ActionRole] = "action"; 0047 roles[ActionTypeRole] = "actionType"; 0048 return roles; 0049 } 0050 0051 void PersonActionsModel::setPersonUri(const QString &id) 0052 { 0053 Q_D(PersonActions); 0054 0055 if (id == d->id) { 0056 return; 0057 } 0058 0059 delete d->person; 0060 d->id = id; 0061 0062 if (!id.isEmpty()) { 0063 d->person = new PersonData(id, this); 0064 connect(d->person, &PersonData::dataChanged, this, &PersonActionsModel::resetActions); 0065 0066 resetActions(); 0067 } else { 0068 beginResetModel(); 0069 d->actions.clear(); 0070 endResetModel(); 0071 } 0072 0073 Q_EMIT personChanged(); 0074 } 0075 0076 void PersonActionsModel::resetActions() 0077 { 0078 Q_D(PersonActions); 0079 0080 beginResetModel(); 0081 d->actions = KPeople::actionsForPerson(d->id, this); 0082 endResetModel(); 0083 } 0084 0085 QString PersonActionsModel::personUri() const 0086 { 0087 Q_D(const PersonActions); 0088 return d->id; 0089 } 0090 0091 QVariant PersonActionsModel::data(const QModelIndex &index, int role) const 0092 { 0093 Q_D(const PersonActions); 0094 0095 if (!index.isValid()) { 0096 return QVariant(); 0097 } 0098 0099 switch (role) { 0100 case Qt::DisplayRole: 0101 return d->actions[index.row()]->text(); 0102 case Qt::DecorationRole: 0103 return d->actions[index.row()]->icon(); 0104 case Qt::ToolTip: 0105 return d->actions[index.row()]->toolTip(); 0106 case IconNameRole: 0107 return d->actions[index.row()]->icon().name(); 0108 case ActionRole: 0109 return QVariant::fromValue<QObject *>(d->actions[index.row()]); 0110 case ActionTypeRole: 0111 return d->actions[index.row()]->property("actionType"); 0112 } 0113 0114 return QVariant(); 0115 } 0116 0117 int PersonActionsModel::rowCount(const QModelIndex &parent) const 0118 { 0119 Q_D(const PersonActions); 0120 0121 return parent.isValid() ? 0 : d->actions.size(); 0122 } 0123 0124 void PersonActionsModel::triggerAction(int row) const 0125 { 0126 Q_D(const PersonActions); 0127 if (d->actions.count() >= row) { 0128 qWarning() << "no action in row" << row << ". Actions available:" << d->actions.count(); 0129 return; 0130 } 0131 d->actions[row]->trigger(); 0132 } 0133 0134 QList<QAction *> PersonActionsModel::actions() const 0135 { 0136 Q_D(const PersonActions); 0137 return d->actions; 0138 } 0139 0140 #include "moc_personactionsmodel_p.cpp"