KPeople

persondetailsview.cpp
1 /*
2  SPDX-FileCopyrightText: 2011 Martin Klapetek <[email protected]>
3  SPDX-FileCopyrightText: 2013 David Edmundson <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.1-or-later
6 */
7 
8 #include "persondetailsview.h"
9 
10 #include "kpeople_widgets_debug.h"
11 
12 #include "abstractfieldwidgetfactory.h"
13 #include "emaildetailswidget.h"
14 #include "global.h"
15 
16 #include <KPluginFactory>
17 #include <KPluginMetaData>
18 
19 #include <QFormLayout>
20 #include <QIcon>
21 #include <QLabel>
22 #include <QList>
23 #include <QVBoxLayout>
24 
25 #include "ui_person-details-presentation.h"
26 
27 namespace KPeople
28 {
29 class PersonDetailsViewPrivate
30 {
31 public:
32  PersonData *m_person;
33  Ui::PersonDetailsPresentation *m_personDetailsPresentation;
34  QWidget *m_mainWidget;
36 };
37 }
38 
39 using namespace KPeople;
40 
41 class CoreFieldsPlugin : public AbstractFieldWidgetFactory
42 {
43 public:
44  CoreFieldsPlugin(const QString &field);
45  ~CoreFieldsPlugin() override;
46  QString label() const override;
47  int sortWeight() const override;
48  QWidget *createDetailsWidget(const PersonData &person, QWidget *parent) const override;
49 
50 private:
51  QString m_field;
52 };
53 
54 CoreFieldsPlugin::CoreFieldsPlugin(const QString &field)
55  : m_field(field)
56 {
57 }
58 
59 CoreFieldsPlugin::~CoreFieldsPlugin()
60 {
61 }
62 
63 QString CoreFieldsPlugin::label() const
64 {
65 #ifdef __GNUC__
66 #warning fixme, should be made user-visible somehow
67 #endif
68  return m_field;
69 }
70 
71 int CoreFieldsPlugin::sortWeight() const
72 {
73  return 1;
74 }
75 
76 QWidget *CoreFieldsPlugin::createDetailsWidget(const PersonData &person, QWidget *parent) const
77 {
78  // we have a plugin specific for e-mails.
79  if (m_field == QLatin1String("email")) {
80  return nullptr;
81  }
82 
83  QString text = person.contactCustomProperty(m_field).toString();
84  if (text.isEmpty()) {
85  return nullptr;
86  }
87  return new QLabel(text, parent);
88 }
89 
90 PersonDetailsView::PersonDetailsView(QWidget *parent)
91  : QWidget(parent)
92  , d_ptr(new PersonDetailsViewPrivate())
93 {
95 
96  auto *layout = new QVBoxLayout(this);
97  d->m_mainWidget = new QWidget(this);
98  d->m_person = nullptr;
99 
100  QWidget *details = new QWidget();
101  d->m_personDetailsPresentation = new Ui::PersonDetailsPresentation();
102  d->m_personDetailsPresentation->setupUi(details);
103  layout->addWidget(details);
104  layout->addWidget(d->m_mainWidget);
105  layout->addItem(new QSpacerItem(1, 1, QSizePolicy::Fixed, QSizePolicy::Expanding));
106 
107  // create plugins
108 #ifdef __GNUC__
109 #warning figure out a way to list properties
110 #endif
111  const QStringList fields{QStringLiteral("name"), QStringLiteral("all-email")};
112  for (const QString &field : fields) {
113  d->m_plugins << new CoreFieldsPlugin(field);
114  }
115 
116  d->m_plugins << new EmailFieldsPlugin();
117 
118  // load every KPeopleWidgets Plugin
119  const QVector<KPluginMetaData> personPluginList = KPluginMetaData::findPlugins(QStringLiteral("kpeople/widgets"));
120 
121  for (const KPluginMetaData &data : personPluginList) {
122  auto fieldWidgetFactoryResult = KPluginFactory::instantiatePlugin<AbstractFieldWidgetFactory>(data);
123  Q_ASSERT_X(fieldWidgetFactoryResult, Q_FUNC_INFO, qPrintable(fieldWidgetFactoryResult.errorText));
124  d->m_plugins << fieldWidgetFactoryResult.plugin;
125  }
126 
127  // TODO Sort plugins
128 }
129 
130 PersonDetailsView::~PersonDetailsView()
131 {
132  delete d_ptr;
133 }
134 
136 {
138  if (d->m_person) {
139  disconnect(d->m_person, SIGNAL(dataChanged()), this, SLOT(reload()));
140  }
141 
142  d->m_person = person;
143  connect(d->m_person, SIGNAL(dataChanged()), this, SLOT(reload()));
144  reload();
145 }
146 
147 // void PersonDetailsView::setPersonsModel(PersonsModel *model)
148 // {
149 // Q_D(PersonDetailsView);
150 // Q_FOREACH (AbstractPersonDetailsWidget *detailsWidget, d->m_detailWidgets) {
151 // detailsWidget->setPersonsModel(model);
152 // }
153 // }
154 
155 void PersonDetailsView::reload()
156 {
158 
159  // replace the entire main widget
160  int layoutIndex = layout()->indexOf(d->m_mainWidget);
161  layout()->takeAt(layoutIndex);
162  d->m_mainWidget->deleteLater();
163  d->m_mainWidget = new QWidget(this);
164  dynamic_cast<QVBoxLayout *>(layout())->insertWidget(layoutIndex, d->m_mainWidget);
165 
166  QFormLayout *layout = new QFormLayout(d->m_mainWidget);
167  layout->setSpacing(4);
168 
169  // update header information
170  // FIXME - possibly split this out into a new class with a nice setPerson method
171 
172  QPixmap avatar = d->m_person->photo();
173 
174  d->m_personDetailsPresentation->avatarPixmapLabel->setPixmap(avatar.scaled(96, 96, Qt::KeepAspectRatio)); // FIXME
175  d->m_personDetailsPresentation->presencePixmapLabel->setPixmap(QIcon::fromTheme(d->m_person->presenceIconName()).pixmap(32, 32)); // FIXME
176  d->m_personDetailsPresentation->nameLabel->setText(d->m_person->name());
177 
178  for (AbstractFieldWidgetFactory *widgetFactory : std::as_const(d->m_plugins)) {
179  const QString label = widgetFactory->label() + QLatin1Char(':');
180  QWidget *widget = widgetFactory->createDetailsWidget(d->m_person->personUri(), this);
181 
182  if (widget) {
183  QFont font = widget->font();
184  font.setBold(true);
185  widget->setFont(font);
186  QLabel *widgetLabel = new QLabel(label, this);
187  layout->addRow(widgetLabel, widget);
188  }
189  }
190 }
Use PersonDetailsView to integrate a person&#39;s information in the GUI.
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
Allows to query the information about a given person.
Definition: persondata.h:34
void setBold(bool enable)
void setSpacing(int spacing)
QString label(StandardShortcut id)
bool isEmpty() const const
Q_SCRIPTABLE QVariant contactCustomProperty(const QString &key) const
Definition: persondata.cpp:145
QPixmap scaled(int width, int height, Qt::AspectRatioMode aspectRatioMode, Qt::TransformationMode transformMode) const const
void addRow(QWidget *label, QWidget *field)
void setPerson(PersonData *person)
Specifies the person for which the details will be displayed.
KeepAspectRatio
Q_D(Todo)
static QVector< KPluginMetaData > findPlugins(const QString &directory, std::function< bool(const KPluginMetaData &)> filter={})
QIcon fromTheme(const QString &name)
void dataChanged()
One of the contact sources has changed.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu Jan 27 2022 22:42:01 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.