Skip to content

Commit

Permalink
move the logic to save line edit state to QgsLineEdit (#36117)
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed May 1, 2020
1 parent bf04b0c commit dd560f4
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 72 deletions.
22 changes: 21 additions & 1 deletion python/gui/auto_generated/qgsfilterlineedit.sip.in
Expand Up @@ -212,14 +212,20 @@ Will select all text when this widget receives the focus.
.. versionadded:: 3.0
%End


virtual bool event( QEvent *event );

%Docstring
Reimplemented to enable/disable the clear action
depending on read-only status

.. versionadded:: 3.0.1
%End

bool hasStateStored() const;
%Docstring
Returns if a state is already saved

.. versionadded:: 3.14
%End

public slots:
Expand All @@ -231,6 +237,20 @@ Clears the widget and resets it to the null value.
.. seealso:: :py:func:`nullValue`

.. versionadded:: 3.0
%End

void storeState();
%Docstring
Stores the current state of the line edit (selection and cursor position)

.. versionadded:: 3.14
%End

void restoreState();
%Docstring
Restores the current state of the line edit (selection and cursor position)

.. versionadded:: 3.14
%End

signals:
Expand Down
25 changes: 3 additions & 22 deletions src/gui/qgsfeaturelistcombobox.cpp
Expand Up @@ -122,7 +122,7 @@ void QgsFeatureListComboBox::onItemSelected( const QModelIndex &index )

void QgsFeatureListComboBox::onCurrentIndexChanged( int i )
{
if ( !mHasStoredEditState )
if ( !mLineEdit->hasStateStored() )
mIsCurrentlyEdited = false;
QModelIndex modelIndex = mModel->index( i, 0, QModelIndex() );
mModel->setExtraIdentifierValues( mModel->data( modelIndex, QgsFeatureFilterModel::IdentifierValuesRole ).toList() );
Expand All @@ -143,17 +143,15 @@ void QgsFeatureListComboBox::storeLineEditState()
{
if ( mIsCurrentlyEdited )
{
mHasStoredEditState = true;
mLineEditState.store( mLineEdit );
mLineEdit->storeState( );
}
}

void QgsFeatureListComboBox::restoreLineEditState()
{
if ( mIsCurrentlyEdited )
{
mHasStoredEditState = false;
mLineEditState.restore( mLineEdit );
mLineEdit->restoreState( );
}
}

Expand Down Expand Up @@ -302,20 +300,3 @@ void QgsFeatureListComboBox::setFilterExpression( const QString &filterExpressio
{
mModel->setFilterExpression( filterExpression );
}

void QgsFeatureListComboBox::LineEditState::store( QLineEdit *lineEdit )
{
text = lineEdit->text();
selectionStart = lineEdit->selectionStart();
selectionLength = lineEdit->selectedText().length();
cursorPosition = lineEdit->cursorPosition();

}

void QgsFeatureListComboBox::LineEditState::restore( QLineEdit *lineEdit ) const
{
lineEdit->setText( text );
lineEdit->setCursorPosition( cursorPosition );
if ( selectionStart > -1 )
lineEdit->setSelection( selectionStart, selectionLength );
}
13 changes: 0 additions & 13 deletions src/gui/qgsfeaturelistcombobox.h
Expand Up @@ -248,24 +248,11 @@ class GUI_EXPORT QgsFeatureListComboBox : public QComboBox
void onDataChanged( const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles = QVector<int>() );

private:
struct LineEditState
{
void store( QLineEdit *lineEdit );
void restore( QLineEdit *lineEdit ) const;

QString text;
int selectionStart;
int selectionLength;
int cursorPosition;
};

QgsFeatureFilterModel *mModel = nullptr;
QCompleter *mCompleter = nullptr;
QgsFilterLineEdit *mLineEdit;
bool mPopupRequested = false;
bool mIsCurrentlyEdited = false;
bool mHasStoredEditState = false;
LineEditState mLineEditState;

friend class TestQgsFeatureListComboBox;
};
Expand Down
24 changes: 3 additions & 21 deletions src/gui/qgsfeaturepickerwidget.cpp
Expand Up @@ -140,7 +140,7 @@ void QgsFeaturePickerWidget::onItemSelected( const QModelIndex &index )

void QgsFeaturePickerWidget::onCurrentIndexChanged( int i )
{
if ( !mHasStoredEditState )
if ( !mLineEdit->hasStateStored() )
mIsCurrentlyEdited = false;

mPreviousButton->setEnabled( i > 0 );
Expand Down Expand Up @@ -168,17 +168,15 @@ void QgsFeaturePickerWidget::storeLineEditState()
{
if ( mIsCurrentlyEdited )
{
mHasStoredEditState = true;
mLineEditState.store( mLineEdit );
mLineEdit->storeState( );
}
}

void QgsFeaturePickerWidget::restoreLineEditState()
{
if ( mIsCurrentlyEdited )
{
mHasStoredEditState = false;
mLineEditState.restore( mLineEdit );
mLineEdit->restoreState( );
}
}

Expand Down Expand Up @@ -256,22 +254,6 @@ void QgsFeaturePickerWidget::setFilterExpression( const QString &filterExpressio
mModel->setFilterExpression( filterExpression );
}

void QgsFeaturePickerWidget::LineEditState::store( QLineEdit *lineEdit )
{
text = lineEdit->text();
selectionStart = lineEdit->selectionStart();
selectionLength = lineEdit->selectedText().length();
cursorPosition = lineEdit->cursorPosition();
}

void QgsFeaturePickerWidget::LineEditState::restore( QLineEdit *lineEdit ) const
{
lineEdit->setText( text );
lineEdit->setCursorPosition( cursorPosition );
if ( selectionStart > -1 )
lineEdit->setSelection( selectionStart, selectionLength );
}

bool QgsFeaturePickerWidget::fetchGeometry() const
{
return mModel->fetchGeometry();
Expand Down
14 changes: 0 additions & 14 deletions src/gui/qgsfeaturepickerwidget.h
Expand Up @@ -218,17 +218,6 @@ class GUI_EXPORT QgsFeaturePickerWidget : public QWidget
void browseFeatures( int direction );

private:
struct LineEditState
{
void store( QLineEdit *lineEdit );
void restore( QLineEdit *lineEdit ) const;

QString text;
int selectionStart;
int selectionLength;
int cursorPosition;
};

QComboBox *mComboBox;
QToolButton *mPreviousButton;
QToolButton *mNextButton;
Expand All @@ -238,10 +227,7 @@ class GUI_EXPORT QgsFeaturePickerWidget : public QWidget
bool mPopupRequested = false;
bool mIsCurrentlyEdited = false;
bool mHasStoredEditState = false;
LineEditState mLineEditState;
bool mShowBrowserButtons = false;

friend class TestQgsFeaturePickerWidget;
};


Expand Down
18 changes: 18 additions & 0 deletions src/gui/qgsfilterlineedit.cpp
Expand Up @@ -214,6 +214,24 @@ bool QgsFilterLineEdit::event( QEvent *event )
return QLineEdit::event( event );
}

void QgsFilterLineEdit::storeState()
{
mLineEditState.text = text();
mLineEditState.selectionStart = selectionStart();
mLineEditState.selectionLength = selectedText().length();
mLineEditState.cursorPosition = cursorPosition();
mLineEditState.hasStateStored = true;
}

void QgsFilterLineEdit::restoreState()
{
setText( mLineEditState.text );
setCursorPosition( mLineEditState.cursorPosition );
if ( mLineEditState.selectionStart > -1 )
setSelection( mLineEditState.selectionStart, mLineEditState.selectionLength );
mLineEditState.hasStateStored = false;
}

/// @cond PRIVATE
void QgsSpinBoxLineEdit::focusInEvent( QFocusEvent *e )
{
Expand Down
30 changes: 29 additions & 1 deletion src/gui/qgsfilterlineedit.h
Expand Up @@ -214,7 +214,6 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
*/
void setSelectOnFocus( bool selectOnFocus );


/**
* Reimplemented to enable/disable the clear action
* depending on read-only status
Expand All @@ -223,6 +222,12 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
*/
bool event( QEvent *event ) override;

/**
* Returns if a state is already saved
* \since QGIS 3.14
*/
bool hasStateStored() const {return mLineEditState.hasStateStored;}

public slots:

/**
Expand All @@ -232,6 +237,18 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
*/
virtual void clearValue();

/**
* Stores the current state of the line edit (selection and cursor position)
* \since QGIS 3.14
*/
void storeState();

/**
* Restores the current state of the line edit (selection and cursor position)
* \since QGIS 3.14
*/
void restoreState();

signals:

/**
Expand Down Expand Up @@ -273,6 +290,15 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
void updateClearIcon();

private:
struct LineEditState
{
bool hasStateStored = false;
QString text;
int selectionStart;
int selectionLength;
int cursorPosition;
};

QIcon mClearIcon;
QAction *mClearAction = nullptr;
QAction *mSearchAction = nullptr;
Expand All @@ -289,6 +315,8 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
bool mWaitingForMouseRelease = false;
bool mSelectOnFocus = false;

LineEditState mLineEditState;

QgsAnimatedIcon *mBusySpinnerAnimatedIcon = nullptr;

//! Returns TRUE if clear button should be shown
Expand Down

0 comments on commit dd560f4

Please sign in to comment.