smartlistselectorbuttonnotifier.cpp 3.43 KB
Newer Older
1
/***************************************************************************
2
 * Copyright (C) 2019-2019 by Savoir-faire Linux                                *
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * Author: Andreas Traczyk <andreas.traczyk@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 "smartlistselectorbuttonnotifier.h"

#include "lrcinstance.h"
22
#include "utils.h"
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#include "ringthemeutils.h"

#include "api/conversationmodel.h"

#include <QPainter>

SmartlistSelectorButtonNotifier::SmartlistSelectorButtonNotifier(QWidget *parent)
    : QPushButton(parent)
{
}

void SmartlistSelectorButtonNotifier::setTypeFilter(lrc::api::profile::Type filter)
{
    typeFilter_ = filter;
}

void
SmartlistSelectorButtonNotifier::paintEvent(QPaintEvent *event)
{
    // nb: don't paint the 'button' here!
    // QPushButton::paintEvent(event);
    Q_UNUSED(event);

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);
    QFont font(painter.font());

    int totalUnreadMessages = 0;

    using namespace lrc::api::profile;
    switch (typeFilter_) {
    case Type::RING:
    {
        auto convModel = LRCInstance::getCurrentConversationModel();
        auto ringConversations = convModel->getFilteredConversations(Type::RING);
        std::for_each(ringConversations.begin(), ringConversations.end(),
            [&totalUnreadMessages, convModel](const auto& conversation) {
60
                totalUnreadMessages += conversation.unreadMessages;
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
            });
        break;
    }
    case Type::PENDING:
    {
        auto& accountInfo = LRCInstance::getCurrentAccountInfo();
        totalUnreadMessages = accountInfo.contactModel->pendingRequestCount();
        break;
    }
    default:
        break;
    }

    if (totalUnreadMessages) {
        QString messageCountText = (totalUnreadMessages > 9) ? "9+" : QString::number(totalUnreadMessages);
        qreal fontSize = messageCountText.count() > 1 ? 7 : 8;
        font.setPointSize(fontSize);

        QRect buttonRect(this->rect());

        // ellipse
        QPainterPath ellipse;
        qreal radius = buttonRect.width() / 2;
        ellipse.addRoundedRect(buttonRect, radius, radius);
        painter.fillPath(ellipse, RingTheme::notificationRed_);

        // text
        painter.setPen(Qt::white);
        painter.setOpacity(1);
        painter.setFont(font);
        buttonRect.setTop(buttonRect.top() - 2);
        painter.drawText(buttonRect, Qt::AlignCenter, messageCountText);
    }
}