Debian Codegraph @

src/imports/Components/1.3/Captions.qml

raw
1 /*
2 * Copyright 2015 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 import QtQuick 2.4
18 import QtQuick.Layouts 1.1
19 import Lomiri.Components 1.3
20
21 /*!
22 \qmltype Captions
23 \inqmlmodule Lomiri.Components
24 \ingroup lomiri-listitem
25 \since Lomiri.Components 1.2
26 \brief Container providing captionStyles for a twin-label column that can be used in
27 RowLayout or GridLayout.
28
29 The labels are placed in a column and can be accessed through \l title and
30 \l subtitle properties. The default spacing between the labels is 0.5 grid units.
31
32 The container only shows the labels which has its text property set to a valid
33 string. The labels not having any text set are not occupying the space. When
34 embedded in a positioner or in a RowLayout, the container is aligned vertically
35 centered. It is recommended to be used in context with RowLayout or GridLayout,
36 however can be used with any component.
37
38 \qml
39 import QtQuick 2.4
40 import Lomiri.Components 1.3
41
42 ListItem {
43 RowLayout {
44 Captions {
45 title.text: "Caption"
46 subtitle.text: "Subtitle text"
47 // attached properties of an Item type property
48 // can be changed only with assignment
49 Component.onCompleted: subtitle.Layout.alignment = Qt.AlignRight
50 }
51 Captions {
52 captionStyle: Lomiri.SummaryCaptionStyle
53 title.text: "Text"
54 subtitle.text: "Text"
55 }
56 }
57 }
58 \endqml
59
60 Additional items can also be added to the layout after the two labels.
61 \qml
62 Captions {
63 title.text: "Caption"
64 subtitle.text: "Subtitle"
65 Label {
66 text: "third line"
67 textSize: Label.XxSmall
68 }
69 }
70 \endqml
71 */
72 ColumnLayout {
73 id: captions
74
75 /*!
76 \qmlproperty enumeration captionStyle
77 The property configures the arrangement and font sizes of the Labels in the
78 component. It can take the following values:
79 \list
80 \li \b Lomiri.TitleCaptionStyle - (default) typical configuration for a left aligned
81 twin-label setup, where the text covers the remaining area on a list
82 layout.
83 \li \b Lomiri.SummaryCaptionStyle - configuration for a right-aligned twin label setup,
84 with 6 grid units width.
85 \endlist
86 */
87 property int captionStyle: Lomiri.TitleCaptionStyle
88
89 /*!
90 \qmlproperty Label title
91 \readonly
92 Label occupying the top area of the container.
93 */
94 property alias title: titleLabel
95
96 /*!
97 \qmlproperty Label subtitle
98 \readonly
99 Label occupying the bottom area of the container.
100 */
101 property alias subtitle: subtitleLabel
102
103 clip: true
104 spacing: units.gu(0.5)
105 Layout.fillWidth: (captionStyle == Lomiri.TitleCaptionStyle)
106 Layout.alignment: Qt.AlignVCenter | ((captionStyle == Lomiri.SummaryCaptionStyle) ? Qt.AlignRight : Qt.AlignLeft)
107 Layout.minimumWidth: 0
108 Layout.maximumWidth: (captionStyle == Lomiri.SummaryCaptionStyle) ? units.gu(6) : parent.width
109 Layout.preferredWidth: (captionStyle == Lomiri.SummaryCaptionStyle) ? Layout.maximumWidth : 0
110 Layout.minimumHeight: 0
111 Layout.maximumHeight: parent ? parent.height : childrenRect.height
112 Layout.preferredHeight: childrenRect.height
113
114 // handle visibility, do not override visible property!
115 // 'hide' column if title and subtitle is an empty string and
116 // there's no more children added
117 states: State {
118 name: "__q_invisible"
119 when: (title.text === "" && subtitle.text === "") && (captions.children.length == 2)
120 PropertyChanges {
121 target: captions
122 visible: false
123 }
124 }
125
126 Label {
127 Layout.fillWidth: true
128 id: titleLabel
129 textSize: (captionStyle == Lomiri.SummaryCaptionStyle) ? Label.Small : Label.Medium
130 horizontalAlignment: (captionStyle == Lomiri.SummaryCaptionStyle) ? Text.AlignRight : Text.AlignLeft
131 visible: text !== ""
132 elide: (captionStyle == Lomiri.TitleCaptionStyle) ? Text.ElideRight : Text.ElideNone
133 }
134 Label {
135 Layout.fillWidth: true
136 id: subtitleLabel
137 textSize: Label.Small
138 horizontalAlignment: (captionStyle == Lomiri.SummaryCaptionStyle) ? Text.AlignRight : Text.AlignLeft
139 visible: text !== ""
140 maximumLineCount: (captionStyle == Lomiri.TitleCaptionStyle) ? 2 : 1
141 wrapMode: (captionStyle == Lomiri.TitleCaptionStyle) ? Text.Wrap : Text.NoWrap
142 elide: (captionStyle == Lomiri.TitleCaptionStyle) ? Text.ElideRight : Text.ElideNone
143 }
144 }