Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport release-3_28] QgsBrowserGuiModel::flags(): cache value for faster browser redrawn (refs #53265) #53289

Merged
merged 2 commits into from
May 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 40 additions & 5 deletions src/gui/qgsbrowserguimodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ QgsDataItemGuiContext QgsBrowserGuiModel::createDataItemContext() const
return context;
}

struct QgsBrowserGuiModelCachedAcceptDropValue
{
bool acceptDrop;
int numberOfProviders;
};
Q_DECLARE_METATYPE( QgsBrowserGuiModelCachedAcceptDropValue )

Qt::ItemFlags QgsBrowserGuiModel::flags( const QModelIndex &index ) const
{
if ( !index.isValid() )
Expand All @@ -55,15 +62,43 @@ Qt::ItemFlags QgsBrowserGuiModel::flags( const QModelIndex &index ) const
flags |= Qt::ItemIsDropEnabled;
else
{
// new support
// Cache the value of acceptDrop(), as it can be slow to evaluate.
// e.g. for a OGR datasource, this requires to open it. And this method
// is called each time the browser is redrawn.
// We cache the number of providers too, to be able to invalidate the
// cached value if new providers are installed.
QVariant cachedProperty = ptr->property( "_qgs_accept_drop_cached" );
const QList<QgsDataItemGuiProvider *> providers = QgsGui::dataItemGuiProviderRegistry()->providers();
for ( QgsDataItemGuiProvider *provider : providers )
bool refreshAcceptDrop = true;
if ( cachedProperty.isValid() )
{
QgsBrowserGuiModelCachedAcceptDropValue cached = cachedProperty.value<QgsBrowserGuiModelCachedAcceptDropValue>();
if ( cached.numberOfProviders == providers.size() )
{
refreshAcceptDrop = false;
if ( cached.acceptDrop )
flags |= Qt::ItemIsDropEnabled;
}
}

if ( refreshAcceptDrop )
{
if ( provider->acceptDrop( ptr, createDataItemContext() ) )
// new support
for ( QgsDataItemGuiProvider *provider : providers )
{
flags |= Qt::ItemIsDropEnabled;
break;
if ( provider->acceptDrop( ptr, createDataItemContext() ) )
{
flags |= Qt::ItemIsDropEnabled;
break;
}
}

QgsBrowserGuiModelCachedAcceptDropValue cached;
cached.acceptDrop = ( flags & Qt::ItemIsDropEnabled ) != 0;
cached.numberOfProviders = providers.size();
QVariant var;
var.setValue( cached );
ptr->setProperty( "_qgs_accept_drop_cached", var );
}
}
return flags;
Expand Down