Debian Codegraph @

widgets/clearcombobox.cpp

raw symbols SCIP
1 #include "./clearcombobox.h"
2
3 #include <QHBoxLayout>
4 #include <QStyle>
5 #include <QStyleOptionComboBox>
6
7 namespace QtUtilities {
8
9 /*!
10 * \class ClearComboBox
11 * \brief A QComboBox with an embedded button for clearing its contents.
12 */
13
14 /// \cond
15 static inline auto *getComboBoxLineEdit(QComboBox *comboBox)
16 {
17 comboBox->setEditable(true);
18 return comboBox->lineEdit();
19 }
20 /// \endcond
21
22 /*!
23 * \brief Constructs a clear combo box.
24 * \remarks The combo box is initialized to be editable and which must not be changed.
25 */
26 ClearComboBox::ClearComboBox(QWidget *parent)
27 : QComboBox(parent)
28 , ButtonOverlay(this, getComboBoxLineEdit(this))
29 {
30 ButtonOverlay::setClearButtonEnabled(true);
31 }
32
33 /*!
34 * \brief Destroys the clear combo box.
35 */
36 ClearComboBox::~ClearComboBox()
37 {
38 }
39
40 /*!
41 * \brief Updates the visibility of the clear button.
42 */
43 void ClearComboBox::handleTextChanged(const QString &text)
44 {
45 updateClearButtonVisibility(!text.isEmpty());
46 }
47
48 void ClearComboBox::handleClearButtonClicked()
49 {
50 clearEditText();
51 }
52
53 void ClearComboBox::handleCustomLayoutCreated()
54 {
55 const QStyle *const s = style();
56 QStyleOptionComboBox opt;
57 opt.initFrom(this);
58 setContentsMarginsFromEditFieldRectAndFrameWidth(
59 s->subControlRect(QStyle::CC_ComboBox, &opt, QStyle::SC_ComboBoxEditField, this), s->pixelMetric(QStyle::PM_ComboBoxFrameWidth, &opt, this));
60 connect(this, &ClearComboBox::currentTextChanged, this, &ClearComboBox::handleTextChanged);
61 }
62
63 bool ClearComboBox::isCleared() const
64 {
65 return currentText().isEmpty();
66 }
67 } // namespace QtUtilities