17 #include <QTreeWidgetItemIterator>
28 #include <QDialogButtonBox>
29 #include <QPushButton>
32 QStringList QgsNewDatabaseTableNameWidget::FILESYSTEM_BASED_DATAITEM_PROVIDERS { QStringLiteral(
"GPKG" ), QStringLiteral(
"spatialite" ) };
36 const QStringList &providersFilter,
49 mBrowserModel = browserModel;
56 mOkButton->setEnabled(
false );
58 QStringList shownDataItemProvidersFilter;
61 for (
const auto &provider : providerList )
63 if ( provider->dataProviderKey().isEmpty() )
71 if ( provider->capabilities() & QgsDataProvider::DataCapability::Database )
73 if ( providersFilter.isEmpty() || providersFilter.contains( provider->dataProviderKey() ) )
75 mShownProviders.insert( provider->dataProviderKey() );
76 shownDataItemProvidersFilter.push_back( provider->name() );
86 if ( ! providersFilter.isEmpty() && shownDataItemProvidersFilter.isEmpty() )
88 shownDataItemProvidersFilter = providersFilter;
92 mBrowserTreeView->setHeaderHidden(
true );
93 mBrowserTreeView->setModel( &mBrowserProxyModel );
94 mBrowserTreeView->setBrowserModel( mBrowserModel );
97 connect( mNewTableName, &QLineEdit::textChanged,
this, [ = ]
99 mTableName = mNewTableName->text();
105 connect( mActionRefresh, &QAction::triggered,
this, [ = ]
107 refreshModel( QModelIndex() );
110 connect( mBrowserTreeView, &QgsBrowserTreeView::clicked,
this, [ = ](
const QModelIndex & index )
112 if ( index.isValid() )
114 if ( const QgsDataItem *dataItem = mBrowserProxyModel.dataItem( index ) )
116 if ( const QgsDataCollectionItem *collectionItem = qobject_cast<const QgsDataCollectionItem *>( dataItem ) )
118 const QString providerKey { QgsApplication::dataItemProviderRegistry()->dataProviderKey( dataItem->providerKey() ) };
119 bool validationRequired { false };
120 const QString oldSchema { mSchemaName };
122 if ( mDataProviderKey != providerKey )
125 mDataProviderKey = providerKey;
126 emit providerKeyChanged( providerKey );
127 validationRequired = true;
130 if ( collectionItem->layerCollection( ) )
132 mIsFilePath = FILESYSTEM_BASED_DATAITEM_PROVIDERS.contains( collectionItem->providerKey() );
134 mSchemaName = mIsFilePath ? collectionItem->path().remove( QRegularExpression( QStringLiteral(
"^[A-z]+:/" ) ) ) : collectionItem->name();
135 mConnectionName = mIsFilePath ? collectionItem->name() : collectionItem->parent()->name();
136 if ( oldSchema != mSchemaName )
138 emit schemaNameChanged( mSchemaName );
140 QgsSettings().setValue( QStringLiteral(
"newDatabaseTableNameWidgetLastSelectedItem" ),
141 mBrowserProxyModel.data( index, QgsBrowserGuiModel::PathRole ).toString(), QgsSettings::Section::Gui );
142 validationRequired = true;
146 if ( validationRequired )
164 mOkButton->setVisible( visible );
167 void QgsNewDatabaseTableNameWidget::refreshModel(
const QModelIndex &index )
170 QgsDataItem *item = mBrowserModel->dataItem( index );
174 mBrowserModel->
refresh( index );
177 for (
int i = 0; i < mBrowserModel->rowCount( index ); i++ )
179 QModelIndex idx = mBrowserModel->index( i, 0, index );
180 QModelIndex proxyIdx = mBrowserProxyModel.mapFromSource( idx );
181 QgsDataItem *child = mBrowserModel->dataItem( idx );
185 if ( mBrowserTreeView->isExpanded( proxyIdx ) || mBrowserTreeView->hasExpandedDescendant( proxyIdx ) || ( child && child->
capabilities2() &
QgsDataItem::Fast ) )
199 void QgsNewDatabaseTableNameWidget::updateUri()
201 const QString oldUri { mUri };
203 if ( dataProviderMetadata )
208 QVariantMap uriParts { dataProviderMetadata->decodeUri( conn->uri() ) };
209 uriParts[ QStringLiteral(
"layerName" ) ] = mTableName;
210 uriParts[ QStringLiteral(
"schema" ) ] = mSchemaName;
211 uriParts[ QStringLiteral(
"table" ) ] = mTableName;
214 uriParts[ QStringLiteral(
"dbname" ) ] = mSchemaName;
216 mUri = dataProviderMetadata->encodeUri( uriParts );
228 if ( mUri != oldUri )
230 emit uriChanged( mUri );
251 return mDataProviderKey;
254 void QgsNewDatabaseTableNameWidget::validate()
256 const bool wasValid { mIsValid };
258 mIsValid = ! mDataProviderKey.isEmpty() &&
259 mShownProviders.contains( mDataProviderKey ) &&
260 ! mSchemaName.isEmpty() &&
261 ! mTableName.isEmpty() &&
262 ! tableNames( ).contains( mTableName );
264 mValidationError.clear();
267 bool isError {
false };
271 if ( mTableName.isEmpty() && mSchemaName.isEmpty() )
273 mValidationError = tr(
"Select a database schema and enter a unique name for the new table" );
275 else if ( ! mTableName.isEmpty() &&
276 ! mSchemaName.isEmpty() &&
277 tableNames( ).contains( mTableName ) )
280 mValidationError = tr(
"A table named '%1' already exists" ).arg( mTableName );
282 else if ( mSchemaName.isEmpty() )
284 mValidationError = tr(
"Select a database schema" );
286 else if ( mTableName.isEmpty() )
288 mValidationError = tr(
"Enter a unique name for the new table" );
290 else if ( tableNames( ).contains( mTableName ) )
292 mValidationError = tr(
"A table named '%1' already exists" ).arg( mTableName );
296 mValidationError = tr(
"Select a database schema and enter a unique name for the new table" );
300 mValidationResults->setStyleSheet( isError ?
301 QStringLiteral(
"* { color: red; }" ) :
304 mValidationResults->setText( mValidationError );
305 mValidationResults->setVisible( ! mIsValid );
306 if ( wasValid != mIsValid )
308 emit validationChanged( mIsValid );
312 QStringList QgsNewDatabaseTableNameWidget::tableNames()
314 QStringList tableNames;
315 QModelIndex index { mBrowserTreeView->currentIndex() };
316 if ( index.isValid() )
318 QgsDataItem *dataItem { mBrowserProxyModel.dataItem( index ) };
322 if ( ! dataProviderKey.isEmpty() )
328 if ( parentDataItem )
333 const QString cacheKey { conn->
uri() + dataItem->name() };
334 if ( mTableNamesCache.contains( cacheKey ) )
336 tableNames = mTableNamesCache.value( cacheKey );
341 for (
const auto &tp : tables )
343 tableNames.push_back( tp.tableName() );
345 mTableNamesCache[ cacheKey ] = tableNames;
363 return mValidationError;
368 QWidget::showEvent( e );
369 QString lastSelectedPath(
QgsSettings().value( QStringLiteral(
"newDatabaseTableNameWidgetLastSelectedItem" ),
370 QString(), QgsSettings::Section::Gui ).toString() );
371 if ( ! lastSelectedPath.isEmpty() )
373 QModelIndexList items = mBrowserProxyModel.match(
374 mBrowserProxyModel.index( 0, 0 ),
376 QVariant::fromValue( lastSelectedPath ),
378 Qt::MatchRecursive );
379 if ( items.count( ) > 0 )
381 QModelIndex expandIndex = items.at( 0 );
382 if ( expandIndex.isValid() )
384 mBrowserTreeView->scrollTo( expandIndex, QgsBrowserTreeView::ScrollHint::PositionAtTop );
385 mBrowserTreeView->expand( expandIndex );
397 mWidget =
new QgsNewDatabaseTableNameWidget( browserModel, providersFilter );
398 QVBoxLayout *vl =
new QVBoxLayout();
399 vl->addWidget( mWidget, 1 );
400 QDialogButtonBox *buttonBox =
new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel );
401 connect( buttonBox, &QDialogButtonBox::accepted,
this, &QDialog::accept );
402 connect( buttonBox, &QDialogButtonBox::rejected,
this, &QDialog::reject );
403 buttonBox->button( QDialogButtonBox::Ok )->setEnabled(
false );
405 vl->addWidget( buttonBox );
411 return mWidget->schema();
416 return mWidget->uri();
421 return mWidget->table();
426 return mWidget->dataProviderKey();
431 return mWidget->isValid();
436 return mWidget->validationError();