QSkinny 0.8.0
C++/Qt UI toolkit
Loading...
Searching...
No Matches
QskTextFieldSkinlet.cpp
1/******************************************************************************
2 * QSkinny - Copyright (C) The authors
3 * SPDX-License-Identifier: BSD-3-Clause
4 *****************************************************************************/
5
6#include "QskTextFieldSkinlet.h"
7#include "QskTextField.h"
8
9using Q = QskTextField;
10
11QskTextFieldSkinlet::QskTextFieldSkinlet( QskSkin* skin )
12 : Inherited( skin )
13{
14 setNodeRoles( { PanelRole, TextPanelRole, PlaceholderRole } );
15}
16
17QskTextFieldSkinlet::~QskTextFieldSkinlet()
18{
19}
20
21QRectF QskTextFieldSkinlet::subControlRect( const QskSkinnable* skinnable,
22 const QRectF& contentsRect, QskAspect::Subcontrol subControl ) const
23{
24 if ( subControl == Q::Panel )
25 return contentsRect;
26
27 if ( subControl == Q::TextPanel )
28 return skinnable->subControlContentsRect( contentsRect, Q::Panel );
29
30 if ( subControl == Q::Text )
31 return skinnable->subControlContentsRect( contentsRect, Q::TextPanel );
32
33 if ( subControl == Q::Placeholder )
34 {
35 const auto textField = static_cast< const QskTextField* >( skinnable );
36 if( textField->text().isEmpty() )
37 return subControlRect( skinnable, contentsRect, Q::Text );
38
39 return QRectF();
40 }
41
42 return Inherited::subControlRect( skinnable, contentsRect, subControl );
43}
44
45QSGNode* QskTextFieldSkinlet::updateSubNode(
46 const QskSkinnable* skinnable, quint8 nodeRole, QSGNode* node ) const
47{
48 const auto textField = static_cast< const QskTextField* >( skinnable );
49
50 switch ( nodeRole )
51 {
52 case PanelRole:
53 {
54 return updateBoxNode( skinnable, node, Q::Panel );
55 }
56 case TextPanelRole:
57 {
58 return updateBoxNode( skinnable, node, Q::TextPanel );
59 }
60 case PlaceholderRole:
61 {
62 const auto text = effectivePlaceholderText( textField );
63 if ( text.isEmpty() )
64 return nullptr;
65
66 const auto subControl = Q::Placeholder;
67
68 QskSkinHintStatus status;
69
70 auto options = skinnable->textOptionsHint( subControl, &status );
71 if ( !status.isValid() )
72 options.setElideMode( Qt::ElideRight );
73
74 return updateTextNode( skinnable, node,
75 textField->subControlRect( subControl ),
76 textField->alignmentHint( subControl, Qt::AlignLeft ),
77 options, text, subControl );
78 }
79 }
80
81 return Inherited::updateSubNode( skinnable, nodeRole, node );
82}
83
84QSizeF QskTextFieldSkinlet::sizeHint( const QskSkinnable* skinnable,
85 Qt::SizeHint which, const QSizeF& constraint ) const
86{
87 Q_UNUSED( constraint ); // TODO ...
88
89 if ( which != Qt::PreferredSize )
90 return QSizeF();
91
92 const auto input = static_cast< const QskAbstractTextInput* >( skinnable );
93
94 auto hint = input->unwrappedTextSize();
95 hint = hint.grownBy( skinnable->marginHint( Q::Text ) );
96
97 hint = input->outerBoxSize( Q::TextPanel, hint );
98 hint = hint.expandedTo( input->strutSizeHint( Q::TextPanel ) );
99
100 hint = skinnable->outerBoxSize( Q::Panel, hint );
101 hint = hint.expandedTo( skinnable->strutSizeHint( Q::Panel ) );
102
103 return hint;
104}
105
106QString QskTextFieldSkinlet::effectivePlaceholderText(
107 const QskTextField* textField ) const
108{
109 if ( textField->text().isEmpty() &&
110 !( textField->isReadOnly() || textField->isEditing() ) )
111 {
112 return textField->placeholderText();
113 }
114
115 return QString();
116}
117
118#include "moc_QskTextFieldSkinlet.cpp"
Subcontrol
For use within the rendering or lay-outing of a specific QskSkinnable.
Definition QskAspect.h:104
bool isValid() const
QMarginsF marginHint(QskAspect, QskSkinHintStatus *=nullptr) const
Retrieves a margin hint.
QSizeF strutSizeHint(QskAspect, QskSkinHintStatus *=nullptr) const
Retrieves a strut size hint.
QSizeF outerBoxSize(QskAspect, const QSizeF &innerBoxSize) const
Calculate the size, when being expanded by paddings, indentations.
QRectF subControlContentsRect(const QRectF &, QskAspect::Subcontrol) const
Calculate the inner rectangle for subControl.