QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
qgsorderbydialog.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgosorderbydialog.cpp
3
4 ---------------------
5 begin : 20.12.2015
6 copyright : (C) 2015 by Matthias Kuhn
8 ***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
17#include "qgsorderbydialog.h"
18#include "moc_qgsorderbydialog.cpp"
19
22#include "qgsvectorlayer.h"
23
24#include <QTableWidget>
25#include <QKeyEvent>
26
28 : QDialog( parent )
29 , mLayer( layer )
30{
31 setupUi( this );
32
33 mOrderByTableWidget->horizontalHeader()->setSectionResizeMode( QHeaderView::Stretch );
34 mOrderByTableWidget->horizontalHeader()->setSectionResizeMode( 1, QHeaderView::ResizeToContents );
35 mOrderByTableWidget->horizontalHeader()->setSectionResizeMode( 2, QHeaderView::ResizeToContents );
36
37 mOrderByTableWidget->installEventFilter( this );
38
39 connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsOrderByDialog::showHelp );
40}
41
43{
44 mOrderByTableWidget->setRowCount( orderBy.length() + 1 );
45
46 int i = 0;
47 const auto constOrderBy = orderBy;
48 for ( const QgsFeatureRequest::OrderByClause &orderByClause : constOrderBy )
49 {
50 setRow( i, orderByClause );
51
52 ++i;
53 }
54
55 // Add an empty widget at the end
56 setRow( i, QgsFeatureRequest::OrderByClause( QString() ) );
57}
58
60{
62
63 for ( int i = 0; i < mOrderByTableWidget->rowCount(); ++i )
64 {
65 QString expressionText = static_cast<QgsFieldExpressionWidget *>( mOrderByTableWidget->cellWidget( i, 0 ) )->currentText();
66 const bool isExpression = static_cast<QgsFieldExpressionWidget *>( mOrderByTableWidget->cellWidget( i, 0 ) )->isExpression();
67
68 if ( ! expressionText.isEmpty() )
69 {
70 bool asc = true;
71 const int ascIndex = static_cast<QComboBox *>( mOrderByTableWidget->cellWidget( i, 1 ) )->currentIndex();
72 if ( ascIndex == 1 )
73 asc = false;
74
75 bool nullsFirst = false;
76 const int nullsFirstIndex = static_cast<QComboBox *>( mOrderByTableWidget->cellWidget( i, 2 ) )->currentIndex();
77 if ( nullsFirstIndex == 1 )
78 nullsFirst = true;
79
80 if ( !isExpression )
81 expressionText = QgsExpression::quotedColumnRef( expressionText );
82
83 const QgsFeatureRequest::OrderByClause orderByClause( expressionText, asc, nullsFirst );
84
85 orderBy << orderByClause;
86 }
87 }
88
89 return orderBy;
90}
91
92void QgsOrderByDialog::onExpressionChanged( const QString &expression )
93{
94 // The sender() is the field widget which is the cell widget of the first column
95 int row;
96 for ( row = 0; row < mOrderByTableWidget->rowCount(); ++row )
97 {
98 if ( mOrderByTableWidget->cellWidget( row, 0 ) == sender() )
99 {
100 break;
101 }
102 }
103
104 if ( expression.isEmpty() && row != mOrderByTableWidget->rowCount() - 1 )
105 {
106 mOrderByTableWidget->removeRow( row );
107 }
108 else if ( !expression.isEmpty() && row == mOrderByTableWidget->rowCount() - 1 )
109 {
110 mOrderByTableWidget->insertRow( mOrderByTableWidget->rowCount() );
111 setRow( row + 1, QgsFeatureRequest::OrderByClause( QString() ) );
112 }
113}
114
115void QgsOrderByDialog::setRow( int row, const QgsFeatureRequest::OrderByClause &orderByClause )
116{
117 QgsFieldExpressionWidget *fieldExpression = new QgsFieldExpressionWidget();
118 fieldExpression->setLayer( mLayer );
119 fieldExpression->setField( orderByClause.expression().expression() );
120 connect( fieldExpression, static_cast < void ( QgsFieldExpressionWidget::* )( const QString & ) >( &QgsFieldExpressionWidget::fieldChanged ), this, &QgsOrderByDialog::onExpressionChanged );
121
122 QComboBox *ascComboBox = new QComboBox();
123 ascComboBox->addItem( tr( "Ascending" ) );
124 ascComboBox->addItem( tr( "Descending" ) );
125 ascComboBox->setCurrentIndex( orderByClause.ascending() ? 0 : 1 );
126
127 QComboBox *nullsFirstComboBox = new QComboBox();
128 nullsFirstComboBox->addItem( tr( "NULLs Last" ) );
129 nullsFirstComboBox->addItem( tr( "NULLs First" ) );
130 nullsFirstComboBox->setCurrentIndex( orderByClause.nullsFirst() ? 1 : 0 );
131
132 mOrderByTableWidget->setCellWidget( row, 0, fieldExpression );
133 mOrderByTableWidget->setCellWidget( row, 1, ascComboBox );
134 mOrderByTableWidget->setCellWidget( row, 2, nullsFirstComboBox );
135}
136
137bool QgsOrderByDialog::eventFilter( QObject *obj, QEvent *e )
138{
139 Q_UNUSED( obj )
140 Q_ASSERT( obj == mOrderByTableWidget );
141
142 if ( e->type() == QEvent::KeyPress )
143 {
144 QKeyEvent *keyEvent = static_cast<QKeyEvent *>( e );
145
146 if ( keyEvent->key() == Qt::Key_Delete )
147 {
148 if ( mOrderByTableWidget->currentRow() != mOrderByTableWidget->rowCount() - 1 )
149 mOrderByTableWidget->removeRow( mOrderByTableWidget->currentRow() );
150 return true;
151 }
152 }
153
154 return QDialog::eventFilter( obj, e );
155}
156
157void QgsOrderByDialog::showHelp()
158{
159 QgsHelp::openHelp( QStringLiteral( "working_with_vector/vector_properties.html#layer-rendering" ) );
160}
QString expression() const
Returns the original, unmodified expression string.
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes)
The OrderByClause class represents an order by clause for a QgsFeatureRequest.
QgsExpression expression() const
The expression.
bool ascending() const
Order ascending.
bool nullsFirst() const
Set if NULLS should be returned first.
Represents a list of OrderByClauses, with the most important first and the least important last.
The QgsFieldExpressionWidget class creates a widget to choose fields and edit expressions It contains...
void setField(const QString &fieldName)
sets the current field or expression in the widget
void setLayer(QgsMapLayer *layer)
Sets the layer used to display the fields and expression.
void fieldChanged(const QString &fieldName)
Emitted when the currently selected field changes.
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition qgshelp.cpp:39
bool eventFilter(QObject *obj, QEvent *e) override
void setOrderBy(const QgsFeatureRequest::OrderBy &orderBy)
Set the order by to manage.
QgsOrderByDialog(QgsVectorLayer *layer, QWidget *parent=nullptr)
Create a new order by dialog.
QgsFeatureRequest::OrderBy orderBy()
Gets the order by defined in the dialog.
Represents a vector layer which manages a vector based data sets.