31#include "moc_qgsfilterlineedit.cpp"
33using namespace Qt::StringLiterals;
40 const int iconSize = std::floor( std::max(
Qgis::UI_SCALE_FACTOR * fontMetrics().height() * 0.75, 16.0 ) );
42 mClearIcon.addPixmap(
QgsApplication::getThemeIcon(
"/mIconClearText.svg" ).pixmap( QSize( iconSize, iconSize ) ), QIcon::Normal, QIcon::On );
43 mClearIcon.addPixmap(
QgsApplication::getThemeIcon(
"/mIconClearTextHover.svg" ).pixmap( QSize( iconSize, iconSize ) ), QIcon::Selected, QIcon::On );
45 connect(
this, &QLineEdit::textChanged,
this, &QgsFilterLineEdit::onTextChanged );
50 mClearButtonVisible = visible;
56 if ( visible && !mSearchAction )
59 mSearchAction =
new QAction( searchIcon, QString(),
this );
60 mSearchAction->setCheckable(
false );
61 addAction( mSearchAction, QLineEdit::LeadingPosition );
63 else if ( !visible && mSearchAction )
65 mSearchAction->deleteLater();
66 mSearchAction =
nullptr;
79void QgsFilterLineEdit::updateClearIcon()
81 const bool showClear = shouldShowClear();
82 if ( showClear && !mClearAction )
84 mClearAction =
new QAction( mClearIcon, QString(),
this );
85 mClearAction->setCheckable(
false );
86 addAction( mClearAction, QLineEdit::TrailingPosition );
89 else if ( !showClear && mClearAction )
96 removeAction( mClearAction );
97 mClearAction->deleteLater();
98 mClearAction =
nullptr;
104 QLineEdit::focusInEvent( e );
105 if ( e->reason() == Qt::MouseFocusReason && (
isNull() || mSelectOnFocus ) )
107 mWaitingForMouseRelease =
true;
113 QLineEdit::mouseReleaseEvent( e );
114 if ( mWaitingForMouseRelease )
116 mWaitingForMouseRelease =
false;
123 switch ( mClearMode )
126 setText( mNullValue );
131 setText( mDefaultValue );
139void QgsFilterLineEdit::onTextChanged(
const QString &text )
145 setStyleSheet( u
"QLineEdit { font: italic; color: gray; } %1"_s.arg( mStyleSheet ) );
150 setStyleSheet( mStyleSheet );
155void QgsFilterLineEdit::updateBusySpinner()
157 if ( !mBusySpinnerAction )
159 mBusySpinnerAction = addAction( mBusySpinnerAnimatedIcon->icon(), QLineEdit::TrailingPosition );
161 mBusySpinnerAction->setIcon( mBusySpinnerAnimatedIcon->icon() );
166 return mSelectOnFocus;
190 if ( !mBusySpinnerAnimatedIcon )
193 mBusySpinnerAnimatedIcon->connectFrameChanged(
this, &QgsFilterLineEdit::updateBusySpinner );
197 mBusySpinnerAnimatedIcon->disconnectFrameChanged(
this, &QgsFilterLineEdit::updateBusySpinner );
198 removeAction( mBusySpinnerAction );
199 mBusySpinnerAction =
nullptr;
206bool QgsFilterLineEdit::shouldShowClear()
const
208 if ( !isEnabled() || isReadOnly() || !mClearButtonVisible )
211 switch ( mClearMode )
217 return value() != mDefaultValue;
224 if (
event->type() == QEvent::ReadOnlyChange ||
event->type() == QEvent::EnabledChange )
227 return QLineEdit::event(
event );
232 mLineEditState.text = text();
233 mLineEditState.selectionStart = selectionStart();
234 mLineEditState.selectionLength = selectedText().length();
235 mLineEditState.cursorPosition = cursorPosition();
236 mLineEditState.hasStateStored =
true;
241 setText( mLineEditState.text );
242 setCursorPosition( mLineEditState.cursorPosition );
243 if ( mLineEditState.selectionStart > -1 )
244 setSelection( mLineEditState.selectionStart, mLineEditState.selectionLength );
245 mLineEditState.hasStateStored =
false;
249void QgsSpinBoxLineEdit::focusInEvent( QFocusEvent *e )
static const double UI_SCALE_FACTOR
UI scaling factor.
Animated icon is keeping an animation running if there are listeners connected to frameChanged.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static QString iconPath(const QString &iconFile)
Returns path to the desired icon file.
void focusInEvent(QFocusEvent *e) override
void restoreState()
Restores the current state of the line edit (selection and cursor position).
void storeState()
Stores the current state of the line edit (selection and cursor position).
void selectOnFocusChanged()
Will select all text when this widget receives the focus.
void showSpinnerChanged()
Show a spinner icon.
virtual void clearValue()
Clears the widget and resets it to the null value.
@ ClearToNull
Reset value to null.
@ ClearToDefault
Reset value to default value (see defaultValue() ).
bool isNull() const
Determine if the current text represents null.
bool selectOnFocus() const
Will select all text when this widget receives the focus.
void setShowSearchIcon(bool visible)
Define if a search icon shall be shown on the left of the image when no text is entered.
void setShowClearButton(bool visible)
Sets whether the widget's clear button is visible.
QgsFilterLineEdit(QWidget *parent=nullptr, const QString &nullValue=QString())
Constructor for QgsFilterLineEdit.
void setSelectOnFocus(bool selectOnFocus)
Will select all text when this widget receives the focus.
void mouseReleaseEvent(QMouseEvent *e) override
void cleared()
Emitted when the widget is cleared.
void setDefaultValue(const QString &defaultValue)
Sets the default value for the widget.
void setShowSpinner(bool showSpinner)
Show a spinner icon.
bool event(QEvent *event) override
Reimplemented to enable/disable the clear action depending on read-only status.
void valueChanged(const QString &value)
Same as textChanged() but with support for null values.