File: clearlineedit.cpp

package info (click to toggle)
martchus-qtutilities 6.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 780 kB
  • sloc: cpp: 5,908; xml: 37; sh: 25; ansic: 12; makefile: 12
file content (57 lines) | stat: -rw-r--r-- 1,316 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "./clearlineedit.h"

#include <QStyle>
#include <QStyleOptionFrame>

namespace QtUtilities {

/*!
 * \class ClearLineEdit
 * \brief A QLineEdit with an embedded button for clearing its contents.
 */

/*!
 * \brief Constructs a clear line edit.
 */
ClearLineEdit::ClearLineEdit(QWidget *parent)
    : QLineEdit(parent)
    , ButtonOverlay(this, this)
{
    ButtonOverlay::setClearButtonEnabled(true);
}

/*!
 * \brief Destroys the clear combo box.
 */
ClearLineEdit::~ClearLineEdit()
{
}

/*!
 * \brief Updates the visibility of the clear button.
 */
void ClearLineEdit::handleTextChanged(const QString &text)
{
    updateClearButtonVisibility(!text.isEmpty());
}

void ClearLineEdit::handleClearButtonClicked()
{
    clear();
}

void ClearLineEdit::handleCustomLayoutCreated()
{
    const QStyle *const s = style();
    QStyleOptionFrame opt;
    opt.initFrom(this);
    setContentsMarginsFromEditFieldRectAndFrameWidth(s->subElementRect(QStyle::SE_LineEditContents, &opt, this),
        s->pixelMetric(QStyle::PM_DefaultFrameWidth, &opt, m_widget), s->pixelMetric(QStyle::PM_LayoutVerticalSpacing, &opt, m_widget));
    connect(this, &ClearLineEdit::textChanged, this, &ClearLineEdit::handleTextChanged);
}

bool ClearLineEdit::isCleared() const
{
    return text().isEmpty();
}
} // namespace QtUtilities