Warning, /plasma/plasma-workspace/kcms/users/src/ui/PicturesSheet.qml is written in an unsupported language. File is not indexed.

0001 /*
0002     SPDX-FileCopyrightText: 2019 Nicolas Fella <nicolas.fella@gmx.de>
0003     SPDX-FileCopyrightText: 2020 Carson Black <uhhadd@gmail.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 import QtQuick 2.15
0009 import QtQuick.Controls 2.15 as QQC2
0010 import QtQuick.Dialogs 6.3 as Dialogs
0011 import QtQuick.Layouts 1.15
0012 
0013 import org.kde.kirigami 2.20 as Kirigami
0014 
0015 Kirigami.OverlaySheet {
0016     id: picturesSheet
0017 
0018     title: i18nc("@title", "Change Avatar")
0019     required property Kirigami.Page usersDetailPage
0020 
0021     readonly property var colorPalette: [
0022         {"name": i18nc("@item:intable", "It's Nothing"),     "color": "transparent", "dark": false},
0023         {"name": i18nc("@item:intable", "Feisty Flamingo"),  "color": "#E93A9A", "dark": true},
0024         {"name": i18nc("@item:intable", "Dragon's Fruit"),   "color": "#E93D58", "dark": true},
0025         {"name": i18nc("@item:intable", "Sweet Potato"),     "color": "#E9643A", "dark": true},
0026         {"name": i18nc("@item:intable", "Ambient Amber"),    "color": "#EF973C", "dark": false},
0027         {"name": i18nc("@item:intable", "Sparkle Sunbeam"),  "color": "#E8CB2D", "dark": false},
0028         {"name": i18nc("@item:intable", "Lemon-Lime"),       "color": "#B6E521", "dark": false},
0029         {"name": i18nc("@item:intable", "Verdant Charm"),    "color": "#3DD425", "dark": false},
0030         {"name": i18nc("@item:intable", "Mellow Meadow"),    "color": "#00D485", "dark": false},
0031         {"name": i18nc("@item:intable", "Tepid Teal"),       "color": "#00D3B8", "dark": false},
0032         {"name": i18nc("@item:intable", "Plasma Blue"),      "color": "#3DAEE9", "dark": true},
0033         {"name": i18nc("@item:intable", "Pon Purple"),       "color": "#B875DC", "dark": true},
0034         {"name": i18nc("@item:intable", "Bajo Purple"),      "color": "#926EE4", "dark": true},
0035         {"name": i18nc("@item:intable", "Burnt Charcoal"),   "color": "#232629", "dark": true},
0036         {"name": i18nc("@item:intable", "Paper Perfection"), "color": "#EEF1F5", "dark": false},
0037         {"name": i18nc("@item:intable", "Cafétera Brown"),   "color": "#CB775A", "dark": false},
0038         {"name": i18nc("@item:intable", "Rich Hardwood"),    "color": "#6A250E", "dark": true}
0039     ]
0040 
0041     component PicturesGridLayout: GridLayout {
0042         rowSpacing: Kirigami.Units.smallSpacing
0043         columns: Math.floor((stackSwitcher.implicitWidth - (Kirigami.Units.gridUnit + Kirigami.Units.largeSpacing * 2)) / (Kirigami.Units.gridUnit * 6))
0044         columnSpacing: Kirigami.Units.smallSpacing
0045 
0046         Layout.fillWidth: true
0047         Layout.alignment: Qt.AlignHCenter
0048         Layout.leftMargin: Kirigami.Units.largeSpacing
0049         Layout.rightMargin: Kirigami.Units.largeSpacing
0050     }
0051 
0052     component PictureButton: QQC2.Button {
0053         Layout.preferredHeight: Kirigami.Units.gridUnit * 6
0054         Layout.preferredWidth: Layout.preferredHeight
0055         display: QQC2.AbstractButton.IconOnly
0056     }
0057 
0058     component HomeButton: PictureButton {
0059         focus: !stackSwitcher.busy
0060         text: i18nc("@action:button", "Go Back")
0061 
0062         ColumnLayout {
0063             anchors.centerIn: parent
0064 
0065             Kirigami.Icon {
0066                 width: Kirigami.Units.gridUnit * 4
0067                 height: Kirigami.Units.gridUnit * 4
0068                 source: "go-previous"
0069 
0070                 Layout.alignment: Qt.AlignHCenter
0071             }
0072         }
0073 
0074         onClicked: stackSwitcher.pop()
0075     }
0076 
0077     component InitialsButton: PictureButton {
0078         text: i18nc("@action:button", "Initials")
0079         property alias color: colorRectangle.color
0080         property alias headingColor: heading.color
0081 
0082         Rectangle {
0083             id: colorRectangle
0084 
0085             color: "transparent"
0086             anchors.fill: parent
0087             anchors.margins: Kirigami.Units.smallSpacing
0088 
0089             Kirigami.Heading {
0090                 id: heading
0091                 anchors.fill: parent
0092                 anchors.margins: Kirigami.Units.smallSpacing
0093                 font.pixelSize: Kirigami.Units.gridUnit * 4
0094                 fontSizeMode: Text.Fit
0095                 horizontalAlignment: Text.AlignHCenter
0096                 verticalAlignment: Text.AlignVCenter
0097                 text: kcm.initializeString(user.displayPrimaryName)
0098                 textFormat: Text.PlainText
0099                 Accessible.ignored: true
0100             }
0101         }
0102     }
0103 
0104     component IconButton: PictureButton {
0105         property alias color: colorRectangle.color
0106         property alias iconColor: icon.color
0107 
0108         Rectangle {
0109             id: colorRectangle
0110 
0111             color: "transparent"
0112             anchors.fill: parent
0113             anchors.margins: Kirigami.Units.smallSpacing
0114 
0115             Kirigami.Icon {
0116                 id: icon
0117                 source: "user-identity"
0118                 width: Kirigami.Units.gridUnit * 4
0119                 height: Kirigami.Units.gridUnit * 4
0120                 anchors.centerIn: parent
0121             }
0122         }
0123     }
0124 
0125     component MainPage: ColumnLayout {
0126         PicturesGridLayout {
0127             PictureButton {
0128                 id: openButton
0129 
0130                 text: i18nc("@action:button", "Choose File…")
0131 
0132                 contentItem: Item {
0133                     Dialogs.FileDialog {
0134                         id: fileDialog
0135                         title: i18nc("@title", "Choose a picture")
0136                         onAccepted: {
0137                             usersDetailPage.oldImage = usersDetailPage.user.face
0138                             usersDetailPage.user.face = fileDialog.selectedFile
0139                             usersDetailPage.overrideImage = true
0140                             picturesSheet.close()
0141                         }
0142                     }
0143 
0144                     ColumnLayout {
0145                         // Centering rather than filling is desired to keep the
0146                         // entire layout nice and tight when the text is short
0147                         anchors.centerIn: parent
0148                         spacing: 0 // the icon should bring its own
0149 
0150                         Kirigami.Icon {
0151                             id: openIcon
0152 
0153                             implicitWidth: Kirigami.Units.iconSizes.huge
0154                             implicitHeight: Kirigami.Units.iconSizes.huge
0155                             source: "document-open"
0156 
0157                             Layout.alignment: Qt.AlignHCenter
0158                         }
0159                         QQC2.Label {
0160                             text: openButton.text
0161                             textFormat: Text.PlainText
0162 
0163                             Layout.fillWidth: true
0164                             Layout.maximumWidth: Kirigami.Units.gridUnit * 5
0165                             Layout.maximumHeight: openButton.availableHeight - openIcon.height
0166                             horizontalAlignment: Text.AlignHCenter
0167                             verticalAlignment: Text.AlignBottom
0168                             fontSizeMode: Text.HorizontalFit
0169                             wrapMode: Text.Wrap
0170                             elide: Text.ElideRight
0171                         }
0172                     }
0173                 }
0174 
0175                 onClicked: fileDialog.open()
0176             }
0177 
0178             InitialsButton {
0179                 onClicked: stackSwitcher.push(initialsPage)
0180             }
0181 
0182             IconButton {
0183                 text: i18nc("@action:button", "Placeholder Icon")
0184                 iconColor: "black"
0185                 onClicked: stackSwitcher.push(iconsPage)
0186             }
0187 
0188             Repeater {
0189                 model: kcm.avatarFiles
0190                 PictureButton {
0191                     id: delegate
0192 
0193                     required property string modelData
0194 
0195                     readonly property url source: "file:" + modelData
0196 
0197                     hoverEnabled: true
0198 
0199                     QQC2.ToolTip.delay: Kirigami.Units.toolTipDelay
0200                     QQC2.ToolTip.text: modelData
0201                     QQC2.ToolTip.visible: hovered || activeFocus
0202                     text: modelData
0203 
0204                     Kirigami.ShadowedImage {
0205                         id: imgDelegate
0206                         radius: width
0207                         anchors.centerIn: parent
0208                         width: Kirigami.Units.gridUnit * 5
0209                         height: Kirigami.Units.gridUnit * 5
0210                         sourceSize.width: width
0211                         sourceSize.height: height
0212                         asynchronous: true
0213                         mipmap: true
0214                         source: delegate.source
0215                     }
0216 
0217                     onClicked: {
0218                         usersDetailPage.oldImage = usersDetailPage.user.face
0219                         usersDetailPage.user.face = delegate.source
0220                         usersDetailPage.overrideImage = true
0221                         picturesSheet.close()
0222                     }
0223                 }
0224             }
0225         }
0226     }
0227 
0228     component InitialsPage: ColumnLayout {
0229         PicturesGridLayout {
0230             HomeButton {}
0231 
0232             Repeater {
0233                 model: picturesSheet.colorPalette
0234                 delegate: InitialsButton {
0235                     color: modelData.color
0236                     headingColor: modelData.dark ? "white" : "black"
0237                     hoverEnabled: true
0238 
0239                     text: modelData.name
0240                     QQC2.ToolTip.delay: Kirigami.Units.toolTipDelay
0241                     QQC2.ToolTip.text: modelData.name
0242                     QQC2.ToolTip.visible: hovered || activeFocus
0243 
0244                     onClicked: {
0245                         grabToImage(function(result) {
0246                             const uri = kcm.plonkImageInTempfile(result.image)
0247                             if (uri != "") {
0248                                 usersDetailPage.oldImage = usersDetailPage.user.face
0249                                 usersDetailPage.user.face = uri
0250                                 usersDetailPage.overrideImage = true
0251                             }
0252                             picturesSheet.close()
0253                         })
0254                     }
0255                 }
0256             }
0257         }
0258     }
0259 
0260     component IconsPage: ColumnLayout {
0261         PicturesGridLayout {
0262             HomeButton {}
0263 
0264             Repeater {
0265                 model: picturesSheet.colorPalette
0266                 delegate: IconButton {
0267                     text: modelData.name
0268                     color: modelData.color
0269                     iconColor: modelData.dark ? "white" : "black"
0270 
0271                     Accessible.description: i18nc("@info:whatsthis", "User avatar placeholder icon")
0272 
0273                     onClicked: {
0274                         grabToImage(function(result) {
0275                             const uri = kcm.plonkImageInTempfile(result.image)
0276                             if (uri != "") {
0277                                 usersDetailPage.oldImage = usersDetailPage.user.face
0278                                 usersDetailPage.user.face = uri
0279                                 usersDetailPage.overrideImage = true
0280                             }
0281                             picturesSheet.close()
0282                         })
0283                     }
0284                 }
0285             }
0286         }
0287     }
0288 
0289     readonly property MainPage mainPage: MainPage {}
0290     readonly property InitialsPage initialsPage: InitialsPage {}
0291     readonly property IconsPage iconsPage: IconsPage {}
0292 
0293     onClosed: {
0294         destroy();
0295     }
0296 
0297     QQC2.StackView {
0298         id: stackSwitcher
0299 
0300         implicitWidth: usersDetailPage.width - Kirigami.Units.largeSpacing * 4
0301         implicitHeight: currentItem.implicitHeight
0302 
0303         focus: true
0304         initialItem: mainPage
0305 
0306         Keys.onEscapePressed: picturesSheet.close();
0307     }
0308 
0309     Component.onCompleted: open()
0310 }