QGCLinkConfiguration.cc 10 KB
Newer Older
1 2 3 4
/*=====================================================================

QGroundControl Open Source Ground Control Station

5
(c) 2009, 2015 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

This file is part of the QGROUNDCONTROL project

    QGROUNDCONTROL is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    QGROUNDCONTROL 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.

======================================================================*/

/**
 * @file
 *   @brief Implementation of QGCLinkConfiguration
 *   @author Gus Grubba <mavlink@grubba.com>
 */

#include "SettingsDialog.h"
#include "QGCLinkConfiguration.h"
#include "ui_QGCLinkConfiguration.h"
#include "QGCCommConfiguration.h"
#include "QGCMessageBox.h"
Don Gagne's avatar
Don Gagne committed
35 36
#include "UDPLink.h"
#include "TCPLink.h"
37 38 39 40 41 42

QGCLinkConfiguration::QGCLinkConfiguration(QWidget *parent) :
    QWidget(parent),
    _ui(new Ui::QGCLinkConfiguration)
{
    // Stop automatic link updates while this UI is up
43
    qgcApp()->toolbox()->linkManager()->suspendConfigurationUpdates(true);
44 45 46 47 48 49 50 51 52 53 54 55 56
    _ui->setupUi(this);
    _viewModel = new LinkViewModel;
    _ui->linkView->setModel(_viewModel);
    _ui->connectLinkButton->setEnabled(false);
    _ui->delLinkButton->setEnabled(false);
    _ui->editLinkButton->setEnabled(false);
}

QGCLinkConfiguration::~QGCLinkConfiguration()
{
    if(_viewModel) delete _viewModel;
    if(_ui) delete _ui;
    // Resume automatic link updates
57
    qgcApp()->toolbox()->linkManager()->suspendConfigurationUpdates(false);
58 59 60 61 62
}

void QGCLinkConfiguration::on_delLinkButton_clicked()
{
    QModelIndex index = _ui->linkView->currentIndex();
dogmaphobic's avatar
dogmaphobic committed
63 64 65 66 67 68 69 70 71 72 73
    if(index.row() >= 0) {
        LinkConfiguration* config = _viewModel->getConfiguration(index.row());
        if(config) {
            // Ask user if they are sure
            QMessageBox::StandardButton button = QGCMessageBox::question(
                tr("Delete Link Configuration"),
                tr("Are you sure you want to delete %1?\nDeleting a configuration will also disconnect it if connected.").arg(config->name()),
                QMessageBox::Yes | QMessageBox::Cancel,
                QMessageBox::Cancel);
            if (button == QMessageBox::Yes) {
                // Get link attached to this configuration (if any)
Don Gagne's avatar
Don Gagne committed
74
                LinkInterface* iface = config->link();
dogmaphobic's avatar
dogmaphobic committed
75
                if(iface) {
Don Gagne's avatar
Don Gagne committed
76
                    qgcApp()->toolbox()->linkManager()->disconnectLink(iface);
dogmaphobic's avatar
dogmaphobic committed
77 78
                }
                _viewModel->beginChange();
Don Gagne's avatar
Don Gagne committed
79

dogmaphobic's avatar
dogmaphobic committed
80
                // Remove configuration
Don Gagne's avatar
Don Gagne committed
81 82 83 84
                QmlObjectListModel* linkConfigurations = qgcApp()->toolbox()->linkManager()->linkConfigurations();
                linkConfigurations->removeOne(config);
                delete config;

dogmaphobic's avatar
dogmaphobic committed
85
                // Save list
86
                qgcApp()->toolbox()->linkManager()->saveLinkConfigurationList();
dogmaphobic's avatar
dogmaphobic committed
87
                _viewModel->endChange();
88 89 90
            }
        }
    }
dogmaphobic's avatar
dogmaphobic committed
91
    _updateButtons();
92 93
}

dogmaphobic's avatar
dogmaphobic committed
94
void QGCLinkConfiguration::on_linkView_clicked(const QModelIndex&)
95
{
dogmaphobic's avatar
dogmaphobic committed
96
    _updateButtons();
97 98 99 100 101
}

