2010-05-09 17:51:04 +02:00
|
|
|
/* This file is part of Clementine.
|
2014-11-02 19:36:21 +01:00
|
|
|
Copyright 2010-2012, David Sansome <me@davidsansome.com>
|
|
|
|
Copyright 2011, Arnaud Bienner <arnaud.bienner@gmail.com>
|
|
|
|
Copyright 2014, Krzysztof Sobiecki <sobkas@gmail.com>
|
|
|
|
Copyright 2014, John Maguire <john.maguire@gmail.com>
|
2010-05-09 17:51:04 +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 "mergedproxymodel.h"
|
2012-03-10 22:05:57 +01:00
|
|
|
#include "core/logging.h"
|
2010-05-09 17:51:04 +02:00
|
|
|
|
2010-05-09 19:19:48 +02:00
|
|
|
#include <QStringList>
|
2010-05-09 23:16:54 +02:00
|
|
|
|
|
|
|
#include <limits>
|
2010-05-09 19:19:48 +02:00
|
|
|
|
2014-02-06 17:44:29 +01:00
|
|
|
// boost::multi_index still relies on these being in the global namespace.
|
|
|
|
using std::placeholders::_1;
|
|
|
|
using std::placeholders::_2;
|
|
|
|
|
|
|
|
#include <boost/multi_index_container.hpp>
|
|
|
|
#include <boost/multi_index/member.hpp>
|
|
|
|
#include <boost/multi_index/hashed_index.hpp>
|
|
|
|
#include <boost/multi_index/ordered_index.hpp>
|
|
|
|
|
|
|
|
using boost::multi_index::hashed_unique;
|
|
|
|
using boost::multi_index::identity;
|
|
|
|
using boost::multi_index::indexed_by;
|
|
|
|
using boost::multi_index::member;
|
|
|
|
using boost::multi_index::multi_index_container;
|
|
|
|
using boost::multi_index::ordered_unique;
|
|
|
|
using boost::multi_index::tag;
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
std::size_t hash_value(const QModelIndex& index) { return qHash(index); }
|
2010-05-09 17:51:04 +02:00
|
|
|
|
2014-02-06 17:44:29 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct Mapping {
|
2014-11-02 21:37:15 +01:00
|
|
|
explicit Mapping(const QModelIndex& _source_index) :
|
|
|
|
source_index(_source_index) {}
|
2014-02-06 17:44:29 +01:00
|
|
|
|
|
|
|
QModelIndex source_index;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct tag_by_source {};
|
|
|
|
struct tag_by_pointer {};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
class MergedProxyModelPrivate {
|
|
|
|
private:
|
|
|
|
typedef multi_index_container<
|
2014-02-07 16:34:20 +01:00
|
|
|
Mapping*,
|
|
|
|
indexed_by<
|
|
|
|
hashed_unique<tag<tag_by_source>,
|
|
|
|
member<Mapping, QModelIndex, &Mapping::source_index> >,
|
|
|
|
ordered_unique<tag<tag_by_pointer>, identity<Mapping*> > > >
|
|
|
|
MappingContainer;
|
2014-02-06 17:44:29 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
MappingContainer mappings_;
|
|
|
|
};
|
|
|
|
|
2010-05-09 17:51:04 +02:00
|
|
|
MergedProxyModel::MergedProxyModel(QObject* parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: QAbstractProxyModel(parent),
|
|
|
|
resetting_model_(nullptr),
|
|
|
|
p_(new MergedProxyModelPrivate) {}
|
2010-05-09 17:51:04 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
MergedProxyModel::~MergedProxyModel() { DeleteAllMappings(); }
|
2010-05-09 17:53:28 +02:00
|
|
|
|
|
|
|
void MergedProxyModel::DeleteAllMappings() {
|
2014-02-06 17:44:29 +01:00
|
|
|
const auto& begin = p_->mappings_.get<tag_by_pointer>().begin();
|
|
|
|
const auto& end = p_->mappings_.get<tag_by_pointer>().end();
|
2010-05-09 17:53:28 +02:00
|
|
|
qDeleteAll(begin, end);
|
|
|
|
}
|
|
|
|
|
2010-05-09 17:51:04 +02:00
|
|
|
void MergedProxyModel::AddSubModel(const QModelIndex& source_parent,
|
2010-05-11 20:01:30 +02:00
|
|
|
QAbstractItemModel* submodel) {
|
2010-05-09 17:51:04 +02:00
|
|
|
connect(submodel, SIGNAL(modelReset()), this, SLOT(SubModelReset()));
|
2014-02-07 16:34:20 +01:00
|
|
|
connect(submodel, SIGNAL(rowsAboutToBeInserted(QModelIndex, int, int)), this,
|
|
|
|
SLOT(RowsAboutToBeInserted(QModelIndex, int, int)));
|
|
|
|
connect(submodel, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)), this,
|
|
|
|
SLOT(RowsAboutToBeRemoved(QModelIndex, int, int)));
|
|
|
|
connect(submodel, SIGNAL(rowsInserted(QModelIndex, int, int)), this,
|
|
|
|
SLOT(RowsInserted(QModelIndex, int, int)));
|
|
|
|
connect(submodel, SIGNAL(rowsRemoved(QModelIndex, int, int)), this,
|
|
|
|
SLOT(RowsRemoved(QModelIndex, int, int)));
|
|
|
|
connect(submodel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this,
|
|
|
|
SLOT(DataChanged(QModelIndex, QModelIndex)));
|
2010-07-04 01:00:07 +02:00
|
|
|
|
2010-07-04 02:58:01 +02:00
|
|
|
QModelIndex proxy_parent = mapFromSource(source_parent);
|
|
|
|
const int rows = submodel->rowCount();
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (rows) beginInsertRows(proxy_parent, 0, rows - 1);
|
2010-07-04 02:58:01 +02:00
|
|
|
|
2010-07-04 02:33:34 +02:00
|
|
|
merge_points_.insert(submodel, source_parent);
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (rows) endInsertRows();
|
2010-05-09 17:51:04 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void MergedProxyModel::RemoveSubModel(const QModelIndex& source_parent) {
|
2010-07-04 02:33:34 +02:00
|
|
|
// Find the submodel that the parent corresponded to
|
|
|
|
QAbstractItemModel* submodel = merge_points_.key(source_parent);
|
|
|
|
merge_points_.remove(submodel);
|
|
|
|
|
|
|
|
// The submodel might have been deleted already so we must be careful not
|
|
|
|
// to dereference it.
|
|
|
|
|
|
|
|
// Remove all the children of the item that got deleted
|
|
|
|
QModelIndex proxy_parent = mapFromSource(source_parent);
|
|
|
|
|
|
|
|
// We can't know how many children it had, since we can't dereference it
|
|
|
|
resetting_model_ = submodel;
|
|
|
|
beginRemoveRows(proxy_parent, 0, std::numeric_limits<int>::max() - 1);
|
|
|
|
endRemoveRows();
|
2014-02-06 16:49:49 +01:00
|
|
|
resetting_model_ = nullptr;
|
2010-07-04 02:33:34 +02:00
|
|
|
|
|
|
|
// Delete all the mappings that reference the submodel
|
2014-02-06 17:44:29 +01:00
|
|
|
auto it = p_->mappings_.get<tag_by_pointer>().begin();
|
|
|
|
auto end = p_->mappings_.get<tag_by_pointer>().end();
|
2010-07-04 02:33:34 +02:00
|
|
|
while (it != end) {
|
|
|
|
if ((*it)->source_index.model() == submodel) {
|
|
|
|
delete *it;
|
2014-02-06 17:44:29 +01:00
|
|
|
it = p_->mappings_.get<tag_by_pointer>().erase(it);
|
2010-07-04 02:33:34 +02:00
|
|
|
} else {
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-09 17:51:04 +02:00
|
|
|
void MergedProxyModel::setSourceModel(QAbstractItemModel* source_model) {
|
|
|
|
if (sourceModel()) {
|
2014-02-07 16:34:20 +01:00
|
|
|
disconnect(sourceModel(), SIGNAL(modelReset()), this,
|
|
|
|
SLOT(SourceModelReset()));
|
|
|
|
disconnect(sourceModel(),
|
|
|
|
SIGNAL(rowsAboutToBeInserted(QModelIndex, int, int)), this,
|
|
|
|
SLOT(RowsAboutToBeInserted(QModelIndex, int, int)));
|
|
|
|
disconnect(sourceModel(),
|
|
|
|
SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)), this,
|
|
|
|
SLOT(RowsAboutToBeRemoved(QModelIndex, int, int)));
|
|
|
|
disconnect(sourceModel(), SIGNAL(rowsInserted(QModelIndex, int, int)), this,
|
|
|
|
SLOT(RowsInserted(QModelIndex, int, int)));
|
|
|
|
disconnect(sourceModel(), SIGNAL(rowsRemoved(QModelIndex, int, int)), this,
|
|
|
|
SLOT(RowsRemoved(QModelIndex, int, int)));
|
|
|
|
disconnect(sourceModel(), SIGNAL(dataChanged(QModelIndex, QModelIndex)),
|
|
|
|
this, SLOT(DataChanged(QModelIndex, QModelIndex)));
|
|
|
|
disconnect(sourceModel(), SIGNAL(layoutAboutToBeChanged()), this,
|
|
|
|
SLOT(LayoutAboutToBeChanged()));
|
|
|
|
disconnect(sourceModel(), SIGNAL(layoutChanged()), this,
|
|
|
|
SLOT(LayoutChanged()));
|
2010-05-09 17:51:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QAbstractProxyModel::setSourceModel(source_model);
|
|
|
|
|
|
|
|
connect(sourceModel(), SIGNAL(modelReset()), this, SLOT(SourceModelReset()));
|
2014-02-07 16:34:20 +01:00
|
|
|
connect(sourceModel(), SIGNAL(rowsAboutToBeInserted(QModelIndex, int, int)),
|
|
|
|
this, SLOT(RowsAboutToBeInserted(QModelIndex, int, int)));
|
|
|
|
connect(sourceModel(), SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)),
|
|
|
|
this, SLOT(RowsAboutToBeRemoved(QModelIndex, int, int)));
|
|
|
|
connect(sourceModel(), SIGNAL(rowsInserted(QModelIndex, int, int)), this,
|
|
|
|
SLOT(RowsInserted(QModelIndex, int, int)));
|
|
|
|
connect(sourceModel(), SIGNAL(rowsRemoved(QModelIndex, int, int)), this,
|
|
|
|
SLOT(RowsRemoved(QModelIndex, int, int)));
|
|
|
|
connect(sourceModel(), SIGNAL(dataChanged(QModelIndex, QModelIndex)), this,
|
|
|
|
SLOT(DataChanged(QModelIndex, QModelIndex)));
|
|
|
|
connect(sourceModel(), SIGNAL(layoutAboutToBeChanged()), this,
|
|
|
|
SLOT(LayoutAboutToBeChanged()));
|
|
|
|
connect(sourceModel(), SIGNAL(layoutChanged()), this, SLOT(LayoutChanged()));
|
2010-05-09 17:51:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MergedProxyModel::SourceModelReset() {
|
|
|
|
// Delete all mappings
|
2010-05-09 17:53:28 +02:00
|
|
|
DeleteAllMappings();
|
2010-05-09 17:51:04 +02:00
|
|
|
|
|
|
|
// Clear the containers
|
2014-02-06 17:44:29 +01:00
|
|
|
p_->mappings_.clear();
|
2010-05-09 17:51:04 +02:00
|
|
|
merge_points_.clear();
|
|
|
|
|
|
|
|
// Reset the proxy
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MergedProxyModel::SubModelReset() {
|
2010-05-11 20:01:30 +02:00
|
|
|
QAbstractItemModel* submodel = static_cast<QAbstractItemModel*>(sender());
|
2010-05-09 17:51:04 +02:00
|
|
|
|
2014-11-02 21:37:15 +01:00
|
|
|
// TODO(David Sansome): When we require Qt 4.6, use beginResetModel() and
|
|
|
|
// endResetModel() in LibraryModel and catch those here - that will let
|
|
|
|
// us do away with this std::numeric_limits<int>::max() hack.
|
2010-05-09 23:16:54 +02:00
|
|
|
|
|
|
|
// Remove all the children of the item that got deleted
|
|
|
|
QModelIndex source_parent = merge_points_.value(submodel);
|
|
|
|
QModelIndex proxy_parent = mapFromSource(source_parent);
|
|
|
|
|
|
|
|
// We can't know how many children it had, since it's already disappeared...
|
|
|
|
resetting_model_ = submodel;
|
|
|
|
beginRemoveRows(proxy_parent, 0, std::numeric_limits<int>::max() - 1);
|
|
|
|
endRemoveRows();
|
2014-02-06 16:49:49 +01:00
|
|
|
resetting_model_ = nullptr;
|
2010-05-09 23:16:54 +02:00
|
|
|
|
2010-05-09 17:51:04 +02:00
|
|
|
// Delete all the mappings that reference the submodel
|
2014-02-06 17:44:29 +01:00
|
|
|
auto it = p_->mappings_.get<tag_by_pointer>().begin();
|
|
|
|
auto end = p_->mappings_.get<tag_by_pointer>().end();
|
2010-05-09 17:51:04 +02:00
|
|
|
while (it != end) {
|
|
|
|
if ((*it)->source_index.model() == submodel) {
|
|
|
|
delete *it;
|
2014-02-06 17:44:29 +01:00
|
|
|
it = p_->mappings_.get<tag_by_pointer>().erase(it);
|
2010-05-09 17:51:04 +02:00
|
|
|
} else {
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-09 23:16:54 +02:00
|
|
|
// "Insert" items from the newly reset submodel
|
|
|
|
int count = submodel->rowCount();
|
|
|
|
if (count) {
|
2014-02-07 16:34:20 +01:00
|
|
|
beginInsertRows(proxy_parent, 0, count - 1);
|
2010-05-09 23:16:54 +02:00
|
|
|
endInsertRows();
|
|
|
|
}
|
2010-05-17 01:44:33 +02:00
|
|
|
|
|
|
|
emit SubModelReset(proxy_parent, submodel);
|
2010-05-09 17:51:04 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QModelIndex MergedProxyModel::GetActualSourceParent(
|
|
|
|
const QModelIndex& source_parent, QAbstractItemModel* model) const {
|
2010-05-09 17:51:04 +02:00
|
|
|
if (!source_parent.isValid() && model != sourceModel())
|
|
|
|
return merge_points_.value(model);
|
|
|
|
return source_parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MergedProxyModel::RowsAboutToBeInserted(const QModelIndex& source_parent,
|
|
|
|
int start, int end) {
|
2014-02-07 16:34:20 +01:00
|
|
|
beginInsertRows(
|
|
|
|
mapFromSource(GetActualSourceParent(
|
|
|
|
source_parent, static_cast<QAbstractItemModel*>(sender()))),
|
2010-05-09 17:51:04 +02:00
|
|
|
start, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MergedProxyModel::RowsInserted(const QModelIndex&, int, int) {
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MergedProxyModel::RowsAboutToBeRemoved(const QModelIndex& source_parent,
|
|
|
|
int start, int end) {
|
2014-02-07 16:34:20 +01:00
|
|
|
beginRemoveRows(
|
|
|
|
mapFromSource(GetActualSourceParent(
|
|
|
|
source_parent, static_cast<QAbstractItemModel*>(sender()))),
|
2010-05-09 17:51:04 +02:00
|
|
|
start, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MergedProxyModel::RowsRemoved(const QModelIndex&, int, int) {
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QModelIndex MergedProxyModel::mapToSource(const QModelIndex& proxy_index)
|
|
|
|
const {
|
|
|
|
if (!proxy_index.isValid()) return QModelIndex();
|
2010-05-09 17:51:04 +02:00
|
|
|
|
|
|
|
Mapping* mapping = static_cast<Mapping*>(proxy_index.internalPointer());
|
2014-02-06 17:44:29 +01:00
|
|
|
if (p_->mappings_.get<tag_by_pointer>().find(mapping) ==
|
|
|
|
p_->mappings_.get<tag_by_pointer>().end())
|
2010-05-09 23:16:54 +02:00
|
|
|
return QModelIndex();
|
2014-02-07 16:34:20 +01:00
|
|
|
if (mapping->source_index.model() == resetting_model_) return QModelIndex();
|
2010-05-09 23:16:54 +02:00
|
|
|
|
2010-05-09 17:51:04 +02:00
|
|
|
return mapping->source_index;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QModelIndex MergedProxyModel::mapFromSource(const QModelIndex& source_index)
|
|
|
|
const {
|
|
|
|
if (!source_index.isValid()) return QModelIndex();
|
|
|
|
if (source_index.model() == resetting_model_) return QModelIndex();
|
2010-05-09 17:51:04 +02:00
|
|
|
|
|
|
|
// Add a mapping if we don't have one already
|
2014-02-07 16:34:20 +01:00
|
|
|
const auto& it = p_->mappings_.get<tag_by_source>().find(source_index);
|
2010-05-09 17:51:04 +02:00
|
|
|
Mapping* mapping;
|
2014-02-06 17:44:29 +01:00
|
|
|
if (it != p_->mappings_.get<tag_by_source>().end()) {
|
2010-05-09 17:51:04 +02:00
|
|
|
mapping = *it;
|
|
|
|
} else {
|
|
|
|
mapping = new Mapping(source_index);
|
2014-02-06 17:44:29 +01:00
|
|
|
const_cast<MergedProxyModel*>(this)->p_->mappings_.insert(mapping);
|
2010-05-09 17:51:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return createIndex(source_index.row(), source_index.column(), mapping);
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QModelIndex MergedProxyModel::index(int row, int column,
|
|
|
|
const QModelIndex& parent) const {
|
2010-05-09 17:51:04 +02:00
|
|
|
QModelIndex source_index;
|
|
|
|
|
|
|
|
if (!parent.isValid()) {
|
|
|
|
source_index = sourceModel()->index(row, column, QModelIndex());
|
|
|
|
} else {
|
|
|
|
QModelIndex source_parent = mapToSource(parent);
|
|
|
|
const QAbstractItemModel* child_model = merge_points_.key(source_parent);
|
|
|
|
|
|
|
|
if (child_model)
|
|
|
|
source_index = child_model->index(row, column, QModelIndex());
|
|
|
|
else
|
|
|
|
source_index = source_parent.model()->index(row, column, source_parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mapFromSource(source_index);
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QModelIndex MergedProxyModel::parent(const QModelIndex& child) const {
|
2010-05-09 17:51:04 +02:00
|
|
|
QModelIndex source_child = mapToSource(child);
|
|
|
|
if (source_child.model() == sourceModel())
|
|
|
|
return mapFromSource(source_child.parent());
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!IsKnownModel(source_child.model())) return QModelIndex();
|
2010-12-29 00:21:56 +01:00
|
|
|
|
2010-05-09 17:51:04 +02:00
|
|
|
if (!source_child.parent().isValid())
|
2010-05-11 20:01:30 +02:00
|
|
|
return mapFromSource(merge_points_.value(GetModel(source_child)));
|
2010-05-09 17:51:04 +02:00
|
|
|
return mapFromSource(source_child.parent());
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
int MergedProxyModel::rowCount(const QModelIndex& parent) const {
|
|
|
|
if (!parent.isValid()) return sourceModel()->rowCount(QModelIndex());
|
2010-05-09 17:51:04 +02:00
|
|
|
|
|
|
|
QModelIndex source_parent = mapToSource(parent);
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!IsKnownModel(source_parent.model())) return 0;
|
2010-12-29 00:21:56 +01:00
|
|
|
|
2010-05-09 17:51:04 +02:00
|
|
|
const QAbstractItemModel* child_model = merge_points_.key(source_parent);
|
2010-05-09 18:53:35 +02:00
|
|
|
if (child_model) {
|
|
|
|
// Query the source model but disregard what it says, so it gets a chance
|
|
|
|
// to lazy load
|
|
|
|
source_parent.model()->rowCount(source_parent);
|
|
|
|
|
2010-05-09 17:51:04 +02:00
|
|
|
return child_model->rowCount(QModelIndex());
|
2010-05-09 18:53:35 +02:00
|
|
|
}
|
|
|
|
|
2010-05-09 17:51:04 +02:00
|
|
|
return source_parent.model()->rowCount(source_parent);
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
int MergedProxyModel::columnCount(const QModelIndex& parent) const {
|
|
|
|
if (!parent.isValid()) return sourceModel()->columnCount(QModelIndex());
|
2010-05-09 17:51:04 +02:00
|
|
|
|
|
|
|
QModelIndex source_parent = mapToSource(parent);
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!IsKnownModel(source_parent.model())) return 0;
|
2010-12-29 00:21:56 +01:00
|
|
|
|
2010-05-09 17:51:04 +02:00
|
|
|
const QAbstractItemModel* child_model = merge_points_.key(source_parent);
|
2014-02-07 16:34:20 +01:00
|
|
|
if (child_model) return child_model->columnCount(QModelIndex());
|
2010-05-09 17:51:04 +02:00
|
|
|
return source_parent.model()->columnCount(source_parent);
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
bool MergedProxyModel::hasChildren(const QModelIndex& parent) const {
|
|
|
|
if (!parent.isValid()) return sourceModel()->hasChildren(QModelIndex());
|
2010-05-09 17:51:04 +02:00
|
|
|
|
|
|
|
QModelIndex source_parent = mapToSource(parent);
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!IsKnownModel(source_parent.model())) return false;
|
2010-12-29 00:21:56 +01:00
|
|
|
|
2010-05-09 17:51:04 +02:00
|
|
|
const QAbstractItemModel* child_model = merge_points_.key(source_parent);
|
|
|
|
|
|
|
|
if (child_model)
|
2010-05-09 18:53:35 +02:00
|
|
|
return child_model->hasChildren(QModelIndex()) ||
|
|
|
|
source_parent.model()->hasChildren(source_parent);
|
2010-05-09 17:51:04 +02:00
|
|
|
return source_parent.model()->hasChildren(source_parent);
|
|
|
|
}
|
|
|
|
|
2010-12-29 00:21:56 +01:00
|
|
|
QVariant MergedProxyModel::data(const QModelIndex& proxyIndex, int role) const {
|
2010-05-09 17:51:04 +02:00
|
|
|
QModelIndex source_index = mapToSource(proxyIndex);
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!IsKnownModel(source_index.model())) return QVariant();
|
2010-12-29 00:21:56 +01:00
|
|
|
|
2010-05-09 17:51:04 +02:00
|
|
|
return source_index.model()->data(source_index, role);
|
|
|
|
}
|
2010-05-09 19:19:48 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QMap<int, QVariant> MergedProxyModel::itemData(const QModelIndex& proxy_index)
|
|
|
|
const {
|
2010-05-09 19:19:48 +02:00
|
|
|
QModelIndex source_index = mapToSource(proxy_index);
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!source_index.isValid()) return sourceModel()->itemData(QModelIndex());
|
2010-05-09 19:19:48 +02:00
|
|
|
return source_index.model()->itemData(source_index);
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
Qt::ItemFlags MergedProxyModel::flags(const QModelIndex& index) const {
|
2010-05-09 19:19:48 +02:00
|
|
|
QModelIndex source_index = mapToSource(index);
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!source_index.isValid()) return sourceModel()->flags(QModelIndex());
|
2010-05-09 19:19:48 +02:00
|
|
|
return source_index.model()->flags(source_index);
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
bool MergedProxyModel::setData(const QModelIndex& index, const QVariant& value,
|
2010-05-09 19:19:48 +02:00
|
|
|
int role) {
|
|
|
|
QModelIndex source_index = mapToSource(index);
|
|
|
|
|
|
|
|
if (!source_index.isValid())
|
|
|
|
return sourceModel()->setData(index, value, role);
|
2010-05-11 20:01:30 +02:00
|
|
|
return GetModel(index)->setData(index, value, role);
|
2010-05-09 19:19:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QStringList MergedProxyModel::mimeTypes() const {
|
|
|
|
QStringList ret;
|
|
|
|
ret << sourceModel()->mimeTypes();
|
|
|
|
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const QAbstractItemModel* model : merge_points_.keys()) {
|
2010-05-09 19:19:48 +02:00
|
|
|
ret << model->mimeTypes();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QMimeData* MergedProxyModel::mimeData(const QModelIndexList& indexes) const {
|
|
|
|
if (indexes.isEmpty()) return 0;
|
2010-05-09 19:19:48 +02:00
|
|
|
|
|
|
|
// Only ask the first index's model
|
|
|
|
const QAbstractItemModel* model = mapToSource(indexes[0]).model();
|
2011-09-22 19:50:43 +02:00
|
|
|
if (!model) {
|
|
|
|
return 0;
|
|
|
|
}
|
2010-05-09 19:19:48 +02:00
|
|
|
|
|
|
|
// Only ask about the indexes that are actually in that model
|
|
|
|
QModelIndexList indexes_in_model;
|
|
|
|
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const QModelIndex& proxy_index : indexes) {
|
2010-05-09 19:19:48 +02:00
|
|
|
QModelIndex source_index = mapToSource(proxy_index);
|
2014-02-07 16:34:20 +01:00
|
|
|
if (source_index.model() != model) continue;
|
2010-05-09 19:19:48 +02:00
|
|
|
indexes_in_model << source_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
return model->mimeData(indexes_in_model);
|
|
|
|
}
|
2011-11-02 00:02:49 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
bool MergedProxyModel::dropMimeData(const QMimeData* data,
|
|
|
|
Qt::DropAction action, int row, int column,
|
|
|
|
const QModelIndex& parent) {
|
2011-10-30 23:59:43 +01:00
|
|
|
if (!parent.isValid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sourceModel()->dropMimeData(data, action, row, column, parent);
|
|
|
|
}
|
2010-05-09 19:53:27 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QModelIndex MergedProxyModel::FindSourceParent(const QModelIndex& proxy_index)
|
|
|
|
const {
|
|
|
|
if (!proxy_index.isValid()) return QModelIndex();
|
2010-05-09 19:53:27 +02:00
|
|
|
|
|
|
|
QModelIndex source_index = mapToSource(proxy_index);
|
2014-02-07 16:34:20 +01:00
|
|
|
if (source_index.model() == sourceModel()) return source_index;
|
2010-05-11 20:01:30 +02:00
|
|
|
return merge_points_.value(GetModel(source_index));
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
bool MergedProxyModel::canFetchMore(const QModelIndex& parent) const {
|
2010-05-11 20:01:30 +02:00
|
|
|
QModelIndex source_index = mapToSource(parent);
|
|
|
|
|
|
|
|
if (!source_index.isValid())
|
|
|
|
return sourceModel()->canFetchMore(QModelIndex());
|
|
|
|
return source_index.model()->canFetchMore(source_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MergedProxyModel::fetchMore(const QModelIndex& parent) {
|
|
|
|
QModelIndex source_index = mapToSource(parent);
|
|
|
|
|
|
|
|
if (!source_index.isValid())
|
|
|
|
sourceModel()->fetchMore(QModelIndex());
|
|
|
|
else
|
|
|
|
GetModel(source_index)->fetchMore(source_index);
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QAbstractItemModel* MergedProxyModel::GetModel(const QModelIndex& source_index)
|
|
|
|
const {
|
2010-05-11 20:01:30 +02:00
|
|
|
// This is essentially const_cast<QAbstractItemModel*>(source_index.model()),
|
|
|
|
// but without the const_cast
|
|
|
|
const QAbstractItemModel* const_model = source_index.model();
|
2014-02-07 16:34:20 +01:00
|
|
|
if (const_model == sourceModel()) return sourceModel();
|
2014-02-10 14:29:07 +01:00
|
|
|
for (QAbstractItemModel* submodel : merge_points_.keys()) {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (submodel == const_model) return submodel;
|
2010-05-11 20:01:30 +02:00
|
|
|
}
|
2014-02-06 16:49:49 +01:00
|
|
|
return nullptr;
|
2010-05-09 19:53:27 +02:00
|
|
|
}
|
2010-07-04 13:43:17 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void MergedProxyModel::DataChanged(const QModelIndex& top_left,
|
|
|
|
const QModelIndex& bottom_right) {
|
2010-07-04 13:43:17 +02:00
|
|
|
emit dataChanged(mapFromSource(top_left), mapFromSource(bottom_right));
|
|
|
|
}
|
2010-09-18 14:33:23 +02:00
|
|
|
|
|
|
|
void MergedProxyModel::LayoutAboutToBeChanged() {
|
|
|
|
old_merge_points_.clear();
|
2014-02-10 14:29:07 +01:00
|
|
|
for (QAbstractItemModel* key : merge_points_.keys()) {
|
2010-09-18 14:33:23 +02:00
|
|
|
old_merge_points_[key] = merge_points_.value(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MergedProxyModel::LayoutChanged() {
|
2014-02-10 14:29:07 +01:00
|
|
|
for (QAbstractItemModel* key : merge_points_.keys()) {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!old_merge_points_.contains(key)) continue;
|
2010-09-18 14:33:23 +02:00
|
|
|
|
|
|
|
const int old_row = old_merge_points_[key].row();
|
|
|
|
const int new_row = merge_points_[key].row();
|
|
|
|
|
|
|
|
if (old_row != new_row) {
|
|
|
|
reset();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-12-29 00:21:56 +01:00
|
|
|
|
|
|
|
bool MergedProxyModel::IsKnownModel(const QAbstractItemModel* model) const {
|
|
|
|
if (model == this || model == sourceModel() ||
|
|
|
|
merge_points_.contains(const_cast<QAbstractItemModel*>(model)))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
2011-01-10 23:26:13 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QModelIndexList MergedProxyModel::mapFromSource(
|
|
|
|
const QModelIndexList& source_indexes) const {
|
2011-01-10 23:26:13 +01:00
|
|
|
QModelIndexList ret;
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const QModelIndex& index : source_indexes) {
|
2011-01-10 23:26:13 +01:00
|
|
|
ret << mapFromSource(index);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QModelIndexList MergedProxyModel::mapToSource(
|
|
|
|
const QModelIndexList& proxy_indexes) const {
|
2011-01-10 23:26:13 +01:00
|
|
|
QModelIndexList ret;
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const QModelIndex& index : proxy_indexes) {
|
2011-01-10 23:26:13 +01:00
|
|
|
ret << mapToSource(index);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|