LeechCraft 0.6.70-19214-g121e5318a7
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
lazyrootmixin.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 <concepts>
12#include <functional>
13#include <utility>
14#include <QAbstractItemModel>
15
16namespace LC::Util
17{
36 template<std::derived_from<QAbstractItemModel> Model>
37 class LazyRootMixin : public Model
38 {
39 const std::function<void ()> Load_;
40 bool Loaded_ = false;
41 public:
49 template<typename... Args>
50 explicit LazyRootMixin (std::function<void ()> load, Args&&... args)
51 : Model { std::forward<Args> (args)... }
52 , Load_ { std::move (load) }
53 {
54 }
55
58 bool IsLoaded () const
59 {
60 return Loaded_;
61 }
62
66 {
67 if (!Loaded_)
68 Reload ();
69 }
70
73 void Reload ()
74 {
75 Loaded_ = true;
76 this->removeRows (0, this->rowCount ());
77 Load_ ();
78 }
79
80 bool hasChildren (const QModelIndex& parent) const override
81 {
82 return IsUnloadedRoot (parent) || Model::hasChildren (parent);
83 }
84
85 bool canFetchMore (const QModelIndex& parent) const override
86 {
87 return IsUnloadedRoot (parent) || Model::canFetchMore (parent);
88 }
89
90 void fetchMore (const QModelIndex& parent) override
91 {
92 if (IsUnloadedRoot (parent))
93 Reload ();
94 else
95 Model::fetchMore (parent);
96 }
97 private:
98 bool IsUnloadedRoot (const QModelIndex& parent) const
99 {
100 return !parent.isValid () && !Loaded_;
101 }
102 };
103}
bool IsLoaded() const
Returns whether the model has been populated.
bool canFetchMore(const QModelIndex &parent) const override
void fetchMore(const QModelIndex &parent) override
void EnsureLoaded()
Populates the model if it hasn't been populated yet.
bool hasChildren(const QModelIndex &parent) const override
LazyRootMixin(std::function< void()> load, Args &&... args)
Constructs the model passing the arguments to the base constructor.
void Reload()
Drops the rows and populates the model anew.
@ Model
The human-readable name of the device model (QString).