Plasma5Support

datacontainer.h
1/*
2 SPDX-FileCopyrightText: 2006-2007 Aaron Seigo <aseigo@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#ifndef PLASMA_DATACONTAINER_H
8#define PLASMA_DATACONTAINER_H
9
10#include <QHash>
11#include <QObject>
12#include <QTimer>
13
14#include <KJob>
15#include <plasma5support/dataengine.h>
16#include <plasma5support/plasma5support_export.h>
17
19
20namespace Plasma5Support
21{
22class DataContainerPrivate;
23
24/**
25 * @class DataContainer plasma/datacontainer.h <Plasma/DataContainer>
26 *
27 * @brief A set of data exported via a DataEngine
28 *
29 * Plasma5Support::DataContainer wraps the data exported by a DataEngine
30 * implementation, providing a generic wrapper for the data.
31 *
32 * A DataContainer may have zero or more associated pieces of data which
33 * are keyed by strings. The data itself is stored as QVariants. This allows
34 * easy and flexible retrieval of the information associated with this object
35 * without writing DataContainer or DataEngine specific code in visualizations.
36 *
37 * If you are creating your own DataContainer objects (and are passing them to
38 * DataEngine::addSource()), you normally just need to listen to the
39 * updateRequested() signal (as well as any other methods you might have of
40 * being notified of new data) and call setData() to actually update the data.
41 * Then you need to either trigger the scheduleSourcesUpdated signal of the
42 * parent DataEngine or call checkForUpdate() on the DataContainer.
43 *
44 * You also need to set a suitable name for the source with setObjectName().
45 * See DataEngine::addSource() for more information.
46 *
47 * Note that there is normally no need to subclass DataContainer, except as
48 * a way of encapsulating the data retrieval for a source, since all notifications
49 * are done via signals rather than virtual methods.
50 **/
51class PLASMA5SUPPORT_EXPORT DataContainer : public QObject
52{
53 friend class DataEngine;
54 friend class DataEnginePrivate;
56
57public:
58 /**
59 * Constructs a default DataContainer that has no name or data
60 * associated with it
61 **/
62 explicit DataContainer(QObject *parent = nullptr);
63 ~DataContainer() override;
64
65 /**
66 * Returns the data for this DataContainer
67 **/
68 const DataEngine::Data data() const;
69
70 /**
71 * Set a value for a key.
72 *
73 * This also marks this source as needing to signal an update.
74 *
75 * If you call setData() directly on a DataContainer, you need to
76 * either trigger the scheduleSourcesUpdated() slot for the
77 * data engine it belongs to or call checkForUpdate() on the
78 * DataContainer.
79 *
80 * @param key a string used as the key for the data
81 * @param value a QVariant holding the actual data. If a invalid
82 * QVariant is passed in and the key currently exists in the
83 * data, then the data entry is removed
84 **/
85 void setData(const QString &key, const QVariant &value);
86
87 /**
88 * Removes all data currently associated with this source
89 *
90 * If you call removeAllData() on a DataContainer, you need to
91 * either trigger the scheduleSourcesUpdated() slot for the
92 * data engine it belongs to or call checkForUpdate() on the
93 * DataContainer.
94 **/
95 void removeAllData();
96
97 /**
98 * Associates a model with this DataContainer. Use this for data
99 * that is intended to be a long list of items.
100 *
101 * The ownership of the model is transferred to the DataContainer,
102 * so the model will be deleted when a new one is set or when the
103 * DataContainer itself is deleted, so it will be deleted when there won't be any
104 * visualization associated to this source.
105 *
106 * Normally you should set the model from DataEngine::setModel instead from here.
107 *
108 * @param model the model that will be associated with this DataContainer
109 */
111
112 /**
113 * @return the model owned by this DataSource
114 */
116
117 /**
118 * @return true if the visualization is currently connected
119 */
120 bool visualizationIsConnected(QObject *visualization) const;
121
122 /**
123 * Connects an object to this DataContainer.
124 *
125 * May be called repeatedly for the same visualization without
126 * side effects
127 *
128 * @param visualization the object to connect to this DataContainer
129 * @param pollingInterval the time in milliseconds between updates
130 * @param alignment the clock position to align updates to
131 **/
132 void connectVisualization(QObject *visualization, uint pollingInterval, Plasma5Support::Types::IntervalAlignment alignment);
133
134 /**
135 * sets this data container to be automatically stored.
136 * @param whether this data container should be stored
137 * @since 4.6
138 */
139 void setStorageEnabled(bool store);
140
141 /**
142 * @return true if the data container has been marked for storage
143 * @since 4.6
144 */
145 bool isStorageEnabled() const;
146
147 /**
148 * @return true if the data container has been updated, but not stored
149 */
150 bool needsToBeStored() const;
151
152 /**
153 * sets that the data container needs to be stored or not.
154 * @param whether the data container needs to be stored
155 */
156 void setNeedsToBeStored(bool store);
157
158 /**
159 * @return the DataEngine that the DataContainer is
160 * a child of.
161 */
162 DataEngine *getDataEngine();
163
164 /**
165 * @return true if one or more visualizations is connected to this DataContainer
166 */
167 bool isUsed() const;
168
169public Q_SLOTS:
170 /**
171 * Disconnects an object from this DataContainer.
172 *
173 * Note that if this source was created by DataEngine::sourceRequestEvent(),
174 * it will be deleted by DataEngine once control returns to the event loop.
175 **/
176 void disconnectVisualization(QObject *visualization);
177
178 /**
179 * Forces immediate update signals to all visualizations
180 * @since 4.4
181 */
183
185 /**
186 * Emitted when the data has been updated, allowing visualizations to
187 * reflect the new data.
188 *
189 * Note that you should not normally emit this directly. Instead, use
190 * checkForUpdate() or the DataEngine::scheduleSourcesUpdated() slot.
191 *
192 * @param source the objectName() of the DataContainer (and hence the name
193 * of the source) that updated its data
194 * @param data the updated data
195 **/
196 void dataUpdated(const QString &source, const Plasma5Support::DataEngine::Data &data);
197
198 /**
199 * A new model has been associated to this source,
200 * visualizations can safely use it as long they are connected to this source.
201 *
202 * @param source the objectName() of the DataContainer (and hence the name
203 * of the source) that owns the model
204 * @param model the QAbstractItemModel instance
205 */
207
208 /**
209 * Emitted when the last visualization is disconnected.
210 *
211 * Note that if this source was created by DataEngine::sourceRequestEvent(),
212 * it will be deleted by DataEngine once control returns to the event loop
213 * after this signal is emitted.
214 *
215 * @param source the name of the source that became unused
216 **/
217 void becameUnused(const QString &source);
218
219 /**
220 * Emitted when an update is requested.
221 *
222 * If a polling interval was passed connectVisualization(), this signal
223 * will be emitted every time the interval expires.
224 *
225 * Note that if you create your own DataContainer (and pass it to
226 * DataEngine::addSource()), you will need to listen to this signal
227 * and refresh the data when it is triggered.
228 *
229 * @param source the datacontainer the update was requested for. Useful
230 * for classes that update the data for several containers.
231 **/
233
234protected:
235 /**
236 * Checks whether any data has changed and, if so, emits dataUpdated().
237 **/
238 void checkForUpdate();
239
240 /**
241 * Returns how long ago, in msecs, that the data in this container was last updated.
242 *
243 * This is used by DataEngine to compress updates that happen more quickly than the
244 * minimum polling interval by calling setNeedsUpdate() instead of calling
245 * updateSourceEvent() immediately.
246 **/
247 uint timeSinceLastUpdate() const;
248
249 /**
250 * Indicates that the data should be treated as dirty the next time hasUpdates() is called.
251 *
252 * This is needed for the case where updateRequested() is triggered but we don't want to
253 * update the data immediately because it has just been updated. The second request won't
254 * be fulfilled in this case, because we never updated the data and so never called
255 * checkForUpdate(). So we claim it needs an update anyway.
256 **/
257 void setNeedsUpdate(bool update = true);
258
259protected Q_SLOTS:
260 /**
261 * @reimp from QObject
262 */
263 void timerEvent(QTimerEvent *event) override;
264
265private:
266 friend class SignalRelay;
267 friend class DataContainerPrivate;
268 friend class DataEngineManager;
269 DataContainerPrivate *const d;
270
271 Q_PRIVATE_SLOT(d, void storeJobFinished(KJob *job))
272 Q_PRIVATE_SLOT(d, void populateFromStoredData(KJob *job))
273 Q_PRIVATE_SLOT(d, void retrieve())
274};
275
276} // Plasma namespace
277
278#endif // multiple inclusion guard
void disconnectVisualization(QObject *visualization)
Disconnects an object from this DataContainer.
void setModel(QAbstractItemModel *model)
Associates a model with this DataContainer.
void timerEvent(QTimerEvent *event) override
void setData(const QString &key, const QVariant &value)
Set a value for a key.
DataContainer(QObject *parent=nullptr)
Constructs a default DataContainer that has no name or data associated with it.
void connectVisualization(QObject *visualization, uint pollingInterval, Plasma5Support::Types::IntervalAlignment alignment)
Connects an object to this DataContainer.
QAbstractItemModel * model()
uint timeSinceLastUpdate() const
Returns how long ago, in msecs, that the data in this container was last updated.
void updateRequested(DataContainer *source)
Emitted when an update is requested.
bool visualizationIsConnected(QObject *visualization) const
const DataEngine::Data data() const
Returns the data for this DataContainer.
void setStorageEnabled(bool store)
sets this data container to be automatically stored.
void dataUpdated(const QString &source, const Plasma5Support::DataEngine::Data &data)
Emitted when the data has been updated, allowing visualizations to reflect the new data.
void checkForUpdate()
Checks whether any data has changed and, if so, emits dataUpdated().
void setNeedsUpdate(bool update=true)
Indicates that the data should be treated as dirty the next time hasUpdates() is called.
void removeAllData()
Removes all data currently associated with this source.
void becameUnused(const QString &source)
Emitted when the last visualization is disconnected.
void modelChanged(const QString &source, QAbstractItemModel *model)
A new model has been associated to this source, visualizations can safely use it as long they are con...
void setNeedsToBeStored(bool store)
sets that the data container needs to be stored or not.
void forceImmediateUpdate()
Forces immediate update signals to all visualizations.
IntervalAlignment
Possible timing alignments.
Namespace for everything in libplasma.
Definition datamodel.cpp:15
QObject(QObject *parent)
Q_OBJECTQ_OBJECT
Q_SIGNALSQ_SIGNALS
Q_SLOTSQ_SLOTS
virtual bool event(QEvent *e)
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:47:07 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.