ParameterEditorController.cc 4.26 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 29 30 31 32 33 34
/*=====================================================================
 
 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"
#include "QGCFileDialog.h"
#include "QGCMessageBox.h"
#include "QGCMapRCToParamDialog.h"
#include "MainWindow.h"

/// @Brief Constructs a new ParameterEditorController Widget. This widget is used within the PX4VehicleConfig set of screens.
Don Gagne's avatar
Don Gagne committed
35
ParameterEditorController::ParameterEditorController(void)
Don Gagne's avatar
Don Gagne committed
36 37 38 39 40 41 42 43
{
    const QMap<int, QMap<QString, QStringList> >& groupMap = _autopilot->getGroupMap();
    
    foreach (int componentId, groupMap.keys()) {
		_componentIds += QString("%1").arg(componentId);
	}
}

44 45 46 47 48
ParameterEditorController::~ParameterEditorController()
{
    
}

Don Gagne's avatar
Don Gagne committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
QStringList ParameterEditorController::getGroupsForComponent(int componentId)
{
	const QMap<int, QMap<QString, QStringList> >& groupMap = _autopilot->getGroupMap();

	return groupMap[componentId].keys();
}

QStringList ParameterEditorController::getFactsForGroup(int componentId, QString group)
{
	const QMap<int, QMap<QString, QStringList> >& groupMap = _autopilot->getGroupMap();
	
	return groupMap[componentId][group];
}

void ParameterEditorController::clearRCToParam(void)
{
	Q_ASSERT(_uas);
	_uas->unsetRCToParameterMap();
}

void ParameterEditorController::saveToFile(void)
{
	Q_ASSERT(_autopilot);
	
    QString msgTitle("Save Parameters");
    
	QString fileName = QGCFileDialog::getSaveFileName(NULL,
                                                      msgTitle,
                                                      qgcApp()->savedParameterFilesLocation(),
                                                      "Parameter Files (*.params)",
                                                      "params",
                                                      true);
	if (!fileName.isEmpty()) {
		QFile file(fileName);
        
		if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
            QGCMessageBox::critical(msgTitle, "Unable to create file");
			return;
		}
        
		QTextStream stream(&file);
		_autopilot->writeParametersToStream(stream);
		file.close();
	}
}

void ParameterEditorController::loadFromFile(void)
{
97 98
    QString errors;
    
Don Gagne's avatar
Don Gagne committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    Q_ASSERT(_autopilot);
    
    QString msgTitle("Load Parameters");
    
	QString fileName = QGCFileDialog::getOpenFileName(NULL,
                                                      msgTitle,
                                                      qgcApp()->savedParameterFilesLocation(),
													  "Parameter Files (*.params);;All Files (*)");
    if (!fileName.isEmpty()) {
        QFile file(fileName);
        
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
            QGCMessageBox::critical(msgTitle, "Unable to open file");
            return;
        }
        
        QTextStream stream(&file);
116
        errors = _autopilot->readParametersFromStream(stream);
Don Gagne's avatar
Don Gagne committed
117
        file.close();
118 119 120 121
        
        if (!errors.isEmpty()) {
            emit showErrorMessage(errors);
        }
Don Gagne's avatar
Don Gagne committed
122 123 124 125 126 127 128 129
    }
}

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

130 131 132 133 134 135
void ParameterEditorController::resetAllToDefaults(void)
{
    _autopilot->resetAllParametersToDefaults();
    refresh();
}

Don Gagne's avatar
Don Gagne committed
136 137 138 139 140 141
void ParameterEditorController::setRCToParam(const QString& paramName)
{
	Q_ASSERT(_uas);
	QGCMapRCToParamDialog * d = new QGCMapRCToParamDialog(paramName, _uas, MainWindow::instance());
	d->exec();
}