Warning, /libraries/kirigami-addons/src/dateandtime/private/DesktopDateInput.qml is written in an unsupported language. File is not indexed.

0001 /*
0002  *   SPDX-FileCopyrightText: 2019 David Edmundson <davidedmundson@kde.org>
0003  *
0004  *   SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 import QtQuick 2.3
0008 import QtQuick.Layouts 1.2
0009 import QtQuick.Controls 2.3
0010 
0011 import org.kde.kirigami 2.4 as Kirigami
0012 import org.kde.kirigamiaddons.dateandtime 0.1
0013 
0014 RowLayout {
0015     id: layout
0016 
0017     //DAVE - if I'm in an RTL country are my date formats pre-reversed?
0018     //i.e Wikipedia says afghan is d/m/yyyy   should year go on the left or the right?
0019 
0020     property date value
0021     property alias selectedDate: layout.value
0022 
0023     property string dateFormat: Qt.locale().dateFormat(Locale.ShortFormat)
0024 
0025 
0026     //date formats can be in big endian (china), little endian (Europe), or absolutely ridiculous endian (US)
0027     //separators are also different
0028     Component.onCompleted: {
0029         for (var i in layout.children) {
0030             layout.children[i].destroy();
0031         }
0032 
0033         var parse = /([^dMy]*)([dMy]+)([^dMy]*)([dMy]+)([^dMy]*)([dMy]+)([^dMy]*)/
0034         var parts = parse.exec(dateFormat);
0035         for(var i=1; i < parts.length; i++) {
0036             var part = parts[i];
0037 
0038             if (!part) {
0039                 continue;
0040             }
0041 
0042             if (part.startsWith("d")) {
0043                 daySelectComponent.createObject(layout);
0044             } else if (part.startsWith("M")) {
0045                 monthSelectComponent.createObject(layout);
0046             } else if (part.startsWith("y")) {
0047                 yearSelectComponent.createObject(layout);
0048             }
0049         }
0050     }
0051 
0052     Component {
0053         id: daySelectComponent
0054 
0055         SpinBox {
0056             function daysInMonth (year, month) {
0057                 // we want the days in @p month but go back one day from its start, therefore +1
0058                 return new Date(year, month + 1, 0).getDate();
0059             }
0060 
0061             from: 1
0062             to: {
0063                 return daysInMonth(layout.value.getFullYear(),
0064                                    layout.value.getMonth());
0065             }
0066             editable: true
0067             value: layout.value.getDate();
0068             onValueModified: {
0069                 var dt = layout.value;
0070                 dt.setDate(value);
0071                 layout.value = dt;
0072             }
0073         }
0074     }
0075 
0076     Component {
0077         id: monthSelectComponent
0078         ComboBox {
0079             id: combo
0080             // JS Date months start at 0, so we can map directly
0081             currentIndex: layout.value.getMonth() // DAVE should be a binding
0082             textRole: "display"
0083             model: YearModel {
0084                 year: layout.value.getFullYear()
0085             }
0086             onActivated: {
0087                 var dt = layout.value;
0088                 dt.setMonth(currentIndex);
0089                 layout.value = dt;
0090             }
0091         }
0092     }
0093 
0094     Component {
0095         id: yearSelectComponent
0096         SpinBox {
0097             from: 1970
0098             to: 2100 //I assume we'll have a new LTS release by then
0099             editable: true
0100             //default implementation does toLocaleString which looks super weird adding a comma
0101             textFromValue: function(value) {return value}
0102             value: layout.value.getFullYear();
0103             onValueModified: {
0104                 var dt = layout.value;
0105                 dt.setFullYear(value);
0106                 layout.value = dt;
0107             }
0108         }
0109     }
0110 }