QSkinny 0.8.0
C++/Qt UI toolkit
Loading...
Searching...
No Matches
QskTextLabel.cpp
1/******************************************************************************
2 * QSkinny - Copyright (C) The authors
3 * SPDX-License-Identifier: BSD-3-Clause
4 *****************************************************************************/
5
6#include "QskTextLabel.h"
7#include "QskAspect.h"
8#include "QskTextOptions.h"
9#include "QskFontRole.h"
10
11QSK_SUBCONTROL( QskTextLabel, Panel )
12QSK_SUBCONTROL( QskTextLabel, Text )
13
14class QskTextLabel::PrivateData
15{
16 public:
17 PrivateData( const QString& txt, QskTextOptions::TextFormat textFormat )
18 : text( txt )
19 , effectiveTextFormat( textFormat )
20 , hasPanel( false )
21 {
22 }
23
24 QString text;
25
26 mutable QskTextOptions::TextFormat effectiveTextFormat;
27
28 bool hasPanel : 1;
29};
30
31QskTextLabel::QskTextLabel( QQuickItem* parent )
32 : QskTextLabel( QString(), parent )
33{
34}
35
36QskTextLabel::QskTextLabel( const QString& text, QQuickItem* parent )
37 : Inherited( parent )
38 , m_data( new PrivateData( text, textOptions().format() ) )
39{
40 initSizePolicy( QskSizePolicy::Minimum, QskSizePolicy::Fixed );
41}
42
43QskTextLabel::~QskTextLabel()
44{
45}
46
47void QskTextLabel::setPanel( bool on )
48{
49 if ( on == m_data->hasPanel )
50 return;
51
52 m_data->hasPanel = on;
53
55 update();
56
57 Q_EMIT panelChanged( on );
58}
59
60bool QskTextLabel::hasPanel() const
61{
62 return m_data->hasPanel;
63}
64
65void QskTextLabel::setText( const QString& text )
66{
67 if ( text == m_data->text )
68 return;
69
70 m_data->text = text;
71 m_data->effectiveTextFormat = textOptions().format();
72
74 update();
75
76 Q_EMIT textChanged( text );
77}
78
79QString QskTextLabel::text() const
80{
81 return m_data->text;
82}
83
84void QskTextLabel::setTextOptions( const QskTextOptions& textOptions )
85{
86 if ( setTextOptionsHint( Text, textOptions ) )
87 {
88#if 0
89 // we are killing user settings of the policy this way ??
90
91 const QskSizePolicy::Policy policy = ( options.wrapMode() == QTextOption::NoWrap )
92 ? QskSizePolicy::Minimum : QskSizePolicy::Preferred;
93
94 setSizePolicy( policy, sizePolicy().verticalPolicy() );
95#endif
96
97 m_data->effectiveTextFormat = textOptions.format();
98 Q_EMIT textOptionsChanged( textOptions );
99 }
100}
101
102QskTextOptions QskTextLabel::textOptions() const
103{
104 return textOptionsHint( Text );
105}
106
107void QskTextLabel::resetTextOptions()
108{
109 if ( resetTextOptionsHint( Text ) )
110 Q_EMIT textOptionsChanged( textOptions() );
111}
112
113void QskTextLabel::setTextFormat( QskTextOptions::TextFormat format )
114{
115 auto options = textOptions();
116 options.setFormat( format );
117
118 setTextOptions( options );
119}
120
121QskTextOptions::TextFormat QskTextLabel::textFormat() const
122{
123 return textOptions().format();
124}
125
126QskTextOptions::TextFormat QskTextLabel::effectiveTextFormat() const
127{
128 const auto options = textOptions();
129
130 if ( options.format() != QskTextOptions::AutoText )
131 return options.format();
132
133 if ( m_data->effectiveTextFormat == QskTextOptions::AutoText )
134 m_data->effectiveTextFormat = options.effectiveFormat( m_data->text );
135
136 return m_data->effectiveTextFormat;
137}
138
139void QskTextLabel::setWrapMode( QskTextOptions::WrapMode wrapMode )
140{
141 auto options = textOptions();
142 options.setWrapMode( wrapMode );
143
144 setTextOptions( options );
145}
146
147QskTextOptions::WrapMode QskTextLabel::wrapMode() const
148{
149 return textOptions().wrapMode();
150}
151
152void QskTextLabel::setElideMode( Qt::TextElideMode elideMode )
153{
154 auto options = textOptions();
155 options.setElideMode( elideMode );
156
157 setTextOptions( options );
158}
159
160Qt::TextElideMode QskTextLabel::elideMode() const
161{
162 return textOptions().elideMode();
163}
164
165void QskTextLabel::setFontRole( const QskFontRole& role )
166{
167 if ( setFontRoleHint( Text, role ) )
168 Q_EMIT fontRoleChanged( role );
169}
170
171void QskTextLabel::resetFontRole()
172{
173 if ( resetFontRoleHint( Text ) )
174 Q_EMIT fontRoleChanged( fontRoleHint( Text ) );
175}
176
177QskFontRole QskTextLabel::fontRole() const
178{
179 return fontRoleHint( Text );
180}
181
182void QskTextLabel::setTextColor( const QColor& color )
183{
184 if ( setColor( Text, color ) )
185 Q_EMIT textColorChanged( color );
186}
187
188void QskTextLabel::resetTextColor()
189{
190 if ( resetColor( Text ) )
191 Q_EMIT textColorChanged( color( Text ) );
192}
193
194QColor QskTextLabel::textColor() const
195{
196 return color( Text );
197}
198
199void QskTextLabel::setAlignment( Qt::Alignment alignment )
200{
201 if ( setAlignmentHint( Text, alignment ) )
202 Q_EMIT alignmentChanged( alignment );
203}
204
205void QskTextLabel::resetAlignment()
206{
207 if ( resetAlignmentHint( Text ) )
208 Q_EMIT alignmentChanged( alignment() );
209}
210
211Qt::Alignment QskTextLabel::alignment() const
212{
213 return alignmentHint( Text, Qt::AlignLeft | Qt::AlignTop );
214}
215
216QFont QskTextLabel::font() const
217{
218 return effectiveFont( QskTextLabel::Text );
219}
220
221void QskTextLabel::changeEvent( QEvent* event )
222{
223 switch ( event->type() )
224 {
225 case QEvent::LocaleChange:
226 {
227 if ( !m_data->text.isEmpty() )
228 {
229 // maybe Qt::LayoutDirection has changed
230 update();
231 }
232
233 break;
234 }
235 default:
236 break;
237 }
238
239 Inherited::changeEvent( event );
240}
241
242#include "moc_QskTextLabel.cpp"
void setSizePolicy(QskSizePolicy)
QskSizePolicy sizePolicy
Definition QskControl.h:43
void resetImplicitSize()
Definition QskItem.cpp:721
virtual void changeEvent(QEvent *)
Definition QskItem.cpp:859
bool resetColor(QskAspect)
Removes a color hint from the local table.
bool setAlignmentHint(QskAspect, Qt::Alignment)
Sets an alignment hint.
bool resetFontRoleHint(QskAspect)
Removes a font role hint from the local table.
bool setColor(QskAspect, Qt::GlobalColor)
Sets a color hint.
QFont effectiveFont(QskAspect) const
bool setFontRoleHint(QskAspect, const QskFontRole &)
Sets a font role hint.
QskFontRole fontRoleHint(QskAspect, QskSkinHintStatus *=nullptr) const
Retrieves a font role hint.
bool resetAlignmentHint(QskAspect)
Removes an alignment hint from the local table.
QColor color(QskAspect, QskSkinHintStatus *=nullptr) const
Retrieves a color hint.
Qt::Alignment alignmentHint(QskAspect, Qt::Alignment=Qt::Alignment()) const
Retrieves an alignment hint.
void changeEvent(QEvent *) override