1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
|
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Quick Controls module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
import QtQuick 2.2
import QtQuick.Controls 1.2
import QtQuick.Controls.Private 1.0
/*!
\qmltype CalendarStyle
\inqmlmodule QtQuick.Controls.Styles
\since 5.3
\ingroup controlsstyling
\brief Provides custom styling for \l Calendar
\section2 Component Map
\image calendarstyle-components-week-numbers.png
The calendar has the following styleable components:
\table
\row \li \image square-white.png
\li \l background
\li Fills the entire control.
\row \li \image square-yellow.png
\li \l navigationBar
\li
\row \li \image square-green.png
\li \l dayOfWeekDelegate
\li One instance per day of week.
\row \li \image square-red.png
\li \l weekNumberDelegate
\li One instance per week.
\row \li \image square-blue.png
\li \l dayDelegate
\li One instance per day of month.
\endtable
\section2 Custom Style Example
\qml
Calendar {
anchors.centerIn: parent
style: CalendarStyle {
gridVisible: false
dayDelegate: Rectangle {
gradient: Gradient {
GradientStop {
position: 0.00
color: styleData.selected ? "#111" : (styleData.visibleMonth && styleData.valid ? "#444" : "#666");
}
GradientStop {
position: 1.00
color: styleData.selected ? "#444" : (styleData.visibleMonth && styleData.valid ? "#111" : "#666");
}
GradientStop {
position: 1.00
color: styleData.selected ? "#777" : (styleData.visibleMonth && styleData.valid ? "#111" : "#666");
}
}
Label {
text: styleData.date.getDate()
anchors.centerIn: parent
color: styleData.valid ? "white" : "grey"
}
Rectangle {
width: parent.width
height: 1
color: "#555"
anchors.bottom: parent.bottom
}
Rectangle {
width: 1
height: parent.height
color: "#555"
anchors.right: parent.right
}
}
}
}
\endqml
*/
Style {
id: calendarStyle
/*!
The Calendar this style is attached to.
*/
readonly property Calendar control: __control
/*!
The color of the grid lines.
*/
property color gridColor: "#d3d3d3"
/*!
This property determines the visibility of the grid.
The default value is \c true.
*/
property bool gridVisible: true
/*!
\internal
The width of each grid line.
*/
property real __gridLineWidth: 1
/*! \internal */
property color __horizontalSeparatorColor: gridColor
/*! \internal */
property color __verticalSeparatorColor: gridColor
function __cellRectAt(index) {
return CalendarUtils.cellRectAt(index, control.__panel.columns, control.__panel.rows,
control.__panel.availableWidth, control.__panel.availableHeight, gridVisible ? __gridLineWidth : 0);
}
function __isValidDate(date) {
return date !== undefined
&& date.getTime() >= control.minimumDate.getTime()
&& date.getTime() <= control.maximumDate.getTime();
}
/*!
The background of the calendar.
The implicit size of the calendar is calculated based on the implicit size of the background delegate.
*/
property Component background: Rectangle {
color: "#fff"
implicitWidth: Math.max(250, Math.round(TextSingleton.implicitHeight * 14))
implicitHeight: Math.max(250, Math.round(TextSingleton.implicitHeight * 14))
}
/*!
The navigation bar of the calendar.
Styles the bar at the top of the calendar that contains the
next month/previous month buttons and the selected date label.
The properties provided to the delegate are:
\table
\row \li readonly property string \b styleData.title
\li The title of the calendar.
\endtable
*/
property Component navigationBar: Rectangle {
height: Math.round(TextSingleton.implicitHeight * 2.73)
color: "#f9f9f9"
Rectangle {
color: Qt.rgba(1,1,1,0.6)
height: 1
width: parent.width
}
Rectangle {
anchors.bottom: parent.bottom
height: 1
width: parent.width
color: "#ddd"
}
HoverButton {
id: previousMonth
width: parent.height
height: width
anchors.verticalCenter: parent.verticalCenter
anchors.left: parent.left
source: "images/leftanglearrow.png"
onClicked: control.showPreviousMonth()
}
Label {
id: dateText
text: styleData.title
elide: Text.ElideRight
horizontalAlignment: Text.AlignHCenter
font.pixelSize: TextSingleton.implicitHeight * 1.25
anchors.verticalCenter: parent.verticalCenter
anchors.left: previousMonth.right
anchors.leftMargin: 2
anchors.right: nextMonth.left
anchors.rightMargin: 2
}
HoverButton {
id: nextMonth
width: parent.height
height: width
anchors.verticalCenter: parent.verticalCenter
anchors.right: parent.right
source: "images/rightanglearrow.png"
onClicked: control.showNextMonth()
}
}
/*!
The delegate that styles each date in the calendar.
The properties provided to each delegate are:
\table
\row \li readonly property date \b styleData.date
\li The date this delegate represents.
\row \li readonly property bool \b styleData.selected
\li \c true if this is the selected date.
\row \li readonly property int \b styleData.index
\li The index of this delegate.
\row \li readonly property bool \b styleData.valid
\li \c true if this date is greater than or equal to than \l {Calendar::minimumDate}{minimumDate} and
less than or equal to \l {Calendar::maximumDate}{maximumDate}.
\row \li readonly property bool \b styleData.today
\li \c true if this date is equal to today's date.
\row \li readonly property bool \b styleData.visibleMonth
\li \c true if the month in this date is the visible month.
\row \li readonly property bool \b styleData.hovered
\li \c true if the mouse is over this cell.
\note This property is \c true even when the mouse is hovered over an invalid date.
\row \li readonly property bool \b styleData.pressed
\li \c true if the mouse is pressed on this cell.
\note This property is \c true even when the mouse is pressed on an invalid date.
\endtable
*/
property Component dayDelegate: Rectangle {
anchors.fill: parent
anchors.leftMargin: (!addExtraMargin || control.weekNumbersVisible) && styleData.index % CalendarUtils.daysInAWeek === 0 ? 0 : -1
anchors.rightMargin: !addExtraMargin && styleData.index % CalendarUtils.daysInAWeek === CalendarUtils.daysInAWeek - 1 ? 0 : -1
anchors.bottomMargin: !addExtraMargin && styleData.index >= CalendarUtils.daysInAWeek * (CalendarUtils.weeksOnACalendarMonth - 1) ? 0 : -1
anchors.topMargin: styleData.selected ? -1 : 0
color: styleData.date !== undefined && styleData.selected ? selectedDateColor : "transparent"
readonly property bool addExtraMargin: control.frameVisible && styleData.selected
readonly property color sameMonthDateTextColor: "#444"
readonly property color selectedDateColor: Qt.platform.os === "osx" ? "#3778d0" : SystemPaletteSingleton.highlight(control.enabled)
readonly property color selectedDateTextColor: "white"
readonly property color differentMonthDateTextColor: "#bbb"
readonly property color invalidDateColor: "#dddddd"
Label {
id: dayDelegateText
text: styleData.date.getDate()
anchors.centerIn: parent
horizontalAlignment: Text.AlignRight
font.pixelSize: Math.min(parent.height/3, parent.width/3)
color: {
var theColor = invalidDateColor;
if (styleData.valid) {
// Date is within the valid range.
theColor = styleData.visibleMonth ? sameMonthDateTextColor : differentMonthDateTextColor;
if (styleData.selected)
theColor = selectedDateTextColor;
}
theColor;
}
}
}
/*!
The delegate that styles each weekday.
The height of the weekday row is calculated based on the maximum implicit height of the delegates.
The properties provided to each delegate are:
\table
\row \li readonly property int \b styleData.index
\li The index (0-6) of the delegate.
\row \li readonly property int \b styleData.dayOfWeek
\li The day of the week this delegate represents. Possible values:
\list
\li \c Locale.Sunday
\li \c Locale.Monday
\li \c Locale.Tuesday
\li \c Locale.Wednesday
\li \c Locale.Thursday
\li \c Locale.Friday
\li \c Locale.Saturday
\endlist
\endtable
*/
property Component dayOfWeekDelegate: Rectangle {
color: gridVisible ? "#fcfcfc" : "transparent"
implicitHeight: Math.round(TextSingleton.implicitHeight * 2.25)
Label {
text: control.locale.dayName(styleData.dayOfWeek, control.dayOfWeekFormat)
anchors.centerIn: parent
}
}
/*!
The delegate that styles each week number.
The width of the week number column is calculated based on the maximum implicit width of the delegates.
The properties provided to each delegate are:
\table
\row \li readonly property int \b styleData.index
\li The index (0-5) of the delegate.
\row \li readonly property int \b styleData.weekNumber
\li The number of the week this delegate represents.
\endtable
*/
property Component weekNumberDelegate: Rectangle {
implicitWidth: Math.round(TextSingleton.implicitHeight * 2)
Label {
text: styleData.weekNumber
anchors.centerIn: parent
color: "#444"
}
}
/*! \internal */
property Component panel: Item {
id: panelItem
implicitWidth: backgroundLoader.implicitWidth
implicitHeight: backgroundLoader.implicitHeight
property alias navigationBarItem: navigationBarLoader.item
property alias dayOfWeekHeaderRow: dayOfWeekHeaderRow
readonly property int weeksToShow: 6
readonly property int rows: weeksToShow
readonly property int columns: CalendarUtils.daysInAWeek
// The combined available width and height to be shared amongst each cell.
readonly property real availableWidth: viewContainer.width
readonly property real availableHeight: viewContainer.height
property int hoveredCellIndex: -1
property int pressedCellIndex: -1
property int pressCellIndex: -1
Rectangle {
anchors.fill: parent
color: "transparent"
border.color: gridColor
visible: control.frameVisible
}
Item {
id: container
anchors.fill: parent
anchors.margins: control.frameVisible ? 1 : 0
Loader {
id: backgroundLoader
anchors.fill: parent
sourceComponent: background
}
Loader {
id: navigationBarLoader
anchors.left: parent.left
anchors.right: parent.right
anchors.top: parent.top
sourceComponent: navigationBar
active: control.navigationBarVisible
property QtObject styleData: QtObject {
readonly property string title: control.locale.standaloneMonthName(control.visibleMonth)
+ new Date(control.visibleYear, control.visibleMonth, 1).toLocaleDateString(control.locale, " yyyy")
}
}
Row {
id: dayOfWeekHeaderRow
anchors.top: navigationBarLoader.bottom
anchors.left: parent.left
anchors.leftMargin: (control.weekNumbersVisible ? weekNumbersItem.width : 0)
anchors.right: parent.right
spacing: gridVisible ? __gridLineWidth : 0
Repeater {
id: repeater
model: CalendarHeaderModel {
locale: control.locale
}
Loader {
id: dayOfWeekDelegateLoader
sourceComponent: dayOfWeekDelegate
width: __cellRectAt(index).width
readonly property int __index: index
readonly property var __dayOfWeek: dayOfWeek
property QtObject styleData: QtObject {
readonly property alias index: dayOfWeekDelegateLoader.__index
readonly property alias dayOfWeek: dayOfWeekDelegateLoader.__dayOfWeek
}
}
}
}
Rectangle {
id: topGridLine
color: __horizontalSeparatorColor
width: parent.width
height: __gridLineWidth
visible: gridVisible
anchors.top: dayOfWeekHeaderRow.bottom
}
Row {
id: gridRow
width: weekNumbersItem.width + viewContainer.width
height: viewContainer.height
anchors.top: topGridLine.bottom
Column {
id: weekNumbersItem
visible: control.weekNumbersVisible
height: viewContainer.height
spacing: gridVisible ? __gridLineWidth : 0
Repeater {
id: weekNumberRepeater
model: panelItem.weeksToShow
Loader {
id: weekNumberDelegateLoader
height: __cellRectAt(index * panelItem.columns).height
sourceComponent: weekNumberDelegate
readonly property int __index: index
property int __weekNumber: control.__model.weekNumberAt(index)
Connections {
target: control
onVisibleMonthChanged: __weekNumber = control.__model.weekNumberAt(index)
onVisibleYearChanged: __weekNumber = control.__model.weekNumberAt(index)
}
Connections {
target: control.__model
onCountChanged: __weekNumber = control.__model.weekNumberAt(index)
}
property QtObject styleData: QtObject {
readonly property alias index: weekNumberDelegateLoader.__index
readonly property int weekNumber: weekNumberDelegateLoader.__weekNumber
}
}
}
}
Rectangle {
id: separator
anchors.topMargin: - dayOfWeekHeaderRow.height - 1
anchors.top: weekNumbersItem.top
anchors.bottom: weekNumbersItem.bottom
width: __gridLineWidth
color: __verticalSeparatorColor
visible: control.weekNumbersVisible
}
// Contains the grid lines and the grid itself.
Item {
id: viewContainer
width: container.width - (control.weekNumbersVisible ? weekNumbersItem.width + separator.width : 0)
height: container.height - navigationBarLoader.height - dayOfWeekHeaderRow.height - topGridLine.height
Repeater {
id: verticalGridLineRepeater
model: panelItem.columns - 1
delegate: Rectangle {
x: __cellRectAt(index + 1).x - __gridLineWidth
y: 0
width: __gridLineWidth
height: viewContainer.height
color: gridColor
visible: gridVisible
}
}
Repeater {
id: horizontalGridLineRepeater
model: panelItem.rows - 1
delegate: Rectangle {
x: 0
y: __cellRectAt((index + 1) * panelItem.columns).y - __gridLineWidth
width: viewContainer.width
height: __gridLineWidth
color: gridColor
visible: gridVisible
}
}
MouseArea {
id: mouseArea
anchors.fill: parent
hoverEnabled: Settings.hoverEnabled
function cellIndexAt(mouseX, mouseY) {
var viewContainerPos = viewContainer.mapFromItem(mouseArea, mouseX, mouseY);
var child = viewContainer.childAt(viewContainerPos.x, viewContainerPos.y);
// In the tests, the mouseArea sometimes gets picked instead of the cells,
// probably because stuff is still loading. To be safe, we check for that here.
return child && child !== mouseArea ? child.__index : -1;
}
onEntered: {
hoveredCellIndex = cellIndexAt(mouseX, mouseY);
if (hoveredCellIndex === undefined) {
hoveredCellIndex = cellIndexAt(mouseX, mouseY);
}
var date = view.model.dateAt(hoveredCellIndex);
if (__isValidDate(date)) {
control.hovered(date);
}
}
onExited: {
hoveredCellIndex = -1;
}
onPositionChanged: {
var indexOfCell = cellIndexAt(mouse.x, mouse.y);
var previousHoveredCellIndex = hoveredCellIndex;
hoveredCellIndex = indexOfCell;
if (indexOfCell !== -1) {
var date = view.model.dateAt(indexOfCell);
if (__isValidDate(date)) {
if (hoveredCellIndex !== previousHoveredCellIndex)
control.hovered(date);
// The date must be different for the pressed signal to be emitted.
if (pressed && date.getTime() !== control.selectedDate.getTime()) {
control.pressed(date);
// You can't select dates in a different month while dragging.
if (date.getMonth() === control.selectedDate.getMonth()) {
control.selectedDate = date;
pressedCellIndex = indexOfCell;
}
}
}
}
}
onPressed: {
pressCellIndex = cellIndexAt(mouse.x, mouse.y);
if (pressCellIndex !== -1) {
var date = view.model.dateAt(pressCellIndex);
pressedCellIndex = pressCellIndex;
if (__isValidDate(date)) {
control.selectedDate = date;
control.pressed(date);
}
}
}
onReleased: {
var indexOfCell = cellIndexAt(mouse.x, mouse.y);
if (indexOfCell !== -1) {
// The cell index might be valid, but the date has to be too. We could let the
// selected date validation take care of this, but then the selected date would
// change to the earliest day if a day before the minimum date is clicked, for example.
var date = view.model.dateAt(indexOfCell);
if (__isValidDate(date)) {
control.released(date);
}
}
pressedCellIndex = -1;
}
onClicked: {
var indexOfCell = cellIndexAt(mouse.x, mouse.y);
if (indexOfCell !== -1 && indexOfCell === pressCellIndex) {
var date = view.model.dateAt(indexOfCell);
if (__isValidDate(date))
control.clicked(date);
}
}
onDoubleClicked: {
var indexOfCell = cellIndexAt(mouse.x, mouse.y);
if (indexOfCell !== -1) {
var date = view.model.dateAt(indexOfCell);
if (__isValidDate(date))
control.doubleClicked(date);
}
}
onPressAndHold: {
var indexOfCell = cellIndexAt(mouse.x, mouse.y);
if (indexOfCell !== -1 && indexOfCell === pressCellIndex) {
var date = view.model.dateAt(indexOfCell);
if (__isValidDate(date))
control.pressAndHold(date);
}
}
}
Connections {
target: control
onSelectedDateChanged: view.selectedDateChanged()
}
Repeater {
id: view
property int currentIndex: -1
model: control.__model
Component.onCompleted: selectedDateChanged()
function selectedDateChanged() {
if (model !== undefined && model.locale !== undefined) {
currentIndex = model.indexAt(control.selectedDate);
}
}
delegate: Loader {
id: delegateLoader
x: __cellRectAt(index).x
y: __cellRectAt(index).y
width: __cellRectAt(index).width
height: __cellRectAt(index).height
sourceComponent: dayDelegate
readonly property int __index: index
readonly property date __date: date
// We rely on the fact that an invalid QDate will be converted to a Date
// whose year is -4713, which is always an invalid date since our
// earliest minimum date is the year 1.
readonly property bool valid: __isValidDate(date)
property QtObject styleData: QtObject {
readonly property alias index: delegateLoader.__index
readonly property bool selected: control.selectedDate.getTime() === date.getTime()
readonly property alias date: delegateLoader.__date
readonly property bool valid: delegateLoader.valid
// TODO: this will not be correct if the app is running when a new day begins.
readonly property bool today: date.getTime() === new Date().setHours(0, 0, 0, 0)
readonly property bool visibleMonth: date.getMonth() === control.visibleMonth
readonly property bool hovered: panelItem.hoveredCellIndex == index
readonly property bool pressed: panelItem.pressedCellIndex == index
// todo: pressed property here, clicked and doubleClicked in the control itself
}
}
}
}
}
}
}
}
|