3 #include "common/util.h"
17 Ticks(double minValue
, double maxValue
, int maxCount
);
19 int count() const {return ((int)((_max
- _min
) / _d
)) + 1;}
20 double val(int i
) const {return _min
+ i
* _d
;}
21 double min() const {return _min
;}
22 double max() const {return _max
;}
30 Ticks::Ticks(double minValue
, double maxValue
, int maxCount
)
32 double range
= Util::niceNum(maxValue
- minValue
, false);
33 _d
= Util::niceNum(range
/ maxCount
, false);
34 _min
= ceil(minValue
/ _d
) * _d
;
35 _max
= floor(maxValue
/ _d
) * _d
;
39 AxisItem::AxisItem(Type type
, QGraphicsItem
*parent
)
40 : QGraphicsItem(parent
), _locale(QLocale::system())
46 _font
.setPixelSize(FONT_SIZE
);
47 _font
.setFamily(FONT_FAMILY
);
50 void AxisItem::setRange(const RangeF
&range
)
52 prepareGeometryChange();
55 QFontMetrics
fm(_font
);
56 Ticks
ticks(_range
.min(), _range
.max(),
57 (_type
== X
) ? XTICKS
* _zoom
: YTICKS
* _zoom
);
58 _ticks
= QVector
<Tick
>(ticks
.count());
60 for (int i
= 0; i
< ticks
.count(); i
++) {
62 t
.value
= ticks
.val(i
);
63 t
.boundingBox
= fm
.tightBoundingRect(_locale
.toString(t
.value
));
70 void AxisItem::setSize(qreal size
)
72 prepareGeometryChange();
78 void AxisItem::updateBoundingRect()
80 QFontMetrics
fm(_font
);
81 QRect es
= _ticks
.isEmpty() ? QRect() : _ticks
.last().boundingBox
;
82 QRect ss
= _ticks
.isEmpty() ? QRect() : _ticks
.first().boundingBox
;
85 _boundingRect
= QRectF(-ss
.width()/2, -TICK
/2, _size
+ es
.width()/2
86 + ss
.width()/2, es
.height() - 2*fm
.descent() + TICK
+ 2*PADDING
);
89 for (int i
= 0; i
< _ticks
.count(); i
++)
90 mtw
= qMax(_ticks
.at(i
).boundingBox
.width(), mtw
);
91 _boundingRect
= QRectF(-(mtw
+ 2*PADDING
+ TICK
/2 - fm
.descent()),
92 -(_size
+ es
.height()/2 + fm
.descent()), mtw
+ 2*PADDING
93 + TICK
- fm
.descent(), _size
+ es
.height()/2 + fm
.descent()
98 void AxisItem::paint(QPainter
*painter
, const QStyleOptionGraphicsItem
*option
,
105 painter
->setRenderHint(QPainter::Antialiasing
, false);
106 painter
->setFont(_font
);
107 QPen
pen(painter
->pen());
108 pen
.setWidth(AXIS_WIDTH
);
109 painter
->setPen(pen
);
112 painter
->drawLine(0, 0, _size
, 0);
114 for (int i
= 0; i
< _ticks
.count(); i
++) {
115 qreal val
= _ticks
.at(i
).value
;
116 ts
= _ticks
.at(i
).boundingBox
;
118 painter
->drawLine((_size
/_range
.size()) * (val
- _range
.min()),
119 TICK
/2, (_size
/_range
.size()) * (val
- _range
.min()), -TICK
/2);
120 painter
->drawText(((_size
/_range
.size()) * (val
- _range
.min()))
121 - (ts
.width()/2), ts
.height() + TICK
/2 + PADDING
,
122 _locale
.toString(val
));
125 painter
->drawLine(0, 0, 0, -_size
);
128 for (int i
= 0; i
< _ticks
.count(); i
++) {
129 qreal val
= _ticks
.at(i
).value
;
130 ts
= _ticks
.at(i
).boundingBox
;
131 mtw
= qMax(ts
.width(), mtw
);
133 painter
->drawLine(TICK
/2, -((_size
/_range
.size())
134 * (val
- _range
.min())), -TICK
/2, -((_size
/_range
.size())
135 * (val
- _range
.min())));
136 painter
->drawText(-(ts
.width() + PADDING
+ TICK
/2),
137 -((_size
/_range
.size()) * (val
- _range
.min())) + (ts
.height()/2),
138 _locale
.toString(val
));
142 //painter->setPen(Qt::red);
143 //painter->drawRect(boundingRect());
146 QSizeF
AxisItem::margin() const
148 QFontMetrics
fm(_font
);
149 QRect es
= _ticks
.isEmpty() ? QRect() : _ticks
.last().boundingBox
;
152 return QSizeF(es
.width()/2, es
.height() - 2*fm
.descent() + TICK
/2
156 for (int i
= 0; i
< _ticks
.count(); i
++)
157 mtw
= qMax(_ticks
.at(i
).boundingBox
.width(), mtw
);
159 return QSizeF(mtw
+ 2*PADDING
+ TICK
/2 - fm
.descent(),
160 es
.height()/2 + fm
.descent());
164 QList
<qreal
> AxisItem::ticks() const
168 for (int i
= 0; i
< _ticks
.count(); i
++)
169 list
.append(((_size
/_range
.size()) * (_ticks
.at(i
).value