Warning, /pim/kube/views/calendar/qml/DateSelector.qml is written in an unsupported language. File is not indexed.

0001 /*
0002  *  Copyright (C) 2018 Christian Mollekopf, <mollekopf@kolabsys.com>
0003  *
0004  *  This program is free software; you can redistribute it and/or modify
0005  *  it under the terms of the GNU General Public License as published by
0006  *  the Free Software Foundation; either version 2 of the License, or
0007  *  (at your option) any later version.
0008  *
0009  *  This program is distributed in the hope that it will be useful,
0010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0012  *  GNU General Public License for more details.
0013  *
0014  *  You should have received a copy of the GNU General Public License along
0015  *  with this program; if not, write to the Free Software Foundation, Inc.,
0016  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
0017  */
0018 import QtQuick 2.4
0019 import QtQuick.Controls 2.2
0020 import Qt.labs.calendar 1.0
0021 import QtQuick.Layouts 1.3
0022 
0023 import "dateutils.js" as DateUtils
0024 
0025 import org.kube.framework 1.0 as Kube
0026 
0027 Item {
0028     id: root
0029     property date selectedDate
0030     property var selectedEnd: null
0031     property date notBefore: new Date(0) //Earliest possible by epoch
0032     property color backgroundColor: Kube.Colors.darkBackgroundColor
0033     property color textColor: Kube.Colors.highlightedTextColor
0034     property bool invertIcons: true
0035     property bool rangeSelection: false
0036 
0037 
0038     // Private
0039     property date currentDate: selectedDate
0040 
0041     function next() {
0042         root.currentDate = DateUtils.nextMonth(root.currentDate)
0043     }
0044     function previous() {
0045         root.currentDate = DateUtils.previousMonth(root.currentDate)
0046     }
0047     signal selected(date date)
0048     signal endSelected(date date)
0049 
0050     implicitWidth: Math.max(grid.implicitWidth, dateLabel.implicitWidth + 2 * Kube.Units.gridUnit)
0051     implicitHeight: column.implicitHeight
0052 
0053     onNotBeforeChanged: {
0054         if (notBefore.getTime() > selectedDate.getTime()) {
0055             root.selected(notBefore)
0056         }
0057     }
0058 
0059     Column {
0060         id: column
0061         anchors.fill: parent
0062         spacing: Kube.Units.smallSpacing
0063         Item {
0064             anchors {
0065                 left: parent.left
0066                 right: parent.right
0067             }
0068             height: Kube.Units.gridUnit
0069             Kube.IconButton {
0070                 anchors {
0071                     verticalCenter: parent.verticalCenter
0072                     left: parent.left
0073                 }
0074                 height: parent.height
0075                 width: parent.height
0076                 color: root.backgroundColor
0077                 iconName: Kube.Icons.iconName(Kube.Icons.goBack, root.invertIcons)
0078                 visible: root.notBefore.getTime() < (new Date(root.selectedDate.getFullYear(), root.selectedDate.getMonth(), root.selectedDate.getDate())).getTime()
0079                 onClicked: {
0080                     root.previous()
0081                 }
0082             }
0083             Kube.Label {
0084                 id: dateLabel
0085                 anchors {
0086                     verticalCenter: parent.verticalCenter
0087                     horizontalCenter: parent.horizontalCenter
0088                 }
0089                 color: root.textColor
0090                 font.bold: true
0091                 text: root.currentDate.toLocaleString(Qt.locale(), "MMMM yyyy")
0092             }
0093             Kube.IconButton {
0094                 anchors {
0095                     verticalCenter: parent.verticalCenter
0096                     right: parent.right
0097                 }
0098                 height: parent.height
0099                 width: parent.height
0100                 color: root.backgroundColor
0101                 iconName: Kube.Icons.iconName(Kube.Icons.goNext, root.invertIcons)
0102                 onClicked: {
0103                     root.next()
0104                 }
0105             }
0106         }
0107 
0108         MonthGrid {
0109             id: grid
0110             anchors {
0111                 left: parent.left
0112                 right: parent.right
0113             }
0114 
0115             month: root.currentDate.getMonth()
0116             year: root.currentDate.getFullYear()
0117             locale: Qt.locale()
0118 
0119             contentItem: GridLayout {
0120                 rows: 6
0121                 columns: 7
0122                 rowSpacing: grid.spacing
0123                 columnSpacing: grid.spacing
0124 
0125                 Repeater {
0126                     model: grid.source
0127                     delegate: grid.delegate
0128                 }
0129             }
0130 
0131             delegate: Text {
0132                 horizontalAlignment: Text.AlignHCenter
0133                 verticalAlignment: Text.AlignVCenter
0134                 opacity: (model.month === grid.month && model.date.getTime() >= root.notBefore.getTime()) ? 1 : 0.5
0135                 text: model.day
0136                 font: grid.font
0137                 color: root.textColor
0138                 Layout.preferredHeight: Kube.Units.gridUnit / 4 * 3
0139                 Layout.fillWidth: true
0140 
0141                 Rectangle {
0142                     anchors {
0143                         left: parent.left
0144                         right: parent.right
0145                         bottom: parent.bottom
0146                     }
0147                     function dateIsInRange(day, month, selectedDate, selectedEnd) {
0148                         const startMonth = selectedDate.getMonth();
0149                         const startDay = selectedDate.getDate();
0150                         if (day === startDay && month === startMonth) {
0151                             return true;
0152                         }
0153                         if (selectedEnd) {
0154                             const endMonth = selectedEnd.getMonth();
0155                             const endDay = selectedEnd.getDate();
0156                             if (
0157                                 (month < endMonth && month === startMonth && day >= startDay) ||
0158                                 (month > startMonth && month < endMonth) ||
0159                                 (month > startMonth && month === endMonth && day <= endDay) ||
0160                                 (month === startMonth && day >= startDay && day <= endDay)
0161                             ) {
0162                                 return true;
0163                             }
0164                         }
0165                         return false;
0166                     }
0167                     height: 2
0168                     color: Kube.Colors.plasmaBlue
0169                     opacity: 0.6
0170                     visible: dateIsInRange(model.day, model.month, root.selectedDate, root.selectedEnd)
0171                 }
0172             }
0173 
0174             onClicked: {
0175                 if (
0176                     root.rangeSelection && root.selectedDate && !root.selectedEnd &&
0177                     date.getTime() >= root.selectedDate.getTime()
0178                 ) {
0179                     //Select the end of the range
0180                     root.selectedEnd = date
0181                     root.endSelected(date)
0182                 } else {
0183                     if (date.getTime() >= root.notBefore.getTime()) {
0184                         //Set the start
0185                         root.selectedEnd = null
0186                         root.selected(date)
0187                         root.endSelected(date)
0188                     }
0189                 }
0190             }
0191         }
0192     }
0193 }