| 1 |
// SPDX-License-Identifier: LGPL-3.0-only |
| 2 |
/*************************************************************************** |
| 3 |
* * |
| 4 |
* Copyright: https://code.google.com/p/qsint/ * |
| 5 |
* License: LGPL * |
| 6 |
* * |
| 7 |
***************************************************************************/ |
| 8 |
|
| 9 |
#include "actionlabel.h" |
| 10 |
|
| 11 |
#include <QApplication> |
| 12 |
#include <QStyleOptionToolButton> |
| 13 |
#include <QAction> |
| 14 |
#include <QPainter> |
| 15 |
#include <cmath> |
| 16 |
|
| 17 |
namespace QSint |
| 18 |
{ |
| 19 |
|
| 20 |
ActionLabel::ActionLabel(QWidget *parent) |
| 21 |
: QToolButton(parent) |
| 22 |
{ |
| 23 |
init(); |
| 24 |
} |
| 25 |
|
| 26 |
ActionLabel::ActionLabel(QAction *action, QWidget *parent) |
| 27 |
: QToolButton(parent) |
| 28 |
{ |
| 29 |
init(); |
| 30 |
setDefaultAction(action); |
| 31 |
} |
| 32 |
|
| 33 |
void ActionLabel::init() |
| 34 |
{ |
| 35 |
setProperty("class", "action"); |
| 36 |
setCursor(Qt::PointingHandCursor); |
| 37 |
setToolButtonStyle(Qt::ToolButtonTextBesideIcon); |
| 38 |
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); |
| 39 |
setFocusPolicy(Qt::StrongFocus); |
| 40 |
} |
| 41 |
|
| 42 |
QSize ActionLabel::sizeHint() const |
| 43 |
{ |
| 44 |
ensurePolished(); |
| 45 |
|
| 46 |
QStyleOptionToolButton opt; |
| 47 |
initStyleOption(&opt); |
| 48 |
|
| 49 |
QString s = text().isEmpty() ? QStringLiteral("XXXX") : text(); |
| 50 |
QSize textSize = fontMetrics().size(Qt::TextShowMnemonic, s); |
| 51 |
constexpr int padding = 10; |
| 52 |
|
| 53 |
int width = textSize.width(); |
| 54 |
if (!icon().isNull()) { |
| 55 |
width += opt.iconSize.width() + padding; |
| 56 |
} |
| 57 |
|
| 58 |
if (menu()) { |
| 59 |
width += style()->pixelMetric(QStyle::PM_MenuButtonIndicator, &opt, this); |
| 60 |
} |
| 61 |
|
| 62 |
int height = qMax(textSize.height(), opt.iconSize.height()); |
| 63 |
|
| 64 |
return style()->sizeFromContents( |
| 65 |
QStyle::CT_PushButton, &opt, QSize(width + 2 * padding, height + padding), this |
| 66 |
); |
| 67 |
} |
| 68 |
|
| 69 |
QSize ActionLabel::minimumSizeHint() const |
| 70 |
{ |
| 71 |
return sizeHint(); |
| 72 |
} |
| 73 |
|
| 74 |
void ActionLabel::paintEvent(QPaintEvent *event) |
| 75 |
{ |
| 76 |
Q_UNUSED(event); |
| 77 |
QPainter painter(this); |
| 78 |
|
| 79 |
constexpr int elementSpacing = 5; |
| 80 |
int iconSize = fontMetrics().height(); |
| 81 |
QIcon icon = this->icon(); |
| 82 |
QString text = this->text(); |
| 83 |
|
| 84 |
QRect contentRect = rect().adjusted(elementSpacing, 0, -elementSpacing, 0); // Apply margins |
| 85 |
|
| 86 |
int currentX = contentRect.left(); |
| 87 |
|
| 88 |
if (!icon.isNull()) { |
| 89 |
QPixmap pixmap = icon.pixmap(iconSize, iconSize); |
| 90 |
QRect iconRect(currentX, contentRect.center().y() - iconSize / 2, iconSize, iconSize); |
| 91 |
painter.setRenderHint(QPainter::SmoothPixmapTransform); |
| 92 |
painter.drawPixmap(iconRect, pixmap); |
| 93 |
currentX += iconSize + elementSpacing; |
| 94 |
} |
| 95 |
|
| 96 |
if (!text.isEmpty()) { |
| 97 |
QRect textRect(currentX, contentRect.top(), contentRect.right() - currentX, contentRect.height()); |
| 98 |
|
| 99 |
QFont font = this->font(); |
| 100 |
font.setUnderline(underMouse()); |
| 101 |
painter.setFont(font); |
| 102 |
|
| 103 |
painter.setRenderHint(QPainter::TextAntialiasing); |
| 104 |
painter.drawText(textRect, Qt::AlignVCenter | Qt::AlignLeft, text); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
} // namespace QSint |