File: treeview.cpp

package info (click to toggle)
cantata 3.3.1.ds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 38,300 kB
  • sloc: cpp: 105,773; perl: 1,366; xml: 723; python: 139; lex: 110; sh: 105; yacc: 78; makefile: 8
file content (399 lines) | stat: -rw-r--r-- 10,550 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
 * Cantata
 *
 * Copyright (c) 2011-2022 Craig Drummond <craig.p.drummond@gmail.com>
 *
 * ----
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "treeview.h"
#include "basicitemdelegate.h"
#include "config.h"
#include "icons.h"
#include "models/roles.h"
#include "mpd-interface/song.h"
#include "support/utils.h"
#include <QApplication>
#include <QDrag>
#include <QHeaderView>
#include <QList>
#include <QMap>
#include <QMimeData>
#include <QMouseEvent>
#include <QPaintEvent>
#include <QPainter>
#include <QStyle>
#include <algorithm>

#define SINGLE_CLICK style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick, 0, this)

QImage TreeView::setOpacity(const QImage& orig, double opacity)
{
	QImage img = QImage::Format_ARGB32 == orig.format() ? orig : orig.convertToFormat(QImage::Format_ARGB32);
	uchar* bits = img.bits();
	for (int i = 0; i < img.height() * img.bytesPerLine(); i += 4) {
		if (0 != bits[i + 3]) {
			bits[i + 3] *= opacity;
		}
	}
	return img;
}

