diff --git a/Linphone/core/phone-number/PhoneNumberProxy.cpp b/Linphone/core/phone-number/PhoneNumberProxy.cpp index 2fcfa1217..976fa86dd 100644 --- a/Linphone/core/phone-number/PhoneNumberProxy.cpp +++ b/Linphone/core/phone-number/PhoneNumberProxy.cpp @@ -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(sourceModel()); + if (!model) return -1; + if (countryCallingCode.isEmpty()) return -1; + + auto list = model->getSharedList(); + auto it = std::find_if(list.begin(), list.end(), [countryCallingCode](const QSharedPointer &a) { + return a.objectCast()->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) { diff --git a/Linphone/core/phone-number/PhoneNumberProxy.hpp b/Linphone/core/phone-number/PhoneNumberProxy.hpp index 291add975..39931a5b0 100644 --- a/Linphone/core/phone-number/PhoneNumberProxy.hpp +++ b/Linphone/core/phone-number/PhoneNumberProxy.hpp @@ -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 diff --git a/Linphone/view/Item/PhoneNumberComboBox.qml b/Linphone/view/Item/PhoneNumberComboBox.qml index a665d5f94..ffb53b653 100644 --- a/Linphone/view/Item/PhoneNumberComboBox.qml +++ b/Linphone/view/Item/PhoneNumberComboBox.qml @@ -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() }