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