dde-dock/plugins/new-network/item/wirelessitem.cpp
listenerri 20951861e2 refactor: network plugin
Change-Id: I7606a159304398d4471c21f677084a965f0a0a42
2018-06-21 21:17:05 +08:00

226 lines
6.6 KiB
C++

/*
* Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd.
*
* Author: sbw <sbw@sbw.so>
*
* Maintainer: sbw <sbw@sbw.so>
*
* 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 3 of the License, or
* 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, see <http://www.gnu.org/licenses/>.
*/
#include "wirelessitem.h"
#include "util/imageutil.h"
#include <QPainter>
#include <QMouseEvent>
#include <QApplication>
#include <QIcon>
using namespace dde::network;
WirelessItem::WirelessItem(WirelessDevice *device)
: DeviceItem(device),
m_refershTimer(new QTimer(this)),
m_wirelessApplet(new QWidget),
m_wirelessPopup(new QLabel)
//m_APList(nullptr)
{
m_refershTimer->setSingleShot(false);
m_refershTimer->setInterval(100);
m_wirelessApplet->setVisible(false);
m_wirelessPopup->setObjectName("wireless-" + m_device->path());
m_wirelessPopup->setVisible(false);
m_wirelessPopup->setStyleSheet("color:white;"
"padding: 0px 3px;");
connect(m_device, static_cast<void (NetworkDevice::*) (const QString &statStr) const>(&NetworkDevice::statusChanged), m_refershTimer, static_cast<void (QTimer::*) ()>(&QTimer::start));
connect(m_refershTimer, &QTimer::timeout, this, static_cast<void (WirelessItem::*)()>(&WirelessItem::update));
QMetaObject::invokeMethod(this, "init", Qt::QueuedConnection);
}
WirelessItem::~WirelessItem()
{
//m_APList->deleteLater();
//m_APList->controlPanel()->deleteLater();
}
QWidget *WirelessItem::itemApplet()
{
return m_wirelessApplet;
}
QWidget *WirelessItem::itemTips()
{
const NetworkDevice::DeviceStatus stat = m_device->status();
m_wirelessPopup->setText(tr("No Network"));
if (stat == NetworkDevice::Activated)
{
const QJsonObject obj = static_cast<WirelessDevice *>(m_device)->activeApInfo();
if (obj.contains("Ip4"))
{
const QJsonObject ip4 = obj["Ip4"].toObject();
if (ip4.contains("Address"))
{
m_wirelessPopup->setText(tr("Wireless Connection: %1").arg(ip4["Address"].toString()));
}
}
}
return m_wirelessPopup;
}
bool WirelessItem::eventFilter(QObject *o, QEvent *e)
{
/* TODO: refactor */
//if (o == m_APList && e->type() == QEvent::Resize)
//QMetaObject::invokeMethod(this, "adjustHeight", Qt::QueuedConnection);
return false;
}
void WirelessItem::paintEvent(QPaintEvent *e)
{
DeviceItem::paintEvent(e);
const Dock::DisplayMode displayMode = qApp->property(PROP_DISPLAY_MODE).value<Dock::DisplayMode>();
const auto ratio = qApp->devicePixelRatio();
const int iconSize = displayMode == Dock::Fashion ? std::min(width(), height()) * 0.8 : 16;
QPixmap pixmap = iconPix(displayMode, iconSize * ratio);
pixmap.setDevicePixelRatio(ratio);
QPainter painter(this);
if (displayMode == Dock::Fashion)
{
QPixmap pixmap = backgroundPix(iconSize * ratio);
pixmap.setDevicePixelRatio(ratio);
painter.drawPixmap(rect().center() - pixmap.rect().center() / ratio, pixmap);
}
painter.drawPixmap(rect().center() - pixmap.rect().center() / ratio, pixmap);
}
void WirelessItem::resizeEvent(QResizeEvent *e)
{
DeviceItem::resizeEvent(e);
m_icons.clear();
}
void WirelessItem::mousePressEvent(QMouseEvent *e)
{
if (e->button() != Qt::RightButton)
return e->ignore();
const QPoint p(e->pos() - rect().center());
if (p.manhattanLength() < std::min(width(), height()) * 0.8 * 0.5)
{
emit requestContextMenu();
return;
}
return QWidget::mousePressEvent(e);
}
const QPixmap WirelessItem::iconPix(const Dock::DisplayMode displayMode, const int size)
{
QString type;
/* TODO: refactor */
//const auto state = m_APList->wirelessState();
const auto state = m_device->status();
if (state <= NetworkDevice::Disconnected)
{
type = "disconnect";
m_refershTimer->stop();
}
else
{
int strength = 0;
if (state == NetworkDevice::Activated)
{
strength = static_cast<WirelessDevice *>(m_device)->activeApStrgength();
m_refershTimer->stop();
}
else
{
strength = QTime::currentTime().msec() / 10 % 100;
if (!m_refershTimer->isActive())
m_refershTimer->start();
}
if (strength == 100)
type = "80";
else if (strength < 20)
type = "0";
else
type = QString::number(strength / 10 & ~0x1) + "0";
}
const QString key = QString("wireless-%1%2")
.arg(type)
.arg(displayMode == Dock::Fashion ? "" : "-symbolic");
return cachedPix(key, size);
}
const QPixmap WirelessItem::backgroundPix(const int size)
{
return cachedPix("wireless-background", size);
}
const QPixmap WirelessItem::cachedPix(const QString &key, const int size)
{
if (!m_icons.contains(key))
m_icons.insert(key, QIcon::fromTheme(key,
QIcon(":/wireless/resources/wireless/" + key + ".svg")).pixmap(size));
return m_icons.value(key);
}
void WirelessItem::init()
{
/* TODO: refactor */
//const auto devInfo = m_networkManager->device(m_devicePath);
//m_APList = new WirelessList(devInfo);
//m_APList->installEventFilter(this);
//m_APList->setObjectName("wireless-" + m_devicePath);
//QVBoxLayout *vLayout = new QVBoxLayout;
//vLayout->addWidget(m_APList->controlPanel());
//vLayout->addWidget(m_APList);
//vLayout->setMargin(0);
//vLayout->setSpacing(0);
//m_wirelessApplet->setLayout(vLayout);
//connect(m_APList, &WirelessList::activeAPChanged, this, static_cast<void (WirelessItem::*)()>(&WirelessItem::update));
//connect(m_APList, &WirelessList::wirelessStateChanged, this, static_cast<void (WirelessItem::*)()>(&WirelessItem::update));
}
void WirelessItem::adjustHeight()
{
/* TODO: refactor */
//m_wirelessApplet->setFixedHeight(m_APList->height() + m_APList->controlPanel()->height());
}
void WirelessItem::refreshIcon()
{
update();
}