From f95f7f60fd43c17e4e009be9351fa368d87024f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anthony=20L=C3=A9onard?= <anthony.leonard@savoirfairelinux.com> Date: Tue, 4 Apr 2017 11:01:51 -0400 Subject: [PATCH] add delegate for pending contact requests This change adds an item delegate used to display every elements in the pending contact requests list. The displayed view is consistent with the existing smart list view. It shows the RingID of the sender and its picture. This last one is currently the default picture but is expected to become the one embedded in the payload if any. Change-Id: Idc7afb5c6bfcda7bce67dfd042a28f9789506815 Reviewed-by: Olivier Soldano <olivier.soldano@savoirfairelinux.com> --- RingWinClient.pro | 6 +- callwidget.cpp | 3 + contactrequestitemdelegate.cpp | 101 +++++++++++++++++++++++++++++++++ contactrequestitemdelegate.h | 43 ++++++++++++++ stylesheet.css | 6 +- 5 files changed, 155 insertions(+), 4 deletions(-) create mode 100644 contactrequestitemdelegate.cpp create mode 100644 contactrequestitemdelegate.h diff --git a/RingWinClient.pro b/RingWinClient.pro index 8ab4a34..d397160 100644 --- a/RingWinClient.pro +++ b/RingWinClient.pro @@ -74,7 +74,8 @@ SOURCES += main.cpp\ photoboothdialog.cpp \ sendcontactrequestwidget.cpp \ currentaccountwidget.cpp \ - contactrequestwidget.cpp + contactrequestwidget.cpp \ + contactrequestitemdelegate.cpp HEADERS += mainwindow.h \ callwidget.h \ @@ -112,7 +113,8 @@ HEADERS += mainwindow.h \ photoboothdialog.h \ sendcontactrequestwidget.h \ currentaccountwidget.h \ - contactrequestwidget.h + contactrequestwidget.h \ + contactrequestitemdelegate.h contains(DEFINES, URI_PROTOCOL) { HEADERS += shmclient.h diff --git a/callwidget.cpp b/callwidget.cpp index cb10747..0920293 100644 --- a/callwidget.cpp +++ b/callwidget.cpp @@ -58,6 +58,7 @@ #include "imdelegate.h" #include "pixbufmanipulator.h" #include "settingskey.h" +#include "contactrequestitemdelegate.h" #include "profilemodel.h" #include "profile.h" @@ -152,6 +153,8 @@ CallWidget::CallWidget(QWidget* parent) : connect(AccountModel::instance().userSelectionModel(), &QItemSelectionModel::currentChanged, this, &CallWidget::selectedAccountChanged); + ui->contactReqList->setItemDelegate(new ContactRequestItemDelegate()); + // It needs to be called manually once to initialize the ui with the account selected at start. // The second argument (previous) is set to an invalid QModelIndex as it is the first selection. selectedAccountChanged(AccountModel::instance().userSelectionModel()->currentIndex(), QModelIndex()); diff --git a/contactrequestitemdelegate.cpp b/contactrequestitemdelegate.cpp new file mode 100644 index 0000000..15ee10a --- /dev/null +++ b/contactrequestitemdelegate.cpp @@ -0,0 +1,101 @@ +/*************************************************************************** + * Copyright (C) 2017 by Savoir-faire Linux * + * Author: Anthony Léonard <anthony.leonard@savoirfairelinux.com> * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see <http://www.gnu.org/licenses/>. * + ***************************************************************************/ + +#include "contactrequestitemdelegate.h" +#include "ringthemeutils.h" + +#include "accountmodel.h" +#include "pendingcontactrequestmodel.h" +#include "contactrequest.h" + +#include <QPainter> +#include <QApplication> + +ContactRequestItemDelegate::ContactRequestItemDelegate(QObject* parent) : + QItemDelegate(parent) +{} + +void +ContactRequestItemDelegate::paint(QPainter* painter + , const QStyleOptionViewItem& option + , const QModelIndex& index + ) const +{ + painter->setRenderHint(QPainter::Antialiasing); + + QStyleOptionViewItem opt(option); + + // Not having focus removes dotted lines around the item + if (opt.state & QStyle::State_HasFocus) + opt.state ^= QStyle::State_HasFocus; + + // First, we draw the control itself + QStyle* style = opt.widget ? opt.widget->style() : QApplication::style(); + style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget); + + // Then, we print the text + QFont font(painter->font()); + font.setPointSize(10); + font.setBold(true); + painter->setFont(font); + + QFontMetrics fontMetrics(font); + + QRect rectText(opt.rect); + rectText.setLeft(opt.rect.left() + dxText_); + rectText.setTop(opt.rect.top() + dyText_); + rectText.setBottom(rectText.top() + fontMetrics.height()); + + QString text(index.data().toString()); + text = fontMetrics.elidedText(text, Qt::ElideRight, rectText.width()); + + QPen pen(painter->pen()); + + pen.setColor(RingTheme::lightBlack_); + painter->setPen(pen); + + painter->drawText(rectText,text); + + // Draw a picture + // TODO: Draw the incoming CR picture if part of the payload + QRect rectPic(opt.rect.left() + dxImage_, opt.rect.top() + dyImage_, sizeImage_, sizeImage_); + drawDecoration(painter, opt, rectPic, + QPixmap::fromImage(QImage(":/images/user/btn-default-userpic.svg").scaled(QSize(sizeImage_, sizeImage_), + Qt::KeepAspectRatio, + Qt::SmoothTransformation))); + + // Draw separator when item is not selected + if (not (opt.state & QStyle::State_Selected)) { + QRect rect(opt.rect); + pen.setColor(RingTheme::lightGrey_); + painter->setPen(pen); + painter->drawLine(rect.left() + separatorYPadding_, rect.bottom(), + rect.right() - separatorYPadding_, + rect.bottom()); + } +} + +QSize +ContactRequestItemDelegate::sizeHint(const QStyleOptionViewItem& option + , const QModelIndex& index + ) const +{ + QSize size = QItemDelegate::sizeHint(option, index); + size.setHeight(cellHeight_); + return size; +} diff --git a/contactrequestitemdelegate.h b/contactrequestitemdelegate.h new file mode 100644 index 0000000..2d059e3 --- /dev/null +++ b/contactrequestitemdelegate.h @@ -0,0 +1,43 @@ +/*************************************************************************** + * Copyright (C) 2017 by Savoir-faire Linux * + * Author: Anthony Léonard <anthony.leonard@savoirfairelinux.com> * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see <http://www.gnu.org/licenses/>. * + ***************************************************************************/ + +#pragma once + +#include <QObject> +#include <QItemDelegate> + +class ContactRequestItemDelegate : public QItemDelegate +{ +public: + ContactRequestItemDelegate(QObject* parent = 0); + +protected: + void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const; + QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const; + +private: + constexpr static int cellHeight_ = 60; + constexpr static int sizeImage_ = 48; + constexpr static int dxImage_ = 16; + constexpr static int dyImage_ = 6; + + constexpr static int dxText_ = dxImage_ + sizeImage_ + 12; + constexpr static int dyText_ = 13; + + constexpr static int separatorYPadding_ = 20; +}; diff --git a/stylesheet.css b/stylesheet.css index 9bb04c9..52ff700 100644 --- a/stylesheet.css +++ b/stylesheet.css @@ -111,12 +111,14 @@ QListView#contactReqList border: none; } -SmartList::item:selected, QListView#accountView::item:selected, QListView#contactView::item:selected{ +SmartList::item:selected, QListView#accountView::item:selected, QListView#contactView::item:selected, +QListView#contactReqList::item:selected{ background-color: rgba(220, 220, 220, 255); border: none; } -SmartList::item:hover, QListView#accountView::item:hover, QListView#contactView::item:hover{ +SmartList::item:hover, QListView#accountView::item:hover, QListView#contactView::item:hover, +QListView#contactReqList::item:hover{ background-color: rgba(242, 242, 242, 255); } -- GitLab