26#include "moc_qgsdatabasetablecombobox.cpp"
28using namespace Qt::StringLiterals;
32 , mProvider( provider )
33 , mConnection( connection )
36 if ( !provider.isEmpty() && !connection.isEmpty() )
51 mAllowEmpty = allowEmpty;
53 mModel->setAllowEmptyTable( allowEmpty );
61void QgsDatabaseTableComboBox::init()
63 mComboBox =
new QComboBox();
65 mSortModel =
new QgsDatabaseTableComboBoxSortModel(
this );
67 mSortModel->setSourceModel( mModel );
68 mSortModel->setSortRole( Qt::DisplayRole );
69 mSortModel->setSortLocaleAware(
true );
70 mSortModel->setSortCaseSensitivity( Qt::CaseInsensitive );
71 mSortModel->setDynamicSortFilter(
true );
72 mSortModel->sort( 0 );
74 mComboBox->setModel( mSortModel );
76 QHBoxLayout *l =
new QHBoxLayout();
77 l->setContentsMargins( 0, 0, 0, 0 );
78 l->addWidget( mComboBox );
80 QToolButton *refreshButton =
new QToolButton();
81 refreshButton->setAutoRaise(
true );
82 refreshButton->setToolTip( tr(
"Refresh tables" ) );
84 l->addWidget( refreshButton );
89 connect( mComboBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::activated ),
this, &QgsDatabaseTableComboBox::indexChanged );
90 connect( mSortModel, &QAbstractItemModel::rowsInserted,
this, &QgsDatabaseTableComboBox::rowsChanged );
91 connect( mSortModel, &QAbstractItemModel::rowsRemoved,
this, &QgsDatabaseTableComboBox::rowsChanged );
99 if ( table.isEmpty() )
102 mComboBox->setCurrentIndex( 0 );
104 mComboBox->setCurrentIndex( -1 );
111 for (
const QModelIndex &proxyIdx : idxs )
116 mComboBox->setCurrentIndex( proxyIdx.row() );
121 mComboBox->setCurrentIndex( -1 );
127 if ( provider.isEmpty() && mConnection == connection )
130 if ( !provider.isEmpty() )
131 mProvider = provider;
133 mConnection = connection;
138 if ( !mConnection.isEmpty() )
141 mModel->setAllowEmptyTable( mAllowEmpty );
146 mSortModel->setSourceModel( mModel );
148 oldModel->deleteLater();
155 if ( schema == mSchema )
159 if ( !mProvider.isEmpty() && !mConnection.isEmpty() )
164 mModel->setAllowEmptyTable( mAllowEmpty );
165 mSortModel->setSourceModel( mModel );
166 oldModel->deleteLater();
182 const QModelIndex proxyIndex = mSortModel->index( mComboBox->currentIndex(), 0 );
183 if ( !proxyIndex.isValid() )
193 const QModelIndex proxyIndex = mSortModel->index( mComboBox->currentIndex(), 0 );
194 if ( !proxyIndex.isValid() )
202void QgsDatabaseTableComboBox::indexChanged(
int i )
208void QgsDatabaseTableComboBox::rowsChanged()
210 if ( mComboBox->count() == 1 || ( mAllowEmpty && mComboBox->count() == 2 && mComboBox->currentIndex() == 1 ) )
215 else if ( mComboBox->count() == 0 )
222QgsDatabaseTableComboBoxSortModel::QgsDatabaseTableComboBoxSortModel( QObject *parent )
223 : QSortFilterProxyModel( parent )
227bool QgsDatabaseTableComboBoxSortModel::lessThan(
const QModelIndex &left,
const QModelIndex &right )
const
236 const QString leftStr = sourceModel()->data( left ).toString();
237 const QString rightStr = sourceModel()->data( right ).toString();
238 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.