1 #include "wifiview.h"
2 #include <QPainter>
3 #include <QStyleOptionViewItem>
4 #include <QStyleOption>
5 #include <QRect>
6 #include <QTextOption>
7 #include <QDebug>
8 #include <QCoreApplication>
9 #include <QPalette>
10 #include <math.h>
11
WifiItemDelegate(QObject * parent)12 WifiItemDelegate::WifiItemDelegate(QObject *parent):QAbstractItemDelegate(parent)
13 {
14
15 }
16
paint(QPainter * painter,const QStyleOptionViewItem & option,const QModelIndex & index) const17 void WifiItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
18 {
19 QList<QVariant> list = index.data(Qt::DisplayRole).toList();
20 // QString bssid =list[0].toString();
21 QString wifiNmae = list[4].toString();
22 // QString flag = list[3].toString();
23 int connectState = list[5].toInt();
24 QString text = wifiNmae;
25 int sig = list[2].toInt();
26 painter->setRenderHint(QPainter::Antialiasing);
27 int width = option.rect.width();
28 int height = option.rect.height();
29 int x = option.rect.x();
30 int y= option.rect.y();
31 int nameWidth =width -40;
32
33 if(option.state &QStyle::State_Selected)
34 {
35 painter->save();
36 QPen pen(Qt::transparent);
37 painter->setPen(pen);
38 painter->setBrush(QColor(236, 238,237));
39 painter->setOpacity(0.7);
40 painter->drawRect(x, y, width, height);
41 painter->restore();
42 }
43
44 QRectF nameRect(x,y, nameWidth, height);
45 painter->save();
46 QTextOption o(Qt::AlignLeft);
47 QRectF fitNameRect =painter->boundingRect(nameRect, text, o);
48 if(connectState > 0)
49 {
50 QTextOption bottom(Qt::AlignCenter);
51 QString text ="connected";
52 painter->setPen(Qt::blue);
53 if(connectState !=2)
54 {
55 text="connect..."; painter->setPen(Qt::gray);
56 }
57
58 QRectF connectRect = painter->boundingRect(nameRect, text, bottom);
59 painter->drawText(connectRect, text);
60 }
61
62 painter->drawText(fitNameRect, text);
63 painter->setPen(QColor(215, 212,209));
64 painter->drawLine(x+5, y+height, x+width-5, y+height+1);
65 painter->restore();
66
67 QRectF sigRect(x+ nameWidth, y+ 10, height, height);
68 int startAngle = 40 * 16;
69 int spanAngle = 100 * 16;
70 QPen pen(Qt::gray);
71 pen.setWidthF(1.8);
72 int level =getLevel(sig);
73 for(int i=0; i<3; i++){
74 painter->save();
75 if(i< 4-level)
76 {
77 pen.setColor(Qt::gray);
78 }else{
79 pen.setColor(Qt::black);
80 }
81 painter->setPen(pen);
82 painter->drawArc(sigRect, startAngle, spanAngle);
83 painter->restore();
84 int adj =6;
85 sigRect.adjust(adj, adj,-adj,-adj);
86 }
87
88 QRectF centerRect(x+ nameWidth, y+ 10, height, height);
89 int r = (height/2)-2;
90 centerRect.adjust(r, r, -r, -r);
91 QColor color;
92 level ==0 ? color = Qt::gray: color = Qt::black;
93 painter->setPen(color);
94 painter->setBrush(color);
95 painter->drawEllipse(centerRect);
96 }
97
sizeHint(const QStyleOptionViewItem & option,const QModelIndex & index) const98 QSize WifiItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
99 {
100 Q_UNUSED(option)
101 Q_UNUSED(index)
102 return QSize(200, 40);
103 }
104
105
getLevel(int singalLevel) const106 int WifiItemDelegate::getLevel(int singalLevel) const
107 {
108 int ret = 1;
109 if( singalLevel >= -66){
110 ret =4;
111 }else if(singalLevel >=-77 &&singalLevel<-66){
112 ret =3;
113 }else if(singalLevel >=-88 && singalLevel<-77){
114 ret =2;
115 }else if(singalLevel >=-100 && singalLevel<-88){
116 ret =1;
117 }else{
118 ret =0;
119 }
120 return ret;
121 }
122
123
WifiModel(QObject * parent)124 WifiModel::WifiModel(QObject *parent):QAbstractListModel(parent)
125 {
126 }
127
removeRepeat(const QList<ScanInfo> & list)128 QList<ScanInfo> removeRepeat(const QList<ScanInfo> &list)
129 {
130 QList<ScanInfo> results;
131 foreach (ScanInfo info, list) {
132 if(!results.contains(info)){
133 results <<info;
134 }
135 }
136 return results;
137 }
138
setData(const QList<ScanInfo> & list)139 void WifiModel::setData(const QList<ScanInfo> &list)
140 {
141 if(list == m_list)
142 {
143 return;
144 }
145 beginResetModel();
146 m_list = list;
147 m_fixCount =0;
148 endResetModel();
149 }
150
setDataHostap(const QList<ScanInfoAp> & infos)151 void WifiModel::setDataHostap(const QList<ScanInfoAp> &infos)
152 {
153 QList<ScanInfo> list;
154 foreach (ScanInfoAp info, infos) {
155 ScanInfo l;
156 l.ssid = info.ssid;
157 l.signalLevel = info.signal;
158 list << l;
159 }
160
161 setData(list);
162 }
163
lostConnectHoseap(const QString & ssid)164 void WifiModel::lostConnectHoseap(const QString &ssid)
165 {
166 QList<ScanInfo> list = m_list;
167 ScanInfo info;
168 info.ssid = ssid;
169 list.removeOne(info);
170 setData(list);
171 }
172
rowCount(const QModelIndex & parent) const173 int WifiModel::rowCount(const QModelIndex &parent) const
174 {
175 Q_UNUSED(parent)
176 return m_list.size();
177 }
178
data(const QModelIndex & index,int role) const179 QVariant WifiModel::data(const QModelIndex &index, int role) const
180 {
181 if (!index.isValid())
182 {
183 return QVariant();
184 }
185 if (index.row() >= m_list.size() || index.row() < 0)
186 return QVariant();
187 if(role == Qt::DisplayRole)
188 {
189 ScanInfo d =m_list[index.row()];
190 QList<QVariant> list;
191 list << d.bssid
192 <<d.frequency
193 << d.signalLevel
194 <<d.flags
195 << d.ssid
196 <<d.state;
197 return QVariant(list);
198 }
199 return QVariant();
200 }
201
canFetchMore(const QModelIndex & parent) const202 bool WifiModel::canFetchMore(const QModelIndex &parent) const
203 {
204 Q_UNUSED(parent)
205 return m_fixCount< m_list.size();
206 }
207
fetchMore(const QModelIndex & parent)208 void WifiModel::fetchMore(const QModelIndex &parent)
209 {
210 Q_UNUSED(parent)
211 int remainder = m_list.size() - m_fixCount;
212 int itemsToFetch = qMin(10, remainder);
213 beginInsertRows(QModelIndex(), m_fixCount, m_fixCount+itemsToFetch-1);
214 m_fixCount += itemsToFetch;
215 endInsertRows();
216 }
217