Skip to content

Commit

Permalink
add browsing buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Apr 30, 2020
1 parent 4c2e552 commit 3fd45ec
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 4 deletions.
17 changes: 17 additions & 0 deletions python/gui/auto_generated/qgsfeaturepickerwidget.sip.in
Expand Up @@ -10,6 +10,7 @@




class QgsFeaturePickerWidget : QWidget
{
%Docstring
Expand Down Expand Up @@ -111,6 +112,17 @@ Returns the feature request fetch limit
Defines the feature request fetch limit
%End

bool showBrowserButtons() const;
%Docstring
Returns if the browsing buttons are shown
%End

void setShowBrowserButtons( bool showBrowserButtons );
%Docstring
Defines if the browsing buttons are shown
%End


QModelIndex currentModelIndex() const;
%Docstring
The index of the currently selected item.
Expand Down Expand Up @@ -164,6 +176,11 @@ Emitted when the fetching of the geometry changes
void fetchLimitChanged();
%Docstring
Emitted when the fetching limit for the feature request changes
%End

void showBrowserButtonsChanged();
%Docstring
Emitted when showing the browser buttons changes
%End

};
Expand Down
5 changes: 3 additions & 2 deletions src/gui/qgsexpressionpreviewwidget.cpp
Expand Up @@ -27,15 +27,16 @@ QgsExpressionPreviewWidget::QgsExpressionPreviewWidget( QWidget *parent )
{
setupUi( this );
mPreviewLabel->clear();
mFeaturePickerWidget->setShowBrowserButtons( true );

connect( mFeatureChooserWidget, &QgsFeaturePickerWidget::featureChanged, this, &QgsExpressionPreviewWidget::setCurrentFeature );
connect( mFeaturePickerWidget, &QgsFeaturePickerWidget::featureChanged, this, &QgsExpressionPreviewWidget::setCurrentFeature );
connect( mPreviewLabel, &QLabel::linkActivated, this, &QgsExpressionPreviewWidget::linkActivated );
}

void QgsExpressionPreviewWidget::setLayer( QgsVectorLayer *layer )
{
mLayer = layer;
mFeatureChooserWidget->setLayer( layer );
mFeaturePickerWidget->setLayer( layer );
}

void QgsExpressionPreviewWidget::setExpressionText( const QString &expression )
Expand Down
45 changes: 44 additions & 1 deletion src/gui/qgsfeaturepickerwidget.cpp
Expand Up @@ -14,6 +14,7 @@
***************************************************************************/

#include <QHBoxLayout>
#include <QToolButton>
#include <QKeyEvent>

#include "qgsfeaturepickerwidget.h"
Expand All @@ -29,6 +30,19 @@ QgsFeaturePickerWidget::QgsFeaturePickerWidget( QWidget *parent )
mComboBox = new QComboBox( this );
mComboBox->setEditable( true );
layout->addWidget( mComboBox );

mPreviousButton = new QToolButton( this );
mPreviousButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionArrowLeft.svg" ) ) );
mPreviousButton->setEnabled( false );
mPreviousButton->setVisible( mShowBrowserButtons );
layout->addWidget( mPreviousButton );

mNextButton = new QToolButton( this );
mNextButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionArrowRight.svg" ) ) );
mNextButton->setEnabled( false );
mNextButton->setVisible( mShowBrowserButtons );
layout->addWidget( mNextButton );

setLayout( layout );

mCompleter->setCaseSensitivity( Qt::CaseInsensitive );
Expand All @@ -54,6 +68,9 @@ QgsFeaturePickerWidget::QgsFeaturePickerWidget( QWidget *parent )

connect( mComboBox, static_cast<void( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsFeaturePickerWidget::onCurrentIndexChanged );

connect( mPreviousButton, &QToolButton::clicked, this, [ = ]() {browseFeatures( -1 );} );
connect( mNextButton, &QToolButton::clicked, this, [ = ]() {browseFeatures( 1 );} );

mLineEdit = new QgsFilterLineEdit( nullptr, QgsApplication::nullRepresentation() );
mLineEdit->setSelectOnFocus( true );
mLineEdit->setShowClearButton( allowNull() );
Expand All @@ -64,7 +81,6 @@ QgsFeaturePickerWidget::QgsFeaturePickerWidget( QWidget *parent )

connect( mLineEdit, &QgsFilterLineEdit::textEdited, this, &QgsFeaturePickerWidget::onCurrentTextChanged );

setToolTip( tr( "Just start typing what you are looking for." ) );
}

