ChatWindow.qml 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import QtQuick
  2. import QtQuick.Layouts
  3. import QtQuick.Controls
  4. import FluentUI
  5. import Controller
  6. import QtQuick.Dialogs
  7. FluWindow {
  8. title:"ChatGPT"
  9. width: 680
  10. height: 600
  11. minimumWidth: 500
  12. minimumHeight: 600
  13. onInitArgument:
  14. (argument)=>{
  15. scrollview.focus = true
  16. }
  17. ChatController{
  18. id:controller
  19. onResponseDataChanged: {
  20. appendMessage(false,responseData)
  21. }
  22. }
  23. ListModel{
  24. id:model_message
  25. ListElement{
  26. isMy:false
  27. text:"欢迎使用ChatGPT"
  28. }
  29. ListElement{
  30. isMy:true
  31. text:"好的,3Q"
  32. }
  33. }
  34. FluAppBar{
  35. id:appbar
  36. title:"ChatGPT"
  37. width:parent.width
  38. }
  39. Component{
  40. id:com_text
  41. TextEdit {
  42. id:item_text
  43. text: message
  44. wrapMode: Text.WrapAnywhere
  45. readOnly: true
  46. selectByMouse: true
  47. selectByKeyboard: true
  48. selectedTextColor: Qt.rgba(51,153,255,1)
  49. color:FluColors.Black
  50. selectionColor: {
  51. if(FluTheme.dark){
  52. return FluTheme.primaryColor.lighter
  53. }else{
  54. return FluTheme.primaryColor.dark
  55. }
  56. }
  57. width: Math.min(list_message.width-200,600,implicitWidth)
  58. TapHandler{
  59. acceptedButtons: Qt.RightButton
  60. onTapped: {
  61. menu_item.showMenu(item_text.selectedText)
  62. }
  63. }
  64. }
  65. }
  66. FluArea{
  67. id:layout_content
  68. anchors{
  69. top: appbar.bottom
  70. left: parent.left
  71. right: parent.right
  72. bottom: layout_bottom.top
  73. margins: 10
  74. }
  75. color: FluTheme.dark ? Qt.rgba(39/255,39/255,39/255,1) : Qt.rgba(245/255,245/255,245/255,1)
  76. ListView{
  77. id:list_message
  78. anchors.fill: parent
  79. model:model_message
  80. clip: true
  81. ScrollBar.vertical: FluScrollBar {}
  82. preferredHighlightBegin: 0
  83. preferredHighlightEnd: 0
  84. highlightMoveDuration: 0
  85. header:Item{
  86. width: list_message.width
  87. height:20
  88. }
  89. footer:Item{
  90. width: list_message.width
  91. height:20
  92. }
  93. delegate: Item{
  94. width: ListView.view.width
  95. height: childrenRect.height
  96. FluRectangle{
  97. id:item_avatar
  98. width: 30
  99. height: 30
  100. radius:[15,15,15,15]
  101. anchors{
  102. right: isMy ? parent.right : undefined
  103. rightMargin: isMy ? 20 : undefined
  104. left: isMy ? undefined : parent.left
  105. leftMargin: isMy ? undefined : 20
  106. top:parent.top
  107. }
  108. Image {
  109. asynchronous: true
  110. anchors.fill: parent
  111. sourceSize: Qt.size(100,100)
  112. source: isMy ? "qrc:/example/res/svg/avatar_2.svg" : "qrc:/example/res/image/logo_openai.png"
  113. }
  114. }
  115. Rectangle{
  116. id:item_layout_content
  117. color: isMy ? "#FF95EC69" : "#FFFFFF"
  118. width: item_msg_loader.width+10
  119. height: item_msg_loader.height+10
  120. radius: 3
  121. anchors{
  122. top: item_avatar.top
  123. right: isMy ? item_avatar.left : undefined
  124. rightMargin: isMy ? 10 : undefined
  125. left: isMy ? undefined : item_avatar.right
  126. leftMargin: isMy ? undefined : 10
  127. }
  128. Loader{
  129. id:item_msg_loader
  130. property var message: model.text
  131. anchors.centerIn: parent
  132. sourceComponent: com_text
  133. }
  134. }
  135. Item{
  136. id:item_layout_bottom
  137. width: parent.width
  138. anchors.top: item_layout_content.bottom
  139. height: 20
  140. }
  141. }
  142. }
  143. }
  144. FluArea{
  145. id:layout_bottom
  146. height: 90
  147. anchors{
  148. bottom: parent.bottom
  149. bottomMargin: 10
  150. left: parent.left
  151. right: parent.right
  152. leftMargin: 10
  153. rightMargin: 10
  154. }
  155. ScrollView{
  156. id:scrollview
  157. anchors{
  158. bottom: parent.bottom
  159. left: parent.left
  160. right: button_send.left
  161. bottomMargin: 10
  162. leftMargin: 10
  163. rightMargin: 10
  164. }
  165. height: Math.min(textbox.implicitHeight,64)
  166. FluMultilineTextBox{
  167. id:textbox
  168. focus:true
  169. placeholderText: "请输入消息"
  170. }
  171. }
  172. FluFilledButton{
  173. id:button_send
  174. text:controller.isLoading ? timer_loading.loadingText :"发送"
  175. anchors{
  176. bottom: parent.bottom
  177. right: parent.right
  178. bottomMargin: 10
  179. rightMargin: 10
  180. }
  181. width: 60
  182. disabled: controller.isLoading
  183. onClicked:{
  184. var text = textbox.text
  185. appendMessage(true,text)
  186. controller.sendMessage(text)
  187. textbox.clear()
  188. }
  189. Timer{
  190. id:timer_loading
  191. property int count : 0
  192. property string loadingText : ""
  193. interval: 500
  194. running: controller.isLoading
  195. repeat: true
  196. onTriggered: {
  197. switch(count%3){
  198. case 0:
  199. loadingText = "."
  200. break
  201. case 1:
  202. loadingText = ".."
  203. break
  204. case 2:
  205. loadingText = "..."
  206. break
  207. default:
  208. loadingText = ""
  209. break
  210. }
  211. count++
  212. }
  213. }
  214. }
  215. }
  216. FluMenu{
  217. id:menu_item
  218. focus: false
  219. property string selectedText: ""
  220. FluMenuItem{
  221. text:"复制"
  222. onClicked: {
  223. controller.clipText(menu_item.selectedText)
  224. showSuccess("复制成功")
  225. }
  226. }
  227. function showMenu(text){
  228. menu_item.selectedText = text
  229. menu_item.popup()
  230. }
  231. }
  232. function appendMessage(isMy,text){
  233. model_message.append({isMy:isMy,text:text})
  234. list_message.positionViewAtEnd()
  235. }
  236. }