Qt Utilities  5.10.0
Common Qt related C++ classes and routines used by my applications such as dialogs, widgets and models
buttonoverlay.cpp
Go to the documentation of this file.
1 #include "./buttonoverlay.h"
2 #include "./iconbutton.h"
3 
4 #include <QCursor>
5 #include <QHBoxLayout>
6 #include <QStyle>
7 #include <QStyleOption>
8 #include <QToolTip>
9 #include <QWidget>
10 
11 #include <functional>
12 
19 namespace Widgets {
20 
40  : m_widget(widget)
41  , m_buttonWidget(new QWidget(widget))
42  , m_buttonLayout(new QHBoxLayout(m_buttonWidget))
43  , m_clearButton(nullptr)
44  , m_infoButton(nullptr)
45 {
46  // setup button widget and layout
47  const QMargins margins = widget->contentsMargins();
48  QStyleOption opt;
49  opt.initFrom(m_widget);
50  const int frameWidth = widget->style()->pixelMetric(QStyle::PM_DefaultFrameWidth, &opt, m_widget);
51  const int pad = 2;
52  m_buttonLayout->setContentsMargins(
53  margins.left() + frameWidth + pad, margins.top() + frameWidth, margins.right() + frameWidth + pad, margins.bottom() + frameWidth);
54  m_buttonLayout->setAlignment(Qt::AlignCenter | Qt::AlignRight);
55  widget->setLayout(m_buttonLayout);
56 }
57 
62 {
63 }
64 
69 {
70  if (isClearButtonEnabled() && !enabled) {
71  // disable clear button
72  m_buttonLayout->removeWidget(m_clearButton);
73  delete m_clearButton;
74  m_clearButton = nullptr;
75  } else if (!isClearButtonEnabled() && enabled) {
76  // enable clear button
77  m_clearButton = new IconButton;
78  m_clearButton->setHidden(isCleared());
79  m_clearButton->setPixmap(
80  /*QIcon::fromTheme(QStringLiteral("edit-clear"), */ QIcon(QStringLiteral(":/qtutilities/icons/hicolor/48x48/actions/"
81  "edit-clear.png") /*)*/)
82  .pixmap(16));
83  m_clearButton->setGeometry(0, 0, 16, 16);
84  m_clearButton->setToolTip(QObject::tr("Clear"));
85  QObject::connect(m_clearButton, &IconButton::clicked, std::bind(&ButtonOverlay::handleClearButtonClicked, this));
86  m_buttonLayout->addWidget(m_clearButton);
87  }
88 }
89 
98 void ButtonOverlay::enableInfoButton(const QPixmap &pixmap, const QString &infoText)
99 {
100  if (!m_infoButton) {
101  m_infoButton = new IconButton;
102  m_infoButton->setGeometry(0, 0, 16, 16);
103  QObject::connect(m_infoButton, &IconButton::clicked, std::bind(&ButtonOverlay::showInfo, this));
104  if (m_clearButton) {
105  m_buttonLayout->insertWidget(m_buttonLayout->count() - 2, m_infoButton);
106  } else {
107  m_buttonLayout->addWidget(m_infoButton);
108  }
109  }
110  m_infoButton->setPixmap(pixmap);
111  m_infoButton->setToolTip(infoText);
112 }
113 
119 {
120  if (m_infoButton) {
121  m_buttonLayout->removeWidget(m_infoButton);
122  delete m_infoButton;
123  m_infoButton = nullptr;
124  }
125 }
126 
132 void ButtonOverlay::addCustomButton(QWidget *button)
133 {
134  m_buttonLayout->addWidget(button);
135 }
136 
142 void ButtonOverlay::insertCustomButton(int index, QWidget *button)
143 {
144  m_buttonLayout->insertWidget(index, button);
145 }
146 
153 {
154  m_buttonLayout->removeWidget(button);
155 }
156 
163 {
164  if (m_clearButton) {
165  m_clearButton->setVisible(visible);
166  }
167 }
168 
175 {
176 }
177 
184 {
185  return false;
186 }
187 
193 void ButtonOverlay::showInfo()
194 {
195  if (m_infoButton) {
196  QToolTip::showText(QCursor::pos(), m_infoButton->toolTip(), m_infoButton);
197  }
198 }
199 } // namespace Widgets
void disableInfoButton()
Hides an info button if one is shown.
A simple QAbstractButton implementation displaying a QPixmap.
Definition: iconbutton.h:11
void enableInfoButton(const QPixmap &pixmap, const QString &infoText)
Shows an info button with the specified pixmap and infoText.
void setPixmap(const QPixmap &pixmap)
Sets the pixmap.
Definition: iconbutton.h:43
void updateClearButtonVisibility(bool visible)
Updates the visibility of the clear button.
void addCustomButton(QWidget *button)
Adds a custom button.
ButtonOverlay(QWidget *widget)
Constructs a button overlay for the specified widget.
virtual bool isCleared() const
Returns whether the related widget is cleared.
virtual void handleClearButtonClicked()
Clears the related widget.
void removeCustomButton(QWidget *button)
Removes the specified custom button.
void setClearButtonEnabled(bool enabled)
Sets whether the clear button is enabled.
Provides a set of extended widgets such as ClearLineEdit and ClearComboBox.
Definition: buttonoverlay.h:13
bool isClearButtonEnabled() const
Returns whether the clear button is enabled.
Definition: buttonoverlay.h:58
virtual ~ButtonOverlay()
Destroys the button overlay.
void insertCustomButton(int index, QWidget *button)
Inserts a custom button at the specified index.