show ap list

Change-Id: I80ecff8f8c6254f08542fb9b33a564f5c510b873
This commit is contained in:
石博文 2016-07-26 16:59:44 +08:00 committed by Hualet Wang
parent 2a5c0c872d
commit bd38772b43
7 changed files with 192 additions and 12 deletions

View File

@ -1,7 +1,50 @@
#include "accesspoint.h"
AccessPoint::AccessPoint(QObject *parent)
: QObject(parent)
{
#include <QDebug>
AccessPoint::AccessPoint(const QJsonObject &apInfo)
: QObject(nullptr)
{
m_strength = apInfo.value("Strength").toInt();
m_secured = apInfo.value("Secured").toBool();
m_securedInEap = apInfo.value("SecuredInEap").toBool();
m_path = apInfo.value("Path").toString();
m_ssid = apInfo.value("Ssid").toString();
}
AccessPoint::AccessPoint(const AccessPoint &ap)
: QObject(nullptr)
{
*this = ap;
}
bool AccessPoint::operator==(const AccessPoint &ap) const
{
return m_ssid == ap.m_ssid;
}
bool AccessPoint::operator<(const AccessPoint &ap) const
{
return m_strength < ap.m_strength;
}
AccessPoint &AccessPoint::operator=(const AccessPoint &ap)
{
m_strength = ap.m_strength;
m_secured = ap.m_secured;
m_securedInEap = ap.m_securedInEap;
m_path = ap.m_path;
m_ssid = ap.m_ssid;
return *this;
}
const QString AccessPoint::ssid() const
{
return m_ssid;
}
int AccessPoint::strength() const
{
return m_strength;
}

View File

@ -2,13 +2,28 @@
#define ACCESSPOINT_H
#include <QObject>
#include <QJsonObject>
class AccessPoint : public QObject
{
Q_OBJECT
public:
explicit AccessPoint(QObject *parent = 0);
explicit AccessPoint(const QJsonObject &apInfo);
AccessPoint(const AccessPoint &ap);
bool operator==(const AccessPoint &ap) const;
bool operator<(const AccessPoint &ap) const;
AccessPoint &operator=(const AccessPoint &ap);
const QString ssid() const;
int strength() const;
private:
int m_strength;
bool m_secured;
bool m_securedInEap;
QString m_path;
QString m_ssid;
};
#endif // ACCESSPOINT_H

View File

@ -0,0 +1,21 @@
#include "accesspointwidget.h"
#include <QHBoxLayout>
#include <QDebug>
AccessPointWidget::AccessPointWidget(const AccessPoint &ap)
: QWidget(nullptr),
m_ssid(new QLabel)
{
m_ssid->setText(ap.ssid());
m_ssid->setStyleSheet("color:white;");
QHBoxLayout *centeralLayout = new QHBoxLayout;
centeralLayout->addWidget(m_ssid);
centeralLayout->setSpacing(0);
centeralLayout->setMargin(0);
setLayout(centeralLayout);
}

View File

@ -0,0 +1,20 @@
#ifndef ACCESSPOINTWIDGET_H
#define ACCESSPOINTWIDGET_H
#include "accesspoint.h"
#include <QWidget>
#include <QLabel>
class AccessPointWidget : public QWidget
{
Q_OBJECT
public:
explicit AccessPointWidget(const AccessPoint &ap);
private:
QLabel *m_ssid;
};
#endif // ACCESSPOINTWIDGET_H

View File

@ -1,13 +1,18 @@
#include "wirelessapplet.h"
#include "accesspointwidget.h"
#include <QJsonDocument>
#define WIDTH 300
#define WIDTH 300
#define MAX_HEIGHT 200
#define ITEM_HEIGHT 30
WirelessApplet::WirelessApplet(const QString &devicePath, QWidget *parent)
: QScrollArea(parent),
m_devicePath(devicePath),
m_updateAPTimer(new QTimer(this)),
m_centeralLayout(new QVBoxLayout),
m_centeralWidget(new QWidget),
m_controlPanel(new DeviceControlWidget(this)),
@ -15,6 +20,9 @@ WirelessApplet::WirelessApplet(const QString &devicePath, QWidget *parent)
{
setFixedHeight(WIDTH);
m_updateAPTimer->setSingleShot(true);
m_updateAPTimer->setInterval(100);
m_centeralWidget->setFixedWidth(WIDTH);
m_centeralWidget->setLayout(m_centeralLayout);
@ -27,9 +35,17 @@ WirelessApplet::WirelessApplet(const QString &devicePath, QWidget *parent)
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setStyleSheet("background-color:transparent;");
setDeviceInfo();
QMetaObject::invokeMethod(this, "init", Qt::QueuedConnection);
connect(m_networkInter, &DBusNetwork::AccessPointPropertiesChanged, this, &WirelessApplet::APChanged);
connect(m_networkInter, &DBusNetwork::AccessPointPropertiesChanged, this, &WirelessApplet::APPropertiesChanged);
connect(m_updateAPTimer, &QTimer::timeout, this, &WirelessApplet::updateAPList);
}
void WirelessApplet::init()
{
setDeviceInfo();
loadAPList();
}
void WirelessApplet::setDeviceInfo()
@ -61,10 +77,63 @@ void WirelessApplet::setDeviceInfo()
}
}
void WirelessApplet::APChanged(const QString &devPath, const QString &info)
void WirelessApplet::loadAPList()
{
const QString data = m_networkInter->GetAccessPoints(QDBusObjectPath(m_devicePath));
const QJsonDocument doc = QJsonDocument::fromJson(data.toUtf8());
Q_ASSERT(doc.isArray());
for (auto item : doc.array())
{
Q_ASSERT(item.isObject());
AccessPoint ap(item.toObject());
if (!m_apList.contains(ap))
m_apList.append(ap);
}
m_updateAPTimer->start();
}
void WirelessApplet::APPropertiesChanged(const QString &devPath, const QString &info)
{
if (devPath != m_devicePath)
return;
qDebug() << info;
QJsonDocument doc = QJsonDocument::fromJson(info.toUtf8());
Q_ASSERT(doc.isObject());
const AccessPoint ap(doc.object());
auto it = std::find_if(m_apList.begin(), m_apList.end(),
[&] (const AccessPoint &a) {return a == ap;});
if (it == m_apList.end())
return;
if (*it < ap)
return;
*it = ap;
m_updateAPTimer->start();
}
void WirelessApplet::updateAPList()
{
Q_ASSERT(sender() == m_updateAPTimer);
// remove old items
while (QLayoutItem *item = m_centeralLayout->takeAt(1))
{
delete item->widget();
delete item;
}
for (auto ap : m_apList)
{
AccessPointWidget *apw = new AccessPointWidget(ap);
m_centeralLayout->addWidget(apw);
}
const int contentHeight = m_apList.count() * ITEM_HEIGHT + m_controlPanel->height();
m_centeralWidget->setFixedHeight(contentHeight);
setFixedHeight(std::min(contentHeight, MAX_HEIGHT));
}