void QGCLinkConfiguration::on_connectLinkButton_clicked()
{
    QModelIndex index = _ui->linkView->currentIndex();
dogmaphobic's avatar
dogmaphobic committed
102 103 104
    if(index.row() >= 0) {
        LinkConfiguration* config = _viewModel->getConfiguration(index.row());
        if(config) {
Don Gagne's avatar
Don Gagne committed
105
            LinkInterface* link = config->link();
106
            if(link) {
dogmaphobic's avatar
dogmaphobic committed
107 108
                // Disconnect Link
                if (link->isConnected()) {
Don Gagne's avatar
Don Gagne committed
109
                    qgcApp()->toolbox()->linkManager()->disconnectLink(link);
dogmaphobic's avatar
dogmaphobic committed
110 111
                }
            } else {
112
                LinkInterface* link = qgcApp()->toolbox()->linkManager()->createConnectedLink(config);
dogmaphobic's avatar
dogmaphobic committed
113 114 115 116 117 118 119 120 121 122
                if(link) {
                    // Now go hunting for the parent so we can shut this down
                    QWidget* pQw = parentWidget();
                    while(pQw) {
                        SettingsDialog* pDlg = dynamic_cast<SettingsDialog*>(pQw);
                        if(pDlg) {
                            pDlg->accept();
                            break;
                        }
                        pQw = pQw->parentWidget();
123 124 125 126 127
                    }
                }
            }
        }
    }
dogmaphobic's avatar
dogmaphobic committed
128
    _updateButtons();
129 130 131 132 133 134 135 136
}

void QGCLinkConfiguration::on_editLinkButton_clicked()
{
    QModelIndex index = _ui->linkView->currentIndex();
    _editLink(index.row());
}

137 138 139 140 141 142
void QGCLinkConfiguration::_fixUnnamed(LinkConfiguration* config)
{
    Q_ASSERT(config != NULL);
    //-- Check for "Unnamed"
    if (config->name() == tr("Unnamed")) {
        switch(config->type()) {
dogmaphobic's avatar
dogmaphobic committed
143
#ifndef __ios__
144 145
            case LinkConfiguration::TypeSerial: {
                QString tname = dynamic_cast<SerialConfiguration*>(config)->portName();
146
#ifdef Q_OS_WIN
147 148
                tname.replace("\\\\.\\", "");
#else
149
                tname.replace("/dev/cu.", "");
150 151 152
                tname.replace("/dev/", "");
#endif
                config->setName(QString("Serial Device on %1").arg(tname));
153
                break;
154
                }
dogmaphobic's avatar
dogmaphobic committed
155
#endif
156 157 158 159 160 161 162 163 164 165 166 167
            case LinkConfiguration::TypeUdp:
                config->setName(
                    QString("UDP Link on Port %1").arg(dynamic_cast<UDPConfiguration*>(config)->localPort()));
                break;
            case LinkConfiguration::TypeTcp: {
                    TCPConfiguration* tconfig = dynamic_cast<TCPConfiguration*>(config);
                    if(tconfig) {
                        config->setName(
                            QString("TCP Link %1:%2").arg(tconfig->address().toString()).arg((int)tconfig->port()));
                    }
                }
                break;
dogmaphobic's avatar
dogmaphobic committed
168
#ifndef __mobile__
Don Gagne's avatar
Don Gagne committed
169
            case LinkConfiguration::TypeLogReplay: {
dogmaphobic's avatar
dogmaphobic committed
170 171 172 173
                    LogReplayLinkConfiguration* tconfig = dynamic_cast<LogReplayLinkConfiguration*>(config);
                    if(tconfig) {
                        config->setName(QString("Log Replay %1").arg(tconfig->logFilenameShort()));
                    }
Don Gagne's avatar
Don Gagne committed
174 175
                }
                break;
dogmaphobic's avatar
dogmaphobic committed
176
#endif
177
#ifdef QT_DEBUG
178 179 180 181 182
            case LinkConfiguration::TypeMock:
                config->setName(
                    QString("Mock Link"));
                break;
#endif
183 184 185
            case LinkConfiguration::TypeLast:
            default:
                break;
186 187 188 189
        }
    }
}

