kde-playground/plasma/applets/network/network.cpp
Ivailo Monev 0c02c88b22 plasma: new network applet
Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
2024-06-04 02:49:20 +03:00

155 lines
4.9 KiB
C++

/* This file is part of the KDE project
Copyright (C) 2024 Ivailo Monev <xakepa10@gmail.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2, as published by the Free Software Foundation.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "network.h"
#include <KIcon>
#include <KNetworkManager>
#include <Plasma/CheckBox>
#include <KDebug>
// TODO: KNetworkManager::isEnabled() for initial status
// TODO: KNetworkManager::connections() for connections status (which is the one being used)
static const int s_svgiconsize = 256;
static const QString s_defaultpopupicon = QString::fromLatin1("network-workgroup");
static const QSizeF s_minimumsize = QSizeF(150, 50);
static QIcon kNetworkIcon(QObject *parent, const bool active, bool wireless, const int strength)
{
QIcon result;
Plasma::Svg plasmasvg(parent);
plasmasvg.setImagePath("icons/network");
plasmasvg.setContainsMultipleImages(true);
if (plasmasvg.isValid()) {
QPixmap iconpixmap(s_svgiconsize, s_svgiconsize);
iconpixmap.fill(Qt::transparent);
QPainter iconpainter(&iconpixmap);
if (wireless) {
if (strength > 75) {
plasmasvg.paint(&iconpainter, iconpixmap.rect(), "network-wireless-100");
} else if (strength > 50) {
plasmasvg.paint(&iconpainter, iconpixmap.rect(), "network-wireless-75");
} else if (strength > 25) {
plasmasvg.paint(&iconpainter, iconpixmap.rect(), "network-wireless-50");
} else if (strength > 0) {
plasmasvg.paint(&iconpainter, iconpixmap.rect(), "network-wireless-25");
} else {
plasmasvg.paint(&iconpainter, iconpixmap.rect(), "network-wireless-0");
}
} else {
plasmasvg.paint(&iconpainter, iconpixmap.rect(), active ? "network-wired-activated" : "network-wired");
}
result = QIcon(iconpixmap);
} else {
result = KIcon(s_defaultpopupicon);
}
return result;
}
class NetworkAppletWidget : public QGraphicsWidget
{
Q_OBJECT
public:
NetworkAppletWidget(NetworkApplet* network);
~NetworkAppletWidget();
private Q_SLOTS:
void slotDisableNetwork(const bool disable);
void slotStatusChanged(const KNetworkManager::KNetworkStatus status);
private:
NetworkApplet* m_network;
QGraphicsLinearLayout* m_layout;
Plasma::CheckBox* m_disablenetworkbox;
KNetworkManager* m_networkmanager;
};
NetworkAppletWidget::NetworkAppletWidget(NetworkApplet* network)
: QGraphicsWidget(network),
m_network(network),
m_layout(nullptr),
m_disablenetworkbox(nullptr)
{
setMinimumSize(s_minimumsize);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
m_layout = new QGraphicsLinearLayout(Qt::Vertical, this);
m_disablenetworkbox = new Plasma::CheckBox(this);
m_disablenetworkbox->setText(i18n("Disable network"));
m_disablenetworkbox->setEnabled(KNetworkManager::isSupported());
connect(m_disablenetworkbox, SIGNAL(toggled(bool)), this, SLOT(slotDisableNetwork(bool)));
m_layout->addItem(m_disablenetworkbox);
setLayout(m_layout);
m_networkmanager = new KNetworkManager(this);
slotStatusChanged(m_networkmanager->status());
connect(
m_networkmanager, SIGNAL(statusChanged(KNetworkManager::KNetworkStatus)),
this, SLOT(slotStatusChanged(KNetworkManager::KNetworkStatus))
);
}
NetworkAppletWidget::~NetworkAppletWidget()
{
}
void NetworkAppletWidget::slotDisableNetwork(const bool disable)
{
if (disable) {
m_networkmanager->enable(false);
} else {
m_networkmanager->enable(true);
}
}
void NetworkAppletWidget::slotStatusChanged(const KNetworkManager::KNetworkStatus status)
{
m_network->setPopupIcon(
kNetworkIcon(this, status == KNetworkManager::ConnectedStatus, false, 0)
);
}
NetworkApplet::NetworkApplet(QObject *parent, const QVariantList &args)
: Plasma::PopupApplet(parent, args),
m_networkwidget(nullptr)
{
setAspectRatioMode(Plasma::AspectRatioMode::IgnoreAspectRatio);
setPopupIcon(s_defaultpopupicon);
}
NetworkApplet::~NetworkApplet()
{
delete m_networkwidget;
}
void NetworkApplet::init()
{
Plasma::PopupApplet::init();
m_networkwidget = new NetworkAppletWidget(this);
}
QGraphicsWidget* NetworkApplet::graphicsWidget()
{
return m_networkwidget;
}
#include "moc_network.cpp"
#include "network.moc"