Merged in f5soh/librepilot/update_credits (pull request #529)
[librepilot.git] / ground / gcs / src / plugins / uavobjectwidgetutils / configtaskwidget.h
blob5494d46264842e89a2e432347937a4961a2e084e
1 /**
2 ******************************************************************************
4 * @file configtaskwidget.h
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * @addtogroup GCSPlugins GCS Plugins
7 * @{
8 * @addtogroup UAVObjectWidgetUtils Plugin
9 * @{
10 * @brief Utility plugin for UAVObject to Widget relation management
11 *****************************************************************************/
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #ifndef CONFIGTASKWIDGET_H
28 #define CONFIGTASKWIDGET_H
30 #include "uavobjectwidgetutils_global.h"
32 #include <QWidget>
33 #include <QList>
34 #include <QVariant>
36 namespace ExtensionSystem {
37 class PluginManager;
39 class UAVObject;
40 class UAVObjectField;
41 class UAVObjectManager;
42 class UAVObjectUtilManager;
43 class SmartSaveButton;
45 class QComboBox;
46 class QPushButton;
47 class QEvent;
49 class ShadowWidgetBinding : public QObject {
50 Q_OBJECT
51 public:
52 ShadowWidgetBinding(QWidget *widget, double scale, bool isLimited);
53 ~ShadowWidgetBinding();
54 QWidget *widget() const;
55 double scale() const;
56 bool isLimited() const;
58 protected:
59 QWidget *m_widget;
60 double m_scale;
61 bool m_isLimited;
64 class WidgetBinding : public ShadowWidgetBinding {
65 Q_OBJECT
66 public:
67 WidgetBinding(QWidget *widget, UAVObject *object, UAVObjectField *field, int index, double scale, bool isLimited);
68 ~WidgetBinding();
70 QString units() const;
71 QString type() const;
72 bool isInteger() const;
73 UAVObject *object() const;
74 UAVObjectField *field() const;
75 int index() const;
76 QList<ShadowWidgetBinding *> shadows() const;
78 void addShadow(QWidget *widget, double scale, bool isLimited);
79 bool matches(QString objectName, QString fieldName, int index, quint32 instanceId);
81 bool isEnabled() const;
82 void setIsEnabled(bool isEnabled);
84 QVariant value() const;
85 void setValue(const QVariant &value);
87 void updateObjectFieldFromValue();
88 void updateValueFromObjectField();
90 private:
91 UAVObject *m_object;
92 UAVObjectField *m_field;
93 int m_index;
94 bool m_isEnabled;
95 QList<ShadowWidgetBinding *> m_shadows;
96 QVariant m_value;
99 class UAVOBJECTWIDGETUTILS_EXPORT ConfigTaskWidget : public QWidget {
100 Q_OBJECT
102 public:
103 enum ConfigTaskType { AutoPilot, OPLink, Child };
105 ConfigTaskWidget(QWidget *parent = 0, ConfigTaskType configType = AutoPilot);
106 virtual ~ConfigTaskWidget();
108 void bind();
110 bool isDirty();
111 void setDirty(bool value);
112 void clearDirty();
114 virtual bool shouldObjectBeSaved(UAVObject *object);
116 protected:
117 // Combobox helper functions
118 static bool isComboboxOptionSelected(QComboBox *combo, int optionValue);
119 static int getComboboxSelectedOption(QComboBox *combo);
120 static void setComboboxSelectedOption(QComboBox *combo, int optionValue);
121 static int getComboboxIndexForOption(QComboBox *combo, int optionValue);
122 static void enableComboBoxOptionItem(QComboBox *combo, int optionValue, bool enable);
124 void disableMouseWheelEvents();
125 bool eventFilter(QObject *obj, QEvent *evt);
127 UAVObjectManager *getObjectManager();
129 void addUAVObject(QString objectName, QList<int> *reloadGroups = NULL);
130 void addUAVObject(UAVObject *objectName, QList<int> *reloadGroups = NULL);
132 // TODO should be protected (see VehicleConfig::registerWidgets(ConfigTaskWidget &parent)
133 public:
134 void addWidget(QWidget *widget);
136 void addWidgetBinding(QString objectName, QString fieldName, QWidget *widget, int index = 0, double scale = 1,
137 bool isLimited = false, QList<int> *reloadGroupIDs = 0, quint32 instID = 0);
138 void addWidgetBinding(UAVObject *object, UAVObjectField *field, QWidget *widget, int index = 0, double scale = 1,
139 bool isLimited = false, QList<int> *reloadGroupIDs = 0, quint32 instID = 0);
141 protected:
142 void addAutoBindings();
144 void addWidgetBinding(QString objectName, QString fieldName, QWidget *widget, QString elementName, double scale,
145 bool isLimited = false, QList<int> *reloadGroupIDs = 0, quint32 instID = 0);
146 void addWidgetBinding(UAVObject *object, UAVObjectField *field, QWidget *widget, QString elementName, double scale,
147 bool isLimited = false, QList<int> *reloadGroupIDs = 0, quint32 instID = 0);
149 void addWidgetBinding(QString objectName, QString fieldName, QWidget *widget, QString elementName);
150 void addWidgetBinding(UAVObject *object, UAVObjectField *field, QWidget *widget, QString elementName);
152 void addWidgetToReloadGroups(QWidget *widget, QList<int> *reloadGroupIDs);
154 bool addShadowWidgetBinding(QString objectName, QString fieldName, QWidget *widget, int index = 0, double scale = 1,
155 bool isLimited = false, QList<int> *m_reloadGroups = NULL, quint32 instID = 0);
157 bool allObjectsUpdated();
158 void setOutOfLimitsStyle(QString style)
160 m_outOfLimitsStyle = style;
162 void addHelpButton(QPushButton *button, QString url);
163 void setWikiURL(QString url);
165 protected slots:
166 void apply();
167 void save();
169 signals:
170 void connected();
171 void disconnected();
172 void widgetContentsChanged(QWidget *widget);
173 void defaultRequested(int group);
174 void enableControlsChanged(bool enable);
176 protected:
177 int boardModel() const
179 return m_currentBoardId;
181 bool expertMode() const;
182 virtual QString mapObjectName(const QString objectName);
183 virtual UAVObject *getObject(const QString name, quint32 instId = 0);
184 virtual void buildOptionComboBox(QComboBox *combo, UAVObjectField *field, int index, bool applyLimits);
186 virtual void enableControls(bool enable);
187 virtual void refreshWidgetsValuesImpl(UAVObject *) {};
188 virtual void updateObjectsFromWidgetsImpl() {};
190 bool isConnected() const;
191 void updateEnableControls();
193 protected slots:
194 void setWidgetBindingObjectEnabled(QString objectName, bool enabled);
196 virtual void widgetsContentsChanged();
197 void refreshWidgetsValues(UAVObject *obj = NULL);
198 void updateObjectsFromWidgets();
200 private slots:
201 void onConnect();
202 void onDisconnect();
204 void disableObjectUpdates();
205 void enableObjectUpdates();
206 void objectUpdated(UAVObject *object);
207 void invalidateObjects();
209 void saveSuccessful();
211 void defaultButtonClicked();
212 void reloadButtonClicked();
213 void helpButtonPressed();
215 private:
216 struct ObjectComparator {
217 quint32 objid;
218 quint32 objinstid;
219 bool operator==(const ObjectComparator & lhs)
221 return lhs.objid == this->objid && lhs.objinstid == this->objinstid;
225 enum ButtonTypeEnum { None, SaveButton, ApplyButton, ReloadButton, DefaultButton, HelpButton };
226 struct BindingStruct {
227 QString objectName;
228 QString fieldName;
229 QString elementName;
230 int index;
231 QString url;
232 ButtonTypeEnum buttonType;
233 QList<int> buttonGroup;
234 double scale;
235 bool haslimits;
238 // indicates if this is an "autopilot" widget (CC3D, Revolution, ...), an OPLink widget or a Child widget (for vehicle config)
239 // TODO the logic that this flag controls should be moved to derived classes
240 ConfigTaskType m_configType;
242 // only valid for "autopilot" widgets
243 int m_currentBoardId;
245 bool m_isConnected;
246 bool m_isWidgetUpdatesAllowed;
247 bool m_isDirty;
248 bool m_refreshing;
250 QStringList m_objects;
252 // Wiki address for help button (will be concatenated with WIKI_URL_ROOT)
253 QString m_wikiURL;
255 QMultiHash<int, WidgetBinding *> m_reloadGroups;
256 QMultiHash<QWidget *, WidgetBinding *> m_widgetBindingsPerWidget;
257 QMultiHash<UAVObject *, WidgetBinding *> m_widgetBindingsPerObject;
259 ExtensionSystem::PluginManager *m_pluginManager;
260 UAVObjectUtilManager *m_objectUtilManager;
262 QHash<UAVObject *, bool> m_updatedObjects;
264 SmartSaveButton *m_saveButton;
265 QHash<QPushButton *, QString> m_helpButtons;
266 QList<QPushButton *> m_reloadButtons;
268 QString m_outOfLimitsStyle;
269 QTimer *m_realtimeUpdateTimer;
271 bool setWidgetFromField(QWidget *widget, UAVObjectField *field, WidgetBinding *binding);
273 QVariant getVariantFromWidget(QWidget *widget, WidgetBinding *binding);
274 bool setWidgetFromVariant(QWidget *widget, QVariant value, WidgetBinding *binding);
276 void connectWidgetUpdatesToSlot(QWidget *widget, const char *function);
277 void disconnectWidgetUpdatesToSlot(QWidget *widget, const char *function);
279 void resetLimits();
281 void loadWidgetLimits(QWidget *widget, UAVObjectField *field, int index, bool applyLimits, double scale);
283 void checkWidgetsLimits(QWidget *widget, UAVObjectField *field, int index, bool hasLimits, QVariant value, double scale);
285 void dumpBindings();
287 int fieldIndexFromElementName(QString objectName, QString fieldName, QString elementName);
289 void doAddWidgetBinding(QString objectName, QString fieldName, QWidget *widget, int index = 0, double scale = 1,
290 bool isLimited = false, QList<int> *reloadGroupIDs = 0, quint32 instID = 0);
292 void addApplyButton(QPushButton *button);
293 void addSaveButton(QPushButton *button);
294 void addReloadButton(QPushButton *button, int buttonGroup);
295 void addDefaultButton(QPushButton *button, int buttonGroup);
298 #endif // CONFIGTASKWIDGET_H