2017-05-31 06:08:14 +00:00
|
|
|
// Copyright 2017 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2017-05-31 06:08:14 +00:00
|
|
|
|
2018-07-06 22:40:15 +00:00
|
|
|
#include "DolphinQt/QtUtils/ElidedButton.h"
|
2017-05-31 06:08:14 +00:00
|
|
|
|
|
|
|
#include <QStyleOptionButton>
|
|
|
|
#include <QStylePainter>
|
|
|
|
|
|
|
|
ElidedButton::ElidedButton(const QString& text, Qt::TextElideMode elide_mode)
|
|
|
|
: QPushButton(text, nullptr), m_elide_mode{elide_mode}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt::TextElideMode ElidedButton::elideMode() const
|
|
|
|
{
|
|
|
|
return m_elide_mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ElidedButton::setElideMode(Qt::TextElideMode elide_mode)
|
|
|
|
{
|
|
|
|
if (elide_mode == m_elide_mode)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_elide_mode = elide_mode;
|
|
|
|
repaint();
|
|
|
|
}
|
|
|
|
|
2019-03-03 14:05:59 +00:00
|
|
|
QSize ElidedButton::sizeHint() const
|
|
|
|
{
|
|
|
|
// Long text produces big sizeHints which is throwing layouts off
|
|
|
|
// even when setting fixed sizes. This seems like a Qt layout bug.
|
|
|
|
// Let's always return the sizeHint of an empty button to work around this.
|
|
|
|
return QPushButton(parentWidget()).sizeHint();
|
|
|
|
}
|
|
|
|
|
2017-05-31 06:08:14 +00:00
|
|
|
void ElidedButton::paintEvent(QPaintEvent* event)
|
|
|
|
{
|
|
|
|
QStyleOptionButton option;
|
|
|
|
initStyleOption(&option);
|
|
|
|
|
|
|
|
option.text = fontMetrics().elidedText(
|
|
|
|
text(), m_elide_mode,
|
|
|
|
style()->subElementRect(QStyle::SE_PushButtonContents, &option, this).width());
|
|
|
|
|
|
|
|
QStylePainter{this}.drawControl(QStyle::CE_PushButton, option);
|
|
|
|
}
|