]> git.sur5r.net Git - minitube/blob - src/playlistview.cpp
Imported Upstream version 2.2
[minitube] / src / playlistview.cpp
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 "playlistview.h"
22 #include "playlistmodel.h"
23 #include "playlistitemdelegate.h"
24 #include "painterutils.h"
25
26 PlaylistView::PlaylistView(QWidget *parent) : QListView(parent),
27     clickableAuthors(true) {
28     setItemDelegate(new PlaylistItemDelegate(this));
29     setSelectionMode(QAbstractItemView::ExtendedSelection);
30
31     setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Expanding);
32     setMinimumWidth(175);
33
34     // dragndrop
35     setDragEnabled(true);
36     setAcceptDrops(true);
37     setDropIndicatorShown(true);
38     setDragDropMode(QAbstractItemView::DragDrop);
39
40     // cosmetics
41     setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
42     setFrameShape(QFrame::NoFrame);
43     setAttribute(Qt::WA_MacShowFocusRect, false);
44     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
45     setUniformItemSizes(true);
46
47     connect(this, SIGNAL(entered(const QModelIndex &)),
48             SLOT(itemEntered(const QModelIndex &)));
49     setMouseTracking(true);
50 }
51
52 void PlaylistView::itemEntered(const QModelIndex &index) {
53     PlaylistModel *listModel = dynamic_cast<PlaylistModel *>(model());
54     if (listModel) listModel->setHoveredRow(index.row());
55 }
56
57 void PlaylistView::leaveEvent(QEvent * /* event */) {
58     PlaylistModel *listModel = dynamic_cast<PlaylistModel *>(model());
59     if (listModel) listModel->clearHover();
60 }
61
62 void PlaylistView::mouseMoveEvent(QMouseEvent *event) {
63     if (isHoveringThumbnail(event)) {
64         setCursor(Qt::PointingHandCursor);
65     } else if (isShowMoreItem(indexAt(event->pos()))) {
66         setCursor(Qt::PointingHandCursor);
67     } else if (isHoveringAuthor(event)) {
68         QMetaObject::invokeMethod(model(), "enterAuthorHover");
69         setCursor(Qt::PointingHandCursor);
70     } else {
71         QMetaObject::invokeMethod(model(), "exitAuthorHover");
72         unsetCursor();
73     }
74     QListView::mouseMoveEvent(event);
75 }
76
77 void PlaylistView::mousePressEvent(QMouseEvent *event) {
78     if (event->button() == Qt::LeftButton) {
79         if (isHoveringAuthor(event)) {
80             QMetaObject::invokeMethod(model(), "enterAuthorPressed");
81         }
82     }
83     QListView::mousePressEvent(event);
84 }
85
86 void PlaylistView::mouseReleaseEvent(QMouseEvent *event) {
87     if (event->button() == Qt::LeftButton) {
88         QMetaObject::invokeMethod(model(), "exitAuthorPressed");
89         const QModelIndex index =  indexAt(event->pos());
90         if (isHoveringThumbnail(event)) {
91             emit activated(index);
92             unsetCursor();
93         } else if (isHoveringAuthor(event)) {
94             emit authorPushed(index);
95         } else if (isShowMoreItem(index)) {
96             PlaylistModel *listModel = dynamic_cast<PlaylistModel *>(model());
97             listModel->searchMore();
98             unsetCursor();
99         }
100     }
101     QListView::mouseReleaseEvent(event);
102 }
103
104 bool PlaylistView::isHoveringAuthor(QMouseEvent *event) {
105     if (!clickableAuthors) return false;
106
107     const QModelIndex itemIndex = indexAt(event->pos());
108     const QRect itemRect = visualRect(itemIndex);
109     // qDebug() << " itemRect.x()" <<  itemRect.x();
110
111     PlaylistItemDelegate *delegate = dynamic_cast<PlaylistItemDelegate *>(itemDelegate());
112     if (!delegate) return false;
113
114     QRect rect = delegate->authorRect(itemIndex);
115
116     const int x = event->x() - itemRect.x() - rect.x();
117     const int y = event->y() - itemRect.y() - rect.y();
118     bool ret = x > 0 && x < rect.width() && y > 0 && y < rect.height();
119
120     return ret;
121 }
122
123 bool PlaylistView::isHoveringThumbnail(QMouseEvent *event) {
124     const QModelIndex index = indexAt(event->pos());
125     const QRect itemRect = visualRect(index);
126     static const QRect thumbRect(0, 0, 160, 90);
127     const int x = event->x() - itemRect.x() - thumbRect.x();
128     const int y = event->y() - itemRect.y() - thumbRect.y();
129     return x > 0 && x < thumbRect.width() && y > 0 && y < thumbRect.height();
130 }
131
132 bool PlaylistView::isShowMoreItem(const QModelIndex &index) {
133     return model()->rowCount() > 1 &&
134             model()->rowCount() == index.row() + 1;
135 }
136
137 void PlaylistView::paintEvent(QPaintEvent *event) {
138     QListView::paintEvent(event);
139 #ifndef Q_WS_X11
140     PainterUtils::topShadow(viewport());
141 #endif
142 }