blob: 5899d844cc9d2fb4d028189a3e4dc1b93672c448 [file] [log] [blame]
Olivier SOLDANO5d4a1ff2017-05-08 13:12:47 -04001/**************************************************************************
2* Copyright (C) 2015-2017 by Savoir-faire Linux *
3* Author: Olivier Soldano <olivier.soldano@savoirfairelinux.com> *
4* *
5* This program is free software; you can redistribute it and/or modify *
6* it under the terms of the GNU General Public License as published by *
7* the Free Software Foundation; either version 3 of the License, or *
8* (at your option) any later version. *
9* *
10* This program is distributed in the hope that it will be useful, *
11* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13* GNU General Public License for more details. *
14* *
15* You should have received a copy of the GNU General Public License *
16* along with this program. If not, see <http://www.gnu.org/licenses/>. *
17**************************************************************************/
18
19#include "bannedcontactswidget.h"
20#include "ui_bannedcontactswidget.h"
21
22#include <QAbstractItemModel>
23#include <QPainter>
24
25#include "ringthemeutils.h"
26
27// LRC
28#include "account.h"
29#include "bannedcontactmodel.h"
30
31
32/* Widget */
33
34BannedContactsWidget::BannedContactsWidget(QWidget *parent) :
35 QWidget(parent),
36 ui(new Ui::BannedContactsWidget)
37{
38 ui->setupUi(this);
39 bannedItemDelegate_ = new BannedContactItemDelegate();
40 ui->bannedList->setItemDelegate(bannedItemDelegate_);
41
42 connect(ui->bannedList, &QAbstractItemView::clicked, this, &BannedContactsWidget::setCurrentContact);
43 connect(ui->bannedList, &QAbstractItemView::clicked, this, [this](){ui->debanStack->setCurrentWidget(ui->debanPage);});
44 connect(ui->debanButton, &QPushButton::clicked, this, &BannedContactsWidget::debanContact);
45}
46
47BannedContactsWidget::~BannedContactsWidget()
48{
49 delete ui;
50}
51
52void BannedContactsWidget::setAccount(Account *ac)
53{
54 account_ = ac;
55
56 // Configure banned list to display banned contacts
57 ui->bannedList->setModel(account_->bannedContactModel());
58
59 // deban page hidden when model changes
60 ui->debanStack->setCurrentWidget(ui->cleanPage);
61}
62
63void BannedContactsWidget::setCurrentContact(const QModelIndex index)
64{
65 ui->bannedIdLabel->setText(index.data().toString());
66 bannedContact_ = index.data(static_cast<int>(ContactMethod::Role::Object)).value<ContactMethod*>();
67 qDebug() << "banned contact method = " << bannedContact_;
68}
69
70void BannedContactsWidget::debanContact()
71{
72 if (bannedContact_){
73 account_->bannedContactModel()->remove(bannedContact_);
74 ui->debanStack->setCurrentWidget(ui->cleanPage);
75 } else {
76 qDebug() << "banned contact: contactMethod not set!";
77 }
78}
79
80
81/* List Item Delegate */
82
83BannedContactItemDelegate::BannedContactItemDelegate(QObject *parent):
84 QItemDelegate(parent)
85{
86}
87
88void BannedContactItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
89{
90 painter->setRenderHint(QPainter::Antialiasing);
91
92 QStyleOptionViewItem opt(option);
93
94 // Not having focus removes dotted lines around the item
95 if (opt.state & QStyle::State_HasFocus)
96 opt.state ^= QStyle::State_HasFocus;
97
98 // First, we draw the control itself
99 QStyle* style = opt.widget ? opt.widget->style() : QApplication::style();
100 style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget);
101
102 // Then, we print the text
103 QFont font(painter->font());
104 font.setPointSize(10);
105 font.setBold(true);
106 painter->setFont(font);
107
108 QFontMetrics fontMetrics(font);
109
110 QRect rectText(opt.rect);
111 rectText.setLeft(opt.rect.left() + dxText_);
112 rectText.setBottom(rectText.top() + dyText_ + fontMetrics.height()/2);
113 rectText.setTop(rectText.bottom() - fontMetrics.height());
114
115 QString text(index.data().toString());
116 text = fontMetrics.elidedText(text, Qt::ElideRight, rectText.width());
117
118 QPen pen(painter->pen());
119
120 pen.setColor(RingTheme::lightBlack_);
121 painter->setPen(pen);
122
123 painter->drawText(rectText,text);
124
125 // Draw separator when item is not selected
126 if (not (opt.state & QStyle::State_Selected)) {
127 QRect rect(opt.rect);
128 pen.setColor(RingTheme::lightGrey_);
129 painter->setPen(pen);
130 painter->drawLine(rect.left() + separatorYPadding_, rect.bottom(),
131 rect.right() - separatorYPadding_,
132 rect.bottom());
133 }
134}
135
136QSize BannedContactItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
137{
138 QSize size = QItemDelegate::sizeHint(option, index);
139 size.setHeight(cellHeight_);
140 return size;
141}