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