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