QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
21#include "qgsvectorlayer.h"
22
23#include <QKeyEvent>
24#include <QTableWidget>
25
26#include "moc_qgsorderbydialog.cpp"
27
29 : QDialog( parent )
30 , mLayer( layer )
31{
32 setupUi( this );
33
34 mOrderByTableWidget->horizontalHeader()->setSectionResizeMode( QHeaderView::Stretch );
35 mOrderByTableWidget->horizontalHeader()->setSectionResizeMode( 1, QHeaderView::ResizeToContents );
36 mOrderByTableWidget->horizontalHeader()->setSectionResizeMode( 2, QHeaderView::ResizeToContents );
37
38 mOrderByTableWidget->installEventFilter( this );
39
40 connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsOrderByDialog::showHelp );
41}
42
44{
45 mOrderByTableWidget->setRowCount( orderBy.length() + 1 );
46
47 int i = 0;
48 const auto constOrderBy = orderBy;
49 for ( const QgsFeatureRequest::OrderByClause &orderByClause : constOrderBy )
50 {
51 setRow( i, orderByClause );
52
53 ++i;
54 }
55
56 // Add an empty widget at the end
57 setRow( i, QgsFeatureRequest::OrderByClause( QString() ) );
58}
59
61{
63
64 for ( int i = 0; i < mOrderByTableWidget->rowCount(); ++i )
65 {
66 QString expressionText = static_cast<QgsFieldExpressionWidget *>( mOrderByTableWidget->cellWidget( i, 0 ) )->currentText();
67 const bool isExpression = static_cast<QgsFieldExpressionWidget *>( mOrderByTableWidget->cellWidget( i, 0 ) )->isExpression();
68
69 if ( !expressionText.isEmpty() )
70 {
71 bool asc = true;
72 const int ascIndex = static_cast<QComboBox *>( mOrderByTableWidget->cellWidget( i, 1 ) )->currentIndex();
73 if ( ascIndex == 1 )
74 asc = false;
75
76 bool nullsFirst = false;
77 const int nullsFirstIndex = static_cast<QComboBox *>( mOrderByTableWidget->cellWidget( i, 2 ) )->currentIndex();
78 if ( nullsFirstIndex == 1 )
79 nullsFirst = true;
80
81 if ( !isExpression )
82 expressionText = QgsExpression::quotedColumnRef( expressionText );
83
84 const QgsFeatureRequest::OrderByClause orderByClause( expressionText, asc, nullsFirst );
85
86 orderBy << orderByClause;
87 }
88 }
89
90 return orderBy;
91}
92
93void QgsOrderByDialog::onExpressionChanged( const QString &expression )
94{
95 // The sender() is the field widget which is the cell widget of the first column
96 int row;
97 for ( row = 0; row < mOrderByTableWidget->rowCount(); ++row )
98 {
99 if ( mOrderByTableWidget->cellWidget( row, 0 ) == sender() )
100 {
101 break;
102 }
103 }
104
105 if ( expression.isEmpty() && row != mOrderByTableWidget->rowCount() - 1 )
106 {
107 mOrderByTableWidget->removeRow( row );
108 }
109 else if ( !expression.isEmpty() && row == mOrderByTableWidget->rowCount() - 1 )
110 {
111 mOrderByTableWidget->insertRow( mOrderByTableWidget->rowCount() );
112 setRow( row + 1, QgsFeatureRequest::OrderByClause( QString() ) );
113 }
114}
115
116void QgsOrderByDialog::setRow( int row, const QgsFeatureRequest::OrderByClause &orderByClause )
117{
118 QgsFieldExpressionWidget *fieldExpression = new QgsFieldExpressionWidget();
119 fieldExpression->setLayer( mLayer );
120 fieldExpression->setField( orderByClause.expression().expression() );
121 connect( fieldExpression, static_cast<void ( QgsFieldExpressionWidget::* )( const QString & )>( &QgsFieldExpressionWidget::fieldChanged ), this, &QgsOrderByDialog::onExpressionChanged );
122
123 QComboBox *ascComboBox = new QComboBox();
124 ascComboBox->addItem( tr( "Ascending" ) );
125 ascComboBox->addItem( tr( "Descending" ) );
126 ascComboBox->setCurrentIndex( orderByClause.ascending() ? 0 : 1 );
127
128 QComboBox *nullsFirstComboBox = new QComboBox();
129 nullsFirstComboBox->addItem( tr( "NULLs Last" ) );
130 nullsFirstComboBox->addItem( tr( "NULLs First" ) );
131 nullsFirstComboBox->setCurrentIndex( orderByClause.nullsFirst() ? 1 : 0 );
132
133 mOrderByTableWidget->setCellWidget( row, 0, fieldExpression );
134 mOrderByTableWidget->setCellWidget( row, 1, ascComboBox );
135 mOrderByTableWidget->setCellWidget( row, 2, nullsFirstComboBox );
136}
137
138bool QgsOrderByDialog::eventFilter( QObject *obj, QEvent *e )
139{
140 Q_UNUSED( obj )
141 Q_ASSERT( obj == mOrderByTableWidget );
142
143 if ( e->type() == QEvent::KeyPress )
144 {
145 QKeyEvent *keyEvent = static_cast<QKeyEvent *>( e );
146
147 if ( keyEvent->key() == Qt::Key_Delete )
148 {
149 if ( mOrderByTableWidget->currentRow() != mOrderByTableWidget->rowCount() - 1 )
150 mOrderByTableWidget->removeRow( mOrderByTableWidget->currentRow() );
151 return true;
152 }
153 }
154
155 return QDialog::eventFilter( obj, e );
156}
157
158void QgsOrderByDialog::showHelp()
159{
160 QgsHelp::openHelp( QStringLiteral( "working_with_vector/vector_properties.html#layer-rendering" ) );
161}
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.
A widget for selection of layer fields or expression creation.
void setField(const QString &fieldName)
sets the current field or expression in the widget
bool isExpression() const
If the content is not just a simple field this method will return true.
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.
QString currentText() const
Returns the current text that is set in the expression area.
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition qgshelp.cpp:38
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 dataset.