Warning, /pim/itinerary/src/app/FormPriceEditDelegate.qml is written in an unsupported language. File is not indexed.

0001 // SPDX-FileCopyrightText: 2023 Volker Krause <vkrause@kde.org>
0002 // SPDX-License-Identifier: LGPL-2.0-or-later
0003 
0004 import QtQuick
0005 import QtQuick.Layouts
0006 import QtQuick.Controls as QQC2
0007 import org.kde.kirigami as Kirigami
0008 import org.kde.kirigamiaddons.formcard as FormCard
0009 import org.kde.i18n.localeData
0010 import org.kde.kitinerary
0011 import org.kde.itinerary
0012 
0013 FormCard.AbstractFormDelegate {
0014     id: root
0015 
0016     /** The element of which the price properties should be edited. */
0017     property var item
0018     /** Default currency if no price is already set on @p item. */
0019     property string defaultCurrency: Country.fromAlpha2(Settings.homeCountryIsoCode).currencyCode
0020 
0021     /** Save the current value/currency to @p item. */
0022     function apply(item)
0023     {
0024         // TODO deal with the price being set on the nested ticket
0025         item.totalPrice = Number.fromLocaleString(Qt.locale(), priceEdit.text);
0026         item.priceCurrency = currencyBox.currentText;
0027     }
0028 
0029     // input validation, as in FormTextFieldDelegate
0030     property alias status: formErrorHandler.type
0031     property alias statusMessage: formErrorHandler.text
0032 
0033     text: i18n("Price")
0034     visible: PriceUtil.canHavePrice(root.item)
0035 
0036     focusPolicy: Qt.NoFocus
0037     onActiveFocusChanged: {
0038         if (activeFocus) {
0039             priceEdit.forceActiveFocus();
0040         }
0041     }
0042     onClicked: priceEdit.forceActiveFocus()
0043     background: null
0044 
0045     contentItem: ColumnLayout {
0046         spacing: Kirigami.Units.smallSpacing
0047         QQC2.Label {
0048             Layout.fillWidth: true
0049             elide: Text.ElideRight
0050             text: root.text
0051             Accessible.ignored: true
0052         }
0053 
0054         RowLayout {
0055             // place currency before or after the value depending on the current locale
0056             layoutDirection: Number(1).toLocaleCurrencyString(Qt.locale(), 'X').startsWith('X') ? Qt.RightToLeft : Qt.LeftToRight
0057 
0058             QQC2.TextField {
0059                 id: priceEdit
0060 
0061                 text: PriceUtil.hasPrice(root.item) ? Number(PriceUtil.price(root.item)).toLocaleString(Qt.locale(), 'f', priceValidator.decimals) : ''
0062 
0063                 inputMethodHints: Qt.ImhFormattedNumbersOnly
0064                 validator: DoubleValidator {
0065                     id: priceValidator
0066                     bottom: 0
0067                     decimals: PriceUtil.decimalCount(currencyBox.currentText)
0068                     notation: DoubleValidator.StandardNotation
0069                 }
0070             }
0071 
0072             QQC2.ComboBox {
0073                 id: currencyBox
0074                 function unique(a) { return [...new Set(a)]; }
0075                 model: unique(Country.allCountries.map(c => c.currencyCode).filter((code) => code != '').sort())
0076                 Component.onCompleted: {
0077                     currencyBox.currentIndex = Qt.binding(function() { return currencyBox.find(PriceUtil.hasPrice(root.item) ? PriceUtil.currency(root.item) : root.defaultCurrency, Qt.MatchExactly) });
0078                 }
0079             }
0080         }
0081         Kirigami.InlineMessage {
0082             id: formErrorHandler
0083             visible: formErrorHandler.text.length > 0
0084             Layout.topMargin: visible ? Kirigami.Units.smallSpacing : 0
0085             Layout.fillWidth: true
0086             type: Kirigami.MessageType.Warning
0087             text: {
0088                 try {
0089                     Number.fromLocaleString(Qt.locale(), priceEdit.text);
0090                 } catch(err) {
0091                     return i18n("Not a valid number.");
0092                 }
0093                 return "";
0094             }
0095         }
0096     }
0097 }