Skip to content
Permalink
Browse files

move the logic to save line edit state to QgsLineEdit (#36117)

  • Loading branch information
3nids committed May 1, 2020
1 parent bf04b0c commit dd560f49b31230e472885fe411b616b4afc72586
@@ -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:
@@ -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:
@@ -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() );
@@ -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( );
}
}

@@ -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 );
}
@@ -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;
};
@@ -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 );
@@ -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( );
}
}

@@ -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();
@@ -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;
@@ -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;
};


@@ -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 )
{
@@ -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
@@ -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:

/**
@@ -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:

/**
@@ -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;
@@ -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

0 comments on commit dd560f4

Please sign in to comment.
You can’t perform that action at this time.