File: playlistview.cpp

package info (click to toggle)
esperanza 0.2.1-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 600 kB
  • ctags: 536
  • sloc: cpp: 4,533; sh: 63; makefile: 4
file content (284 lines) | stat: -rw-r--r-- 6,322 bytes parent folder | download
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include "playlistview.h"
#include "playlistmodel.h"
#include "xclient.h"
#include "playerwidget.h"

#include <QHeaderView>
#include <QPainter>
#include <QSettings>

PlaylistDelegate::PlaylistDelegate (QObject *parent, PlaylistModel *model) : QItemDelegate (parent)
{
	m_model = model;
}

void
PlaylistDelegate::paint (QPainter *painter,
						 const QStyleOptionViewItem &option,
						 const QModelIndex &index) const
{
	QSettings s;

	QStyleOptionViewItem o (option);
	if (index.data (PlaylistModel::CurrentEntryRole).toBool ()) {
		QFont f (o.font);
		f.setBold (true);
		o.font = f;
	}
	if (index.internalId() != -1) {
		o.state |= QStyle::State_Selected;
		if (s.value ("ui/contextareabright").toBool ()) {
			QPalette p (o.palette);
			p.setColor (QPalette::Highlight, p.highlight ().color ().light ());
			o.palette = p;
		}
	} 

	QItemDelegate::paint (painter, o, index);

}

PlaylistView::PlaylistView (QWidget *parent, XClient *client) : QTreeView (parent)
{
	m_client = client;
	m_parent = parent;
	m_removed = false;

	m_model = new PlaylistModel (this, m_client);
	setModel (m_model);

	setItemDelegate (new PlaylistDelegate (this, m_model));

	setIndentation (0);
	setAlternatingRowColors (true);
	setItemsExpandable (false);
	setRootIsDecorated (false);
	setTabKeyNavigation (false);
	setTextElideMode (Qt::ElideNone);

	setVerticalScrollBarPolicy (Qt::ScrollBarAlwaysOn);

	setDragEnabled (false);
	setAcceptDrops (false);
	setDropIndicatorShown (true);

	QHeaderView *head = header ();
	QSettings s;

	head->resizeSection (0, s.value ("playlist/section0", 180).toInt ());
	head->setResizeMode (0, QHeaderView::Interactive);
	connect (head, SIGNAL (sectionResized (int, int, int)), this, SLOT (head_size (int, int, int)));

    setSelectionMode (QAbstractItemView::ExtendedSelection);
    setSelectionBehavior (QAbstractItemView::SelectRows);

    m_selections = new QItemSelectionModel (m_model);
	setSelectionModel (m_selections);

	connect (m_selections, SIGNAL (currentRowChanged (const QModelIndex &, const QModelIndex &)),
			 this, SLOT (item_selected (const QModelIndex &, const QModelIndex &)));

	connect (this, SIGNAL (doubleClicked (const QModelIndex &)),
			 this, SLOT (jump_pos (const QModelIndex &)));

	connect (client, SIGNAL(gotConnection (XClient *)),
			 this, SLOT (got_connection (XClient *))); 

	connect (m_client->settings (), SIGNAL (settingsChanged ()),
			 this, SLOT (changed_settings ()));

	connect (m_model, SIGNAL (rowsInserted (const QModelIndex &, int, int)),
			 this, SLOT (rows_inserted ()));

	setIconSize (QSize (75, 75));
}

void
PlaylistView::mousePressEvent (QMouseEvent *ev)
{
	if (ev->buttons () & Qt::RightButton || ev->buttons () & Qt::MidButton) {
		ev->ignore ();
		return;
	}

	if (!indexAt (ev->pos ()).isValid ()) {
		ev->ignore ();
		return;
	}

	QTreeView::mousePressEvent (ev);
}

