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