FluTimePicker.qml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. import QtQuick
  2. import QtQuick.Controls
  3. import QtQuick.Layouts
  4. import QtQuick.Window
  5. import FluentUI
  6. Rectangle {
  7. enum HourFormat {
  8. H,
  9. HH
  10. }
  11. property color dividerColor: FluTheme.dark ? Qt.rgba(77/255,77/255,77/255,1) : Qt.rgba(239/255,239/255,239/255,1)
  12. property color hoverColor: FluTheme.dark ? Qt.rgba(68/255,68/255,68/255,1) : Qt.rgba(251/255,251/255,251/255,1)
  13. property color normalColor: FluTheme.dark ? Qt.rgba(61/255,61/255,61/255,1) : Qt.rgba(254/255,254/255,254/255,1)
  14. property var window : Window.window
  15. property int hourFormat: FluTimePicker.H
  16. property int isH: hourFormat === FluTimePicker.H
  17. id:root
  18. color: {
  19. if(mouse_area.containsMouse){
  20. return hoverColor
  21. }
  22. return normalColor
  23. }
  24. height: 30
  25. width: 300
  26. radius: 4
  27. border.width: 1
  28. border.color: dividerColor
  29. MouseArea{
  30. id:mouse_area
  31. hoverEnabled: true
  32. anchors.fill: parent
  33. onClicked: {
  34. popup.showPopup()
  35. }
  36. }
  37. Rectangle{
  38. id:divider_1
  39. width: 1
  40. x: isH ? parent.width/3 : parent.width/2
  41. height: parent.height
  42. color: dividerColor
  43. }
  44. Rectangle{
  45. id:divider_2
  46. width: 1
  47. x:parent.width*2/3
  48. height: parent.height
  49. color: dividerColor
  50. visible: isH
  51. }
  52. FluText{
  53. id:text_hour
  54. anchors{
  55. left: parent.left
  56. right: divider_1.left
  57. top: parent.top
  58. bottom: parent.bottom
  59. }
  60. verticalAlignment: Text.AlignVCenter
  61. horizontalAlignment: Text.AlignHCenter
  62. text:"时"
  63. }
  64. FluText{
  65. id:text_minute
  66. anchors{
  67. left: divider_1.right
  68. right: isH ? divider_2.left : parent.right
  69. top: parent.top
  70. bottom: parent.bottom
  71. }
  72. verticalAlignment: Text.AlignVCenter
  73. horizontalAlignment: Text.AlignHCenter
  74. text:"分"
  75. }
  76. FluText{
  77. id:text_ampm
  78. visible: isH
  79. anchors{
  80. left: divider_2.right
  81. right: parent.right
  82. top: parent.top
  83. bottom: parent.bottom
  84. }
  85. verticalAlignment: Text.AlignVCenter
  86. horizontalAlignment: Text.AlignHCenter
  87. text:"AM/PM"
  88. }
  89. Menu{
  90. id:popup
  91. width: container.width
  92. height: container.height
  93. modal: true
  94. dim:false
  95. enter: Transition {
  96. reversible: true
  97. NumberAnimation {
  98. property: "opacity"
  99. from:0
  100. to:1
  101. duration: 83
  102. }
  103. }
  104. exit:Transition {
  105. NumberAnimation {
  106. property: "opacity"
  107. from:1
  108. to:0
  109. duration: 83
  110. }
  111. }
  112. background:Item{
  113. FluShadow{
  114. radius: 4
  115. }
  116. }
  117. contentItem: Item{
  118. clip: true
  119. Rectangle{
  120. id:container
  121. height: 340
  122. width: 300
  123. radius: 4
  124. color: FluTheme.dark ? Qt.rgba(51/255,48/255,48/255,1) : Qt.rgba(248/255,250/255,253/255,1)
  125. MouseArea{
  126. anchors.fill: parent
  127. }
  128. RowLayout{
  129. id:layout_content
  130. spacing: 0
  131. width: parent.width
  132. height: 300
  133. Component{
  134. id:list_delegate
  135. Item{
  136. height:38
  137. width:getListView().width
  138. function getListView(){
  139. if(type === 0)
  140. return list_view_1
  141. if(type === 1)
  142. return list_view_2
  143. if(type === 2)
  144. return list_view_3
  145. }
  146. Rectangle{
  147. anchors.fill: parent
  148. anchors.topMargin: 2
  149. anchors.bottomMargin: 2
  150. anchors.leftMargin: 5
  151. anchors.rightMargin: 5
  152. color: {
  153. if(getListView().currentIndex === position){
  154. if(FluTheme.dark){
  155. return item_mouse.containsMouse ? Qt.darker(FluTheme.primaryColor.lighter,1.1) : FluTheme.primaryColor.lighter
  156. }else{
  157. return item_mouse.containsMouse ? Qt.lighter(FluTheme.primaryColor.dark,1.1): FluTheme.primaryColor.dark
  158. }
  159. }
  160. if(item_mouse.containsMouse){
  161. return FluTheme.dark ? Qt.rgba(63/255,60/255,61/255,1) : Qt.rgba(237/255,237/255,242/255,1)
  162. }
  163. return FluTheme.dark ? Qt.rgba(51/255,48/255,48/255,1) : Qt.rgba(0,0,0,0)
  164. }
  165. radius: 3
  166. MouseArea{
  167. id:item_mouse
  168. anchors.fill: parent
  169. hoverEnabled: true
  170. onClicked: {
  171. getListView().currentIndex = position
  172. if(type === 0){
  173. text_hour.text = model
  174. }
  175. if(type === 1){
  176. text_minute.text = model
  177. }
  178. if(type === 2){
  179. text_ampm.text = model
  180. }
  181. }
  182. }
  183. FluText{
  184. text:model
  185. color: {
  186. if(getListView().currentIndex === position){
  187. if(FluTheme.dark){
  188. return Qt.rgba(0,0,0,1)
  189. }else{
  190. return Qt.rgba(1,1,1,1)
  191. }
  192. }else{
  193. return FluTheme.dark ? "#FFFFFF" : "#1A1A1A"
  194. }
  195. }
  196. anchors.centerIn: parent
  197. }
  198. }
  199. }
  200. }
  201. ListView{
  202. id:list_view_1
  203. width: isH ? 100 : 150
  204. height: parent.height
  205. boundsBehavior:Flickable.StopAtBounds
  206. ScrollBar.vertical: FluScrollBar {}
  207. preferredHighlightBegin: 0
  208. preferredHighlightEnd: 0
  209. highlightMoveDuration: 0
  210. model: isH ? generateArray(1,12) : generateArray(0,23)
  211. clip: true
  212. delegate: Loader{
  213. property var model: modelData
  214. property int type:0
  215. property int position:index
  216. sourceComponent: list_delegate
  217. }
  218. }
  219. Rectangle{
  220. width: 1
  221. height: parent.height
  222. color: dividerColor
  223. }
  224. ListView{
  225. id:list_view_2
  226. width: isH ? 100 : 150
  227. height: parent.height
  228. model: generateArray(0,59)
  229. clip: true
  230. preferredHighlightBegin: 0
  231. preferredHighlightEnd: 0
  232. highlightMoveDuration: 0
  233. ScrollBar.vertical: FluScrollBar {}
  234. boundsBehavior:Flickable.StopAtBounds
  235. delegate: Loader{
  236. property var model: modelData
  237. property int type:1
  238. property int position:index
  239. sourceComponent: list_delegate
  240. }
  241. }
  242. Rectangle{
  243. width: 1
  244. height: parent.height
  245. color: dividerColor
  246. visible: isH
  247. }
  248. ListView{
  249. id:list_view_3
  250. width: 100
  251. height: 76
  252. model: ["上午","下午"]
  253. clip: true
  254. visible: isH
  255. preferredHighlightBegin: 0
  256. preferredHighlightEnd: 0
  257. highlightMoveDuration: 0
  258. ScrollBar.vertical: FluScrollBar {}
  259. Layout.alignment: Qt.AlignVCenter
  260. boundsBehavior:Flickable.StopAtBounds
  261. delegate: Loader{
  262. property var model: modelData
  263. property int type:2
  264. property int position:index
  265. sourceComponent: list_delegate
  266. }
  267. }
  268. }
  269. Rectangle{
  270. width: parent.width
  271. height: 1
  272. anchors.top: layout_content.bottom
  273. color: dividerColor
  274. }
  275. Rectangle{
  276. id:layout_actions
  277. height: 40
  278. radius: 5
  279. color: FluTheme.dark ? Qt.rgba(32/255,32/255,32/255,1) : Qt.rgba(243/255,243/255,243/255,1)
  280. anchors{
  281. bottom:parent.bottom
  282. left: parent.left
  283. right: parent.right
  284. }
  285. Item {
  286. id:divider
  287. width: 1
  288. height: parent.height
  289. anchors.centerIn: parent
  290. }
  291. FluButton{
  292. anchors{
  293. left: parent.left
  294. leftMargin: 20
  295. rightMargin: 10
  296. right: divider.left
  297. verticalCenter: parent.verticalCenter
  298. }
  299. text: "取消"
  300. onClicked: {
  301. popup.close()
  302. }
  303. }
  304. FluFilledButton{
  305. anchors{
  306. right: parent.right
  307. left: divider.right
  308. rightMargin: 20
  309. leftMargin: 10
  310. verticalCenter: parent.verticalCenter
  311. }
  312. text: "确定"
  313. onClicked: {
  314. changeFlag = false
  315. popup.close()
  316. }
  317. }
  318. }
  319. }
  320. }
  321. y:35
  322. function showPopup() {
  323. changeFlag = true
  324. rowData[0] = text_hour.text
  325. rowData[1] = text_minute.text
  326. rowData[2] = text_ampm.text
  327. var now = new Date();
  328. var hour
  329. var ampm;
  330. if(isH){
  331. hour = now.getHours();
  332. if(hour>12){
  333. ampm = "下午"
  334. hour = hour-12
  335. }else{
  336. ampm = "上午"
  337. }
  338. }else{
  339. hour = now.getHours();
  340. }
  341. hour = text_hour.text === "时"? hour.toString().padStart(2, '0') : text_hour.text
  342. var minute = text_minute.text === "分"? now.getMinutes().toString().padStart(2, '0') : text_minute.text
  343. ampm = text_ampm.text === "AM/PM"?ampm:text_ampm.text
  344. list_view_1.currentIndex = list_view_1.model.indexOf(hour);
  345. list_view_2.currentIndex = list_view_2.model.indexOf(minute);
  346. list_view_3.currentIndex = list_view_3.model.indexOf(ampm);
  347. text_hour.text = hour
  348. text_minute.text = minute
  349. if(isH){
  350. text_ampm.text = ampm
  351. }
  352. var pos = root.mapToItem(null, 0, 0)
  353. if(window.height>pos.y+root.height+container.height){
  354. popup.y = root.height
  355. } else if(pos.y>container.height){
  356. popup.y = -container.height
  357. } else {
  358. popup.y = window.height-(pos.y+container.height)
  359. }
  360. popup.open()
  361. }
  362. onClosed: {
  363. if(changeFlag){
  364. text_hour.text = rowData[0]
  365. text_minute.text = rowData[1]
  366. text_ampm.text = rowData[2]
  367. }
  368. }
  369. }
  370. property bool changeFlag: true
  371. readonly property var rowData: ["","",""]
  372. function generateArray(start, n) {
  373. var arr = [];
  374. for (var i = start; i <= n; i++) {
  375. arr.push(i.toString().padStart(2, '0'));
  376. }
  377. return arr;
  378. }
  379. }