QSkinny 0.8.0
C++/Qt UI toolkit
Loading...
Searching...
No Matches
QskTextLabelSkinlet.cpp
1/******************************************************************************
2 * QSkinny - Copyright (C) The authors
3 * SPDX-License-Identifier: BSD-3-Clause
4 *****************************************************************************/
5
6#include "QskTextLabelSkinlet.h"
7#include "QskTextLabel.h"
8
9#include "QskTextOptions.h"
10#include "QskTextRenderer.h"
11
12#include <qfontmetrics.h>
13#include <qmath.h>
14
15QskTextLabelSkinlet::QskTextLabelSkinlet( QskSkin* skin )
16 : Inherited( skin )
17{
18 setNodeRoles( { PanelRole, TextRole } );
19}
20
21QskTextLabelSkinlet::~QskTextLabelSkinlet() = default;
22
23QRectF QskTextLabelSkinlet::subControlRect( const QskSkinnable* skinnable,
24 const QRectF& contentsRect, QskAspect::Subcontrol subControl ) const
25{
26 if ( subControl == QskTextLabel::Panel )
27 {
28 return contentsRect;
29 }
30 else if ( subControl == QskTextLabel::Text )
31 {
32 const auto label = static_cast< const QskTextLabel* >( skinnable );
33
34 if ( label->hasPanel() )
35 {
36 return label->subControlContentsRect(
37 contentsRect, QskTextLabel::Panel );
38 }
39
40 return contentsRect;
41 }
42
43 return Inherited::subControlRect( skinnable, contentsRect, subControl );
44}
45
46QSGNode* QskTextLabelSkinlet::updateSubNode(
47 const QskSkinnable* skinnable, quint8 nodeRole, QSGNode* node ) const
48{
49 const auto label = static_cast< const QskTextLabel* >( skinnable );
50
51 switch ( nodeRole )
52 {
53 case PanelRole:
54 {
55 if ( !label->hasPanel() )
56 return nullptr;
57
58 return updateBoxNode( label, node, QskTextLabel::Panel );
59 }
60 case TextRole:
61 {
62 return updateTextNode( label, node,
63 label->text(), QskTextLabel::Text );
64 }
65 }
66
67 return Inherited::updateSubNode( skinnable, nodeRole, node );
68}
69
70QSizeF QskTextLabelSkinlet::sizeHint( const QskSkinnable* skinnable,
71 Qt::SizeHint which, const QSizeF& constraint ) const
72{
73 if ( which != Qt::PreferredSize )
74 return QSizeF();
75
76 const auto label = static_cast< const QskTextLabel* >( skinnable );
77
78 const auto font = label->effectiveFont( QskTextLabel::Text );
79
80 auto textOptions = label->textOptions();
81 textOptions.setFormat( label->effectiveTextFormat() );
82
83 const auto text = label->text();
84
85 QSizeF hint;
86
87 const qreal lineHeight = QFontMetricsF( font ).height();
88
89 if ( text.isEmpty() )
90 {
91 if ( constraint.height() < 0.0 )
92 hint.setHeight( qCeil( lineHeight ) );
93 }
94 else if ( constraint.width() >= 0.0 )
95 {
96 if ( textOptions.effectiveElideMode() != Qt::ElideNone )
97 {
98 hint.setHeight( qCeil( lineHeight ) );
99 }
100 else
101 {
102 /*
103 In case of QskTextOptions::NoWrap we could count
104 the line numbers and calculate the height from
105 lineHeight. TODO ...
106 */
107 qreal maxHeight = std::numeric_limits< qreal >::max();
108 if ( maxHeight / lineHeight > textOptions.maximumLineCount() )
109 {
110 // be careful with overflows
111 maxHeight = textOptions.maximumLineCount() * lineHeight;
112 }
113
114 QSizeF size( constraint.width(), maxHeight );
115
116 size = QskTextRenderer::textSize( text, font, textOptions, size );
117
118 if ( label->hasPanel() )
119 size = label->outerBoxSize( QskTextLabel::Panel, size );
120
121 hint.setHeight( qCeil( size.height() ) );
122 }
123 }
124 else if ( constraint.height() >= 0.0 )
125 {
126 const qreal maxWidth = std::numeric_limits< qreal >::max();
127
128 QSizeF size( maxWidth, constraint.height() );
129
130 size = QskTextRenderer::textSize( text, font, textOptions, size );
131
132 if ( label->hasPanel() )
133 size = label->outerBoxSize( QskTextLabel::Panel, size );
134
135 hint.setWidth( qCeil( size.width() ) );
136 }
137 else
138 {
139 hint = QskTextRenderer::textSize( text, font, textOptions );
140
141 if ( label->hasPanel() )
142 hint = label->outerBoxSize( QskTextLabel::Panel, hint );
143 }
144
145 return hint;
146}
147
148#include "moc_QskTextLabelSkinlet.cpp"
Subcontrol
For use within the rendering or lay-outing of a specific QskSkinnable.
Definition QskAspect.h:104
QRectF subControlContentsRect(QskAspect::Subcontrol) const
QFont effectiveFont(QskAspect) const