phone number cbox default value
This commit is contained in:
parent
94c2617dfe
commit
7764044ca8
3 changed files with 42 additions and 2 deletions
|
|
@ -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 PhoneNumberProxy::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const {
|
||||||
bool show = (mFilterText.isEmpty() || mFilterText == "*");
|
bool show = (mFilterText.isEmpty() || mFilterText == "*");
|
||||||
if (!show) {
|
if (!show) {
|
||||||
|
|
|
||||||
|
|
@ -29,21 +29,28 @@ class PhoneNumberProxy : public SortFilterProxy {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
Q_PROPERTY(QString filterText READ getFilterText WRITE setFilterText NOTIFY filterTextChanged)
|
Q_PROPERTY(QString filterText READ getFilterText WRITE setFilterText NOTIFY filterTextChanged)
|
||||||
|
Q_PROPERTY(QString defaultCountryCallingCode READ getDefaultCountryCallingCode NOTIFY defaultCountryCallingCodeChanged)
|
||||||
|
|
||||||
public:
|
public:
|
||||||
PhoneNumberProxy(QObject *parent = Q_NULLPTR);
|
PhoneNumberProxy(QObject *parent = Q_NULLPTR);
|
||||||
|
|
||||||
QString getFilterText() const;
|
QString getFilterText() const;
|
||||||
void setFilterText(const QString &filter);
|
void setFilterText(const QString &filter);
|
||||||
|
QString getDefaultCountryCallingCode() const;
|
||||||
|
void setDefaultCountryCallingCode(const QString &filter);
|
||||||
|
|
||||||
|
Q_INVOKABLE int findIndexByCountryCallingCode(const QString& countryCallingCode);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void filterTextChanged();
|
void filterTextChanged();
|
||||||
|
void defaultCountryCallingCodeChanged();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
|
virtual bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
|
||||||
virtual bool lessThan(const QModelIndex &left, const QModelIndex &right) const override;
|
virtual bool lessThan(const QModelIndex &left, const QModelIndex &right) const override;
|
||||||
|
|
||||||
QString mFilterText;
|
QString mFilterText;
|
||||||
|
QString mDefaultCountryCallingCode;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ ColumnLayout {
|
||||||
property string label: ""
|
property string label: ""
|
||||||
property int backgroundWidth: 100
|
property int backgroundWidth: 100
|
||||||
readonly property string currentText: combobox.model.getAt(combobox.currentIndex) ? combobox.model.getAt(combobox.currentIndex).countryCallingCode : ""
|
readonly property string currentText: combobox.model.getAt(combobox.currentIndex) ? combobox.model.getAt(combobox.currentIndex).countryCallingCode : ""
|
||||||
|
property alias defaultCallingCode: phoneNumberModel.defaultCountryCallingCode
|
||||||
|
|
||||||
Text {
|
Text {
|
||||||
visible: label.length > 0
|
visible: label.length > 0
|
||||||
|
|
@ -22,7 +23,13 @@ ColumnLayout {
|
||||||
|
|
||||||
Control.ComboBox {
|
Control.ComboBox {
|
||||||
id: combobox
|
id: combobox
|
||||||
model: PhoneNumberProxy{}
|
model: PhoneNumberProxy {
|
||||||
|
id: phoneNumberModel
|
||||||
|
onCountChanged: {
|
||||||
|
var defaultIndex = findIndexByCountryCallingCode(defaultCallingCode)
|
||||||
|
combobox.currentIndex = defaultIndex < 0 ? 0 : defaultIndex
|
||||||
|
}
|
||||||
|
}
|
||||||
background: Loader {
|
background: Loader {
|
||||||
sourceComponent: backgroundRectangle
|
sourceComponent: backgroundRectangle
|
||||||
}
|
}
|
||||||
|
|
@ -108,7 +115,6 @@ ColumnLayout {
|
||||||
visible: parent.containsMouse
|
visible: parent.containsMouse
|
||||||
}
|
}
|
||||||
onPressed: {
|
onPressed: {
|
||||||
listView.currentIndex = index
|
|
||||||
combobox.currentIndex = index
|
combobox.currentIndex = index
|
||||||
listPopup.close()
|
listPopup.close()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue