ParameterEditorController.cc 5.78 KB
Newer Older
Don Gagne's avatar
Don Gagne committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*=====================================================================
 
 QGroundControl Open Source Ground Control Station
 
 (c) 2009, 2015 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 
 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
///     @author Don Gagne <don@thegagnes.com>

#include "ParameterEditorController.h"
#include "AutoPilotPluginManager.h"
29
#include "QGCApplication.h"
Don Gagne's avatar
Don Gagne committed
30

31 32 33
#ifndef __mobile__
#include "QGCFileDialog.h"
#include "QGCMapRCToParamDialog.h"
Don Gagne's avatar
Don Gagne committed
34
#include "MainWindow.h"
35 36
#endif

Don Gagne's avatar
Don Gagne committed
37 38
#include <QStandardPaths>

Don Gagne's avatar
Don Gagne committed
39
/// @Brief Constructs a new ParameterEditorController Widget. This widget is used within the PX4VehicleConfig set of screens.
Don Gagne's avatar
Don Gagne committed
40
ParameterEditorController::ParameterEditorController(void)
Don Gagne's avatar
Don Gagne committed
41
{
42 43 44 45 46 47 48
    if (_autopilot) {
        const QMap<int, QMap<QString, QStringList> >& groupMap = _autopilot->getGroupMap();

        foreach (int componentId, groupMap.keys()) {
            _componentIds += QString("%1").arg(componentId);
        }
    }
Don Gagne's avatar
Don Gagne committed
49 50
}

51 52 53 54 55
ParameterEditorController::~ParameterEditorController()
{
    
}

Don Gagne's avatar
Don Gagne committed
56 57 58 59 60 61 62
QStringList ParameterEditorController::getGroupsForComponent(int componentId)
{
	const QMap<int, QMap<QString, QStringList> >& groupMap = _autopilot->getGroupMap();

	return groupMap[componentId].keys();
}

Don Gagne's avatar
Don Gagne committed
63
QStringList ParameterEditorController::getParametersForGroup(int componentId, QString group)
Don Gagne's avatar
Don Gagne committed
64 65 66 67 68 69
{
	const QMap<int, QMap<QString, QStringList> >& groupMap = _autopilot->getGroupMap();
	
	return groupMap[componentId][group];
}

Don Gagne's avatar
Don Gagne committed
70 71 72 73
QStringList ParameterEditorController::searchParametersForComponent(int componentId, const QString& searchText, bool searchInName, bool searchInDescriptions)
{
    QStringList list;
    
74
    foreach(const QString &paramName, _autopilot->parameterNames(componentId)) {
Don Gagne's avatar
Don Gagne committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
        if (searchText.isEmpty()) {
            list += paramName;
        } else {
            Fact* fact = _autopilot->getParameterFact(componentId, paramName);
            
            if (searchInName && fact->name().contains(searchText, Qt::CaseInsensitive)) {
                list += paramName;
            } else if (searchInDescriptions && (fact->shortDescription().contains(searchText, Qt::CaseInsensitive) || fact->longDescription().contains(searchText, Qt::CaseInsensitive))) {
                list += paramName;
            }
        }
    }
    list.sort();
    
    return list;
}

Don Gagne's avatar
Don Gagne committed
92 93 94 95 96 97
void ParameterEditorController::clearRCToParam(void)
{
	Q_ASSERT(_uas);
	_uas->unsetRCToParameterMap();
}

Don Gagne's avatar
Don Gagne committed
98
void ParameterEditorController::saveToFile(const QString& filename)
Don Gagne's avatar
Don Gagne committed
99
{
100 101 102 103 104
    if (!_autopilot) {
        qWarning() << "Internal error _autopilot==NULL";
        return;
    }

Don Gagne's avatar
Don Gagne committed
105 106
    if (!filename.isEmpty()) {
        QFile file(filename);
Don Gagne's avatar
Don Gagne committed
107 108
        
		if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
Don Gagne's avatar
Don Gagne committed
109
            qgcApp()->showMessage(QString("Unable to create file: %1").arg(filename));
Don Gagne's avatar
Don Gagne committed
110 111 112 113 114 115 116 117 118
			return;
		}
        
		QTextStream stream(&file);
		_autopilot->writeParametersToStream(stream);
		file.close();
	}
}

Don Gagne's avatar
Don Gagne committed
119
void ParameterEditorController::saveToFilePicker(void)
Don Gagne's avatar
Don Gagne committed
120
{
121
#ifndef __mobile__
Don Gagne's avatar
Don Gagne committed
122 123 124 125 126 127 128 129 130 131 132 133
    QString fileName = QGCFileDialog::getSaveFileName(NULL,
                                                      "Save Parameters",
                                                      QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation),
                                                      "Parameter Files (*.params)",
                                                      "params",
                                                      true);
    saveToFile(fileName);
#endif
}

void ParameterEditorController::loadFromFile(const QString& filename)
{
134 135
    QString errors;
    
136 137 138 139 140
    if (!_autopilot) {
        qWarning() << "Internal error _autopilot==NULL";
        return;
    }

Don Gagne's avatar
Don Gagne committed
141 142
    if (!filename.isEmpty()) {
        QFile file(filename);
Don Gagne's avatar
Don Gagne committed
143 144
        
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
Don Gagne's avatar
Don Gagne committed
145
            qgcApp()->showMessage(QString("Unable to open file: %1").arg(filename));
Don Gagne's avatar
Don Gagne committed
146 147 148 149
            return;
        }
        
        QTextStream stream(&file);
150
        errors = _autopilot->readParametersFromStream(stream);
Don Gagne's avatar
Don Gagne committed
151
        file.close();
152 153 154 155
        
        if (!errors.isEmpty()) {
            emit showErrorMessage(errors);
        }
Don Gagne's avatar
Don Gagne committed
156
    }
Don Gagne's avatar
Don Gagne committed
157 158 159 160 161 162 163 164 165 166
}

void ParameterEditorController::loadFromFilePicker(void)
{
#ifndef __mobile__
    QString fileName = QGCFileDialog::getOpenFileName(NULL,
                                                      "Load Parameters",
                                                      QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation),
                                                      "Parameter Files (*.params);;All Files (*)");
    loadFromFile(fileName);
167
#endif
Don Gagne's avatar
Don Gagne committed
168 169 170 171 172 173 174
}

void ParameterEditorController::refresh(void)
{
	_autopilot->refreshAllParameters();
}

175 176 177 178 179 180
void ParameterEditorController::resetAllToDefaults(void)
{
    _autopilot->resetAllParametersToDefaults();
    refresh();
}

Don Gagne's avatar
Don Gagne committed
181 182
void ParameterEditorController::setRCToParam(const QString& paramName)
{
183 184 185
#ifdef __mobile__
    Q_UNUSED(paramName)
#else
Don Gagne's avatar
Don Gagne committed
186
	Q_ASSERT(_uas);
187
    QGCMapRCToParamDialog * d = new QGCMapRCToParamDialog(paramName, _uas, qgcApp()->toolbox()->multiVehicleManager(), MainWindow::instance());
Don Gagne's avatar
Don Gagne committed
188
	d->exec();
189
#endif
Don Gagne's avatar
Don Gagne committed
190
}