smartlistview.cpp 5.23 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 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 60 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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
/***************************************************************************
 * Copyright (C) 2015-2017 by Savoir-faire Linux                           *
 * Author: Jäger Nicolas <nicolas.jager@savoirfairelinux.com>              *
 * Author: Anthony Léonard <anthony.leonard@savoirfairelinux.com>          *
 * Author: Olivier Soldano <olivier.soldano@savoirfairelinux.com>          *
 * 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 "smartlistview.h"

#include "invitebuttonswidget.h"
#include "smartlistmodel.h"
#include "utils.h"

#include <QScrollBar>
#include <QMouseEvent>
#include <QDebug>

#include <ciso646>

SmartListView::SmartListView(QWidget *parent) :
    QTreeView(parent)
{
    verticalScrollBar()->setEnabled(true);
    setVerticalScrollMode(ScrollPerPixel);
    verticalScrollBar()->setStyleSheet("QScrollBar:vertical { width: 0px; }");
    setDragEnabled(true);
    setAcceptDrops(true);
    setDropIndicatorShown(true);
    setSelectionMode(QAbstractItemView::SingleSelection);
    setMouseTracking(true);
    installEventFilter(this);

    QObject::connect(this, &QAbstractItemView::entered,
        [this](const QModelIndex & index) {
            auto type = Utils::toEnum<lrc::api::profile::Type>(
                index.data(static_cast<int>(SmartListModel::Role::ContactType)).value<int>()
                );
            if (type == lrc::api::profile::Type::PENDING) {
                auto widget = indexWidget(index);
                if (!widget) {
                    InviteButtonsWidget* buttons = new InviteButtonsWidget();
                    setIndexWidget(index, buttons);

                    QObject::connect(buttons, &InviteButtonsWidget::btnAcceptInviteClicked, this,
                        [this, index]() {
                            hoveredRow_ = QModelIndex();
                            emit btnAcceptInviteClicked(index);
                        });
                    QObject::connect(buttons, &InviteButtonsWidget::btnIgnoreInviteClicked, this,
                        [this, index]() {
                            hoveredRow_ = QModelIndex();
                            emit btnIgnoreInviteClicked(index);
                        });
                    QObject::connect(buttons, &InviteButtonsWidget::btnBlockInviteClicked, this,
                        [this, index]() {
                            hoveredRow_ = QModelIndex();
                            emit btnBlockInviteClicked(index);
                        });
                }
                else {
                    widget->show();
                }

                if (hoveredRow_.isValid() && indexWidget(hoveredRow_)) {
                    indexWidget(hoveredRow_)->hide();
                }

                hoveredRow_ = index;
            }
        });
}

SmartListView::~SmartListView()
{
    reset();
}

void
SmartListView::enterEvent(QEvent* event)
{
    Q_UNUSED(event);
}

void
SmartListView::leaveEvent(QEvent* event)
{
    Q_UNUSED(event);
    hoveredRow_ = QModelIndex();
}

void
SmartListView::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::RightButton) {
        emit customContextMenuRequested(event->pos());
    }
    else {
        QTreeView::mousePressEvent(event);
    }
}

bool
SmartListView::eventFilter(QObject* watched, QEvent* event)
{
    auto index = this->indexAt(static_cast<QMouseEvent*>(event)->pos());
    if (!index.isValid()) {
        hoveredRow_ = QModelIndex();
    }
    if (qobject_cast<QScrollBar*>(watched) && event->type() == QEvent::Enter) {
        hoveredRow_ = QModelIndex();
        return true;
    }
    return QObject::eventFilter(watched, event);
}

void
SmartListView::drawRow(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
    if (index == hoveredRow_ && indexWidget(hoveredRow_)) {
        indexWidget(index)->setVisible(true);
135 136 137 138 139 140 141
    } else if (indexWidget(index)) {
        auto type = Utils::toEnum<lrc::api::profile::Type>(
            index.data(static_cast<int>(SmartListModel::Role::ContactType)).value<int>()
            );
        if (type == lrc::api::profile::Type::PENDING) {
            indexWidget(index)->setVisible(false);
        }
142 143 144
    }
    QTreeView::drawRow(painter, option, index);
}