qtutilities/widgets/clearcombobox.cpp

68 lines
1.5 KiB
C++
Raw Permalink Normal View History

2015-09-06 20:19:21 +02:00
#include "./clearcombobox.h"
2015-04-22 18:57:44 +02:00
#include <QHBoxLayout>
#include <QStyle>
#include <QStyleOptionComboBox>
namespace QtUtilities {
2015-04-22 18:57:44 +02:00
/*!
* \class ClearComboBox
2015-04-22 18:57:44 +02:00
* \brief A QComboBox with an embedded button for clearing its contents.
*/
/// \cond
static inline auto *getComboBoxLineEdit(QComboBox *comboBox)
{
comboBox->setEditable(true);
return comboBox->lineEdit();
}
/// \endcond
2015-04-22 18:57:44 +02:00
/*!
* \brief Constructs a clear combo box.
* \remarks The combo box is initialized to be editable and which must not be changed.
2015-04-22 18:57:44 +02:00
*/
2017-05-01 03:16:25 +02:00
ClearComboBox::ClearComboBox(QWidget *parent)
: QComboBox(parent)
, ButtonOverlay(this, getComboBoxLineEdit(this))
2015-04-22 18:57:44 +02:00
{
ButtonOverlay::setClearButtonEnabled(true);
2015-04-22 18:57:44 +02:00
}
/*!
* \brief Destroys the clear combo box.
*/
ClearComboBox::~ClearComboBox()
2017-05-01 03:16:25 +02:00
{
}
2015-04-22 18:57:44 +02:00
/*!
* \brief Updates the visibility of the clear button.
*/
void ClearComboBox::handleTextChanged(const QString &text)
{
updateClearButtonVisibility(!text.isEmpty());
}
void ClearComboBox::handleClearButtonClicked()
{
clearEditText();
}
void ClearComboBox::handleCustomLayoutCreated()
{
const QStyle *const s = style();
QStyleOptionComboBox opt;
opt.initFrom(this);
setContentsMarginsFromEditFieldRectAndFrameWidth(
s->subControlRect(QStyle::CC_ComboBox, &opt, QStyle::SC_ComboBoxEditField, this), s->pixelMetric(QStyle::PM_ComboBoxFrameWidth, &opt, this));
connect(this, &ClearComboBox::currentTextChanged, this, &ClearComboBox::handleTextChanged);
}
2015-04-22 18:57:44 +02:00
bool ClearComboBox::isCleared() const
{
return currentText().isEmpty();
}
} // namespace QtUtilities