Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 1 | /*************************************************************************** |
Anthony Léonard | 2fde81d | 2017-04-17 10:06:55 -0400 | [diff] [blame] | 2 | * Copyright (C) 2015-2017 by Savoir-faire Linux * |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 3 | * Author: Jäger Nicolas <nicolas.jager@savoirfairelinux.com> * |
Olivier SOLDANO | 47aa97f | 2017-04-04 10:40:00 -0400 | [diff] [blame] | 4 | * Author: Anthony Léonard <anthony.leonard@savoirfairelinux.com> * |
| 5 | * Author: Olivier Soldano <olivier.soldano@savoirfairelinux.com> * |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 6 | * * |
| 7 | * This program is free software; you can redistribute it and/or modify * |
| 8 | * it under the terms of the GNU General Public License as published by * |
| 9 | * the Free Software Foundation; either version 3 of the License, or * |
| 10 | * (at your option) any later version. * |
| 11 | * * |
| 12 | * This program is distributed in the hope that it will be useful, * |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 15 | * GNU General Public License for more details. * |
| 16 | * * |
| 17 | * You should have received a copy of the GNU General Public License * |
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. * |
| 19 | **************************************************************************/ |
| 20 | |
| 21 | #include <QStyledItemDelegate> |
Nicolas Jager | 41e4fcf | 2016-01-28 11:37:03 -0500 | [diff] [blame] | 22 | #include <QEvent> |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 23 | #include <QTreeWidgetItem> |
Nicolas Jager | 540a511 | 2016-01-15 15:54:34 -0500 | [diff] [blame] | 24 | #include <QScrollBar> |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 25 | |
| 26 | #include "smartlistdelegate.h" |
| 27 | #include "combar.h" |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 28 | #include "smartlist.h" |
| 29 | |
Andreas Traczyk | e0a60b5 | 2018-07-10 18:16:15 -0400 | [diff] [blame] | 30 | #include <ciso646> |
| 31 | |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 32 | SmartList::SmartList(QWidget *parent) : |
Nicolas Jager | 41e4fcf | 2016-01-28 11:37:03 -0500 | [diff] [blame] | 33 | QTreeView(parent) |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 34 | { |
Nicolas Jager | 540a511 | 2016-01-15 15:54:34 -0500 | [diff] [blame] | 35 | verticalScrollBar()->hide(); |
Nicolas Jager | 41e4fcf | 2016-01-28 11:37:03 -0500 | [diff] [blame] | 36 | |
| 37 | connect(this, &QAbstractItemView::entered, [this](const QModelIndex & index) { |
Nicolas Jager | e41bb58 | 2016-03-04 15:15:11 -0500 | [diff] [blame] | 38 | auto widget = indexWidget(index); |
| 39 | if (!widget) { |
| 40 | ComBar* bar = new ComBar(); |
| 41 | setIndexWidget(index, bar); |
| 42 | connect(bar, &ComBar::btnVideoClicked, this, [=](){ emit btnVideoClicked(); }); |
| 43 | } |
| 44 | else if (index.isValid()) |
| 45 | indexWidget(index)->setVisible(true); |
| 46 | |
| 47 | if(hoveredRow_.isValid() and indexWidget(hoveredRow_)) |
| 48 | indexWidget(hoveredRow_)->setVisible(false); |
| 49 | |
| 50 | hoveredRow_ = index; |
Nicolas Jager | 41e4fcf | 2016-01-28 11:37:03 -0500 | [diff] [blame] | 51 | }); |
| 52 | |
| 53 | setVerticalScrollMode(ScrollPerPixel); |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | SmartList::~SmartList() |
| 57 | { |
Nicolas Jager | e41bb58 | 2016-03-04 15:15:11 -0500 | [diff] [blame] | 58 | reset(); |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void |
| 62 | SmartList::enterEvent(QEvent* event) |
| 63 | { |
| 64 | Q_UNUSED(event); |
Nicolas Jager | 540a511 | 2016-01-15 15:54:34 -0500 | [diff] [blame] | 65 | verticalScrollBar()->show(); |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void |
| 69 | SmartList::leaveEvent(QEvent* event) |
| 70 | { |
| 71 | Q_UNUSED(event); |
| 72 | |
Nicolas Jager | e41bb58 | 2016-03-04 15:15:11 -0500 | [diff] [blame] | 73 | hoveredRow_ = QModelIndex(); |
Nicolas Jager | 540a511 | 2016-01-15 15:54:34 -0500 | [diff] [blame] | 74 | verticalScrollBar()->hide(); |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void |
| 78 | SmartList::setSmartListItemDelegate(SmartListDelegate* delegate) |
| 79 | { |
Nicolas Jager | e41bb58 | 2016-03-04 15:15:11 -0500 | [diff] [blame] | 80 | if (delegate) { |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 81 | setItemDelegate(delegate); |
| 82 | smartListDelegate_ = delegate; |
Nicolas Jager | 41e4fcf | 2016-01-28 11:37:03 -0500 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | bool |
| 87 | SmartList::eventFilter(QObject* watched, QEvent* event) |
| 88 | { |
| 89 | |
| 90 | if (qobject_cast<QScrollBar*>(watched) && event->type() == QEvent::Enter) { |
Nicolas Jager | e41bb58 | 2016-03-04 15:15:11 -0500 | [diff] [blame] | 91 | hoveredRow_ = QModelIndex(); |
Nicolas Jager | 41e4fcf | 2016-01-28 11:37:03 -0500 | [diff] [blame] | 92 | return true; |
| 93 | } |
| 94 | |
| 95 | return QObject::eventFilter(watched, event); |
| 96 | } |
| 97 | |
Nicolas Jager | e3d23e6 | 2016-02-18 11:24:49 -0500 | [diff] [blame] | 98 | |
| 99 | void |
Nicolas Jager | e41bb58 | 2016-03-04 15:15:11 -0500 | [diff] [blame] | 100 | SmartList::drawRow(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const |
Nicolas Jager | e3d23e6 | 2016-02-18 11:24:49 -0500 | [diff] [blame] | 101 | { |
Nicolas Jager | e41bb58 | 2016-03-04 15:15:11 -0500 | [diff] [blame] | 102 | if(index == hoveredRow_ && indexWidget(hoveredRow_)) |
| 103 | indexWidget(index)->setVisible(true); |
| 104 | else if(indexWidget(index)) |
| 105 | indexWidget(index)->setVisible(false); |
| 106 | |
| 107 | QTreeView::drawRow(painter, option, index); |
Nicolas Jager | e3d23e6 | 2016-02-18 11:24:49 -0500 | [diff] [blame] | 108 | } |