Olivier SOLDANO | 6936119 | 2017-04-20 10:32:05 -0400 | [diff] [blame] | 1 | /************************************************************************** |
| 2 | * Copyright (C) 2015-2017 by Savoir-faire Linux * |
| 3 | * Author: Anthony LĂ©onard <anthony.leonard@savoirfairelinux.com> * |
| 4 | * Author: Olivier Soldano <olivier.soldano@savoirfairelinux.com> * |
| 5 | * * |
| 6 | * This program is free software; you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 3 of the License, or * |
| 9 | * (at your option) any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. * |
| 18 | **************************************************************************/ |
| 19 | |
| 20 | #include "contactrequestlistwidget.h" |
| 21 | |
| 22 | //LRC |
| 23 | #include "contactrequest.h" |
| 24 | #include "pendingcontactrequestmodel.h" |
| 25 | #include "itemdataroles.h" |
| 26 | |
| 27 | //CLIENT |
| 28 | #include "quickactcontactrequestwidget.h" |
| 29 | #include <QScrollBar> |
| 30 | #include <QAbstractItemView> |
| 31 | #include <QEvent> |
| 32 | |
| 33 | ContactRequestListWidget::ContactRequestListWidget(QWidget *parent) : |
| 34 | QTreeView(parent) |
| 35 | { |
| 36 | // for hover detection |
| 37 | setMouseTracking(true); |
| 38 | |
| 39 | //connects the entered signal to the creation and display of hovering widget |
| 40 | connect(this, &QAbstractItemView::entered, [this](const QModelIndex & index) { |
| 41 | //get current hovered row quick action widget |
| 42 | auto widget = indexWidget(index); |
| 43 | //build and add hovering quick action widget to row and display, if already built just display |
| 44 | if (!widget) { |
| 45 | QuickActContactRequestWidget* quickBtns = new QuickActContactRequestWidget(); |
| 46 | setIndexWidget(index, quickBtns); |
| 47 | connect(quickBtns, &QuickActContactRequestWidget::quickValidCRBtnClicked, |
| 48 | this, |
| 49 | [=](){ model()->data(index, static_cast<int>(Ring::Role::Object)).value<ContactRequest*>()->accept(); }); |
| 50 | |
| 51 | connect(quickBtns, &QuickActContactRequestWidget::quickMuteCRBtnClicked, |
| 52 | this, |
| 53 | [=](){ model()->data(index, static_cast<int>(Ring::Role::Object)).value<ContactRequest*>()->discard(); }); |
| 54 | |
| 55 | connect(quickBtns, &QuickActContactRequestWidget::quickBanCRBtnClicked, |
| 56 | this, |
| 57 | [=](){ model()->data(index, static_cast<int>(Ring::Role::Object)).value<ContactRequest*>()->block(); }); |
| 58 | } |
| 59 | else if (index.isValid()){ |
| 60 | indexWidget(index)->setVisible(true); |
| 61 | } |
| 62 | |
| 63 | //hide previously shown hover widget (if any) |
| 64 | if(hoveredCRIndex_.isValid() and indexWidget(hoveredCRIndex_)) |
| 65 | indexWidget(hoveredCRIndex_)->setVisible(false); |
| 66 | |
| 67 | //update current hovered index |
| 68 | hoveredCRIndex_ = index; |
| 69 | }); |
| 70 | } |
| 71 | |
| 72 | ContactRequestListWidget::~ContactRequestListWidget() |
| 73 | { |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | ContactRequestListWidget::setItemModel(QAbstractItemModel *model) |
| 78 | { |
| 79 | setModel(model); |
| 80 | |
| 81 | // Hide unused columns |
| 82 | for(int column = 1; column < model->columnCount(); column++){ |
| 83 | hideColumn(column); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | |
| 88 | void |
| 89 | ContactRequestListWidget::enterEvent(QEvent* event) |
| 90 | { |
| 91 | Q_UNUSED(event); |
| 92 | verticalScrollBar()->show(); |
| 93 | } |
| 94 | |
| 95 | void |
| 96 | ContactRequestListWidget::leaveEvent(QEvent* event) |
| 97 | { |
| 98 | Q_UNUSED(event); |
| 99 | |
| 100 | hoveredCRIndex_ = QModelIndex(); |
| 101 | verticalScrollBar()->hide(); |
| 102 | } |
| 103 | |
| 104 | bool |
| 105 | ContactRequestListWidget::eventFilter(QObject* watched, QEvent* event) |
| 106 | { |
| 107 | if (qobject_cast<QScrollBar*>(watched) && event->type() == QEvent::Enter) { |
| 108 | hoveredCRIndex_ = QModelIndex(); |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | return QObject::eventFilter(watched, event); |
| 113 | } |
| 114 | |
| 115 | |
| 116 | void |
| 117 | ContactRequestListWidget::drawRow(QPainter* painter, |
| 118 | const QStyleOptionViewItem& option, const QModelIndex& index) const |
| 119 | { |
| 120 | if(index == hoveredCRIndex_ && indexWidget(hoveredCRIndex_)) |
| 121 | indexWidget(index)->setVisible(true); |
| 122 | else if(indexWidget(index)) |
| 123 | indexWidget(index)->setVisible(false); |
| 124 | |
| 125 | QTreeView::drawRow(painter, option, index); |
| 126 | } |