17#include "moc_qgsdatabasetablecombobox.cpp"
26 , mProvider( provider )
27 , mConnection( connection )
30 if ( !provider.isEmpty() && !connection.isEmpty() )
45 mAllowEmpty = allowEmpty;
55void QgsDatabaseTableComboBox::init()
57 mComboBox =
new QComboBox();
59 mSortModel =
new QgsDatabaseTableComboBoxSortModel(
this );
61 mSortModel->setSourceModel( mModel );
62 mSortModel->setSortRole( Qt::DisplayRole );
63 mSortModel->setSortLocaleAware(
true );
64 mSortModel->setSortCaseSensitivity( Qt::CaseInsensitive );
65 mSortModel->setDynamicSortFilter(
true );
66 mSortModel->sort( 0 );
68 mComboBox->setModel( mSortModel );
70 QHBoxLayout *l =
new QHBoxLayout();
71 l->setContentsMargins( 0, 0, 0, 0 );
72 l->addWidget( mComboBox );
74 QToolButton *refreshButton =
new QToolButton();
75 refreshButton->setAutoRaise(
true );
76 refreshButton->setToolTip( tr(
"Refresh tables" ) );
78 l->addWidget( refreshButton );
83 connect( mComboBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::activated ),
this, &QgsDatabaseTableComboBox::indexChanged );
84 connect( mSortModel, &QAbstractItemModel::rowsInserted,
this, &QgsDatabaseTableComboBox::rowsChanged );
85 connect( mSortModel, &QAbstractItemModel::rowsRemoved,
this, &QgsDatabaseTableComboBox::rowsChanged );
93 if ( table.isEmpty() )
96 mComboBox->setCurrentIndex( 0 );
98 mComboBox->setCurrentIndex( -1 );
105 for (
const QModelIndex &proxyIdx : idxs )
110 mComboBox->setCurrentIndex( proxyIdx.row() );
115 mComboBox->setCurrentIndex( -1 );
121 if ( provider.isEmpty() && mConnection == connection )
124 if ( !provider.isEmpty() )
125 mProvider = provider;
127 mConnection = connection;
132 if ( !mConnection.isEmpty() )
140 mSortModel->setSourceModel( mModel );
142 oldModel->deleteLater();
149 if ( schema == mSchema )
153 if ( !mProvider.isEmpty() && !mConnection.isEmpty() )
159 mSortModel->setSourceModel( mModel );
160 oldModel->deleteLater();
176 const QModelIndex proxyIndex = mSortModel->index( mComboBox->currentIndex(), 0 );
177 if ( !proxyIndex.isValid() )
187 const QModelIndex proxyIndex = mSortModel->index( mComboBox->currentIndex(), 0 );
188 if ( !proxyIndex.isValid() )
196void QgsDatabaseTableComboBox::indexChanged(
int i )
202void QgsDatabaseTableComboBox::rowsChanged()
204 if ( mComboBox->count() == 1 || ( mAllowEmpty && mComboBox->count() == 2 && mComboBox->currentIndex() == 1 ) )
209 else if ( mComboBox->count() == 0 )
216QgsDatabaseTableComboBoxSortModel::QgsDatabaseTableComboBoxSortModel( QObject *parent )
217 : 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.
@ Empty
Entry is an empty entry.