| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
/**************************************************************************** |
| 3 |
* * |
| 4 |
* Copyright (c) 2025 Alfredo Monclus <[email protected]> * |
| 5 |
* * |
| 6 |
* This file is part of FreeCAD. * |
| 7 |
* * |
| 8 |
* FreeCAD is free software: you can redistribute it and/or modify it * |
| 9 |
* under the terms of the GNU Lesser General Public License as * |
| 10 |
* published by the Free Software Foundation, either version 2.1 of the * |
| 11 |
* License, or (at your option) any later version. * |
| 12 |
* * |
| 13 |
* FreeCAD is distributed in the hope that it will be useful, but * |
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 16 |
* Lesser General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Lesser General Public * |
| 19 |
* License along with FreeCAD. If not, see * |
| 20 |
* <https://www.gnu.org/licenses/>. * |
| 21 |
* * |
| 22 |
***************************************************************************/ |
| 23 |
|
| 24 |
// This custom widget adds the missing ellipsize functionality in QT5 |
| 25 |
|
| 26 |
#include "ElideCheckBox.h" |
| 27 |
|
| 28 |
namespace Gui |
| 29 |
{ |
| 30 |
|
| 31 |
ElideCheckBox::ElideCheckBox(QWidget* parent) |
| 32 |
: QCheckBox(parent) |
| 33 |
{} |
| 34 |
|
| 35 |
void ElideCheckBox::paintEvent(QPaintEvent* event) |
| 36 |
{ |
| 37 |
Q_UNUSED(event); |
| 38 |
QStyleOptionButton option; |
| 39 |
option.initFrom(this); |
| 40 |
|
| 41 |
option.state = (isChecked() ? QStyle::State_On : QStyle::State_Off) |
| 42 |
| (isEnabled() ? QStyle::State_Enabled : QStyle::State_ReadOnly) |
| 43 |
| (underMouse() ? QStyle::State_MouseOver : QStyle::State_None) |
| 44 |
| (hasFocus() ? QStyle::State_HasFocus : QStyle::State_None); |
| 45 |
|
| 46 |
QPainter painter(this); |
| 47 |
|
| 48 |
style()->drawControl(QStyle::CE_CheckBox, &option, &painter, this); |
| 49 |
|
| 50 |
QRect textRect = style()->subElementRect(QStyle::SE_CheckBoxContents, &option, this); |
| 51 |
|
| 52 |
QFontMetrics fm(font()); |
| 53 |
QString elidedText = fm.elidedText(text(), Qt::ElideRight, textRect.width()); |
| 54 |
|
| 55 |
painter.setPen(palette().color(QPalette::WindowText)); |
| 56 |
painter.drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, elidedText); |
| 57 |
} |
| 58 |
|
| 59 |
QSize ElideCheckBox::sizeHint() const |
| 60 |
{ |
| 61 |
QFontMetrics fm(font()); |
| 62 |
int width = fm.horizontalAdvance(this->text()) |
| 63 |
+ style()->pixelMetric(QStyle::PM_IndicatorWidth, nullptr, this) |
| 64 |
+ style()->pixelMetric(QStyle::PM_CheckBoxLabelSpacing, nullptr, this); |
| 65 |
int height = fm.height(); |
| 66 |
return {width, height}; |
| 67 |
} |
| 68 |
|
| 69 |
QSize ElideCheckBox::minimumSizeHint() const |
| 70 |
{ |
| 71 |
QFontMetrics fm(font()); |
| 72 |
QString minimumText = QStringLiteral("A..."); |
| 73 |
int width = fm.horizontalAdvance(minimumText) |
| 74 |
+ style()->pixelMetric(QStyle::PM_IndicatorWidth, nullptr, this) |
| 75 |
+ style()->pixelMetric(QStyle::PM_CheckBoxLabelSpacing, nullptr, this); |
| 76 |
int height = fm.height(); |
| 77 |
return {width, height}; |
| 78 |
} |
| 79 |
|
| 80 |
} // namespace Gui |
| 81 |
|
| 82 |
#include "moc_ElideCheckBox.cpp" // NOLINT |