LeechCraft 0.6.70-19214-g121e5318a7
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
selectionproxymodel.h
Go to the documentation of this file.
1/**********************************************************************
2 * LeechCraft - modular cross-platform feature rich internet client.
3 * Copyright (C) 2006-2014 Georg Rudoy
4 *
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7 **********************************************************************/
8
9#pragma once
10
11#include <QIdentityProxyModel>
12#include "modelsanitizer.h"
13
14namespace LC::Util
15{
16 template<typename Id>
17 class SelectionProxyModel : public QIdentityProxyModel
18 {
19 public:
20 struct Config
21 {
24 const std::function<QModelIndexList (QSet<Id>)> FindItems_;
25 };
26 private:
27 const Config Config_;
28
29 QSet<Id> Selections_;
30 public:
31 explicit SelectionProxyModel (QAbstractItemModel& source, const Config& config, QObject *parent = nullptr)
32 : QIdentityProxyModel { parent }
33 , Config_ { config }
34 {
35 QIdentityProxyModel::setSourceModel (&source);
37 }
38
39 int GetIsSelectedRole () const
40 {
41 return Config_.IsSelectedRole_;
42 }
43
44 void SetSelections (const QSet<Id>& selections)
45 {
46 if (Selections_ == selections)
47 return;
48
49 const auto changed = (Selections_ | selections) - (Selections_ & selections);
50 Selections_ = selections;
51 EmitByIds (changed);
52 }
53
54 QVariant data (const QModelIndex& index, int role) const override
55 {
56 if (role != Config_.IsSelectedRole_ || !index.isValid ())
57 return QIdentityProxyModel::data (index, role);
58
59 const auto id = index.data (Config_.SourceIdRole_).template value<Id> ();
60 return Selections_.contains (id);
61 }
62 private:
63 void EmitByIds (const QSet<Id>& ids)
64 {
65 const auto lastColumn = sourceModel ()->columnCount () - 1;
66 for (const auto& idx : Config_.FindItems_ (ids))
67 emit dataChanged (mapFromSource (idx.siblingAtColumn (0)),
68 mapFromSource (idx.siblingAtColumn (lastColumn)),
69 { Config_.IsSelectedRole_ });
70 }
71 };
72}
SelectionProxyModel(QAbstractItemModel &source, const Config &config, QObject *parent=nullptr)
void SetSelections(const QSet< Id > &selections)
QVariant data(const QModelIndex &index, int role) const override
void InstallModelSanitizer(QAbstractItemModel &model)
const std::function< QModelIndexList(QSet< Id >)> FindItems_