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 )
113 if ( proxyIdx.isValid()
117 mComboBox->setCurrentIndex( proxyIdx.row() );
122 mComboBox->setCurrentIndex( -1 );
128 if ( provider.isEmpty() && mConnection == connection )
131 if ( !provider.isEmpty() )
132 mProvider = provider;
134 mConnection = connection;
139 if ( !mConnection.isEmpty() )
142 mModel->setAllowEmptyTable( mAllowEmpty );
147 mSortModel->setSourceModel( mModel );
149 oldModel->deleteLater();
156 if ( schema == mSchema )
160 if ( !mProvider.isEmpty() && !mConnection.isEmpty() )
165 mModel->setAllowEmptyTable( mAllowEmpty );
166 mSortModel->setSourceModel( mModel );
167 oldModel->deleteLater();
183 const QModelIndex proxyIndex = mSortModel->index( mComboBox->currentIndex(), 0 );
184 if ( !proxyIndex.isValid() )
194 const QModelIndex proxyIndex = mSortModel->index( mComboBox->currentIndex(), 0 );
195 if ( !proxyIndex.isValid() )
203void QgsDatabaseTableComboBox::indexChanged(
int i )
209void QgsDatabaseTableComboBox::rowsChanged()
211 if ( mComboBox->count() == 1 || ( mAllowEmpty && mComboBox->count() == 2 && mComboBox->currentIndex() == 1 ) )
216 else if ( mComboBox->count() == 0 )
223QgsDatabaseTableComboBoxSortModel::QgsDatabaseTableComboBoxSortModel( QObject *parent )
224 : 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.