Olivier SOLDANO | 5d4a1ff | 2017-05-08 13:12:47 -0400 | [diff] [blame] | 1 | /************************************************************************** |
| 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 | |
Andreas Traczyk | e0a60b5 | 2018-07-10 18:16:15 -0400 | [diff] [blame] | 31 | #include <ciso646> |
Olivier SOLDANO | 5d4a1ff | 2017-05-08 13:12:47 -0400 | [diff] [blame] | 32 | |
| 33 | /* Widget */ |
| 34 | |
| 35 | BannedContactsWidget::BannedContactsWidget(QWidget *parent) : |
| 36 | QWidget(parent), |
| 37 | ui(new Ui::BannedContactsWidget) |
| 38 | { |
| 39 | ui->setupUi(this); |
| 40 | bannedItemDelegate_ = new BannedContactItemDelegate(); |
| 41 | ui->bannedList->setItemDelegate(bannedItemDelegate_); |
| 42 | |
| 43 | connect(ui->bannedList, &QAbstractItemView::clicked, this, &BannedContactsWidget::setCurrentContact); |
| 44 | connect(ui->bannedList, &QAbstractItemView::clicked, this, [this](){ui->debanStack->setCurrentWidget(ui->debanPage);}); |
| 45 | connect(ui->debanButton, &QPushButton::clicked, this, &BannedContactsWidget::debanContact); |
| 46 | } |
| 47 | |
| 48 | BannedContactsWidget::~BannedContactsWidget() |
| 49 | { |
| 50 | delete ui; |
| 51 | } |
| 52 | |
| 53 | void BannedContactsWidget::setAccount(Account *ac) |
| 54 | { |
| 55 | account_ = ac; |
| 56 | |
| 57 | // Configure banned list to display banned contacts |
| 58 | ui->bannedList->setModel(account_->bannedContactModel()); |
| 59 | |
| 60 | // deban page hidden when model changes |
| 61 | ui->debanStack->setCurrentWidget(ui->cleanPage); |
| 62 | } |
| 63 | |
| 64 | void BannedContactsWidget::setCurrentContact(const QModelIndex index) |
| 65 | { |
| 66 | ui->bannedIdLabel->setText(index.data().toString()); |
| 67 | bannedContact_ = index.data(static_cast<int>(ContactMethod::Role::Object)).value<ContactMethod*>(); |
| 68 | qDebug() << "banned contact method = " << bannedContact_; |
| 69 | } |
| 70 | |
| 71 | void BannedContactsWidget::debanContact() |
| 72 | { |
| 73 | if (bannedContact_){ |
| 74 | account_->bannedContactModel()->remove(bannedContact_); |
| 75 | ui->debanStack->setCurrentWidget(ui->cleanPage); |
| 76 | } else { |
| 77 | qDebug() << "banned contact: contactMethod not set!"; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | |
| 82 | /* List Item Delegate */ |
| 83 | |
| 84 | BannedContactItemDelegate::BannedContactItemDelegate(QObject *parent): |
| 85 | QItemDelegate(parent) |
| 86 | { |
| 87 | } |
| 88 | |
| 89 | void BannedContactItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const |
| 90 | { |
| 91 | painter->setRenderHint(QPainter::Antialiasing); |
| 92 | |
| 93 | QStyleOptionViewItem opt(option); |
| 94 | |
| 95 | // Not having focus removes dotted lines around the item |
| 96 | if (opt.state & QStyle::State_HasFocus) |
| 97 | opt.state ^= QStyle::State_HasFocus; |
| 98 | |
| 99 | // First, we draw the control itself |
| 100 | QStyle* style = opt.widget ? opt.widget->style() : QApplication::style(); |
| 101 | style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget); |
| 102 | |
| 103 | // Then, we print the text |
| 104 | QFont font(painter->font()); |
| 105 | font.setPointSize(10); |
| 106 | font.setBold(true); |
| 107 | painter->setFont(font); |
| 108 | |
| 109 | QFontMetrics fontMetrics(font); |
| 110 | |
| 111 | QRect rectText(opt.rect); |
| 112 | rectText.setLeft(opt.rect.left() + dxText_); |
| 113 | rectText.setBottom(rectText.top() + dyText_ + fontMetrics.height()/2); |
| 114 | rectText.setTop(rectText.bottom() - fontMetrics.height()); |
| 115 | |
| 116 | QString text(index.data().toString()); |
| 117 | text = fontMetrics.elidedText(text, Qt::ElideRight, rectText.width()); |
| 118 | |
| 119 | QPen pen(painter->pen()); |
| 120 | |
| 121 | pen.setColor(RingTheme::lightBlack_); |
| 122 | painter->setPen(pen); |
| 123 | |
| 124 | painter->drawText(rectText,text); |
| 125 | |
| 126 | // Draw separator when item is not selected |
| 127 | if (not (opt.state & QStyle::State_Selected)) { |
| 128 | QRect rect(opt.rect); |
| 129 | pen.setColor(RingTheme::lightGrey_); |
| 130 | painter->setPen(pen); |
| 131 | painter->drawLine(rect.left() + separatorYPadding_, rect.bottom(), |
| 132 | rect.right() - separatorYPadding_, |
| 133 | rect.bottom()); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | QSize BannedContactItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const |
| 138 | { |
| 139 | QSize size = QItemDelegate::sizeHint(option, index); |
| 140 | size.setHeight(cellHeight_); |
| 141 | return size; |
| 142 | } |