1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
|
#include "./buttonoverlay.h"
#include "./iconbutton.h"
#include <QAction>
#include <QComboBox>
#include <QCursor>
#include <QHBoxLayout>
#include <QIcon>
#include <QLineEdit>
#include <QStyle>
#include <QStyleOption>
#include <QToolTip>
#include <QWidget>
#include <functional>
namespace QtUtilities {
/*!
* \class ButtonOverlay
* \brief The ButtonOverlay class is used to display buttons on top of other widgets.
*
* This class had been created before QLineEdit's functions setClearButtonEnabled() and
* addAction() have been available. (These functions have been available only since Qt 5.2.)
*
* The downside of the "custom approach" compared to QLineEdit's own functions is that the
* buttons are shown over the text as the text margins are not updated accordingly. Hence
* the ButtonOverlay class has been updated to use QLineEdit's functions internally when the
* specified widget is QLineEdit-based and its QLineEdit has been passed to the constructor.
* However, when using any functions which can not be implemented using QLineEdit's own
* functions, the ButtonOverlay has to fallback to its "custom approach". All functions which
* cause this have a remark in their documentation.
*
* When QLineEdit's functions can not be used, the ButtonOverlay class creates a new layout
* manager and sets it to the widget specified when constructing an instance. Thus this widget
* must not already have a layout manager.
*
* The class is used to implement widget customization like ClearLineEidt and ClearComboBox
* and most of the times it makes sense to use these widgets instead of using ButtonOverlay
* directly.
*/
/*!
* \brief Constructs a button overlay for the specified \a widget.
* \param widget Specifies the widget to display the buttons on.
* \remarks This function enforces the "custom approach" mentioned in the class documentation
* and should therefore be avoided.
*/
ButtonOverlay::ButtonOverlay(QWidget *widget)
: m_widget(widget)
, m_buttonWidget(nullptr)
, m_buttonLayout(nullptr)
, m_clearButton(nullptr)
, m_infoButtonOrAction(nullptr)
{
fallbackToUsingCustomLayout();
}
/*!
* \brief Constructs a button overlay for the specified \a widget.
* \param widget Specifies the widget to display the buttons on.
* \param lineEdit Specifies the line edit used by \a widget to use the QLineEdit's functions
* for adding actions instead of a custom layout.
*/
ButtonOverlay::ButtonOverlay(QWidget *widget, QLineEdit *lineEdit)
: m_widget(widget)
, m_buttonWidget(lineEdit)
, m_buttonLayout(nullptr)
, m_clearButton(nullptr)
, m_infoButtonOrAction(nullptr)
{
if (!m_buttonWidget) {
fallbackToUsingCustomLayout();
}
}
/*!
* \brief Destroys the button overlay.
*/
ButtonOverlay::~ButtonOverlay()
{
}
/*!
* \brief Returns whether the "custom approach" mentioned in the class documentation is used.
*/
bool ButtonOverlay::isUsingCustomLayout() const
{
return m_buttonLayout != nullptr;
}
/*!
* \brief Returns the layout manager holding the buttons.
* \remarks This function enforces the "custom approach" mentioned in the class documentation
* and should therefore be avoided.
*/
QHBoxLayout *ButtonOverlay::buttonLayout()
{
fallbackToUsingCustomLayout();
return m_buttonLayout;
}
/*!
* \brief Returns whether the clear button is enabled.
*/
bool ButtonOverlay::isClearButtonEnabled() const
{
if (isUsingCustomLayout()) {
return m_clearButton != nullptr;
}
return lineEditForWidget()->isClearButtonEnabled();
}
/*!
* \brief Returns whether the info button is enabled.
*/
bool ButtonOverlay::isInfoButtonEnabled() const
{
return m_infoButtonOrAction != nullptr;
}
/*!
* \brief Sets whether the clear button is enabled.
*/
void ButtonOverlay::setClearButtonEnabled(bool enabled)
{
if (auto *const le = lineEditForWidget()) {
le->setClearButtonEnabled(enabled);
return;
}
const auto clearButtonEnabled = isClearButtonEnabled();
if (clearButtonEnabled && !enabled) {
// disable clear button
m_buttonLayout->removeWidget(m_clearButton);
delete m_clearButton;
m_clearButton = nullptr;
} else if (!clearButtonEnabled && enabled) {
// enable clear button
m_clearButton = new IconButton;
m_clearButton->setHidden(isCleared());
m_clearButton->setPixmap(QIcon::fromTheme(QStringLiteral("edit-clear")).pixmap(IconButton::defaultPixmapSize));
m_clearButton->setGeometry(QRect(QPoint(), IconButton::defaultPixmapSize));
m_clearButton->setToolTip(QObject::tr("Clear"));
QObject::connect(m_clearButton, &IconButton::clicked, std::bind(&ButtonOverlay::handleClearButtonClicked, this));
m_buttonLayout->addWidget(m_clearButton);
}
}
/*!
* \brief Shows an info button with the specified \a pixmap and \a infoText.
*
* If there is already an info button enabled, it gets replaced with the new
* button.
*
* \sa ButtonOverlay::disableInfoButton()
*/
void ButtonOverlay::enableInfoButton(const QPixmap &pixmap, const QString &infoText)
{
if (auto *const le = lineEditForWidget()) {
disableInfoButton();
auto *const action = le->addAction(QIcon(pixmap), QLineEdit::TrailingPosition);
action->setToolTip(infoText);
QObject::connect(action, &QAction::triggered, std::bind(&ButtonOverlay::showInfo, this));
m_infoButtonOrAction = action;
return;
}
auto *infoButton = static_cast<IconButton *>(m_infoButtonOrAction);
if (!infoButton) {
m_infoButtonOrAction = infoButton = new IconButton;
infoButton->setGeometry(QRect(QPoint(), IconButton::defaultPixmapSize));
if (m_clearButton) {
m_buttonLayout->insertWidget(m_buttonLayout->count() - 2, infoButton);
} else {
m_buttonLayout->addWidget(infoButton);
}
}
infoButton->setPixmap(pixmap);
infoButton->setToolTip(infoText);
}
/*!
* \brief Hides an info button if one is shown.
* \sa ButtonOverlay::enableInfoButton()
*/
void ButtonOverlay::disableInfoButton()
{
if (auto *const le = lineEditForWidget()) {
if (auto *const infoAction = static_cast<QAction *>(m_infoButtonOrAction)) {
le->removeAction(infoAction);
m_infoButtonOrAction = nullptr;
}
return;
}
if (auto *infoButton = static_cast<IconButton *>(m_infoButtonOrAction)) {
m_buttonLayout->removeWidget(infoButton);
delete infoButton;
m_infoButtonOrAction = nullptr;
}
}
/*!
* \brief Adds a custom \a button.
*
* The button overlay takes ownership over the specified \a button.
*
* \remarks This function enforces the "custom approach" mentioned in the class documentation
* and should therefore be avoided.
*/
void ButtonOverlay::addCustomButton(QWidget *button)
{
fallbackToUsingCustomLayout();
m_buttonLayout->addWidget(button);
}
/*!
* \brief Inserts a custom \a button at the specified \a index.
*
* The button overlay takes ownership over the specified \a button.
*
* \remarks This function enforces the "custom approach" mentioned in the class documentation
* and should therefore be avoided.
*/
void ButtonOverlay::insertCustomButton(int index, QWidget *button)
{
fallbackToUsingCustomLayout();
m_buttonLayout->insertWidget(index, button);
}
/*!
* \brief Removes the specified custom \a button; does nothing if \a button has not been added.
*
* The ownership of widget remains the same as when it was added.
*/
void ButtonOverlay::removeCustomButton(QWidget *button)
{
if (isUsingCustomLayout()) {
m_buttonLayout->removeWidget(button);
}
}
/*!
* \brief Adds a custom \a action.
*/
void ButtonOverlay::addCustomAction(QAction *action)
{
if (auto *const le = lineEditForWidget()) {
le->addAction(action, QLineEdit::TrailingPosition);
} else {
addCustomButton(IconButton::fromAction(action, reinterpret_cast<std::uintptr_t>(this)));
}
}
/*!
* \brief Inserts a custom \a action at the specified \a index.
*/
void ButtonOverlay::insertCustomAction(int index, QAction *action)
{
if (auto *const le = lineEditForWidget()) {
const auto actions = le->actions();
le->insertAction(index < actions.size() ? actions[index] : nullptr, action);
} else {
insertCustomButton(index, IconButton::fromAction(action, reinterpret_cast<std::uintptr_t>(this)));
}
}
/*!
* \brief Removes the specified custom \a action; does nothing if \a action has not been added.
*/
void ButtonOverlay::removeCustomAction(QAction *action)
{
if (auto *const le = lineEditForWidget()) {
le->removeAction(action);
} else {
removeCustomButton(IconButton::fromAction(action, reinterpret_cast<std::uintptr_t>(this)));
}
}
/*!
* \brief Updates the visibility of the clear button.
*
* This function is meant to be called when subclassing.
*/
void ButtonOverlay::updateClearButtonVisibility(bool visible)
{
if (m_clearButton) {
m_clearButton->setVisible(visible);
}
}
/*!
* \brief Clears the related widget.
*
* This function is meant to be implemented when subclassing to support the clear button.
*/
void ButtonOverlay::handleClearButtonClicked()
{
}
/*!
* \brief Applies additional handling when the button layout has been created.
*
* This function is meant to be implemented when subclassing when additional handling is
* required.
*/
void ButtonOverlay::handleCustomLayoutCreated()
{
}
/*!
* \brief Switches to the "custom approach".
* \remarks This function is internally used when any legacy function is called
* or when the QLineEdit for the specified widget can not be determined.
*/
void ButtonOverlay::fallbackToUsingCustomLayout()
{
// skip if custom layout is already used
if (isUsingCustomLayout()) {
return;
}
// disable QLineEdit's clear button and actions; save configuration
auto clearButtonEnabled = false;
auto *iconAction = static_cast<QAction *>(m_infoButtonOrAction);
QPixmap infoPixmap;
QString infoText;
QList<QAction *> actions;
if (auto *const le = lineEditForWidget()) {
if ((clearButtonEnabled = le->isClearButtonEnabled())) {
setClearButtonEnabled(false);
}
if ((iconAction = static_cast<QAction *>(m_infoButtonOrAction))) {
const auto icon = iconAction->icon();
const auto sizes = icon.availableSizes();
infoPixmap = icon.pixmap(sizes.empty() ? IconButton::defaultPixmapSize : sizes.front());
infoText = iconAction->toolTip();
disableInfoButton();
}
actions = le->actions();
for (auto *const action : actions) {
le->removeAction(action);
}
}
// initialize custom layout
m_buttonLayout = new QHBoxLayout(m_buttonWidget);
m_buttonWidget = new QWidget(m_widget);
m_buttonLayout->setAlignment(Qt::AlignCenter | Qt::AlignRight);
m_widget->setLayout(m_buttonLayout);
handleCustomLayoutCreated();
// restore old configuration
if (clearButtonEnabled) {
setClearButtonEnabled(true);
}
if (iconAction) {
enableInfoButton(infoPixmap, infoText);
}
for (auto *const action : actions) {
addCustomAction(action);
}
}
/*!
* \brief Returns the QLineEdit used to implement the button overlay.
* \remarks This is always nullptr in case the "custom approach" is used.
*/
QLineEdit *ButtonOverlay::lineEditForWidget() const
{
return isUsingCustomLayout() ? nullptr : static_cast<QLineEdit *>(m_buttonWidget);
}
/*!
* \brief Returns whether the related widget is cleared.
*
* This method is meant to be implemented when subclassing.
*/
bool ButtonOverlay::isCleared() const
{
return false;
}
/*!
* \brief Shows the info text using a tool tip.
*
* This method is called when the info button is clicked.
*
* \remarks
* This function avoids using QCursor::pos() because it is problematic to use under Wayland. For the action case it seems not
* possible to avoid it because the position of QLineEditIconButton used by QLineEdit is not exposed.
*/
void ButtonOverlay::showInfo()
{
if (lineEditForWidget()) {
if (auto *const infoAction = static_cast<QAction *>(m_infoButtonOrAction)) {
if (const auto pos = QCursor::pos(); !pos.isNull()) {
QToolTip::showText(pos, infoAction->toolTip(), m_widget);
}
}
return;
}
if (auto *const infoButton = static_cast<IconButton *>(m_infoButtonOrAction)) {
QToolTip::showText(infoButton->mapToGlobal(infoButton->rect().center()), infoButton->toolTip(), infoButton);
}
}
/*!
* \brief Sets the contents margins of the button layout so the overlay buttons will only be shown over the \a editFieldRect and
* not interfere with e.g. spin box buttons.
* \remarks This function enforces the "custom approach" mentioned in the class documentation
* and should therefore be avoided. Of course it makes sense to call it within handleCustomLayoutCreated().
*/
void ButtonOverlay::setContentsMarginsFromEditFieldRectAndFrameWidth(const QRect &editFieldRect, int frameWidth, int padding)
{
const auto margins = m_widget->contentsMargins();
const auto buttonWidth = m_widget->width() - editFieldRect.width();
buttonLayout()->setContentsMargins(margins.left() + frameWidth + padding, margins.top() + frameWidth,
margins.right() + frameWidth + padding + buttonWidth, margins.bottom() + frameWidth);
}
} // namespace QtUtilities
|