chat.qml 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright (C) 2021 The Qt Company Ltd.
  2. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
  3. import QtQuick
  4. import QtQuick.Layouts
  5. import QtQuick.Controls
  6. import ChatModel
  7. ApplicationWindow {
  8. id: window
  9. title: qsTr("Chat")
  10. width: 640
  11. height: 960
  12. visible: true
  13. SqlConversationModel {
  14. id: chat_model
  15. }
  16. ColumnLayout {
  17. anchors.fill: parent
  18. ListView {
  19. id: listView
  20. Layout.fillWidth: true
  21. Layout.fillHeight: true
  22. Layout.margins: pane.leftPadding + messageField.leftPadding
  23. displayMarginBeginning: 40
  24. displayMarginEnd: 40
  25. verticalLayoutDirection: ListView.BottomToTop
  26. spacing: 12
  27. model: chat_model
  28. delegate: Column {
  29. anchors.right: sentByMe ? listView.contentItem.right : undefined
  30. spacing: 6
  31. readonly property bool sentByMe: model.recipient !== "Me"
  32. Row {
  33. id: messageRow
  34. spacing: 6
  35. anchors.right: sentByMe ? parent.right : undefined
  36. Rectangle {
  37. width: Math.min(messageText.implicitWidth + 24,
  38. listView.width - (!sentByMe ? messageRow.spacing : 0))
  39. height: messageText.implicitHeight + 24
  40. radius: 15
  41. color: sentByMe ? "lightgrey" : "steelblue"
  42. Label {
  43. id: messageText
  44. text: model.message
  45. color: sentByMe ? "black" : "white"
  46. anchors.fill: parent
  47. anchors.margins: 12
  48. wrapMode: Label.Wrap
  49. }
  50. }
  51. }
  52. Label {
  53. id: timestampText
  54. text: Qt.formatDateTime(model.timestamp, "d MMM hh:mm")
  55. color: "lightgrey"
  56. anchors.right: sentByMe ? parent.right : undefined
  57. }
  58. }
  59. ScrollBar.vertical: ScrollBar {}
  60. }
  61. Pane {
  62. id: pane
  63. Layout.fillWidth: true
  64. RowLayout {
  65. width: parent.width
  66. TextArea {
  67. id: messageField
  68. Layout.fillWidth: true
  69. placeholderText: qsTr("Compose message")
  70. wrapMode: TextArea.Wrap
  71. }
  72. Button {
  73. id: sendButton
  74. text: qsTr("Send")
  75. enabled: messageField.length > 0
  76. onClicked: {
  77. listView.model.send_message("machine", messageField.text, "Me");
  78. messageField.text = "";
  79. }
  80. }
  81. }
  82. }
  83. }
  84. }