20 #include <QHBoxLayout>
21 #include <QToolButton>
25 , mProvider( provider )
26 , mConnection( connection )
29 if ( !provider.isEmpty() && !connection.isEmpty() )
44 mAllowEmpty = allowEmpty;
54 void 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() )
157 mSortModel->setSourceModel( mModel );
158 oldModel->deleteLater();
174 const QModelIndex proxyIndex = mSortModel->index( mComboBox->currentIndex(), 0 );
175 if ( !proxyIndex.isValid() )
185 const QModelIndex proxyIndex = mSortModel->index( mComboBox->currentIndex(), 0 );
186 if ( !proxyIndex.isValid() )
194 void QgsDatabaseTableComboBox::indexChanged(
int i )
200 void QgsDatabaseTableComboBox::rowsChanged()
202 if ( mComboBox->count() == 1 || ( mAllowEmpty && mComboBox->count() == 2 && mComboBox->currentIndex() == 1 ) )
207 else if ( mComboBox->count() == 0 )
214 QgsDatabaseTableComboBoxSortModel::QgsDatabaseTableComboBoxSortModel( QObject *parent )
215 : QSortFilterProxyModel( parent )
220 bool QgsDatabaseTableComboBoxSortModel::lessThan(
const QModelIndex &left,
const QModelIndex &right )
const
229 const QString leftStr = sourceModel()->data( left ).toString();
230 const QString rightStr = sourceModel()->data( right ).toString();
231 return QString::localeAwareCompare( leftStr, rightStr ) < 0;