QGIS API Documentation 4.1.0-Master (60fea48833c)
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
18#include "qgsapplication.h"
19#include "qgsgui.h"
20#include "qgshistoryentry.h"
21#include "qgshistoryentrynode.h"
22#include "qgshistoryprovider.h"
24
25#include <QIcon>
26#include <QString>
27
28#include "moc_qgshistoryentrymodel.cpp"
29
30using namespace Qt::StringLiterals;
31
33class QgsHistoryEntryRootNode : public QgsHistoryEntryGroup
34{
35 public:
36 QVariant data( int = Qt::DisplayRole ) const override;
37
38 void addEntryNode( const QgsHistoryEntry &entry, QgsHistoryEntryNode *node, QgsHistoryEntryModel *model );
39
44 static QString dateGroup( const QDateTime &timestamp, QString &sortKey );
45
46 QgsHistoryEntryDateGroupNode *dateNode( const QDateTime &timestamp, QgsHistoryEntryModel *model );
47
48 private:
49 QMap<QString, QgsHistoryEntryDateGroupNode *> mDateGroupNodes;
50};
51
52class QgsHistoryEntryDateGroupNode : public QgsHistoryEntryGroup
53{
54 public:
55 QgsHistoryEntryDateGroupNode( const QString &title, const QString &key )
56 : mTitle( title )
57 , mKey( key )
58 {}
59
60 QVariant data( int role = Qt::DisplayRole ) const override
61 {
62 switch ( role )
63 {
64 case Qt::DisplayRole:
65 case Qt::ToolTipRole:
66 return mTitle;
67
68 case Qt::DecorationRole:
69 return QgsApplication::getThemeIcon( u"mIconFolder.svg"_s );
70
71 default:
72 break;
73 }
74
75 return QVariant();
76 }
77
78 QString mTitle;
79 QString mKey;
80};
82
84 : QAbstractItemModel( parent )
85 , mContext( context )
86 , mRegistry( registry ? registry : QgsGui::historyProviderRegistry() )
87 , mProviderId( providerId )
88 , mBackends( backends )
89{
90 mRootNode = std::make_unique<QgsHistoryEntryRootNode>();
91
92 // populate with existing entries
93 const QList<QgsHistoryEntry> entries = mRegistry->queryEntries( QDateTime(), QDateTime(), mProviderId, mBackends );
94 for ( const QgsHistoryEntry &entry : entries )
95 {
96 QgsAbstractHistoryProvider *provider = mRegistry->providerById( entry.providerId );
97 if ( !provider )
98 continue;
99
100 if ( QgsHistoryEntryNode *node = provider->createNodeForEntry( entry, mContext ) )
101 {
102 mIdToNodeHash.insert( entry.id, node );
103 mRootNode->addEntryNode( entry, node, nullptr );
104 }
105 }
106
107 connect( mRegistry, &QgsHistoryProviderRegistry::entryAdded, this, &QgsHistoryEntryModel::entryAdded );
108 connect( mRegistry, &QgsHistoryProviderRegistry::entryUpdated, this, &QgsHistoryEntryModel::entryUpdated );
109 connect( mRegistry, &QgsHistoryProviderRegistry::historyCleared, this, &QgsHistoryEntryModel::historyCleared );
110}
111
114
115int QgsHistoryEntryModel::rowCount( const QModelIndex &parent ) const
116{
118 if ( !n )
119 return 0;
120
121 return n->childCount();
122}
123
124int QgsHistoryEntryModel::columnCount( const QModelIndex &parent ) const
125{
126 Q_UNUSED( parent )
127 return 1;
128}
129
130QModelIndex QgsHistoryEntryModel::index( int row, int column, const QModelIndex &parent ) const
131{
132 if ( column < 0 || column >= columnCount( parent ) || row < 0 || row >= rowCount( parent ) )
133 return QModelIndex();
134
136 if ( !n )
137 return QModelIndex(); // have no children
138
139 return createIndex( row, column, n->childAt( row ) );
140}
141
142QModelIndex QgsHistoryEntryModel::parent( const QModelIndex &child ) const
143{
144 if ( !child.isValid() )
145 return QModelIndex();
146
147 if ( QgsHistoryEntryNode *n = index2node( child ) )
148 {
149 return indexOfParentNode( n->parent() ); // must not be null
150 }
151 else
152 {
153 Q_ASSERT( false );
154 return QModelIndex();
155 }
156}
157
158QVariant QgsHistoryEntryModel::data( const QModelIndex &index, int role ) const
159{
160 if ( !index.isValid() || index.column() > 1 )
161 return QVariant();
162
164 if ( !node )
165 return QVariant();
166
167 return node->data( role );
168}
169
170Qt::ItemFlags QgsHistoryEntryModel::flags( const QModelIndex &index ) const
171{
172 if ( !index.isValid() )
173 {
174 Qt::ItemFlags rootFlags = Qt::ItemFlags();
175 return rootFlags;
176 }
177
178 Qt::ItemFlags f = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
179 return f;
180}
181
183{
184 if ( !index.isValid() )
185 return mRootNode.get();
186
187 return reinterpret_cast<QgsHistoryEntryNode *>( index.internalPointer() );
188}
189
190void QgsHistoryEntryModel::entryAdded( long long id, const QgsHistoryEntry &entry, Qgis::HistoryProviderBackend backend )
191{
192 // ignore entries we don't care about
193 if ( !( mBackends & backend ) )
194 return;
195 if ( !mProviderId.isEmpty() && entry.providerId != mProviderId )
196 return;
197
198 QgsAbstractHistoryProvider *provider = mRegistry->providerById( entry.providerId );
199 if ( !provider )
200 return;
201
202 if ( QgsHistoryEntryNode *node = provider->createNodeForEntry( entry, mContext ) )
203 {
204 mIdToNodeHash.insert( id, node );
205 mRootNode->addEntryNode( entry, node, this );
206 }
207}
208
209void QgsHistoryEntryModel::entryUpdated( long long id, const QVariantMap &entry, Qgis::HistoryProviderBackend backend )
210{
211 // ignore entries we don't care about
212 if ( !( mBackends & backend ) )
213 return;
214
215 // an update is a remove + reinsert operation
216 if ( QgsHistoryEntryNode *node = mIdToNodeHash.value( id ) )
217 {
218 bool ok = false;
219 QgsHistoryEntry historyEntry = mRegistry->entry( id, ok, backend );
220 historyEntry.entry = entry;
221 const QString providerId = historyEntry.providerId;
222 QgsAbstractHistoryProvider *provider = mRegistry->providerById( providerId );
223 if ( !provider )
224 return;
225
226 const QModelIndex nodeIndex = node2index( node );
227 const int existingChildRows = node->childCount();
228 provider->updateNodeForEntry( node, historyEntry, mContext );
229 const int newChildRows = node->childCount();
230
231 if ( newChildRows < existingChildRows )
232 {
233 beginRemoveRows( nodeIndex, newChildRows, existingChildRows - 1 );
234 endRemoveRows();
235 }
236 else if ( existingChildRows < newChildRows )
237 {
238 beginInsertRows( nodeIndex, existingChildRows, newChildRows - 1 );
239 endInsertRows();
240 }
241
242 const QModelIndex topLeft = index( 0, 0, nodeIndex );
243 const QModelIndex bottomRight = index( newChildRows - 1, columnCount() - 1, nodeIndex );
244 emit dataChanged( topLeft, bottomRight );
245 emit dataChanged( nodeIndex, nodeIndex );
246 }
247}
248
249void QgsHistoryEntryModel::historyCleared( Qgis::HistoryProviderBackend backend, const QString &providerId )
250{
251 // ignore entries we don't care about
252 if ( !( mBackends & backend ) )
253 return;
254
255 if ( !mProviderId.isEmpty() && !providerId.isEmpty() && providerId != mProviderId )
256 return;
257
258 beginResetModel();
259 mRootNode->clear();
260 mIdToNodeHash.clear();
261 endResetModel();
262}
263
264QModelIndex QgsHistoryEntryModel::node2index( QgsHistoryEntryNode *node ) const
265{
266 if ( !node || !node->parent() )
267 return QModelIndex(); // this is the only root item -> invalid index
268
269 QModelIndex parentIndex = node2index( node->parent() );
270
271 int row = node->parent()->indexOf( node );
272 Q_ASSERT( row >= 0 );
273 return index( row, 0, parentIndex );
274}
275
276QModelIndex QgsHistoryEntryModel::indexOfParentNode( QgsHistoryEntryNode *parentNode ) const
277{
278 Q_ASSERT( parentNode );
279
280 QgsHistoryEntryGroup *grandParentNode = parentNode->parent();
281 if ( !grandParentNode )
282 return QModelIndex(); // root node -> invalid index
283
284 int row = grandParentNode->indexOf( parentNode );
285 Q_ASSERT( row >= 0 );
286
287 return createIndex( row, 0, parentNode );
288}
289
290//
291// QgsHistoryEntryRootNode
292//
294QVariant QgsHistoryEntryRootNode::data( int ) const
295{
296 return QVariant();
297}
298
299void QgsHistoryEntryRootNode::addEntryNode( const QgsHistoryEntry &entry, QgsHistoryEntryNode *node, QgsHistoryEntryModel *model )
300{
301 QgsHistoryEntryDateGroupNode *targetDateNode = dateNode( entry.timestamp, model );
302
303 if ( model )
304 {
305 const QModelIndex dateNodeIndex = model->node2index( targetDateNode );
306 model->beginInsertRows( dateNodeIndex, 0, 0 );
307 }
308 targetDateNode->insertChild( 0, node );
309 if ( model )
310 {
311 model->endInsertRows();
312 }
313}
314
315QString QgsHistoryEntryRootNode::dateGroup( const QDateTime &timestamp, QString &sortKey )
316{
317 QString groupString;
318 if ( timestamp.date() == QDateTime::currentDateTime().date() )
319 {
320 groupString = QObject::tr( "Today" );
321 sortKey = u"0"_s;
322 }
323 else
324 {
325 const qint64 intervalDays = timestamp.date().daysTo( QDateTime::currentDateTime().date() );
326 if ( intervalDays == 1 )
327 {
328 groupString = QObject::tr( "Yesterday" );
329 sortKey = u"1"_s;
330 }
331 else if ( intervalDays < 8 )
332 {
333 groupString = QObject::tr( "Last 7 days" );
334 sortKey = u"2"_s;
335 }
336 else
337 {
338 // a bit of trickiness here, we need dates ordered descending
339 sortKey = u"3: %1 %2"_s.arg( QDate::currentDate().year() - timestamp.date().year(), 5, 10, '0'_L1 ).arg( 12 - timestamp.date().month(), 2, 10, '0'_L1 );
340 groupString = timestamp.toString( u"MMMM yyyy"_s );
341 }
342 }
343 return groupString;
344}
345
346QgsHistoryEntryDateGroupNode *QgsHistoryEntryRootNode::dateNode( const QDateTime &timestamp, QgsHistoryEntryModel *model )
347{
348 QString dateGroupKey;
349 const QString dateTitle = dateGroup( timestamp, dateGroupKey );
350
351 QgsHistoryEntryDateGroupNode *node = mDateGroupNodes.value( dateGroupKey );
352 if ( !node )
353 {
354 node = new QgsHistoryEntryDateGroupNode( dateTitle, dateGroupKey );
355 mDateGroupNodes[dateGroupKey] = node;
356
357 int targetIndex = 0;
358 bool isInsert = false;
359 for ( const auto &child : mChildren )
360 {
361 if ( QgsHistoryEntryDateGroupNode *candidateNode = dynamic_cast<QgsHistoryEntryDateGroupNode *>( child.get() ) )
362 {
363 if ( candidateNode->mKey > dateGroupKey )
364 {
365 isInsert = true;
366 break;
367 }
368 }
369 targetIndex++;
370 }
371
372 if ( isInsert )
373 {
374 if ( model )
375 {
376 model->beginInsertRows( QModelIndex(), targetIndex, targetIndex );
377 }
378 insertChild( targetIndex, node );
379 if ( model )
380 {
381 model->endInsertRows();
382 }
383 }
384 else
385 {
386 if ( model )
387 {
388 model->beginInsertRows( QModelIndex(), childCount(), childCount() );
389 }
390 addChild( node );
391 if ( model )
392 {
393 model->endInsertRows();
394 }
395 }
396 }
397
398 return node;
399}
HistoryProviderBackend
History provider backends.
Definition qgis.h:3617
QFlags< HistoryProviderBackend > HistoryProviderBackends
Definition qgis.h:3622
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.
A singleton class containing various registry and other global members related to GUI classes.
Definition qgsgui.h:71
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.
A registry for objects which track user history (i.e.
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.
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....