DateTimeEdit.qml 2.16 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
    Copyright (C) 2018 Volker Krause <vkrause@kde.org>

    This program is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This program is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
    License for more details.

    You should have received a copy of the GNU General Public License
15
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
16 17
*/

18
import QtQuick 2.7
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
import QtQuick.Layouts 1.1
import QtQuick.Controls 2.1 as QQC2
import org.kde.kirigami 2.0 as Kirigami
import org.kde.kitinerary 1.0
import org.kde.itinerary 1.0
import "." as App

MouseArea {
    id: root

    property variant obj
    property string propertyName
    property date value: Util.dateTimeStripTimezone(obj, propertyName)
    property bool isModified: Util.dateTimeStripTimezone(obj, propertyName).getTime() != value.getTime()

    implicitHeight: layout.implicitHeight
    implicitWidth: layout.implicitWidth

    RowLayout {
        id: layout

        QQC2.Label {
            text: isModified ? value.toLocaleString(Qt.locale(), Locale.ShortFormat) : Localizer.formatDateTime(obj, propertyName)
        }

        Kirigami.Icon {
            source: "document-edit"
            width: Kirigami.Units.iconSizes.smallMedium
            height: width
        }
    }

    // FIXME super ugly, we reference stuff from our parent here
    onClicked: {
        dateTimeEditSheet.value = isNaN(root.value.getTime()) ? new Date() : root.value;
        dateTimeEditSheet.sheetOpen = true;
        conn.enabled = true;
    }

    Connections {
        id: conn
        enabled: false
        target: dateTimeEditSheet
        onSheetOpenChanged: {
            console.log(root.value, dateTimeEditSheet.value);
            root.value = dateTimeEditSheet.value;
            root.value.setSeconds(0, 0);
            enabled = false;
        }
    }
}