1 | // Copyright (C) 2020 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "qquickstyleitemtextfield.h" |
5 | |
6 | QT_BEGIN_NAMESPACE |
7 | |
8 | QFont QQuickStyleItemTextField::styleFont(QQuickItem *control) const |
9 | { |
10 | return style()->font(element: QStyle::CE_ComboBoxLabel, state: controlSize(item: control)); |
11 | } |
12 | |
13 | void QQuickStyleItemTextField::connectToControl() const |
14 | { |
15 | QQuickStyleItem::connectToControl(); |
16 | auto textField = control<QQuickTextField>(); |
17 | connect(sender: textField, signal: &QQuickTextField::readOnlyChanged, context: this, slot: &QQuickStyleItem::markImageDirty); |
18 | connect(sender: textField, signal: &QQuickTextField::focusChanged, context: this, slot: &QQuickStyleItem::markImageDirty); |
19 | } |
20 | |
21 | StyleItemGeometry QQuickStyleItemTextField::calculateGeometry() |
22 | { |
23 | QStyleOptionFrame styleOption; |
24 | initStyleOption(styleOption); |
25 | StyleItemGeometry geometry; |
26 | |
27 | geometry.minimumSize = style()->sizeFromContents(ct: QStyle::CT_LineEdit, opt: &styleOption, contentsSize: QSize(0, 0)); |
28 | |
29 | // Inspired by QLineEdit::sizeHint() |
30 | QFontMetricsF fm(styleFont(control: const_cast<QQuickItem*>(control<QQuickItem>()))); |
31 | const QSize sz(qCeil(v: fm.horizontalAdvance(QLatin1Char('x')) * 17), |
32 | contentSize().height()); |
33 | geometry.implicitSize = style()->sizeFromContents(ct: QStyle::CT_LineEdit, opt: &styleOption, contentsSize: sz); |
34 | |
35 | styleOption.rect = QRect(QPoint(0, 0), geometry.implicitSize); |
36 | geometry.layoutRect = styleOption.rect; |
37 | geometry.contentRect = style()->subElementRect(subElement: QStyle::SE_LineEditContents, option: &styleOption); |
38 | geometry.ninePatchMargins = style()->ninePatchMargins(ce: QStyle::CE_ShapedFrame, opt: &styleOption, imageSize: geometry.minimumSize); |
39 | geometry.focusFrameRadius = style()->pixelMetric(metric: QStyle::PM_TextFieldFocusFrameRadius, option: &styleOption); |
40 | |
41 | return geometry; |
42 | } |
43 | |
44 | void QQuickStyleItemTextField::paintEvent(QPainter *painter) const |
45 | { |
46 | QStyleOptionFrame styleOption; |
47 | initStyleOption(styleOption); |
48 | style()->drawPrimitive(pe: QStyle::PE_PanelLineEdit, opt: &styleOption, p: painter); |
49 | } |
50 | |
51 | void QQuickStyleItemTextField::initStyleOption(QStyleOptionFrame &styleOption) const |
52 | { |
53 | initStyleOptionBase(styleOption); |
54 | auto textField = control<QQuickTextField>(); |
55 | |
56 | styleOption.lineWidth = style()->pixelMetric(metric: QStyle::PM_DefaultFrameWidth, option: &styleOption); |
57 | styleOption.midLineWidth = 0; |
58 | styleOption.state |= QStyle::State_Sunken; |
59 | if (textField->isReadOnly()) |
60 | styleOption.state |= QStyle::State_ReadOnly; |
61 | } |
62 | |
63 | QT_END_NAMESPACE |
64 | |
65 | #include "moc_qquickstyleitemtextfield.cpp" |
66 | |