| 1 | // Copyright 2017 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/QtUtils/ElidedButton.h" |
| 5 | |
| 6 | #include <QStyleOptionButton> |
| 7 | #include <QStylePainter> |
| 8 | |
| 9 | ElidedButton::ElidedButton(const QString& text, Qt::TextElideMode elide_mode) |
| 10 | : QPushButton(text, nullptr), m_elide_mode{elide_mode} |
| 11 | { |
| 12 | } |
| 13 | |
| 14 | Qt::TextElideMode ElidedButton::elideMode() const |
| 15 | { |
| 16 | return m_elide_mode; |
| 17 | } |
| 18 | |
| 19 | void ElidedButton::setElideMode(Qt::TextElideMode elide_mode) |
| 20 | { |
| 21 | if (elide_mode == m_elide_mode) |
| 22 | return; |
| 23 | |
| 24 | m_elide_mode = elide_mode; |
| 25 | repaint(); |
| 26 | } |
| 27 | |
| 28 | QSize ElidedButton::sizeHint() const |
| 29 | { |
| 30 | // Long text produces big sizeHints which is throwing layouts off |
| 31 | // even when setting fixed sizes. This seems like a Qt layout bug. |
| 32 | // Let's always return the sizeHint of an empty button to work around this. |
| 33 | return QPushButton(parentWidget()).sizeHint(); |
| 34 | } |
| 35 | |
| 36 | void ElidedButton::paintEvent(QPaintEvent* event) |
| 37 | { |
| 38 | QStyleOptionButton option; |
| 39 | initStyleOption(&option); |
| 40 | |
| 41 | option.text = fontMetrics().elidedText( |
| 42 | text(), m_elide_mode, |
| 43 | style()->subElementRect(QStyle::SE_PushButtonContents, &option, this).width()); |
| 44 | |
| 45 | QStylePainter{this}.drawControl(QStyle::CE_PushButton, option); |
| 46 | } |