-
Notifications
You must be signed in to change notification settings - Fork 1
/
playlistview.cpp
123 lines (111 loc) · 3.55 KB
/
playlistview.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "playlistview.h"
#include <QMenu>
#include <QMimeData>
#include <QModelIndex>
#include <QMouseEvent>
#include <QProgressDialog>
#include <QContextMenuEvent>
#include <QDebug>
#include "playlistmodel.h"
PlayListView::PlayListView(QWidget *parent) : QTableView(parent)
{
}
void PlayListView::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasUrls())
event->acceptProposedAction();
}
void PlayListView::dropEvent(QDropEvent *event)
{
PlayListModel * playlist = dynamic_cast<PlayListModel*>(model());
if (playlist) {
QList<QUrl> urls = event->mimeData()->urls();
event->acceptProposedAction();
playlist->appendUrls(urls);
} else {
qWarning() << this << "Incompatiable model for drag and drop event";
}
}
void PlayListView::contextMenuEvent(QContextMenuEvent * event)
{
QModelIndex index = indexAt(event->pos());
if (index.isValid()) {
QMenu *menu = new QMenu(this);
menu->addAction(QString("show file"));
menu->exec(QCursor::pos());
QTableView::contextMenuEvent(event);
} else {
event->ignore();
}
}
void PlayListView::keyPressEvent(QKeyEvent *event) {
if (state() == QAbstractItemView::EditingState) {
QTableView::keyPressEvent(event);
return;
}
if (event->key() == Qt::Key_Backspace || event->key() == Qt::Key_Delete) {
QModelIndexList indexes = selectionModel()->selection().indexes();
QSet<int> itr_set;
for (int i = 0; i < indexes.count(); ++i) {
QModelIndex index = indexes.at(i);
itr_set.insert(index.row());
}
QList<int> itr_list = itr_set.values();
std::sort(itr_list.begin(), itr_list.end());
QListIterator<int> itr_list_i(itr_list);
itr_list_i.toBack();
while (itr_list_i.hasPrevious()) {
model()->removeRow(itr_list_i.previous());
}
event->accept();
} else if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) {
QModelIndexList selection = selectionModel()->selection().indexes();
if (!selection.isEmpty()) {
QModelIndex index = selection.first();
if (index.isValid()) {
PlayListModel * playlist = dynamic_cast<PlayListModel*>(model());
if (playlist) {
playlist->clickedItem(index);
} else {
qWarning() << this << "Incompatiable model for drag and drop event";
}
}
}
event->accept();
} else if (event->key() == Qt::Key_Left
|| event->key() == Qt::Key_Right
|| event->key() == Qt::Key_L) {
event->ignore();
} else {
QTableView::keyPressEvent(event);
}
}
void PlayListView::mouseReleaseEvent(QMouseEvent * event)
{
if (event->modifiers() & Qt::AltModifier) {
QModelIndex index = indexAt(event->pos());
if (index.isValid()) {
edit(index);
event->accept();
} else {
event->ignore();
}
} else {
QTableView::mousePressEvent(event);
}
}
void PlayListView::mouseDoubleClickEvent(QMouseEvent * event)
{
QModelIndex index = indexAt(event->pos());
if (index.isValid()) {
PlayListModel * playlist = dynamic_cast<PlayListModel*>(model());
if (playlist) {
playlist->clickedItem(index);
} else {
qWarning() << this << "Incompatiable model for drag and drop event";
}
event->accept();
} else {
event->ignore();
}
}