Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame^] | 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2018 by Savoir-faire Linux * |
| 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 "smartlistselectorbuttonnotifier.h" |
| 20 | |
| 21 | #include "lrcinstance.h" |
| 22 | #include "ringthemeutils.h" |
| 23 | |
| 24 | #include "api/conversationmodel.h" |
| 25 | |
| 26 | #include <QPainter> |
| 27 | |
| 28 | SmartlistSelectorButtonNotifier::SmartlistSelectorButtonNotifier(QWidget *parent) |
| 29 | : QPushButton(parent) |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | void SmartlistSelectorButtonNotifier::setTypeFilter(lrc::api::profile::Type filter) |
| 34 | { |
| 35 | typeFilter_ = filter; |
| 36 | } |
| 37 | |
| 38 | void |
| 39 | SmartlistSelectorButtonNotifier::paintEvent(QPaintEvent *event) |
| 40 | { |
| 41 | // nb: don't paint the 'button' here! |
| 42 | // QPushButton::paintEvent(event); |
| 43 | Q_UNUSED(event); |
| 44 | |
| 45 | QPainter painter(this); |
| 46 | painter.setRenderHint(QPainter::Antialiasing, true); |
| 47 | QFont font(painter.font()); |
| 48 | |
| 49 | int totalUnreadMessages = 0; |
| 50 | |
| 51 | using namespace lrc::api::profile; |
| 52 | switch (typeFilter_) { |
| 53 | case Type::RING: |
| 54 | { |
| 55 | auto convModel = LRCInstance::getCurrentConversationModel(); |
| 56 | auto ringConversations = convModel->getFilteredConversations(Type::RING); |
| 57 | std::for_each(ringConversations.begin(), ringConversations.end(), |
| 58 | [&totalUnreadMessages, convModel](const auto& conversation) { |
| 59 | totalUnreadMessages += convModel->getNumberOfUnreadMessagesFor(conversation.uid); |
| 60 | }); |
| 61 | break; |
| 62 | } |
| 63 | case Type::PENDING: |
| 64 | { |
| 65 | auto& accountInfo = LRCInstance::getCurrentAccountInfo(); |
| 66 | totalUnreadMessages = accountInfo.contactModel->pendingRequestCount(); |
| 67 | break; |
| 68 | } |
| 69 | default: |
| 70 | break; |
| 71 | } |
| 72 | |
| 73 | if (totalUnreadMessages) { |
| 74 | QString messageCountText = (totalUnreadMessages > 9) ? "9+" : QString::number(totalUnreadMessages); |
| 75 | qreal fontSize = messageCountText.count() > 1 ? 7 : 8; |
| 76 | font.setPointSize(fontSize); |
| 77 | |
| 78 | QRect buttonRect(this->rect()); |
| 79 | |
| 80 | // ellipse |
| 81 | QPainterPath ellipse; |
| 82 | qreal radius = buttonRect.width() / 2; |
| 83 | ellipse.addRoundedRect(buttonRect, radius, radius); |
| 84 | painter.fillPath(ellipse, RingTheme::notificationRed_); |
| 85 | |
| 86 | // text |
| 87 | painter.setPen(Qt::white); |
| 88 | painter.setOpacity(1); |
| 89 | painter.setFont(font); |
| 90 | buttonRect.setTop(buttonRect.top() - 2); |
| 91 | painter.drawText(buttonRect, Qt::AlignCenter, messageCountText); |
| 92 | } |
| 93 | } |