17#include "moc_qgshistoryentrymodel.cpp"
31 QVariant
data(
int = Qt::DisplayRole )
const override;
39 static QString dateGroup(
const QDateTime ×tamp, QString &sortKey );
41 QgsHistoryEntryDateGroupNode *dateNode(
const QDateTime ×tamp,
QgsHistoryEntryModel *model );
45 QMap< QString, QgsHistoryEntryDateGroupNode * > mDateGroupNodes;
53 QgsHistoryEntryDateGroupNode(
const QString &title,
const QString &key )
60 QVariant
data(
int role = Qt::DisplayRole )
const override
68 case Qt::DecorationRole:
85 : QAbstractItemModel( parent )
87 , mRegistry( registry ? registry :
QgsGui::historyProviderRegistry() )
88 , mProviderId( providerId )
89 , mBackends( backends )
91 mRootNode = std::make_unique< QgsHistoryEntryRootNode>();
94 const QList< QgsHistoryEntry > entries = mRegistry->
queryEntries( QDateTime(), QDateTime(), mProviderId, mBackends );
103 mIdToNodeHash.insert( entry.id, node );
104 mRootNode->addEntryNode( entry, node,
nullptr );
137 return QModelIndex();
141 return QModelIndex();
143 return createIndex( row, column, n->
childAt( row ) );
148 if ( !child.isValid() )
149 return QModelIndex();
153 return indexOfParentNode( n->parent() );
158 return QModelIndex();
171 return node->
data( role );
176 if ( !
index.isValid() )
178 Qt::ItemFlags rootFlags = Qt::ItemFlags();
182 Qt::ItemFlags f = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
188 if ( !
index.isValid() )
189 return mRootNode.get();
198 if ( !( mBackends & backend ) )
200 if ( !mProviderId.isEmpty() && entry.
providerId != mProviderId )
209 mIdToNodeHash.insert(
id, node );
210 mRootNode->addEntryNode( entry, node,
this );
217 if ( !( mBackends & backend ) )
225 historyEntry.
entry = entry;
226 const QString providerId = historyEntry.
providerId;
231 const QModelIndex nodeIndex = node2index( node );
232 const int existingChildRows = node->childCount();
234 const int newChildRows = node->childCount();
236 if ( newChildRows < existingChildRows )
238 beginRemoveRows( nodeIndex, newChildRows, existingChildRows - 1 );
241 else if ( existingChildRows < newChildRows )
243 beginInsertRows( nodeIndex, existingChildRows, newChildRows - 1 );
247 const QModelIndex topLeft =
index( 0, 0, nodeIndex );
248 const QModelIndex bottomRight =
index( newChildRows - 1,
columnCount() - 1, nodeIndex );
249 emit dataChanged( topLeft, bottomRight );
250 emit dataChanged( nodeIndex, nodeIndex );
257 if ( !( mBackends & backend ) )
260 if ( !mProviderId.isEmpty() && !providerId.isEmpty() && providerId != mProviderId )
265 mIdToNodeHash.clear();
271 if ( !node || !node->
parent() )
272 return QModelIndex();
274 QModelIndex parentIndex = node2index( node->
parent() );
277 Q_ASSERT( row >= 0 );
278 return index( row, 0, parentIndex );
281QModelIndex QgsHistoryEntryModel::indexOfParentNode(
QgsHistoryEntryNode *parentNode )
const
283 Q_ASSERT( parentNode );
286 if ( !grandParentNode )
287 return QModelIndex();
289 int row = grandParentNode->
indexOf( parentNode );
290 Q_ASSERT( row >= 0 );
292 return createIndex( row, 0, parentNode );
299QVariant QgsHistoryEntryRootNode::data(
int )
const
306 QgsHistoryEntryDateGroupNode *targetDateNode = dateNode( entry.
timestamp, model );
310 const QModelIndex dateNodeIndex = model->node2index( targetDateNode );
311 model->beginInsertRows( dateNodeIndex, 0, 0 );
313 targetDateNode->insertChild( 0, node );
316 model->endInsertRows();
320QString QgsHistoryEntryRootNode::dateGroup(
const QDateTime ×tamp, QString &sortKey )
323 if ( timestamp.date() == QDateTime::currentDateTime().date() )
325 groupString = QObject::tr(
"Today" );
326 sortKey = QStringLiteral(
"0" );
330 const qint64 intervalDays = timestamp.date().daysTo( QDateTime::currentDateTime().date() );
331 if ( intervalDays == 1 )
333 groupString = QObject::tr(
"Yesterday" );
334 sortKey = QStringLiteral(
"1" );
336 else if ( intervalDays < 8 )
338 groupString = QObject::tr(
"Last 7 days" );
339 sortKey = QStringLiteral(
"2" );
344 sortKey = QStringLiteral(
"3: %1 %2" ).arg( QDate::currentDate().year() - timestamp.date().year(), 5, 10, QLatin1Char(
'0' ) )
345 .arg( 12 - timestamp.date().month(), 2, 10, QLatin1Char(
'0' ) );
346 groupString = timestamp.toString( QStringLiteral(
"MMMM yyyy" ) );
352QgsHistoryEntryDateGroupNode *QgsHistoryEntryRootNode::dateNode(
const QDateTime ×tamp,
QgsHistoryEntryModel *model )
354 QString dateGroupKey;
355 const QString dateTitle = dateGroup( timestamp, dateGroupKey );
357 QgsHistoryEntryDateGroupNode *node = mDateGroupNodes.value( dateGroupKey );
360 node =
new QgsHistoryEntryDateGroupNode( dateTitle, dateGroupKey );
361 mDateGroupNodes[ dateGroupKey ] = node;
364 bool isInsert =
false;
365 for (
const auto &child : mChildren )
367 if ( QgsHistoryEntryDateGroupNode *candidateNode =
dynamic_cast< QgsHistoryEntryDateGroupNode *
>( child.get() ) )
369 if ( candidateNode->mKey > dateGroupKey )
382 model->beginInsertRows( QModelIndex(), targetIndex, targetIndex );
384 insertChild( targetIndex, node );
387 model->endInsertRows();
394 model->beginInsertRows( QModelIndex(), childCount(), childCount() );
399 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....