Skip to content
Permalink
Browse files

Add refresh button

  • Loading branch information
elpaso committed Mar 12, 2020
1 parent 22712f5 commit c0c3326f90019e218432dcebf2907ab26e30be3a
@@ -77,7 +77,11 @@ Returns ``True`` if the widget contains a valid new table name
Returns the validation error or an empty string is the widget status is valid
%End

virtual void showEvent( QShowEvent *e );

%Docstring
Scroll to last selected index and expand it's children
%End

signals:

@@ -22,6 +22,7 @@
#include "qgsdataitemprovider.h"
#include "qgsproviderregistry.h"
#include "qgsprovidermetadata.h"
#include "qgssettings.h"


// List of data item provider keys that are filesystem based
@@ -57,8 +58,7 @@ QgsNewDatabaseTableNameWidget::QgsNewDatabaseTableNameWidget(
{
continue;
}
QgsProviderMetadata *metadata { QgsProviderRegistry::instance()->providerMetadata( provider->dataProviderKey() ) };
if ( ! metadata )
if ( ! QgsProviderRegistry::instance()->providerMetadata( provider->dataProviderKey() ) )
{
continue;
}
@@ -94,6 +94,11 @@ QgsNewDatabaseTableNameWidget::QgsNewDatabaseTableNameWidget(
validate();
} );

connect( mActionRefresh, &QAction::triggered, this, [ = ]
{
refreshModel( QModelIndex() );
} );

connect( mBrowserTreeView, &QgsBrowserTreeView::clicked, this, [ = ]( const QModelIndex & index )
{
if ( index.isValid() )
@@ -123,6 +128,9 @@ QgsNewDatabaseTableNameWidget::QgsNewDatabaseTableNameWidget(
if ( oldSchema != mSchemaName )
{
emit schemaNameChanged( mSchemaName );
// Store last viewed item
QgsSettings().setValue( QStringLiteral( "newDatabaseTableNameWidgetLastSelectedItem" ),
mBrowserProxyModel.data( index, QgsBrowserGuiModel::PathRole ).toString(), QgsSettings::Section::Gui );
validationRequired = true;
}
}
@@ -140,24 +148,56 @@ QgsNewDatabaseTableNameWidget::QgsNewDatabaseTableNameWidget(
validate();
}

void QgsNewDatabaseTableNameWidget::refreshModel( const QModelIndex &index )
{

QgsDataItem *item = mBrowserModel->dataItem( index );

if ( item && ( item->capabilities2() & QgsDataItem::Fertile ) )
{
mBrowserModel->refresh( index );
}

for ( int i = 0; i < mBrowserModel->rowCount( index ); i++ )
{
QModelIndex idx = mBrowserModel->index( i, 0, index );
QModelIndex proxyIdx = mBrowserProxyModel.mapFromSource( idx );
QgsDataItem *child = mBrowserModel->dataItem( idx );

// Check also expanded descendants so that the whole expanded path does not get collapsed if one item is collapsed.
// Fast items (usually root items) are refreshed so that when collapsed, it is obvious they are if empty (no expand symbol).
if ( mBrowserTreeView->isExpanded( proxyIdx ) || mBrowserTreeView->hasExpandedDescendant( proxyIdx ) || ( child && child->capabilities2() & QgsDataItem::Fast ) )
{
refreshModel( idx );
}
else
{
if ( child && ( child->capabilities2() & QgsDataItem::Fertile ) )
{
child->depopulate();
}
}
}
}

void QgsNewDatabaseTableNameWidget::updateUri()
{
const QString oldUri { mUri };
QgsProviderMetadata *md { QgsProviderRegistry::instance()->providerMetadata( mDataProviderKey ) };
if ( md )
QgsProviderMetadata *dataProviderMetadata { QgsProviderRegistry::instance()->providerMetadata( mDataProviderKey ) };
if ( dataProviderMetadata )
{
QgsAbstractProviderConnection *conn { md->findConnection( mConnectionName ) };
QgsAbstractProviderConnection *conn { dataProviderMetadata->findConnection( mConnectionName ) };
if ( conn )
{
QVariantMap uriParts { md->decodeUri( conn->uri() ) };
QVariantMap uriParts { dataProviderMetadata->decodeUri( conn->uri() ) };
uriParts[ QStringLiteral( "layerName" ) ] = mTableName;
uriParts[ QStringLiteral( "schema" ) ] = mSchemaName;
uriParts[ QStringLiteral( "table" ) ] = mTableName;
if ( mIsFilePath )
{
uriParts[ QStringLiteral( "dbname" ) ] = mSchemaName;
}
mUri = md->encodeUri( uriParts );
mUri = dataProviderMetadata->encodeUri( uriParts );
}
else
{
@@ -306,3 +346,28 @@ QString QgsNewDatabaseTableNameWidget::validationError() const
{
return mValidationError;
}

void QgsNewDatabaseTableNameWidget::showEvent( QShowEvent *e )
{
QWidget::showEvent( e );
QString lastSelectedPath( QgsSettings().value( QStringLiteral( "newDatabaseTableNameWidgetLastSelectedItem" ),
QString(), QgsSettings::Section::Gui ).toString() );
if ( ! lastSelectedPath.isEmpty() )
{
QModelIndexList items = mBrowserProxyModel.match(
mBrowserProxyModel.index( 0, 0 ),
QgsBrowserGuiModel::PathRole,
QVariant::fromValue( lastSelectedPath ),
1,
Qt::MatchRecursive );
if ( items.count( ) > 0 )
{
QModelIndex expandIndex = items.at( 0 );
if ( expandIndex.isValid() )
{
mBrowserTreeView->scrollTo( expandIndex, QgsBrowserTreeView::ScrollHint::PositionAtTop );
mBrowserTreeView->expand( expandIndex );
}
}
}
}
@@ -89,7 +89,8 @@ class GUI_EXPORT QgsNewDatabaseTableNameWidget : public QWidget, private Ui::Qgs
*/
QString validationError() const;


//! Scroll to last selected index and expand it's children
void showEvent( QShowEvent *e ) override;

signals:

@@ -135,6 +136,7 @@ class GUI_EXPORT QgsNewDatabaseTableNameWidget : public QWidget, private Ui::Qgs
void updateUri();
void validate();
QStringList tableNames();
void refreshModel( const QModelIndex &index );

QgsBrowserProxyModel mBrowserProxyModel;
QgsBrowserGuiModel *mBrowserModel = nullptr;
@@ -11,9 +11,6 @@
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="spacing">
<number>4</number>
</property>
<property name="leftMargin">
<number>4</number>
</property>
@@ -27,26 +24,21 @@
<number>4</number>
</property>
<item>
<layout class="QVBoxLayout" name="verticalLayout_2">
<property name="spacing">
<number>3</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
<widget class="QToolBar" name="mBrowserToolbar">
<property name="iconSize">
<size>
<width>24</width>
<height>24</height>
</size>
</property>
<property name="bottomMargin">
<number>0</number>
<property name="floatable">
<bool>false</bool>
</property>
<item>
<widget class="QgsBrowserTreeView" name="mBrowserTreeView"/>
</item>
</layout>
<addaction name="mActionRefresh"/>
</widget>
</item>
<item>
<widget class="QgsBrowserTreeView" name="mBrowserTreeView"/>
</item>
<item>
<widget class="QLabel" name="label">
@@ -125,6 +117,8 @@
</customwidgets>
<resources>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
</resources>
<connections/>
</ui>

0 comments on commit c0c3326

Please sign in to comment.
You can’t perform that action at this time.