FluDatePicker.qml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. import QtQuick
  2. import QtQuick.Controls
  3. import QtQuick.Layouts
  4. import QtQuick.Window
  5. import FluentUI
  6. Rectangle {
  7. property color dividerColor: FluTheme.dark ? Qt.rgba(77/255,77/255,77/255,1) : Qt.rgba(239/255,239/255,239/255,1)
  8. property color hoverColor: FluTheme.dark ? Qt.rgba(68/255,68/255,68/255,1) : Qt.rgba(251/255,251/255,251/255,1)
  9. property color normalColor: FluTheme.dark ? Qt.rgba(61/255,61/255,61/255,1) : Qt.rgba(254/255,254/255,254/255,1)
  10. property var window : Window.window
  11. property bool showYear: true
  12. property bool changeFlag: true
  13. readonly property var rowData: ["","",""]
  14. id:root
  15. color: {
  16. if(mouse_area.containsMouse){
  17. return hoverColor
  18. }
  19. return normalColor
  20. }
  21. height: 30
  22. width: 300
  23. radius: 4
  24. border.width: 1
  25. border.color: dividerColor
  26. MouseArea{
  27. id:mouse_area
  28. hoverEnabled: true
  29. anchors.fill: parent
  30. onClicked: {
  31. popup.showPopup()
  32. }
  33. }
  34. Rectangle{
  35. id:divider_1
  36. width: 1
  37. x: parent.width/3
  38. height: parent.height
  39. color: dividerColor
  40. visible: showYear
  41. }
  42. Rectangle{
  43. id:divider_2
  44. width: 1
  45. x: showYear ? parent.width*2/3 : parent.width/2
  46. height: parent.height
  47. color: dividerColor
  48. }
  49. FluText{
  50. id:text_year
  51. anchors{
  52. left: parent.left
  53. right: divider_1.left
  54. top: parent.top
  55. bottom: parent.bottom
  56. }
  57. visible: showYear
  58. verticalAlignment: Text.AlignVCenter
  59. horizontalAlignment: Text.AlignHCenter
  60. text:"年"
  61. }
  62. FluText{
  63. id:text_month
  64. anchors{
  65. left: showYear ? divider_1.right : parent.left
  66. right: divider_2.left
  67. top: parent.top
  68. bottom: parent.bottom
  69. }
  70. verticalAlignment: Text.AlignVCenter
  71. horizontalAlignment: Text.AlignHCenter
  72. text:"月"
  73. }
  74. FluText{
  75. id:text_day
  76. anchors{
  77. left: divider_2.right
  78. right: parent.right
  79. top: parent.top
  80. bottom: parent.bottom
  81. }
  82. verticalAlignment: Text.AlignVCenter
  83. horizontalAlignment: Text.AlignHCenter
  84. text:"日"
  85. }
  86. Menu{
  87. id:popup
  88. modal: true
  89. width: container.width
  90. height: container.height
  91. dim:false
  92. enter: Transition {
  93. reversible: true
  94. NumberAnimation {
  95. property: "opacity"
  96. from:0
  97. to:1
  98. duration: 83
  99. }
  100. }
  101. exit:Transition {
  102. NumberAnimation {
  103. property: "opacity"
  104. from:1
  105. to:0
  106. duration: 83
  107. }
  108. }
  109. background:Item{
  110. FluShadow{
  111. radius: 4
  112. }
  113. }
  114. contentItem: Item{
  115. clip: true
  116. Rectangle{
  117. id:container
  118. radius: 4
  119. width: 300
  120. height: 340
  121. color: FluTheme.dark ? Qt.rgba(51/255,48/255,48/255,1) : Qt.rgba(248/255,250/255,253/255,1)
  122. MouseArea{
  123. anchors.fill: parent
  124. }
  125. FluShadow{
  126. radius: 4
  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_year.text = model
  174. list_view_2.model = generateMonthArray(1,12)
  175. text_month.text = list_view_2.model[list_view_2.currentIndex]
  176. list_view_3.model = generateMonthDaysArray(list_view_1.model[list_view_1.currentIndex],list_view_2.model[list_view_2.currentIndex])
  177. text_day.text = list_view_3.model[list_view_3.currentIndex]
  178. }
  179. if(type === 1){
  180. text_month.text = model
  181. list_view_3.model = generateMonthDaysArray(list_view_1.model[list_view_1.currentIndex],list_view_2.model[list_view_2.currentIndex])
  182. text_day.text = list_view_3.model[list_view_3.currentIndex]
  183. }
  184. if(type === 2){
  185. text_day.text = model
  186. }
  187. }
  188. }
  189. FluText{
  190. text:model
  191. color: {
  192. if(getListView().currentIndex === position){
  193. if(FluTheme.dark){
  194. return Qt.rgba(0,0,0,1)
  195. }else{
  196. return Qt.rgba(1,1,1,1)
  197. }
  198. }else{
  199. return FluTheme.dark ? "#FFFFFF" : "#1A1A1A"
  200. }
  201. }
  202. anchors.centerIn: parent
  203. }
  204. }
  205. }
  206. }
  207. ListView{
  208. id:list_view_1
  209. width: 100
  210. height: parent.height
  211. boundsBehavior:Flickable.StopAtBounds
  212. ScrollBar.vertical: FluScrollBar {}
  213. model: generateYearArray(1924,2048)
  214. clip: true
  215. preferredHighlightBegin: 0
  216. preferredHighlightEnd: 0
  217. highlightMoveDuration: 0
  218. visible: showYear
  219. delegate: Loader{
  220. property var model: modelData
  221. property int type:0
  222. property int position:index
  223. sourceComponent: list_delegate
  224. }
  225. }
  226. Rectangle{
  227. width: 1
  228. height: parent.height
  229. color: dividerColor
  230. }
  231. ListView{
  232. id:list_view_2
  233. width: showYear ? 100 : 150
  234. height: parent.height
  235. clip: true
  236. ScrollBar.vertical: FluScrollBar {}
  237. preferredHighlightBegin: 0
  238. preferredHighlightEnd: 0
  239. highlightMoveDuration: 0
  240. boundsBehavior:Flickable.StopAtBounds
  241. delegate: Loader{
  242. property var model: modelData
  243. property int type:1
  244. property int position:index
  245. sourceComponent: list_delegate
  246. }
  247. }
  248. Rectangle{
  249. width: 1
  250. height: parent.height
  251. color: dividerColor
  252. }
  253. ListView{
  254. id:list_view_3
  255. width: showYear ? 100 : 150
  256. height: parent.height
  257. clip: true
  258. preferredHighlightBegin: 0
  259. preferredHighlightEnd: 0
  260. highlightMoveDuration: 0
  261. ScrollBar.vertical: FluScrollBar {}
  262. Layout.alignment: Qt.AlignVCenter
  263. boundsBehavior:Flickable.StopAtBounds
  264. delegate: Loader{
  265. property var model: modelData
  266. property int type:2
  267. property int position:index
  268. sourceComponent: list_delegate
  269. }
  270. }
  271. }
  272. Rectangle{
  273. width: parent.width
  274. height: 1
  275. anchors.top: layout_content.bottom
  276. color: dividerColor
  277. }
  278. Rectangle{
  279. id:layout_actions
  280. height: 40
  281. radius: 5
  282. color: FluTheme.dark ? Qt.rgba(32/255,32/255,32/255,1) : Qt.rgba(243/255,243/255,243/255,1)
  283. anchors{
  284. bottom:parent.bottom
  285. left: parent.left
  286. right: parent.right
  287. }
  288. Item {
  289. id:divider
  290. width: 1
  291. height: parent.height
  292. anchors.centerIn: parent
  293. }
  294. FluButton{
  295. anchors{
  296. left: parent.left
  297. leftMargin: 20
  298. rightMargin: 10
  299. right: divider.left
  300. verticalCenter: parent.verticalCenter
  301. }
  302. text: "取消"
  303. onClicked: {
  304. popup.close()
  305. }
  306. }
  307. FluFilledButton{
  308. anchors{
  309. right: parent.right
  310. left: divider.right
  311. rightMargin: 20
  312. leftMargin: 10
  313. verticalCenter: parent.verticalCenter
  314. }
  315. text: "确定"
  316. onClicked: {
  317. changeFlag = false
  318. popup.close()
  319. }
  320. }
  321. }
  322. }
  323. }
  324. y:35
  325. function showPopup() {
  326. changeFlag = true
  327. rowData[0] = text_year.text
  328. rowData[1] = text_month.text
  329. rowData[2] = text_day.text
  330. const now = new Date();
  331. var year = text_year.text === "年"? now.getFullYear() : Number(text_year.text);
  332. var month = text_month.text === "月"? now.getMonth() + 1 : Number(text_month.text);
  333. var day = text_day.text === "日" ? now.getDate() : Number(text_day.text);
  334. list_view_1.currentIndex = list_view_1.model.indexOf(year)
  335. text_year.text = year
  336. list_view_2.model = generateMonthArray(1,12)
  337. list_view_2.currentIndex = list_view_2.model.indexOf(month)
  338. text_month.text = month
  339. list_view_3.model = generateMonthDaysArray(year,month)
  340. list_view_3.currentIndex = list_view_3.model.indexOf(day)
  341. text_day.text = day
  342. var pos = root.mapToItem(null, 0, 0)
  343. if(window.height>pos.y+root.height+container.height){
  344. popup.y = root.height
  345. } else if(pos.y>container.height){
  346. popup.y = -container.height
  347. } else {
  348. popup.y = window.height-(pos.y+container.height)
  349. }
  350. popup.open()
  351. }
  352. onClosed: {
  353. if(changeFlag){
  354. text_year.text = rowData[0]
  355. text_month.text = rowData[1]
  356. text_day.text = rowData[2]
  357. }
  358. }
  359. }
  360. function generateYearArray(startYear, endYear) {
  361. const yearArray = [];
  362. for (let year = startYear; year <= endYear; year++) {
  363. yearArray.push(year);
  364. }
  365. return yearArray;
  366. }
  367. function generateMonthArray(startMonth, endMonth) {
  368. const monthArray = [];
  369. for (let month = startMonth; month <= endMonth; month++) {
  370. monthArray.push(month);
  371. }
  372. return monthArray;
  373. }
  374. function generateMonthDaysArray(year, month) {
  375. const monthDaysArray = [];
  376. const lastDayOfMonth = new Date(year, month, 0).getDate();
  377. for (let day = 1; day <= lastDayOfMonth; day++) {
  378. monthDaysArray.push(day);
  379. }
  380. return monthDaysArray;
  381. }
  382. }