Skip to content

Commit 09cc9e4

Browse files
committed
Use field expression widget in order by dialog
1 parent 9c9ff79 commit 09cc9e4

File tree

3 files changed

+40
-75
lines changed

3 files changed

+40
-75
lines changed

src/gui/qgsfieldexpressionwidget.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ class GUI_EXPORT QgsFieldExpressionWidget : public QWidget
7979
*/
8080
bool isValidExpression( QString *expressionError = nullptr ) const;
8181

82+
/**
83+
* If the content is not just a simple field this method will return true.
84+
*/
8285
bool isExpression() const;
86+
8387
/**
8488
* Return the current text that is set in the expression area
8589
*/

src/gui/qgsorderbydialog.cpp

Lines changed: 30 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "qgsorderbydialog.h"
1818

1919
#include "qgsexpressionbuilderdialog.h"
20+
#include "qgsfieldexpressionwidget.h"
2021

2122
#include <QTableWidget>
2223
#include <QCheckBox>
@@ -27,8 +28,6 @@ QgsOrderByDialog::QgsOrderByDialog( QgsVectorLayer* layer, QWidget* parent )
2728
, mLayer( layer )
2829
{
2930
setupUi( this );
30-
connect( mOrderByTableWidget, SIGNAL( cellDoubleClicked( int, int ) ), this, SLOT( onCellDoubleClicked( int, int ) ) );
31-
connect( mOrderByTableWidget, SIGNAL( cellChanged( int, int ) ), this, SLOT( onCellChanged( int, int ) ) );
3231

3332
mOrderByTableWidget->horizontalHeader()->setResizeMode( QHeaderView::Stretch );
3433
mOrderByTableWidget->horizontalHeader()->setResizeMode( 1, QHeaderView::Interactive );
@@ -45,28 +44,13 @@ void QgsOrderByDialog::setOrderBy( const QgsFeatureRequest::OrderBy& orderBy )
4544
int i = 0;
4645
Q_FOREACH ( const QgsFeatureRequest::OrderByClause& orderByClause, orderBy )
4746
{
48-
QTableWidgetItem* expressionItem = new QTableWidgetItem( orderByClause.expression().expression() );
49-
QCheckBox* ascCheckBox = new QCheckBox();
50-
ascCheckBox->setChecked( orderByClause.ascending() );
51-
QCheckBox* nullsFirstCheckBox = new QCheckBox();
52-
nullsFirstCheckBox->setChecked( orderByClause.nullsFirst() );
53-
54-
mOrderByTableWidget->setItem( i, 0, expressionItem );
55-
mOrderByTableWidget->setCellWidget( i, 1, ascCheckBox );
56-
mOrderByTableWidget->setCellWidget( i, 2, nullsFirstCheckBox );
47+
setRow( i, orderByClause );
5748

5849
++i;
5950
}
6051

6152
// Add an empty widget at the end
62-
QTableWidgetItem* expressionItem = new QTableWidgetItem( "" );
63-
QCheckBox* ascCheckBox = new QCheckBox();
64-
ascCheckBox->setChecked( true );
65-
QCheckBox* nullsFirstCheckBox = new QCheckBox();
66-
67-
mOrderByTableWidget->setItem( i, 0, expressionItem );
68-
mOrderByTableWidget->setCellWidget( i, 1, ascCheckBox );
69-
mOrderByTableWidget->setCellWidget( i, 2, nullsFirstCheckBox );
53+
setRow( i, QgsFeatureRequest::OrderByClause( "" ) );
7054
}
7155

7256
QgsFeatureRequest::OrderBy QgsOrderByDialog::orderBy()
@@ -75,7 +59,7 @@ QgsFeatureRequest::OrderBy QgsOrderByDialog::orderBy()
7559

