LeechCraft 0.6.70-17335-ge406ffdcaf
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
13namespace LC::Util
14{
15 template<typename Id>
16 class SelectionProxyModel : public QIdentityProxyModel
17 {
18 public:
19 struct Config
20 {
23 const std::function<QModelIndexList (QSet<Id>)> FindItems_;
24 };
25 private:
26 const Config Config_;
27
28 QSet<Id> Selections_;
29 public:
30 explicit SelectionProxyModel (QAbstractItemModel& source, const Config& config, QObject *parent = nullptr)
31 : QIdentityProxyModel { parent }
32 , Config_ { config }
33 {
34 QIdentityProxyModel::setSourceModel (&source);
35 }
36
37 int GetIsSelectedRole () const
38 {
39 return Config_.IsSelectedRole_;
40 }
41
42 void SetSelections (const QSet<Id>& selections)
43 {
44 if (Selections_ == selections)
45 return;
46
47 EmitByIds (std::exchange (Selections_, {}));
48 Selections_ = selections;
49 EmitByIds (Selections_);
50 }
51
52 QVariant data (const QModelIndex& index, int role) const override
53 {
54 if (role != Config_.IsSelectedRole_)
55 return QIdentityProxyModel::data (index, role);
56
57 const auto id = index.data (Config_.SourceIdRole_).template value<Id> ();
58 return Selections_.contains (id);
59 }
60 private:
61 void EmitByIds (const QSet<Id>& ids)
62 {
63 const auto lastColumn = sourceModel ()->columnCount () - 1;
64 for (const auto& idx : Config_.FindItems_ (ids))
65 emit dataChanged (idx.siblingAtColumn (0), idx.siblingAtColumn (lastColumn), { Config_.IsSelectedRole_ });
66 }
67 };
68}
SelectionProxyModel(QAbstractItemModel &source, const Config &config, QObject *parent=nullptr)
void SetSelections(const QSet< Id > &selections)
QVariant data(const QModelIndex &index, int role) const override
const std::function< QModelIndexList(QSet< Id >)> FindItems_