qcgaugewidget.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002 **                                                                        **
00003 **  QcGauge, for instrumentation, and real time data measurement          **
00004 **  visualization widget for Qt.                                          **
00005 **  Copyright (C) 2015 Hadj Tahar Berrima                                 **
00006 **                                                                        **
00007 **  This program is free software: you can redistribute it and/or modify  **
00008 **  it under the terms of the GNU Lesser General Public License as        **
00009 **  published by the Free Software Foundation, either version 3 of the    **
00010 **  License, or (at your option) any later version.                       **
00011 **                                                                        **
00012 **  This program is distributed in the hope that it will be useful,       **
00013 **  but WITHOUT ANY WARRANTY; without even the implied warranty of        **
00014 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         **
00015 **  GNU Lesser General Public License for more details.                   **
00016 **                                                                        **
00017 **  You should have received a copy of the GNU Lesser General Public      **
00018 **  License along with this program.                                      **
00019 **  If not, see http://www.gnu.org/licenses/.                             **
00020 **                                                                        **
00021 ****************************************************************************
00022 **           Author:  Hadj Tahar Berrima                                  **
00023 **           Website: http://pytricity.com/                               **
00024 **           Contact: berrima_tahar@yahoo.com                             **
00025 **           Date:    1 dec 2014                                          **
00026 **           Version:  1.0                                                **
00027 ****************************************************************************/
00028 
00029 
00030 #include "rqt_gauges/qcgaugewidget.h"
00031 
00035 
00036 QcGaugeWidget::QcGaugeWidget(QWidget *parent) :
00037     QWidget(parent)
00038 {
00039     setMinimumSize(250,250);
00040 }
00041 
00042 QcBackgroundItem *QcGaugeWidget::addBackground(float position)
00043 {
00044     QcBackgroundItem * item = new QcBackgroundItem(this);
00045     item->setPosition(position);
00046     mItems.append(item);
00047     return item;
00048 }
00049 
00050 QcDegreesItem *QcGaugeWidget::addDegrees(float position)
00051 {
00052     QcDegreesItem * item = new QcDegreesItem(this);
00053     item->setPosition(position);
00054 
00055     mItems.append(item);
00056     return item;
00057 }
00058 
00059 
00060 QcValuesItem *QcGaugeWidget::addValues(float position)
00061 {
00062     QcValuesItem * item = new QcValuesItem(this);
00063     item->setPosition(position);
00064     mItems.append(item);
00065     return item;
00066 }
00067 
00068 QcArcItem *QcGaugeWidget::addArc(float position)
00069 {
00070     QcArcItem * item = new QcArcItem(this);
00071     item->setPosition(position);
00072     mItems.append(item);
00073     return item;
00074 }
00075 
00076 QcColorBand *QcGaugeWidget::addColorBand(float position)
00077 {
00078     QcColorBand * item = new QcColorBand(this);
00079     item->setPosition(position);
00080     mItems.append(item);
00081     return item;
00082 }
00083 
00084 QcNeedleItem *QcGaugeWidget::addNeedle(float position)
00085 {
00086     QcNeedleItem * item = new QcNeedleItem(this);
00087     item->setPosition(position);
00088     mItems.append(item);
00089     return item;
00090 }
00091 
00092 QcLabelItem *QcGaugeWidget::addLabel(float position)
00093 {
00094     QcLabelItem * item = new QcLabelItem(this);
00095     item->setPosition(position);
00096     mItems.append(item);
00097     return item;
00098 }
00099 
00100 QcGlassItem *QcGaugeWidget::addGlass(float position)
00101 {
00102     QcGlassItem * item = new QcGlassItem(this);
00103     item->setPosition(position);
00104     mItems.append(item);
00105     return item;
00106 }
00107 
00108 QcAttitudeMeter *QcGaugeWidget::addAttitudeMeter(float position)
00109 {
00110     QcAttitudeMeter * item = new QcAttitudeMeter(this);
00111     item->setPosition(position);
00112     mItems.append(item);
00113     return item;
00114 }
00115 
00116 void QcGaugeWidget::addItem(QcItem *item,float position)
00117 {
00118     // takes parentship of the item
00119     item->setParent(this);
00120     item->setPosition(position);
00121     mItems.append(item);
00122 }
00123 
00124 int QcGaugeWidget::removeItem(QcItem *item)
00125 {
00126    return mItems.removeAll(item);
00127 }
00128 
00129 QList<QcItem *> QcGaugeWidget::items()
00130 {
00131     return mItems;
00132 }
00133 
00134 
00135 void QcGaugeWidget::paintEvent(QPaintEvent */*paintEvt*/)
00136 {
00137     QPainter painter(this);
00138     painter.setRenderHint(QPainter::Antialiasing);
00139 
00140     foreach (QcItem * item, mItems) {
00141         item->draw(&painter);
00142     }
00143 }
00147 
00148 QcItem::QcItem(QObject *parent) :
00149     QObject(parent)
00150 {
00151 
00152     parentWidget = qobject_cast<QWidget*>(parent);
00153     mPosition = 50;
00154 }
00155 
00156 int QcItem::type()
00157 {
00158     return 50;
00159 }
00160 
00161 void QcItem::update()
00162 {
00163     parentWidget->update();
00164 }
00165 
00166 float QcItem::position()
00167 {
00168     return mPosition;
00169 }
00170 
00171 QRectF QcItem::rect()
00172 {
00173     return mRect;
00174 }
00175 
00176 void QcItem::setPosition(float position)
00177 {
00178     if(position>100)
00179         mPosition = 100;
00180     else if(position<0)
00181         mPosition = 0;
00182     else
00183         mPosition = position;
00184     update();
00185 }
00186 
00187 QRectF QcItem::adjustRect(float percentage)
00188 {
00189     float r = getRadius(mRect);
00190     float offset =   r-(percentage*r)/100.0;
00191     QRectF tmpRect = mRect.adjusted(offset,offset,-offset,-offset);
00192     return tmpRect;
00193 }
00194 
00195 float QcItem::getRadius(const QRectF &tmpRect)
00196 {
00197     float r = 0;
00198     if(tmpRect.width()<tmpRect.height())
00199         r = tmpRect.width()/2.0;
00200     else
00201         r = tmpRect.height()/2.0;
00202     return r;
00203 }
00204 
00205 QRectF QcItem::resetRect()
00206 {
00207     mRect = parentWidget->rect();
00208     float r = getRadius(mRect);
00209     mRect.setWidth(2.0*r);
00210     mRect.setHeight(2.0*r);
00211     mRect.moveCenter(parentWidget->rect().center());
00212     return mRect;
00213 }
00214 
00215 QPointF QcItem::getPoint(float deg,const QRectF &tmpRect)
00216 {
00217     float r = getRadius(tmpRect);
00218     float xx=cos(0.0174533*deg)*r; // Convert deg to rad
00219     float yy=sin(0.0174533*deg)*r; // Convert deg to rad
00220     QPointF pt;
00221     xx=tmpRect.center().x()-xx;
00222     yy=tmpRect.center().y()-yy;
00223     pt.setX(xx);
00224     pt.setY(yy);
00225     return pt;
00226 }
00227 
00228 
00229 
00230 float QcItem::getAngle(const QPointF&pt, const QRectF &tmpRect)
00231 {
00232     float xx=tmpRect.center().x()-pt.x();
00233     float yy=tmpRect.center().y()-pt.y();
00234     return 0.0174533*( atan2(yy,xx));
00235 }
00236 
00240 
00241 QcScaleItem::QcScaleItem(QObject *parent) :
00242     QcItem(parent)
00243 {
00244     mMinDegree = -45;
00245     mMaxDegree = 225;
00246     mMinValue = 0;
00247     mMaxValue = 100;
00248 }
00249 
00250 void QcScaleItem::setValueRange(float minValue, float maxValue)
00251 {
00252     if(!(minValue<maxValue))
00253         throw( InvalidValueRange);
00254     mMinValue = minValue;
00255     mMaxValue = maxValue;
00256 
00257 }
00258 
00259 void QcScaleItem::setDgereeRange(float minDegree, float maxDegree)
00260 {
00261     if(!(minDegree<maxDegree))
00262         throw( InvalidValueRange);
00263     mMinDegree = minDegree;
00264     mMaxDegree = maxDegree;
00265 }
00266 
00267 float QcScaleItem::getDegFromValue(float v)
00268 {
00269     float a = (mMaxDegree-mMinDegree)/(mMaxValue-mMinValue);
00270     float b = -a*mMinValue+mMinDegree;
00271     return a*v+b;
00272 }
00273 
00274 
00275 void QcScaleItem::setMinValue(float minValue)
00276 {
00277     if(minValue>mMaxValue)
00278         throw (InvalidValueRange);
00279     mMinValue = minValue;
00280     update();
00281 }
00282 
00283 
00284 void QcScaleItem::setMaxValue(float maxValue)
00285 {
00286     if(maxValue<mMinValue )
00287         throw (InvalidValueRange);
00288     mMaxValue = maxValue;
00289     update();
00290 }
00291 
00292 void QcScaleItem::setMinDegree(float minDegree)
00293 {
00294     if(minDegree>mMaxDegree)
00295         throw (InvalidDegreeRange);
00296     mMinDegree = minDegree;
00297     update();
00298 }
00299 void QcScaleItem::setMaxDegree(float maxDegree)
00300 {
00301     if(maxDegree<mMinDegree)
00302         throw (InvalidDegreeRange);
00303     mMaxDegree = maxDegree;
00304     update();
00305 }
00306 
00310 
00311 QcBackgroundItem::QcBackgroundItem(QObject *parent) :
00312     QcItem(parent)
00313 {
00314     setPosition(88);
00315     mPen = Qt::NoPen;
00316     setPosition(88);
00317 
00318     addColor(0.4,Qt::darkGray);
00319     addColor(0.8,Qt::black);
00320 
00321 }
00322 
00323 
00324 void QcBackgroundItem::draw(QPainter* painter)
00325 {
00326     QRectF tmpRect = resetRect();
00327     painter->setBrush(Qt::NoBrush);
00328     QLinearGradient linearGrad(tmpRect.topLeft(), tmpRect.bottomRight());
00329     for(int i = 0;i<mColors.size();i++){
00330         linearGrad.setColorAt(mColors[i].first,mColors[i].second);
00331     }
00332     painter->setPen(mPen);
00333     painter->setBrush(linearGrad);
00334     painter->drawEllipse(adjustRect(position()));
00335 }
00336 
00337 void QcBackgroundItem::addColor(float position, const QColor &color)
00338 {
00339     if(position<0||position>1)
00340         return;
00341       QPair<float,QColor> pair;
00342       pair.first = position;
00343       pair.second = color;
00344       mColors.append(pair);
00345       update();
00346 }
00347 
00348 void QcBackgroundItem::clearrColors()
00349 {
00350     mColors.clear();
00351 }
00352 
00356 
00357 QcGlassItem::QcGlassItem(QObject *parent) :
00358     QcItem(parent)
00359 {
00360     setPosition(88);
00361 }
00362 
00363 void QcGlassItem::draw(QPainter *painter)
00364 {
00365     resetRect();
00366     QRectF tmpRect1 = adjustRect(position());
00367     QRectF tmpRect2 = tmpRect1;
00368     float r = getRadius(tmpRect1);
00369     tmpRect2.setHeight(r/2.0);
00370     painter->setPen(Qt::NoPen);
00371 
00372     QColor clr1 = Qt::gray ;
00373     QColor clr2 = Qt::white;
00374     clr1.setAlphaF(0.2);
00375     clr2.setAlphaF(0.4);
00376 
00377     QLinearGradient linearGrad1(tmpRect1.topLeft(), tmpRect1.bottomRight());
00378     linearGrad1.setColorAt(0.1, clr1);
00379     linearGrad1.setColorAt(0.5, clr2);
00380 
00381     painter->setBrush(linearGrad1);
00382     painter->drawPie(tmpRect1,0,16*180);
00383     tmpRect2.moveCenter(rect().center());
00384     painter->drawPie(tmpRect2,0,-16*180);
00385 }
00389 
00390 QcLabelItem::QcLabelItem(QObject *parent) :
00391     QcItem(parent)
00392 {
00393     setPosition(50);
00394     mAngle = 270;
00395     mText = "%";
00396     mColor = Qt::black;
00397 }
00398 
00399 void QcLabelItem::draw(QPainter *painter)
00400 {
00401     resetRect();
00402     QRectF tmpRect = adjustRect(position());
00403     float r = getRadius(rect());
00404     QFont font("Meiryo UI", r/10.0, QFont::Bold);
00405     painter->setFont(font);
00406     painter->setPen(QPen(mColor));
00407 
00408     QPointF txtCenter = getPoint(mAngle,tmpRect);
00409     QFontMetrics fMetrics = painter->fontMetrics();
00410     QSize sz = fMetrics.size( Qt::TextSingleLine, mText );
00411     QRectF txtRect(QPointF(0,0), sz );
00412     txtRect.moveCenter(txtCenter);
00413 
00414     painter->drawText( txtRect, Qt::TextSingleLine,mText );
00415 
00416 }
00417 
00418 void QcLabelItem::setAngle(float a)
00419 {
00420     mAngle = a;
00421     update();
00422 }
00423 
00424 float QcLabelItem::angle()
00425 {
00426     return mAngle;
00427 }
00428 
00429 void QcLabelItem::setText(const QString &text, bool repaint)
00430 {
00431     mText = text;
00432     if(repaint)
00433         update();
00434 }
00435 
00436 QString QcLabelItem::text()
00437 {
00438     return mText;
00439 }
00440 
00441 void QcLabelItem::setColor(const QColor &color)
00442 {
00443     mColor = color;
00444     update();
00445 }
00446 
00447 QColor QcLabelItem::color()
00448 {
00449     return mColor;
00450 }
00451 
00455 
00456 QcArcItem::QcArcItem(QObject *parent) :
00457     QcScaleItem(parent)
00458 {
00459     setPosition(80);
00460     mColor = Qt::black;
00461 }
00462 
00463 void QcArcItem::draw(QPainter *painter)
00464 {
00465     resetRect();
00466     QRectF tmpRect= adjustRect(position());
00467     float r = getRadius(tmpRect);
00468 
00469     QPen pen;
00470     pen.setColor(mColor);
00471     pen.setWidthF(r/40);
00472     painter->setPen(pen);
00473     painter->drawArc(tmpRect,-16*(mMinDegree+180),-16*(mMaxDegree-mMinDegree));
00474 }
00475 
00476 void QcArcItem::setColor(const QColor &color)
00477 {
00478     mColor = color;
00479 }
00483 
00484 QcColorBand::QcColorBand(QObject *parent) :
00485     QcScaleItem(parent)
00486 {
00487     QColor tmpColor;
00488     tmpColor.setAlphaF(0.1);
00489     QPair<QColor,float> pair;
00490 
00491     pair.first = Qt::green;
00492     pair.second = 10;
00493     mBandColors.append(pair);
00494 
00495     pair.first = Qt::darkGreen;
00496     pair.second = 50;
00497     mBandColors.append(pair);
00498 
00499     pair.first = Qt::red;
00500     pair.second = 100;
00501     mBandColors.append(pair);
00502 
00503     setPosition(50);
00504 }
00505 
00506 QPainterPath QcColorBand::createSubBand(float from, float sweep)
00507 {
00508     QRectF tmpRect = adjustRect(position());
00509     QPainterPath path;
00510     path.arcMoveTo(tmpRect,180+from);
00511     path.arcTo(tmpRect,180+from,-sweep);
00512     return path;
00513 }
00514 
00515 void QcColorBand::draw(QPainter *painter)
00516 {
00517     resetRect();
00518     float r = getRadius(rect());
00519     QPen pen;
00520     pen.setCapStyle(Qt::FlatCap);
00521     pen.setWidthF(r/20.0);
00522     painter->setBrush(Qt::NoBrush);
00523     float offset = getDegFromValue(mBandStartValue);
00524     for(int i = 0;i<mBandColors.size();i++){
00525         QColor clr = mBandColors[i].first;
00526         float sweep;
00527         if(i==0)
00528             sweep = getDegFromValue(mBandColors[i].second)-getDegFromValue(mMinValue);
00529         else
00530             sweep = getDegFromValue(mBandColors[i].second)-getDegFromValue(mBandColors[i-1].second);
00531         QPainterPath path = createSubBand(-offset,sweep);
00532         offset += sweep;
00533         pen.setColor(clr);
00534         painter->setPen(pen);
00535         painter->drawPath(path);
00536     }
00537 }
00538 void QcColorBand::setColors(const QList<QPair<QColor, float> > &colors)
00539 {
00540     mBandColors = colors;
00541     update();
00542 }
00543 
00547 
00548 QcDegreesItem::QcDegreesItem(QObject *parent) :
00549     QcScaleItem(parent)
00550 {
00551     mStep = 10;
00552     mColor = Qt::black;
00553     mSubDegree = false;
00554     setPosition(90);
00555 }
00556 
00557 
00558 void QcDegreesItem::draw(QPainter *painter)
00559 {
00560     resetRect();
00561     QRectF tmpRect = adjustRect(position());
00562 
00563     painter->setPen(mColor);
00564     float r = getRadius(tmpRect);
00565     for(float val = mMinValue;val<=mMaxValue;val+=mStep){
00566         float deg = getDegFromValue(val);
00567         QPointF pt = getPoint(deg,tmpRect);
00568         QPainterPath path;
00569         path.moveTo(pt);
00570         path.lineTo(tmpRect.center());
00571         pt = path.pointAtPercent(0.03);
00572         QPointF newPt = path.pointAtPercent(0.13);
00573 
00574         QPen pen;
00575         pen.setColor(mColor);
00576         if(!mSubDegree)
00577             pen.setWidthF(r/25.0);
00578 
00579         painter->setPen(pen);
00580         painter->drawLine(pt,newPt);
00581 
00582     }
00583 }
00584 
00585 void QcDegreesItem::setStep(float step)
00586 {
00587     mStep = step;
00588     update();
00589 }
00590 
00591 void QcDegreesItem::setColor(const QColor& color)
00592 {
00593     mColor = color;
00594     update();
00595 }
00596 
00597 void QcDegreesItem::setSubDegree(bool b)
00598 {
00599     mSubDegree = b;
00600     update();
00601 }
00602 
00606 
00607 QcNeedleItem::QcNeedleItem(QObject *parent) :
00608     QcScaleItem(parent)
00609 {
00610     mCurrentValue = 0;
00611     mColor = Qt::black;
00612     mLabel = NULL;
00613     mNeedleType = FeatherNeedle;
00614 }
00615 
00616 void QcNeedleItem::draw(QPainter *painter)
00617 {
00618     resetRect();
00619     QRectF tmpRect = adjustRect(position());
00620     painter->save();
00621     painter->translate(tmpRect.center());
00622     float deg = getDegFromValue( mCurrentValue);
00623     painter->rotate(deg+90.0);
00624     painter->setBrush(QBrush(mColor));
00625     painter->setPen(Qt::NoPen);
00626 
00627     QLinearGradient grad;
00628 
00629     switch (mNeedleType) {
00630     case QcNeedleItem::FeatherNeedle:
00631         createFeatherNeedle(getRadius(tmpRect));
00632         break;
00633     case QcNeedleItem::DiamonNeedle:
00634         createDiamonNeedle(getRadius(tmpRect));
00635         break;
00636     case QcNeedleItem::TriangleNeedle:
00637         createTriangleNeedle(getRadius(tmpRect));
00638         break;
00639     case QcNeedleItem::AttitudeMeterNeedle:
00640         createAttitudeNeedle(getRadius(tmpRect));
00641         break;
00642     case QcNeedleItem::CompassNeedle:
00643         createCompassNeedle(getRadius(tmpRect));
00644         grad.setStart(mNeedlePoly[0]);
00645         grad.setFinalStop(mNeedlePoly[1]);
00646         grad.setColorAt(0.9,Qt::red);
00647         grad.setColorAt(1,Qt::blue);
00648         painter->setBrush(grad);
00649 
00650         break;
00651 
00652     default:
00653         break;
00654     }
00655     painter->drawConvexPolygon(mNeedlePoly);
00656     painter->restore();
00657 }
00658 
00659 void QcNeedleItem::setCurrentValue(float value)
00660 {
00661        if(value<mMinValue)
00662         mCurrentValue = mMinValue;
00663     else if(value>mMaxValue)
00664         mCurrentValue = mMaxValue;
00665     else
00666         mCurrentValue = value;
00667 
00668     if(mLabel!=0)
00669         mLabel->setText(QString::number(mCurrentValue),false);
00670 
00672 //    if(mLabel!=0){
00673 //        QString currentValue;
00674 //        mLabel->setText( currentValue ,false);
00675 //        mLabel->setText(currentValue.sprintf(mFormat.toStdString().c_str(), mCurrentValue),false);
00676 //        Q_UNUSED(currentValue);
00677 //    }
00678     update();
00679 }
00680 
00681 float QcNeedleItem::currentValue()
00682 {
00683     return mCurrentValue;
00684 }
00685 
00686 void QcNeedleItem::setValueFormat(QString format){
00687     mFormat = format;
00688     update();
00689 }
00690 
00691 QString QcNeedleItem::currentValueFormat(){
00692     return mFormat;
00693 }
00694 
00695 void QcNeedleItem::setColor(const QColor &color)
00696 {
00697     mColor = color;
00698     update();
00699 }
00700 
00701 QColor QcNeedleItem::color()
00702 {
00703     return mColor;
00704 }
00705 
00706 void QcNeedleItem::setLabel(QcLabelItem *label)
00707 {
00708     mLabel = label;
00709     update();
00710 }
00711 
00712 QcLabelItem *QcNeedleItem::label()
00713 {
00714     return mLabel;
00715 }
00716 
00717 
00718 void QcNeedleItem::setNeedle(QcNeedleItem::NeedleType needleType)
00719 {
00720     mNeedleType = needleType;
00721     update();
00722 }
00723 
00724 
00725 void QcNeedleItem::createDiamonNeedle(float r)
00726 {
00727     QVector<QPointF> tmpPoints;
00728     tmpPoints.append(QPointF(0.0, 0.0));
00729     tmpPoints.append(QPointF(-r/20.0,r/20.0));
00730     tmpPoints.append(QPointF(0.0, r));
00731     tmpPoints.append(QPointF(r/20.0,r/20.0));
00732     mNeedlePoly = tmpPoints;
00733 }
00734 
00735 void QcNeedleItem::createTriangleNeedle(float r)
00736 {
00737     QVector<QPointF> tmpPoints;
00738     tmpPoints.append(QPointF(0.0, r));
00739     tmpPoints.append(QPointF(-r/40.0, 0.0));
00740     tmpPoints.append(QPointF(r/40.0,0.0));
00741     mNeedlePoly = tmpPoints;
00742 }
00743 
00744 void QcNeedleItem::createFeatherNeedle(float r)
00745 {
00746     QVector<QPointF> tmpPoints;
00747     tmpPoints.append(QPointF(0.0, r));
00748     tmpPoints.append(QPointF(-r/40.0, 0.0));
00749     tmpPoints.append(QPointF(-r/15.0, -r/5.0));
00750     tmpPoints.append(QPointF(r/15.0,-r/5));
00751     tmpPoints.append(QPointF(r/40.0,0.0));
00752     mNeedlePoly = tmpPoints;
00753 }
00754 
00755 void QcNeedleItem::createAttitudeNeedle(float r)
00756 {
00757     QVector<QPointF> tmpPoints;
00758     tmpPoints.append(QPointF(0.0, r));
00759     tmpPoints.append(QPointF(-r/20.0, 0.85*r));
00760     tmpPoints.append(QPointF(r/20.0,0.85*r));
00761     mNeedlePoly = tmpPoints;
00762 }
00763 
00764 void QcNeedleItem::createCompassNeedle(float r)
00765 {
00766     QVector<QPointF> tmpPoints;
00767     tmpPoints.append(QPointF(0.0, r));
00768     tmpPoints.append(QPointF(-r/15.0, 0.0));
00769     tmpPoints.append(QPointF(0.0, -r));
00770     tmpPoints.append(QPointF(r/15.0,0.0));
00771     mNeedlePoly = tmpPoints;
00772 }
00773 
00777 
00778 QcValuesItem::QcValuesItem(QObject *parent) :
00779     QcScaleItem(parent)
00780 {
00781     setPosition(70);
00782     mColor = Qt::black;
00783     mStep = 10;
00784 }
00785 
00786 
00787 void QcValuesItem::draw(QPainter*painter)
00788 {
00789     QRectF  tmpRect = resetRect();
00790     float r = getRadius(adjustRect(99));
00791     QFont font("Meiryo UI",0, QFont::Bold);
00792     font.setPointSizeF(0.08*r);
00793 
00794     painter->setFont(font);
00795     painter->setPen(mColor);
00796     for(float val = mMinValue;val<=mMaxValue;val+=mStep){
00797         float deg = getDegFromValue(val);
00798         QPointF pt = getPoint(deg,tmpRect);
00799         QPainterPath path;
00800         path.moveTo(pt);
00801         path.lineTo(    tmpRect.center());
00802         QString strVal = QString::number(val);
00803         QFontMetrics fMetrics = painter->fontMetrics();
00804         QSize sz = fMetrics.size( Qt::TextSingleLine, strVal );
00805         QRectF txtRect(QPointF(0,0), sz );
00806         QPointF textCenter = path.pointAtPercent(1.0-position()/100.0);
00807         txtRect.moveCenter(textCenter);
00808 
00809         painter->drawText( txtRect, Qt::TextSingleLine, strVal );
00810     }
00811 }
00812 
00813 void QcValuesItem::setStep(float step)
00814 {
00815     mStep = step;
00816 }
00817 
00818 
00819 void QcValuesItem::setColor(const QColor& color)
00820 {
00821     mColor = color;
00822 }
00823 
00827 
00828 QcAttitudeMeter::QcAttitudeMeter(QObject *parent) :
00829     QcItem(parent)
00830 {
00831     mPitch = 0;
00832     mRoll = 0;
00833 }
00834 
00835 void QcAttitudeMeter::setCurrentPitch(float pitch)
00836 {
00837     mPitch=-pitch;
00838     update();
00839 }
00840 
00841 void QcAttitudeMeter::setCurrentRoll(float roll)
00842 {
00843     mRoll = roll;
00844     update();
00845 }
00846 
00847 QPointF QcAttitudeMeter::getIntersection(float r, const QPointF &pitchPoint, const QPointF &pt)
00848 {
00849     // refrence it to zero
00850 
00851     Q_UNUSED(r)
00852     float a = (pitchPoint.y()-pt.y())/(pitchPoint.x()-pt.x());
00853     float b = pt.y()-a*pt.x();
00854     return QPointF(0,a*0+b);
00855 }
00856 
00857 float QcAttitudeMeter::getStartAngle(const QRectF& tmpRect)
00858 {
00859     float r = getRadius(tmpRect);
00860     QPointF pt1 = getPoint(mRoll,tmpRect);
00861     pt1.setY(pt1.y()-mPitchOffset);
00862     QPointF pitchPoint = QPointF(tmpRect.center().x(),tmpRect.center().y()-mPitchOffset);
00863 
00864 
00866     QPainterPath path1;
00867     path1.moveTo(pitchPoint);
00868     path1.lineTo(getIntersection(r,pitchPoint,pt1)+QPointF(0,5));
00869     path1.lineTo(getIntersection(r,pitchPoint,pt1)+QPointF(0,-5));
00870 
00871     QPainterPath path2;
00872     path2.addEllipse(tmpRect);
00873 
00874     QPointF p = path1.intersected(path2).pointAtPercent(.5);
00875     return getAngle(p,tmpRect);
00876 }
00877 
00878 void QcAttitudeMeter::draw(QPainter *painter)
00879 {
00880     resetRect();
00881     QRectF tmpRect = adjustRect(position());
00882     float r = getRadius(tmpRect);
00883     if(mPitch<0)
00884         mPitchOffset = 0.0135*r*mPitch;
00885     else
00886         mPitchOffset = 0.015*r*mPitch;
00887 
00888     painter->setPen(Qt::NoPen);
00889     drawUpperEllipse(painter,tmpRect);
00890     drawLowerEllipse(painter,tmpRect);
00891 
00892     // Steps
00893 
00894     drawPitchSteps(painter,tmpRect);
00895     drawHandle(painter);
00896 
00897     drawDegrees(painter);
00898 
00899 }
00900 
00901 void QcAttitudeMeter::drawDegrees(QPainter *painter)
00902 {
00903     resetRect();
00904     QRectF tmpRect = adjustRect(position());
00905     float r = getRadius(tmpRect);
00906     QPen pen;
00907 
00908     pen.setColor(Qt::white);
00909     painter->setPen(pen);
00910     for(int deg = 60;deg<=120;deg+=10){
00911         if(deg == 90)
00912             continue;
00913         drawDegree(painter,tmpRect,deg);
00914     }
00915 
00916     pen.setWidthF(r/30.0);
00917     painter->setPen(pen);
00918     drawDegree(painter,tmpRect,0);
00919     drawDegree(painter,tmpRect,90);
00920     drawDegree(painter,tmpRect,180);
00921     drawDegree(painter,tmpRect,30);
00922     drawDegree(painter,tmpRect,150);
00923 }
00924 
00925 
00926 void QcAttitudeMeter::drawDegree(QPainter * painter, const QRectF& tmpRect,float deg)
00927 {
00928     QPointF pt1 = getPoint(deg,tmpRect);
00929     QPointF pt2 = tmpRect.center();
00930     QPainterPath path;
00931     path.moveTo(pt1);
00932     path.lineTo(pt2);
00933     QPointF pt = path.pointAtPercent(0.1);
00934     painter->drawLine(pt1,pt);
00935 }
00936 
00937 
00938 void QcAttitudeMeter::drawUpperEllipse(QPainter *painter, const QRectF &tmpRect)
00939 {
00940 
00941     QLinearGradient radialGrad1(tmpRect.topLeft(),tmpRect.bottomRight());
00942     QColor clr1 = Qt::blue;
00943     clr1.setAlphaF(0.5);
00944     QColor clr2 = Qt::darkBlue;
00945     clr2.setAlphaF(0.5);
00946     radialGrad1.setColorAt(0, clr1);
00947     radialGrad1.setColorAt(.8, clr2);
00948 
00949 
00950     float offset = getStartAngle(tmpRect);
00951     float startAngle = 180-offset;
00952     float endAngle = offset-2*mRoll;
00953     float span =endAngle-startAngle;
00954 
00955     painter->setBrush(radialGrad1);
00956     painter->drawChord(tmpRect,16*startAngle,16*span);
00957 
00958 }
00959 
00960 
00961 void QcAttitudeMeter::drawLowerEllipse(QPainter *painter, const QRectF &tmpRect)
00962 {
00963     QLinearGradient radialGrad2(tmpRect.topLeft(),tmpRect.bottomRight());
00964     QColor clr1 = QColor(139,119,118);
00965     QColor clr2 = QColor(139,119,101);
00966     radialGrad2.setColorAt(0, clr1);
00967     radialGrad2.setColorAt(.8, clr2);
00968 
00969     float offset = getStartAngle(tmpRect);
00970     float startAngle = 180+offset;
00971     float endAngle = offset-2*mRoll;
00972     float span =endAngle+startAngle;
00973 
00974     painter->setPen(Qt::NoPen);
00975     painter->setBrush(radialGrad2);
00976     painter->drawChord(tmpRect,-16*startAngle,16*span);
00977 
00978 }
00979 
00980 void QcAttitudeMeter::drawPitchSteps(QPainter *painter, const QRectF &tmpRect)
00981 {
00982     float r = getRadius(tmpRect);
00983     QPointF center = tmpRect.center();
00984     painter->save();
00985     painter->translate(center.x(),center.y()-mPitchOffset);
00986     painter->rotate(mRoll);
00987     QPen pen;
00988     pen.setColor(Qt::white);
00989     pen.setWidthF(r/40.0);
00990 
00991     painter->setPen(pen);
00992     for (int i = -30;i<=30;i+=10){
00993         QPointF pt1;
00994         pt1.setX(-0.01*r*abs(i));
00995         pt1.setY(r/70.0*i);
00996         QPointF pt2;
00997         pt2.setX(0.01*r*abs(i));
00998         pt2.setY(r/70.0*i);
00999         painter->drawLine(pt1,pt2);
01000 
01001         if(i==0)
01002             continue;
01003 
01004         // draw value
01005         QFont font("Meiryo UI",0, QFont::Bold);
01006         font.setPointSizeF(0.08*r);
01007         painter->setFont(font);
01008         QString strVal = QString::number(abs(i));
01009         QFontMetrics fMetrics = painter->fontMetrics();
01010         QSize sz = fMetrics.size( Qt::TextSingleLine, strVal );
01011         QRectF leftTxtRect(QPointF(0,0), sz );
01012         QRectF rightTxtRect(QPointF(0,0), sz );
01013         leftTxtRect.moveCenter(pt1-QPointF(0.1*r,0));
01014         rightTxtRect.moveCenter(pt2+QPointF(0.1*r,0));
01015         painter->drawText( leftTxtRect, Qt::TextSingleLine, strVal );
01016         painter->drawText( rightTxtRect, Qt::TextSingleLine, strVal );
01017     }
01018     painter->restore();
01019 }
01020 
01021 void QcAttitudeMeter::drawHandle(QPainter *painter)
01022 {
01023     QRectF tmpRct = adjustRect(15);
01024     float r = getRadius(tmpRct);
01025     QPen pen;
01026     pen.setColor(Qt::gray);
01027     pen.setWidthF(0.25*r);
01028     painter->setPen(pen);
01029     painter->drawArc(tmpRct,0,-16*180);
01030 
01031     QPointF center = tmpRct.center();
01032     QPointF leftPt1 = center;
01033     QPointF leftPt2 = center;
01034     QPointF rightPt1 = center;
01035     QPointF rightPt2 = center;
01036     leftPt1.setX(center.x()-2*r);
01037     leftPt2.setX(center.x()-r);
01038     rightPt1.setX(center.x()+2*r);
01039     rightPt2.setX(center.x()+r);
01040     painter->drawLine(leftPt1,leftPt2);
01041     painter->drawLine(rightPt1,rightPt2);
01042     painter->drawEllipse(adjustRect(2));
01043 
01044     //
01045     QPointF pt1 = center;
01046     QPointF pt2 = center;
01047     // to get the real 100 % radius, without recomputing
01048     pt1.setY(center.y()+r);
01049     pt2.setY(center.y()+4*r);
01050     pen.setColor(Qt::gray);
01051     painter->setPen(pen);
01052     painter->drawLine(pt1,pt2);
01053 
01054     // trapezium
01055     painter->setPen(Qt::gray);
01056     painter->setBrush(Qt::gray);
01057     QPolygonF trapPoly;
01058     QPointF tmpPt = center;
01059     tmpPt.setX(center.x()-r);
01060     tmpPt.setY(center.y()+4*r);
01061     trapPoly.append(tmpPt);
01062     tmpRct = adjustRect(position());
01063     trapPoly.append(getPoint(290,tmpRct));
01064     trapPoly.append(getPoint(250,tmpRct));
01065     tmpPt = center;
01066     tmpPt.setX(center.x()+r);
01067     tmpPt.setY(center.y()+4*r);
01068     trapPoly.append(tmpPt);
01069     painter->drawPolygon(trapPoly);
01070     painter->drawChord(tmpRct,-16*70,-16*40);
01071 }
01072 


gauges
Author(s):
autogenerated on Sat Jun 8 2019 19:36:20