11#include <QStyleOption>
51 , m_buttonWidget(nullptr)
52 , m_buttonLayout(nullptr)
53 , m_clearButton(nullptr)
54 , m_infoButtonOrAction(nullptr)
56 fallbackToUsingCustomLayout();
67 , m_buttonWidget(lineEdit)
68 , m_buttonLayout(nullptr)
69 , m_clearButton(nullptr)
70 , m_infoButtonOrAction(nullptr)
72 if (!m_buttonWidget) {
73 fallbackToUsingCustomLayout();
89 return m_buttonLayout !=
nullptr;
99 fallbackToUsingCustomLayout();
100 return m_buttonLayout;
109 return m_clearButton !=
nullptr;
111 return lineEditForWidget()->isClearButtonEnabled();
119 return m_infoButtonOrAction !=
nullptr;
127 if (
auto *
const le = lineEditForWidget()) {
128 le->setClearButtonEnabled(enabled);
132 if (clearButtonEnabled && !enabled) {
134 m_buttonLayout->removeWidget(m_clearButton);
135 delete m_clearButton;
136 m_clearButton =
nullptr;
137 }
else if (!clearButtonEnabled && enabled) {
143 m_clearButton->setToolTip(QObject::tr(
"Clear"));
145 m_buttonLayout->addWidget(m_clearButton);
159 if (
auto *
const le = lineEditForWidget()) {
161 auto *
const action = le->addAction(QIcon(pixmap), QLineEdit::TrailingPosition);
162 action->setToolTip(infoText);
163 QObject::connect(action, &QAction::triggered, std::bind(&ButtonOverlay::showInfo,
this));
164 m_infoButtonOrAction = action;
167 auto *infoButton =
static_cast<IconButton *
>(m_infoButtonOrAction);
169 m_infoButtonOrAction = infoButton =
new IconButton;
172 m_buttonLayout->insertWidget(m_buttonLayout->count() - 2, infoButton);
174 m_buttonLayout->addWidget(infoButton);
178 infoButton->setToolTip(infoText);
187 if (
auto *
const le = lineEditForWidget()) {
188 if (
auto *
const infoAction =
static_cast<QAction *
>(m_infoButtonOrAction)) {
189 le->removeAction(infoAction);
190 m_infoButtonOrAction =
nullptr;
194 if (
auto *infoButton =
static_cast<IconButton *
>(m_infoButtonOrAction)) {
195 m_buttonLayout->removeWidget(infoButton);
197 m_infoButtonOrAction =
nullptr;
211 fallbackToUsingCustomLayout();
212 m_buttonLayout->addWidget(button);
225 fallbackToUsingCustomLayout();
226 m_buttonLayout->insertWidget(index, button);
237 m_buttonLayout->removeWidget(button);
246 if (
auto *
const le = lineEditForWidget()) {
247 le->addAction(action, QLineEdit::TrailingPosition);
258 if (
auto *
const le = lineEditForWidget()) {
259 const auto actions = le->actions();
260 le->insertAction(index < actions.size() ? actions[index] :
nullptr, action);
271 if (
auto *
const le = lineEditForWidget()) {
272 le->removeAction(action);
286 m_clearButton->setVisible(visible);
314void ButtonOverlay::fallbackToUsingCustomLayout()
322 auto clearButtonEnabled =
false;
323 auto *iconAction =
static_cast<QAction *
>(m_infoButtonOrAction);
326 QList<QAction *> actions;
327 if (
auto *
const le = lineEditForWidget()) {
328 if ((clearButtonEnabled = le->isClearButtonEnabled())) {
331 if ((iconAction =
static_cast<QAction *
>(m_infoButtonOrAction))) {
332 const auto icon = iconAction->icon();
333 const auto sizes = icon.availableSizes();
335 infoText = iconAction->toolTip();
338 actions = le->actions();
339 for (
auto *
const action : actions) {
340 le->removeAction(action);
345 m_buttonLayout =
new QHBoxLayout(m_buttonWidget);
346 m_buttonWidget =
new QWidget(m_widget);
347 m_buttonLayout->setAlignment(Qt::AlignCenter | Qt::AlignRight);
348 m_widget->setLayout(m_buttonLayout);
352 if (clearButtonEnabled) {
358 for (
auto *
const action : actions) {
367QLineEdit *ButtonOverlay::lineEditForWidget()
const
391void ButtonOverlay::showInfo()
393 if (
auto const *
const le = lineEditForWidget()) {
394 if (
auto *
const infoAction =
static_cast<QAction *
>(m_infoButtonOrAction)) {
395 const auto pos = QCursor::pos();
397 QToolTip::showText(pos, infoAction->toolTip(), m_widget);
402 if (
auto *
const infoButton =
static_cast<IconButton *
>(m_infoButtonOrAction)) {
403 QToolTip::showText(infoButton->mapToGlobal(infoButton->rect().center()), infoButton->toolTip(), infoButton);
413void ButtonOverlay::setContentsMarginsFromEditFieldRectAndFrameWidth(
const QRect &editFieldRect,
int frameWidth,
int padding)
415 const auto margins = m_widget->contentsMargins();
416 const auto buttonWidth = m_widget->width() - editFieldRect.width();
417 buttonLayout()->setContentsMargins(margins.left() + frameWidth + padding, margins.top() + frameWidth,
418 margins.right() + frameWidth + padding + buttonWidth, margins.bottom() + frameWidth);