1 #include "gaugelabel.h"
5 GaugeLabel::GaugeLabel(QGraphicsItem *parent) :
11 void GaugeLabel::paint(QPainter *painter,const QStyleOptionGraphicsItem *option,
14 drawNumLabel(painter);
16 //////////////////////////////////////////////////////////////////////////
17 void GaugeLabel::drawNumLabel(QPainter *painter)
21 int vsplit,hsplit,uw,base;
24 int h=size().height();
28 painter->setTransform(QTransform().translate(scenePos().x(),scenePos().y()).scale(m_scale,m_scale));
29 // qDebug() <<"drawnumlabel=" << m_name << " x=" << scenePos().x() << " y=" << scenePos().y() << " h=" << h << " w=" << w;
30 QFontMetrics fm(m_font);
31 painter->setFont(m_font);
34 int lIncr=(lDigits&1?10:1)*(lDigits&2?100:1)*(lDigits&4?10000:1);
35 lWidth=fm.width(QString().number(lIncr-1));
40 ldo=lDigits>1?ld%10:0;
44 utext=QString("%1").arg(ud,3);
49 float lo=(fmod(m_value,lIncr/10)/lIncr)*fh*10;
50 vsplit=(h-fm.ascent()-8)/2;
51 base=fm.ascent()+vsplit;
52 drawFrame(painter, m_style,hsplit,vsplit);
53 // qDebug("val=%f ld=%d ldo=%d lo=%f h=%d lDigits=%d lIncr=%d\n",m_value,ld,ldo,lo,h,lDigits,lIncr);
54 if((ld*10/lIncr)==9) {
55 ud=(val+lIncr/2)/(lIncr);
56 painter->drawText(w-hsplit- uw, base+lo, QString("%1").arg(abs(ud-1),3));
57 painter->drawText(w-hsplit- uw, base-fh+lo,QString("%1").arg(abs(ud ),3) );
58 } else painter->drawText(w-hsplit- uw, base,utext );
59 painter->drawText(w-hsplit, base +2*fh+lo, QString("%1").arg(abs(ld-ldo-2*lIncr/10+lIncr)%lIncr,lDigits,10,QLatin1Char('0')));
60 painter->drawText(w-hsplit, base +fh +lo, QString("%1").arg(abs(ld-ldo- lIncr/10+lIncr)%lIncr,lDigits,10,QLatin1Char('0')));
61 painter->drawText(w-hsplit, base +lo, QString("%1").arg(abs(ld-ldo),lDigits,10,QLatin1Char('0')));
62 painter->drawText(w-hsplit, base -fh +lo ,QString("%1").arg(abs((ld-ldo+ lIncr/10)%lIncr),lDigits,10,QLatin1Char('0')));
63 painter->drawText(w-hsplit, base -2*fh+lo ,QString("%1").arg(abs((ld-ldo+2*lIncr/10)%lIncr),lDigits,10,QLatin1Char('0')));
67 sprintf(text, "%3d", val);
68 painter->drawText(7,base + fh , text);
69 // drawLabel(painter, text,m_style);
71 painter->restore(); // Restore and remove clicping rectabgle
74 ///////////////////////////////////////////////////////////////////////////
75 void GaugeLabel::drawFrame(QPainter *painter,int style,int hsplit,int vsplit)
77 int textlen = 0, i = 0;
80 int height=size().height();
81 int width=size().width();
83 // qDebug() << "height=" << height << " width=" << width << " hsplit=" << hsplit << " vsplit=" << vsplit;
85 QVector<QPoint> nboxv;
91 if (style&7 == LEFT) xplace+=5;
93 nboxv.push_back(QPoint(xplace,yplace+vsplit));
98 nboxv.push_back(QPoint(xplace+(width/2)-5,yplace ));
99 nboxv.push_back(QPoint(xplace+(width/2) ,yplace-5));
100 nboxv.push_back(QPoint(xplace+(width/2)+5,yplace ));
104 nboxv.push_back(QPoint(xplace+width-hsplit,yplace+vsplit));
105 nboxv.push_back(QPoint(xplace+width-hsplit,yplace));
106 nboxv.push_back(QPoint(xplace+width,yplace));
108 nboxv.push_back(QPoint(xplace+width,yplace ));
111 if (style&7== RIGHT) {
113 nboxv.push_back(QPoint(xplace+width ,yplace+(height/2)-5));
114 nboxv.push_back(QPoint(xplace+width+5,yplace+(height/2) ));
115 nboxv.push_back(QPoint(xplace+width ,yplace+(height/2)+5));
119 nboxv.push_back(QPoint(xplace+width,yplace+height));
120 nboxv.push_back(QPoint(xplace+width-hsplit,yplace+height));
121 nboxv.push_back(QPoint(xplace+width-hsplit,yplace+height-vsplit));
123 nboxv.push_back(QPoint(xplace,yplace+height-vsplit));
125 if (style&7== DOWN) {
126 nboxv.push_back(QPoint(xplace+(width/2)-5,yplace+height));
127 nboxv.push_back(QPoint(xplace+(width/2) ,yplace+height+5));
128 nboxv.push_back(QPoint(xplace+(width/2)+5,yplace+height));
131 nboxv.push_back(QPoint(xplace,yplace+height-vsplit));
133 if (style&7 == LEFT) {
134 nboxv.push_back(QPoint(xplace ,yplace+(height/2)+5));
135 nboxv.push_back(QPoint(xplace-5,yplace+(height/2) ));
136 nboxv.push_back(QPoint(xplace ,yplace+(height/2)-5));
139 nboxv.push_back(QPoint(xplace,yplace+vsplit));
141 // pdrawable->draw_rectangle(pwidget->get_style()->get_black_gc(),
142 // true, xplace, yplace, width, height);
146 painter->setClipRect(QRect(xplace+width-hsplit,yplace, width+10, height+1));
147 painter->setClipRect(QRect(xplace-5,yplace+vsplit, width+10, height-2*vsplit+1),Qt::UniteClip);
148 // painter->drawRect(QRect(xplace-5,yplace, width+10, height));
151 // QFont tapeFont("Helvetica [Cronyx]", 18);
152 // painter->setFont(tapeFont);
153 // painter->setPen(pen);
154 QBrush brush(m_backColor);
155 painter->setBrush(brush);
157 painter->drawPolygon(nboxv);
159 painter->drawLines(nboxv);
160 /* QPen redpen(Qt::red);
161 painter->setPen(redpen);
163 foreach (QPoint p,nboxv) {
165 painter->drawText(p, QString().number(ii++));