31 #include <QFileDialog>
35 : QGraphicsView(parent)
37 setMinimumSize(32, 32);
38 setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
39 setScene(
new QGraphicsScene(
this));
40 setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
41 m_renderer =
new QSvgRenderer();
55 connect(&dialTimer, SIGNAL(timeout()),
this, SLOT(moveIndex()));
78 if (!(object1.isEmpty() || nfield1.isEmpty())) {
82 if (nfield1.contains(
"-")) {
83 QStringList fieldSubfield = nfield1.split(
"-", QString::SkipEmptyParts);
84 field1 = fieldSubfield.at(0);
85 subfield1 = fieldSubfield.at(1);
89 haveSubField1 =
false;
92 fieldName->setPlainText(nfield1);
96 qDebug() <<
"Error: Object is unknown (" << object1 <<
") this should not happen.";
116 QRegExp(subfield1, Qt::CaseSensitive, QRegExp::FixedString));
117 v = field->
getDouble(indexOfSubField) * factor;
121 s.sprintf(
"%.*f", places, v);
128 if (m_renderer->elementExists(
"symbol-" + s)) {
129 fieldSymbol->setElementId(
"symbol-" + s);
131 fieldSymbol->setElementId(
"symbol");
137 fieldValue->setPlainText(s);
139 if (index && !dialTimer.isActive())
142 qDebug() <<
"Wrong field, maybe an issue with object disconnection ?";
154 QGraphicsScene *l_scene = scene();
155 if (QFile::exists(dfn) && m_renderer->load(dfn) && m_renderer->isValid()) {
158 background =
new QGraphicsSvgItem();
159 background->setSharedRenderer(m_renderer);
160 background->setElementId(
"background");
161 background->setFlags(QGraphicsItem::ItemClipsChildrenToShape
162 | QGraphicsItem::ItemClipsToShape);
163 l_scene->addItem(background);
167 if (m_renderer->elementExists(
"red")) {
170 red =
new QGraphicsSvgItem();
171 red->setSharedRenderer(m_renderer);
172 red->setElementId(
"red");
173 red->setParentItem(background);
174 yellow =
new QGraphicsSvgItem();
175 yellow->setSharedRenderer(m_renderer);
176 yellow->setElementId(
"yellow");
177 yellow->setParentItem(background);
178 green =
new QGraphicsSvgItem();
179 green->setSharedRenderer(m_renderer);
180 green->setElementId(
"green");
181 green->setParentItem(background);
184 QMatrix textMatrix = m_renderer->matrixForElement(
"bargraph");
185 qreal bgX = textMatrix.mapRect(m_renderer->boundsOnElement(
"bargraph")).
x();
186 qreal bgY = textMatrix.mapRect(m_renderer->boundsOnElement(
"bargraph")).
y();
187 bargraphSize = textMatrix.mapRect(m_renderer->boundsOnElement(
"bargraph")).width();
189 qreal bargraphHeight =
190 textMatrix.mapRect(m_renderer->boundsOnElement(
"bargraph")).height();
191 if (bargraphHeight > bargraphSize) {
193 bargraphSize = bargraphHeight;
195 verticalDial =
false;
198 double range = maxValue - minValue;
200 green->resetTransform();
201 double greenScale = (greenMax - greenMin) / range;
202 double greenStart = verticalDial
203 ? (maxValue - greenMax) / range * green->boundingRect().height()
204 : (greenMin - minValue) / range * green->boundingRect().width();
208 matrix.scale(1, greenScale);
209 matrix.translate(bgX, (greenStart + bgY) / greenScale);
211 matrix.scale(greenScale, 1);
212 matrix.translate((greenStart + bgX) / greenScale, bgY);
214 green->setTransform(matrix,
false);
216 yellow->resetTransform();
217 double yellowScale = (yellowMax - yellowMin) / range;
218 double yellowStart = verticalDial
219 ? (maxValue - yellowMax) / range * yellow->boundingRect().height()
220 : (yellowMin - minValue) / range * yellow->boundingRect().width();
223 matrix.scale(1, yellowScale);
224 matrix.translate(bgX, (yellowStart + bgY) / yellowScale);
226 matrix.scale(yellowScale, 1);
227 matrix.translate((yellowStart + bgX) / yellowScale, bgY);
229 yellow->setTransform(matrix,
false);
231 red->resetTransform();
232 double redScale = (redMax - redMin) / range;
233 double redStart = verticalDial
234 ? (maxValue - redMax) / range * red->boundingRect().height()
235 : (redMin - minValue) / range * red->boundingRect().width();
238 matrix.scale(1, redScale);
239 matrix.translate(bgX, (redStart + bgY) / redScale);
241 matrix.scale(redScale, 1);
242 matrix.translate((redStart + bgX) / redScale, bgY);
244 red->setTransform(matrix,
false);
253 if (m_renderer->elementExists(
"needle")) {
254 QMatrix textMatrix = m_renderer->matrixForElement(
"needle");
255 QRectF nRect = textMatrix.mapRect(m_renderer->boundsOnElement(
"needle"));
259 matrix.translate(startX, startY);
260 index =
new QGraphicsSvgItem();
261 index->setSharedRenderer(m_renderer);
262 index->setElementId(
"needle");
263 index->setTransform(matrix,
false);
264 index->setParentItem(background);
270 if (m_renderer->elementExists(
"field")) {
271 QMatrix textMatrix = m_renderer->matrixForElement(
"field");
272 QRectF rect = textMatrix.mapRect(m_renderer->boundsOnElement(
"field"));
273 qreal startX = rect.x();
274 qreal startY = rect.y();
275 qreal elHeight = rect.height();
277 matrix.translate(startX, startY - elHeight / 2);
278 fieldName =
new QGraphicsTextItem(
"field");
279 fieldName->setFont(QFont(
"Arial", (
int)elHeight));
280 fieldName->setDefaultTextColor(QColor(
"White"));
281 fieldName->setTransform(matrix,
false);
282 fieldName->setParentItem(background);
288 if (m_renderer->elementExists(
"value")) {
289 QMatrix textMatrix = m_renderer->matrixForElement(
"value");
290 QRectF nRect = textMatrix.mapRect(m_renderer->boundsOnElement(
"value"));
291 qreal startX = nRect.x();
292 qreal startY = nRect.y();
293 qreal elHeight = nRect.height();
295 matrix.translate(startX, startY - elHeight / 2);
296 fieldValue =
new QGraphicsTextItem(
"0.00");
297 fieldValue->setFont(QFont(
"Arial", (
int)elHeight));
298 fieldValue->setDefaultTextColor(QColor(
"White"));
299 fieldValue->setTransform(matrix,
false);
300 fieldValue->setParentItem(background);
307 if (m_renderer->elementExists(
"symbol")) {
308 QMatrix textMatrix = m_renderer->matrixForElement(
"symbol");
309 qreal startX = textMatrix.mapRect(m_renderer->boundsOnElement(
"symbol")).
x();
310 qreal startY = textMatrix.mapRect(m_renderer->boundsOnElement(
"symbol")).
y();
312 matrix.translate(startX, startY);
313 fieldSymbol =
new QGraphicsSvgItem();
314 fieldSymbol->setElementId(
"symbol");
315 fieldSymbol->setSharedRenderer(m_renderer);
316 fieldSymbol->setTransform(matrix,
false);
317 fieldSymbol->setParentItem(background);
322 if (m_renderer->elementExists(
"foreground")) {
323 foreground =
new QGraphicsSvgItem();
324 foreground->setSharedRenderer(m_renderer);
325 foreground->setElementId(
"foreground");
326 foreground->setParentItem(background);
332 l_scene->setSceneRect(background->boundingRect());
336 if (!dialTimer.isActive() && index)
339 qDebug() <<
"no file ";
340 m_renderer->load(QString(
":/lineardial/images/empty.svg"));
342 background =
new QGraphicsSvgItem();
343 background->setSharedRenderer(m_renderer);
344 l_scene->addItem(background);
356 QFont font = QFont(
"Arial", 12);
357 font.fromString(fontProps);
359 int fieldSize = fieldName->font().pointSize();
360 font.setPointSize(fieldSize);
361 fieldName->setFont(font);
364 int fieldSize = fieldValue->font().pointSize();
365 font.setPointSize(fieldSize);
366 fieldValue->setFont(font);
378 if (!m_renderer->isValid()) {
379 qDebug() <<
"Dial file not loaded, not rendering";
382 QGraphicsView::paintEvent(event);
391 fitInView(background, Qt::KeepAspectRatio);
399 indexTarget = 100 * (maxValue - value) / (maxValue - minValue);
401 indexTarget = 100 * (value - minValue) / (maxValue - minValue);
408 void LineardialGadgetWidget::moveIndex()
414 if ((fabs((indexValue - indexTarget) * 10) > 3)) {
415 indexValue += (indexTarget - indexValue) / 5;
417 indexValue = indexTarget;
421 index->resetTransform();
422 qreal trans = indexValue * bargraphSize / 100;
424 matrix.translate(startX, trans + startY);
426 matrix.translate(trans + startX, startY);
428 index->setTransform(matrix,
false);
LineardialGadgetWidget(QWidget *parent=nullptr)
void updateIndex(UAVObject *object1)
Called by the UAVObject which got updated.
double getDouble(int index=0) const
Core plugin system that manages the plugins, their life cycle and their registered objects...
QVariant getValue(int index=0) const
void setIndex(double val)
void setDialFont(QString fontProps)
void connectInput(QString obj, QString field)
Connects the widget to the relevant UAVObjects.
void setDialFile(QString dfn)
Setup dial using its master SVG template.
UAVObjectField * getField(const QString &name)
~LineardialGadgetWidget()
void resizeEvent(QResizeEvent *event)
void paintEvent(QPaintEvent *event)
QStringList getElementNames() const
UAVObject * getObject(const QString &name, quint32 instId=0)