blob: 21db00e2f101f64f53480855c4473c0d334e34e1 [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
Andreas Traczyke0a60b52018-07-10 18:16:15 -040030#include <ciso646>
31
Nicolas Jager97a21b42015-12-03 16:55:45 -050032SmartList::SmartList(QWidget *parent) :
Nicolas Jager41e4fcf2016-01-28 11:37:03 -050033 QTreeView(parent)
Nicolas Jager97a21b42015-12-03 16:55:45 -050034{
Nicolas Jager540a5112016-01-15 15:54:34 -050035 verticalScrollBar()->hide();
Nicolas Jager41e4fcf2016-01-28 11:37:03 -050036
37 connect(this, &QAbstractItemView::entered, [this](const QModelIndex & index) {
Nicolas Jagere41bb582016-03-04 15:15:11 -050038 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 Jager41e4fcf2016-01-28 11:37:03 -050051 });
52
53 setVerticalScrollMode(ScrollPerPixel);
Nicolas Jager97a21b42015-12-03 16:55:45 -050054}
55
56SmartList::~SmartList()
57{
Nicolas Jagere41bb582016-03-04 15:15:11 -050058 reset();
Nicolas Jager97a21b42015-12-03 16:55:45 -050059}
60
61void
62SmartList::enterEvent(QEvent* event)
63{
64 Q_UNUSED(event);
Nicolas Jager540a5112016-01-15 15:54:34 -050065 verticalScrollBar()->show();
Nicolas Jager97a21b42015-12-03 16:55:45 -050066}
67
68void
69SmartList::leaveEvent(QEvent* event)
70{
71 Q_UNUSED(event);
72
Nicolas Jagere41bb582016-03-04 15:15:11 -050073 hoveredRow_ = QModelIndex();
Nicolas Jager540a5112016-01-15 15:54:34 -050074 verticalScrollBar()->hide();
Nicolas Jager97a21b42015-12-03 16:55:45 -050075}
76
77void
78SmartList::setSmartListItemDelegate(SmartListDelegate* delegate)
79{
Nicolas Jagere41bb582016-03-04 15:15:11 -050080 if (delegate) {
Nicolas Jager97a21b42015-12-03 16:55:45 -050081 setItemDelegate(delegate);
82 smartListDelegate_ = delegate;
Nicolas Jager41e4fcf2016-01-28 11:37:03 -050083 }
84}
85
86bool
87SmartList::eventFilter(QObject* watched, QEvent* event)
88{
89
90 if (qobject_cast<QScrollBar*>(watched) && event->type() == QEvent::Enter) {
Nicolas Jagere41bb582016-03-04 15:15:11 -050091 hoveredRow_ = QModelIndex();
Nicolas Jager41e4fcf2016-01-28 11:37:03 -050092 return true;
93 }
94
95 return QObject::eventFilter(watched, event);
96}
97
Nicolas Jagere3d23e62016-02-18 11:24:49 -050098
99void
Nicolas Jagere41bb582016-03-04 15:15:11 -0500100SmartList::drawRow(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
Nicolas Jagere3d23e62016-02-18 11:24:49 -0500101{
Nicolas Jagere41bb582016-03-04 15:15:11 -0500102 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 Jagere3d23e62016-02-18 11:24:49 -0500108}