Warning, /libraries/kirigami-addons/src/formcard/FormGridContainer.qml is written in an unsupported language. File is not indexed.

0001 // SPDX-FileCopyrightText: 2022 Devin Lin <devin@kde.org>
0002 // SPDX-FileCopyrightText: 2023 Rishi Kumar <rsi.dev17@gmail.com>
0003 // SPDX-FileCopyrightText: 2023 Carl Schwan <carl@carlschwan.eu>
0004 // SPDX-License-Identifier: LGPL-2.0-or-later
0005 
0006 import QtQml 2.15
0007 import QtQuick 2.15
0008 import QtQuick.Controls 2.15 as QQC2
0009 import QtQuick.Layouts 1.15
0010 
0011 import org.kde.kirigami 2.19 as Kirigami
0012 
0013 import "private" as Private
0014 
0015 /**
0016  * This component render a grid of small cards.
0017  *
0018  * This is used to display multiple information in a MobileForm.FormLayout
0019  * without taking too much vertical space.
0020  *
0021  * @code{.qml}
0022  * import org.kde.kirigamiaddons.labs.mobileform 0.1 as MobileForm
0023  *
0024  * MobileForm.FormGridContainer {
0025  *     id: container
0026  *
0027  *     Layout.topMargin: Kirigami.Units.largeSpacing
0028  *     Layout.fillWidth: true
0029  *
0030  *     infoCards: [
0031  *         MobileForm.FormGridContainer.InfoCard {
0032  *             title: "42"
0033  *             subtitle: i18nc("@info:Number of Posts", "Posts")
0034  *         },
0035  *         MobileForm.FormGridContainer.InfoCard {
0036  *             title: "42"
0037  *             subtitle: i18nc("@info:Number of followers.", "Followers")
0038  *         }
0039  *     ]
0040  * }
0041  * @endcode
0042  *
0043  *
0044  * @since KirigamiAddons 0.11.0
0045  *
0046  * @inherit QtQuick.Item
0047  */
0048 Item {
0049     id: root
0050 
0051     /**
0052      * This property holds the maximum width of the grid.
0053      */
0054     property real maximumWidth: Kirigami.Units.gridUnit * 30
0055 
0056     /**
0057      * @brief This property holds the padding used around the content edges.
0058      *
0059      * default: `0`
0060      */
0061     property real padding: 0
0062     property real verticalPadding: padding
0063     property real horizontalPadding: padding
0064     property real topPadding: verticalPadding
0065     property real bottomPadding: verticalPadding
0066     property real leftPadding: horizontalPadding
0067     property real rightPadding: horizontalPadding
0068 
0069     /**
0070      * This property holds whether the card's width is being restricted.
0071      */
0072     readonly property bool cardWidthRestricted: root.width > root.maximumWidth
0073 
0074     /**
0075      * This property holds the InfoCards which should be displayed.
0076      *
0077      * Each InfoCard contains a title and an optional subtitle
0078      *
0079      * @code{.qml}
0080      * import org.kde.kirigamiaddons.labs.mobileform 0.1 as MobileForm
0081      *
0082      * MobileForm.FormGridContainer {
0083      *     infoCards: [
0084      *         MobileForm.FormGridContainer.InfoCard {
0085      *             title: "42"
0086      *             subtitle: i18nc("@info:Number of Posts", "Posts")
0087      *         },
0088      *         MobileForm.FormGridContainer.InfoCard {
0089      *             title: "42"
0090      *         },
0091      *         MobileForm.FormGridContainer.InfoCard {
0092      *             title: "Details"
0093      *             action: Kirigami.Action {
0094      *                 onClicked: pageStack.push("Details.qml")
0095      *             }
0096      *         }
0097      *     ]
0098      * }
0099      * @endcode
0100      */
0101     property list<InfoCard> infoCards
0102 
0103     component InfoCard: QtObject {
0104         property string title
0105         property string subtitle
0106         property Kirigami.Action action
0107     }
0108 
0109     Kirigami.Theme.colorSet: Kirigami.Theme.View
0110     Kirigami.Theme.inherit: false
0111 
0112     implicitHeight: topPadding + bottomPadding + grid.implicitHeight
0113 
0114     Item {
0115         anchors {
0116             top: parent.top
0117             bottom: parent.bottom
0118             left: parent.left
0119             right: parent.right
0120 
0121             leftMargin: root.cardWidthRestricted ? Math.round((root.width - root.maximumWidth) / 2) : 0
0122             rightMargin: root.cardWidthRestricted ? Math.round((root.width - root.maximumWidth) / 2) : 0
0123         }
0124 
0125         GridLayout {
0126             id: grid
0127 
0128             readonly property int cellWidth: Kirigami.Units.gridUnit * 10
0129 
0130             anchors {
0131                 fill: parent
0132                 leftMargin: root.leftPadding
0133                 rightMargin: root.rightPadding
0134                 topMargin: root.topPadding
0135                 bottomMargin: root.bottomPadding
0136             }
0137 
0138             columns: root.cardWidthRestricted && cardRepeater.count % 3 === 0 ? 3 : 2
0139             columnSpacing: Kirigami.Units.smallSpacing
0140             rowSpacing: Kirigami.Units.smallSpacing
0141 
0142             Repeater {
0143                 id: cardRepeater
0144 
0145                 model: root.infoCards
0146 
0147                 QQC2.AbstractButton {
0148                     id: infoCardDelegate
0149 
0150                     required property int index
0151                     required property QtObject modelData
0152 
0153                     readonly property string title: modelData.title
0154                     readonly property string subtitle: modelData.subtitle
0155 
0156                     action: modelData.action
0157 
0158                     leftPadding: Kirigami.Units.largeSpacing
0159                     rightPadding: Kirigami.Units.largeSpacing
0160                     topPadding: Kirigami.Units.largeSpacing
0161                     bottomPadding: Kirigami.Units.largeSpacing
0162 
0163                     leftInset: root.cardWidthRestricted ? 0 : -infoCardDelegate.background.border.width
0164                     rightInset: root.cardWidthRestricted ? 0 : -infoCardDelegate.background.border.width
0165 
0166                     hoverEnabled: true
0167 
0168                     Accessible.name: title + " " + subtitle
0169                     Accessible.role: action ? Accessible.Button : Accessible.Note
0170 
0171                     Layout.preferredWidth: grid.cellWidth
0172                     Layout.columnSpan: cardRepeater.count % grid.columns !== 0 && index === cardRepeater.count - 1 ? 2 : 1
0173                     Layout.fillWidth: true
0174                     Layout.fillHeight: true
0175 
0176                     background: Rectangle {
0177                         radius: root.cardWidthRestricted ? Kirigami.Units.smallSpacing : 0
0178                         color: Kirigami.Theme.backgroundColor
0179 
0180                         border {
0181                             color: Kirigami.ColorUtils.linearInterpolation(Kirigami.Theme.backgroundColor, Kirigami.Theme.textColor, 0.15)
0182                             width: 1
0183                         }
0184 
0185                         Rectangle {
0186                             anchors.fill: parent
0187                             radius: root.cardWidthRestricted ? Kirigami.Units.smallSpacing : 0
0188 
0189                             color: {
0190                                 let alpha = 0;
0191 
0192                                 if (!infoCardDelegate.enabled || !infoCardDelegate.action) {
0193                                     alpha = 0;
0194                                 } else if (infoCardDelegate.pressed) {
0195                                     alpha = 0.2;
0196                                 } else if (infoCardDelegate.visualFocus) {
0197                                     alpha = 0.1;
0198                                 } else if (!Kirigami.Settings.tabletMode && infoCardDelegate.hovered) {
0199                                     alpha = 0.07;
0200                                 }
0201 
0202                                 return Qt.rgba(Kirigami.Theme.textColor.r, Kirigami.Theme.textColor.g, Kirigami.Theme.textColor.b, alpha)
0203                             }
0204 
0205                             Behavior on color {
0206                                 ColorAnimation { duration: Kirigami.Units.shortDuration }
0207                             }
0208                         }
0209                     }
0210 
0211                     contentItem: ColumnLayout {
0212                         spacing: 0
0213 
0214                         // Title
0215                         Kirigami.Heading {
0216                             Layout.fillWidth: true
0217                             level: 4
0218                             text: infoCardDelegate.title
0219                             verticalAlignment: Text.AlignVCenter
0220                             horizontalAlignment: Text.AlignHCenter
0221                             maximumLineCount: 2
0222                             elide: Text.ElideRight
0223                             wrapMode: Text.Wrap
0224                         }
0225 
0226                         // Subtitle
0227                         QQC2.Label {
0228                             Layout.fillWidth: true
0229                             Layout.fillHeight: true
0230                             visible: infoCardDelegate.subtitle
0231                             text: infoCardDelegate.subtitle
0232                             horizontalAlignment: Text.AlignHCenter
0233                             elide: Text.ElideRight
0234                             wrapMode: Text.Wrap
0235                             opacity: 0.6
0236                             verticalAlignment: Text.AlignTop
0237                         }
0238                     }
0239                 }
0240             }
0241         }
0242     }
0243 }