30 QVariant
data(
int = Qt::DisplayRole )
const override;
38 static QString dateGroup(
const QDateTime ×tamp, QString &sortKey );
40 QgsHistoryEntryDateGroupNode *dateNode(
const QDateTime ×tamp,
QgsHistoryEntryModel *model );
44 QMap< QString, QgsHistoryEntryDateGroupNode * > mDateGroupNodes;
52 QgsHistoryEntryDateGroupNode(
const QString &title,
const QString &key )
59 QVariant
data(
int role = Qt::DisplayRole )
const override
67 case Qt::DecorationRole:
84 : QAbstractItemModel( parent )
86 , mRegistry( registry ? registry :
QgsGui::historyProviderRegistry() )
87 , mProviderId( providerId )
88 , mBackends( backends )
90 mRootNode = std::make_unique< QgsHistoryEntryRootNode>();
93 const QList< QgsHistoryEntry > entries = mRegistry->
queryEntries( QDateTime(), QDateTime(), mProviderId, mBackends );
102 mIdToNodeHash.insert( entry.id, node );
103 mRootNode->addEntryNode( entry, node,
nullptr );
136 return QModelIndex();
140 return QModelIndex();
142 return createIndex( row, column, n->
childAt( row ) );
147 if ( !child.isValid() )
148 return QModelIndex();
152 return indexOfParentNode( n->parent() );
157 return QModelIndex();
170 return node->
data( role );
175 if ( !
index.isValid() )
177 Qt::ItemFlags rootFlags = Qt::ItemFlags();
181 Qt::ItemFlags f = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
187 if ( !
index.isValid() )
188 return mRootNode.get();
197 if ( !( mBackends & backend ) )
199 if ( !mProviderId.isEmpty() && entry.
providerId != mProviderId )
208 mIdToNodeHash.insert(
id, node );
209 mRootNode->addEntryNode( entry, node,
this );
216 if ( !( mBackends & backend ) )
224 historyEntry.
entry = entry;
225 const QString providerId = historyEntry.
providerId;
230 const QModelIndex nodeIndex = node2index( node );
231 const int existingChildRows = node->childCount();
233 const int newChildRows = node->childCount();
235 if ( newChildRows < existingChildRows )
237 beginRemoveRows( nodeIndex, newChildRows, existingChildRows - 1 );
240 else if ( existingChildRows < newChildRows )
242 beginInsertRows( nodeIndex, existingChildRows, newChildRows - 1 );
246 const QModelIndex topLeft =
index( 0, 0, nodeIndex );
247 const QModelIndex bottomRight =
index( newChildRows - 1,
columnCount() - 1, nodeIndex );
248 emit dataChanged( topLeft, bottomRight );
249 emit dataChanged( nodeIndex, nodeIndex );
256 if ( !( mBackends & backend ) )
259 if ( !mProviderId.isEmpty() && !providerId.isEmpty() && providerId != mProviderId )
264 mIdToNodeHash.clear();
270 if ( !node || !node->
parent() )
271 return QModelIndex();
273 QModelIndex parentIndex = node2index( node->
parent() );
276 Q_ASSERT( row >= 0 );
277 return index( row, 0, parentIndex );
280QModelIndex QgsHistoryEntryModel::indexOfParentNode(
QgsHistoryEntryNode *parentNode )
const
282 Q_ASSERT( parentNode );
285 if ( !grandParentNode )
286 return QModelIndex();
288 int row = grandParentNode->
indexOf( parentNode );
289 Q_ASSERT( row >= 0 );
291 return createIndex( row, 0, parentNode );
298QVariant QgsHistoryEntryRootNode::data(
int )
const
305 QgsHistoryEntryDateGroupNode *targetDateNode = dateNode( entry.
timestamp, model );
309 const QModelIndex dateNodeIndex = model->node2index( targetDateNode );
310 model->beginInsertRows( dateNodeIndex, 0, 0 );
312 targetDateNode->insertChild( 0, node );
315 model->endInsertRows();
319QString QgsHistoryEntryRootNode::dateGroup(
const QDateTime ×tamp, QString &sortKey )
322 if ( timestamp.date() == QDateTime::currentDateTime().date() )
324 groupString = QObject::tr(
"Today" );
325 sortKey = QStringLiteral(
"0" );
329 const qint64 intervalDays = timestamp.date().daysTo( QDateTime::currentDateTime().date() );
330 if ( intervalDays == 1 )
332 groupString = QObject::tr(
"Yesterday" );
333 sortKey = QStringLiteral(
"1" );
335 else if ( intervalDays < 8 )
337 groupString = QObject::tr(
"Last 7 days" );
338 sortKey = QStringLiteral(
"2" );
343 sortKey = QStringLiteral(
"3: %1 %2" ).arg( QDate::currentDate().year() - timestamp.date().year(), 5, 10, QLatin1Char(
'0' ) )
344 .arg( 12 - timestamp.date().month(), 2, 10, QLatin1Char(
'0' ) );
345 groupString = timestamp.toString( QStringLiteral(
"MMMM yyyy" ) );
351QgsHistoryEntryDateGroupNode *QgsHistoryEntryRootNode::dateNode(
const QDateTime ×tamp,
QgsHistoryEntryModel *model )
353 QString dateGroupKey;
354 const QString dateTitle = dateGroup( timestamp, dateGroupKey );
356 QgsHistoryEntryDateGroupNode *node = mDateGroupNodes.value( dateGroupKey );
359 node =
new QgsHistoryEntryDateGroupNode( dateTitle, dateGroupKey );
360 mDateGroupNodes[ dateGroupKey ] = node;
363 bool isInsert =
false;
364 for (
const auto &child : mChildren )
366 if ( QgsHistoryEntryDateGroupNode *candidateNode =
dynamic_cast< QgsHistoryEntryDateGroupNode *
>( child.get() ) )
368 if ( candidateNode->mKey > dateGroupKey )
381 model->beginInsertRows( QModelIndex(), targetIndex, targetIndex );
383 insertChild( targetIndex, node );
386 model->endInsertRows();
393 model->beginInsertRows( QModelIndex(), childCount(), childCount() );
398 model->endInsertRows();
HistoryProviderBackend
History provider backends.
QFlags< HistoryProviderBackend > HistoryProviderBackends
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...
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.
~QgsHistoryEntryModel() override
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....