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

0001 /*
0002  *  Copyright (C) 2019 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 
0019 import QtQuick 2
0020 import QtQuick.Layouts 1
0021 import QtQuick.Controls 2
0022 import org.kube.framework 1.0 as Kube
0023 
0024 
0025 import "dateutils.js" as DateUtils
0026 
0027 ColumnLayout {
0028     id: root
0029 
0030     property date initialStart
0031     onInitialStartChanged: {
0032         startDate.dateTime = initialStart
0033     }
0034     property date initialEnd
0035     onInitialEndChanged: {
0036         endDate.dateTime = initialEnd
0037     }
0038     property alias allDay: checkBox.checked
0039     property date start: initialStart
0040     property date end: initialEnd
0041 
0042     function setAllDay() {
0043         if (DateUtils.sameDay(start, end)) {
0044             // Default to true when switching to multiday
0045             root.allDay = false
0046         } else {
0047             root.allDay = true
0048         }
0049     }
0050 
0051     property var notBefore: new Date(0)
0052     spacing: Kube.Units.smallSpacing
0053 
0054     Kube.Button {
0055         id: button
0056 
0057         Layout.preferredWidth: implicitWidth
0058 
0059         function formatString(start, end, allDay) {
0060             var startDate = start.toLocaleDateString();
0061             if (allDay) {
0062                 if (DateUtils.sameDay(start, end)) {
0063                     // Tuesday, April 4, 2023
0064                     return startDate;
0065                 } else {
0066                     // Tuesday, April 4, 2023 - Wednesday, April 12, 2023
0067                     return startDate + " " +
0068                         " - " +
0069                         end.toLocaleDateString();
0070                 }
0071             }
0072             if (DateUtils.sameDay(start, end)) {
0073                 // Tuesday, April 4, 2023 12:30 - 13:00
0074                 return startDate + " " +
0075                     start.toLocaleTimeString(Qt.locale(), "hh:mm") +
0076                     " - " +
0077                     end.toLocaleTimeString(Qt.locale(), "hh:mm");
0078             }
0079             // Tuesday, April 4, 2023 12:30 - Wednesday, April 12, 2023 13:00
0080             return startDate + " " +
0081                 start.toLocaleTimeString(Qt.locale(), "hh:mm") +
0082                 " - " +
0083                 end.toLocaleDateString() + " " +
0084                 end.toLocaleTimeString(Qt.locale(), "hh:mm");
0085         }
0086 
0087         text: formatString(root.start, root.end, root.allDay)
0088 
0089         onClicked: {
0090             popup.open()
0091         }
0092 
0093         Kube.Popup {
0094             id: popup
0095 
0096             x: button.x
0097             y: button.y
0098             width: Math.max(selector.implicitWidth + Kube.Units.largeSpacing * 7, button.width)
0099             height: buttonOverlay.height + layout.implicitHeight + 2 * Kube.Units.smallSpacing
0100             modal: false
0101             focus: true
0102             padding: 0
0103             background: Rectangle {
0104                 anchors.fill: parent
0105                 color: Kube.Colors.viewBackgroundColor
0106                 border.color: Kube.Colors.buttonColor
0107                 border.width: 1
0108             }
0109             Kube.Button {
0110                 id: buttonOverlay
0111                 anchors {
0112                     top: parent.top
0113                     left: parent.left
0114                     right: parent.right
0115                 }
0116                 height: button.height
0117                 text: button.formatString(root.start, root.end, root.allDay)
0118                 onClicked: popup.close()
0119             }
0120 
0121             ColumnLayout {
0122                 id: layout
0123                 anchors {
0124                     top: buttonOverlay.bottom
0125                     bottom: parent.bottom
0126                     left: parent.left
0127                     right: parent.right
0128                     topMargin: Kube.Units.smallSpacing
0129                     leftMargin: Kube.Units.smallSpacing
0130                     rightMargin: Kube.Units.smallSpacing
0131                     bottomMargin: Kube.Units.smallSpacing
0132                 }
0133 
0134                 DateSelector {
0135                     id: selector
0136                     notBefore: root.notBefore
0137                     backgroundColor: Kube.Colors.backgroundColor
0138                     textColor: Kube.Colors.textColor
0139                     invertIcons: false
0140                     rangeSelection: true
0141                     selectedDate: root.start
0142                     selectedEnd: root.end
0143                     onSelected: {
0144                         root.start = DateUtils.applyTimeFromDate(date, root.start)
0145                         root.setAllDay()
0146                     }
0147                     onEndSelected: {
0148                         root.end = DateUtils.applyTimeFromDate(date, root.end)
0149                         root.setAllDay()
0150                     }
0151                 }
0152 
0153                 RowLayout {
0154                     spacing: Kube.Units.smallSpacing
0155                     Kube.CheckBox {
0156                         id: checkBox
0157                     }
0158                     Kube.Label {
0159                         text: qsTr("All day")
0160                     }
0161                 }
0162 
0163                 RowLayout {
0164                     visible: !root.allDay
0165                     Kube.Label {
0166                         text: qsTr("begins")
0167                     }
0168                     TimeSelector {
0169                         id: startDate
0170                         Layout.preferredWidth: Kube.Units.gridUnit * 3
0171                         objectName: "startDate"
0172                         dateTime: root.start
0173                         onDateTimeChanged: {
0174                             root.start = dateTime
0175                         }
0176                     }
0177                     Kube.Label {
0178                         text: qsTr("ends")
0179                     }
0180                     TimeSelector {
0181                         id: endDate
0182                         Layout.preferredWidth: Kube.Units.gridUnit * 3
0183                         objectName: "endDate"
0184                         dateTime: root.end
0185                         notBefore: root.start
0186                         onDateTimeChanged: {
0187                             root.end = dateTime
0188                         }
0189                     }
0190                 }
0191             }
0192         }
0193     }
0194 }