blob: 4accf0504d6c7bb2d1b7dd648f0235c472863ca2 [file] [log] [blame]
Andreas Traczykb8b13ba2018-08-21 16:30:16 -04001/***************************************************************************
2 * Copyright (C) 2015-2017 by Savoir-faire Linux *
3 * Author: Jäger Nicolas <nicolas.jager@savoirfairelinux.com> *
4 * Author: Anthony Léonard <anthony.leonard@savoirfairelinux.com> *
5 * Author: Olivier Soldano <olivier.soldano@savoirfairelinux.com> *
6 * Author: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com> *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 3 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
20 **************************************************************************/
21
22#include "smartlistview.h"
23
24#include "invitebuttonswidget.h"
25#include "smartlistmodel.h"
26#include "utils.h"
27
28#include <QScrollBar>
29#include <QMouseEvent>
30#include <QDebug>
31
32#include <ciso646>
33
34SmartListView::SmartListView(QWidget *parent) :
35 QTreeView(parent)
36{
37 verticalScrollBar()->setEnabled(true);
38 setVerticalScrollMode(ScrollPerPixel);
39 verticalScrollBar()->setStyleSheet("QScrollBar:vertical { width: 0px; }");
40 setDragEnabled(true);
41 setAcceptDrops(true);
42 setDropIndicatorShown(true);
43 setSelectionMode(QAbstractItemView::SingleSelection);
44 setMouseTracking(true);
45 installEventFilter(this);
46
47 QObject::connect(this, &QAbstractItemView::entered,
48 [this](const QModelIndex & index) {
49 auto type = Utils::toEnum<lrc::api::profile::Type>(
50 index.data(static_cast<int>(SmartListModel::Role::ContactType)).value<int>()
51 );
52 if (type == lrc::api::profile::Type::PENDING) {
53 auto widget = indexWidget(index);
54 if (!widget) {
55 InviteButtonsWidget* buttons = new InviteButtonsWidget();
56 setIndexWidget(index, buttons);
57
58 QObject::connect(buttons, &InviteButtonsWidget::btnAcceptInviteClicked, this,
59 [this, index]() {
60 hoveredRow_ = QModelIndex();
61 emit btnAcceptInviteClicked(index);
62 });
63 QObject::connect(buttons, &InviteButtonsWidget::btnIgnoreInviteClicked, this,
64 [this, index]() {
65 hoveredRow_ = QModelIndex();
66 emit btnIgnoreInviteClicked(index);
67 });
68 QObject::connect(buttons, &InviteButtonsWidget::btnBlockInviteClicked, this,
69 [this, index]() {
70 hoveredRow_ = QModelIndex();
71 emit btnBlockInviteClicked(index);
72 });
73 }
74 else {
75 widget->show();
76 }
77
78 if (hoveredRow_.isValid() && indexWidget(hoveredRow_)) {
79 indexWidget(hoveredRow_)->hide();
80 }
81
82 hoveredRow_ = index;
83 }
84 });
85}
86
87SmartListView::~SmartListView()
88{
89 reset();
90}
91
92void
93SmartListView::enterEvent(QEvent* event)
94{
95 Q_UNUSED(event);
96}
97
98void
99SmartListView::leaveEvent(QEvent* event)
100{
101 Q_UNUSED(event);
102 hoveredRow_ = QModelIndex();
103}
104
105void
106SmartListView::mousePressEvent(QMouseEvent *event)
107{
108 if (event->button() == Qt::RightButton) {
109 emit customContextMenuRequested(event->pos());
110 }
111 else {
112 QTreeView::mousePressEvent(event);
113 }
114}
115
116bool
117SmartListView::eventFilter(QObject* watched, QEvent* event)
118{
119 auto index = this->indexAt(static_cast<QMouseEvent*>(event)->pos());
120 if (!index.isValid()) {
121 hoveredRow_ = QModelIndex();
122 }
123 if (qobject_cast<QScrollBar*>(watched) && event->type() == QEvent::Enter) {
124 hoveredRow_ = QModelIndex();
125 return true;
126 }
127 return QObject::eventFilter(watched, event);
128}
129
130void
Andreas Traczykb8b13ba2018-08-21 16:30:16 -0400131SmartListView::drawRow(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
132{
133 if (index == hoveredRow_ && indexWidget(hoveredRow_)) {
134 indexWidget(index)->setVisible(true);
Andreas Traczyk43c08232018-10-31 13:42:09 -0400135 } else if (indexWidget(index)) {
136 auto type = Utils::toEnum<lrc::api::profile::Type>(
137 index.data(static_cast<int>(SmartListModel::Role::ContactType)).value<int>()
138 );
139 if (type == lrc::api::profile::Type::PENDING) {
140 indexWidget(index)->setVisible(false);
141 }
Andreas Traczykb8b13ba2018-08-21 16:30:16 -0400142 }
143 QTreeView::drawRow(painter, option, index);
144}