View File

@ -2,10 +2,13 @@
#define WIRELESSAPPLET_H
#include "devicecontrolwidget.h"
#include "accesspoint.h"
#include "../../dbus/dbusnetwork.h"
#include <QScrollArea>
#include <QVBoxLayout>
#include <QList>
#include <QTimer>
class WirelessApplet : public QScrollArea
{
@ -16,13 +19,20 @@ public:
private:
void setDeviceInfo();
void loadAPList();
private slots:
void APChanged(const QString &devPath, const QString &info);
void init();
void APPropertiesChanged(const QString &devPath, const QString &info);
void updateAPList();
private:
const QString m_devicePath;
QList<AccessPoint> m_apList;
QTimer *m_updateAPTimer;
QVBoxLayout *m_centeralLayout;
QWidget *m_centeralWidget;
DeviceControlWidget *m_controlPanel;

View File

@ -21,7 +21,8 @@ HEADERS += \
item/wirelessitem.h \
item/applet/wirelessapplet.h \
item/applet/devicecontrolwidget.h \
item/applet/accesspoint.h
item/applet/accesspoint.h \
item/applet/accesspointwidget.h
SOURCES += \
networkplugin.cpp \
@ -34,7 +35,8 @@ SOURCES += \
item/wirelessitem.cpp \
item/applet/wirelessapplet.cpp \
item/applet/devicecontrolwidget.cpp \
item/applet/accesspoint.cpp
item/applet/accesspoint.cpp \
item/applet/accesspointwidget.cpp
target.path = $${PREFIX}/lib/dde-dock/plugins/
INSTALLS += target