blob: cda019a7ad3262860e20317466c15acc877cf22b [file] [log] [blame]
Nicolas Jager97a21b42015-12-03 16:55:45 -05001/***************************************************************************
Anthony Léonard2fde81d2017-04-17 10:06:55 -04002 * Copyright (C) 2015-2017 by Savoir-faire Linux *
Nicolas Jager97a21b42015-12-03 16:55:45 -05003 * Author: Jäger Nicolas <nicolas.jager@savoirfairelinux.com> *
Olivier SOLDANO47aa97f2017-04-04 10:40:00 -04004 * Author: Anthony Léonard <anthony.leonard@savoirfairelinux.com> *
5 * Author: Olivier Soldano <olivier.soldano@savoirfairelinux.com> *
Nicolas Jager97a21b42015-12-03 16:55:45 -05006 * *
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 Jager41e4fcf2016-01-28 11:37:03 -050022#include <QEvent>
Nicolas Jager97a21b42015-12-03 16:55:45 -050023#include <QTreeWidgetItem>
Nicolas Jager540a5112016-01-15 15:54:34 -050024#include <QScrollBar>
Nicolas Jager97a21b42015-12-03 16:55:45 -050025
26#include "smartlistdelegate.h"
27#include "combar.h"
Nicolas Jager97a21b42015-12-03 16:55:45 -050028#include "smartlist.h"
29
30SmartList::SmartList(QWidget *parent) :
Nicolas Jager41e4fcf2016-01-28 11:37:03 -050031 QTreeView(parent)
Nicolas Jager97a21b42015-12-03 16:55:45 -050032{
Nicolas Jager540a5112016-01-15 15:54:34 -050033 verticalScrollBar()->hide();
Nicolas Jager41e4fcf2016-01-28 11:37:03 -050034
35 connect(this, &QAbstractItemView::entered, [this](const QModelIndex & index) {
Nicolas Jagere41bb582016-03-04 15:15:11 -050036 auto widget = indexWidget(index);
37 if (!widget) {
38 ComBar* bar = new ComBar();
39 setIndexWidget(index, bar);
40 connect(bar, &ComBar::btnVideoClicked, this, [=](){ emit btnVideoClicked(); });
41 }
42 else if (index.isValid())
43 indexWidget(index)->setVisible(true);
44
45 if(hoveredRow_.isValid() and indexWidget(hoveredRow_))
46 indexWidget(hoveredRow_)->setVisible(false);
47
48 hoveredRow_ = index;
Nicolas Jager41e4fcf2016-01-28 11:37:03 -050049 });
50
51 setVerticalScrollMode(ScrollPerPixel);
Nicolas Jager97a21b42015-12-03 16:55:45 -050052}
53
54SmartList::~SmartList()
55{
Nicolas Jagere41bb582016-03-04 15:15:11 -050056 reset();
Nicolas Jager97a21b42015-12-03 16:55:45 -050057}
58
59void
60SmartList::enterEvent(QEvent* event)
61{
62 Q_UNUSED(event);
Nicolas Jager540a5112016-01-15 15:54:34 -050063 verticalScrollBar()->show();
Nicolas Jager97a21b42015-12-03 16:55:45 -050064}
65
66void
67SmartList::leaveEvent(QEvent* event)
68{
69 Q_UNUSED(event);
70
Nicolas Jagere41bb582016-03-04 15:15:11 -050071 hoveredRow_ = QModelIndex();
Nicolas Jager540a5112016-01-15 15:54:34 -050072 verticalScrollBar()->hide();
Nicolas Jager97a21b42015-12-03 16:55:45 -050073}
74
75void
76SmartList::setSmartListItemDelegate(SmartListDelegate* delegate)
77{
Nicolas Jagere41bb582016-03-04 15:15:11 -050078 if (delegate) {
Nicolas Jager97a21b42015-12-03 16:55:45 -050079 setItemDelegate(delegate);
80 smartListDelegate_ = delegate;
Nicolas Jager41e4fcf2016-01-28 11:37:03 -050081 }
82}
83
84bool
85SmartList::eventFilter(QObject* watched, QEvent* event)
86{
87
88 if (qobject_cast<QScrollBar*>(watched) && event->type() == QEvent::Enter) {
Nicolas Jagere41bb582016-03-04 15:15:11 -050089 hoveredRow_ = QModelIndex();
Nicolas Jager41e4fcf2016-01-28 11:37:03 -050090 return true;
91 }
92
93 return QObject::eventFilter(watched, event);
94}
95
Nicolas Jagere3d23e62016-02-18 11:24:49 -050096
97void
Nicolas Jagere41bb582016-03-04 15:15:11 -050098SmartList::drawRow(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
Nicolas Jagere3d23e62016-02-18 11:24:49 -050099{
Nicolas Jagere41bb582016-03-04 15:15:11 -0500100 if(index == hoveredRow_ && indexWidget(hoveredRow_))
101 indexWidget(index)->setVisible(true);
102 else if(indexWidget(index))
103 indexWidget(index)->setVisible(false);
104
105 QTreeView::drawRow(painter, option, index);
Nicolas Jagere3d23e62016-02-18 11:24:49 -0500106}