1/****************************************************************************
2**
3** Copyright (C) 2019 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the examples of Qt for Python.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40import QtQuick 2.12
41import QtQuick.Layouts 1.12
42import QtQuick.Controls 2.12
43
44ApplicationWindow {
45    id: window
46    title: qsTr("Chat")
47    width: 640
48    height: 960
49    visible: true
50    ColumnLayout {
51        anchors.fill: parent
52
53        ListView {
54            id: listView
55            Layout.fillWidth: true
56            Layout.fillHeight: true
57            Layout.margins: pane.leftPadding + messageField.leftPadding
58            displayMarginBeginning: 40
59            displayMarginEnd: 40
60            verticalLayoutDirection: ListView.BottomToTop
61            spacing: 12
62            model: chat_model
63            delegate: Column {
64                readonly property bool sentByMe: model.recipient !== "Me"
65                anchors.right: sentByMe ? parent.right : undefined
66                spacing: 6
67
68                Row {
69                    id: messageRow
70                    spacing: 6
71                    anchors.right: sentByMe ? parent.right : undefined
72
73                    Rectangle {
74                        width: Math.min(messageText.implicitWidth + 24, listView.width - messageRow.spacing)
75                        height: messageText.implicitHeight + 24
76                        radius: 15
77                        color: sentByMe ? "lightgrey" : "#ff627c"
78
79                        Label {
80                            id: messageText
81                            text: model.message
82                            color: sentByMe ? "black" : "white"
83                            anchors.fill: parent
84                            anchors.margins: 12
85                            wrapMode: Label.Wrap
86                        }
87                    }
88                }
89
90                Label {
91                    id: timestampText
92                    text: Qt.formatDateTime(model.timestamp, "d MMM hh:mm")
93                    color: "lightgrey"
94                    anchors.right: sentByMe ? parent.right : undefined
95                }
96            }
97
98            ScrollBar.vertical: ScrollBar {}
99        }
100
101        Pane {
102            id: pane
103            Layout.fillWidth: true
104
105            RowLayout {
106                width: parent.width
107
108                TextArea {
109                    id: messageField
110                    Layout.fillWidth: true
111                    placeholderText: qsTr("Compose message")
112                    wrapMode: TextArea.Wrap
113                }
114
115                Button {
116                    id: sendButton
117                    text: qsTr("Send")
118                    enabled: messageField.length > 0
119                    onClicked: {
120                        chat_model.send_message("machine", messageField.text, "Me");
121                        messageField.text = "";
122                    }
123                }
124            }
125        }
126    }
127}
128