LeechCraft 0.6.70-19424-g027e980fd1
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
flattofoldersproxymodel.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 <functional>
12#include <memory>
13#include <QAbstractItemModel>
14#include <QStringList>
15#include <QMultiHash>
16#include "modelsconfig.h"
17
18namespace LC::Util
19{
20 struct FlatTreeItem;
21 using FlatTreeItem_ptr = std::shared_ptr<FlatTreeItem>;
22
46 class UTIL_MODELS_API FlatToFoldersProxyModel : public QAbstractItemModel
47 {
48 public:
49 struct Config
50 {
53 std::function<QStringList (const QModelIndex&)> KeysOf_;
55 QString DropFormat_;
56 };
57 private:
58 const Config Config_;
59
60 QAbstractItemModel *SourceModel_ = nullptr;
61
62 FlatTreeItem_ptr Root_;
63 QMultiHash<QPersistentModelIndex, FlatTreeItem_ptr> Items_;
64 public:
65 explicit FlatToFoldersProxyModel (Config, QObject* = nullptr);
66
67 int columnCount (const QModelIndex& = {}) const override;
68 QVariant data (const QModelIndex&, int = Qt::DisplayRole) const override;
69 QVariant headerData (int, Qt::Orientation, int) const override;
70 Qt::ItemFlags flags (const QModelIndex&) const override;
71 bool setData (const QModelIndex&, const QVariant&, int = Qt::EditRole) override;
72 QModelIndex index (int, int, const QModelIndex& = {}) const override;
73 QModelIndex parent (const QModelIndex&) const override;
74 int rowCount (const QModelIndex& = {}) const override;
75
76 Qt::DropActions supportedDropActions () const override;
77 QStringList mimeTypes () const override;
78 QMimeData* mimeData (const QModelIndexList& indexes) const override;
79 bool dropMimeData (const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex& parent) override;
80
81 void SetSourceModel (QAbstractItemModel*);
82 QAbstractItemModel* GetSourceModel () const;
83 QModelIndex MapToSource (const QModelIndex&) const;
84 QList<QModelIndex> MapFromSource (const QModelIndex&) const;
85
86 bool IsFolder (const QModelIndex&) const;
87 QList<QModelIndex> GetChildren (const QModelIndex&) const;
88 QList<QVariant> GetChildrenData (const QModelIndex& index, int role) const;
89
90 template<typename T>
91 QList<T> GetChildrenData (const QModelIndex& index, int role) const
92 {
93 QList<T> result;
94 for (const auto& var : GetChildrenData (index, role))
95 result.push_back (var.value<T> ());
96 return result;
97 }
98
107 void ReplaceKey (const QString& old, const QStringList& newKeys);
108 private:
109 QStringList KeysOf (const QModelIndex&) const;
110 const FlatTreeItem& ToFlatOrRoot (const QModelIndex&) const;
111 QModelIndex IndexOf (const FlatTreeItem&, int column = 0) const;
112
113 FlatTreeItem_ptr FindFolder (const QString&) const;
114 FlatTreeItem_ptr GetFolder (const QString&);
115 void AddForKey (const QString&, const QPersistentModelIndex&);
116 void RemoveLeaf (const FlatTreeItem_ptr&);
117 void UpdateLeaves (const QPersistentModelIndex&, const QList<int>& roles);
118 void RenameFolder (FlatTreeItem&, const QString&);
119
120 void HandleDataChanged (const QModelIndex&, const QModelIndex&, const QList<int>&);
121 void HandleModelReset ();
122 void HandleRowsInserted (const QModelIndex&, int, int);
123 void HandleRowsAboutToBeRemoved (const QModelIndex&, int, int);
124 };
125}
QList< T > GetChildrenData(const QModelIndex &index, int role) const
FlatToFoldersProxyModel(Config, QObject *=nullptr)
QList< QVariant > GetChildrenData(const QModelIndex &index, int role) const
int columnCount(const QModelIndex &={}) const override
QModelIndex index(int, int, const QModelIndex &={}) const override
#define UTIL_MODELS_API
std::shared_ptr< FlatTreeItem > FlatTreeItem_ptr
std::function< QStringList(const QModelIndex &)> KeysOf_