Example #1
0
void MainWindow::connectToServer(QString host, int port, QString username, QString password)
{
    // Create client
    QArClient *client = new QArClient(username);

    // Connect client to server
    if (!client->connect(host, port, username, password))
    {
        QString msg;
        QTextStream ts(&msg);
        ts << "Error: Unable to connect to " << host;
        QMessageBox msgBox;
        msgBox.setText(msg);
        msgBox.exec();
        return;
    }

    // Connect signals and slots
    // -- Client to MapScene --
    qRegisterMetaType<ArRobotInfo>("ArRobotInfo");
    QObject::connect((QObject *) client, SIGNAL(updatePose(ArRobotInfo)),
                     _mapScene, SLOT(updateRobotPose(ArRobotInfo)));
    QObject::connect((QObject *) client, SIGNAL(updatePath(Points *)),
                     _mapScene, SLOT(updateRobotPath(Points *)));
    QObject::connect((QObject *) client, SIGNAL(updatePose(ArRobotInfo)),
                     this, SLOT(updateStatus(ArRobotInfo)));
    // -- MapScene to Client --
    QObject::connect((QObject *) _mapScene, SIGNAL(mapChanged(ArMap *)),
                     client, SLOT(mapChanged(ArMap *)));
    QObject::connect((QObject *) _mapScene, SIGNAL(stop()),
                     client, SLOT(stop()));

    // -- Client to main window --
    QObject::connect(client, SIGNAL(disconnected(QString)),
                      this, SLOT(lostConnection(QString)));

    // -- Teleop to client --
    QObject::connect((QObject *) &_teleop,
                     SIGNAL(sendInputs(double, double, double)),
                     client,
                     SLOT(ratioDrive(double, double, double)));

    // Set as main client
    // TODO: support multiple robots
    _client = client;

    // If no map exists
    if (_mapScene->getMap() == NULL)
    {
        // Get map from server
        client->lock();
        ArMap *map = client->getMapFromServer();
        client->unlock();
        _mapScene->renderMap(map);

        // Fill goals combobox with goal names
        QStringList goalList(_mapScene->goalList());
        _goalsComboBox->addItems(goalList);
    }
    else
    {
        // Send map to server
        _mapScene->updateMap();
    }

    // Set map view
    ui->mapView->fitInView(_mapScene->sceneRect(), Qt::KeepAspectRatio);

    // Get periodic updates
    client->getUpdates(100);

    // Display connected status
    statusBar()->showMessage(QString("Connected."), 0);

    // Record user name
    _user.append(username);

    // Enable map and nav controls
    toggleMapControls(true);
    toggleNavControls(true);

    // Start logging
    _sessionName.clear();
    QTextStream ts2(&_sessionName);
    ts2 << username << '_' << _mapScene->getMapName() << '_'
        << QDateTime::currentDateTime().toString(QString("yyyy_MM_dd_hh:mm:ss"));
    startDataLogging(_sessionName);
}