KPeople

persondetailsview.cpp
1/*
2 SPDX-FileCopyrightText: 2011 Martin Klapetek <mklapetek@kde.org>
3 SPDX-FileCopyrightText: 2013 David Edmundson <davidedmundsonk@kde.org>
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
27namespace KPeople
28{
29class PersonDetailsViewPrivate
30{
31public:
32 PersonData *m_person;
33 Ui::PersonDetailsPresentation *m_personDetailsPresentation;
34 QWidget *m_mainWidget;
36};
37}
38
39using namespace KPeople;
40
41class CoreFieldsPlugin : public AbstractFieldWidgetFactory
42{
43public:
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
50private:
51 QString m_field;
52};
53
54CoreFieldsPlugin::CoreFieldsPlugin(const QString &field)
55 : m_field(field)
56{
57}
58
59CoreFieldsPlugin::~CoreFieldsPlugin()
60{
61}
62
63QString CoreFieldsPlugin::label() const
64{
65#ifdef __GNUC__
66#warning fixme, should be made user-visible somehow
67#endif
68 return m_field;
69}
70
71int CoreFieldsPlugin::sortWeight() const
72{
73 return 1;
74}
75
76QWidget *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
90PersonDetailsView::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 QList<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
130PersonDetailsView::~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
155void 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}
191
192#include "moc_persondetailsview.cpp"
Allows to query the information about a given person.
Definition persondata.h:35
Q_SCRIPTABLE QVariant contactCustomProperty(const QString &key) const
Use PersonDetailsView to integrate a person's information in the GUI.
void setPerson(PersonData *person)
Specifies the person for which the details will be displayed.
static QList< KPluginMetaData > findPlugins(const QString &directory, std::function< bool(const KPluginMetaData &)> filter={}, KPluginMetaDataOptions options={})
QPixmap pixmap(QWindow *window, const QSize &size, Mode mode, State state) const const
QIcon fromTheme(const QString &name)
virtual int indexOf(const QLayoutItem *layoutItem) const const
virtual void setSpacing(int)
virtual QLayoutItem * takeAt(int index)=0
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)
QObject * parent() const const
QPixmap scaled(const QSize &size, Qt::AspectRatioMode aspectRatioMode, Qt::TransformationMode transformMode) const const
bool isEmpty() const const
KeepAspectRatio
QString toString() const const
QWidget(QWidget *parent, Qt::WindowFlags f)
QLayout * layout() const const
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:03:42 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.