-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathareacodelineedit.cpp
99 lines (89 loc) · 3.42 KB
/
areacodelineedit.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* Copyright (C) 2019 Tianjin KYLIN Information Technology Co., Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "areacodelineedit.h"
/* 读取国家代码JSon文件并写入ComboBox */
void AreaCodeLineEdit::InittoCountrycode() {
m_loadFile = new QFile(":/country.json",this);
if(!m_loadFile->open(QIODevice::ReadOnly)) {
qDebug() <<"Open fail!";
return ;
}
QByteArray Data = m_loadFile->readAll();
m_loadFile->close();
QJsonParseError json_error;
m_jsonFile = new QJsonDocument(QJsonDocument::fromJson(Data,&json_error));
if(json_error.error != QJsonParseError::NoError) {
qDebug() << "Json Error!";
return ;
}
m_jsonCode = m_jsonFile->array();
for(int itempos = 0;itempos < m_jsonCode.count();itempos ++) {
QJsonObject json = m_jsonCode.at(itempos).toObject();
QJsonArray jar = json.value("items").toArray();
for(int country = 0;country < jar.count();country ++) {
QString c_code = jar.at(country).toObject().value("label").toString();
QString code = jar.at(country).toObject().value("code").toString();
QString c_en = jar.at(country).toObject().value("label_en").toString();
m_countryCode.insert(c_en,QPair<QString,QString>(c_code,code));
}
}
}
AreaCodeLineEdit::AreaCodeLineEdit(QWidget *parent) : QLineEdit(parent)
{
//Allocate the memories
m_countryComboBox = new ComboBox(this);
m_verticalLine = new QFrame(this);
m_hboxLayout = new QHBoxLayout;
//Resize
resize(338,36);
setMinimumSize(338,36);
m_verticalLine->setParent(this);
setMaximumSize(338,36);
// setMaximumSize(82,36);
m_verticalLine->setMaximumSize(2,14);
m_verticalLine->setMinimumSize(2,14);
m_verticalLine->resize(2,14);
setTextMargins(98,0,0,0);
//Configuration
InittoCountrycode();
QMapIterator<QString,QPair<QString,QString>> Iter(m_countryCode);
while(Iter.hasNext())
{
Iter.next();
m_countryComboBox->addItem(Iter.value().second,Iter.value().first);
}
m_verticalLine->setFrameShape(QFrame::VLine);
m_verticalLine->setFrameShadow(QFrame::Plain);
m_verticalLine->setLineWidth(1);
m_verticalLine->setFixedHeight(14);
m_verticalLine->setStyleSheet("color:#CCCCCC");
setPlaceholderText(tr("Sign up by Phone"));
//Set Layout
m_hboxLayout->setContentsMargins(2,0,0,0);
m_hboxLayout->addWidget(m_countryComboBox);
m_hboxLayout->addSpacing(8);
m_hboxLayout->addWidget(m_verticalLine);
m_hboxLayout->setAlignment(Qt::AlignLeft);
setLayout(m_hboxLayout);
setContentsMargins(0,0,0,0);
}
AreaCodeLineEdit::~AreaCodeLineEdit() {
delete m_jsonFile;
}