25 , mProvider( provider )
26 , mConnection( connection )
29 if ( !provider.isEmpty() && !connection.isEmpty() )
44 mAllowEmpty = allowEmpty;
54void QgsDatabaseTableComboBox::init()
56 mComboBox =
new QComboBox();
58 mSortModel =
new QgsDatabaseTableComboBoxSortModel(
this );
60 mSortModel->setSourceModel( mModel );
61 mSortModel->setSortRole( Qt::DisplayRole );
62 mSortModel->setSortLocaleAware(
true );
63 mSortModel->setSortCaseSensitivity( Qt::CaseInsensitive );
64 mSortModel->setDynamicSortFilter(
true );
65 mSortModel->sort( 0 );
67 mComboBox->setModel( mSortModel );
69 QHBoxLayout *l =
new QHBoxLayout();
70 l->setContentsMargins( 0, 0, 0, 0 );
71 l->addWidget( mComboBox );
73 QToolButton *refreshButton =
new QToolButton();
74 refreshButton->setAutoRaise(
true );
75 refreshButton->setToolTip( tr(
"Refresh tables" ) );
77 l->addWidget( refreshButton );
82 connect( mComboBox,
static_cast < void ( QComboBox::* )(
int )
> ( &QComboBox::activated ),
this, &QgsDatabaseTableComboBox::indexChanged );
83 connect( mSortModel, &QAbstractItemModel::rowsInserted,
this, &QgsDatabaseTableComboBox::rowsChanged );
84 connect( mSortModel, &QAbstractItemModel::rowsRemoved,
this, &QgsDatabaseTableComboBox::rowsChanged );
92 if ( table.isEmpty() )
95 mComboBox->setCurrentIndex( 0 );
97 mComboBox->setCurrentIndex( -1 );
104 for (
const QModelIndex &proxyIdx : idxs )
109 mComboBox->setCurrentIndex( proxyIdx.row() );
114 mComboBox->setCurrentIndex( -1 );
120 if ( provider.isEmpty() && mConnection == connection )
123 if ( !provider.isEmpty() )
124 mProvider = provider;
126 mConnection = connection;
131 if ( !mConnection.isEmpty() )
139 mSortModel->setSourceModel( mModel );
141 oldModel->deleteLater();
148 if ( schema == mSchema )
152 if ( !mProvider.isEmpty() && !mConnection.isEmpty() )
158 mSortModel->setSourceModel( mModel );
159 oldModel->deleteLater();
175 const QModelIndex proxyIndex = mSortModel->index( mComboBox->currentIndex(), 0 );
176 if ( !proxyIndex.isValid() )
186 const QModelIndex proxyIndex = mSortModel->index( mComboBox->currentIndex(), 0 );
187 if ( !proxyIndex.isValid() )
195void QgsDatabaseTableComboBox::indexChanged(
int i )
201void QgsDatabaseTableComboBox::rowsChanged()
203 if ( mComboBox->count() == 1 || ( mAllowEmpty && mComboBox->count() == 2 && mComboBox->currentIndex() == 1 ) )
208 else if ( mComboBox->count() == 0 )
215QgsDatabaseTableComboBoxSortModel::QgsDatabaseTableComboBoxSortModel( QObject *parent )
216 : QSortFilterProxyModel( parent )
221bool QgsDatabaseTableComboBoxSortModel::lessThan(
const QModelIndex &left,
const QModelIndex &right )
const
230 const QString leftStr = sourceModel()->data( left ).toString();
231 const QString rightStr = sourceModel()->data( right ).toString();
232 return QString::localeAwareCompare( leftStr, rightStr ) < 0;
The QgsAbstractDatabaseProviderConnection class provides common functionality for DB based connection...
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
void setSchema(const QString &schema)
Sets the schema from which to retrieve the available tables.
void tableChanged(const QString &table, const QString &schema=QString())
Emitted whenever the currently selected table changes.
void setTable(const QString &table, const QString &schema=QString())
Sets the current table selected in the combo box.
void setConnectionName(const QString &connection, const QString &provider=QString())
Sets the database connection name from which to retrieve the available tables.
void refreshTables()
Refreshes the list of available tables.
QString currentSchema() const
Returns the schema of the current table selected in the combo box.
bool allowEmptyTable() const
Returns true if the combobox allows the empty table ("not set") choice.
void setAllowEmptyTable(bool allowEmpty)
Sets whether an optional empty table ("not set") option is present in the combobox.
QgsDatabaseTableComboBox(const QString &provider, const QString &connection, const QString &schema=QString(), QWidget *parent=nullptr)
Constructor for QgsDatabaseTableComboBox, for the specified provider and connection.
QString currentTable() const
Returns the name of the current table selected in the combo box.
A model containing tables from a database connection.
void refresh()
Refreshes the table list by querying the underlying connection.
void setAllowEmptyTable(bool allowEmpty)
Sets whether an optional empty table ("not set") option is present in the model.
@ RoleEmpty
Entry is an empty entry.
@ RoleTableName
Table name.
@ RoleSchema
Table schema.