Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 1 | /*************************************************************************** |
Sébastien Blin | 68abac9 | 2019-01-02 17:41:31 -0500 | [diff] [blame] | 2 | * Copyright (C) 2019-2019 by Savoir-faire Linux * |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 3 | * Author: Andreas Traczyk <andreas.traczyk@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 "accountitemdelegate.h" |
| 20 | |
| 21 | #include <QApplication> |
| 22 | #include <QTextDocument> |
| 23 | #include <QPixmap> |
| 24 | |
| 25 | #include "utils.h" |
| 26 | #include "accountlistmodel.h" |
| 27 | #include "ringthemeutils.h" |
Andreas Traczyk | 6ace34f | 2018-12-14 14:31:23 -0500 | [diff] [blame] | 28 | #include "lrcinstance.h" |
Andreas Traczyk | 46508b5 | 2019-01-04 11:49:24 -0500 | [diff] [blame] | 29 | #include "mainwindow.h" |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 30 | |
| 31 | #undef REGISTERED |
| 32 | |
| 33 | AccountItemDelegate::AccountItemDelegate(QObject *parent) |
| 34 | : QItemDelegate(parent) |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | void |
| 39 | AccountItemDelegate::paint(QPainter* painter, |
| 40 | const QStyleOptionViewItem& option, |
| 41 | const QModelIndex& index) const |
| 42 | { |
| 43 | QStyleOptionViewItem opt(option); |
| 44 | painter->setRenderHint(QPainter::Antialiasing); |
| 45 | |
| 46 | // Not having focus removes dotted lines around the item |
| 47 | if (opt.state & QStyle::State_HasFocus) { |
| 48 | opt.state.setFlag(QStyle::State_HasFocus, false); |
| 49 | } |
| 50 | |
| 51 | // First, we draw the control itself |
| 52 | QStyle* style = opt.widget ? opt.widget->style() : QApplication::style(); |
| 53 | style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget); |
| 54 | |
Andreas Traczyk | dc2703e | 2019-01-17 18:02:31 -0500 | [diff] [blame] | 55 | QColor presenceBorderColor = Qt::white; |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 56 | bool selected = false; |
| 57 | if (option.state & QStyle::State_Selected) { |
| 58 | selected = true; |
| 59 | opt.state ^= QStyle::State_Selected; |
| 60 | painter->fillRect(option.rect, RingTheme::smartlistSelection_); |
Andreas Traczyk | dc2703e | 2019-01-17 18:02:31 -0500 | [diff] [blame] | 61 | presenceBorderColor = RingTheme::smartlistSelection_; |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 62 | } |
| 63 | else if (option.state & QStyle::State_MouseOver) { |
| 64 | painter->fillRect(option.rect, RingTheme::smartlistHighlight_); |
Andreas Traczyk | dc2703e | 2019-01-17 18:02:31 -0500 | [diff] [blame] | 65 | presenceBorderColor = RingTheme::smartlistHighlight_; |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | QRect &rect = opt.rect; |
| 69 | |
Andreas Traczyk | 46508b5 | 2019-01-04 11:49:24 -0500 | [diff] [blame] | 70 | // define and set the two fonts |
| 71 | QFont fontPrimary = painter->font(); |
| 72 | QFont fontSecondary = painter->font(); |
| 73 | fontPrimary.setWeight(QFont::ExtraLight); |
| 74 | auto scalingRatio = MainWindow::instance().getCurrentScalingRatio(); |
| 75 | if (scalingRatio > 1.0) { |
| 76 | fontPrimary.setPointSize(fontSize_ - 1); |
| 77 | fontSecondary.setPointSize(fontSize_ - 2); |
| 78 | } else { |
| 79 | fontPrimary.setPointSize(fontSize_); |
| 80 | fontSecondary.setPointSize(fontSize_ - 1); |
| 81 | } |
Andreas Traczyk | 2965014 | 2019-01-03 20:33:56 -0500 | [diff] [blame] | 82 | |
Andreas Traczyk | 6ace34f | 2018-12-14 14:31:23 -0500 | [diff] [blame] | 83 | QPen pen(painter->pen()); |
| 84 | |
| 85 | // is it the add account row? |
| 86 | if (index.row() == LRCInstance::accountModel().getAccountList().size()) { |
| 87 | pen.setColor(RingTheme::lightBlack_); |
| 88 | painter->setPen(pen); |
Andreas Traczyk | 46508b5 | 2019-01-04 11:49:24 -0500 | [diff] [blame] | 89 | painter->setFont(fontPrimary); |
| 90 | QFontMetrics fontMetrics(fontPrimary); |
Andreas Traczyk | 6ace34f | 2018-12-14 14:31:23 -0500 | [diff] [blame] | 91 | painter->drawText(rect, Qt::AlignVCenter | Qt::AlignHCenter, tr("Add Account") + "+"); |
| 92 | return; |
| 93 | } |
| 94 | |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 95 | // Avatar drawing |
| 96 | opt.decorationSize = QSize(avatarSize_, avatarSize_); |
| 97 | opt.decorationPosition = QStyleOptionViewItem::Left; |
| 98 | opt.decorationAlignment = Qt::AlignCenter; |
| 99 | |
Andreas Traczyk | 2965014 | 2019-01-03 20:33:56 -0500 | [diff] [blame] | 100 | QRect rectAvatar( |
| 101 | leftPadding_ + rect.left(), |
| 102 | rect.top() + topPadding_, |
| 103 | avatarSize_, |
| 104 | avatarSize_ |
| 105 | ); |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 106 | drawDecoration(painter, opt, rectAvatar, |
| 107 | QPixmap::fromImage(index.data(AccountListModel::Role::Picture).value<QImage>()) |
| 108 | .scaled(avatarSize_, avatarSize_, Qt::KeepAspectRatio, Qt::SmoothTransformation)); |
| 109 | |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 110 | auto status = index.data(static_cast<int>(AccountListModel::Role::Status)).value<int>(); |
| 111 | auto isPresent = Utils::toEnum<lrc::api::account::Status>(status) == lrc::api::account::Status::REGISTERED; |
Andreas Traczyk | 1c1302c | 2018-12-31 12:28:16 -0500 | [diff] [blame] | 112 | // Presence indicator |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 113 | if (isPresent) { |
Andreas Traczyk | 1c1302c | 2018-12-31 12:28:16 -0500 | [diff] [blame] | 114 | QPainterPath outerCircle, innerCircle; |
| 115 | QPointF center(rectAvatar.right() - avatarSize_ / 6, (rectAvatar.bottom() - avatarSize_ / 6) + 1); |
| 116 | qreal outerCRadius = avatarSize_ / 6, innerCRadius = outerCRadius * 0.75; |
| 117 | outerCircle.addEllipse(center, outerCRadius, outerCRadius); |
| 118 | innerCircle.addEllipse(center, innerCRadius, innerCRadius); |
Andreas Traczyk | dc2703e | 2019-01-17 18:02:31 -0500 | [diff] [blame] | 119 | painter->fillPath(outerCircle, presenceBorderColor); |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 120 | painter->fillPath(innerCircle, RingTheme::presenceGreen_); |
| 121 | } |
| 122 | |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 123 | painter->setPen(pen); |
| 124 | |
Andreas Traczyk | 2965014 | 2019-01-03 20:33:56 -0500 | [diff] [blame] | 125 | QRect rectTexts(leftPadding_ + rect.left() + leftPadding_ + avatarSize_, |
| 126 | rect.top() + topPadding_ / 2, |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 127 | rect.width(), |
| 128 | rect.height() / 2); |
| 129 | |
| 130 | // The name is displayed at the avatar's right |
| 131 | QVariant name = index.data(static_cast<int>(AccountListModel::Role::Alias)); |
| 132 | if (name.isValid()) |
| 133 | { |
Andreas Traczyk | 46508b5 | 2019-01-04 11:49:24 -0500 | [diff] [blame] | 134 | fontPrimary.setItalic(false); |
| 135 | fontPrimary.setBold(false); |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 136 | pen.setColor(RingTheme::lightBlack_); |
| 137 | painter->setPen(pen); |
Andreas Traczyk | 46508b5 | 2019-01-04 11:49:24 -0500 | [diff] [blame] | 138 | painter->setFont(fontPrimary); |
| 139 | QFontMetrics fontMetrics(fontPrimary); |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 140 | QString nameStr = fontMetrics.elidedText(name.value<QString>(), Qt::ElideRight, |
Andreas Traczyk | 2965014 | 2019-01-03 20:33:56 -0500 | [diff] [blame] | 141 | rectTexts.width() - avatarSize_ - leftPadding_ - rightPadding_ * 2); |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 142 | painter->drawText(rectTexts, Qt::AlignVCenter | Qt::AlignLeft, nameStr); |
| 143 | } |
| 144 | |
Isa Nanic | f7e0792 | 2018-10-29 10:52:05 -0400 | [diff] [blame] | 145 | // Display the secondary ID under the name |
| 146 | |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 147 | QString idStr = index.data(static_cast<int>(AccountListModel::Role::Username)).value<QString>(); |
| 148 | if (idStr != name.toString()) { |
Andreas Traczyk | 46508b5 | 2019-01-04 11:49:24 -0500 | [diff] [blame] | 149 | fontSecondary.setItalic(false); |
| 150 | fontSecondary.setBold(false); |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 151 | pen.setColor(RingTheme::grey_); |
| 152 | painter->setPen(pen); |
Andreas Traczyk | 46508b5 | 2019-01-04 11:49:24 -0500 | [diff] [blame] | 153 | painter->setFont(fontSecondary); |
| 154 | QFontMetrics fontMetrics(fontSecondary); |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 155 | if (!idStr.isNull()) { |
| 156 | idStr = fontMetrics.elidedText(idStr, Qt::ElideRight, |
Andreas Traczyk | 2965014 | 2019-01-03 20:33:56 -0500 | [diff] [blame] | 157 | rectTexts.width() - avatarSize_ - leftPadding_ - rightPadding_ * 2); |
| 158 | painter->drawText(QRect(leftPadding_ + rect.left() + leftPadding_ + avatarSize_, |
| 159 | rect.top() + topPadding_ * 3, |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 160 | rect.width(), |
| 161 | rect.height() / 2), |
| 162 | Qt::AlignBottom | Qt::AlignLeft, idStr); |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | QSize AccountItemDelegate::sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const |
| 168 | { |
| 169 | QSize size = QItemDelegate::sizeHint(option, index); |
| 170 | size.setHeight(cellHeight_); |
| 171 | return size; |
| 172 | } |