2010-05-17 01:44:33 +02:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-05-17 01:44:33 +02:00
|
|
|
|
|
|
|
Clementine 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 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "autoexpandingtreeview.h"
|
2011-01-10 23:26:13 +01:00
|
|
|
#include "core/mimedata.h"
|
2010-05-17 01:44:33 +02:00
|
|
|
|
2010-12-26 19:07:57 +01:00
|
|
|
#include <QMouseEvent>
|
2010-09-18 15:55:04 +02:00
|
|
|
#include <QtDebug>
|
|
|
|
|
2010-05-17 01:44:33 +02:00
|
|
|
const int AutoExpandingTreeView::kRowsToShow = 50;
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
AutoExpandingTreeView::AutoExpandingTreeView(QWidget* parent)
|
|
|
|
: QTreeView(parent),
|
|
|
|
auto_open_(true),
|
|
|
|
expand_on_reset_(true),
|
|
|
|
add_on_double_click_(true),
|
|
|
|
ignore_next_click_(false) {
|
2010-12-11 12:26:47 +01:00
|
|
|
setExpandsOnDoubleClick(false);
|
|
|
|
|
2010-05-17 01:44:33 +02:00
|
|
|
connect(this, SIGNAL(expanded(QModelIndex)), SLOT(ItemExpanded(QModelIndex)));
|
2010-12-07 22:13:40 +01:00
|
|
|
connect(this, SIGNAL(clicked(QModelIndex)), SLOT(ItemClicked(QModelIndex)));
|
2014-02-07 16:34:20 +01:00
|
|
|
connect(this, SIGNAL(doubleClicked(QModelIndex)),
|
|
|
|
SLOT(ItemDoubleClicked(QModelIndex)));
|
2010-05-17 01:44:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void AutoExpandingTreeView::reset() {
|
|
|
|
QTreeView::reset();
|
|
|
|
|
|
|
|
// Expand nodes in the tree until we have about 50 rows visible in the view
|
|
|
|
if (auto_open_ && expand_on_reset_) {
|
|
|
|
RecursivelyExpand(rootIndex());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void AutoExpandingTreeView::RecursivelyExpand(const QModelIndex& index) {
|
2010-05-17 01:44:33 +02:00
|
|
|
int rows = model()->rowCount(index);
|
|
|
|
RecursivelyExpand(index, &rows);
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
bool AutoExpandingTreeView::RecursivelyExpand(const QModelIndex& index,
|
|
|
|
int* count) {
|
|
|
|
if (!CanRecursivelyExpand(index)) return true;
|
2010-09-18 15:55:04 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (model()->canFetchMore(index)) model()->fetchMore(index);
|
2010-05-17 01:44:33 +02:00
|
|
|
|
|
|
|
int children = model()->rowCount(index);
|
2014-02-07 16:34:20 +01:00
|
|
|
if (*count + children > kRowsToShow) return false;
|
2010-05-17 01:44:33 +02:00
|
|
|
|
|
|
|
expand(index);
|
|
|
|
*count += children;
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < children; ++i) {
|
|
|
|
if (!RecursivelyExpand(model()->index(i, 0, index), count)) return false;
|
2010-05-17 01:44:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AutoExpandingTreeView::ItemExpanded(const QModelIndex& index) {
|
|
|
|
if (model()->rowCount(index) == 1 && auto_open_)
|
|
|
|
expand(model()->index(0, 0, index));
|
|
|
|
}
|
2010-12-07 22:13:40 +01:00
|
|
|
|
|
|
|
void AutoExpandingTreeView::ItemClicked(const QModelIndex& index) {
|
2010-12-11 12:26:47 +01:00
|
|
|
if (ignore_next_click_) {
|
|
|
|
ignore_next_click_ = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-12-11 11:40:54 +01:00
|
|
|
setExpanded(index, !isExpanded(index));
|
2010-12-07 22:13:40 +01:00
|
|
|
}
|
2010-12-11 12:26:47 +01:00
|
|
|
|
|
|
|
void AutoExpandingTreeView::ItemDoubleClicked(const QModelIndex& index) {
|
|
|
|
ignore_next_click_ = true;
|
2011-01-10 23:26:13 +01:00
|
|
|
|
2012-03-11 00:38:54 +01:00
|
|
|
if (add_on_double_click_) {
|
|
|
|
QMimeData* data = model()->mimeData(QModelIndexList() << index);
|
|
|
|
if (MimeData* mime_data = qobject_cast<MimeData*>(data)) {
|
|
|
|
mime_data->from_doubleclick_ = true;
|
|
|
|
}
|
|
|
|
emit AddToPlaylistSignal(data);
|
2011-01-10 23:26:13 +01:00
|
|
|
}
|
2010-12-11 12:26:47 +01:00
|
|
|
}
|
2010-12-26 19:07:57 +01:00
|
|
|
|
|
|
|
void AutoExpandingTreeView::mousePressEvent(QMouseEvent* event) {
|
|
|
|
if (event->modifiers() != Qt::NoModifier) {
|
|
|
|
ignore_next_click_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTreeView::mousePressEvent(event);
|
2011-03-10 13:42:35 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
// enqueue to playlist with middleClick
|
2011-03-10 13:42:35 +01:00
|
|
|
if (event->button() == Qt::MidButton) {
|
|
|
|
QMimeData* data = model()->mimeData(selectedIndexes());
|
|
|
|
if (MimeData* mime_data = qobject_cast<MimeData*>(data)) {
|
|
|
|
mime_data->enqueue_now_ = true;
|
|
|
|
}
|
|
|
|
emit AddToPlaylistSignal(data);
|
|
|
|
}
|
2010-12-26 19:07:57 +01:00
|
|
|
}
|
2011-03-13 15:14:16 +01:00
|
|
|
|
|
|
|
void AutoExpandingTreeView::keyPressEvent(QKeyEvent* e) {
|
|
|
|
switch (e->key()) {
|
|
|
|
case Qt::Key_Enter:
|
|
|
|
case Qt::Key_Return:
|
2014-02-07 16:34:20 +01:00
|
|
|
if (currentIndex().isValid()) emit doubleClicked(currentIndex());
|
2011-03-13 15:14:16 +01:00
|
|
|
e->accept();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Qt::Key_Backspace:
|
|
|
|
case Qt::Key_Escape:
|
|
|
|
emit FocusOnFilterSignal(e);
|
|
|
|
e->accept();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTreeView::keyPressEvent(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AutoExpandingTreeView::UpAndFocus() {
|
|
|
|
setCurrentIndex(moveCursor(QAbstractItemView::MoveUp, Qt::NoModifier));
|
|
|
|
setFocus();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AutoExpandingTreeView::DownAndFocus() {
|
|
|
|
setCurrentIndex(moveCursor(QAbstractItemView::MoveDown, Qt::NoModifier));
|
|
|
|
setFocus();
|
|
|
|
}
|