File indexing completed on 2024-05-19 05:51:03

0001 /***************************************************************************
0002 **                                                                        **
0003 **  QcGauge, for instrumentation, and real time data measurement          **
0004 **  visualization widget for Qt.                                          **
0005 **  Copyright (C) 2015 Hadj Tahar Berrima                                 **
0006 **                                                                        **
0007 **  This program is free software: you can redistribute it and/or modify  **
0008 **  it under the terms of the GNU Lesser General Public License as        **
0009 **  published by the Free Software Foundation, either version 3 of the    **
0010 **  License, or (at your option) any later version.                       **
0011 **                                                                        **
0012 **  This program is distributed in the hope that it will be useful,       **
0013 **  but WITHOUT ANY WARRANTY; without even the implied warranty of        **
0014 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         **
0015 **  GNU Lesser General Public License for more details.                   **
0016 **                                                                        **
0017 **  You should have received a copy of the GNU Lesser General Public      **
0018 **  License along with this program.                                      **
0019 **  If not, see http://www.gnu.org/licenses/.                             **
0020 **                                                                        **
0021 ****************************************************************************
0022 **           Author:  Hadj Tahar Berrima                                  **
0023 **           Website: http://pytricity.com/                               **
0024 **           Contact: berrima_tahar@yahoo.com                             **
0025 **           Date:    1 dec 2014                                          **
0026 **           Version:  1.0                                                **
0027 ****************************************************************************
0028 **           Modified: Chris Rizzitello 2019                              **
0029 **           Website: http://atelier.kde.org                              **
0030 **           Contact: rizzitello@kde.org                                  **
0031 **           Version:  Atelier-Custom                                     **
0032 ****************************************************************************/
0033 #pragma once
0034 
0035 #include <QObject>
0036 #include <QPainter>
0037 #include <QRectF>
0038 #include <QWidget>
0039 #include <QtMath>
0040 
0041 #if defined(QCGAUGE_COMPILE_LIBRARY)
0042 #define QCGAUGE_DECL Q_DECL_EXPORT
0043 #elif defined(QCGAUGE_USE_LIBRARY)
0044 #define QCGAUGE_DECL Q_DECL_IMPORT
0045 #else
0046 #define QCGAUGE_DECL
0047 #endif
0048 
0049 class QcGaugeWidget;
0050 class QcItem;
0051 class QcBackgroundItem;
0052 class QcDegreesItem;
0053 class QcValuesItem;
0054 class QcArcItem;
0055 class QcNeedleItem;
0056 class QcLabelItem;
0057 
0058 ///////////////////////////////////////////////////////////////////////////////////////////
0059 ///////////////////////////////////////////////////////////////////////////////////////////
0060 ///////////////////////////////////////////////////////////////////////////////////////////
0061 class QCGAUGE_DECL QcGaugeWidget : public QWidget
0062 {
0063     Q_OBJECT
0064 public:
0065     explicit QcGaugeWidget(QWidget *parent = nullptr);
0066     ~QcGaugeWidget() override = default;
0067 
0068     QcBackgroundItem *addBackground(float position);
0069     QcDegreesItem *addDegrees(float position);
0070     QcValuesItem *addValues(float position);
0071     QcArcItem *addArc(float position);
0072     QcNeedleItem *addNeedle(float position);
0073     QcLabelItem *addLabel(float position);
0074 
0075     void addItem(QcItem *item, float position);
0076     int removeItem(QcItem *item);
0077     QList<QcItem *> items();
0078 
0079 private:
0080     QList<QcItem *> mItems;
0081 
0082 protected:
0083     void paintEvent(QPaintEvent *) override;
0084 };
0085 
0086 ///////////////////////////////////////////////////////////////////////////////////////////
0087 ///////////////////////////////////////////////////////////////////////////////////////////
0088 ///////////////////////////////////////////////////////////////////////////////////////////
0089 
0090 class QCGAUGE_DECL QcItem : public QObject
0091 {
0092     Q_OBJECT
0093 public:
0094     explicit QcItem(QObject *parent = nullptr);
0095     virtual void draw(QPainter *) = 0;
0096     virtual int type();
0097 
0098     void setPosition(float percentage);
0099     float position();
0100     QRectF rect();
0101     enum Error { InvalidValueRange, InvalidDegreeRange, InvalidStep };
0102 
0103 protected:
0104     QRectF adjustRect(float percentage);
0105     float getRadius(const QRectF &);
0106     float getAngle(const QPointF &, const QRectF &tmpRect);
0107     QPointF getPoint(float deg, const QRectF &tmpRect);
0108     QRectF resetRect();
0109     void update();
0110 
0111 private:
0112     QRectF mRect;
0113     QWidget *parentWidget;
0114     float mPosition;
0115 };
0116 ///////////////////////////////////////////////////////////////////////////////////////////
0117 ///////////////////////////////////////////////////////////////////////////////////////////
0118 ///////////////////////////////////////////////////////////////////////////////////////////
0119 
0120 class QCGAUGE_DECL QcScaleItem : public QcItem
0121 {
0122     Q_OBJECT
0123 public:
0124     explicit QcScaleItem(QObject *parent = nullptr);
0125 
0126     void setValueRange(float minValue, float maxValue);
0127     void setDgereeRange(float minDegree, float maxDegree);
0128     void setMinValue(float minValue);
0129     void setMaxValue(float maxValue);
0130     void setMinDegree(float minDegree);
0131     void setMaxDegree(float maxDegree);
0132     float minValue();
0133     float maxValue();
0134     float minDegree();
0135     float maxDegree();
0136 
0137 protected:
0138     float getDegFromValue(float);
0139 
0140 private:
0141     float mMinValue;
0142     float mMaxValue;
0143     float mMinDegree;
0144     float mMaxDegree;
0145 };
0146 ///////////////////////////////////////////////////////////////////////////////////////////
0147 ///////////////////////////////////////////////////////////////////////////////////////////
0148 ///////////////////////////////////////////////////////////////////////////////////////////
0149 
0150 class QCGAUGE_DECL QcBackgroundItem : public QcItem
0151 {
0152     Q_OBJECT
0153 public:
0154     explicit QcBackgroundItem(QObject *parent = nullptr);
0155     void draw(QPainter *) override;
0156     void addColor(float position, const QColor &color);
0157     void clearrColors();
0158 
0159 private:
0160     QPen mPen;
0161     QList<QPair<float, QColor>> mColors;
0162     QLinearGradient mLinearGrad;
0163 };
0164 
0165 ///////////////////////////////////////////////////////////////////////////////////////////
0166 ///////////////////////////////////////////////////////////////////////////////////////////
0167 ///////////////////////////////////////////////////////////////////////////////////////////
0168 
0169 class QCGAUGE_DECL QcLabelItem : public QcItem
0170 {
0171     Q_OBJECT
0172 public:
0173     explicit QcLabelItem(QObject *parent = nullptr);
0174     void draw(QPainter *) override;
0175     void setAngle(float);
0176     float angle();
0177     void setFont(const QFont &font);
0178     void setText(const QString &text, bool repaint = true);
0179     QString text();
0180     void setColor(const QColor &color);
0181     QColor color();
0182 
0183 private:
0184     float mAngle;
0185     QString mText;
0186     QColor mColor;
0187     QFont mFont;
0188 };
0189 
0190 ///////////////////////////////////////////////////////////////////////////////////////////
0191 ///////////////////////////////////////////////////////////////////////////////////////////
0192 ///////////////////////////////////////////////////////////////////////////////////////////
0193 
0194 class QCGAUGE_DECL QcArcItem : public QcScaleItem
0195 {
0196     Q_OBJECT
0197 public:
0198     explicit QcArcItem(QObject *parent = nullptr);
0199     void draw(QPainter *) override;
0200     void setColor(const QColor &color);
0201 
0202 private:
0203     QColor mColor;
0204 
0205 signals:
0206 
0207 public slots:
0208 };
0209 ///////////////////////////////////////////////////////////////////////////////////////////
0210 ///////////////////////////////////////////////////////////////////////////////////////////
0211 ///////////////////////////////////////////////////////////////////////////////////////////
0212 
0213 class QCGAUGE_DECL QcDegreesItem : public QcScaleItem
0214 {
0215     Q_OBJECT
0216 public:
0217     explicit QcDegreesItem(QObject *parent = nullptr);
0218     void draw(QPainter *painter) override;
0219     void setStep(float step);
0220     void setColor(const QColor &color);
0221     void setSubDegree(bool);
0222 
0223 private:
0224     float mStep;
0225     QColor mColor;
0226     bool mSubDegree;
0227 };
0228 ///////////////////////////////////////////////////////////////////////////////////////////
0229 ///////////////////////////////////////////////////////////////////////////////////////////
0230 ///////////////////////////////////////////////////////////////////////////////////////////
0231 
0232 class QCGAUGE_DECL QcNeedleItem : public QcScaleItem
0233 {
0234     Q_OBJECT
0235 public:
0236     explicit QcNeedleItem(QObject *parent = nullptr);
0237     void draw(QPainter *) override;
0238     void setCurrentValue(float value);
0239     float currentValue();
0240     void setColor(const QColor &color);
0241     QColor color();
0242 
0243     void setLabel(QcLabelItem *);
0244     QcLabelItem *label();
0245 
0246 private:
0247     void createDiamonNeedle(float r);
0248     QPolygonF mNeedlePoly;
0249     float mCurrentValue;
0250     QColor mColor;
0251     QcLabelItem *mLabel;
0252 };
0253 ///////////////////////////////////////////////////////////////////////////////////////////
0254 ///////////////////////////////////////////////////////////////////////////////////////////
0255 ///////////////////////////////////////////////////////////////////////////////////////////
0256 
0257 class QCGAUGE_DECL QcValuesItem : public QcScaleItem
0258 {
0259     Q_OBJECT
0260 public:
0261     explicit QcValuesItem(QObject *parent = nullptr);
0262     void draw(QPainter *) override;
0263     void setFont(const QFont &font);
0264     void setStep(float step);
0265     void setColor(const QColor &color);
0266 
0267 private:
0268     float mStep {10};
0269     QFont mFont;
0270     QColor mColor;
0271 };