QgsVectorLayer *QgsFeaturePickerWidget::layer() const
Expand Down Expand Up @@ -126,8 +142,13 @@ void QgsFeaturePickerWidget::onCurrentIndexChanged( int i )
{
if ( !mHasStoredEditState )
mIsCurrentlyEdited = false;

mPreviousButton->setEnabled( i > 0 );
mNextButton->setEnabled( i < mComboBox->model()->rowCount() - 1 );

if ( i < 0 )
return;

QModelIndex modelIndex = mModel->index( i, 0, QModelIndex() );
mModel->setFeature( mModel->data( modelIndex, QgsFeaturePickerModel::FeatureIdRole ).value<QgsFeatureId>() );
mLineEdit->setText( mModel->data( modelIndex, QgsFeaturePickerModel::ValueRole ).toString() );
Expand Down Expand Up @@ -187,6 +208,12 @@ void QgsFeaturePickerWidget::onDataChanged( const QModelIndex &topLeft, const QM
}
}

void QgsFeaturePickerWidget::browseFeatures( int direction )
{
int newIndex = std::min( std::max( 0, mComboBox->currentIndex() + direction ), mComboBox->model()->rowCount() - 1 );
mComboBox->setCurrentIndex( newIndex );
}

QModelIndex QgsFeaturePickerWidget::currentModelIndex() const
{
return mModel->index( mModel->extraIdentifierValueIndex(), 0, QModelIndex() );
Expand Down Expand Up @@ -264,3 +291,19 @@ void QgsFeaturePickerWidget::setFetchLimit( int fetchLimit )
{
mModel->setFetchLimit( fetchLimit );
}

bool QgsFeaturePickerWidget::showBrowserButtons() const
{
return mShowBrowserButtons;
}

void QgsFeaturePickerWidget::setShowBrowserButtons( bool showBrowserButtons )
{
if ( showBrowserButtons == mShowBrowserButtons )
return;

mShowBrowserButtons = showBrowserButtons;
mPreviousButton->setVisible( mShowBrowserButtons );
mNextButton->setVisible( mShowBrowserButtons );
emit showBrowserButtonsChanged();
}
21 changes: 21 additions & 0 deletions src/gui/qgsfeaturepickerwidget.h
Expand Up @@ -24,6 +24,8 @@
#include "qgsfeaturerequest.h"
#include "qgis_gui.h"

class QToolButton;

class QgsVectorLayer;
class QgsFeaturePickerModel;
class QgsAnimatedIcon;
Expand All @@ -49,6 +51,7 @@ class GUI_EXPORT QgsFeaturePickerWidget : public QWidget
Q_PROPERTY( bool allowNull READ allowNull WRITE setAllowNull NOTIFY allowNullChanged )
Q_PROPERTY( bool fetchGeometry READ fetchGeometry WRITE setFetchGeometry NOTIFY fetchGeometryChanged )
Q_PROPERTY( int fetchLimit READ fetchLimit WRITE setFetchLimit NOTIFY fetchLimitChanged )
Q_PROPERTY( bool showBrowserButtons READ showBrowserButtons WRITE setShowBrowserButtons NOTIFY showBrowserButtonsChanged )

public:

Expand Down Expand Up @@ -137,6 +140,17 @@ class GUI_EXPORT QgsFeaturePickerWidget : public QWidget
*/
void setFetchLimit( int fetchLimit );

/**
* Returns if the browsing buttons are shown
*/
bool showBrowserButtons() const;

/**
* Defines if the browsing buttons are shown
*/
void setShowBrowserButtons( bool showBrowserButtons );


/**
* The index of the currently selected item.
*/
Expand Down Expand Up @@ -188,6 +202,9 @@ class GUI_EXPORT QgsFeaturePickerWidget : public QWidget
*/
void fetchLimitChanged();

//! Emitted when showing the browser buttons changes
void showBrowserButtonsChanged();

private slots:
void onCurrentTextChanged( const QString &text );
void onFilterUpdateCompleted();
Expand All @@ -198,6 +215,7 @@ class GUI_EXPORT QgsFeaturePickerWidget : public QWidget
void storeLineEditState();
void restoreLineEditState();
void onDataChanged( const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles = QVector<int>() );
void browseFeatures( int direction );

private:
struct LineEditState
Expand All @@ -212,13 +230,16 @@ class GUI_EXPORT QgsFeaturePickerWidget : public QWidget
};

QComboBox *mComboBox;
QToolButton *mPreviousButton;
QToolButton *mNextButton;
QgsFeaturePickerModel *mModel = nullptr;
QCompleter *mCompleter = nullptr;
QgsFilterLineEdit *mLineEdit;
bool mPopupRequested = false;
bool mIsCurrentlyEdited = false;
bool mHasStoredEditState = false;
LineEditState mLineEditState;
bool mShowBrowserButtons = false;

friend class TestQgsFeaturePickerWidget;
};
Expand Down
2 changes: 1 addition & 1 deletion src/ui/qgsexpressionpreviewbase.ui
Expand Up @@ -68,7 +68,7 @@
</widget>
</item>
<item>
<widget class="QgsFeaturePickerWidget" name="mFeatureChooserWidget"/>
<widget class="QgsFeaturePickerWidget" name="mFeaturePickerWidget"/>
</item>
</layout>
</item>
Expand Down

0 comments on commit 3fd45ec

Please sign in to comment.