void
PlaylistView::rows_inserted ()
{
	QModelIndex idx = m_model->index (0, 0);
	if (!idx.isValid ())
		return;
	if (!m_model->cached_size (idx.column ()).isValid () && idx.internalId () == -1) {
		m_model->set_cached_size (idx.column (), sizeHintForIndex (idx));
	}
	idx = m_model->index (0, 1);
	if (!m_model->cached_size (idx.column ()).isValid () && idx.internalId () == -1) {
		m_model->set_cached_size (idx.column (), sizeHintForIndex (idx));
	}
}

void
PlaylistView::keyPressEvent (QKeyEvent *ev)
{
	QModelIndex idx = m_selections->currentIndex ();
	QModelIndex nidx;

	switch (ev->key ()) {
		case Qt::Key_Up:
			nidx = m_model->index (idx.row () - 1, idx.column (), QModelIndex ());
			break;
		case Qt::Key_Down:
			nidx = m_model->index (idx.row () + 1, idx.column (), QModelIndex ());
			break;
		default:
			ev->ignore ();
			return;
	}

	if (nidx.isValid ())
		setCurrentIndex (nidx);
}

void
PlaylistView::changed_settings ()
{
	QSettings s;

	collapseAll ();

	if (!s.value ("playlist/compactmode").toBool ()) {
		if (getSelection ().size () > 1)
			return;
		setExpanded (m_selections->currentIndex (), true);
	}
}

void
PlaylistView::got_connection (XClient *client)
{
	m_client = client;
	client->playlist.broadcastCurrentPos (Xmms::bind (&PlaylistView::handle_update_pos, this));
	client->playlist.currentPos (Xmms::bind (&PlaylistView::handle_update_pos, this));
}

bool
PlaylistView::handle_update_pos (const uint32_t &pos)
{
	QSettings s;
	if (m_removed) {
		m_removed = false;
		return true;
	}

	if (s.value ("playlist/jumptocurrent").toBool ())
		setCurrentIndex (m_model->index (pos, 0));
	return true;
}

void
PlaylistView::item_selected (const QModelIndex &n, const QModelIndex &old)
{
	QSettings s;

	if (s.value ("playlist/compactmode").toBool ())
		return;

	if (n.internalId () != -1) {
		setCurrentIndex (n.parent ());
		return;
	}

	setCurrentIndex (n);

	QModelIndexList l = getSelection ();
	
	if (l.count () < 1) {
		setCurrentIndex (old);
	} else if (l.count () > 1) {
		collapseAll ();
	} else {
		collapseAll ();

		setExpanded (n, true);

		QModelIndex c = n.child (0, 0);
		scrollTo (c);
	}

	/* emit current id */
	if (l.count () > 1 || l.count () < 1) {
		emit selectedID (0);
	} else {
		emit selectedID (l[0].data (PlaylistModel::MedialibIdRole).toUInt ());
	}
}

static bool
dummy_uint (const uint32_t &)
{
	return false;
}

void
PlaylistView::jump_pos (const QModelIndex &i)
{
	QModelIndex idx = i;

	if (!idx.isValid ())
		idx = currentIndex ();

	uint32_t row = idx.row ();
	if (idx.internalId () != -1)
		row = idx.parent ().row ();

	m_client->playlist.setNext (row, &dummy_uint);
	/* Note. tickle before checking status is a good
	 * idea here. It seems to bork on linux platform
	 * otherwise
	 */
	m_client->playback.tickle (&XClient::log);

	PlayerWidget *pw = dynamic_cast<PlayerWidget *> (m_parent);
	if (pw->status () != Xmms::Playback::PLAYING) {
		m_client->playback.start (&XClient::log);

		if (pw->status () == Xmms::Playback::PAUSED) {
			m_client->playback.tickle (&XClient::log);
		}
	}

}

QModelIndexList
PlaylistView::getSelection ()
{
	QModelIndexList lst = m_selections->selectedIndexes ();
	QModelIndexList ret;

	for (int i = 0; i < lst.size (); i++) {
		QModelIndex idx = lst.at (i);
		if (idx.column () != 0)
			continue;

		ret.append (idx);
	}

	return ret;
}

void
PlaylistView::head_size (int c, int o, int n)
{
	if (c == 0) {
		QSettings s;
		s.setValue ("playlist/section0", n);
	}
}