| 1 | #include "./clearlineedit.h" |
| 2 | |
| 3 | #include <QStyle> |
| 4 | #include <QStyleOptionFrame> |
| 5 | |
| 6 | namespace QtUtilities { |
| 7 | |
| 8 | /*! |
| 9 | * \class ClearLineEdit |
| 10 | * \brief A QLineEdit with an embedded button for clearing its contents. |
| 11 | */ |
| 12 | |
| 13 | /*! |
| 14 | * \brief Constructs a clear line edit. |
| 15 | */ |
| 16 | ClearLineEdit::ClearLineEdit(QWidget *parent) |
| 17 | : QLineEdit(parent) |
| 18 | , ButtonOverlay(this, this) |
| 19 | { |
| 20 | ButtonOverlay::setClearButtonEnabled(true); |
| 21 | } |
| 22 | |
| 23 | /*! |
| 24 | * \brief Destroys the clear combo box. |
| 25 | */ |
| 26 | ClearLineEdit::~ClearLineEdit() |
| 27 | { |
| 28 | } |
| 29 | |
| 30 | /*! |
| 31 | * \brief Updates the visibility of the clear button. |
| 32 | */ |
| 33 | void ClearLineEdit::handleTextChanged(const QString &text) |
| 34 | { |
| 35 | updateClearButtonVisibility(!text.isEmpty()); |
| 36 | } |
| 37 | |
| 38 | void ClearLineEdit::handleClearButtonClicked() |
| 39 | { |
| 40 | clear(); |
| 41 | } |
| 42 | |
| 43 | void ClearLineEdit::handleCustomLayoutCreated() |
| 44 | { |
| 45 | const QStyle *const s = style(); |
| 46 | QStyleOptionFrame opt; |
| 47 | opt.initFrom(this); |
| 48 | setContentsMarginsFromEditFieldRectAndFrameWidth(s->subElementRect(QStyle::SE_LineEditContents, &opt, this), |
| 49 | s->pixelMetric(QStyle::PM_DefaultFrameWidth, &opt, m_widget), s->pixelMetric(QStyle::PM_LayoutVerticalSpacing, &opt, m_widget)); |
| 50 | connect(this, &ClearLineEdit::textChanged, this, &ClearLineEdit::handleTextChanged); |
| 51 | } |
| 52 | |
| 53 | bool ClearLineEdit::isCleared() const |
| 54 | { |
| 55 | return text().isEmpty(); |
| 56 | } |
| 57 | } // namespace QtUtilities |