18 #include <QMessageBox>
20 #include <QPushButton>
28 QWidget *parent, Qt::WindowFlags fl )
29 : QDialog( parent, fl )
30 , mPreviousFieldRow( -1 )
36 restoreGeometry( settings.value(
"/Windows/QueryBuilder/geometry" ).toByteArray() );
38 QPushButton *pbn =
new QPushButton(
tr(
"&Test" ) );
39 buttonBox->addButton( pbn, QDialogButtonBox::ActionRole );
40 connect( pbn, SIGNAL( clicked() ),
this, SLOT(
test() ) );
42 pbn =
new QPushButton(
tr(
"&Clear" ) );
43 buttonBox->addButton( pbn, QDialogButtonBox::ActionRole );
44 connect( pbn, SIGNAL( clicked() ),
this, SLOT(
clear() ) );
50 mUseUnfilteredLayer->setDisabled( mLayer->
subsetString().isEmpty() );
52 lblDataUri->setText(
tr(
"Set provider filter on %1" ).arg( layer->
name() ) );
53 txtSQL->setText( mOrigSubsetString );
61 settings.setValue(
"/Windows/QueryBuilder/geometry", saveGeometry() );
70 void QgsQueryBuilder::populateFields()
73 for (
int idx = 0; idx < fields.
count(); ++idx )
75 QStandardItem *myItem =
new QStandardItem( fields[idx].name() );
76 myItem->setData( idx );
77 myItem->setEditable(
false );
78 mModelFields->insertRow( mModelFields->rowCount(), myItem );
82 setupLstFieldsModel();
85 void QgsQueryBuilder::setupLstFieldsModel()
87 lstFields->setModel( mModelFields );
90 void QgsQueryBuilder::setupGuiViews()
93 mModelFields =
new QStandardItemModel();
94 mModelValues =
new QStandardItemModel();
96 lstFields->setViewMode( QListView::ListMode );
97 lstValues->setViewMode( QListView::ListMode );
98 lstFields->setSelectionBehavior( QAbstractItemView::SelectRows );
99 lstValues->setSelectionBehavior( QAbstractItemView::SelectRows );
101 lstFields->setUniformItemSizes(
true );
102 lstValues->setUniformItemSizes(
true );
104 lstFields->setAlternatingRowColors(
true );
105 lstValues->setAlternatingRowColors(
true );
108 void QgsQueryBuilder::fillValues(
int idx,
int limit )
111 mModelValues->clear();
114 QList<QVariant> values;
118 QString nullValue = settings.value(
"qgis/nullValue",
"NULL" ).toString();
120 QgsDebugMsg( QString(
"nullValue: %1" ).arg( nullValue ) );
122 for (
int i = 0; i < values.size(); i++ )
127 else if ( values[i].type() == QVariant::Date && mLayer->
providerType() ==
"ogr" && mLayer->
storageType() ==
"ESRI Shapefile" )
128 value = values[i].toDate().toString(
"yyyy/MM/dd" );
130 value = values[i].toString();
132 QStandardItem *myItem =
new QStandardItem( value );
133 myItem->setEditable(
false );
134 myItem->setData( values[i], Qt::UserRole + 1 );
135 mModelValues->insertRow( mModelValues->rowCount(), myItem );
136 QgsDebugMsg( QString(
"Value is null: %1\nvalue: %2" ).arg( values[i].
isNull() ).arg( values[i].
isNull() ? nullValue : values[i].toString() ) );
142 lstValues->setCursor( Qt::WaitCursor );
145 if ( mUseUnfilteredLayer->isChecked() && !prevSubsetString.isEmpty() )
151 QStandardItemModel *tmp =
new QStandardItemModel();
152 lstValues->setModel( tmp );
154 fillValues( mModelFields->data( lstFields->currentIndex(), Qt::UserRole + 1 ).toInt(), 25 );
155 lstValues->setModel( mModelValues );
164 lstValues->setCursor( Qt::ArrowCursor );
169 lstValues->setCursor( Qt::WaitCursor );
172 if ( mUseUnfilteredLayer->isChecked() && !prevSubsetString.isEmpty() )
178 QStandardItemModel *tmp =
new QStandardItemModel();
179 lstValues->setModel( tmp );
181 fillValues( mModelFields->data( lstFields->currentIndex(), Qt::UserRole + 1 ).toInt(), -1 );
182 lstValues->setModel( mModelValues );
191 lstValues->setCursor( Qt::ArrowCursor );
202 mUseUnfilteredLayer->setDisabled( mLayer->
subsetString().isEmpty() );
204 QMessageBox::information(
this,
205 tr(
"Query Result" ),
206 tr(
"The where clause returned %n row(s).",
"returned test rows", mLayer->
featureCount() ) );
210 QMessageBox::warning(
this,
211 tr(
"Query Failed" ),
212 tr(
"An error occurred when executing the query." )
218 QMessageBox::warning(
this,
219 tr(
"Query Failed" ),
220 tr(
"An error occurred when executing the query." ) );
231 QMessageBox::warning(
this,
232 tr(
"Query Failed" ),
233 tr(
"An error occurred when executing the query." )
239 QMessageBox::warning(
this,
tr(
"Error in Query" ),
tr(
"The subset string could not be set" ) );
258 txtSQL->insertText(
" = " );
264 txtSQL->insertText(
" < " );
270 txtSQL->insertText(
" > " );
276 txtSQL->insertText(
"%" );
282 txtSQL->insertText(
" IN " );
288 txtSQL->insertText(
" NOT IN " );
294 txtSQL->insertText(
" LIKE " );
300 return txtSQL->text();
305 txtSQL->setText( sqlStatement );
310 if ( mPreviousFieldRow != index.row() )
312 mPreviousFieldRow = index.row();
314 btnSampleValues->setEnabled(
true );
315 btnGetAllValues->setEnabled(
true );
317 mModelValues->clear();
323 txtSQL->insertText(
"\"" + mLayer->
pendingFields()[ mModelFields->data( index, Qt::UserRole+1 ).toInt()].name() +
"\"" );
329 QVariant value = mModelValues->data( index, Qt::UserRole + 1 );
330 if ( value.isNull() )
331 txtSQL->insertText(
"NULL" );
332 else if ( value.type() == QVariant::Date && mLayer->
providerType() ==
"ogr" && mLayer->
storageType() ==
"ESRI Shapefile" )
333 txtSQL->insertText(
"'" + value.toDate().toString(
"yyyy/MM/dd" ) +
"'" );
334 else if ( value.type() == QVariant::Int || value.type() == QVariant::Double || value.type() == QVariant::LongLong )
335 txtSQL->insertText( value.toString() );
337 txtSQL->insertText(
"'" + value.toString().replace(
"'",
"''" ) +
"'" );
344 txtSQL->insertText(
" <= " );
350 txtSQL->insertText(
" >= " );
356 txtSQL->insertText(
" != " );
362 txtSQL->insertText(
" AND " );
368 txtSQL->insertText(
" NOT " );
374 txtSQL->insertText(
" OR " );
382 mUseUnfilteredLayer->setDisabled(
true );
387 txtSQL->insertText(
" ILIKE " );
393 lblDataUri->setText( uri );