|
1 /*************************************************************************** |
|
2 * Copyright (C) 2006-2008 by Tomasz Ziobrowski * |
|
3 * http://www.3electrons.com * |
|
4 * e-mail: t.ziobrowski@3electrons.com * |
|
5 * * |
|
6 * * |
|
7 * This program is free software; you can redistribute it and/or modify * |
|
8 * it under the terms of the GNU General Public License as published by * |
|
9 * the Free Software Foundation; either version 2 of the License, or * |
|
10 * (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 General Public License for more details. * |
|
16 * * |
|
17 * You should have received a copy of the GNU General Public License * |
|
18 * along with this program; if not, write to the * |
|
19 * Free Software Foundation, Inc., * |
|
20 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
|
21 ***************************************************************************/ |
|
22 |
|
23 #include <QtGui> |
|
24 #include <cmath> |
|
25 #include <assert.h> |
|
26 |
|
27 #include "manometer.h" |
|
28 #define PI 3.141592653589793238512808959406186204433 |
|
29 |
|
30 using namespace Qt; |
|
31 ManoMeter::ManoMeter(QWidget *parent) |
|
32 : AbstractMeter(parent) |
|
33 { |
|
34 m_max=300.0; |
|
35 m_min=0.0; |
|
36 |
|
37 m_maximum=300.0; // First we set manualy to call calcMaxMin |
|
38 setMinimum(0.0); |
|
39 calcMaxMin(); // Extend max and min to have nice step values |
|
40 |
|
41 setValue(0.0); |
|
42 setNominal(80.0); |
|
43 setCritical(220.0); |
|
44 setValueOffset(-100.0); |
|
45 setDigitOffset(105.0); |
|
46 setSuffix(QString(" [bar]")); |
|
47 m_digitFont.setPointSize(20); |
|
48 m_valueFont.setPointSize(25); |
|
49 |
|
50 // QWidget |
|
51 setSizePolicy (QSizePolicy::Expanding, QSizePolicy::Expanding); |
|
52 setWindowTitle(tr("Analog Barmeter")); |
|
53 resize(311, 311); |
|
54 assert(m_max-m_min != 0); |
|
55 } |
|
56 |
|
57 |
|
58 |
|
59 void ManoMeter::initCoordinateSystem(QPainter & painter) |
|
60 { |
|
61 int side = qMin(width(), height()); |
|
62 // painter initialization |
|
63 painter.setRenderHint(QPainter::Antialiasing); |
|
64 painter.translate(width() / 2, height() / 2); |
|
65 painter.scale(side / 335.0, side / 335.0); |
|
66 } |
|
67 |
|
68 void ManoMeter::paintBackground(QPainter & painter) |
|
69 { |
|
70 static const int scaleTriangle[6] = { -6,141,6,141,0,129 }; |
|
71 initCoordinateSystem(painter); |
|
72 |
|
73 // Painting Malowanie obwiedni tarczy. Biała tarcza z czarną skalą |
|
74 QPen Pen(QColor(0,0,0)); Pen.setWidth(4); |
|
75 painter.setPen(Pen); |
|
76 |
|
77 QRadialGradient back1(QPointF(0.0,0.0),180.0,QPointF(-35.0,145.0)); |
|
78 back1.setColorAt(0.0,QColor(250,250,250)); |
|
79 back1.setColorAt(1.0,QColor(20,20,20)); |
|
80 |
|
81 QRadialGradient back2(QPointF(0.0,0.0),225.0,QPointF(76.5,135.0)); |
|
82 back2.setColorAt(0.0,QColor(10,10,10)); |
|
83 back2.setColorAt(1.0,QColor(250,250,250)); |
|
84 |
|
85 painter.setBrush(QBrush(back1)); |
|
86 painter.drawEllipse(-162,-162,324,324); |
|
87 painter.setPen(Qt::NoPen); |
|
88 painter.setBrush(QBrush(back2)); |
|
89 painter.drawEllipse(-152,-152,304,304); |
|
90 |
|
91 QRadialGradient shield(QPointF(0,0),182,QPointF(-12.0,-15.0)); |
|
92 shield.setColorAt(0.0,Qt::white); |
|
93 shield.setColorAt(0.5,QColor(240,240,240)); |
|
94 shield.setColorAt(1.0,QColor(215,215,215)); |
|
95 |
|
96 |
|
97 // internal scale circle |
|
98 painter.setBrush(QBrush(shield)); |
|
99 painter.setPen(Pen); |
|
100 painter.drawEllipse(-142,-142,284,284); |
|
101 |
|
102 painter.setPen(Qt::NoPen); |
|
103 // nominal |
|
104 painter.setBrush(QColor(0,200,0)); |
|
105 assert(m_max-m_min != 0); |
|
106 int angle = static_cast<int>( (3840 * ( m_nominal - m_min ))/(m_max-m_min) ); |
|
107 if (m_min <= m_nominal && m_nominal < m_max ) |
|
108 painter.drawPie(QRect(-141,-141,282,282),-480,3840 - angle % 5760 ); |
|
109 // Critical |
|
110 |
|
111 painter.setBrush(QBrush(Qt::red)); |
|
112 angle = static_cast<int>( (3840 * ( m_critical - m_min ))/(m_max-m_min) ); |
|
113 if ( m_min <= m_critical && m_critical < m_max ) |
|
114 painter.drawPie(QRect(-141,-141,282,282),-480, 3840 - angle % 5760 ); //-480, 3840*( m_max-m_min - critical()-abs(m_min) )/static_cast<double>(m_max-m_min)); |
|
115 // biała obwiednia |
|
116 painter.setBrush(QBrush(shield)); |
|
117 painter.drawEllipse(-129,-129,258,258); |
|
118 |
|
119 // Ustawienie się na początku skali |
|
120 |
|
121 painter.rotate(60.0); |
|
122 |
|
123 // Rysowanie skali kreski |
|
124 painter.save(); |
|
125 painter.setBrush(QBrush(Qt::black)); |
|
126 int line_length=10; |
|
127 for (int i=0;i<33;i++) |
|
128 { |
|
129 painter.setPen(Pen); |
|
130 |
|
131 if (i % 4) painter.drawLine(0,140,0,140-line_length); |
|
132 else { |
|
133 painter.setPen(Qt::NoPen); |
|
134 painter.drawConvexPolygon(QPolygon(3, scaleTriangle)); |
|
135 } |
|
136 |
|
137 painter.rotate(7.5); |
|
138 |
|
139 Pen.setWidth(3); |
|
140 |
|
141 if (i % 2) line_length=10; |
|
142 else line_length=5; |
|
143 } |
|
144 painter.restore(); |
|
145 |
|
146 // Rysowanie skali liczby . |
|
147 |
|
148 if (true || digitOffset()) |
|
149 { |
|
150 painter.setPen(Qt::black); |
|
151 painter.rotate(-60.0); |
|
152 painter.setFont(digitFont()); |
|
153 for (int i=0;i<9;i++) |
|
154 { |
|
155 double v = m_min + i*(m_max - m_min)/8.0; |
|
156 if (fabs(v) < 0.000001 ) v = 0.0; |
|
157 QString val = QString("%1").arg(v); |
|
158 QSize Size = painter.fontMetrics().size(Qt::TextSingleLine, val); |
|
159 painter.save(); |
|
160 painter.translate( digitOffset() * cos((5+i)*PI/6.0), digitOffset() * sin((5+i)*PI/6.0)); |
|
161 painter.drawText( QPointF( Size.width()/ -2.0, Size.height() / 4.0), val); |
|
162 painter.restore(); |
|
163 } |
|
164 } |
|
165 |
|
166 |
|
167 |
|
168 }// paintBackground |
|
169 |
|
170 void ManoMeter::paintEvent(QPaintEvent * ) |
|
171 { |
|
172 drawBackground(); |
|
173 QPainter painter(this); |
|
174 initCoordinateSystem(painter); |
|
175 // --------------------------------------------- /// |
|
176 static const int hand[12] = {-4, 0, -1, 129, 1, 129, 4, 0, 8,-50, -8,-50}; |
|
177 |
|
178 |
|
179 QPainterPath hand_path; |
|
180 hand_path.moveTo(QPointF(hand[0],hand[1])); |
|
181 |
|
182 for (int i=2;i<10;i+=2) |
|
183 hand_path.lineTo(hand[i],hand[i+1]); |
|
184 |
|
185 hand_path.cubicTo ( 8.1,-51.0, 5.0,-48.0, 0.0,-48.0); |
|
186 hand_path.cubicTo( -5.0,-48.0, -8.1,-51.0, -8.0,-50.0); |
|
187 |
|
188 // Rysowanie wskazówki |
|
189 painter.save(); |
|
190 painter.rotate(60.0); |
|
191 painter.setPen(Qt::NoPen); |
|
192 painter.setBrush(QBrush(Qt::black)); |
|
193 painter.rotate( (( value()-m_min) * 240.0) / static_cast<double> (m_max - m_min) ); |
|
194 |
|
195 painter.drawPath(hand_path); |
|
196 |
|
197 painter.drawEllipse(-10,-10,20,20); |
|
198 |
|
199 |
|
200 painter.restore();// Przywrocenie do wychylenia o 60 stopni |
|
201 |
|
202 // Rysowanie wyświetlanej wartości |
|
203 if (valueOffset()) |
|
204 { |
|
205 |
|
206 if (value() >= critical() ) painter.setPen(Qt::red); |
|
207 painter.setFont(valueFont()); |
|
208 QString Str = prefix() + QString("%1").arg(value()) + suffix(); |
|
209 QSize Size = painter.fontMetrics().size(Qt::TextSingleLine, Str); |
|
210 painter.drawText( QPointF( Size.width() / -2.0,static_cast<int>( 0 - valueOffset())) , Str); |
|
211 } |
|
212 }// paintEvent |