FluCalendarView.qml 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. import QtQuick
  2. import QtQuick.Controls
  3. import FluentUI
  4. Item {
  5. enum DisplayMode {
  6. Month,
  7. Year,
  8. Decade
  9. }
  10. property int displayMode: FluCalendarView.Month
  11. property var date: new Date()
  12. property var currentDate : new Date()
  13. property var toDay: new Date()
  14. signal dateClicked(var date)
  15. id:control
  16. width: 280
  17. height: 325
  18. Component.onCompleted: {
  19. updateMouth(date)
  20. }
  21. Component{
  22. id:com_week
  23. Item{
  24. height: 40
  25. width: 40
  26. FluText{
  27. text:name
  28. anchors.centerIn: parent
  29. }
  30. }
  31. }
  32. Component{
  33. id:com_year
  34. Button{
  35. id:item_control
  36. property bool isYear: control.toDay.getFullYear() === date.getFullYear()
  37. height: 70
  38. width: 70
  39. onClicked:{
  40. control.date = date
  41. displayMode = FluCalendarView.Year
  42. updateYear(date)
  43. }
  44. background: Item{
  45. Rectangle{
  46. width: 60
  47. height: 60
  48. radius: 4
  49. anchors.centerIn: parent
  50. color:{
  51. if(FluTheme.dark){
  52. if(item_control.hovered){
  53. return Qt.rgba(1,1,1,0.05)
  54. }
  55. return Qt.rgba(0,0,0,0)
  56. }else{
  57. if(item_control.hovered){
  58. return Qt.rgba(0,0,0,0.05)
  59. }
  60. return Qt.rgba(0,0,0,0)
  61. }
  62. }
  63. }
  64. Rectangle{
  65. id:backgound_selected
  66. anchors.centerIn: parent
  67. width: 50
  68. height: 50
  69. radius: 25
  70. visible: isYear
  71. color: FluTheme.primaryColor.dark
  72. }
  73. FluText{
  74. text:date.getFullYear()
  75. anchors.centerIn: parent
  76. color: {
  77. if(isYear){
  78. return "#FFFFFF"
  79. }
  80. if(isDecade){
  81. return FluTheme.dark ? "#FFFFFF" : "#1A1A1A"
  82. }
  83. return Qt.rgba(150/255,150/255,150/255,1)
  84. }
  85. }
  86. }
  87. contentItem: Item{}
  88. }
  89. }
  90. Component{
  91. id:com_month
  92. Button{
  93. id:item_control
  94. property bool isYear: control.date.getFullYear() === date.getFullYear()
  95. property bool isMonth: control.toDay.getFullYear() === date.getFullYear() && control.toDay.getMonth() === date.getMonth()
  96. height: 70
  97. width: 70
  98. onClicked:{
  99. control.date = date
  100. displayMode = FluCalendarView.Month
  101. updateMouth(date)
  102. }
  103. background: Item{
  104. Rectangle{
  105. width: 60
  106. height: 60
  107. radius: 4
  108. anchors.centerIn: parent
  109. color:{
  110. if(FluTheme.dark){
  111. if(item_control.hovered){
  112. return Qt.rgba(1,1,1,0.05)
  113. }
  114. return Qt.rgba(0,0,0,0)
  115. }else{
  116. if(item_control.hovered){
  117. return Qt.rgba(0,0,0,0.05)
  118. }
  119. return Qt.rgba(0,0,0,0)
  120. }
  121. }
  122. }
  123. Rectangle{
  124. id:backgound_selected
  125. anchors.centerIn: parent
  126. width: 50
  127. height: 50
  128. radius: 25
  129. visible: isMonth
  130. color: FluTheme.primaryColor.dark
  131. }
  132. FluText{
  133. text:(date.getMonth()+1)+"月"
  134. anchors.centerIn: parent
  135. color: {
  136. if(isMonth){
  137. return "#FFFFFF"
  138. }
  139. if(isYear){
  140. return FluTheme.dark ? "#FFFFFF" : "#1A1A1A"
  141. }
  142. return Qt.rgba(150/255,150/255,150/255,1)
  143. }
  144. }
  145. }
  146. contentItem: Item{}
  147. }
  148. }
  149. Component{
  150. id:com_day
  151. Button{
  152. id:item_control
  153. property bool isMonth: control.date.getMonth() === date.getMonth()
  154. property bool isDay: control.currentDate.getFullYear() === date.getFullYear() && control.currentDate.getMonth() === date.getMonth() && control.currentDate.getDate() === date.getDate()
  155. property bool isToDay: control.toDay.getFullYear() === date.getFullYear() && control.toDay.getMonth() === date.getMonth() && control.toDay.getDate() === date.getDate()
  156. height: 40
  157. width: 40
  158. onClicked: {
  159. currentDate = date
  160. control.dateClicked(date)
  161. }
  162. background: Item{
  163. Rectangle{
  164. width: 36
  165. height: 36
  166. radius: 4
  167. anchors.centerIn: parent
  168. color:{
  169. if(FluTheme.dark){
  170. if(item_control.hovered){
  171. return Qt.rgba(1,1,1,0.05)
  172. }
  173. return Qt.rgba(0,0,0,0)
  174. }else{
  175. if(item_control.hovered){
  176. return Qt.rgba(0,0,0,0.05)
  177. }
  178. return Qt.rgba(0,0,0,0)
  179. }
  180. }
  181. }
  182. Rectangle{
  183. id:backgound_today
  184. anchors.centerIn: parent
  185. width: 36
  186. height: 36
  187. radius: 18
  188. color:"#00000000"
  189. visible: isDay
  190. border.color: FluTheme.primaryColor.dark
  191. border.width: 1
  192. }
  193. Rectangle{
  194. id:backgound_selected
  195. anchors.centerIn: parent
  196. width: 30
  197. height: 30
  198. radius: 15
  199. visible: isToDay
  200. color: FluTheme.primaryColor.dark
  201. }
  202. FluText{
  203. text:date.getDate()
  204. anchors.centerIn: parent
  205. color: {
  206. if(isToDay){
  207. return "#FFFFFF"
  208. }
  209. if(isMonth){
  210. return FluTheme.dark ? "#FFFFFF" : "#1A1A1A"
  211. }
  212. return Qt.rgba(150/255,150/255,150/255,1)
  213. }
  214. }
  215. }
  216. contentItem: Item{}
  217. }
  218. }
  219. FluArea{
  220. anchors.fill: parent
  221. radius: 5
  222. FluShadow{
  223. radius: 5
  224. }
  225. Rectangle{
  226. id:layout_divider
  227. height: 1
  228. width: parent.width
  229. color: FluTheme.dark ? Qt.rgba(45/255,45/255,45/255,1) : Qt.rgba(226/255,229/255,234/255,1)
  230. anchors{
  231. top: parent.top
  232. topMargin: 44
  233. }
  234. }
  235. Item{
  236. id:layout_top
  237. anchors{
  238. left: parent.left
  239. right: parent.right
  240. top: parent.top
  241. bottom: layout_divider.top
  242. }
  243. FluTextButton{
  244. id:title
  245. leftPadding: 0
  246. rightPadding: 0
  247. anchors{
  248. verticalCenter: parent.verticalCenter
  249. left: parent.left
  250. leftMargin: 14
  251. }
  252. disabled: displayMode === FluCalendarView.Decade
  253. onClicked:{
  254. if(displayMode === FluCalendarView.Month){
  255. displayMode = FluCalendarView.Year
  256. updateYear(date)
  257. }else if(displayMode === FluCalendarView.Year){
  258. displayMode = FluCalendarView.Decade
  259. updateDecade(date)
  260. }
  261. }
  262. }
  263. FluIconButton{
  264. id:icon_up
  265. iconSource: FluentIcons.CaretUpSolid8
  266. iconSize: 10
  267. anchors{
  268. verticalCenter: parent.verticalCenter
  269. right: icon_down.left
  270. rightMargin: 14
  271. }
  272. onClicked: {
  273. var year = date.getFullYear()
  274. var month = date.getMonth()
  275. if(displayMode === FluCalendarView.Month){
  276. var lastMonthYear = year;
  277. var lastMonthMonth = month - 1
  278. if (month === 0) {
  279. lastMonthYear = year - 1
  280. lastMonthMonth = 11
  281. }
  282. date = new Date(lastMonthYear,lastMonthMonth,1)
  283. updateMouth(date)
  284. }else if(displayMode === FluCalendarView.Year){
  285. date = new Date(year-1,month,1)
  286. updateYear(date)
  287. }else if(displayMode === FluCalendarView.Decade){
  288. date = new Date(Math.floor(year / 10) * 10-10,month,1)
  289. updateDecade(date)
  290. }
  291. }
  292. }
  293. FluIconButton{
  294. id:icon_down
  295. iconSource: FluentIcons.CaretDownSolid8
  296. iconSize: 10
  297. anchors{
  298. verticalCenter: parent.verticalCenter
  299. right: parent.right
  300. rightMargin: 8
  301. }
  302. onClicked: {
  303. var year = date.getFullYear()
  304. var month = date.getMonth()
  305. if(displayMode === FluCalendarView.Month){
  306. var nextMonthYear = year
  307. var nextMonth = month + 1
  308. if (month === 11) {
  309. nextMonthYear = year + 1
  310. nextMonth = 0
  311. }
  312. date = new Date(nextMonthYear,nextMonth,1)
  313. updateMouth(date)
  314. }else if(displayMode === FluCalendarView.Year){
  315. date = new Date(year+1,month,1)
  316. updateYear(date)
  317. }else if(displayMode === FluCalendarView.Decade){
  318. date = new Date(Math.floor(year / 10) * 10+10,month,1)
  319. updateDecade(date)
  320. }
  321. }
  322. }
  323. }
  324. ListModel {
  325. id:list_model
  326. }
  327. Item{
  328. id:layout_bottom
  329. anchors{
  330. left: parent.left
  331. right: parent.right
  332. top: layout_divider.bottom
  333. bottom: parent.bottom
  334. }
  335. GridView{
  336. model: list_model
  337. anchors.fill: parent
  338. cellHeight: displayMode === FluCalendarView.Month ? 40 : 70
  339. cellWidth: displayMode === FluCalendarView.Month ? 40 : 70
  340. clip: true
  341. boundsBehavior:Flickable.StopAtBounds
  342. delegate: Loader{
  343. property var modelData : model
  344. property var name : model.name
  345. property var date : model.date
  346. property var isDecade: {
  347. return model.isDecade
  348. }
  349. sourceComponent: {
  350. if(model.type === 0){
  351. return com_week
  352. }
  353. if(model.type === 1){
  354. return com_day
  355. }
  356. if(model.type === 2){
  357. return com_month
  358. }
  359. if(model.type === 3){
  360. return com_year
  361. }
  362. return com_day
  363. }
  364. }
  365. }
  366. }
  367. }
  368. function createItemWeek(name){
  369. return {type:0,date:new Date(),name:name,isDecade:false}
  370. }
  371. function createItemDay(date){
  372. return {type:1,date:date,name:"",isDecade:false}
  373. }
  374. function createItemMonth(date){
  375. return {type:2,date:date,name:"",isDecade:false}
  376. }
  377. function createItemYear(date,isDecade){
  378. return {type:3,date:date,name:"",isDecade:isDecade}
  379. }
  380. function updateDecade(date){
  381. list_model.clear()
  382. var year = date.getFullYear()
  383. const decadeStart = Math.floor(year / 10) * 10;
  384. for(var i = decadeStart ; i< decadeStart+10 ; i++){
  385. list_model.append(createItemYear(new Date(i,0,1),true));
  386. }
  387. for(var j = decadeStart+10 ; j< decadeStart+16 ; j++){
  388. list_model.append(createItemYear(new Date(j,0,1),false));
  389. }
  390. title.text = decadeStart+"-"+(decadeStart+10)
  391. }
  392. function updateYear(date){
  393. list_model.clear()
  394. var year = date.getFullYear()
  395. for(var i = 0 ; i< 12 ; i++){
  396. list_model.append(createItemMonth(new Date(year,i)));
  397. }
  398. for(var j = 0 ; j< 4 ; j++){
  399. list_model.append(createItemMonth(new Date(year+1,j)));
  400. }
  401. title.text = year+"年"
  402. }
  403. function updateMouth(date){
  404. list_model.clear()
  405. list_model.append([createItemWeek("一"),createItemWeek("二"),createItemWeek("三"),createItemWeek("四"),createItemWeek("五"),createItemWeek("六"),createItemWeek("日")])
  406. var year = date.getFullYear()
  407. var month = date.getMonth()
  408. var day = date.getDate()
  409. var firstDayOfMonth = new Date(year, month, 1)
  410. var dayOfWeek = firstDayOfMonth.getDay()
  411. var headerSize = (dayOfWeek===0?7:dayOfWeek)-1
  412. if(headerSize!==0){
  413. var lastMonthYear = year;
  414. var lastMonthMonth = month - 1
  415. if (month === 0) {
  416. lastMonthYear = year - 1
  417. lastMonthMonth = 11
  418. }
  419. var lastMonthDays = new Date(lastMonthYear, lastMonthMonth+1, 0).getDate()
  420. for (var i = headerSize-1; i >= 0; i--) {
  421. list_model.append(createItemDay(new Date(lastMonthYear, lastMonthMonth,lastMonthDays-i)))
  422. }
  423. }
  424. const lastDayOfMonth = new Date(year, month+1, 0).getDate()
  425. for (let day = 1; day <= lastDayOfMonth; day++) {
  426. list_model.append(createItemDay(new Date(year, month,day)))
  427. }
  428. var footerSize = 49-list_model.count
  429. var nextMonthYear = year
  430. var nextMonth = month + 1
  431. if (month === 11) {
  432. nextMonthYear = year + 1
  433. nextMonth = 0
  434. }
  435. const nextDayOfMonth = new Date(nextMonthYear, nextMonth+1, 0).getDate()
  436. for (let j = 1; j <= footerSize; j++) {
  437. list_model.append(createItemDay(new Date(nextMonthYear, nextMonth,j)))
  438. }
  439. title.text = year+"年"+(month+1)+"月"
  440. }
  441. }