Messagelib

modelinvariantrowmapper.h
1/******************************************************************************
2 *
3 * SPDX-FileCopyrightText: 2008 Szymon Tomasz Stefanek <pragma@kvirc.net>
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 *
7 *******************************************************************************/
8
9#pragma once
10
11#include <QHash>
12#include <QList>
13#include <QObject>
14
15#include "core/modelinvariantindex.h"
16#include <memory>
17namespace MessageList
18{
19namespace Core
20{
21class ModelInvariantRowMapper;
22class ModelInvariantRowMapperPrivate;
23
24/**
25 * This class is an optimizing helper for dealing with large flat QAbstractItemModel objects.
26 *
27 * The problem:
28 *
29 * When you're an user of a _flat_ QAbstractItemModel you access its contents
30 * by the means of QModelIndex. The model index is basically a row index (for flat models).
31 * You usually fetch some data for a row and then store the row index in order
32 * to fetch more data later (think of a tree view that shows the "name" for an item
33 * but when clicked needs to open a window with the details associated to the item).
34 *
35 * The problem is that your row indexes may become invalid when rows are added
36 * or removed from the model. For instance, if a row is added at position 10,
37 * then any cached index after position 10 must be updated in order to point to
38 * the same content. With very large models this can become a problem since
39 * the update must be somewhat "atomic" in order to preserve consistency.
40 * Atomic, then, means "unbreakable": you can't chunk it in smaller pieces.
41 * This also means that your application will simply freeze if you have a model
42 * with 100000 cached indexes and a row is added/removed at the beginning.
43 * Yet worse: your app will freeze EVERY time a row is added. This means
44 * that if you don't really optimize, or just add non contiguous rows, you
45 * must do the update multiple times...
46 *
47 * This class tries to solve this problem with a little overhead.
48 * It basically gives you a ModelInvariantIndex for each "new" row you query.
49 * Later the model may change by addition/removal of rows but with a ModelInvariantIndex
50 * you will still be able to retrieve the content that the index was pointing
51 * to at the time it was created.
52 *
53 * You don't need to implement any row update in your rowsInserted() / rowsRemoved()
54 * handlers. Just call the modelRowsInserted() modelRowsRemoved() functions:
55 * they will do everything for you in a substantially constant time.
56 *
57 * As the model structure changes the lookups will get a bit slower
58 * since the row mapper must apply the changes sequentially to each invariant.
59 * To avoid this, and to avoid storing the whole history of changes
60 * the ModelInvariantRowMapper will perform a background update of your
61 * ModelInvariantIndex objects. You don't need to care about this: it will happen automagically.
62 *
63 * The ModelInvariantIndex allocation and destruction is in fact left to you.
64 * This is a GOOD approach because you're very likely to have some sort
65 * of structures associated to the items you display. You may then simply
66 * derive your objects from ModelInvariantIndex. This will save an additional
67 * memory allocation for each one of your items (we are optimizing, aren't we ?)
68 * and you will be able to dynamic_cast<> the ModelInvariantIndex pointers
69 * directly to your structure pointers (which will likely save a large QHash<>...).
70 * Even in the unlikely case in that you don't have a data structure for your items,
71 * it's still an operator new() call that YOU make instead of this implementation.
72 * It doesn't impact performance at all. You just have to remember to delete
73 * your ModelInvariantIndex objects when you no longer need them.
74 */
76{
77 friend class ModelInvariantIndex;
78
80
81public:
82 explicit ModelInvariantRowMapper();
83 ~ModelInvariantRowMapper() override;
84
85 /**
86 * Sets the maximum time we can spend inside a single lazy update step.
87 * The larger this time, the more resources we consume and leave less to UI processing
88 * but also larger the update throughput (that is, we update more items per second).
89 * This is 50 msec by default.
90 */
92
93 /**
94 * Sets the idle time between two lazy updates in milliseconds.
95 * The larger this time, the less resources we consume and leave more to UI processing
96 * but also smaller the update throughput (that is, we update less items per second).
97 * This is 50 msec by default.
98 */
99 void setLazyUpdateIdleInterval(int idleInterval);
100
101 /**
102 * Maps a ModelInvariantIndex to the CURRENT associated row index in the model.
103 * As long as the underlying model is consistent, the returned row index is guaranteed to
104 * point to the content that this ModelInvariantIndex pointed at the time it was created.
105 *
106 * Returns the current associated row index in the model or -1 if the invariant
107 * does not belong to this mapper (model) or is marked as invalid at all.
108 */
110
111 /**
112 * Binds a ModelInvariantIndex structure to the specified CURRENT modelIndexRow.
113 * Later you can use the ModelInvariantIndex to retrieve the model contents
114 * that the parameter modelIndexRow refers to... even if the model changes.
115 * Call this function only if you're sure that there is no such invariant yet,
116 * otherwise take a look at modelIndexRowToModelInvariantIndex().
117 *
118 * This function ASSUMES that invariantToFill is a newly allocated ModelInvariantIndex.
119 */
121
122 /**
123 * Finds the existing ModelInvariantIndex that belongs to the specified CURRENT modelIndexRow.
124 * Returns the ModelInvariantIndex found or 0 if such an invariant wasn't yet
125 * created (by the means of createModelInvariantIndex()).
126 */
128
129 /**
130 * This basically applies modelIndexRowToModelInvariantIndex() to a range of elements.
131 * The returned pointer can be null if no existing ModelInvariantIndex object were
132 * present in the range (this can happen if you don't request some of them). If the returned
133 * value is not 0 then you're responsible of deleting it.
134 */
136
137 /**
138 * Call this function when rows are inserted to the underlying model
139 * BEFORE scanning the model for the new items. You probably
140 * want this function to be the first call in your rowsInserted() handler
141 * or the last call in the rowsAboutToBeInserted() handler.
142 */
143 void modelRowsInserted(int modelIndexRowPosition, int count);
144
145 /**
146 * Call this function when rows are removed from the underlying model
147 * AFTER accessing the removed rows for the last time. You probably
148 * want this function to be the first call of your rowsRemoved() handler
149 * or the last call in the rowsAboutToBeRemoved() handler.
150 *
151 * This function will invalidate any ModelInvariantIndex instances
152 * that are affected by the change. It will also do you a favor by returning
153 * the list of the invalidated ModelInvariantIndex objects since
154 * you'll probably want to delete them. The returned pointer can be null
155 * if no existing ModelInvariantIndex object were affected by the change
156 * (this can happen if you don't request some of them). If the returned
157 * value is not 0 then you're responsible of deleting it.
158 */
160
161 /**
162 * Call this function from your handlers of reset() and layoutChanged()
163 * AFTER you ve last accessed the model underlying data.
164 * You probably want this function to be the first call of your
165 * reset() or layoutChanged() handlers.
166 *
167 * This function assumes that all the ModelInvariantIndex
168 * are being invalidated and need to be required.
169 */
170 void modelReset();
171
172private:
173 std::unique_ptr<ModelInvariantRowMapperPrivate> const d;
174};
175} // namespace Core
176} // namespace MessageList
An invariant index that can be ALWAYS used to reference an item inside a QAbstractItemModel.
This class is an optimizing helper for dealing with large flat QAbstractItemModel objects.
QList< ModelInvariantIndex * > * modelIndexRowRangeToModelInvariantIndexList(int startIndexRow, int count)
This basically applies modelIndexRowToModelInvariantIndex() to a range of elements.
void createModelInvariantIndex(int modelIndexRow, ModelInvariantIndex *invariantToFill)
Binds a ModelInvariantIndex structure to the specified CURRENT modelIndexRow.
void modelRowsInserted(int modelIndexRowPosition, int count)
Call this function when rows are inserted to the underlying model BEFORE scanning the model for the n...
void modelReset()
Call this function from your handlers of reset() and layoutChanged() AFTER you ve last accessed the m...
QList< ModelInvariantIndex * > * modelRowsRemoved(int modelIndexRowPosition, int count)
Call this function when rows are removed from the underlying model AFTER accessing the removed rows f...
void setLazyUpdateChunkInterval(int chunkInterval)
Sets the maximum time we can spend inside a single lazy update step.
ModelInvariantIndex * modelIndexRowToModelInvariantIndex(int modelIndexRow)
Finds the existing ModelInvariantIndex that belongs to the specified CURRENT modelIndexRow.
void setLazyUpdateIdleInterval(int idleInterval)
Sets the idle time between two lazy updates in milliseconds.
int modelInvariantIndexToModelIndexRow(ModelInvariantIndex *invariant)
Maps a ModelInvariantIndex to the CURRENT associated row index in the model.
Q_OBJECTQ_OBJECT
T qobject_cast(QObject *object)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:12:43 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.