25#include "moc_qgsdatabasetablecombobox.cpp"
29 , mProvider( provider )
30 , mConnection( connection )
33 if ( !provider.isEmpty() && !connection.isEmpty() )
48 mAllowEmpty = allowEmpty;
50 mModel->setAllowEmptyTable( allowEmpty );
58void QgsDatabaseTableComboBox::init()
60 mComboBox =
new QComboBox();
62 mSortModel =
new QgsDatabaseTableComboBoxSortModel(
this );
64 mSortModel->setSourceModel( mModel );
65 mSortModel->setSortRole( Qt::DisplayRole );
66 mSortModel->setSortLocaleAware(
true );
67 mSortModel->setSortCaseSensitivity( Qt::CaseInsensitive );
68 mSortModel->setDynamicSortFilter(
true );
69 mSortModel->sort( 0 );
71 mComboBox->setModel( mSortModel );
73 QHBoxLayout *l =
new QHBoxLayout();
74 l->setContentsMargins( 0, 0, 0, 0 );
75 l->addWidget( mComboBox );
77 QToolButton *refreshButton =
new QToolButton();
78 refreshButton->setAutoRaise(
true );
79 refreshButton->setToolTip( tr(
"Refresh tables" ) );
81 l->addWidget( refreshButton );
86 connect( mComboBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::activated ),
this, &QgsDatabaseTableComboBox::indexChanged );
87 connect( mSortModel, &QAbstractItemModel::rowsInserted,
this, &QgsDatabaseTableComboBox::rowsChanged );
88 connect( mSortModel, &QAbstractItemModel::rowsRemoved,
this, &QgsDatabaseTableComboBox::rowsChanged );
96 if ( table.isEmpty() )
99 mComboBox->setCurrentIndex( 0 );
101 mComboBox->setCurrentIndex( -1 );
108 for (
const QModelIndex &proxyIdx : idxs )
113 mComboBox->setCurrentIndex( proxyIdx.row() );
118 mComboBox->setCurrentIndex( -1 );
124 if ( provider.isEmpty() && mConnection == connection )
127 if ( !provider.isEmpty() )
128 mProvider = provider;
130 mConnection = connection;
135 if ( !mConnection.isEmpty() )
138 mModel->setAllowEmptyTable( mAllowEmpty );
143 mSortModel->setSourceModel( mModel );
145 oldModel->deleteLater();
152 if ( schema == mSchema )
156 if ( !mProvider.isEmpty() && !mConnection.isEmpty() )
161 mModel->setAllowEmptyTable( mAllowEmpty );
162 mSortModel->setSourceModel( mModel );
163 oldModel->deleteLater();
179 const QModelIndex proxyIndex = mSortModel->index( mComboBox->currentIndex(), 0 );
180 if ( !proxyIndex.isValid() )
190 const QModelIndex proxyIndex = mSortModel->index( mComboBox->currentIndex(), 0 );
191 if ( !proxyIndex.isValid() )
199void QgsDatabaseTableComboBox::indexChanged(
int i )
205void QgsDatabaseTableComboBox::rowsChanged()
207 if ( mComboBox->count() == 1 || ( mAllowEmpty && mComboBox->count() == 2 && mComboBox->currentIndex() == 1 ) )
212 else if ( mComboBox->count() == 0 )
219QgsDatabaseTableComboBoxSortModel::QgsDatabaseTableComboBoxSortModel( QObject *parent )
220 : QSortFilterProxyModel( parent )
224bool QgsDatabaseTableComboBoxSortModel::lessThan(
const QModelIndex &left,
const QModelIndex &right )
const
233 const QString leftStr = sourceModel()->data( left ).toString();
234 const QString rightStr = sourceModel()->data( right ).toString();
235 return QString::localeAwareCompare( leftStr, rightStr ) < 0;
Provides common functionality for database based connections.
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.
@ Empty
Entry is an empty entry.