phone number cbox default value

This commit is contained in:
Gaelle Braud 2023-10-27 16:11:09 +02:00
parent 94c2617dfe
commit 7764044ca8
3 changed files with 42 additions and 2 deletions

View file

@ -39,6 +39,33 @@ void PhoneNumberProxy::setFilterText(const QString &filter) {
}
}
QString PhoneNumberProxy::getDefaultCountryCallingCode() const {
return mDefaultCountryCallingCode;
}
void PhoneNumberProxy::setDefaultCountryCallingCode(const QString &code) {
if (mDefaultCountryCallingCode != code) {
mDefaultCountryCallingCode = code;
invalidate();
emit defaultCountryCallingCodeChanged();
}
}
int PhoneNumberProxy::findIndexByCountryCallingCode(const QString &countryCallingCode) {
auto model = qobject_cast<PhoneNumberList *>(sourceModel());
if (!model) return -1;
if (countryCallingCode.isEmpty()) return -1;
auto list = model->getSharedList<PhoneNumber>();
auto it = std::find_if(list.begin(), list.end(), [countryCallingCode](const QSharedPointer<QObject> &a) {
return a.objectCast<PhoneNumber>()->mCountryCallingCode == countryCallingCode;
});
auto proxyModelIndex = mapFromSource(model->index(it - list.begin()));
return proxyModelIndex.row();
return -1;
}
bool PhoneNumberProxy::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const {
bool show = (mFilterText.isEmpty() || mFilterText == "*");
if (!show) {

View file

@ -29,21 +29,28 @@ class PhoneNumberProxy : public SortFilterProxy {
Q_OBJECT
Q_PROPERTY(QString filterText READ getFilterText WRITE setFilterText NOTIFY filterTextChanged)
Q_PROPERTY(QString defaultCountryCallingCode READ getDefaultCountryCallingCode NOTIFY defaultCountryCallingCodeChanged)
public:
PhoneNumberProxy(QObject *parent = Q_NULLPTR);
QString getFilterText() const;
void setFilterText(const QString &filter);
QString getDefaultCountryCallingCode() const;
void setDefaultCountryCallingCode(const QString &filter);
Q_INVOKABLE int findIndexByCountryCallingCode(const QString& countryCallingCode);
signals:
void filterTextChanged();
void defaultCountryCallingCodeChanged();
protected:
virtual bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
virtual bool lessThan(const QModelIndex &left, const QModelIndex &right) const override;
QString mFilterText;
QString mDefaultCountryCallingCode;
};
#endif

View file

@ -8,6 +8,7 @@ ColumnLayout {
property string label: ""
property int backgroundWidth: 100
readonly property string currentText: combobox.model.getAt(combobox.currentIndex) ? combobox.model.getAt(combobox.currentIndex).countryCallingCode : ""
property alias defaultCallingCode: phoneNumberModel.defaultCountryCallingCode
Text {
visible: label.length > 0
@ -22,7 +23,13 @@ ColumnLayout {
Control.ComboBox {
id: combobox
model: PhoneNumberProxy{}
model: PhoneNumberProxy {
id: phoneNumberModel
onCountChanged: {
var defaultIndex = findIndexByCountryCallingCode(defaultCallingCode)
combobox.currentIndex = defaultIndex < 0 ? 0 : defaultIndex
}
}
background: Loader {
sourceComponent: backgroundRectangle
}
@ -108,7 +115,6 @@ ColumnLayout {
visible: parent.containsMouse
}
onPressed: {
listView.currentIndex = index
combobox.currentIndex = index
listPopup.close()
}