7660
for ( int i = 0; i < mOrderByTableWidget->rowCount(); ++i )
7761
{
78-
QString expressionText = mOrderByTableWidget->item( i, 0 )->text();
62+
QString expressionText = static_cast<QgsFieldExpressionWidget*>( mOrderByTableWidget->cellWidget( i, 0 ) )->currentText();
7963

8064
if ( ! expressionText.isEmpty() )
8165
{
@@ -90,70 +74,43 @@ QgsFeatureRequest::OrderBy QgsOrderByDialog::orderBy()
9074
return orderBy;
9175
}
9276

93-
void QgsOrderByDialog::onCellDoubleClicked( int row, int column )
77+
void QgsOrderByDialog::onExpressionChanged( const QString& expression )
9478
{
95-
// Only act on first cell where the expression text is
96-
if ( 0 == column )
79+
// The sender() is the field widget which is the cell widget of the first column
80+
int row;
81+
for ( row = 0; row < mOrderByTableWidget->rowCount(); ++row )
9782
{
98-
QgsExpressionBuilderDialog dlg( mLayer );
99-
100-
dlg.setExpressionText( mOrderByTableWidget->item( row, column )->text() );
101-
102-
if ( dlg.exec() )
83+
if ( mOrderByTableWidget->cellWidget( row, 0 ) == sender() )
10384
{
104-
QString expressionText = dlg.expressionText();
105-
106-
mOrderByTableWidget->item( row, column )->setText( expressionText );
107-
108-
if ( row == mOrderByTableWidget->rowCount() - 1 )
109-
{
110-
// Add an empty widget at the end if the last row was edited
111-
mOrderByTableWidget->insertRow( mOrderByTableWidget->rowCount() );
112-
113-
QTableWidgetItem* expressionItem = new QTableWidgetItem( "" );
114-
QCheckBox* ascCheckBox = new QCheckBox();
115-
ascCheckBox->setChecked( true );
116-
QCheckBox* nullsFirstCheckBox = new QCheckBox();
117-
118-
mOrderByTableWidget->setItem( row + 1, 0, expressionItem );
119-
mOrderByTableWidget->setCellWidget( row + 1, 1, ascCheckBox );
120-
mOrderByTableWidget->setCellWidget( row + 1, 2, nullsFirstCheckBox );
121-
}
85+
break;
12286
}
12387
}
124-
}
12588

126-
void QgsOrderByDialog::onCellChanged( int row, int column )
127-
{
128-
// If the text was cleared
129-
if ( mOrderByTableWidget->item( row, column )->text().isEmpty() )
89+
if ( expression.isEmpty() && row != mOrderByTableWidget->rowCount() - 1 )
13090
{
131-
// If the first column (expression text) and not the last row was edited
132-
if ( 0 == column && row != mOrderByTableWidget->rowCount() - 1 )
133-
{
134-
{
135-
mOrderByTableWidget->removeRow( row );
136-
}
137-
}
91+
mOrderByTableWidget->removeRow( row );
13892
}
139-
else
93+
else if ( !expression.isEmpty() && row == mOrderByTableWidget->rowCount() - 1 )
14094
{
141-
// If it's the last row and an expression was added: add a new empty one
142-
if ( row == mOrderByTableWidget->rowCount() - 1 && !mOrderByTableWidget->item( row, column )->text().isEmpty() )
143-
{
144-
// Add an empty widget at the end if the last row was edited
145-
mOrderByTableWidget->insertRow( mOrderByTableWidget->rowCount() );
95+
mOrderByTableWidget->insertRow( mOrderByTableWidget->rowCount() );
96+
setRow( row + 1, QgsFeatureRequest::OrderByClause( "" ) );
97+
}
98+
}
14699

147-
QTableWidgetItem* expressionItem = new QTableWidgetItem( "" );
148-
QCheckBox* ascCheckBox = new QCheckBox();
149-
ascCheckBox->setChecked( true );
150-
QCheckBox* nullsFirstCheckBox = new QCheckBox();
100+
void QgsOrderByDialog::setRow( int row, const QgsFeatureRequest::OrderByClause& orderByClause )
101+
{
102+
QgsFieldExpressionWidget* fieldExpression = new QgsFieldExpressionWidget();
103+
fieldExpression->setLayer( mLayer );
104+
fieldExpression->setField( orderByClause.expression().expression() );
105+
connect( fieldExpression, SIGNAL( fieldChanged( QString ) ), this, SLOT( onExpressionChanged( QString ) ) );
106+
QCheckBox* ascCheckBox = new QCheckBox();
107+
ascCheckBox->setChecked( orderByClause.ascending() );
108+
QCheckBox* nullsFirstCheckBox = new QCheckBox();
109+
nullsFirstCheckBox->setChecked( orderByClause.nullsFirst() );
151110

152-
mOrderByTableWidget->setItem( row + 1, 0, expressionItem );
153-
mOrderByTableWidget->setCellWidget( row + 1, 1, ascCheckBox );
154-
mOrderByTableWidget->setCellWidget( row + 1, 2, nullsFirstCheckBox );
155-
}
156-
}
111+
mOrderByTableWidget->setCellWidget( row, 0, fieldExpression );
112+
mOrderByTableWidget->setCellWidget( row, 1, ascCheckBox );
113+
mOrderByTableWidget->setCellWidget( row, 2, nullsFirstCheckBox );
157114
}
158115

159116
bool QgsOrderByDialog::eventFilter( QObject* obj, QEvent* e )

src/gui/qgsorderbydialog.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,14 @@ class GUI_EXPORT QgsOrderByDialog : public QDialog, private Ui::OrderByDialogBas
5353
QgsFeatureRequest::OrderBy orderBy();
5454

5555
private slots:
56-
void onCellDoubleClicked( int row, int column );
57-
void onCellChanged( int row, int column );
56+
void onExpressionChanged( const QString& expression );
5857

5958
private:
59+
/**
60+
* Initialize a row with the given information
61+
*/
62+
void setRow( int row, const QgsFeatureRequest::OrderByClause& orderByClause );
63+
6064
QgsVectorLayer* mLayer;
6165

6266
bool eventFilter( QObject *obj, QEvent *e );

0 commit comments

Comments
 (0)