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


gauges
Author(s): alexvs
autogenerated on Mon Nov 14 2016 03:33:07