ReuseDateField.qml 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import QtQuick
  2. import QtQuick.Controls
  3. import QtQuick.Layouts
  4. import Qt.labs.platform
  5. RowLayout {
  6. id: reusedatefield
  7. anchors.fill: parent
  8. spacing: 10
  9. property string strTitle: ""
  10. property string strValue: ""
  11. Rectangle {
  12. width: parent.width * 0.225
  13. height: parent.height
  14. color: "#00ffffff"
  15. Text {
  16. id: reusedatefield_text_title
  17. anchors.fill: parent
  18. font.pixelSize: 25
  19. color: "#ffffff"
  20. horizontalAlignment: Text.AlignRight
  21. verticalAlignment: Text.AlignVCenter
  22. }
  23. }
  24. Rectangle {
  25. id: reusedatefield_rect_date
  26. x: parent.width * 0.225
  27. width: parent.width * 0.675
  28. height: parent.height * 0.725
  29. color: "#ffffff"
  30. radius: 5
  31. TextField {
  32. id: reusedatefield_text_value
  33. anchors.fill: parent
  34. anchors.margins: 2
  35. font.pointSize: 15
  36. focus: false
  37. color: "#a3000000"
  38. background: Rectangle {
  39. color: "transparent"
  40. }
  41. placeholderText: qsTr("")
  42. font.pixelSize: 20
  43. selectionColor: "#a300aaff"
  44. horizontalAlignment: Text.AlignLeft
  45. verticalAlignment: Text.AlignVCenter
  46. wrapMode: Text.NoWrap
  47. placeholderTextColor: "#60ffffff"
  48. padding:5
  49. renderType: Text.QtRendering
  50. font.styleName: "Regular"
  51. readOnly: true
  52. MouseArea
  53. {
  54. anchors.fill: parent
  55. onClicked: {
  56. reusedatefield_popup_roor.open();
  57. reusedatefield_popup_roor.x = reusedatefield_rect_date.mapToItem(Overlay.overlay, 0, 0).x
  58. reusedatefield_popup_roor.y = reusedatefield_rect_date.mapToItem(Overlay.overlay, 0, 0).y
  59. - reusedatefield_popup_roor.height
  60. }
  61. }
  62. Connections {
  63. target: reusedatefield_popup_roor
  64. onDateSelected :{
  65. reusedatefield.strValue = Qt.formatDate(selectedDate, "yyyy-MM-dd");
  66. }
  67. }
  68. }
  69. Popup {
  70. id: reusedatefield_popup_roor
  71. width: 320
  72. height: 360
  73. parent: Overlay.overlay
  74. modal: true
  75. focus: true
  76. property date selectedDate: new Date()
  77. signal dateSelected(date selectedDate)
  78. Rectangle {
  79. anchors.fill: parent
  80. color: "#ffffff"
  81. radius: 4
  82. border.color: "#cccccc"
  83. Column {
  84. anchors.fill: parent
  85. anchors.margins: 10
  86. spacing: 10
  87. // 年月导航
  88. Row {
  89. width: parent.width
  90. height: 40
  91. spacing: 10
  92. Button {
  93. width: height
  94. text: "<"
  95. onClicked: changeMonth(-1)
  96. }
  97. Label {
  98. text: Qt.formatDate(reusedatefield_popup_roor.selectedDate, "yyyy年MM月")
  99. font.bold: true
  100. font.pixelSize: 16
  101. anchors.verticalCenter: parent.verticalCenter
  102. }
  103. Button {
  104. width: height
  105. text: ">"
  106. onClicked: changeMonth(1)
  107. }
  108. }
  109. // 星期标题
  110. Row {
  111. width: parent.width
  112. height: 30
  113. spacing: 1
  114. Repeater {
  115. model: ["日", "一", "二", "三", "四", "五", "六"]
  116. Label {
  117. width: (parent.width - 6) / 7
  118. text: modelData
  119. horizontalAlignment: Text.AlignHCenter
  120. font.bold: true
  121. }
  122. }
  123. }
  124. // 日期网格
  125. Grid {
  126. width: parent.width
  127. columns: 7
  128. spacing: 1
  129. Repeater {
  130. model: getDaysModel()
  131. Rectangle {
  132. width: (parent.width - 6) / 7
  133. height: 36
  134. color: {
  135. if (!modelData.enabled) return "transparent"
  136. if (modelData.isSelected) return "#4285f4"
  137. if (modelData.isToday) return "#e0e0e0"
  138. return "white"
  139. }
  140. radius: 4
  141. Label {
  142. anchors.centerIn: parent
  143. text: modelData.day
  144. color: {
  145. if (!modelData.enabled) return "transparent"
  146. if (modelData.isSelected) return "white"
  147. return "black"
  148. }
  149. }
  150. MouseArea {
  151. anchors.fill: parent
  152. enabled: modelData.enabled
  153. onClicked: {
  154. reusedatefield_popup_roor.selectedDate = new Date(
  155. reusedatefield_popup_roor.selectedDate.getFullYear(),
  156. reusedatefield_popup_roor.selectedDate.getMonth(),
  157. modelData.day
  158. )
  159. reusedatefield_popup_roor.dateSelected(reusedatefield_popup_roor.selectedDate)
  160. reusedatefield_popup_roor.close()
  161. }
  162. }
  163. }
  164. }
  165. }
  166. }
  167. }
  168. }
  169. }
  170. Connections {
  171. target: reusedatefield
  172. onStrTitleChanged: {
  173. reusedatefield_text_title.text = strTitle;
  174. }
  175. onStrValueChanged: {
  176. reusedatefield_text_value.text = strValue;
  177. }
  178. }
  179. Component.onCompleted: {
  180. reusedatefield_text_title.text = strTitle;
  181. reusedatefield_text_value.text = strValue;
  182. }
  183. function changeMonth(offset) {
  184. var newDate = new Date(
  185. reusedatefield_popup_roor.selectedDate.getFullYear(),
  186. reusedatefield_popup_roor.selectedDate.getMonth() + offset,
  187. reusedatefield_popup_roor.selectedDate.getDate()
  188. )
  189. reusedatefield_popup_roor.selectedDate = newDate
  190. }
  191. function getDaysModel() {
  192. var firstDay = new Date(
  193. reusedatefield_popup_roor.selectedDate.getFullYear(),
  194. reusedatefield_popup_roor.selectedDate.getMonth(),
  195. 1
  196. )
  197. var daysInMonth = new Date(
  198. reusedatefield_popup_roor.selectedDate.getFullYear(),
  199. reusedatefield_popup_roor.selectedDate.getMonth() + 1,
  200. 0
  201. ).getDate()
  202. var startDay = firstDay.getDay()
  203. var today = new Date()
  204. var isToday = (today.getFullYear() === reusedatefield_popup_roor.selectedDate.getFullYear() &&
  205. today.getMonth() === reusedatefield_popup_roor.selectedDate.getMonth())
  206. var days = []
  207. // 填充空白
  208. for (var i = 0; i < startDay; i++) {
  209. days.push({day: "", enabled: false})
  210. }
  211. // 填充日期
  212. for (i = 1; i <= daysInMonth; i++) {
  213. days.push({
  214. day: i,
  215. enabled: true,
  216. isSelected: (i === reusedatefield_popup_roor.selectedDate.getDate()),
  217. isToday: (isToday && i === today.getDate())
  218. })
  219. }
  220. return days
  221. }
  222. }