190 191 192 193 194 195 196
void QGCLinkConfiguration::on_addLinkButton_clicked()
{
    QGCCommConfiguration* commDialog = new QGCCommConfiguration(this);
    if(commDialog->exec() == QDialog::Accepted) {
        // Save changes (if any)
        LinkConfiguration* config = commDialog->getConfig();
        if(config) {
197
            _fixUnnamed(config);
198
            _viewModel->beginChange();
Don Gagne's avatar
Don Gagne committed
199
            qgcApp()->toolbox()->linkManager()->linkConfigurations()->append(commDialog->getConfig());
200
            qgcApp()->toolbox()->linkManager()->saveLinkConfigurationList();
201 202 203
            _viewModel->endChange();
        }
    }
dogmaphobic's avatar
dogmaphobic committed
204
    _updateButtons();
205 206 207 208 209 210 211 212 213
}

void QGCLinkConfiguration::on_linkView_doubleClicked(const QModelIndex &index)
{
    _editLink(index.row());
}

void QGCLinkConfiguration::_editLink(int row)
{
dogmaphobic's avatar
dogmaphobic committed
214 215 216 217 218 219 220 221 222 223 224 225
    if(row >= 0) {
        LinkConfiguration* config = _viewModel->getConfiguration(row);
        if(config) {
            LinkConfiguration* tmpConfig = LinkConfiguration::duplicateSettings(config);
            QGCCommConfiguration* commDialog = new QGCCommConfiguration(this, tmpConfig);
            if(commDialog->exec() == QDialog::Accepted) {
                // Save changes (if any)
                if(commDialog->getConfig()) {
                    _fixUnnamed(tmpConfig);
                    _viewModel->beginChange();
                    config->copyFrom(tmpConfig);
                    // Save it
226
                    qgcApp()->toolbox()->linkManager()->saveLinkConfigurationList();
dogmaphobic's avatar
dogmaphobic committed
227 228 229 230
                    _viewModel->endChange();
                    // Tell link about changes (if any)
                    config->updateSettings();
                }
231
            }
dogmaphobic's avatar
dogmaphobic committed
232 233 234
            // Discard temporary duplicate
            if(commDialog->getConfig())
                delete commDialog->getConfig();
235 236
        }
    }
dogmaphobic's avatar
dogmaphobic committed
237 238 239 240 241 242 243 244
    _updateButtons();
}

void QGCLinkConfiguration::_updateButtons()
{
    LinkConfiguration* config = NULL;
    QModelIndex index = _ui->linkView->currentIndex();
    bool enabled = (index.row() >= 0);
245
    bool deleteEnabled = true;
dogmaphobic's avatar
dogmaphobic committed
246 247 248
    if(enabled) {
        config = _viewModel->getConfiguration(index.row());
        if(config) {
249 250 251 252
            // Can't delete a dynamic link
            if(config->isDynamic()) {
                deleteEnabled = false;
            }
Don Gagne's avatar
Don Gagne committed
253
            LinkInterface* link = config->link();
dogmaphobic's avatar
dogmaphobic committed
254 255 256 257 258 259 260 261
            if(link) {
                _ui->connectLinkButton->setText("Disconnect");
            } else {
                _ui->connectLinkButton->setText("Connect");
            }
        }
    }
    _ui->connectLinkButton->setEnabled(enabled);
262
    _ui->delLinkButton->setEnabled(config != NULL && deleteEnabled);
dogmaphobic's avatar
dogmaphobic committed
263
    _ui->editLinkButton->setEnabled(config != NULL);
264 265 266 267 268 269 270 271 272 273
}

LinkViewModel::LinkViewModel(QObject *parent) : QAbstractListModel(parent)
{
    Q_UNUSED(parent);
}

int LinkViewModel::rowCount( const QModelIndex & parent) const
{
    Q_UNUSED(parent);
Don Gagne's avatar
Don Gagne committed
274
    return qgcApp()->toolbox()->linkManager()->linkConfigurations()->count();
275 276 277 278
}

QVariant LinkViewModel::data( const QModelIndex & index, int role) const
{
Don Gagne's avatar
Don Gagne committed
279 280
    if (role == Qt::DisplayRole && index.row() < rowCount()) {
        QString name(qgcApp()->toolbox()->linkManager()->linkConfigurations()->value<LinkConfiguration*>(index.row())->name());
281 282 283 284 285 286 287
        return name;
    }
    return QVariant();
}

LinkConfiguration* LinkViewModel::getConfiguration(int row)
{
Don Gagne's avatar
Don Gagne committed
288 289
    if(row < rowCount()) {
        return qgcApp()->toolbox()->linkManager()->linkConfigurations()->value<LinkConfiguration*>(row);
290 291 292 293
    }
    return NULL;
}