Warning, /plasma/plasma-workspace/components/calendar/qml/DaysCalendar.qml is written in an unsupported language. File is not indexed.

0001 /*
0002     SPDX-FileCopyrightText: 2013 Heena Mahour <heena393@gmail.com>
0003     SPDX-FileCopyrightText: 2013 Sebastian Kügler <sebas@kde.org>
0004     SPDX-FileCopyrightText: 2015, 2016 Kai Uwe Broulik <kde@privat.broulik.de>
0005     SPDX-FileCopyrightText: 2021 Carl Schwan <carlschwan@kde.org>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 import QtQuick
0011 import QtQuick.Layouts 1.1
0012 
0013 import org.kde.plasma.workspace.calendar 2.0
0014 import org.kde.plasma.components 3.0 as PlasmaComponents3
0015 import org.kde.kirigami 2.20 as Kirigami
0016 
0017 Item {
0018     id: daysCalendar
0019 
0020     signal activated(int index, var date, var item)
0021 
0022     readonly property int gridColumns: showWeekNumbers ? calendarGrid.columns + 1 : calendarGrid.columns
0023 
0024     property int rows
0025     property int columns
0026 
0027     property bool showWeekNumbers
0028 
0029     // how precise date matching should be, 3 = day+month+year, 2 = month+year, 1 = just year
0030     property int dateMatchingPrecision
0031 
0032     property alias repeater: repeater
0033     property alias headerModel: days.model
0034     property alias gridModel: repeater.model
0035 
0036     // Take the calendar width, subtract the inner and outer spacings and divide by number of columns (==days in week)
0037     readonly property int cellWidth: Math.floor((swipeView.width - (daysCalendar.columns + 1) * root.borderWidth) / (daysCalendar.columns + (showWeekNumbers ? 1 : 0)))
0038     // Take the calendar height, subtract the inner spacings and divide by number of rows (root.weeks + one row for day names)
0039     readonly property int cellHeight:  Math.floor((swipeView.height - viewHeader.heading.height - calendarGrid.rows * root.borderWidth) / calendarGrid.rows)
0040 
0041     Column {
0042         id: weeksColumn
0043         visible: showWeekNumbers
0044         anchors {
0045             top: parent.top
0046             left: parent.left
0047             bottom: parent.bottom
0048             // The borderWidth needs to be counted twice here because it goes
0049             // in fact through two lines - the topmost one (the outer edge)
0050             // and then the one below weekday strings
0051             topMargin: daysCalendar.cellHeight + root.borderWidth + root.borderWidth
0052         }
0053         spacing: root.borderWidth
0054 
0055         Repeater {
0056             model: showWeekNumbers ? calendarBackend.weeksModel : []
0057 
0058             PlasmaComponents3.Label {
0059                 height: daysCalendar.cellHeight
0060                 width: daysCalendar.cellWidth
0061                 horizontalAlignment: Text.AlignHCenter
0062                 verticalAlignment: Text.AlignVCenter
0063                 opacity: 0.4
0064                 text: modelData
0065                 textFormat: Text.PlainText
0066                 font.pixelSize: Math.max(Kirigami.Theme.smallFont.pixelSize, daysCalendar.cellHeight / 3)
0067             }
0068         }
0069     }
0070 
0071     Grid {
0072         id: calendarGrid
0073 
0074         anchors {
0075             top: parent.top
0076             right: parent.right
0077             rightMargin: root.borderWidth
0078             bottom: parent.bottom
0079             bottomMargin: root.borderWidth
0080         }
0081 
0082         columns: daysCalendar.columns
0083         rows: daysCalendar.rows + (daysCalendar.headerModel ? 1 : 0)
0084 
0085         spacing: root.borderWidth
0086         columnSpacing: parent.squareCell ? (daysCalendar.width - daysCalendar.columns * (daysCalendar.cellWidth - root.borderWidth)) / daysCalendar.columns : root.borderWidth
0087 
0088         Repeater {
0089             id: days
0090 
0091             PlasmaComponents3.Label {
0092                 width: daysCalendar.cellWidth
0093                 height: daysCalendar.cellHeight
0094                 text: Qt.locale(Qt.locale().uiLanguages[0]).dayName(((calendarBackend.firstDayOfWeek + index) % days.count), Locale.ShortFormat)
0095                 textFormat: Text.PlainText
0096                 font.pixelSize: Math.max(Kirigami.Theme.smallFont.pixelSize, daysCalendar.cellHeight / 3)
0097                 opacity: 0.4
0098                 horizontalAlignment: Text.AlignHCenter
0099                 verticalAlignment: Text.AlignVCenter
0100                 elide: Text.ElideRight
0101                 fontSizeMode: Text.HorizontalFit
0102             }
0103         }
0104 
0105         Repeater {
0106             id: repeater
0107 
0108             DayDelegate {
0109                 id: delegate
0110                 width: daysCalendar.cellWidth
0111                 height: daysCalendar.cellHeight
0112                 dayModel: repeater.model
0113 
0114                 Keys.onPressed: event => {
0115                     if (!daysCalendar.PlasmaComponents3.SwipeView.isCurrentItem) {
0116                         event.accepted = false;
0117                         return;
0118                     }
0119                     switch (event.key) {
0120                     case Qt.Key_Space:
0121                     case Qt.Key_Enter:
0122                     case Qt.Key_Return:
0123                     case Qt.Key_Select:
0124                         daysCalendar.activated(index, model, delegate);
0125                         break;
0126                     }
0127                 }
0128 
0129                 KeyNavigation.left: if (index !== 0) {
0130                     return repeater.itemAt(index - 1);
0131                 } else {
0132                     return daysCalendar.KeyNavigation.left;
0133                 }
0134                 KeyNavigation.tab: daysCalendar.KeyNavigation.tab
0135 
0136                 Keys.onUpPressed: event => {
0137                     if (index >= daysCalendar.columns) {
0138                         repeater.itemAt(index - daysCalendar.columns).forceActiveFocus(Qt.TabFocusReason);
0139                     } else {
0140                         event.accepted = false;
0141                     }
0142                 }
0143                 Keys.onDownPressed: {
0144                     if (index < (daysCalendar.rows - 1) * daysCalendar.columns) {
0145                         repeater.itemAt(index + daysCalendar.columns).forceActiveFocus(Qt.TabFocusReason);
0146                     }
0147                 }
0148 
0149                 onClicked: {
0150                     daysCalendar.activated(index, model, delegate)
0151                 }
0152             }
0153         }
0154     }
0155 }
0156