#include #include #include #include #include #include #include #include #include #include #include #include #include "mainwindow.h" #include "ui_mainwindow.h" #include #include "settingsdialog.h" /** * Constructor of the MainWindow */ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); m_scene = new QGraphicsScene(this); comm = CommModel::init(); sleep(2); widget = new GraphWidget(this, comm); widget->setSelectedIP(0); widget->setFrameStyle(-1); widget->setParent (ui->graphicsView); widget->setMainWindow(this); doPhysics = true; widget->setDoPhysics(true); //ocu = new node(); //ocu->ip_addr = 1; selectedNode = NULL; //ocu->mac_addr = 1; ui->graphicsView->setScene(m_scene); refreshRate = 100; inactiveThreshold = 3000; doPhysics = false; showThroughput = true; showLatency = true; showSignalStrength = true; showInactivityTime = true; isPlaying = false; firstTime = true; timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(updateList())); this->on_playButton_released(); } /** * Destructor of the MainWindow. */ MainWindow::~MainWindow() { delete this->comm; delete ui; } /** * It toggles the play/pause button. It also changes the icon of play/pause */ void MainWindow::togglePlaying() { if(isPlaying) { perror("Changing to paused!\n"); isPlaying = false; ui->playButton->setIcon(QIcon(":/new/prefix1/images/Play.png")); } else { perror("Changing to playing!\n"); isPlaying = true; ui->playButton->setIcon(QIcon(":/new/prefix1/images/Pause.png")); } } /** * @param row The row in the list to place the info * @param myNode The node to be placed * @param model model that display the textual representation of the mesh. * It sets the node in all model. If selected node is same as parameter node, then * it sets the information in the model. */ void MainWindow::setNodeInAllModel(int row, node *myNode, QStandardItemModel* model) { QStandardItem *item; //IP ADDRESS STUFF struct in_addr theAddr; theAddr.s_addr = ntohl(myNode->ip_addr); char *stringrepofIpAddress = inet_ntoa(theAddr); item = new QStandardItem( QString("%0").arg(stringrepofIpAddress)); if (selectedNode == myNode) { QFont font ("Arial"); font.setPixelSize(15); font.setWeight(QFont::Bold); item->setFont(font); } model->setItem(row, 0, item); } /** * * @param row The row in the list to place the info * @param myNode The node to be placed * @param sourceNode The node stuff is coming from * @param model The model to place the node info in, which should be the node data list * @param comm The comm model we get our node data from * It places a node in the list of node data. */ void MainWindow::setNodeInModel(int row, node* myNode, node* sourceNode, QStandardItemModel* model, CommModel* comm) { if (myNode == NULL) return; QFont font ("Tokyo"); font.setPointSize(20); font.setWeight(QFont::Bold); font.setItalic(true); bool sn = false; if (selectedNode == myNode) { sn = true; } QStandardItem *item; //item = new QStandardItem( QString("Node %0").arg(row)); char buffer[17]; sprintf(buffer, "%02x:%02x:%02x:%02x:%02x:%02x", myNode->mac_addr.addr[0], myNode->mac_addr.addr[1], myNode->mac_addr.addr[2], myNode->mac_addr.addr[3], myNode->mac_addr.addr[4], myNode->mac_addr.addr[5]); item = new QStandardItem(QString(buffer)); /*item = new QStandardItem( QString("%1:%2:%3:%4:%5:%6").arg(myNode->mac_addr.addr[0], 0, 16) .arg(myNode->mac_addr.addr[1], 0, 16).arg(myNode->mac_addr.addr[2], 0, 16) .arg(myNode->mac_addr.addr[3], 0, 16).arg(myNode->mac_addr.addr[4], 0, 16) .arg(myNode->mac_addr.addr[5], 0, 16));*/ model->setItem(row, 0, item); //IP ADDRESS STUFF struct in_addr theAddr; theAddr.s_addr = ntohl(myNode->ip_addr); char *stringrepofIpAddress = inet_ntoa(theAddr); int thisCol = 1; if(true) { item = new QStandardItem( QString("%0").arg(stringrepofIpAddress)); model->setItem(row, thisCol++, item); } if(showSignalStrength) { item = new QStandardItem( QString("%3 dB").arg(comm->get_signal_strength(sourceNode, myNode))); model->setItem(row, thisCol++, item); } /*if(true) { item = new QStandardItem( QString("%3").arg(comm->get_weight(sourceNode, myNode))); model->setItem(row, thisCol++, item); }*/ if(showThroughput) { item = new QStandardItem( QString("%3 kb").arg(comm->get_throughput(sourceNode, myNode))); model->setItem(row, thisCol++, item); } if(showInactivityTime) { item = new QStandardItem( QString("%3 ms").arg(comm->get_inactive_time(sourceNode, myNode))); model->setItem(row, thisCol++, item); } } /** * It grabs relevant data from the list of nodes, and display them in the list of node data. */ void MainWindow::updateList() { QStandardItemModel* model = new QStandardItemModel(); QStandardItemModel* model_all = new QStandardItemModel(); std::vector allNodes = comm->get_all_nodes(); //By default the selected node is the first one returned if(allNodes.size() > 0) { selectedNode = allNodes[0]; } else { return; } //Iterate through and remove the selected node form the list of other nodes and set //it as selected for(std::vector::iterator it = allNodes.begin(); it != allNodes.end(); ++it) { if( (*it)->ip_addr == widget->getSelectedIP() ) { selectedNode = *it; printf("Set selectedNode to what u clicked on"); } } /* for(int r = 0; r < allNodes.size(); r++) { node* curNode = allNodes[r]; if( curNode.getIpAddr() == widget->getSelectedIP() ) { selectedNode = allNodes[r]; allNodes.erase(allNodes.begin()+r); //POSSIBLE ERROR ABOVE THIS MIGHT NEED TO BNE begin() + (r-1) } }*/ printf("Before get all nodes\n"); widget->setSelectedIP(selectedNode->ip_addr); for(int r = 0; r < allNodes.size(); r++) { setNodeInAllModel(r, allNodes[r], model_all); } if(selectedNode) { int row = 0; struct in_addr theAddr; theAddr.s_addr = ntohl(selectedNode->ip_addr); char *stringrepofIpAddress = inet_ntoa(theAddr); char nbz[50]; strcpy(nbz, "Selected Node: "); strcat(nbz, stringrepofIpAddress); ui->statusBar->showMessage(nbz); foreach(node* n, comm->get_neighbors(selectedNode)) { setNodeInModel(row++, n, selectedNode, model, comm); } } printf("Before model\n"); model_all->setHeaderData(0, Qt::Horizontal, "IP ADDR"); ui->treeView_2->setModel(model_all); int headerRow = 0; model->setHeaderData(headerRow++, Qt::Horizontal, "MAC ADDR"); model->setHeaderData(headerRow++, Qt::Horizontal, "IP ADDR"); if(showSignalStrength) model->setHeaderData(headerRow++, Qt::Horizontal, "Signal Strength"); //model->setHeaderData(headerRow++, Qt::Horizontal, "Weight"); if(showThroughput) model->setHeaderData(headerRow++, Qt::Horizontal, "Throughput"); if(showInactivityTime) model->setHeaderData(headerRow++, Qt::Horizontal, "Inactive Time"); ui->treeView->setModel((model)); widget->updateGraph(allNodes); widget->show(); } /** * Event handler for playbutton. It does different behavior based on the state of whether the mesh * is currently active or not. When it first gets pressed, it has to initialize the comm model. */ void MainWindow::on_playButton_released() { if(firstTime) { firstTime = false; perror("First time being pressed...\n"); perror("Trying to call init...\n"); // comm = CommModel::init(); //sleep(2); perror("About to upadte List\n"); } //Must manage a boolean isPlaying if(!isPlaying) { updateList(); timer->start(refreshRate); } else { //Do pause stuff perror("Stopping timer.\n"); timer->stop(); } togglePlaying(); } /** * Event handler for setting button. When it gets clicked, the setting dialog must be shown. */ void MainWindow::on_settingsButton_released() { settingsDialog = new SettingsDialog(); settingsDialog->setMainWindow(this); settingsDialog->updateSettings(refreshRate, inactiveThreshold, doPhysics, showLatency, showThroughput, showSignalStrength, showInactivityTime); settingsDialog->show(); } void MainWindow::updateSettings(int _refreshRate, int _inactiveThreshold, bool _doPhysics, bool _showSignalStrength, bool _showThroughput, bool _showInactivityTime, bool _showLatency) { refreshRate = _refreshRate; inactiveThreshold = _inactiveThreshold; doPhysics = _doPhysics; showSignalStrength = _showSignalStrength; showThroughput = _showThroughput; showInactivityTime = _showInactivityTime; showLatency = _showLatency; widget->setDoPhysics(_doPhysics); if (!_doPhysics) { // circle plot fprintf(stderr, "=== plot in circle ===\n"); widget->plotNodes(); } } /** * Returns inactive threshold value * @return threshold value */ int MainWindow::getInactiveThreshold() { return inactiveThreshold; } /** * Returns the current commModel. * @return commModel. */ CommModel * MainWindow::getComm() { return comm; }