1/*
2    SPDX-FileCopyrightText: 2011 Marco Martin <mart@kde.org>
3    SPDX-FileCopyrightText: 2011 Nokia Corporation and /or its subsidiary(-ies) <qt-info@nokia.com>
4
5    This file is part of the Qt Components project.
6
7    SPDX-License-Identifier: BSD-3-Clause
8*/
9
10import QtQuick 2.1
11import org.kde.plasma.core 2.0 as PlasmaCore
12import "." 2.0 as PlasmaComponents
13
14/**
15 * CommonDialog is a convenience component that provides a dialog with the
16 * platform-style title area. You only have to define titleText. CommonDialog
17 * handles its layout automatically.
18 *
19 * Note: This component is experimental, so it may be changed or removed in
20 * future releases.
21 */
22PlasmaComponents.Dialog {
23    id: root
24
25    /** type:string the title of the dialog */
26    property alias titleText: titleAreaText.text
27
28    /** the name or path of the dialog title icon */
29    property string titleIcon
30
31    /** the texts of all the buttons */
32    property var buttonTexts: []
33
34    /**
35     * Emitted when the use clicks on a button
36     * @param index the index of the clicked button: buttonTexts[index] will hold the text of the clicked button.
37     */
38    signal buttonClicked(int index)
39
40    Accessible.role: Accessible.Dialog
41
42    onButtonTextsChanged: {
43        for (var i = buttonRow.children.length; i > 0; --i) {
44            buttonRow.children[i - 1].destroy()
45        }
46        for (var j = 0; j < buttonTexts.length; ++j) {
47            var button = buttonComponent.createObject(buttonRow)
48            button.text = buttonTexts[j]
49            button.index = j
50        }
51    }
52
53    Component {
54        id: buttonComponent
55        PlasmaComponents.Button {
56            property int index
57
58            onClicked: {
59                if (root.status == PlasmaComponents.DialogStatus.Open) {
60                    root.buttonClicked(index)
61                    root.close()
62                }
63            }
64        }
65    }
66
67    QtObject {
68        id: internal
69
70        /*function buttonWidth() {
71            switch (buttonTexts.length) {
72                case 0: return 0
73                case 1: return Math.round((800 - 3 * 4) / 2)
74                default: return (buttonContainer.width - (buttonTexts.length + 1) *
75                    4) / buttonTexts.length
76            }
77        }*/
78
79        function iconSource() {
80            return root.titleIcon
81        }
82    }
83
84    title: PlasmaCore.FrameSvgItem {
85        imagePath: "widgets/extender-dragger"
86        prefix: "root"
87        visible: titleAreaText.text != ""
88        anchors.left: parent.left
89        anchors.right: parent.right
90        //FIXME: +5 because of Plasma::Dialog margins
91        height: titleAreaText.paintedHeight + margins.top + margins.bottom
92
93        LayoutMirroring.childrenInherit: true
94
95        Column {
96            id: titleLayoutHelper // needed to make the text mirror correctly
97
98            anchors {
99                right: parent.right
100                left: titleAreaIcon.source == "" ? parent.left : titleAreaIcon.right
101                top: parent.top
102                bottom: parent.bottom
103                leftMargin: parent.margins.left
104                rightMargin: parent.margins.right
105                topMargin: parent.margins.top
106                bottomMargin: parent.margins.bottom
107            }
108
109            PlasmaComponents.Label {
110                id: titleAreaText
111                LayoutMirroring.enabled: root.LayoutMirroring.enabled
112                elide: Text.ElideRight
113                height: paintedHeight
114                anchors {
115                    left: parent.left
116                    right: parent.right
117                }
118                horizontalAlignment: Text.AlignHCenter
119                verticalAlignment: Text.AlignVCenter
120            }
121        }
122
123        PlasmaCore.IconItem {
124            id: titleAreaIcon
125            width: PlasmaCore.Units.iconSizes.small
126            height: PlasmaCore.Units.iconSizes.small
127            source: titleIcon
128            anchors.left: parent.left
129            anchors.rightMargin: 4
130            anchors.verticalCenter: parent.verticalCenter
131        }
132    }
133
134    buttons: Row {
135        id: buttonRow
136
137        LayoutMirroring.enabled: false
138        LayoutMirroring.childrenInherit: true
139        objectName: "buttonRow"
140        anchors.centerIn: parent
141        spacing: 4
142    }
143}
144