// Qt // Project #include "VNetworkModel.h" #include "WifiInterface.h" #include "Logger.h" using namespace View; /*! * \brief VNetworkModel::initConnections * Makes the necessary connections. Called inside VIEW_DEF_CLASS */ void VNetworkModel::initConnections() { connect(this, SIGNAL(didScan()), &_WifiInterface, SLOT(doScan())); connect(&_WifiInterface, SIGNAL(didAddNetwork(const Network)), this, SLOT(doAddNetwork(const Network))); connect(&_WifiInterface, SIGNAL(didScanStatusChanged(const bool)), this, SLOT(onScanStatusChanged(const bool))); } /*! * \brief VNetworkModel::addNetwork * Adds a network to the network model * \param network (Network) - the network to add to the model */ void VNetworkModel::addNetwork(const Network &vNetwork) { LOG_DEBUG(QString("Adding network with SSID: %1").arg(vNetwork.ssid())); beginInsertRows(QModelIndex(), rowCount(), rowCount()); _networks << vNetwork; endInsertRows(); } /*! * \brief VNetworkModel::rowCount * Gets the number of networks * \param parent - (QModelIndex) the parent QModelIndex * \return (int) - the number of networks */ int VNetworkModel::rowCount(const QModelIndex & parent) const { Q_UNUSED(parent); return _networks.count(); } /*! * \brief VNetworkModel::data * Returns the network properties at the specified index * \param index (QModelIndex) contains the row of data to lookup * \param role - (int) the property index to return. See NetworkDataRole * \return (QVariant) - the value for the specified network property */ QVariant VNetworkModel::data(const QModelIndex & index, int role) const { if (index.row() < 0 || index.row() >= _networks.count()) return QVariant(); const Network &network = _networks[index.row()]; if (role == SSIDRole) return network.ssid(); else if (role == SecurityLevelRole) return network.security(); else if (role == StatusRole) return network.status(); return QVariant(); } /*! * \brief VNetworkModel::roleNames * Translates how to access specific properties of the data for QML from the NetworkDataRole enum * \return (QHash) - maps enums to property names */ QHash VNetworkModel::roleNames() const { QHash roles; roles[SSIDRole] = "ssid"; roles[SecurityLevelRole] = "securityLevel"; roles[StatusRole] = "status"; return roles; } /*! * \brief VNetworkModel::doScan * Handles when a user clicks the Scan button */ void VNetworkModel::doScan() { emit didScan(); } /*! * \brief VNetworkModel::onAddNetwork * Slot that receives a request to add a new network * \param vNetwork - (Network) the new network to add */ void VNetworkModel::doAddNetwork(const Network &vNetwork) { if (!_networks.contains(vNetwork)) { qDebug() << QString("Adding network with SSID: %1.").arg(vNetwork.ssid()); LOG_DEBUG(QString("Adding network with SSID: %1.").arg(vNetwork.ssid())); addNetwork(vNetwork); } else { LOG_DEBUG(QString("Skipping adding network with SSID: %1. It already exists").arg(vNetwork.ssid())); } } /*! * \brief VNetworkModel::onScanStatusChanged * Called when the scan status changes. Updates QML with the current state * \param vScanning - (bool) true if scanning, false otherwise */ void VNetworkModel::onScanStatusChanged(const bool &vScanning) { scanInProgress(vScanning); }