20 #include <QHBoxLayout>
21 #include <QToolButton>
25 , mProvider( provider )
27 if ( !provider.isEmpty() && !connection.isEmpty() )
41 mAllowEmpty = allowEmpty;
51 void QgsDatabaseSchemaComboBox::init()
53 mComboBox =
new QComboBox();
55 mSortModel =
new QgsDatabaseSchemaComboBoxSortModel(
this );
58 mSortModel->setSourceModel( mModel );
60 mSortModel->setSortRole( Qt::DisplayRole );
61 mSortModel->setSortLocaleAware(
true );
62 mSortModel->setSortCaseSensitivity( Qt::CaseInsensitive );
63 mSortModel->setDynamicSortFilter(
true );
64 mSortModel->sort( 0 );
66 mComboBox->setModel( mSortModel );
68 QHBoxLayout *l =
new QHBoxLayout();
69 l->setContentsMargins( 0, 0, 0, 0 );
70 l->addWidget( mComboBox );
72 QToolButton *refreshButton =
new QToolButton();
73 refreshButton->setAutoRaise(
true );
74 refreshButton->setToolTip( tr(
"Refresh schemas" ) );
76 l->addWidget( refreshButton );
81 connect( mComboBox,
static_cast < void ( QComboBox::* )(
int )
> ( &QComboBox::activated ),
this, &QgsDatabaseSchemaComboBox::indexChanged );
82 connect( mSortModel, &QAbstractItemModel::rowsInserted,
this, &QgsDatabaseSchemaComboBox::rowsChanged );
83 connect( mSortModel, &QAbstractItemModel::rowsRemoved,
this, &QgsDatabaseSchemaComboBox::rowsChanged );
91 if ( schema.isEmpty() )
94 mComboBox->setCurrentIndex( 0 );
96 mComboBox->setCurrentIndex( -1 );
102 QModelIndexList idx = mSortModel->match( mSortModel->index( 0, 0 ), Qt::DisplayRole, schema, 1, Qt::MatchFixedString | Qt::MatchCaseSensitive );
105 QModelIndex proxyIdx = idx.at( 0 );
106 if ( proxyIdx.isValid() )
108 mComboBox->setCurrentIndex( proxyIdx.row() );
113 mComboBox->setCurrentIndex( -1 );
119 if ( !provider.isEmpty() )
120 mProvider = provider;
124 if ( !connection.isEmpty() && !mProvider.isEmpty() )
128 mSortModel->setSourceModel( mModel );
133 mSortModel->setSourceModel(
nullptr );
136 oldModel->deleteLater();
152 const QModelIndex proxyIndex = mSortModel->index( mComboBox->currentIndex(), 0 );
153 if ( !proxyIndex.isValid() )
158 return mSortModel->data( proxyIndex, Qt::DisplayRole ).toString();
161 void QgsDatabaseSchemaComboBox::indexChanged(
int i )
167 void QgsDatabaseSchemaComboBox::rowsChanged()
169 if ( mComboBox->count() == 1 || ( mAllowEmpty && mComboBox->count() == 2 && mComboBox->currentIndex() == 1 ) )
174 else if ( mComboBox->count() == 0 )
182 QgsDatabaseSchemaComboBoxSortModel::QgsDatabaseSchemaComboBoxSortModel( QObject *parent )
183 : QSortFilterProxyModel( parent )
188 bool QgsDatabaseSchemaComboBoxSortModel::lessThan(
const QModelIndex &left,
const QModelIndex &right )
const
197 QString leftStr = sourceModel()->data( left ).toString();
198 QString rightStr = sourceModel()->data( right ).toString();
199 return QString::localeAwareCompare( leftStr, rightStr ) < 0;