QPixmap TreeView::createBgndPixmap(const QIcon& icon)
{
	if (icon.isNull()) {
		return QPixmap();
	}
	static int bgndSize = 0;
	if (0 == bgndSize) {
		bgndSize = QFontMetricsF(QApplication::font()).height() * 16;
	}

	QImage img = icon.pixmap(bgndSize, bgndSize).toImage();
	if (img.width() != bgndSize && img.height() != bgndSize) {
		img = img.scaled(bgndSize, bgndSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
	}
	return QPixmap::fromImage(setOpacity(img, 0.075));
}

static bool forceSingleClick = true;

void TreeView::setForceSingleClick(bool v)
{
	forceSingleClick = v;
}

bool TreeView::getForceSingleClick()
{
	return forceSingleClick;
}

TreeView::TreeView(QWidget* parent, bool menuAlwaysAllowed)
	: QTreeView(parent), eventFilter(nullptr), forceSingleColumn(false), alwaysAllowMenu(menuAlwaysAllowed)
{
	setDragEnabled(true);
	setContextMenuPolicy(Qt::NoContextMenu);
	//     setRootIsDecorated(false);
	setAllColumnsShowFocus(true);
	setAlternatingRowColors(false);
	setSelectionMode(QAbstractItemView::ExtendedSelection);
	setSelectionBehavior(QAbstractItemView::SelectRows);
	setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
	// Treeview does not seem to need WA_MouseTracking set, even with QGtkStyle items still
	// highlight under mouse. And enabling WA_MouseTracking here seems to cause drag-n-drop
	// errors if an item is dragged onto playqueue whilst playqueue has a selected item!
	// BUG:145
	//setAttribute(Qt::WA_MouseTracking);

	if (SINGLE_CLICK) {
		connect(this, SIGNAL(activated(const QModelIndex&)), this, SIGNAL(itemActivated(const QModelIndex&)));
	}
}

TreeView::~TreeView()
{
}

void TreeView::setPageDefaults()
{
	sortByColumn(0, Qt::AscendingOrder);
	setHeaderHidden(true);
	setDragDropMode(QAbstractItemView::DragOnly);
	setSortingEnabled(true);
	setAnimated(true);
	forceSingleColumn = true;
}

void TreeView::setExpandOnClick()
{
	connect(this, SIGNAL(clicked(const QModelIndex&)), this, SLOT(itemWasClicked(const QModelIndex&)));
}

void TreeView::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
{
	QTreeView::selectionChanged(selected, deselected);
	bool haveSelection = haveSelectedItems();

	if (!alwaysAllowMenu) {
		setContextMenuPolicy(haveSelection ? Qt::ActionsContextMenu : Qt::NoContextMenu);
	}
	emit itemsSelected(haveSelection);
}

bool TreeView::haveSelectedItems() const
{
	// Dont need the sorted type of 'selectedIndexes' here...
	return selectionModel() && selectionModel()->selectedIndexes().count() > 0;
}

bool TreeView::haveUnSelectedItems() const
{
	// Dont need the sorted type of 'selectedIndexes' here...
	return selectionModel() && model() && selectionModel()->selectedIndexes().count() != model()->rowCount();
}

void TreeView::drag(Qt::DropActions supportedActions, QAbstractItemView* view, const QModelIndexList& items)
{
	if (items.count() > 0) {
		QMimeData* data = view->model()->mimeData(items);
		if (!data) {
			return;
		}
		QDrag* drag = new QDrag(view);
		drag->setMimeData(data);
		int pixSize = Icon::stdSize(Utils::scaleForDpi(32));
		drag->setPixmap(Icons::self()->audioListIcon.pixmap(pixSize, pixSize));
		drag->exec(supportedActions);
	}
}

void TreeView::mouseReleaseEvent(QMouseEvent* event)
{
	if (Qt::NoModifier == event->modifiers() && Qt::LeftButton == event->button()) {
		QTreeView::mouseReleaseEvent(event);
	}
}

QModelIndexList TreeView::selectedIndexes(bool sorted) const
{
	if (!selectionModel()) {
		return QModelIndexList();
	}

	if (sorted) {
		return sortIndexes(selectionModel()->selectedIndexes());
	}
	else if (model() && model()->columnCount() > 1) {
		QModelIndexList list = selectionModel()->selectedIndexes();
		QModelIndexList sel;
		for (const QModelIndex& idx : list) {
			if (0 == idx.column()) {
				sel.append(idx);
			}
		}
		return sel;
	}
	return selectionModel()->selectedIndexes();
}

struct Index : public QModelIndex {
	Index(const QModelIndex& i)
		: QModelIndex(i)
	{
		QModelIndex idx = i;
		while (idx.isValid()) {
			rows.prepend(idx.row());
			idx = idx.parent();
		}
		count = rows.count();
	}

	bool operator<(const Index& rhs) const
	{
		int toCompare = qMax(count, rhs.count);
		for (int i = 0; i < toCompare; ++i) {
			qint32 left = i < count ? rows.at(i) : -1;
			qint32 right = i < rhs.count ? rhs.rows.at(i) : -1;
			if (left < right) {
				return true;
			}
			else if (left > right) {
				return false;
			}
		}
		return false;
	}

	QList<qint32> rows;
	int count;
};

QModelIndexList TreeView::sortIndexes(const QModelIndexList& list)
{
	if (list.isEmpty()) {
		return list;
	}

	// QModelIndex::operator< sorts on row first - but this messes things up if rows
	// have different parents. Therefore, we use the sort above - so that the hierarchy is preserved.
	// First, create the list of 'Index' items to be sorted...
	QList<Index> toSort;
	for (const QModelIndex& i : list) {
		if (0 == i.column()) {
			toSort.append(Index(i));
		}
	}
	// Call std::sort on these - this will use operator<
	std::sort(toSort.begin(), toSort.end());

	// Now convert the QList<Index> into a QModelIndexList
	QModelIndexList sorted;
	for (const Index& i : toSort) {
		sorted.append(i);
	}
	return sorted;
}

void TreeView::expandAll(const QModelIndex& idx, bool singleLevelOnly)
{
	quint32 count = model()->rowCount(idx);
	for (quint32 i = 0; i < count; ++i) {
		expand(model()->index(i, 0, idx), singleLevelOnly);
	}
}

void TreeView::collapseToLevel(int level, const QModelIndex& idx)
{
	quint32 count = model()->rowCount(idx);
	if (level) {
		for (quint32 i = 0; i < count; ++i) {
			collapseToLevel(level - 1, model()->index(i, 0, idx));
		}
	}
	else {
		for (quint32 i = 0; i < count; ++i) {
			collapse(model()->index(i, 0, idx));
		}
	}
}

void TreeView::expand(const QModelIndex& idx, bool singleOnly)
{
	if (idx.isValid()) {
		setExpanded(idx, true);
		if (!singleOnly) {
			quint32 count = model()->rowCount(idx);
			for (quint32 i = 0; i < count; ++i) {
				expand(model()->index(i, 0, idx));
			}
		}
	}
}

void TreeView::collapse(const QModelIndex& idx, bool singleOnly)
{
	if (idx.isValid()) {
		setExpanded(idx, false);
		if (!singleOnly) {
			quint32 count = model()->rowCount(idx);
			for (quint32 i = 0; i < count; ++i) {
				collapse(model()->index(i, 0, idx));
			}
		}
	}
}

bool TreeView::checkBoxClicked(const QModelIndex& idx) const
{
	QRect rect = visualRect(idx);
	rect.moveTo(viewport()->mapToGlobal(QPoint(rect.x(), rect.y())));
	int itemIndentation = rect.x() - visualRect(rootIndex()).x();
	rect = QRect(header()->sectionViewportPosition(0) + itemIndentation, rect.y(), style()->pixelMetric(QStyle::PM_IndicatorWidth), rect.height());
	return rect.contains(QCursor::pos());
}

void TreeView::setUseSimpleDelegate()
{
	setItemDelegate(new BasicItemDelegate(this));
}

void TreeView::setBackgroundImage(const QIcon& icon)
{
	QPalette pal = parentWidget()->palette();
	//    if (!icon.isNull()) {
	//        pal.setColor(QPalette::Base, Qt::transparent);
	//    }
#ifndef Q_OS_MAC
	setPalette(pal);
#endif
	viewport()->setPalette(pal);
	bgnd = createBgndPixmap(icon);
}

void TreeView::paintEvent(QPaintEvent* e)
{
	if (!bgnd.isNull()) {
		QPainter p(viewport());
		QSize sz = size();
		p.fillRect(0, 0, sz.width(), sz.height(), QApplication::palette().color(QPalette::Base));
		p.drawPixmap((sz.width() - bgnd.width()) / 2, (sz.height() - bgnd.height()) / 2, bgnd);
	}
	if (!info.isEmpty() && model() && 0 == model()->rowCount()) {
		QPainter p(viewport());
		QColor col(palette().text().color());
		col.setAlphaF(0.5);
		QFont f(font());
		f.setItalic(true);
		p.setPen(col);
		p.setFont(f);
		p.drawText(rect().adjusted(8, 8, -16, -16), Qt::AlignCenter | Qt::TextWordWrap, info);
	}
	QTreeView::paintEvent(e);
}

void TreeView::setModel(QAbstractItemModel* m)
{
	QAbstractItemModel* old = model();
	QTreeView::setModel(m);

	if (forceSingleColumn && m) {
		int columnCount = m->columnCount();
		if (columnCount > 1) {
			QHeaderView* hdr = header();
			for (int i = 1; i < columnCount; ++i) {
				hdr->setSectionHidden(i, true);
			}
		}
	}

	if (old) {
		disconnect(old, SIGNAL(layoutChanged()), this, SLOT(correctSelection()));
	}

	if (m && old != m) {
		connect(m, SIGNAL(layoutChanged()), this, SLOT(correctSelection()));
	}
}

// Workaround for https://bugreports.qt-project.org/browse/QTBUG-18009
void TreeView::correctSelection()
{
	if (!selectionModel()) {
		return;
	}

	QItemSelection s = selectionModel()->selection();
	setCurrentIndex(currentIndex());
	selectionModel()->select(s, QItemSelectionModel::SelectCurrent);
}

//void TreeView::itemWasActivated(const QModelIndex &index)
//{
//    if (!forceSingleClick) {
//        setExpanded(index, !isExpanded(index));
//    }
//}

void TreeView::itemWasClicked(const QModelIndex& index)
{
	if (forceSingleClick) {
		setExpanded(index, !isExpanded(index));
	}
}

#include "moc_treeview.cpp"