1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-01-26 11:52:10 +01:00

1351 lines
48 KiB
C++
Raw Normal View History

2012-02-05 18:16:38 +00:00
/**
******************************************************************************
*
* @file configtaskwidget.cpp
2012-02-05 20:07:19 +00:00
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
2012-02-05 18:16:38 +00:00
* @addtogroup GCSPlugins GCS Plugins
* @{
2012-02-05 20:07:19 +00:00
* @addtogroup UAVObjectWidgetUtils Plugin
2012-02-05 18:16:38 +00:00
* @{
2012-02-05 20:07:19 +00:00
* @brief Utility plugin for UAVObject to Widget relation management
2012-02-05 18:16:38 +00:00
*****************************************************************************/
/*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "configtaskwidget.h"
#include <QWidget>
#include <QLineEdit>
2012-02-05 18:16:38 +00:00
#include "uavsettingsimportexport/uavsettingsimportexportfactory.h"
/**
* Constructor
*/
ConfigTaskWidget::ConfigTaskWidget(QWidget *parent) : QWidget(parent), m_isConnected(false), m_isWidgetUpdatesAllowed(true),
m_saveButton(NULL), m_isDirty(false), m_outOfLimitsStyle("background-color: rgb(255, 0, 0);"), m_realtimeUpdateTimer(NULL)
2012-02-05 18:16:38 +00:00
{
m_pluginManager = ExtensionSystem::PluginManager::instance();
TelemetryManager *telMngr = m_pluginManager->getObject<TelemetryManager>();
m_objectUtilManager = m_pluginManager->getObject<UAVObjectUtilManager>();
connect(telMngr, SIGNAL(connected()), this, SLOT(onAutopilotConnect()), Qt::UniqueConnection);
connect(telMngr, SIGNAL(disconnected()), this, SLOT(onAutopilotDisconnect()), Qt::UniqueConnection);
connect(telMngr, SIGNAL(connected()), this, SIGNAL(autoPilotConnected()), Qt::UniqueConnection);
connect(telMngr, SIGNAL(disconnected()), this, SIGNAL(autoPilotDisconnected()), Qt::UniqueConnection);
UAVSettingsImportExportFactory *importexportplugin = m_pluginManager->getObject<UAVSettingsImportExportFactory>();
connect(importexportplugin, SIGNAL(importAboutToBegin()), this, SLOT(invalidateObjects()));
2012-02-05 18:16:38 +00:00
}
/**
* Add a widget to the dirty detection pool
* @param widget to add to the detection pool
*/
void ConfigTaskWidget::addWidget(QWidget *widget)
2012-02-05 18:16:38 +00:00
{
addWidgetBinding("", "", widget);
2012-02-05 18:16:38 +00:00
}
/**
* Add an object to the management system
* @param objectName name of the object to add to the management system
*/
void ConfigTaskWidget::addUAVObject(QString objectName, QList<int> *reloadGroups)
2012-02-05 18:16:38 +00:00
{
addWidgetBinding(objectName, "", NULL, 0, 1, false, reloadGroups);
2012-02-05 18:16:38 +00:00
}
void ConfigTaskWidget::addUAVObject(UAVObject *objectName, QList<int> *reloadGroups)
{
QString objstr;
if (objectName) {
objstr = objectName->getName();
}
addUAVObject(objstr, reloadGroups);
2012-02-05 18:16:38 +00:00
}
/**
* Add an UAVObject field to widget relation to the management system
* @param object name of the object to add
* @param field name of the field to add
* @param widget pointer to the widget whitch will display/define the field value
* @param index index of the field element to add to this relation
*/
void ConfigTaskWidget::addWidgetBinding(QString object, QString field, QWidget *widget, QString index)
2012-02-05 18:16:38 +00:00
{
UAVObject *obj = NULL;
UAVObjectField *_field = NULL;
obj = getObject(QString(object));
2012-02-05 18:16:38 +00:00
Q_ASSERT(obj);
_field = obj->getField(QString(field));
Q_ASSERT(_field);
addWidgetBinding(object, field, widget, _field->getElementNames().indexOf(index));
2012-02-05 18:16:38 +00:00
}
void ConfigTaskWidget::addWidgetBinding(UAVObject *obj, UAVObjectField *field, QWidget *widget, QString index)
{
QString objstr;
QString fieldstr;
if (obj) {
objstr = obj->getName();
}
if (field) {
fieldstr = field->getName();
}
addWidgetBinding(objstr, fieldstr, widget, index);
}
/**
* Add a UAVObject field to widget relation to the management system
* @param object name of the object to add
* @param field name of the field to add
* @param widget pointer to the widget whitch will display/define the field value
* @param element name of the element of the field element to add to this relation
* @param scale scale value of this relation
* @param isLimited bool to signal if this widget contents is limited in value
* @param defaultReloadGroups default and reload groups this relation belongs to
* @param instID instance ID of the object used on this relation
*/
void ConfigTaskWidget::addWidgetBinding(QString object, QString field, QWidget *widget, QString element, double scale, bool isLimited, QList<int> *defaultReloadGroups, quint32 instID)
2012-02-05 18:16:38 +00:00
{
UAVObject *obj = getObject(QString(object), instID);
2012-02-05 18:16:38 +00:00
Q_ASSERT(obj);
UAVObjectField *_field;
int index = 0;
if (!field.isEmpty() && obj) {
2012-02-05 18:16:38 +00:00
_field = obj->getField(QString(field));
if (!element.isEmpty()) {
index = _field->getElementNames().indexOf(QString(element));
}
}
addWidgetBinding(object, field, widget, index, scale, isLimited, defaultReloadGroups, instID);
2012-02-05 18:16:38 +00:00
}
void ConfigTaskWidget::addWidgetBinding(UAVObject *obj, UAVObjectField *field, QWidget *widget, QString element, double scale, bool isLimited, QList<int> *defaultReloadGroups, quint32 instID)
{
QString objstr;
QString fieldstr;
if (obj) {
objstr = obj->getName();
}
if (field) {
fieldstr = field->getName();
}
addWidgetBinding(objstr, fieldstr, widget, element, scale, isLimited, defaultReloadGroups, instID);
}
void ConfigTaskWidget::addWidgetBinding(UAVObject *obj, UAVObjectField *field, QWidget *widget, int index, double scale, bool isLimited, QList<int> *defaultReloadGroups, quint32 instID)
{
QString objstr;
QString fieldstr;
if (obj) {
objstr = obj->getName();
}
if (field) {
fieldstr = field->getName();
}
addWidgetBinding(objstr, fieldstr, widget, index, scale, isLimited, defaultReloadGroups, instID);
}
/**
* Add an UAVObject field to widget relation to the management system
* @param object name of the object to add
* @param field name of the field to add
* @param widget pointer to the widget whitch will display/define the field value
* @param index index of the element of the field to add to this relation
* @param scale scale value of this relation
* @param isLimited bool to signal if this widget contents is limited in value
* @param defaultReloadGroups default and reload groups this relation belongs to
* @param instID instance ID of the object used on this relation
*/
void ConfigTaskWidget::addWidgetBinding(QString object, QString field, QWidget *widget, int index, double scale, bool isLimited, QList<int> *defaultReloadGroups, quint32 instID)
2012-02-05 18:16:38 +00:00
{
if (addShadowWidgetBinding(object, field, widget, index, scale, isLimited, defaultReloadGroups, instID)) {
2012-02-05 18:16:38 +00:00
return;
}
2012-02-05 18:16:38 +00:00
UAVObject *obj = NULL;
UAVObjectField *_field = NULL;
if (!object.isEmpty()) {
obj = getObject(QString(object), instID);
2012-02-05 18:16:38 +00:00
Q_ASSERT(obj);
m_updatedObjects.insert(obj, true);
connect(obj, SIGNAL(objectUpdated(UAVObject *)), this, SLOT(objectUpdated(UAVObject *)));
connect(obj, SIGNAL(objectUpdated(UAVObject *)), this, SLOT(refreshWidgetsValues(UAVObject *)), Qt::UniqueConnection);
2012-02-05 18:16:38 +00:00
}
if (!field.isEmpty() && obj) {
2012-02-05 18:16:38 +00:00
_field = obj->getField(QString(field));
}
WidgetBinding *binding = new WidgetBinding(widget, obj, _field, index, scale, isLimited);
m_widgetBindings.append(binding);
if (obj && m_saveButton) {
m_saveButton->addObject((UAVDataObject *)obj);
2012-02-05 18:16:38 +00:00
}
if (!widget) {
if (defaultReloadGroups && obj) {
foreach(int i, *defaultReloadGroups) {
if (this->m_reloadGroups.contains(i)) {
this->m_reloadGroups.value(i)->append(binding);
} else {
this->m_reloadGroups.insert(i, new QList<WidgetBinding *>());
this->m_reloadGroups.value(i)->append(binding);
}
}
}
} else {
connectWidgetUpdatesToSlot(widget, SLOT(widgetsContentsChanged()));
if (defaultReloadGroups) {
2014-01-01 23:17:31 +01:00
addWidgetToReloadGroups(widget, defaultReloadGroups);
}
m_shadowBindings.insert(widget, binding);
loadWidgetLimits(widget, _field, index, isLimited, scale);
2012-02-05 18:16:38 +00:00
}
}
/**
* destructor
*/
2012-02-05 18:16:38 +00:00
ConfigTaskWidget::~ConfigTaskWidget()
{
if (m_saveButton) {
delete m_saveButton;
}
foreach(QList<WidgetBinding *> *reloadGroup, m_reloadGroups.values()) {
if (reloadGroup) {
delete reloadGroup;
}
2012-02-05 18:16:38 +00:00
}
foreach(WidgetBinding * binding, m_widgetBindings) {
if (binding) {
delete binding;
}
2012-02-05 18:16:38 +00:00
}
if (m_realtimeUpdateTimer) {
delete m_realtimeUpdateTimer;
m_realtimeUpdateTimer = NULL;
}
2012-02-05 18:16:38 +00:00
}
void ConfigTaskWidget::saveObjectToSD(UAVObject *obj)
{
// saveObjectToSD is now handled by the UAVUtils plugin in one
// central place (and one central queue)
ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
UAVObjectUtilManager *utilMngr = pm->getObject<UAVObjectUtilManager>();
2012-02-05 18:16:38 +00:00
utilMngr->saveObjectToSD(obj);
}
/**
* Util function to get a pointer to the object manager
* @return pointer to the UAVObjectManager
*/
UAVObjectManager *ConfigTaskWidget::getObjectManager()
{
2012-02-05 18:16:38 +00:00
ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
UAVObjectManager *objMngr = pm->getObject<UAVObjectManager>();
2012-02-05 18:16:38 +00:00
Q_ASSERT(objMngr);
return objMngr;
}
/**
* Utility function which calculates the Mean value of a list of values
* @param list list of double values
* @returns Mean value of the list of parameter values
*/
2012-02-05 18:16:38 +00:00
double ConfigTaskWidget::listMean(QList<double> list)
{
double accum = 0;
for (int i = 0; i < list.size(); i++) {
2012-02-05 18:16:38 +00:00
accum += list[i];
}
2012-02-05 18:16:38 +00:00
return accum / list.size();
}
/**
* Utility function which calculates the Variance value of a list of values
* @param list list of double values
* @returns Variance of the list of parameter values
*/
double ConfigTaskWidget::listVar(QList<double> list)
{
double mean_accum = 0;
double var_accum = 0;
double mean;
for (int i = 0; i < list.size(); i++) {
mean_accum += list[i];
}
mean = mean_accum / list.size();
for (int i = 0; i < list.size(); i++) {
var_accum += (list[i] - mean) * (list[i] - mean);
}
// Use unbiased estimator
return var_accum / (list.size() - 1);
}
2012-02-05 18:16:38 +00:00
// ************************************
// telemetry start/stop connect/disconnect signals
void ConfigTaskWidget::onAutopilotDisconnect()
{
m_isConnected = false;
2012-02-05 18:16:38 +00:00
enableControls(false);
invalidateObjects();
}
void ConfigTaskWidget::forceConnectedState() // dynamic widgets don't recieve the connected signal. This should be called instead.
{
m_isConnected = true;
setDirty(false);
}
2012-02-05 18:16:38 +00:00
void ConfigTaskWidget::onAutopilotConnect()
{
if (m_objectUtilManager) {
m_currentBoardId = m_objectUtilManager->getBoardModel(); // TODO REMEMBER TO ADD THIS TO FORCE CONNECTED FUNC ON CC3D_RELEASE
}
2012-02-05 18:16:38 +00:00
invalidateObjects();
m_isConnected = true;
foreach(WidgetBinding * binding, m_widgetBindings) {
loadWidgetLimits(binding->widget(), binding->field(), binding->index(), binding->isLimited(), binding->scale());
2012-08-01 14:42:21 +01:00
}
setDirty(false);
2012-02-05 18:16:38 +00:00
enableControls(true);
refreshWidgetsValues();
}
/**
* SLOT Function used to populate the widgets with the initial values
* Overwrite this if you need to change the default behavior
*/
2012-02-05 18:16:38 +00:00
void ConfigTaskWidget::populateWidgets()
{
bool dirtyBack = m_isDirty;
emit populateWidgetsRequested();
foreach(WidgetBinding * binding, m_widgetBindings) {
if (binding->object() != NULL && binding->field() != NULL && binding->widget() != NULL) {
setWidgetFromField(binding->widget(), binding->field(), binding->index(), binding->scale(), binding->isLimited());
2012-02-05 18:16:38 +00:00
}
}
setDirty(dirtyBack);
}
/**
* SLOT function used to refresh the widgets contents of the widgets with relation to
* object field added to the framework pool
* Overwrite this if you need to change the default behavior
*/
void ConfigTaskWidget::refreshWidgetsValues(UAVObject *obj)
2012-02-05 18:16:38 +00:00
{
if (!m_isWidgetUpdatesAllowed) {
return;
}
bool dirtyBack = m_isDirty;
emit refreshWidgetsValuesRequested();
foreach(WidgetBinding * binding, m_widgetBindings) {
if (binding->object() == obj && binding->field() != NULL && binding->widget() != NULL) {
setWidgetFromField(binding->widget(), binding->field(), binding->index(), binding->scale(), binding->isLimited());
2012-02-05 18:16:38 +00:00
}
}
setDirty(dirtyBack);
}
/**
* SLOT function used to update the uavobject fields from widgets with relation to
* object field added to the framework pool
* Overwrite this if you need to change the default behavior
*/
2012-02-05 18:16:38 +00:00
void ConfigTaskWidget::updateObjectsFromWidgets()
{
emit updateObjectsFromWidgetsRequested();
2012-02-05 18:16:38 +00:00
foreach(WidgetBinding * binding, m_widgetBindings) {
if (binding->object() != NULL && binding->field() != NULL) {
setFieldFromWidget(binding->widget(), binding->field(), binding->index(), binding->scale());
2012-02-05 18:16:38 +00:00
}
}
}
/**
* SLOT function used handle help button presses
* Overwrite this if you need to change the default behavior
*/
void ConfigTaskWidget::helpButtonPressed()
{
QString url = m_helpButtons.value((QPushButton *)sender(), QString());
if (!url.isEmpty()) {
QDesktopServices::openUrl(QUrl(url, QUrl::StrictMode));
}
}
/**
* Add update and save buttons to the form
* multiple buttons can be added for the same function
* @param update pointer to the update button
* @param save pointer to the save button
*/
2012-02-05 18:16:38 +00:00
void ConfigTaskWidget::addApplySaveButtons(QPushButton *update, QPushButton *save)
{
if (!m_saveButton) {
m_saveButton = new SmartSaveButton(this);
connect(m_saveButton, SIGNAL(preProcessOperations()), this, SLOT(updateObjectsFromWidgets()));
connect(m_saveButton, SIGNAL(saveSuccessfull()), this, SLOT(clearDirty()));
connect(m_saveButton, SIGNAL(beginOp()), this, SLOT(disableObjectUpdates()));
connect(m_saveButton, SIGNAL(endOp()), this, SLOT(enableObjectUpdates()));
}
if (update && save) {
m_saveButton->addButtons(save, update);
} else if (update) {
m_saveButton->addApplyButton(update);
} else if (save) {
m_saveButton->addSaveButton(save);
}
if (m_widgetBindings.count() > 0) {
foreach(WidgetBinding * binding, m_widgetBindings) {
m_saveButton->addObject((UAVDataObject *)binding->object());
2012-02-05 18:16:38 +00:00
}
}
updateEnableControls();
2012-02-05 18:16:38 +00:00
}
/**
* SLOT function used the enable or disable the SAVE, UPLOAD and RELOAD buttons
* @param enable set to true to enable the buttons or false to disable them
* @param field name of the field to add
*/
2012-02-05 18:16:38 +00:00
void ConfigTaskWidget::enableControls(bool enable)
{
if (m_saveButton) {
m_saveButton->enableControls(enable);
}
foreach(QPushButton * button, m_reloadButtons) {
button->setEnabled(enable);
}
foreach(WidgetBinding * binding, m_widgetBindings) {
if (binding->widget()) {
binding->widget()->setEnabled(enable);
foreach(ShadowWidgetBinding * shadow, binding->shadows()) {
shadow->widget()->setEnabled(enable);
}
}
}
2012-02-05 18:16:38 +00:00
}
bool ConfigTaskWidget::shouldObjectBeSaved(UAVObject *object)
{
Q_UNUSED(object);
return true;
}
/**
* SLOT function called when on of the widgets contents added to the framework changes
*/
void ConfigTaskWidget::forceShadowUpdates()
{
foreach(WidgetBinding * binding, m_widgetBindings) {
foreach(ShadowWidgetBinding * shadow, binding->shadows()) {
disconnectWidgetUpdatesToSlot(shadow->widget(), SLOT(widgetsContentsChanged()));
checkWidgetsLimits(shadow->widget(), binding->field(), binding->index(), shadow->isLimited(),
getVariantFromWidget(binding->widget(), binding->scale(), binding->units()), shadow->scale());
setWidgetFromVariant(shadow->widget(), getVariantFromWidget(binding->widget(), binding->scale(), binding->units()), shadow->scale());
emit widgetContentsChanged(shadow->widget());
connectWidgetUpdatesToSlot(shadow->widget(), SLOT(widgetsContentsChanged()));
}
}
setDirty(true);
}
/**
* SLOT function called when one of the widgets contents added to the framework changes
*/
2012-02-05 18:16:38 +00:00
void ConfigTaskWidget::widgetsContentsChanged()
{
QWidget *emitter = ((QWidget *)sender());
emit widgetContentsChanged(emitter);
double scale;
WidgetBinding *binding = m_shadowBindings.value(emitter, NULL);
if (binding) {
if (binding->widget() == emitter) {
scale = binding->scale();
checkWidgetsLimits(emitter, binding->field(), binding->index(), binding->isLimited(),
getVariantFromWidget(emitter, binding->scale(), binding->units()), binding->scale());
} else {
foreach(ShadowWidgetBinding * shadow, binding->shadows()) {
if (shadow->widget() == emitter) {
scale = shadow->scale();
checkWidgetsLimits(emitter, binding->field(), binding->index(), shadow->isLimited(),
getVariantFromWidget(emitter, scale, binding->units()), scale);
2012-02-05 18:16:38 +00:00
}
}
}
if (binding->widget() != emitter) {
disconnectWidgetUpdatesToSlot(binding->widget(), SLOT(widgetsContentsChanged()));
checkWidgetsLimits(binding->widget(), binding->field(), binding->index(), binding->isLimited(),
getVariantFromWidget(emitter, scale, binding->units()), binding->scale());
setWidgetFromVariant(binding->widget(), getVariantFromWidget(emitter, scale, binding->units()), binding->scale());
emit widgetContentsChanged(binding->widget());
connectWidgetUpdatesToSlot(binding->widget(), SLOT(widgetsContentsChanged()));
2012-02-05 18:16:38 +00:00
}
foreach(ShadowWidgetBinding * shadow, binding->shadows()) {
if (shadow->widget() != emitter) {
disconnectWidgetUpdatesToSlot(shadow->widget(), SLOT(widgetsContentsChanged()));
checkWidgetsLimits(shadow->widget(), binding->field(), binding->index(), shadow->isLimited(),
getVariantFromWidget(emitter, scale, binding->units()), shadow->scale());
setWidgetFromVariant(shadow->widget(), getVariantFromWidget(emitter, scale, binding->units()), shadow->scale());
emit widgetContentsChanged(shadow->widget());
connectWidgetUpdatesToSlot(shadow->widget(), SLOT(widgetsContentsChanged()));
2012-02-05 18:16:38 +00:00
}
}
}
if (m_saveButton) {
m_saveButton->resetIcons();
}
2012-02-05 18:16:38 +00:00
setDirty(true);
}
/**
* SLOT function used clear the forms dirty status flag
*/
2012-02-05 18:16:38 +00:00
void ConfigTaskWidget::clearDirty()
{
setDirty(false);
}
/**
* Sets the form's dirty status flag
* @param value
*/
2012-02-05 18:16:38 +00:00
void ConfigTaskWidget::setDirty(bool value)
{
m_isDirty = value;
2012-02-05 18:16:38 +00:00
}
/**
* Checks if the form is dirty (unsaved changes)
* @return true if the form has unsaved changes
*/
2012-02-05 18:16:38 +00:00
bool ConfigTaskWidget::isDirty()
{
if (m_isConnected) {
return m_isDirty;
} else {
2012-02-05 18:16:38 +00:00
return false;
}
2012-02-05 18:16:38 +00:00
}
/**
* SLOT function used to disable widget contents changes when related object field changes
*/
void ConfigTaskWidget::disableObjectUpdates()
2012-02-05 18:16:38 +00:00
{
m_isWidgetUpdatesAllowed = false;
foreach(WidgetBinding * binding, m_widgetBindings) {
if (binding->object()) {
disconnect(binding->object(), SIGNAL(objectUpdated(UAVObject *)), this, SLOT(refreshWidgetsValues(UAVObject *)));
}
2012-02-05 18:16:38 +00:00
}
}
/**
* SLOT function used to enable widget contents changes when related object field changes
*/
void ConfigTaskWidget::enableObjectUpdates()
2012-02-05 18:16:38 +00:00
{
m_isWidgetUpdatesAllowed = true;
foreach(WidgetBinding * binding, m_widgetBindings) {
if (binding->object()) {
connect(binding->object(), SIGNAL(objectUpdated(UAVObject *)), this, SLOT(refreshWidgetsValues(UAVObject *)), Qt::UniqueConnection);
}
2012-02-05 18:16:38 +00:00
}
}
/**
* Called when an uav object is updated
* @param obj pointer to the object whitch has just been updated
*/
void ConfigTaskWidget::objectUpdated(UAVObject *object)
2012-02-05 18:16:38 +00:00
{
m_updatedObjects[object] = true;
2012-02-05 18:16:38 +00:00
}
/**
* Checks if all objects added to the pool have already been updated
* @return true if all objects added to the pool have already been updated
*/
2012-02-05 18:16:38 +00:00
bool ConfigTaskWidget::allObjectsUpdated()
{
bool result = true;
foreach(UAVObject * object, m_updatedObjects.keys()) {
result = result & m_updatedObjects[object];
2012-02-05 18:16:38 +00:00
}
return result;
2012-02-05 18:16:38 +00:00
}
/**
* Adds a new help button
* @param button pointer to the help button
* @param url url to open in the browser when the help button is pressed
*/
void ConfigTaskWidget::addHelpButton(QPushButton *button, QString url)
{
m_helpButtons.insert(button, url);
connect(button, SIGNAL(clicked()), this, SLOT(helpButtonPressed()));
}
/**
* Invalidates all the uav objects "is updated" flag
*/
2012-02-05 18:16:38 +00:00
void ConfigTaskWidget::invalidateObjects()
{
foreach(UAVObject * obj, m_updatedObjects.keys()) {
m_updatedObjects[obj] = false;
2012-02-05 18:16:38 +00:00
}
}
/**
* SLOT call this to apply changes to uav objects
*/
void ConfigTaskWidget::apply()
{
if (m_saveButton) {
m_saveButton->apply();
}
}
/**
* SLOT call this to save changes to uav objects
*/
void ConfigTaskWidget::save()
{
if (m_saveButton) {
m_saveButton->save();
}
}
/**
* Adds a new shadow widget
* shadow widgets are widgets whitch have a relation to an object already present on the framework pool i.e. already added trough addUAVObjectToWidgetRelation
* This function doesn't have to be used directly, addUAVObjectToWidgetRelation will call it if a previous relation exhists.
* @return returns false if the shadow widget relation failed to be added (no previous relation exhisted)
*/
bool ConfigTaskWidget::addShadowWidgetBinding(QString object, QString field, QWidget *widget, int index, double scale, bool isLimited,
QList<int> *defaultReloadGroups, quint32 instID)
2012-02-05 18:16:38 +00:00
{
foreach(WidgetBinding * binding, m_widgetBindings) {
if (!binding->object() || !binding->widget() || !binding->field()) {
2012-02-05 18:16:38 +00:00
continue;
}
if (binding->object()->getName() == object &&
binding->field()->getName() == field &&
binding->index() == index &&
binding->object()->getInstID() == instID) {
binding->addShadow(widget, scale, isLimited);
m_shadowBindings.insert(widget, binding);
connectWidgetUpdatesToSlot(widget, SLOT(widgetsContentsChanged()));
if (defaultReloadGroups) {
2014-01-01 23:17:31 +01:00
addWidgetToReloadGroups(widget, defaultReloadGroups);
}
loadWidgetLimits(widget, binding->field(), binding->index(), isLimited, scale);
2012-02-05 18:16:38 +00:00
return true;
}
}
return false;
}
/**
* Auto loads widgets based on the Dynamic property named "objrelation"
* Check the wiki for more information
*/
2012-02-05 18:16:38 +00:00
void ConfigTaskWidget::autoLoadWidgets()
{
QPushButton *saveButtonWidget = NULL;
QPushButton *applyButtonWidget = NULL;
foreach(QWidget * widget, this->findChildren<QWidget *>()) {
QVariant info = widget->property("objrelation");
if (info.isValid()) {
2014-01-01 23:17:31 +01:00
bindingStruct uiRelation;
uiRelation.buttonType = none;
uiRelation.scale = 1;
uiRelation.element = QString();
uiRelation.haslimits = false;
foreach(QString str, info.toStringList()) {
QString prop = str.split(":").at(0);
QString value = str.split(":").at(1);
if (prop == "objname") {
uiRelation.objname = value;
} else if (prop == "fieldname") {
uiRelation.fieldname = value;
} else if (prop == "element") {
uiRelation.element = value;
} else if (prop == "scale") {
if (value == "null") {
uiRelation.scale = 1;
} else {
uiRelation.scale = value.toDouble();
}
} else if (prop == "haslimits") {
if (value == "yes") {
uiRelation.haslimits = true;
} else {
uiRelation.haslimits = false;
}
} else if (prop == "button") {
if (value == "save") {
uiRelation.buttonType = save_button;
} else if (value == "apply") {
uiRelation.buttonType = apply_button;
} else if (value == "reload") {
uiRelation.buttonType = reload_button;
} else if (value == "default") {
uiRelation.buttonType = default_button;
} else if (value == "help") {
uiRelation.buttonType = help_button;
}
} else if (prop == "buttongroup") {
foreach(QString s, value.split(",")) {
2012-02-05 18:16:38 +00:00
uiRelation.buttonGroup.append(s.toInt());
}
} else if (prop == "url") {
uiRelation.url = str.mid(str.indexOf(":") + 1);
2012-02-05 18:16:38 +00:00
}
}
if (!uiRelation.buttonType == none) {
QPushButton *button = NULL;
switch (uiRelation.buttonType) {
2012-02-05 18:16:38 +00:00
case save_button:
saveButtonWidget = qobject_cast<QPushButton *>(widget);
if (saveButtonWidget) {
addApplySaveButtons(NULL, saveButtonWidget);
}
2012-02-05 18:16:38 +00:00
break;
case apply_button:
applyButtonWidget = qobject_cast<QPushButton *>(widget);
if (applyButtonWidget) {
addApplySaveButtons(applyButtonWidget, NULL);
}
2012-02-05 18:16:38 +00:00
break;
case default_button:
button = qobject_cast<QPushButton *>(widget);
if (button) {
addDefaultButton(button, uiRelation.buttonGroup.at(0));
}
2012-02-05 18:16:38 +00:00
break;
case reload_button:
button = qobject_cast<QPushButton *>(widget);
if (button) {
addReloadButton(button, uiRelation.buttonGroup.at(0));
}
2012-02-05 18:16:38 +00:00
break;
case help_button:
button = qobject_cast<QPushButton *>(widget);
if (button) {
addHelpButton(button, uiRelation.url);
}
break;
2012-02-05 18:16:38 +00:00
default:
break;
}
} else {
QWidget *wid = qobject_cast<QWidget *>(widget);
if (wid) {
addWidgetBinding(uiRelation.objname, uiRelation.fieldname, wid, uiRelation.element, uiRelation.scale, uiRelation.haslimits, &uiRelation.buttonGroup);
}
2012-02-05 18:16:38 +00:00
}
}
}
refreshWidgetsValues();
forceShadowUpdates();
foreach(WidgetBinding *binding, m_widgetBindings) {
if (binding->widget()) {
qDebug() << "Binding:" << binding->widget()->objectName();
}
foreach(ShadowWidgetBinding *shadow, binding->shadows()) {
if (shadow->widget()) {
qDebug() << " Shadow" << shadow->widget()->objectName();
}
}
}
2012-02-05 18:16:38 +00:00
}
/**
* Adds a widget to a list of default/reload groups
* default/reload groups are groups of widgets to be set with default or reloaded (values from persistent memory) when a defined button is pressed
* @param widget pointer to the widget to be added to the groups
* @param groups list of the groups on which to add the widget
*/
2014-01-01 23:17:31 +01:00
void ConfigTaskWidget::addWidgetToReloadGroups(QWidget *widget, QList<int> *groups)
2012-02-05 18:16:38 +00:00
{
foreach(WidgetBinding *binding, m_widgetBindings) {
bool addBinding = false;
if (binding->widget() == widget) {
addBinding = true;
} else {
foreach(ShadowWidgetBinding *shadow, binding->shadows()) {
if (shadow->widget() == widget) {
addBinding = true;
}
2012-02-05 18:16:38 +00:00
}
}
if (addBinding) {
foreach(int i, *groups) {
if (m_reloadGroups.contains(i)) {
m_reloadGroups.value(i)->append(binding);
} else {
m_reloadGroups.insert(i, new QList<WidgetBinding *>());
m_reloadGroups.value(i)->append(binding);
2012-02-05 18:16:38 +00:00
}
}
}
}
}
/**
* Adds a button to a default group
* @param button pointer to the default button
* @param buttongroup number of the group
*/
2012-02-05 18:16:38 +00:00
void ConfigTaskWidget::addDefaultButton(QPushButton *button, int buttonGroup)
{
button->setProperty("group", buttonGroup);
connect(button, SIGNAL(clicked()), this, SLOT(defaultButtonClicked()));
2012-02-05 18:16:38 +00:00
}
/**
* Adds a button to a reload group
* @param button pointer to the reload button
* @param buttongroup number of the group
*/
2012-02-05 18:16:38 +00:00
void ConfigTaskWidget::addReloadButton(QPushButton *button, int buttonGroup)
{
button->setProperty("group", buttonGroup);
m_reloadButtons.append(button);
connect(button, SIGNAL(clicked()), this, SLOT(reloadButtonClicked()));
2012-02-05 18:16:38 +00:00
}
/**
* Called when a default button is clicked
*/
2012-02-05 18:16:38 +00:00
void ConfigTaskWidget::defaultButtonClicked()
{
int group = sender()->property("group").toInt();
emit defaultRequested(group);
QList<WidgetBinding *> *bindings = m_reloadGroups.value(group);
foreach(WidgetBinding * binding, *bindings) {
if (!binding->object() || !binding->field()) {
2012-02-05 18:16:38 +00:00
continue;
}
UAVDataObject *temp = ((UAVDataObject *)binding->object())->dirtyClone();
setWidgetFromField(binding->widget(), temp->getField(binding->field()->getName()), binding->index(), binding->scale(), binding->isLimited());
2012-02-05 18:16:38 +00:00
}
}
/**
* Called when a reload button is clicked
*/
2012-02-05 18:16:38 +00:00
void ConfigTaskWidget::reloadButtonClicked()
{
if (m_realtimeUpdateTimer) {
return;
}
int group = sender()->property("group").toInt();
QList<WidgetBinding *> *bindings = m_reloadGroups.value(group, NULL);
if (!bindings) {
return;
}
ObjectPersistence *objper = dynamic_cast<ObjectPersistence *>(getObjectManager()->getObject(ObjectPersistence::NAME));
m_realtimeUpdateTimer = new QTimer(this);
QEventLoop *eventLoop = new QEventLoop(this);
connect(m_realtimeUpdateTimer, SIGNAL(timeout()), eventLoop, SLOT(quit()));
connect(objper, SIGNAL(objectUpdated(UAVObject *)), eventLoop, SLOT(quit()));
2014-01-01 23:17:31 +01:00
QList<objectComparator> temp;
foreach(WidgetBinding *binding, *bindings) {
if (binding->object() != NULL) {
2014-01-01 23:17:31 +01:00
objectComparator value;
value.objid = binding->object()->getObjID();
value.objinstid = binding->object()->getInstID();
if (temp.contains(value)) {
continue;
} else {
temp.append(value);
}
2012-02-05 18:16:38 +00:00
ObjectPersistence::DataFields data;
data.Operation = ObjectPersistence::OPERATION_LOAD;
data.Selection = ObjectPersistence::SELECTION_SINGLEOBJECT;
data.ObjectID = binding->object()->getObjID();
data.InstanceID = binding->object()->getInstID();
2012-02-05 18:16:38 +00:00
objper->setData(data);
objper->updated();
m_realtimeUpdateTimer->start(500);
2012-02-05 18:16:38 +00:00
eventLoop->exec();
if (m_realtimeUpdateTimer->isActive()) {
binding->object()->requestUpdate();
if (binding->widget()) {
setWidgetFromField(binding->widget(), binding->field(), binding->index(), binding->scale(), binding->isLimited());
}
2012-02-05 18:16:38 +00:00
}
m_realtimeUpdateTimer->stop();
2012-02-05 18:16:38 +00:00
}
}
if (eventLoop) {
delete eventLoop;
eventLoop = NULL;
}
if (m_realtimeUpdateTimer) {
delete m_realtimeUpdateTimer;
m_realtimeUpdateTimer = NULL;
}
2012-02-05 18:16:38 +00:00
}
/**
* Connects widgets "contents changed" signals to a slot
*/
void ConfigTaskWidget::connectWidgetUpdatesToSlot(QWidget *widget, const char *function)
2012-02-05 18:16:38 +00:00
{
if (!widget) {
2012-02-05 18:16:38 +00:00
return;
}
if (QComboBox * cb = qobject_cast<QComboBox *>(widget)) {
connect(cb, SIGNAL(currentIndexChanged(int)), this, function);
} else if (QSlider * cb = qobject_cast<QSlider *>(widget)) {
connect(cb, SIGNAL(valueChanged(int)), this, function);
} else if (MixerCurveWidget * cb = qobject_cast<MixerCurveWidget *>(widget)) {
connect(cb, SIGNAL(curveUpdated()), this, function);
} else if (QTableWidget * cb = qobject_cast<QTableWidget *>(widget)) {
connect(cb, SIGNAL(cellChanged(int, int)), this, function);
} else if (QSpinBox * cb = qobject_cast<QSpinBox *>(widget)) {
connect(cb, SIGNAL(valueChanged(int)), this, function);
} else if (QDoubleSpinBox * cb = qobject_cast<QDoubleSpinBox *>(widget)) {
connect(cb, SIGNAL(valueChanged(double)), this, function);
} else if (QCheckBox * cb = qobject_cast<QCheckBox *>(widget)) {
connect(cb, SIGNAL(stateChanged(int)), this, function);
} else if (QPushButton * cb = qobject_cast<QPushButton *>(widget)) {
connect(cb, SIGNAL(clicked()), this, function);
} else {
2014-01-01 23:03:20 +01:00
qDebug() << __FUNCTION__ << "widget binding not implemented" << widget->metaObject()->className();
2012-02-05 18:16:38 +00:00
}
}
/**
* Disconnects widgets "contents changed" signals to a slot
*/
void ConfigTaskWidget::disconnectWidgetUpdatesToSlot(QWidget *widget, const char *function)
{
if (!widget) {
return;
}
if (QComboBox * cb = qobject_cast<QComboBox *>(widget)) {
disconnect(cb, SIGNAL(currentIndexChanged(int)), this, function);
} else if (QSlider * cb = qobject_cast<QSlider *>(widget)) {
disconnect(cb, SIGNAL(valueChanged(int)), this, function);
} else if (MixerCurveWidget * cb = qobject_cast<MixerCurveWidget *>(widget)) {
disconnect(cb, SIGNAL(curveUpdated()), this, function);
} else if (QTableWidget * cb = qobject_cast<QTableWidget *>(widget)) {
disconnect(cb, SIGNAL(cellChanged(int, int)), this, function);
} else if (QSpinBox * cb = qobject_cast<QSpinBox *>(widget)) {
disconnect(cb, SIGNAL(valueChanged(int)), this, function);
} else if (QDoubleSpinBox * cb = qobject_cast<QDoubleSpinBox *>(widget)) {
disconnect(cb, SIGNAL(valueChanged(double)), this, function);
} else if (QCheckBox * cb = qobject_cast<QCheckBox *>(widget)) {
disconnect(cb, SIGNAL(stateChanged(int)), this, function);
} else if (QPushButton * cb = qobject_cast<QPushButton *>(widget)) {
disconnect(cb, SIGNAL(clicked()), this, function);
} else {
2014-01-01 23:03:20 +01:00
qDebug() << __FUNCTION__ << "widget binding not implemented" << widget->metaObject()->className();
}
}
/**
* Sets a widget value from an UAVObject field
* @param widget pointer for the widget to set
* @param field pointer to the UAVObject field to use
* @param index index of the element to use
* @param scale scale to be used on the assignement
* @return returns true if the assignement was successfull
*/
bool ConfigTaskWidget::setFieldFromWidget(QWidget *widget, UAVObjectField *field, int index, double scale)
2012-02-05 18:16:38 +00:00
{
if (!widget || !field) {
2012-02-05 18:16:38 +00:00
return false;
}
QVariant ret = getVariantFromWidget(widget, scale, field->getUnits());
if (ret.isValid()) {
field->setValue(ret, index);
2012-02-05 18:16:38 +00:00
return true;
}
{
qDebug() << __FUNCTION__ << "widget to uavobject relation not implemented" << widget->metaObject()->className();
2012-02-05 18:16:38 +00:00
return false;
}
}
/**
* Gets a variant from a widget
* @param widget pointer to the widget from where to get the value
* @param scale scale to be used on the assignement
* @return returns the value of the widget times the scale
*/
QVariant ConfigTaskWidget::getVariantFromWidget(QWidget *widget, double scale, QString units)
2012-02-05 18:16:38 +00:00
{
if (QComboBox * cb = qobject_cast<QComboBox *>(widget)) {
2012-02-05 18:16:38 +00:00
return (QString)cb->currentText();
} else if (QDoubleSpinBox * cb = qobject_cast<QDoubleSpinBox *>(widget)) {
return (double)(cb->value() * scale);
} else if (QSpinBox * cb = qobject_cast<QSpinBox *>(widget)) {
return (double)(cb->value() * scale);
} else if (QSlider * cb = qobject_cast<QSlider *>(widget)) {
return (double)(cb->value() * scale);
} else if (QCheckBox * cb = qobject_cast<QCheckBox *>(widget)) {
return (QString)(cb->isChecked() ? "TRUE" : "FALSE");
} else if (QLineEdit * cb = qobject_cast<QLineEdit *>(widget)) {
QString value = (QString)cb->displayText();
if (units == "hex") {
bool ok;
return value.toUInt(&ok, 16);
} else {
return value;
}
} else {
2012-02-05 18:16:38 +00:00
return QVariant();
}
2012-02-05 18:16:38 +00:00
}
/**
* Sets a widget from a variant
* @param widget pointer for the widget to set
* @param value value to be used on the assignement
* @param scale scale to be used on the assignement
* @param units the units for the value
* @return returns true if the assignement was successfull
*/
bool ConfigTaskWidget::setWidgetFromVariant(QWidget *widget, QVariant value, double scale, QString units)
2012-02-05 18:16:38 +00:00
{
if (QComboBox * cb = qobject_cast<QComboBox *>(widget)) {
2012-02-05 18:16:38 +00:00
cb->setCurrentIndex(cb->findText(value.toString()));
return true;
} else if (QLabel * cb = qobject_cast<QLabel *>(widget)) {
if (scale == 0) {
cb->setText(value.toString());
} else {
cb->setText(QString::number((value.toDouble() / scale)));
}
2012-02-05 18:16:38 +00:00
return true;
} else if (QDoubleSpinBox * cb = qobject_cast<QDoubleSpinBox *>(widget)) {
cb->setValue((double)(value.toDouble() / scale));
2012-02-05 18:16:38 +00:00
return true;
} else if (QSpinBox * cb = qobject_cast<QSpinBox *>(widget)) {
cb->setValue((int)qRound(value.toDouble() / scale));
2012-02-05 18:16:38 +00:00
return true;
} else if (QSlider * cb = qobject_cast<QSlider *>(widget)) {
cb->setValue((int)qRound(value.toDouble() / scale));
2012-02-05 18:16:38 +00:00
return true;
} else if (QCheckBox * cb = qobject_cast<QCheckBox *>(widget)) {
bool bvalue = value.toString() == "TRUE";
cb->setChecked(bvalue);
2012-02-05 18:16:38 +00:00
return true;
} else if (QLineEdit * cb = qobject_cast<QLineEdit *>(widget)) {
if ((scale == 0) || (scale == 1)) {
if (units == "hex") {
cb->setText(QString::number(value.toUInt(), 16).toUpper());
} else {
cb->setText(value.toString());
}
2013-04-11 14:40:52 +01:00
} else {
cb->setText(QString::number((value.toDouble() / scale)));
2013-04-11 14:40:52 +01:00
}
return true;
} else {
2012-02-05 18:16:38 +00:00
return false;
}
2012-02-05 18:16:38 +00:00
}
2013-04-11 14:40:52 +01:00
/**
* Sets a widget from a variant
* @param widget pointer for the widget to set
* @param value value to be used on the assignement
* @param scale scale to be used on the assignement
* @return returns true if the assignement was successfull
*/
bool ConfigTaskWidget::setWidgetFromVariant(QWidget *widget, QVariant value, double scale)
{
return setWidgetFromVariant(widget, value, scale, QString(""));
}
2013-04-11 14:40:52 +01:00
/**
* Sets a widget from a UAVObject field
* @param widget pointer to the widget to set
* @param field pointer to the field from where to get the value from
* @param index index of the element to use
* @param scale scale to be used on the assignement
* @param hasLimits set to true if you want to limit the values (check wiki)
* @return returns true if the assignement was successfull
*/
bool ConfigTaskWidget::setWidgetFromField(QWidget *widget, UAVObjectField *field, int index, double scale, bool hasLimits)
2012-02-05 18:16:38 +00:00
{
if (!widget || !field) {
2012-02-05 18:16:38 +00:00
return false;
}
if (QComboBox * cb = qobject_cast<QComboBox *>(widget)) {
if (cb->count() == 0) {
loadWidgetLimits(cb, field, index, hasLimits, scale);
}
}
QVariant var = field->getValue(index);
checkWidgetsLimits(widget, field, index, hasLimits, var, scale);
bool ret = setWidgetFromVariant(widget, var, scale, field->getUnits());
if (ret) {
2012-02-05 18:16:38 +00:00
return true;
} else {
qDebug() << __FUNCTION__ << "widget to uavobject relation not implemented" << widget->metaObject()->className();
2012-02-05 18:16:38 +00:00
return false;
}
}
void ConfigTaskWidget::checkWidgetsLimits(QWidget *widget, UAVObjectField *field, int index, bool hasLimits, QVariant value, double scale)
2012-02-05 18:16:38 +00:00
{
if (!hasLimits) {
2012-02-05 18:16:38 +00:00
return;
}
if (!field->isWithinLimits(value, index, m_currentBoardId)) {
if (!widget->property("styleBackup").isValid()) {
widget->setProperty("styleBackup", widget->styleSheet());
}
widget->setStyleSheet(m_outOfLimitsStyle);
widget->setProperty("wasOverLimits", (bool)true);
if (QComboBox * cb = qobject_cast<QComboBox *>(widget)) {
if (cb->findText(value.toString()) == -1) {
2012-02-05 18:16:38 +00:00
cb->addItem(value.toString());
}
} else if (QDoubleSpinBox * cb = qobject_cast<QDoubleSpinBox *>(widget)) {
if ((double)(value.toDouble() / scale) > cb->maximum()) {
cb->setMaximum((double)(value.toDouble() / scale));
} else if ((double)(value.toDouble() / scale) < cb->minimum()) {
cb->setMinimum((double)(value.toDouble() / scale));
2012-02-05 18:16:38 +00:00
}
} else if (QSpinBox * cb = qobject_cast<QSpinBox *>(widget)) {
if ((int)qRound(value.toDouble() / scale) > cb->maximum()) {
cb->setMaximum((int)qRound(value.toDouble() / scale));
} else if ((int)qRound(value.toDouble() / scale) < cb->minimum()) {
cb->setMinimum((int)qRound(value.toDouble() / scale));
2012-02-05 18:16:38 +00:00
}
} else if (QSlider * cb = qobject_cast<QSlider *>(widget)) {
if ((int)qRound(value.toDouble() / scale) > cb->maximum()) {
cb->setMaximum((int)qRound(value.toDouble() / scale));
} else if ((int)qRound(value.toDouble() / scale) < cb->minimum()) {
cb->setMinimum((int)qRound(value.toDouble() / scale));
2012-02-05 18:16:38 +00:00
}
}
} else if (widget->property("wasOverLimits").isValid()) {
if (widget->property("wasOverLimits").toBool()) {
widget->setProperty("wasOverLimits", (bool)false);
if (widget->property("styleBackup").isValid()) {
QString style = widget->property("styleBackup").toString();
2012-02-05 18:16:38 +00:00
widget->setStyleSheet(style);
}
loadWidgetLimits(widget, field, index, hasLimits, scale);
2012-02-05 18:16:38 +00:00
}
}
}
void ConfigTaskWidget::loadWidgetLimits(QWidget *widget, UAVObjectField *field, int index, bool hasLimits, double scale)
{
if (!widget || !field) {
2012-02-05 18:16:38 +00:00
return;
}
if (QComboBox * cb = qobject_cast<QComboBox *>(widget)) {
2012-02-05 18:16:38 +00:00
cb->clear();
QStringList option = field->getOptions();
if (hasLimits) {
foreach(QString str, option) {
if (field->isWithinLimits(str, index, m_currentBoardId)) {
2012-02-05 18:16:38 +00:00
cb->addItem(str);
}
2012-02-05 18:16:38 +00:00
}
} else {
2012-02-05 18:16:38 +00:00
cb->addItems(option);
}
2012-02-05 18:16:38 +00:00
}
if (!hasLimits) {
2012-02-05 18:16:38 +00:00
return;
} else if (QDoubleSpinBox * cb = qobject_cast<QDoubleSpinBox *>(widget)) {
if (field->getMaxLimit(index).isValid()) {
cb->setMaximum((double)(field->getMaxLimit(index, m_currentBoardId).toDouble() / scale));
2012-02-05 18:16:38 +00:00
}
if (field->getMinLimit(index, m_currentBoardId).isValid()) {
cb->setMinimum((double)(field->getMinLimit(index, m_currentBoardId).toDouble() / scale));
2012-02-05 18:16:38 +00:00
}
} else if (QSpinBox * cb = qobject_cast<QSpinBox *>(widget)) {
if (field->getMaxLimit(index, m_currentBoardId).isValid()) {
cb->setMaximum((int)qRound(field->getMaxLimit(index, m_currentBoardId).toDouble() / scale));
2012-02-05 18:16:38 +00:00
}
if (field->getMinLimit(index, m_currentBoardId).isValid()) {
cb->setMinimum((int)qRound(field->getMinLimit(index, m_currentBoardId).toDouble() / scale));
2012-02-05 18:16:38 +00:00
}
} else if (QSlider * cb = qobject_cast<QSlider *>(widget)) {
if (field->getMaxLimit(index, m_currentBoardId).isValid()) {
cb->setMaximum((int)qRound(field->getMaxLimit(index, m_currentBoardId).toDouble() / scale));
2012-02-05 18:16:38 +00:00
}
if (field->getMinLimit(index, m_currentBoardId).isValid()) {
cb->setMinimum((int)(field->getMinLimit(index, m_currentBoardId).toDouble() / scale));
2012-02-05 18:16:38 +00:00
}
}
}
UAVObject *ConfigTaskWidget::getObject(const QString name, quint32 instId)
{
return m_pluginManager->getObject<UAVObjectManager>()->getObject(name, instId);
}
void ConfigTaskWidget::updateEnableControls()
{
TelemetryManager *telMngr = m_pluginManager->getObject<TelemetryManager>();
Q_ASSERT(telMngr);
enableControls(telMngr->isConnected());
}
void ConfigTaskWidget::disableMouseWheelEvents()
{
// Disable mouse wheel events
foreach(QSpinBox * sp, findChildren<QSpinBox *>()) {
sp->installEventFilter(this);
}
foreach(QDoubleSpinBox * sp, findChildren<QDoubleSpinBox *>()) {
sp->installEventFilter(this);
}
foreach(QSlider * sp, findChildren<QSlider *>()) {
sp->installEventFilter(this);
}
foreach(QComboBox * sp, findChildren<QComboBox *>()) {
sp->installEventFilter(this);
}
}
bool ConfigTaskWidget::eventFilter(QObject *obj, QEvent *evt)
{
// Filter all wheel events, and ignore them
if (evt->type() == QEvent::Wheel &&
(qobject_cast<QAbstractSpinBox *>(obj) ||
qobject_cast<QComboBox *>(obj) ||
qobject_cast<QAbstractSlider *>(obj))) {
evt->ignore();
return true;
}
return QWidget::eventFilter(obj, evt);
}
WidgetBinding::WidgetBinding(QWidget *widget, UAVObject *object, UAVObjectField *field, int index, double scale, bool isLimited) :
ShadowWidgetBinding(widget, scale, isLimited)
{
m_object = object;
m_field = field;
m_index = index;
}
WidgetBinding::~WidgetBinding()
{
}
QString WidgetBinding::units() const
{
if(m_field) {
return m_field->getUnits();
}
return QString("");
}
UAVObject *WidgetBinding::object() const
{
return m_object;
}
UAVObjectField *WidgetBinding::field() const
{
return m_field;
}
int WidgetBinding::index() const
{
return m_index;
}
QList<ShadowWidgetBinding *> WidgetBinding::shadows() const
{
return m_shadows;
}
void WidgetBinding::addShadow(QWidget *widget, int scale, bool isLimited)
{
ShadowWidgetBinding *shadow = NULL;
// Prefer anything else to QLabel and prefer QDoubleSpinBox to anything else
if ((qobject_cast<QLabel *>(m_widget) && !qobject_cast<QLabel *>(widget)) ||
(!qobject_cast<QDoubleSpinBox *>(m_widget) && qobject_cast<QDoubleSpinBox *>(widget))) {
shadow = new ShadowWidgetBinding(m_widget, m_scale, m_isLimited);
m_isLimited = isLimited;
m_scale = scale;
m_widget = widget;
} else {
shadow = new ShadowWidgetBinding(widget, scale, isLimited);
}
m_shadows.append(shadow);
}
ShadowWidgetBinding::ShadowWidgetBinding(QWidget *widget, double scale, bool isLimited)
{
m_widget = widget;
m_scale = scale;
m_isLimited = isLimited;
}
ShadowWidgetBinding::~ShadowWidgetBinding()
{
}
QWidget *ShadowWidgetBinding::widget() const
{
return m_widget;
}
double ShadowWidgetBinding::scale() const
{
return m_scale;
}
bool ShadowWidgetBinding::isLimited() const
{
return m_isLimited;
}