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 );
44 QMap<QString, QgsHistoryEntryDateGroupNode *> mDateGroupNodes;
50 QgsHistoryEntryDateGroupNode(
const QString &title,
const QString &key )
56 QVariant
data(
int role = Qt::DisplayRole )
const override
64 case Qt::DecorationRole:
80 : QAbstractItemModel( parent )
82 , mRegistry( registry ? registry :
QgsGui::historyProviderRegistry() )
83 , mProviderId( providerId )
84 , mBackends( backends )
86 mRootNode = std::make_unique<QgsHistoryEntryRootNode>();
89 const QList<QgsHistoryEntry> entries = mRegistry->
queryEntries( QDateTime(), QDateTime(), mProviderId, mBackends );
98 mIdToNodeHash.insert( entry.id, node );
99 mRootNode->addEntryNode( entry, node,
nullptr );
130 return QModelIndex();
134 return QModelIndex();
136 return createIndex( row, column, n->
childAt( row ) );
141 if ( !child.isValid() )
142 return QModelIndex();
146 return indexOfParentNode( n->parent() );
151 return QModelIndex();
164 return node->
data( role );
169 if ( !
index.isValid() )
171 Qt::ItemFlags rootFlags = Qt::ItemFlags();
175 Qt::ItemFlags f = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
181 if ( !
index.isValid() )
182 return mRootNode.get();
190 if ( !( mBackends & backend ) )
192 if ( !mProviderId.isEmpty() && entry.
providerId != mProviderId )
201 mIdToNodeHash.insert(
id, node );
202 mRootNode->addEntryNode( entry, node,
this );
209 if ( !( mBackends & backend ) )
217 historyEntry.
entry = entry;
218 const QString providerId = historyEntry.
providerId;
223 const QModelIndex nodeIndex = node2index( node );
224 const int existingChildRows = node->childCount();
226 const int newChildRows = node->childCount();
228 if ( newChildRows < existingChildRows )
230 beginRemoveRows( nodeIndex, newChildRows, existingChildRows - 1 );
233 else if ( existingChildRows < newChildRows )
235 beginInsertRows( nodeIndex, existingChildRows, newChildRows - 1 );
239 const QModelIndex topLeft =
index( 0, 0, nodeIndex );
240 const QModelIndex bottomRight =
index( newChildRows - 1,
columnCount() - 1, nodeIndex );
241 emit dataChanged( topLeft, bottomRight );
242 emit dataChanged( nodeIndex, nodeIndex );
249 if ( !( mBackends & backend ) )
252 if ( !mProviderId.isEmpty() && !providerId.isEmpty() && providerId != mProviderId )
257 mIdToNodeHash.clear();
263 if ( !node || !node->
parent() )
264 return QModelIndex();
266 QModelIndex parentIndex = node2index( node->
parent() );
269 Q_ASSERT( row >= 0 );
270 return index( row, 0, parentIndex );
273QModelIndex QgsHistoryEntryModel::indexOfParentNode(
QgsHistoryEntryNode *parentNode )
const
275 Q_ASSERT( parentNode );
278 if ( !grandParentNode )
279 return QModelIndex();
281 int row = grandParentNode->
indexOf( parentNode );
282 Q_ASSERT( row >= 0 );
284 return createIndex( row, 0, parentNode );
291QVariant QgsHistoryEntryRootNode::data(
int )
const
298 QgsHistoryEntryDateGroupNode *targetDateNode = dateNode( entry.
timestamp, model );
302 const QModelIndex dateNodeIndex = model->node2index( targetDateNode );
303 model->beginInsertRows( dateNodeIndex, 0, 0 );
305 targetDateNode->insertChild( 0, node );
308 model->endInsertRows();
312QString QgsHistoryEntryRootNode::dateGroup(
const QDateTime ×tamp, QString &sortKey )
315 if ( timestamp.date() == QDateTime::currentDateTime().date() )
317 groupString = QObject::tr(
"Today" );
318 sortKey = QStringLiteral(
"0" );
322 const qint64 intervalDays = timestamp.date().daysTo( QDateTime::currentDateTime().date() );
323 if ( intervalDays == 1 )
325 groupString = QObject::tr(
"Yesterday" );
326 sortKey = QStringLiteral(
"1" );
328 else if ( intervalDays < 8 )
330 groupString = QObject::tr(
"Last 7 days" );
331 sortKey = QStringLiteral(
"2" );
336 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' ) );
337 groupString = timestamp.toString( QStringLiteral(
"MMMM yyyy" ) );
343QgsHistoryEntryDateGroupNode *QgsHistoryEntryRootNode::dateNode(
const QDateTime ×tamp,
QgsHistoryEntryModel *model )
345 QString dateGroupKey;
346 const QString dateTitle = dateGroup( timestamp, dateGroupKey );
348 QgsHistoryEntryDateGroupNode *node = mDateGroupNodes.value( dateGroupKey );
351 node =
new QgsHistoryEntryDateGroupNode( dateTitle, dateGroupKey );
352 mDateGroupNodes[dateGroupKey] = node;
355 bool isInsert =
false;
356 for (
const auto &child : mChildren )
358 if ( QgsHistoryEntryDateGroupNode *candidateNode =
dynamic_cast<QgsHistoryEntryDateGroupNode *
>( child.get() ) )
360 if ( candidateNode->mKey > dateGroupKey )
373 model->beginInsertRows( QModelIndex(), targetIndex, targetIndex );
375 insertChild( targetIndex, node );
378 model->endInsertRows();
385 model->beginInsertRows( QModelIndex(), childCount(), childCount() );
390 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....