27#include "moc_qgshistoryentrymodel.cpp"
33 QVariant
data(
int = Qt::DisplayRole )
const override;
35 void addEntryNode(
const QgsHistoryEntry &entry, QgsHistoryEntryNode *node, QgsHistoryEntryModel *model );
41 static QString dateGroup(
const QDateTime ×tamp, QString &sortKey );
43 QgsHistoryEntryDateGroupNode *dateNode(
const QDateTime ×tamp, QgsHistoryEntryModel *model );
46 QMap<QString, QgsHistoryEntryDateGroupNode *> mDateGroupNodes;
52 QgsHistoryEntryDateGroupNode(
const QString &title,
const QString &key )
58 QVariant
data(
int role = Qt::DisplayRole )
const override
66 case Qt::DecorationRole:
82 : QAbstractItemModel(
parent )
84 , mRegistry( registry ? registry :
QgsGui::historyProviderRegistry() )
85 , mProviderId( providerId )
86 , mBackends( backends )
88 mRootNode = std::make_unique<QgsHistoryEntryRootNode>();
91 const QList<QgsHistoryEntry> entries = mRegistry->queryEntries( QDateTime(), QDateTime(), mProviderId, mBackends );
100 mIdToNodeHash.insert( entry.id, node );
101 mRootNode->addEntryNode( entry, node,
nullptr );
132 return QModelIndex();
136 return QModelIndex();
138 return createIndex( row, column, n->
childAt( row ) );
143 if ( !child.isValid() )
144 return QModelIndex();
148 return indexOfParentNode( n->parent() );
153 return QModelIndex();
166 return node->
data( role );
171 if ( !
index.isValid() )
173 Qt::ItemFlags rootFlags = Qt::ItemFlags();
177 Qt::ItemFlags f = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
183 if ( !
index.isValid() )
184 return mRootNode.get();
192 if ( !( mBackends & backend ) )
194 if ( !mProviderId.isEmpty() && entry.
providerId != mProviderId )
203 mIdToNodeHash.insert(
id, node );
204 mRootNode->addEntryNode( entry, node,
this );
211 if ( !( mBackends & backend ) )
215 if ( QgsHistoryEntryNode *node = mIdToNodeHash.value(
id ) )
218 QgsHistoryEntry historyEntry = mRegistry->entry(
id, ok, backend );
219 historyEntry.
entry = entry;
220 const QString providerId = historyEntry.
providerId;
221 QgsAbstractHistoryProvider *provider = mRegistry->providerById( providerId );
225 const QModelIndex nodeIndex = node2index( node );
226 const int existingChildRows = node->childCount();
228 const int newChildRows = node->childCount();
230 if ( newChildRows < existingChildRows )
232 beginRemoveRows( nodeIndex, newChildRows, existingChildRows - 1 );
235 else if ( existingChildRows < newChildRows )
237 beginInsertRows( nodeIndex, existingChildRows, newChildRows - 1 );
241 const QModelIndex topLeft =
index( 0, 0, nodeIndex );
242 const QModelIndex bottomRight =
index( newChildRows - 1,
columnCount() - 1, nodeIndex );
243 emit dataChanged( topLeft, bottomRight );
244 emit dataChanged( nodeIndex, nodeIndex );
251 if ( !( mBackends & backend ) )
254 if ( !mProviderId.isEmpty() && !providerId.isEmpty() && providerId != mProviderId )
259 mIdToNodeHash.clear();
265 if ( !node || !node->
parent() )
266 return QModelIndex();
268 QModelIndex parentIndex = node2index( node->
parent() );
271 Q_ASSERT( row >= 0 );
272 return index( row, 0, parentIndex );
275QModelIndex QgsHistoryEntryModel::indexOfParentNode(
QgsHistoryEntryNode *parentNode )
const
277 Q_ASSERT( parentNode );
279 QgsHistoryEntryGroup *grandParentNode = parentNode->
parent();
280 if ( !grandParentNode )
281 return QModelIndex();
283 int row = grandParentNode->
indexOf( parentNode );
284 Q_ASSERT( row >= 0 );
286 return createIndex( row, 0, parentNode );
293QVariant QgsHistoryEntryRootNode::data(
int )
const
300 QgsHistoryEntryDateGroupNode *targetDateNode = dateNode( entry.
timestamp, model );
304 const QModelIndex dateNodeIndex = model->node2index( targetDateNode );
305 model->beginInsertRows( dateNodeIndex, 0, 0 );
307 targetDateNode->insertChild( 0, node );
310 model->endInsertRows();
314QString QgsHistoryEntryRootNode::dateGroup(
const QDateTime ×tamp, QString &sortKey )
317 if ( timestamp.date() == QDateTime::currentDateTime().date() )
319 groupString = QObject::tr(
"Today" );
320 sortKey = QStringLiteral(
"0" );
324 const qint64 intervalDays = timestamp.date().daysTo( QDateTime::currentDateTime().date() );
325 if ( intervalDays == 1 )
327 groupString = QObject::tr(
"Yesterday" );
328 sortKey = QStringLiteral(
"1" );
330 else if ( intervalDays < 8 )
332 groupString = QObject::tr(
"Last 7 days" );
333 sortKey = QStringLiteral(
"2" );
338 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' ) );
339 groupString = timestamp.toString( QStringLiteral(
"MMMM yyyy" ) );
345QgsHistoryEntryDateGroupNode *QgsHistoryEntryRootNode::dateNode(
const QDateTime ×tamp,
QgsHistoryEntryModel *model )
347 QString dateGroupKey;
348 const QString dateTitle = dateGroup( timestamp, dateGroupKey );
350 QgsHistoryEntryDateGroupNode *node = mDateGroupNodes.value( dateGroupKey );
353 node =
new QgsHistoryEntryDateGroupNode( dateTitle, dateGroupKey );
354 mDateGroupNodes[dateGroupKey] = node;
357 bool isInsert =
false;
358 for (
const auto &child : mChildren )
360 if ( QgsHistoryEntryDateGroupNode *candidateNode =
dynamic_cast<QgsHistoryEntryDateGroupNode *
>( child.get() ) )
362 if ( candidateNode->mKey > dateGroupKey )
375 model->beginInsertRows( QModelIndex(), targetIndex, targetIndex );
377 insertChild( targetIndex, node );
380 model->endInsertRows();
387 model->beginInsertRows( QModelIndex(), childCount(), childCount() );
392 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....