ParameterEditorController.cc 5.2 KB
Newer Older
1 2 3 4 5 6 7 8 9
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

Don Gagne's avatar
Don Gagne committed
10 11 12 13 14 15

/// @file
///     @author Don Gagne <don@thegagnes.com>

#include "ParameterEditorController.h"
#include "AutoPilotPluginManager.h"
16
#include "QGCApplication.h"
Don Gagne's avatar
Don Gagne committed
17

18 19 20
#ifndef __mobile__
#include "QGCFileDialog.h"
#include "QGCMapRCToParamDialog.h"
Don Gagne's avatar
Don Gagne committed
21
#include "MainWindow.h"
22 23
#endif

Don Gagne's avatar
Don Gagne committed
24 25
#include <QStandardPaths>

Don Gagne's avatar
Don Gagne committed
26
/// @Brief Constructs a new ParameterEditorController Widget. This widget is used within the PX4VehicleConfig set of screens.
Don Gagne's avatar
Don Gagne committed
27
ParameterEditorController::ParameterEditorController(void)
Don Gagne's avatar
Don Gagne committed
28
{
29 30 31 32 33 34 35
    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
36 37
}

38 39 40 41 42
ParameterEditorController::~ParameterEditorController()
{
    
}

Don Gagne's avatar
Don Gagne committed
43 44 45 46 47 48 49
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
50
QStringList ParameterEditorController::getParametersForGroup(int componentId, QString group)
Don Gagne's avatar
Don Gagne committed
51 52 53 54 55 56
{
	const QMap<int, QMap<QString, QStringList> >& groupMap = _autopilot->getGroupMap();
	
	return groupMap[componentId][group];
}

Don Gagne's avatar
Don Gagne committed
57 58 59 60
QStringList ParameterEditorController::searchParametersForComponent(int componentId, const QString& searchText, bool searchInName, bool searchInDescriptions)
{
    QStringList list;
    
61
    foreach(const QString &paramName, _autopilot->parameterNames(componentId)) {
Don Gagne's avatar
Don Gagne committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
        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
79 80 81 82 83 84
void ParameterEditorController::clearRCToParam(void)
{
	Q_ASSERT(_uas);
	_uas->unsetRCToParameterMap();
}

Don Gagne's avatar
Don Gagne committed
85
void ParameterEditorController::saveToFile(const QString& filename)
Don Gagne's avatar
Don Gagne committed
86
{
87 88 89 90 91
    if (!_autopilot) {
        qWarning() << "Internal error _autopilot==NULL";
        return;
    }

Don Gagne's avatar
Don Gagne committed
92 93
    if (!filename.isEmpty()) {
        QFile file(filename);
Don Gagne's avatar
Don Gagne committed
94 95
        
		if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
Don Gagne's avatar
Don Gagne committed
96
            qgcApp()->showMessage(QString("Unable to create file: %1").arg(filename));
Don Gagne's avatar
Don Gagne committed
97 98 99 100 101 102 103 104 105
			return;
		}
        
		QTextStream stream(&file);
		_autopilot->writeParametersToStream(stream);
		file.close();
	}
}

Don Gagne's avatar
Don Gagne committed
106
void ParameterEditorController::saveToFilePicker(void)
Don Gagne's avatar
Don Gagne committed
107
{
108
#ifndef __mobile__
Don Gagne's avatar
Don Gagne committed
109 110 111 112 113 114 115 116 117 118 119 120
    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)
{
121 122
    QString errors;
    
123 124 125 126 127
    if (!_autopilot) {
        qWarning() << "Internal error _autopilot==NULL";
        return;
    }

Don Gagne's avatar
Don Gagne committed
128 129
    if (!filename.isEmpty()) {
        QFile file(filename);
Don Gagne's avatar
Don Gagne committed
130 131
        
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
Don Gagne's avatar
Don Gagne committed
132
            qgcApp()->showMessage(QString("Unable to open file: %1").arg(filename));
Don Gagne's avatar
Don Gagne committed
133 134 135 136
            return;
        }
        
        QTextStream stream(&file);
137
        errors = _autopilot->readParametersFromStream(stream);
Don Gagne's avatar
Don Gagne committed
138
        file.close();
139 140 141 142
        
        if (!errors.isEmpty()) {
            emit showErrorMessage(errors);
        }
Don Gagne's avatar
Don Gagne committed
143
    }
Don Gagne's avatar
Don Gagne committed
144 145 146 147 148 149 150 151 152 153
}

void ParameterEditorController::loadFromFilePicker(void)
{
#ifndef __mobile__
    QString fileName = QGCFileDialog::getOpenFileName(NULL,
                                                      "Load Parameters",
                                                      QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation),
                                                      "Parameter Files (*.params);;All Files (*)");
    loadFromFile(fileName);
154
#endif
Don Gagne's avatar
Don Gagne committed
155 156 157 158 159 160 161
}

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

162 163 164 165 166 167
void ParameterEditorController::resetAllToDefaults(void)
{
    _autopilot->resetAllParametersToDefaults();
    refresh();
}

Don Gagne's avatar
Don Gagne committed
168 169
void ParameterEditorController::setRCToParam(const QString& paramName)
{
170 171 172
#ifdef __mobile__
    Q_UNUSED(paramName)
#else
Don Gagne's avatar
Don Gagne committed
173
	Q_ASSERT(_uas);
174
    QGCMapRCToParamDialog * d = new QGCMapRCToParamDialog(paramName, _uas, qgcApp()->toolbox()->multiVehicleManager(), MainWindow::instance());
Don Gagne's avatar
Don Gagne committed
175
	d->exec();
176
#endif
Don Gagne's avatar
Don Gagne committed
177
}