Qt Utilities  5.6.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 <QWidget>
5 #include <QHBoxLayout>
6 #include <QStyle>
7 #include <QStyleOption>
8 #include <QToolTip>
9 #include <QCursor>
10 
11 #include <functional>
12 
18 namespace Widgets {
19 
34 ButtonOverlay::ButtonOverlay(QWidget *widget) :
35  m_widget(widget),
36  m_buttonWidget(new QWidget(widget)),
37  m_buttonLayout(new QHBoxLayout(m_buttonWidget)),
38  m_clearButton(nullptr),
39  m_infoButton(nullptr)
40 {
41  // setup button widget and layout
42  const QMargins margins = widget->contentsMargins();
43  QStyleOption opt;
44  opt.initFrom(m_widget);
45  const int frameWidth = widget->style()->pixelMetric(QStyle::PM_DefaultFrameWidth, &opt, m_widget);
46  const int pad = 2;
47  m_buttonLayout->setContentsMargins(margins.left() + frameWidth + pad, margins.top() + frameWidth, margins.right() + frameWidth + pad, margins.bottom() + frameWidth);
48  m_buttonLayout->setAlignment(Qt::AlignCenter | Qt::AlignRight);
49  widget->setLayout(m_buttonLayout);
50 }
51 
56 {}
57 
62 {
63  if(isClearButtonEnabled() && !enabled) {
64  // disable clear button
65  m_buttonLayout->removeWidget(m_clearButton);
66  delete m_clearButton;
67  m_clearButton = nullptr;
68  } else if(!isClearButtonEnabled() && enabled) {
69  // enable clear button
70  m_clearButton = new IconButton;
71  m_clearButton->setHidden(isCleared());
72  m_clearButton->setPixmap(/*QIcon::fromTheme(QStringLiteral("edit-clear"), */QIcon(QStringLiteral(":/qtutilities/icons/hicolor/48x48/actions/edit-clear.png")/*)*/).pixmap(16));
73  m_clearButton->setGeometry(0, 0, 16, 16);
74  m_clearButton->setToolTip(QObject::tr("Clear"));
75  QObject::connect(m_clearButton, &IconButton::clicked, std::bind(&ButtonOverlay::handleClearButtonClicked, this));
76  m_buttonLayout->addWidget(m_clearButton);
77  }
78 }
79 
87 void ButtonOverlay::enableInfoButton(const QPixmap &pixmap, const QString &infoText)
88 {
89  if(!m_infoButton) {
90  m_infoButton = new IconButton;
91  m_infoButton->setGeometry(0, 0, 16, 16);
92  QObject::connect(m_infoButton, &IconButton::clicked, std::bind(&ButtonOverlay::showInfo, this));
93  if(m_clearButton) {
94  m_buttonLayout->insertWidget(m_buttonLayout->count() - 2, m_infoButton);
95  } else {
96  m_buttonLayout->addWidget(m_infoButton);
97  }
98  }
99  m_infoButton->setPixmap(pixmap);
100  m_infoButton->setToolTip(infoText);
101 }
102 
108 {
109  if(m_infoButton) {
110  m_buttonLayout->removeWidget(m_infoButton);
111  delete m_infoButton;
112  m_infoButton = nullptr;
113  }
114 }
115 
121 void ButtonOverlay::addCustomButton(QWidget *button)
122 {
123  m_buttonLayout->addWidget(button);
124 }
125 
131 void ButtonOverlay::insertCustomButton(int index, QWidget *button)
132 {
133  m_buttonLayout->insertWidget(index, button);
134 }
135 
142 {
143  m_buttonLayout->removeWidget(button);
144 }
145 
152 {
153  if(m_clearButton) {
154  m_clearButton->setVisible(visible);
155  }
156 }
157 
164 {}
165 
172 {
173  return false;
174 }
175 
181 void ButtonOverlay::showInfo()
182 {
183  if(m_infoButton) {
184  QToolTip::showText(QCursor::pos(), m_infoButton->toolTip(), m_infoButton);
185  }
186 }
187 
188 }
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:44
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:59
virtual ~ButtonOverlay()
Destroys the button overlay.
void insertCustomButton(int index, QWidget *button)
Inserts a custom button at the specified index.