28#include "moc_qgshistoryentrymodel.cpp"
30using namespace Qt::StringLiterals;
36 QVariant
data(
int = Qt::DisplayRole )
const override;
38 void addEntryNode(
const QgsHistoryEntry &entry, QgsHistoryEntryNode *node, QgsHistoryEntryModel *model );
44 static QString dateGroup(
const QDateTime ×tamp, QString &sortKey );
46 QgsHistoryEntryDateGroupNode *dateNode(
const QDateTime ×tamp, QgsHistoryEntryModel *model );
49 QMap<QString, QgsHistoryEntryDateGroupNode *> mDateGroupNodes;
55 QgsHistoryEntryDateGroupNode(
const QString &title,
const QString &key )
61 QVariant
data(
int role = Qt::DisplayRole )
const override
69 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 );
135 return QModelIndex();
139 return QModelIndex();
141 return createIndex( row, column, n->
childAt( row ) );
146 if ( !child.isValid() )
147 return QModelIndex();
151 return indexOfParentNode( n->parent() );
156 return QModelIndex();
169 return node->
data( role );
174 if ( !
index.isValid() )
176 Qt::ItemFlags rootFlags = Qt::ItemFlags();
180 Qt::ItemFlags f = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
186 if ( !
index.isValid() )
187 return mRootNode.get();
195 if ( !( mBackends & backend ) )
197 if ( !mProviderId.isEmpty() && entry.
providerId != mProviderId )
206 mIdToNodeHash.insert(
id, node );
207 mRootNode->addEntryNode( entry, node,
this );
214 if ( !( mBackends & backend ) )
218 if ( QgsHistoryEntryNode *node = mIdToNodeHash.value(
id ) )
221 QgsHistoryEntry historyEntry = mRegistry->entry(
id, ok, backend );
222 historyEntry.
entry = entry;
223 const QString providerId = historyEntry.
providerId;
224 QgsAbstractHistoryProvider *provider = mRegistry->providerById( providerId );
228 const QModelIndex nodeIndex = node2index( node );
229 const int existingChildRows = node->childCount();
231 const int newChildRows = node->childCount();
233 if ( newChildRows < existingChildRows )
235 beginRemoveRows( nodeIndex, newChildRows, existingChildRows - 1 );
238 else if ( existingChildRows < newChildRows )
240 beginInsertRows( nodeIndex, existingChildRows, newChildRows - 1 );
244 const QModelIndex topLeft =
index( 0, 0, nodeIndex );
245 const QModelIndex bottomRight =
index( newChildRows - 1,
columnCount() - 1, nodeIndex );
246 emit dataChanged( topLeft, bottomRight );
247 emit dataChanged( nodeIndex, nodeIndex );
254 if ( !( mBackends & backend ) )
257 if ( !mProviderId.isEmpty() && !providerId.isEmpty() && providerId != mProviderId )
262 mIdToNodeHash.clear();
268 if ( !node || !node->
parent() )
269 return QModelIndex();
271 QModelIndex parentIndex = node2index( node->
parent() );
274 Q_ASSERT( row >= 0 );
275 return index( row, 0, parentIndex );
278QModelIndex QgsHistoryEntryModel::indexOfParentNode(
QgsHistoryEntryNode *parentNode )
const
280 Q_ASSERT( parentNode );
282 QgsHistoryEntryGroup *grandParentNode = parentNode->
parent();
283 if ( !grandParentNode )
284 return QModelIndex();
286 int row = grandParentNode->
indexOf( parentNode );
287 Q_ASSERT( row >= 0 );
289 return createIndex( row, 0, parentNode );
296QVariant QgsHistoryEntryRootNode::data(
int )
const
303 QgsHistoryEntryDateGroupNode *targetDateNode = dateNode( entry.
timestamp, model );
307 const QModelIndex dateNodeIndex = model->node2index( targetDateNode );
308 model->beginInsertRows( dateNodeIndex, 0, 0 );
310 targetDateNode->insertChild( 0, node );
313 model->endInsertRows();
317QString QgsHistoryEntryRootNode::dateGroup(
const QDateTime ×tamp, QString &sortKey )
320 if ( timestamp.date() == QDateTime::currentDateTime().date() )
322 groupString = QObject::tr(
"Today" );
327 const qint64 intervalDays = timestamp.date().daysTo( QDateTime::currentDateTime().date() );
328 if ( intervalDays == 1 )
330 groupString = QObject::tr(
"Yesterday" );
333 else if ( intervalDays < 8 )
335 groupString = QObject::tr(
"Last 7 days" );
341 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 );
342 groupString = timestamp.toString( u
"MMMM yyyy"_s );
348QgsHistoryEntryDateGroupNode *QgsHistoryEntryRootNode::dateNode(
const QDateTime ×tamp,
QgsHistoryEntryModel *model )
350 QString dateGroupKey;
351 const QString dateTitle = dateGroup( timestamp, dateGroupKey );
353 QgsHistoryEntryDateGroupNode *node = mDateGroupNodes.value( dateGroupKey );
356 node =
new QgsHistoryEntryDateGroupNode( dateTitle, dateGroupKey );
357 mDateGroupNodes[dateGroupKey] = node;
360 bool isInsert =
false;
361 for (
const auto &child : mChildren )
363 if ( QgsHistoryEntryDateGroupNode *candidateNode =
dynamic_cast<QgsHistoryEntryDateGroupNode *
>( child.get() ) )
365 if ( candidateNode->mKey > dateGroupKey )
378 model->beginInsertRows( QModelIndex(), targetIndex, targetIndex );
380 insertChild( targetIndex, node );
383 model->endInsertRows();
390 model->beginInsertRows( QModelIndex(), childCount(), childCount() );
395 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.
A singleton class containing various registry and other global members related to GUI classes.
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.
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....