blob: dd635f68d1f0fd2dcf4f696eb119c20cb1439206 [file] [log] [blame]
Nicolas Jager97a21b42015-12-03 16:55:45 -05001/***************************************************************************
Nicolas Jager540a5112016-01-15 15:54:34 -05002 * Copyright (C) 2015-2016 by Savoir-faire Linux *
Nicolas Jager97a21b42015-12-03 16:55:45 -05003 * 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 Jager41e4fcf2016-01-28 11:37:03 -050020#include <QEvent>
Nicolas Jager97a21b42015-12-03 16:55:45 -050021#include <QTreeWidgetItem>
Nicolas Jager540a5112016-01-15 15:54:34 -050022#include <QScrollBar>
Nicolas Jager97a21b42015-12-03 16:55:45 -050023
24#include "smartlistdelegate.h"
25#include "combar.h"
Nicolas Jager97a21b42015-12-03 16:55:45 -050026#include "smartlist.h"
27
28SmartList::SmartList(QWidget *parent) :
Nicolas Jager41e4fcf2016-01-28 11:37:03 -050029 QTreeView(parent)
Nicolas Jager97a21b42015-12-03 16:55:45 -050030{
Nicolas Jager540a5112016-01-15 15:54:34 -050031 verticalScrollBar()->hide();
Nicolas Jager41e4fcf2016-01-28 11:37:03 -050032
33 connect(this, &QAbstractItemView::entered, [this](const QModelIndex & index) {
Nicolas Jagere41bb582016-03-04 15:15:11 -050034 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 Jager41e4fcf2016-01-28 11:37:03 -050047 });
48
49 setVerticalScrollMode(ScrollPerPixel);
Nicolas Jager97a21b42015-12-03 16:55:45 -050050}
51
52SmartList::~SmartList()
53{
Nicolas Jagere41bb582016-03-04 15:15:11 -050054 reset();
Nicolas Jager97a21b42015-12-03 16:55:45 -050055}
56
57void
58SmartList::enterEvent(QEvent* event)
59{
60 Q_UNUSED(event);
Nicolas Jager540a5112016-01-15 15:54:34 -050061 verticalScrollBar()->show();
Nicolas Jager97a21b42015-12-03 16:55:45 -050062}
63
64void
65SmartList::leaveEvent(QEvent* event)
66{
67 Q_UNUSED(event);
68
Nicolas Jagere41bb582016-03-04 15:15:11 -050069 hoveredRow_ = QModelIndex();
Nicolas Jager540a5112016-01-15 15:54:34 -050070 verticalScrollBar()->hide();
Nicolas Jager97a21b42015-12-03 16:55:45 -050071}
72
73void
74SmartList::setSmartListItemDelegate(SmartListDelegate* delegate)
75{
Nicolas Jagere41bb582016-03-04 15:15:11 -050076 if (delegate) {
Nicolas Jager97a21b42015-12-03 16:55:45 -050077 setItemDelegate(delegate);
78 smartListDelegate_ = delegate;
Nicolas Jager41e4fcf2016-01-28 11:37:03 -050079 }
80}
81
82bool
83SmartList::eventFilter(QObject* watched, QEvent* event)
84{
85
86 if (qobject_cast<QScrollBar*>(watched) && event->type() == QEvent::Enter) {
Nicolas Jagere41bb582016-03-04 15:15:11 -050087 hoveredRow_ = QModelIndex();
Nicolas Jager41e4fcf2016-01-28 11:37:03 -050088 return true;
89 }
90
91 return QObject::eventFilter(watched, event);
92}
93
Nicolas Jagere3d23e62016-02-18 11:24:49 -050094
95void
Nicolas Jagere41bb582016-03-04 15:15:11 -050096SmartList::drawRow(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
Nicolas Jagere3d23e62016-02-18 11:24:49 -050097{
Nicolas Jagere41bb582016-03-04 15:15:11 -050098 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 Jagere3d23e62016-02-18 11:24:49 -0500104}