ChatView.qml 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import QtQuick 2.4
  2. import examples.grpc.simplechat 1.0
  3. import qtprotobuf.examples 1.0
  4. Rectangle {
  5. id: root
  6. anchors.fill: parent
  7. color: "#303030"
  8. onVisibleChanged: {
  9. if (visible) {
  10. _inputField.forceActiveFocus()
  11. }
  12. }
  13. MouseArea {
  14. anchors.fill: parent
  15. }
  16. ListView {
  17. anchors.top: parent.top
  18. anchors.bottom: _inputField.top
  19. anchors.left: parent.left
  20. anchors.right: parent.right
  21. model: SimpleChatEngine.messages
  22. delegate: Item {
  23. height: _imageWrapper.height + 10
  24. width: root.width
  25. Item {
  26. id: _imageWrapper
  27. height: _messageColumn.height + 20
  28. width: parent.width/2 - 20
  29. property bool ownMessage: SimpleChatEngine.userName === model.modelData.from
  30. anchors{
  31. right: _imageWrapper.ownMessage ? parent.right : undefined
  32. left: _imageWrapper.ownMessage ? undefined : parent.left
  33. rightMargin: _imageWrapper.ownMessage ? 10 : 0
  34. leftMargin: _imageWrapper.ownMessage ? 0 : 10
  35. verticalCenter: parent.verticalCenter
  36. }
  37. Rectangle {
  38. anchors.fill: parent
  39. radius: 20
  40. color: "#424242"
  41. border.color: _imageWrapper.ownMessage ? "#E91E63" : "#F48FB1"
  42. border.width: 1
  43. }
  44. Column {
  45. id: _messageColumn
  46. anchors {
  47. left: parent.left
  48. right: parent.right
  49. leftMargin: 10
  50. rightMargin: 10
  51. verticalCenter: parent.verticalCenter
  52. }
  53. Text {
  54. id: _userName
  55. property string from: _imageWrapper.ownMessage ? qsTr("You") : model.modelData.from
  56. anchors.left: parent.left
  57. anchors.right: parent.right
  58. font.pointSize: 12
  59. font.weight: Font.Bold
  60. color: "#ffffff"
  61. text: _userName.from + ": "
  62. }
  63. Loader {
  64. id: delegateLoader
  65. anchors.left: parent.left
  66. anchors.right: parent.right
  67. height: item ? item.height : 0
  68. sourceComponent: model.modelData.type === ChatMessage.Image ? imageDelegate : textDelegate
  69. onItemChanged: {
  70. if (item) {
  71. item.modelData = model.modelData
  72. }
  73. }
  74. }
  75. }
  76. }
  77. }
  78. onCountChanged: {
  79. positionViewAtEnd()
  80. }
  81. }
  82. Component {
  83. id: textDelegate
  84. Item {
  85. property var modelData: null
  86. height: childrenRect.height
  87. Text {
  88. anchors.left: parent.left
  89. anchors.right: parent.right
  90. height: implicitHeight
  91. font.pointSize: 12
  92. color: "#ffffff"
  93. wrapMode: Text.Wrap
  94. text: SimpleChatEngine.getText(modelData.content)
  95. }
  96. }
  97. }
  98. Component {
  99. id: imageDelegate
  100. Item {
  101. property var modelData: null
  102. height: childrenRect.height
  103. Image {
  104. width: implicitWidth
  105. height: implicitHeight
  106. source: SimpleChatEngine.getImageThumbnail(modelData.content)
  107. }
  108. }
  109. }
  110. ChatInputField {
  111. id: _inputField
  112. focus: true
  113. anchors {
  114. left: parent.left
  115. right: parent.right
  116. bottom: parent.bottom
  117. margins: 20
  118. }
  119. placeholderText: qsTr("Start type here or paste image")
  120. onAccepted: {
  121. SimpleChatEngine.sendMessage(_inputField.text)
  122. _inputField.text = ""
  123. }
  124. Keys.onPressed: {
  125. if (event.key === Qt.Key_V && event.modifiers & Qt.ControlModifier) {
  126. console.log("Ctrl + V")
  127. switch (SimpleChatEngine.clipBoardContentType) {
  128. case ChatMessage.Text:
  129. paste()
  130. break
  131. case ChatMessage.Image:
  132. SimpleChatEngine.sendImageFromClipboard()
  133. break
  134. }
  135. event.accepted = true
  136. }
  137. }
  138. }
  139. }