Show webfinger if displayName is unknown for an author.
[larjonas-pumpa.git] / src / messagerecipients.cpp
blob6403526b4b5585cf16712fa630546e3f2d8c2d7f
1 /*
2 Copyright 2013-2015 Mats Sjöberg
4 This file is part of the Pumpa programme.
6 Pumpa is free software: you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 Pumpa is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Pumpa. If not, see <http://www.gnu.org/licenses/>.
20 #include "messagerecipients.h"
22 #include <QDebug>
24 //------------------------------------------------------------------------------
26 MessageRecipients::MessageRecipients(QWidget* parent) : QWidget(parent) {
27 m_layout = new QGridLayout;
28 m_layout->setColumnStretch(0, 10);
29 m_layout->setColumnStretch(1, 1);
30 m_layout->setContentsMargins(0, 0, 0, 0);
32 m_buttonMapper = new QSignalMapper(this);
33 connect(m_buttonMapper, SIGNAL(mapped(QObject*)),
34 this, SLOT(onRemoveClicked(QObject*)));
35 setLayout(m_layout);
38 //------------------------------------------------------------------------------
40 void MessageRecipients::addRecipient(QASObject* obj) {
41 if (m_widgets.contains(obj))
42 return;
44 QString text("<b>" + obj->displayName() + "</b>");
46 QASActor* actor = obj->asActor();
47 if (actor)
48 text += " (" + actor->webFinger() + ")";
50 QLabel* label = new QLabel(text);
51 QToolButton* button = new QToolButton;
52 button->setText(tr("-"));
53 connect(button, SIGNAL(clicked()), m_buttonMapper, SLOT(map()));
54 m_buttonMapper->setMapping(button, obj);
56 m_list.append(obj);
57 int row = m_layout->rowCount();
58 m_layout->addWidget(label, row, 0);
59 m_layout->addWidget(button, row, 1);
61 m_widgets.insert(obj, qMakePair(label, button));
64 //------------------------------------------------------------------------------
66 void MessageRecipients::removeRecipient(QASObject* obj) {
67 QPair<QLabel*, QToolButton*> widgets = m_widgets[obj];
68 m_layout->removeWidget(widgets.first);
69 m_layout->removeWidget(widgets.second);
70 widgets.first->deleteLater();
71 widgets.second->deleteLater();
72 m_list.removeAll(obj);
73 m_widgets.remove(obj);
76 //------------------------------------------------------------------------------
78 void MessageRecipients::clear() {
79 QLayoutItem* item;
80 while ((item = m_layout->takeAt(0)) != 0) {
81 if (dynamic_cast<QWidgetItem*>(item)) {
82 QWidget* w = item->widget();
83 delete w;
85 delete item;
87 m_list.clear();
88 m_widgets.clear();
91 //------------------------------------------------------------------------------
93 void MessageRecipients::onRemoveClicked(QObject* qObj) {
94 QASObject* obj = qobject_cast<QASObject*>(qObj);
95 if (!obj)
96 return;
98 removeRecipient(obj);