qcgaugewidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2 ** **
3 ** QcGauge, for instrumentation, and real time data measurement **
4 ** visualization widget for Qt. **
5 ** Copyright (C) 2015 Hadj Tahar Berrima **
6 ** **
7 ** This program is free software: you can redistribute it and/or modify **
8 ** it under the terms of the GNU Lesser General Public License as **
9 ** published by the Free Software Foundation, either version 3 of the **
10 ** License, or (at your option) any later version. **
11 ** **
12 ** This program is distributed in the hope that it will be useful, **
13 ** but WITHOUT ANY WARRANTY; without even the implied warranty of **
14 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the **
15 ** GNU Lesser General Public License for more details. **
16 ** **
17 ** You should have received a copy of the GNU Lesser General Public **
18 ** License along with this program. **
19 ** If not, see http://www.gnu.org/licenses/. **
20 ** **
21 ****************************************************************************
22 ** Author: Hadj Tahar Berrima **
23 ** Website: http://pytricity.com/ **
24 ** Contact: berrima_tahar@yahoo.com **
25 ** Date: 1 dec 2014 **
26 ** Version: 1.0 **
27 ****************************************************************************/
28 
29 
30 
31 #include "qcgaugewidget.h"
32 
36 
37 QcGaugeWidget::QcGaugeWidget(QWidget *parent) :
38  QWidget(parent)
39 {
40  setMinimumSize(250,250);
41 }
42 
44 {
45  QcBackgroundItem * item = new QcBackgroundItem(this);
46  item->setPosition(position);
47  mItems.append(item);
48  return item;
49 }
50 
52 {
53  QcDegreesItem * item = new QcDegreesItem(this);
54  item->setPosition(position);
55 
56  mItems.append(item);
57  return item;
58 }
59 
60 
62 {
63  QcValuesItem * item = new QcValuesItem(this);
64  item->setPosition(position);
65  mItems.append(item);
66  return item;
67 }
68 
70 {
71  QcArcItem * item = new QcArcItem(this);
72  item->setPosition(position);
73  mItems.append(item);
74  return item;
75 }
76 
78 {
79  QcColorBand * item = new QcColorBand(this);
80  item->setPosition(position);
81  mItems.append(item);
82  return item;
83 }
84 
86 {
87  QcNeedleItem * item = new QcNeedleItem(this);
88  item->setPosition(position);
89  mItems.append(item);
90  return item;
91 }
92 
94 {
95  QcLabelItem * item = new QcLabelItem(this);
96  item->setPosition(position);
97  mItems.append(item);
98  return item;
99 }
100 
102 {
103  QcGlassItem * item = new QcGlassItem(this);
104  item->setPosition(position);
105  mItems.append(item);
106  return item;
107 }
108 
110 {
111  QcAttitudeMeter * item = new QcAttitudeMeter(this);
112  item->setPosition(position);
113  mItems.append(item);
114  return item;
115 }
116 
117 void QcGaugeWidget::addItem(QcItem *item,float position)
118 {
119  // takes parentship of the item
120  item->setParent(this);
121  item->setPosition(position);
122  mItems.append(item);
123 }
124 
126 {
127  return mItems.removeAll(item);
128 }
129 
130 QList<QcItem *> QcGaugeWidget::items()
131 {
132  return mItems;
133 }
134 
135 
136 void QcGaugeWidget::paintEvent(QPaintEvent */*paintEvt*/)
137 {
138  QPainter painter(this);
139  painter.setRenderHint(QPainter::Antialiasing);
140 
141  foreach (QcItem * item, mItems) {
142  item->draw(&painter);
143  }
144 }
148 
149 QcItem::QcItem(QObject *parent) :
150  QObject(parent)
151 {
152 
153  parentWidget = qobject_cast<QWidget*>(parent);
154  mPosition = 50;
155 }
156 
158 {
159  return 50;
160 }
161 
163 {
164  parentWidget->update();
165 }
166 
168 {
169  return mPosition;
170 }
171 
172 QRectF QcItem::rect()
173 {
174  return mRect;
175 }
176 
178 {
179  if(position>100)
180  mPosition = 100;
181  else if(position<0)
182  mPosition = 0;
183  else
185  update();
186 }
187 
188 QRectF QcItem::adjustRect(float percentage)
189 {
190  float r = getRadius(mRect);
191  float offset = r-(percentage*r)/100.0;
192  QRectF tmpRect = mRect.adjusted(offset,offset,-offset,-offset);
193  return tmpRect;
194 }
195 
196 float QcItem::getRadius(const QRectF &tmpRect)
197 {
198  float r = 0;
199  if(tmpRect.width()<tmpRect.height())
200  r = tmpRect.width()/2.0;
201  else
202  r = tmpRect.height()/2.0;
203  return r;
204 }
205 
207 {
208  mRect = parentWidget->rect();
209  float r = getRadius(mRect);
210  mRect.setWidth(2.0*r);
211  mRect.setHeight(2.0*r);
212  mRect.moveCenter(parentWidget->rect().center());
213  return mRect;
214 }
215 
216 QPointF QcItem::getPoint(float deg,const QRectF &tmpRect)
217 {
218  float r = getRadius(tmpRect);
219  float xx=cos(0.0174533*deg)*r; // Convert deg to rad
220  float yy=sin(0.0174533*deg)*r; // Convert deg to rad
221  QPointF pt;
222  xx=tmpRect.center().x()-xx;
223  yy=tmpRect.center().y()-yy;
224  pt.setX(xx);
225  pt.setY(yy);
226  return pt;
227 }
228 
229 
230 
231 float QcItem::getAngle(const QPointF&pt, const QRectF &tmpRect)
232 {
233  float xx=tmpRect.center().x()-pt.x();
234  float yy=tmpRect.center().y()-pt.y();
235  return 0.0174533*( atan2(yy,xx));
236 }
237 
241 
242 QcScaleItem::QcScaleItem(QObject *parent) :
243  QcItem(parent)
244 {
245  mMinDegree = -45;
246  mMaxDegree = 225;
247  mMinValue = 0;
248  mMaxValue = 100;
249 }
250 
251 void QcScaleItem::setValueRange(float minValue, float maxValue)
252 {
253  if(!(minValue<maxValue))
254  throw( InvalidValueRange);
255  mMinValue = minValue;
256  mMaxValue = maxValue;
257 
258 }
259 
260 void QcScaleItem::setDgereeRange(float minDegree, float maxDegree)
261 {
262  if(!(minDegree<maxDegree))
263  throw( InvalidValueRange);
264  mMinDegree = minDegree;
265  mMaxDegree = maxDegree;
266 }
267 
269 {
271  float b = -a*mMinValue+mMinDegree;
272  return a*v+b;
273 }
274 
275 
276 void QcScaleItem::setMinValue(float minValue)
277 {
278  if(minValue>mMaxValue)
279  throw (InvalidValueRange);
280  mMinValue = minValue;
281  update();
282 }
283 
284 
285 void QcScaleItem::setMaxValue(float maxValue)
286 {
287  if(maxValue<mMinValue )
288  throw (InvalidValueRange);
289  mMaxValue = maxValue;
290  update();
291 }
292 
293 void QcScaleItem::setMinDegree(float minDegree)
294 {
295  if(minDegree>mMaxDegree)
296  throw (InvalidDegreeRange);
297  mMinDegree = minDegree;
298  update();
299 }
300 void QcScaleItem::setMaxDegree(float maxDegree)
301 {
302  if(maxDegree<mMinDegree)
303  throw (InvalidDegreeRange);
304  mMaxDegree = maxDegree;
305  update();
306 }
307 
311 
313  QcItem(parent)
314 {
315  setPosition(88);
316  mPen = Qt::NoPen;
317  setPosition(88);
318 
319  addColor(0.4,Qt::darkGray);
320  addColor(0.8,Qt::black);
321 
322 }
323 
324 
325 void QcBackgroundItem::draw(QPainter* painter)
326 {
327  QRectF tmpRect = resetRect();
328  painter->setBrush(Qt::NoBrush);
329  QLinearGradient linearGrad(tmpRect.topLeft(), tmpRect.bottomRight());
330  for(int i = 0;i<mColors.size();i++){
331  linearGrad.setColorAt(mColors[i].first,mColors[i].second);
332  }
333  painter->setPen(mPen);
334  painter->setBrush(linearGrad);
335  painter->drawEllipse(adjustRect(position()));
336 }
337 
338 void QcBackgroundItem::addColor(float position, const QColor &color)
339 {
340  if(position<0||position>1)
341  return;
342  QPair<float,QColor> pair;
343  pair.first = position;
344  pair.second = color;
345  mColors.append(pair);
346  update();
347 }
348 
350 {
351  mColors.clear();
352 }
353 
357 
358 QcGlassItem::QcGlassItem(QObject *parent) :
359  QcItem(parent)
360 {
361  setPosition(88);
362 }
363 
364 void QcGlassItem::draw(QPainter *painter)
365 {
366  resetRect();
367  QRectF tmpRect1 = adjustRect(position());
368  QRectF tmpRect2 = tmpRect1;
369  float r = getRadius(tmpRect1);
370  tmpRect2.setHeight(r/2.0);
371  painter->setPen(Qt::NoPen);
372 
373  QColor clr1 = Qt::gray ;
374  QColor clr2 = Qt::white;
375  clr1.setAlphaF(0.2);
376  clr2.setAlphaF(0.4);
377 
378  QLinearGradient linearGrad1(tmpRect1.topLeft(), tmpRect1.bottomRight());
379  linearGrad1.setColorAt(0.1, clr1);
380  linearGrad1.setColorAt(0.5, clr2);
381 
382  painter->setBrush(linearGrad1);
383  painter->drawPie(tmpRect1,0,16*180);
384  tmpRect2.moveCenter(rect().center());
385  painter->drawPie(tmpRect2,0,-16*180);
386 }
390 
391 QcLabelItem::QcLabelItem(QObject *parent) :
392  QcItem(parent)
393 {
394  setPosition(50);
395  mAngle = 270;
396  mText = "%";
397  mColor = Qt::black;
398 }
399 
400 void QcLabelItem::draw(QPainter *painter)
401 {
402  resetRect();
403  QRectF tmpRect = adjustRect(position());
404  float r = getRadius(rect());
405  QFont font("Meiryo UI", r/10.0, QFont::Bold);
406  painter->setFont(font);
407  painter->setPen(QPen(mColor));
408 
409  QPointF txtCenter = getPoint(mAngle,tmpRect);
410  QFontMetrics fMetrics = painter->fontMetrics();
411  QSize sz = fMetrics.size( Qt::TextSingleLine, mText );
412  QRectF txtRect(QPointF(0,0), sz );
413  txtRect.moveCenter(txtCenter);
414 
415  painter->drawText( txtRect, Qt::TextSingleLine,mText );
416 
417 }
418 
420 {
421  mAngle = a;
422  update();
423 }
424 
426 {
427  return mAngle;
428 }
429 
430 void QcLabelItem::setText(const QString &text, bool repaint)
431 {
432  mText = text;
433  if(repaint)
434  update();
435 }
436 
438 {
439  return mText;
440 }
441 
442 void QcLabelItem::setColor(const QColor &color)
443 {
444  mColor = color;
445  update();
446 }
447 
449 {
450  return mColor;
451 }
452 
456 
457 QcArcItem::QcArcItem(QObject *parent) :
458  QcScaleItem(parent)
459 {
460  setPosition(80);
461  mColor = Qt::black;
462 }
463 
464 void QcArcItem::draw(QPainter *painter)
465 {
466  resetRect();
467  QRectF tmpRect= adjustRect(position());
468  float r = getRadius(tmpRect);
469 
470  QPen pen;
471  pen.setColor(mColor);
472  pen.setWidthF(r/40);
473  painter->setPen(pen);
474  painter->drawArc(tmpRect,-16*(mMinDegree+180),-16*(mMaxDegree-mMinDegree));
475 }
476 
477 void QcArcItem::setColor(const QColor &color)
478 {
479  mColor = color;
480 }
484 
485 QcColorBand::QcColorBand(QObject *parent) :
486  QcScaleItem(parent)
487 {
488  QColor tmpColor;
489  tmpColor.setAlphaF(0.1);
490  QPair<QColor,float> pair;
491 
492  pair.first = Qt::green;
493  pair.second = 10;
494  mBandColors.append(pair);
495 
496  pair.first = Qt::darkGreen;
497  pair.second = 50;
498  mBandColors.append(pair);
499 
500  pair.first = Qt::red;
501  pair.second = 100;
502  mBandColors.append(pair);
503 
504  setPosition(50);
505 }
506 
507 QPainterPath QcColorBand::createSubBand(float from, float sweep)
508 {
509  QRectF tmpRect = adjustRect(position());
510  QPainterPath path;
511  path.arcMoveTo(tmpRect,180+from);
512  path.arcTo(tmpRect,180+from,-sweep);
513  return path;
514 }
515 
516 void QcColorBand::draw(QPainter *painter)
517 {
518  resetRect();
519  float r = getRadius(rect());
520  QPen pen;
521  pen.setCapStyle(Qt::FlatCap);
522  pen.setWidthF(r/20.0);
523  painter->setBrush(Qt::NoBrush);
524  float offset = getDegFromValue(mBandStartValue);
525  for(int i = 0;i<mBandColors.size();i++){
526  QColor clr = mBandColors[i].first;
527  float sweep;
528  if(i==0)
530  else
531  sweep = getDegFromValue(mBandColors[i].second)-getDegFromValue(mBandColors[i-1].second);
532  QPainterPath path = createSubBand(-offset,sweep);
533  offset += sweep;
534  pen.setColor(clr);
535  painter->setPen(pen);
536  painter->drawPath(path);
537  }
538 }
539 void QcColorBand::setColors(const QList<QPair<QColor, float> > &colors)
540 {
541  mBandColors = colors;
542  update();
543 }
544 
548 
550  QcScaleItem(parent)
551 {
552  mStep = 10;
553  mColor = Qt::black;
554  mSubDegree = false;
555  setPosition(90);
556 }
557 
558 
559 void QcDegreesItem::draw(QPainter *painter)
560 {
561  resetRect();
562  QRectF tmpRect = adjustRect(position());
563 
564  painter->setPen(mColor);
565  float r = getRadius(tmpRect);
566  for(float val = mMinValue;val<=mMaxValue;val+=mStep){
567  float deg = getDegFromValue(val);
568  QPointF pt = getPoint(deg,tmpRect);
569  QPainterPath path;
570  path.moveTo(pt);
571  path.lineTo(tmpRect.center());
572  pt = path.pointAtPercent(0.03);
573  QPointF newPt = path.pointAtPercent(0.13);
574 
575  QPen pen;
576  pen.setColor(mColor);
577  if(!mSubDegree)
578  pen.setWidthF(r/25.0);
579 
580  painter->setPen(pen);
581  painter->drawLine(pt,newPt);
582 
583  }
584 }
585 
586 void QcDegreesItem::setStep(float step)
587 {
588  mStep = step;
589  update();
590 }
591 
592 void QcDegreesItem::setColor(const QColor& color)
593 {
594  mColor = color;
595  update();
596 }
597 
599 {
600  mSubDegree = b;
601  update();
602 }
603 
607 
608 QcNeedleItem::QcNeedleItem(QObject *parent) :
609  QcScaleItem(parent)
610 {
611  mCurrentValue = 0;
612  mColor = Qt::black;
613  mLabel = NULL;
615 }
616 
617 void QcNeedleItem::draw(QPainter *painter)
618 {
619  resetRect();
620  QRectF tmpRect = adjustRect(position());
621  painter->save();
622  painter->translate(tmpRect.center());
623  float deg = getDegFromValue( mCurrentValue);
624  painter->rotate(deg+90.0);
625  painter->setBrush(QBrush(mColor));
626  painter->setPen(Qt::NoPen);
627 
628  QLinearGradient grad;
629 
630  switch (mNeedleType) {
632  createFeatherNeedle(getRadius(tmpRect));
633  break;
635  createDiamonNeedle(getRadius(tmpRect));
636  break;
639  break;
642  break;
644  createCompassNeedle(getRadius(tmpRect));
645  grad.setStart(mNeedlePoly[0]);
646  grad.setFinalStop(mNeedlePoly[1]);
647  grad.setColorAt(0.9,Qt::red);
648  grad.setColorAt(1,Qt::blue);
649  painter->setBrush(grad);
650 
651  break;
652 
653  default:
654  break;
655  }
656  painter->drawConvexPolygon(mNeedlePoly);
657  painter->restore();
658 }
659 
661 {
662  if(value<mMinValue)
664  else if(value>mMaxValue)
666  else
667  mCurrentValue = value;
668 
669  if(mLabel!=0)
670  mLabel->setText(QString::number(mCurrentValue),false);
671 
673 // if(mLabel!=0){
674 // QString currentValue;
675 // mLabel->setText( currentValue ,false);
676 // mLabel->setText(currentValue.sprintf(mFormat.toStdString().c_str(), mCurrentValue),false);
677 // Q_UNUSED(currentValue);
678 // }
679  update();
680 }
681 
683 {
684  return mCurrentValue;
685 }
686 
687 void QcNeedleItem::setValueFormat(QString format){
688  mFormat = format;
689  update();
690 }
691 
693  return mFormat;
694 }
695 
696 void QcNeedleItem::setColor(const QColor &color)
697 {
698  mColor = color;
699  update();
700 }
701 
703 {
704  return mColor;
705 }
706 
708 {
709  mLabel = label;
710  update();
711 }
712 
714 {
715  return mLabel;
716 }
717 
718 
720 {
721  mNeedleType = needleType;
722  update();
723 }
724 
725 
727 {
728  QVector<QPointF> tmpPoints;
729  tmpPoints.append(QPointF(0.0, 0.0));
730  tmpPoints.append(QPointF(-r/20.0,r/20.0));
731  tmpPoints.append(QPointF(0.0, r));
732  tmpPoints.append(QPointF(r/20.0,r/20.0));
733  mNeedlePoly = tmpPoints;
734 }
735 
737 {
738  QVector<QPointF> tmpPoints;
739  tmpPoints.append(QPointF(0.0, r));
740  tmpPoints.append(QPointF(-r/40.0, 0.0));
741  tmpPoints.append(QPointF(r/40.0,0.0));
742  mNeedlePoly = tmpPoints;
743 }
744 
746 {
747  QVector<QPointF> tmpPoints;
748  tmpPoints.append(QPointF(0.0, r));
749  tmpPoints.append(QPointF(-r/40.0, 0.0));
750  tmpPoints.append(QPointF(-r/15.0, -r/5.0));
751  tmpPoints.append(QPointF(r/15.0,-r/5));
752  tmpPoints.append(QPointF(r/40.0,0.0));
753  mNeedlePoly = tmpPoints;
754 }
755 
757 {
758  QVector<QPointF> tmpPoints;
759  tmpPoints.append(QPointF(0.0, r));
760  tmpPoints.append(QPointF(-r/20.0, 0.85*r));
761  tmpPoints.append(QPointF(r/20.0,0.85*r));
762  mNeedlePoly = tmpPoints;
763 }
764 
766 {
767  QVector<QPointF> tmpPoints;
768  tmpPoints.append(QPointF(0.0, r));
769  tmpPoints.append(QPointF(-r/15.0, 0.0));
770  tmpPoints.append(QPointF(0.0, -r));
771  tmpPoints.append(QPointF(r/15.0,0.0));
772  mNeedlePoly = tmpPoints;
773 }
774 
778 
779 QcValuesItem::QcValuesItem(QObject *parent) :
780  QcScaleItem(parent)
781 {
782  setPosition(70);
783  mColor = Qt::black;
784  mStep = 10;
785 }
786 
787 
788 void QcValuesItem::draw(QPainter*painter)
789 {
790  QRectF tmpRect = resetRect();
791  float r = getRadius(adjustRect(99));
792  QFont font("Meiryo UI",0, QFont::Bold);
793  font.setPointSizeF(0.08*r);
794 
795  painter->setFont(font);
796  painter->setPen(mColor);
797  for(float val = mMinValue;val<=mMaxValue;val+=mStep){
798  float deg = getDegFromValue(val);
799  QPointF pt = getPoint(deg,tmpRect);
800  QPainterPath path;
801  path.moveTo(pt);
802  path.lineTo( tmpRect.center());
803  QString strVal = QString::number(val);
804  QFontMetrics fMetrics = painter->fontMetrics();
805  QSize sz = fMetrics.size( Qt::TextSingleLine, strVal );
806  QRectF txtRect(QPointF(0,0), sz );
807  QPointF textCenter = path.pointAtPercent(1.0-position()/100.0);
808  txtRect.moveCenter(textCenter);
809 
810  painter->drawText( txtRect, Qt::TextSingleLine, strVal );
811  }
812 }
813 
814 void QcValuesItem::setStep(float step)
815 {
816  mStep = step;
817 }
818 
819 
820 void QcValuesItem::setColor(const QColor& color)
821 {
822  mColor = color;
823 }
824 
828 
830  QcItem(parent)
831 {
832  mPitch = 0;
833  mRoll = 0;
834 }
835 
837 {
838  mPitch=-pitch;
839  update();
840 }
841 
843 {
844  mRoll = roll;
845  update();
846 }
847 
848 QPointF QcAttitudeMeter::getIntersection(float r, const QPointF &pitchPoint, const QPointF &pt)
849 {
850  // refrence it to zero
851 
852  Q_UNUSED(r)
853  float a = (pitchPoint.y()-pt.y())/(pitchPoint.x()-pt.x());
854  float b = pt.y()-a*pt.x();
855  return QPointF(0,a*0+b);
856 }
857 
858 float QcAttitudeMeter::getStartAngle(const QRectF& tmpRect)
859 {
860  float r = getRadius(tmpRect);
861  QPointF pt1 = getPoint(mRoll,tmpRect);
862  pt1.setY(pt1.y()-mPitchOffset);
863  QPointF pitchPoint = QPointF(tmpRect.center().x(),tmpRect.center().y()-mPitchOffset);
864 
865 
867  QPainterPath path1;
868  path1.moveTo(pitchPoint);
869  path1.lineTo(getIntersection(r,pitchPoint,pt1)+QPointF(0,5));
870  path1.lineTo(getIntersection(r,pitchPoint,pt1)+QPointF(0,-5));
871 
872  QPainterPath path2;
873  path2.addEllipse(tmpRect);
874 
875  QPointF p = path1.intersected(path2).pointAtPercent(.5);
876  return getAngle(p,tmpRect);
877 }
878 
879 void QcAttitudeMeter::draw(QPainter *painter)
880 {
881  resetRect();
882  QRectF tmpRect = adjustRect(position());
883  float r = getRadius(tmpRect);
884  if(mPitch<0)
885  mPitchOffset = 0.0135*r*mPitch;
886  else
887  mPitchOffset = 0.015*r*mPitch;
888 
889  painter->setPen(Qt::NoPen);
890  drawUpperEllipse(painter,tmpRect);
891  drawLowerEllipse(painter,tmpRect);
892 
893  // Steps
894 
895  drawPitchSteps(painter,tmpRect);
896  drawHandle(painter);
897 
898  drawDegrees(painter);
899 
900 }
901 
902 void QcAttitudeMeter::drawDegrees(QPainter *painter)
903 {
904  resetRect();
905  QRectF tmpRect = adjustRect(position());
906  float r = getRadius(tmpRect);
907  QPen pen;
908 
909  pen.setColor(Qt::white);
910  painter->setPen(pen);
911  for(int deg = 60;deg<=120;deg+=10){
912  if(deg == 90)
913  continue;
914  drawDegree(painter,tmpRect,deg);
915  }
916 
917  pen.setWidthF(r/30.0);
918  painter->setPen(pen);
919  drawDegree(painter,tmpRect,0);
920  drawDegree(painter,tmpRect,90);
921  drawDegree(painter,tmpRect,180);
922  drawDegree(painter,tmpRect,30);
923  drawDegree(painter,tmpRect,150);
924 }
925 
926 
927 void QcAttitudeMeter::drawDegree(QPainter * painter, const QRectF& tmpRect,float deg)
928 {
929  QPointF pt1 = getPoint(deg,tmpRect);
930  QPointF pt2 = tmpRect.center();
931  QPainterPath path;
932  path.moveTo(pt1);
933  path.lineTo(pt2);
934  QPointF pt = path.pointAtPercent(0.1);
935  painter->drawLine(pt1,pt);
936 }
937 
938 
939 void QcAttitudeMeter::drawUpperEllipse(QPainter *painter, const QRectF &tmpRect)
940 {
941 
942  QLinearGradient radialGrad1(tmpRect.topLeft(),tmpRect.bottomRight());
943  QColor clr1 = Qt::blue;
944  clr1.setAlphaF(0.5);
945  QColor clr2 = Qt::darkBlue;
946  clr2.setAlphaF(0.5);
947  radialGrad1.setColorAt(0, clr1);
948  radialGrad1.setColorAt(.8, clr2);
949 
950 
951  float offset = getStartAngle(tmpRect);
952  float startAngle = 180-offset;
953  float endAngle = offset-2*mRoll;
954  float span =endAngle-startAngle;
955 
956  painter->setBrush(radialGrad1);
957  painter->drawChord(tmpRect,16*startAngle,16*span);
958 
959 }
960 
961 
962 void QcAttitudeMeter::drawLowerEllipse(QPainter *painter, const QRectF &tmpRect)
963 {
964  QLinearGradient radialGrad2(tmpRect.topLeft(),tmpRect.bottomRight());
965  QColor clr1 = QColor(139,119,118);
966  QColor clr2 = QColor(139,119,101);
967  radialGrad2.setColorAt(0, clr1);
968  radialGrad2.setColorAt(.8, clr2);
969 
970  float offset = getStartAngle(tmpRect);
971  float startAngle = 180+offset;
972  float endAngle = offset-2*mRoll;
973  float span =endAngle+startAngle;
974 
975  painter->setPen(Qt::NoPen);
976  painter->setBrush(radialGrad2);
977  painter->drawChord(tmpRect,-16*startAngle,16*span);
978 
979 }
980 
981 void QcAttitudeMeter::drawPitchSteps(QPainter *painter, const QRectF &tmpRect)
982 {
983  float r = getRadius(tmpRect);
984  QPointF center = tmpRect.center();
985  painter->save();
986  painter->translate(center.x(),center.y()-mPitchOffset);
987  painter->rotate(mRoll);
988  QPen pen;
989  pen.setColor(Qt::white);
990  pen.setWidthF(r/40.0);
991 
992  painter->setPen(pen);
993  for (int i = -30;i<=30;i+=10){
994  QPointF pt1;
995  pt1.setX(-0.01*r*abs(i));
996  pt1.setY(r/70.0*i);
997  QPointF pt2;
998  pt2.setX(0.01*r*abs(i));
999  pt2.setY(r/70.0*i);
1000  painter->drawLine(pt1,pt2);
1001 
1002  if(i==0)
1003  continue;
1004 
1005  // draw value
1006  QFont font("Meiryo UI",0, QFont::Bold);
1007  font.setPointSizeF(0.08*r);
1008  painter->setFont(font);
1009  QString strVal = QString::number(abs(i));
1010  QFontMetrics fMetrics = painter->fontMetrics();
1011  QSize sz = fMetrics.size( Qt::TextSingleLine, strVal );
1012  QRectF leftTxtRect(QPointF(0,0), sz );
1013  QRectF rightTxtRect(QPointF(0,0), sz );
1014  leftTxtRect.moveCenter(pt1-QPointF(0.1*r,0));
1015  rightTxtRect.moveCenter(pt2+QPointF(0.1*r,0));
1016  painter->drawText( leftTxtRect, Qt::TextSingleLine, strVal );
1017  painter->drawText( rightTxtRect, Qt::TextSingleLine, strVal );
1018  }
1019  painter->restore();
1020 }
1021 
1022 void QcAttitudeMeter::drawHandle(QPainter *painter)
1023 {
1024  QRectF tmpRct = adjustRect(15);
1025  float r = getRadius(tmpRct);
1026  QPen pen;
1027  pen.setColor(Qt::gray);
1028  pen.setWidthF(0.25*r);
1029  painter->setPen(pen);
1030  painter->drawArc(tmpRct,0,-16*180);
1031 
1032  QPointF center = tmpRct.center();
1033  QPointF leftPt1 = center;
1034  QPointF leftPt2 = center;
1035  QPointF rightPt1 = center;
1036  QPointF rightPt2 = center;
1037  leftPt1.setX(center.x()-2*r);
1038  leftPt2.setX(center.x()-r);
1039  rightPt1.setX(center.x()+2*r);
1040  rightPt2.setX(center.x()+r);
1041  painter->drawLine(leftPt1,leftPt2);
1042  painter->drawLine(rightPt1,rightPt2);
1043  painter->drawEllipse(adjustRect(2));
1044 
1045  //
1046  QPointF pt1 = center;
1047  QPointF pt2 = center;
1048  // to get the real 100 % radius, without recomputing
1049  pt1.setY(center.y()+r);
1050  pt2.setY(center.y()+4*r);
1051  pen.setColor(Qt::gray);
1052  painter->setPen(pen);
1053  painter->drawLine(pt1,pt2);
1054 
1055  // trapezium
1056  painter->setPen(Qt::gray);
1057  painter->setBrush(Qt::gray);
1058  QPolygonF trapPoly;
1059  QPointF tmpPt = center;
1060  tmpPt.setX(center.x()-r);
1061  tmpPt.setY(center.y()+4*r);
1062  trapPoly.append(tmpPt);
1063  tmpRct = adjustRect(position());
1064  trapPoly.append(getPoint(290,tmpRct));
1065  trapPoly.append(getPoint(250,tmpRct));
1066  tmpPt = center;
1067  tmpPt.setX(center.x()+r);
1068  tmpPt.setY(center.y()+4*r);
1069  trapPoly.append(tmpPt);
1070  painter->drawPolygon(trapPoly);
1071  painter->drawChord(tmpRct,-16*70,-16*40);
1072 }
1073 
float getDegFromValue(float)
QcGlassItem(QObject *parent=0)
void setStep(float step)
float getRadius(const QRectF &)
void drawDegree(QPainter *painter, const QRectF &tmpRect, float deg)
NeedleType mNeedleType
QcAttitudeMeter(QObject *parent=0)
QcDegreesItem(QObject *parent=0)
QcAttitudeMeter * addAttitudeMeter(float position)
QRectF resetRect()
QPointF getPoint(float deg, const QRectF &tmpRect)
QWidget * parentWidget
void drawHandle(QPainter *)
QcValuesItem(QObject *parent=0)
QcDegreesItem * addDegrees(float position)
virtual void draw(QPainter *)
QColor color()
void setMaxDegree(float maxDegree)
void drawUpperEllipse(QPainter *, const QRectF &)
QPainterPath createSubBand(float from, float sweep)
QList< QcItem * > items()
QList< QcItem * > mItems
Definition: qcgaugewidget.h:83
QcBackgroundItem * addBackground(float position)
void draw(QPainter *)
void setPosition(float percentage)
QList< QPair< QColor, float > > mBandColors
void drawDegrees(QPainter *)
void createDiamonNeedle(float r)
QcBackgroundItem(QObject *parent=0)
void drawLowerEllipse(QPainter *, const QRectF &)
void draw(QPainter *)
void draw(QPainter *painter)
void setCurrentRoll(float roll)
QcGlassItem * addGlass(float position)
QcLabelItem * mLabel
void setText(const QString &text, bool repaint=true)
QRectF mRect
QRectF rect()
void setValueFormat(QString format)
QcNeedleItem * addNeedle(float position)
void setCurrentValue(float value)
QString mText
float mPosition
void createTriangleNeedle(float r)
void draw(QPainter *)
void setDgereeRange(float minDegree, float maxDegree)
void setColor(const QColor &color)
void setStep(float step)
void addItem(QcItem *item, float position)
QcGaugeWidget(QWidget *parent=0)
void setMinDegree(float minDegree)
float mCurrentValue
virtual void draw(QPainter *)=0
float mBandStartValue
QcItem(QObject *parent=0)
float position()
QString text()
QPointF getIntersection(float r, const QPointF &pitchPoint, const QPointF &pt)
float mMaxDegree
virtual int type()
void setValueRange(float minValue, float maxValue)
void setMaxValue(float maxValue)
void setCurrentPitch(float pitch)
float mMinDegree
QcColorBand * addColorBand(float position)
void paintEvent(QPaintEvent *)
QcScaleItem(QObject *parent=0)
void setLabel(QcLabelItem *)
QString currentValueFormat()
QcValuesItem * addValues(float position)
int removeItem(QcItem *item)
void setColor(const QColor &color)
void draw(QPainter *)
QColor mColor
void draw(QPainter *)
void setNeedle(QcNeedleItem::NeedleType needleType)
void createCompassNeedle(float r)
void setAngle(float)
void setMinValue(float minValue)
float currentValue()
void draw(QPainter *)
QList< QPair< float, QColor > > mColors
QcArcItem(QObject *parent=0)
void drawPitchSteps(QPainter *, const QRectF &)
void setColors(const QList< QPair< QColor, float > > &colors)
float getAngle(const QPointF &, const QRectF &tmpRect)
void setColor(const QColor &color)
QcNeedleItem(QObject *parent=0)
void draw(QPainter *)
QString mFormat
void createFeatherNeedle(float r)
void createAttitudeNeedle(float r)
void setColor(const QColor &color)
void setSubDegree(bool)
void setColor(const QColor &color)
void update()
QcLabelItem * addLabel(float position)
void addColor(float position, const QColor &color)
QRectF adjustRect(float percentage)
QcLabelItem * label()
QcArcItem * addArc(float position)
QcLabelItem(QObject *parent=0)
QcColorBand(QObject *parent=0)
float getStartAngle(const QRectF &tmpRect)
QPolygonF mNeedlePoly


gauges
Author(s): alexvs
autogenerated on Mon Nov 14 2016 03:55:02