X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fclient%2Fmappedselectionmodel.cpp;fp=src%2Fclient%2Fmappedselectionmodel.cpp;h=84cd9fe050947d42c3fff7ebe35ac0bf4ceb5518;hp=99b1e4fdbbf72467f0f6b4290fe1b5829ec56b43;hb=e190d576540cf5dfb2e63d33a69662083f3db210;hpb=4ea3ce9e1ff6df8b1d2a257da7e5fbba9f6cbfbc diff --git a/src/client/mappedselectionmodel.cpp b/src/client/mappedselectionmodel.cpp index 99b1e4fd..84cd9fe0 100644 --- a/src/client/mappedselectionmodel.cpp +++ b/src/client/mappedselectionmodel.cpp @@ -23,89 +23,60 @@ #include #include #include +#include #include MappedSelectionModel::MappedSelectionModel(QAbstractItemModel *model) : QItemSelectionModel(model) { - _isProxyModel = (bool)proxyModel(); - connect(this, SIGNAL(currentChanged(QModelIndex, QModelIndex)), - this, SLOT(_currentChanged(QModelIndex, QModelIndex))); - connect(this, SIGNAL(selectionChanged(QItemSelection, QItemSelection)), - this, SLOT(_selectionChanged(QItemSelection, QItemSelection))); -} - -MappedSelectionModel::~MappedSelectionModel() { -} - -const QAbstractItemModel *MappedSelectionModel::baseModel() const { - if(isProxyModel()) - return proxyModel()->sourceModel(); - else - return model(); -} - -const QAbstractProxyModel *MappedSelectionModel::proxyModel() const { - return qobject_cast(model()); } QModelIndex MappedSelectionModel::mapFromSource(const QModelIndex &sourceIndex) { - if(isProxyModel()) - return proxyModel()->mapFromSource(sourceIndex); - else - return sourceIndex; + QModelIndex proxyIndex = sourceIndex; + QLinkedList proxies; + const QAbstractItemModel *baseModel = model(); + const QAbstractProxyModel *proxyModel = 0; + while((proxyModel = qobject_cast(baseModel)) != 0) { + proxies.push_back(proxyModel); + baseModel = proxyModel->sourceModel(); + if(baseModel == sourceIndex.model()) + break; + } + + while(!proxies.isEmpty()) { + proxyModel = proxies.takeLast(); + proxyIndex = proxyModel->mapFromSource(proxyIndex); + } + return proxyIndex; } QItemSelection MappedSelectionModel::mapSelectionFromSource(const QItemSelection &sourceSelection) { - if(isProxyModel()) - return proxyModel()->mapSelectionFromSource(sourceSelection); - else + if(sourceSelection.isEmpty()) return sourceSelection; -} - -QModelIndex MappedSelectionModel::mapToSource(const QModelIndex &proxyIndex) { - if(isProxyModel()) - return proxyModel()->mapToSource(proxyIndex); - else - return proxyIndex; -} -QItemSelection MappedSelectionModel::mapSelectionToSource(const QItemSelection &proxySelection) { - if(isProxyModel()) - return proxyModel()->mapSelectionToSource(proxySelection); - else - return proxySelection; -} - -void MappedSelectionModel::mappedSelect(const QModelIndex &index, QItemSelectionModel::SelectionFlags command) { - QModelIndex mappedIndex = mapFromSource(index); - if(!isSelected(mappedIndex)) - select(mappedIndex, command); -} + QItemSelection proxySelection = sourceSelection; + QLinkedList proxies; + const QAbstractItemModel *baseModel = model(); + const QAbstractProxyModel *proxyModel = 0; + while((proxyModel = qobject_cast(baseModel)) != 0) { + proxies.push_back(proxyModel); + baseModel = proxyModel->sourceModel(); + if(baseModel == sourceSelection.first().model()) + break; + } -void MappedSelectionModel::mappedSelect(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command) { - QItemSelection mappedSelection = mapSelectionFromSource(selection); - if(mappedSelection != QItemSelectionModel::selection()) - select(mappedSelection, command); + while(!proxies.isEmpty()) { + proxyModel = proxies.takeLast(); + proxySelection = proxyModel->mapSelectionFromSource(proxySelection); + } + return proxySelection; } - + void MappedSelectionModel::mappedSetCurrentIndex(const QModelIndex &index, QItemSelectionModel::SelectionFlags command) { - QModelIndex mappedIndex = mapFromSource(index); - if(mappedIndex == currentIndex()) - return; - setCurrentIndex(mappedIndex, command); + setCurrentIndex(mapFromSource(index), command); } - -void MappedSelectionModel::_currentChanged(const QModelIndex ¤t, const QModelIndex &previous) { - Q_UNUSED(previous) - if(current.isValid()) - emit mappedCurrentChanged(mapToSource(current)); -} - -void MappedSelectionModel::_selectionChanged(const QItemSelection &selected, const QItemSelection &deselected) { - Q_UNUSED(selected) - Q_UNUSED(deselected) - emit mappedSelectionChanged(mapSelectionToSource(QItemSelectionModel::selection())); +void MappedSelectionModel::mappedSelect(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command) { + select(mapSelectionFromSource(selection), command); }