forked from mpc-qt/mpc-qt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfavoriteswindow.cpp
188 lines (155 loc) · 5.12 KB
/
favoriteswindow.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <QLineEdit>
#include <QPainter>
#include <QtGlobal>
#include "favoriteswindow.h"
#include "ui_favoriteswindow.h"
FavoritesWindow::FavoritesWindow(QWidget *parent) :
QWidget(parent),
ui(new Ui::FavoritesWindow)
{
ui->setupUi(this);
fileList = new FavoritesList(this);
streamList = new FavoritesList(this);
ui->filesTab->layout()->addWidget(fileList);
ui->streamsTab->layout()->addWidget(streamList);
}
FavoritesWindow::~FavoritesWindow()
{
delete ui;
}
void FavoritesWindow::setFiles(const QList<TrackInfo> &tracks)
{
fileList->setTracks(tracks);
}
void FavoritesWindow::setStreams(const QList<TrackInfo> &tracks)
{
streamList->setTracks(tracks);
}
void FavoritesWindow::addTrack(const TrackInfo &track)
{
if (track.url.isLocalFile())
fileList->addTrack(track);
else
streamList->addTrack(track);
updateFavoriteTracks();
}
void FavoritesWindow::updateFavoriteTracks()
{
emit favoriteTracks(fileList->tracks(), streamList->tracks());
}
void FavoritesWindow::on_remove_clicked()
{
FavoritesList *active = ui->tabWidget->currentIndex() == 0 ? fileList : streamList;
for (auto &i : active->selectedItems())
delete i;
}
void FavoritesWindow::on_buttonBox_clicked(QAbstractButton *button)
{
QDialogButtonBox::ButtonRole buttonRole;
buttonRole = ui->buttonBox->buttonRole(button);
if (buttonRole == QDialogButtonBox::AcceptRole)
updateFavoriteTracks();
if (buttonRole == QDialogButtonBox::RejectRole)
emit favoriteTracksCancel();
close();
}
//---------------------------------------------------------------------------
FavoritesList::FavoritesList(QWidget *parent)
: QListWidget(parent)
{
setItemDelegateForColumn(0, new FavoritesDelegate(this));
setSelectionMode(QAbstractItemView::ContiguousSelection);
setDragDropMode(QAbstractItemView::InternalMove);
}
FavoritesList::~FavoritesList()
{
}
void FavoritesList::setTracks(const QList<TrackInfo> &tracks)
{
clear();
for (const auto &t : tracks)
addTrack(t);
}
void FavoritesList::addTrack(const TrackInfo &track)
{
auto item = new FavoritesItem(this, track);
this->addItem(item);
}
QList<TrackInfo> FavoritesList::tracks()
{
QList<TrackInfo> list;
int rows = count();
for (int i = 0; i < rows; i++)
list.append(static_cast<FavoritesItem*>(item(i))->track());
return list;
}
//---------------------------------------------------------------------------
FavoritesItem::FavoritesItem(QListWidget *owner, const TrackInfo &t)
: QListWidgetItem(owner)
{
track_ = t;
Qt::ItemFlags f = flags();
#if QT_VERSION >= 0x050700
f.setFlag(Qt::ItemIsEditable);
#else
f |= Qt::ItemIsEditable;
#endif
this->setFlags(f);
}
FavoritesItem::~FavoritesItem()
{
}
//---------------------------------------------------------------------------
FavoritesDelegate::FavoritesDelegate(QWidget *parent)
: QAbstractItemDelegate(parent)
, owner(static_cast<FavoritesList*>(parent))
{
}
FavoritesDelegate::~FavoritesDelegate()
{
}
QWidget *FavoritesDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
Q_UNUSED(option)
Q_UNUSED(index)
return new QLineEdit(parent);
}
void FavoritesDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
FavoritesItem *item = static_cast<FavoritesItem*>(owner->item(index.row()));
static_cast<QLineEdit*>(editor)->setText(item->track().text);
}
void FavoritesDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
Q_UNUSED(model)
FavoritesItem *item = static_cast<FavoritesItem*>(owner->item(index.row()));
QString text = static_cast<QLineEdit*>(editor)->text();
if (text.isEmpty())
text = item->track().url.toString();
item->track().text = text;
}
QSize FavoritesDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
Q_UNUSED(index)
return QApplication::style()->sizeFromContents(QStyle::CT_ItemViewItem,
&option,
option.rect.size());
}
void FavoritesDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
auto listWidget = qobject_cast<FavoritesList*>(parent());
auto listItem = static_cast<FavoritesItem*>(listWidget->item(index.row()));
QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &option, painter);
QString text = listItem->track().text;
QString time = Helpers::toDateFormat(listItem->track().position) + " / "
+ Helpers::toDateFormat(listItem->track().length);
QRect rc = option.rect.adjusted(3, 0, -3, 0);
painter->drawText(rc, Qt::AlignRight|Qt::AlignCenter, time);
rc.adjust(0, 0, -(3 + option.fontMetrics.horizontalAdvance(time)), 0);
painter->drawText(rc, Qt::AlignLeft|Qt::AlignVCenter, text);
}
void FavoritesDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
Q_UNUSED(index)
editor->setGeometry(option.rect);
}