1 /* $BEGIN_LICENSE
2 
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5 
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10 
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with Minitube.  If not, see <http://www.gnu.org/licenses/>.
18 
19 $END_LICENSE */
20 
21 #include "downloadlistview.h"
22 #include "downloadmodel.h"
23 #include "playlistitemdelegate.h"
24 #include "painterutils.h"
25 
DownloadListView(QWidget * parent)26 DownloadListView::DownloadListView(QWidget *parent) : QListView(parent) {
27 
28 }
29 
leaveEvent(QEvent *)30 void DownloadListView::leaveEvent(QEvent * /* event */) {
31     DownloadModel *downloadModel = qobject_cast<DownloadModel *>(model());
32     if (downloadModel) downloadModel->clearHover();
33 }
34 
mouseMoveEvent(QMouseEvent * event)35 void DownloadListView::mouseMoveEvent(QMouseEvent *event) {
36     // qDebug() << "DownloadListView::mouseMoveEvent" << event->pos();
37 
38     QListView::mouseMoveEvent(event);
39 
40     if (isHoveringPlayIcon(event)) {
41         QMetaObject::invokeMethod(model(), "enterPlayIconHover");
42     } else {
43         QMetaObject::invokeMethod(model(), "exitPlayIconHover");
44     }
45 
46 }
47 
mousePressEvent(QMouseEvent * event)48 void DownloadListView::mousePressEvent(QMouseEvent *event) {
49     if (event->button() == Qt::LeftButton
50         && isHoveringPlayIcon(event)) {
51         QMetaObject::invokeMethod(model(), "enterPlayIconPressed");
52     } else {
53         QListView::mousePressEvent(event);
54     }
55 }
56 
mouseReleaseEvent(QMouseEvent * event)57 void DownloadListView::mouseReleaseEvent(QMouseEvent *event) {
58     if (event->button() == Qt::LeftButton) {
59         QMetaObject::invokeMethod(model(), "exitPlayIconPressed");
60         if (isHoveringPlayIcon(event))
61             emit downloadButtonPushed(indexAt(event->pos()));
62     } else {
63         QListView::mousePressEvent(event);
64     }
65 }
66 
isHoveringPlayIcon(QMouseEvent * event)67 bool DownloadListView::isHoveringPlayIcon(QMouseEvent *event) {
68     const QModelIndex itemIndex = indexAt(event->pos());
69     const QRect itemRect = visualRect(itemIndex);
70     // qDebug() << " itemRect.x()" <<  itemRect.x();
71 
72     PlaylistItemDelegate *delegate = qobject_cast<PlaylistItemDelegate *>(itemDelegate());
73     if (!delegate) return false;
74 
75     QRect buttonRect = delegate->downloadButtonRect(itemRect);
76 
77     const int x = event->x() - itemRect.x() - buttonRect.x();
78     const int y = event->y() - itemRect.y() - buttonRect.y();
79     return x > 0 && x < buttonRect.width() && y > 0 && y < buttonRect.height();
80 }
81 
paintEvent(QPaintEvent * event)82 void DownloadListView::paintEvent(QPaintEvent *event) {
83     QListView::paintEvent(event);
84     // PainterUtils::topShadow(viewport());
85 }
86