QGIS API Documentation 3.41.0-Master (cea29feecf2)
Loading...
Searching...
No Matches
qgshistoryentrymodel.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgshistoryentrymodel.cpp
3 --------------------------
4 begin : April 2023
5 copyright : (C) 2023 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8/***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
17#include "moc_qgshistoryentrymodel.cpp"
18#include "qgshistoryentrynode.h"
20#include "qgsgui.h"
21#include "qgshistoryentry.h"
22#include "qgshistoryprovider.h"
23#include "qgsapplication.h"
24
25#include <QIcon>
26
28class QgsHistoryEntryRootNode : public QgsHistoryEntryGroup
29{
30 public:
31 QVariant data( int = Qt::DisplayRole ) const override;
32
33 void addEntryNode( const QgsHistoryEntry &entry, QgsHistoryEntryNode *node, QgsHistoryEntryModel *model );
34
39 static QString dateGroup( const QDateTime &timestamp, QString &sortKey );
40
41 QgsHistoryEntryDateGroupNode *dateNode( const QDateTime &timestamp, QgsHistoryEntryModel *model );
42
43 private:
44 QMap<QString, QgsHistoryEntryDateGroupNode *> mDateGroupNodes;
45};
46
47class QgsHistoryEntryDateGroupNode : public QgsHistoryEntryGroup
48{
49 public:
50 QgsHistoryEntryDateGroupNode( const QString &title, const QString &key )
51 : mTitle( title )
52 , mKey( key )
53 {
54 }
55
56 QVariant data( int role = Qt::DisplayRole ) const override
57 {
58 switch ( role )
59 {
60 case Qt::DisplayRole:
61 case Qt::ToolTipRole:
62 return mTitle;
63
64 case Qt::DecorationRole:
65 return QgsApplication::getThemeIcon( QStringLiteral( "mIconFolder.svg" ) );
66
67 default:
68 break;
69 }
70
71 return QVariant();
72 }
73
74 QString mTitle;
75 QString mKey;
76};
78
79QgsHistoryEntryModel::QgsHistoryEntryModel( const QString &providerId, Qgis::HistoryProviderBackends backends, QgsHistoryProviderRegistry *registry, const QgsHistoryWidgetContext &context, QObject *parent )
80 : QAbstractItemModel( parent )
81 , mContext( context )
82 , mRegistry( registry ? registry : QgsGui::historyProviderRegistry() )
83 , mProviderId( providerId )
84 , mBackends( backends )
85{
86 mRootNode = std::make_unique<QgsHistoryEntryRootNode>();
87
88 // populate with existing entries
89 const QList<QgsHistoryEntry> entries = mRegistry->queryEntries( QDateTime(), QDateTime(), mProviderId, mBackends );
90 for ( const QgsHistoryEntry &entry : entries )
91 {
92 QgsAbstractHistoryProvider *provider = mRegistry->providerById( entry.providerId );
93 if ( !provider )
94 continue;
95
96 if ( QgsHistoryEntryNode *node = provider->createNodeForEntry( entry, mContext ) )
97 {
98 mIdToNodeHash.insert( entry.id, node );
99 mRootNode->addEntryNode( entry, node, nullptr );
100 }
101 }
102
103 connect( mRegistry, &QgsHistoryProviderRegistry::entryAdded, this, &QgsHistoryEntryModel::entryAdded );
104 connect( mRegistry, &QgsHistoryProviderRegistry::entryUpdated, this, &QgsHistoryEntryModel::entryUpdated );
105 connect( mRegistry, &QgsHistoryProviderRegistry::historyCleared, this, &QgsHistoryEntryModel::historyCleared );
106}
107
111
112int QgsHistoryEntryModel::rowCount( const QModelIndex &parent ) const
113{
115 if ( !n )
116 return 0;
117
118 return n->childCount();
119}
120
121int QgsHistoryEntryModel::columnCount( const QModelIndex &parent ) const
122{
123 Q_UNUSED( parent )
124 return 1;
125}
126
127QModelIndex QgsHistoryEntryModel::index( int row, int column, const QModelIndex &parent ) const
128{
129 if ( column < 0 || column >= columnCount( parent ) || row < 0 || row >= rowCount( parent ) )
130 return QModelIndex();
131
133 if ( !n )
134 return QModelIndex(); // have no children
135
136 return createIndex( row, column, n->childAt( row ) );
137}
138
139QModelIndex QgsHistoryEntryModel::parent( const QModelIndex &child ) const
140{
141 if ( !child.isValid() )
142 return QModelIndex();
143
144 if ( QgsHistoryEntryNode *n = index2node( child ) )
145 {
146 return indexOfParentNode( n->parent() ); // must not be null
147 }
148 else
149 {
150 Q_ASSERT( false );
151 return QModelIndex();
152 }
153}
154
155QVariant QgsHistoryEntryModel::data( const QModelIndex &index, int role ) const
156{
157 if ( !index.isValid() || index.column() > 1 )
158 return QVariant();
159
161 if ( !node )
162 return QVariant();
163
164 return node->data( role );
165}
166
167Qt::ItemFlags QgsHistoryEntryModel::flags( const QModelIndex &index ) const
168{
169 if ( !index.isValid() )
170 {
171 Qt::ItemFlags rootFlags = Qt::ItemFlags();
172 return rootFlags;
173 }
174
175 Qt::ItemFlags f = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
176 return f;
177}
178
180{
181 if ( !index.isValid() )
182 return mRootNode.get();
183
184 return reinterpret_cast<QgsHistoryEntryNode *>( index.internalPointer() );
185}
186
187void QgsHistoryEntryModel::entryAdded( long long id, const QgsHistoryEntry &entry, Qgis::HistoryProviderBackend backend )
188{
189 // ignore entries we don't care about
190 if ( !( mBackends & backend ) )
191 return;
192 if ( !mProviderId.isEmpty() && entry.providerId != mProviderId )
193 return;
194
195 QgsAbstractHistoryProvider *provider = mRegistry->providerById( entry.providerId );
196 if ( !provider )
197 return;
198
199 if ( QgsHistoryEntryNode *node = provider->createNodeForEntry( entry, mContext ) )
200 {
201 mIdToNodeHash.insert( id, node );
202 mRootNode->addEntryNode( entry, node, this );
203 }
204}
205
206void QgsHistoryEntryModel::entryUpdated( long long id, const QVariantMap &entry, Qgis::HistoryProviderBackend backend )
207{
208 // ignore entries we don't care about
209 if ( !( mBackends & backend ) )
210 return;
211
212 // an update is a remove + reinsert operation
213 if ( QgsHistoryEntryNode *node = mIdToNodeHash.value( id ) )
214 {
215 bool ok = false;
216 QgsHistoryEntry historyEntry = mRegistry->entry( id, ok, backend );
217 historyEntry.entry = entry;
218 const QString providerId = historyEntry.providerId;
219 QgsAbstractHistoryProvider *provider = mRegistry->providerById( providerId );
220 if ( !provider )
221 return;
222
223 const QModelIndex nodeIndex = node2index( node );
224 const int existingChildRows = node->childCount();
225 provider->updateNodeForEntry( node, historyEntry, mContext );
226 const int newChildRows = node->childCount();
227
228 if ( newChildRows < existingChildRows )
229 {
230 beginRemoveRows( nodeIndex, newChildRows, existingChildRows - 1 );
231 endRemoveRows();
232 }
233 else if ( existingChildRows < newChildRows )
234 {
235 beginInsertRows( nodeIndex, existingChildRows, newChildRows - 1 );
236 endInsertRows();
237 }
238
239 const QModelIndex topLeft = index( 0, 0, nodeIndex );
240 const QModelIndex bottomRight = index( newChildRows - 1, columnCount() - 1, nodeIndex );
241 emit dataChanged( topLeft, bottomRight );
242 emit dataChanged( nodeIndex, nodeIndex );
243 }
244}
245
246void QgsHistoryEntryModel::historyCleared( Qgis::HistoryProviderBackend backend, const QString &providerId )
247{
248 // ignore entries we don't care about
249 if ( !( mBackends & backend ) )
250 return;
251
252 if ( !mProviderId.isEmpty() && !providerId.isEmpty() && providerId != mProviderId )
253 return;
254
255 beginResetModel();
256 mRootNode->clear();
257 mIdToNodeHash.clear();
258 endResetModel();
259}
260
261QModelIndex QgsHistoryEntryModel::node2index( QgsHistoryEntryNode *node ) const
262{
263 if ( !node || !node->parent() )
264 return QModelIndex(); // this is the only root item -> invalid index
265
266 QModelIndex parentIndex = node2index( node->parent() );
267
268 int row = node->parent()->indexOf( node );
269 Q_ASSERT( row >= 0 );
270 return index( row, 0, parentIndex );
271}
272
273QModelIndex QgsHistoryEntryModel::indexOfParentNode( QgsHistoryEntryNode *parentNode ) const
274{
275 Q_ASSERT( parentNode );
276
277 QgsHistoryEntryGroup *grandParentNode = parentNode->parent();
278 if ( !grandParentNode )
279 return QModelIndex(); // root node -> invalid index
280
281 int row = grandParentNode->indexOf( parentNode );
282 Q_ASSERT( row >= 0 );
283
284 return createIndex( row, 0, parentNode );
285}
286
287//
288// QgsHistoryEntryRootNode
289//
291QVariant QgsHistoryEntryRootNode::data( int ) const
292{
293 return QVariant();
294}
295
296void QgsHistoryEntryRootNode::addEntryNode( const QgsHistoryEntry &entry, QgsHistoryEntryNode *node, QgsHistoryEntryModel *model )
297{
298 QgsHistoryEntryDateGroupNode *targetDateNode = dateNode( entry.timestamp, model );
299
300 if ( model )
301 {
302 const QModelIndex dateNodeIndex = model->node2index( targetDateNode );
303 model->beginInsertRows( dateNodeIndex, 0, 0 );
304 }
305 targetDateNode->insertChild( 0, node );
306 if ( model )
307 {
308 model->endInsertRows();
309 }
310}
311
312QString QgsHistoryEntryRootNode::dateGroup( const QDateTime &timestamp, QString &sortKey )
313{
314 QString groupString;
315 if ( timestamp.date() == QDateTime::currentDateTime().date() )
316 {
317 groupString = QObject::tr( "Today" );
318 sortKey = QStringLiteral( "0" );
319 }
320 else
321 {
322 const qint64 intervalDays = timestamp.date().daysTo( QDateTime::currentDateTime().date() );
323 if ( intervalDays == 1 )
324 {
325 groupString = QObject::tr( "Yesterday" );
326 sortKey = QStringLiteral( "1" );
327 }
328 else if ( intervalDays < 8 )
329 {
330 groupString = QObject::tr( "Last 7 days" );
331 sortKey = QStringLiteral( "2" );
332 }
333 else
334 {
335 // a bit of trickiness here, we need dates ordered descending
336 sortKey = QStringLiteral( "3: %1 %2" ).arg( QDate::currentDate().year() - timestamp.date().year(), 5, 10, QLatin1Char( '0' ) ).arg( 12 - timestamp.date().month(), 2, 10, QLatin1Char( '0' ) );
337 groupString = timestamp.toString( QStringLiteral( "MMMM yyyy" ) );
338 }
339 }
340 return groupString;
341}
342
343QgsHistoryEntryDateGroupNode *QgsHistoryEntryRootNode::dateNode( const QDateTime &timestamp, QgsHistoryEntryModel *model )
344{
345 QString dateGroupKey;
346 const QString dateTitle = dateGroup( timestamp, dateGroupKey );
347
348 QgsHistoryEntryDateGroupNode *node = mDateGroupNodes.value( dateGroupKey );
349 if ( !node )
350 {
351 node = new QgsHistoryEntryDateGroupNode( dateTitle, dateGroupKey );
352 mDateGroupNodes[dateGroupKey] = node;
353
354 int targetIndex = 0;
355 bool isInsert = false;
356 for ( const auto &child : mChildren )
357 {
358 if ( QgsHistoryEntryDateGroupNode *candidateNode = dynamic_cast<QgsHistoryEntryDateGroupNode *>( child.get() ) )
359 {
360 if ( candidateNode->mKey > dateGroupKey )
361 {
362 isInsert = true;
363 break;
364 }
365 }
366 targetIndex++;
367 }
368
369 if ( isInsert )
370 {
371 if ( model )
372 {
373 model->beginInsertRows( QModelIndex(), targetIndex, targetIndex );
374 }
375 insertChild( targetIndex, node );
376 if ( model )
377 {
378 model->endInsertRows();
379 }
380 }
381 else
382 {
383 if ( model )
384 {
385 model->beginInsertRows( QModelIndex(), childCount(), childCount() );
386 }
387 addChild( node );
388 if ( model )
389 {
390 model->endInsertRows();
391 }
392 }
393 }
394
395 return node;
396}
HistoryProviderBackend
History provider backends.
Definition qgis.h:3299
QFlags< HistoryProviderBackend > HistoryProviderBackends
Definition qgis.h:3304
Abstract base class for objects which track user history (i.e.
virtual void updateNodeForEntry(QgsHistoryEntryNode *node, const QgsHistoryEntry &entry, const QgsHistoryWidgetContext &context)
Updates an existing history node for the given entry.
virtual QgsHistoryEntryNode * createNodeForEntry(const QgsHistoryEntry &entry, const QgsHistoryWidgetContext &context)
Creates a new history node for the given entry.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
QgsGui is a singleton class containing various registry and other global members related to GUI class...
Definition qgsgui.h:64
Base class for history entry "group" nodes, which contain children of their own.
QgsHistoryEntryNode * childAt(int index)
Returns the child at the specified index.
int indexOf(QgsHistoryEntryNode *child) const
Returns the index of the specified child node.
An item model representing history entries in a hierarchical tree structure.
QgsHistoryEntryModel(const QString &providerId=QString(), Qgis::HistoryProviderBackends backends=Qgis::HistoryProviderBackend::LocalProfile, QgsHistoryProviderRegistry *registry=nullptr, const QgsHistoryWidgetContext &context=QgsHistoryWidgetContext(), QObject *parent=nullptr)
Constructor for QgsHistoryEntryModel, with the specified parent object.
QgsHistoryEntryNode * index2node(const QModelIndex &index) const
Returns node for given index.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Qt::ItemFlags flags(const QModelIndex &index) const override
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const final
int columnCount(const QModelIndex &parent=QModelIndex()) const final
QModelIndex parent(const QModelIndex &child) const final
int rowCount(const QModelIndex &parent=QModelIndex()) const final
Base class for nodes representing a QgsHistoryEntry.
virtual QVariant data(int role=Qt::DisplayRole) const =0
Returns the node's data for the specified model role.
QgsHistoryEntryGroup * parent()
Returns the node's parent node.
virtual int childCount() const
Returns the number of child nodes owned by this node.
Encapsulates a history entry.
QDateTime timestamp
Entry timestamp.
QString providerId
Associated history provider ID.
QVariantMap entry
Entry details.
The QgsHistoryProviderRegistry is a registry for objects which track user history (i....
QgsAbstractHistoryProvider * providerById(const QString &id)
Returns the provider with matching id, or nullptr if no matching provider is registered.
void entryAdded(long long id, const QgsHistoryEntry &entry, Qgis::HistoryProviderBackend backend)
Emitted when an entry is added.
QgsHistoryEntry entry(long long id, bool &ok, Qgis::HistoryProviderBackend backend=Qgis::HistoryProviderBackend::LocalProfile) const
Returns the entry with matching ID, from the specified backend.
QList< QgsHistoryEntry > queryEntries(const QDateTime &start=QDateTime(), const QDateTime &end=QDateTime(), const QString &providerId=QString(), Qgis::HistoryProviderBackends backends=Qgis::HistoryProviderBackend::LocalProfile) const
Queries history entries which occurred between the specified start and end times.
void entryUpdated(long long id, const QVariantMap &entry, Qgis::HistoryProviderBackend backend)
Emitted when an entry is updated.
void historyCleared(Qgis::HistoryProviderBackend backend, const QString &providerId)
Emitted when the history is cleared for a backend.
Contains settings which reflect the context in which a history widget is shown, e....