bearercloud.cpp Example File
bearercloud/bearercloud.cpp
#include "bearercloud.h"
#include "cloud.h"
#include <QGraphicsTextItem>
#include <QTimer>
#include <QDateTime>
#include <QHostInfo>
#include <QDebug>
#include <math.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
BearerCloud::BearerCloud(QObject *parent)
: QGraphicsScene(parent), timerId(0)
{
setSceneRect(-300, -300, 600, 600);
qsrand(QDateTime::currentDateTime().toTime_t());
offset[QNetworkConfiguration::Active] = 2 * M_PI * qrand() / RAND_MAX;
offset[QNetworkConfiguration::Discovered] = offset[QNetworkConfiguration::Active] + M_PI / 6;
offset[QNetworkConfiguration::Defined] = offset[QNetworkConfiguration::Discovered] - M_PI / 6;
offset[QNetworkConfiguration::Undefined] = offset[QNetworkConfiguration::Undefined] + M_PI / 6;
thisDevice = new QGraphicsTextItem(QHostInfo::localHostName());
thisDevice->setData(0, QLatin1String("This Device"));
thisDevice->setPos(thisDevice->boundingRect().width() / -2,
thisDevice->boundingRect().height() / -2);
addItem(thisDevice);
qreal radius = Cloud::getRadiusForState(QNetworkConfiguration::Active);
QGraphicsEllipseItem *orbit = new QGraphicsEllipseItem(-radius, -radius, 2*radius, 2*radius);
orbit->setPen(QColor(Qt::green));
addItem(orbit);
radius = Cloud::getRadiusForState(QNetworkConfiguration::Discovered);
orbit = new QGraphicsEllipseItem(-radius, -radius, 2*radius, 2*radius);
orbit->setPen(QColor(Qt::blue));
addItem(orbit);
radius = Cloud::getRadiusForState(QNetworkConfiguration::Defined);
orbit = new QGraphicsEllipseItem(-radius, -radius, 2*radius, 2*radius);
orbit->setPen(QColor(Qt::darkGray));
addItem(orbit);
radius = Cloud::getRadiusForState(QNetworkConfiguration::Undefined);
orbit = new QGraphicsEllipseItem(-radius, -radius, 2*radius, 2*radius);
orbit->setPen(QColor(Qt::lightGray));
addItem(orbit);
connect(&manager, SIGNAL(configurationAdded(QNetworkConfiguration)),
this, SLOT(configurationAdded(QNetworkConfiguration)));
connect(&manager, SIGNAL(configurationRemoved(QNetworkConfiguration)),
this, SLOT(configurationRemoved(QNetworkConfiguration)));
connect(&manager, SIGNAL(configurationChanged(QNetworkConfiguration)),
this, SLOT(configurationChanged(QNetworkConfiguration)));
QTimer::singleShot(0, this, SLOT(updateConfigurations()));
}
BearerCloud::~BearerCloud()
{
}
void BearerCloud::cloudMoved()
{
if (!timerId)
timerId = startTimer(1000 / 25);
}
void BearerCloud::timerEvent(QTimerEvent *)
{
QList<Cloud *> clouds;
foreach (QGraphicsItem *item, items()) {
if (Cloud *cloud = qgraphicsitem_cast<Cloud *>(item))
clouds << cloud;
}
foreach (Cloud *cloud, clouds)
cloud->calculateForces();
bool cloudsMoved = false;
foreach (Cloud *cloud, clouds)
cloudsMoved |= cloud->advance();
if (!cloudsMoved) {
killTimer(timerId);
timerId = 0;
}
}
void BearerCloud::configurationAdded(const QNetworkConfiguration &config)
{
const QNetworkConfiguration::StateFlags state = config.state();
configStates.insert(state, config.identifier());
const qreal radius = Cloud::getRadiusForState(state);
const int count = configStates.count(state);
const qreal angle = 2 * M_PI / count;
Cloud *item = new Cloud(config);
configurations.insert(config.identifier(), item);
item->setPos(radius * cos((count-1) * angle + offset[state]),
radius * sin((count-1) * angle + offset[state]));
addItem(item);
cloudMoved();
}
void BearerCloud::configurationRemoved(const QNetworkConfiguration &config)
{
foreach (const QNetworkConfiguration::StateFlags &state, configStates.uniqueKeys())
configStates.remove(state, config.identifier());
Cloud *item = configurations.take(config.identifier());
item->setFinalScale(0.0);
item->setDeleteAfterAnimation(true);
cloudMoved();
}
void BearerCloud::configurationChanged(const QNetworkConfiguration &config)
{
foreach (const QNetworkConfiguration::StateFlags &state, configStates.uniqueKeys())
configStates.remove(state, config.identifier());
configStates.insert(config.state(), config.identifier());
cloudMoved();
}
void BearerCloud::updateConfigurations()
{
QList<QNetworkConfiguration> allConfigurations = manager.allConfigurations();
while (!allConfigurations.isEmpty())
configurationAdded(allConfigurations.takeFirst());
cloudMoved();
}