Sh3/graphicslineitem.cpp
2025-07-02 07:45:34 +03:00

240 lines
4.4 KiB
C++
Raw Permalink Blame History

#include "graphicslineitem.h"
graphicsLineItem::graphicsLineItem(QObject *parent)
{
x0 = x1 = y0 = y1 = wid = 0;
drawCircles = 0;
col = QColor(Qt::black);
}
QRectF graphicsLineItem::boundingRect() const
{
QPointF p1;
if (x0>x1) {
if (y0>y1) p1 = QPointF(x1, y1);
else p1 = QPointF(x1, y0);
}
else
{
if (y0>y1) p1 = QPointF(x0, y1);
else p1 = QPointF(x0, y0);
}
QSizeF s = QSizeF(fabs(x0-x1), fabs(y0-y1));
return QRectF(p1, s);
}
void graphicsLineItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QPen p;
QFont f = painter->font();
p.setColor(col);
p.setWidthF(wid);
p.setCapStyle(Qt::FlatCap);
painter->setPen(p);
painter->setBrush(QBrush(Qt::SolidPattern));
painter->drawLine(QPointF(x0,y0), QPointF(x1, y1));
if (drawCircles&0x1) painter->drawEllipse(QPointF(x0,y0), wid*1.5, wid*1.5);
if (drawCircles&0x2) painter->drawEllipse(QPointF(x1,y1), wid*1.5, wid*1.5);
painter->save();
for (int i=0; i<2; i++)
{
if (! str[i].txt.isEmpty())
{
p.setColor(str[i].col);
f.setPointSizeF(str[i].size);
painter->setFont(f);
painter->setPen(p);
painter->drawText(str[i].pos, str[i].txt);
}
}
painter->restore();
}
void graphicsLineItem::setCircles(bool f1, bool f2)
{
int ret = 0x0;
if (f1) ret+=0x1;
if (f2) ret+=0x2;
drawCircles=ret;
}
void graphicsLineItem::setColor(QColor c)
{
if (col!=c)
{
prepareGeometryChange();
col = c;
}
}
QPointF graphicsLineItem::pos0() {return QPointF(x0,y0);}
QPointF graphicsLineItem::pos1() {return QPointF(x1,y1);}
void graphicsLineItem::setPos0(QPointF p)
{
qreal x, y;
x= p.x(); y= p.y();
if ((x0!=x)||(y0!=y) ) {
prepareGeometryChange();
x0=x; y0=y;
}
}
void graphicsLineItem::setPos1(QPointF p)
{
qreal x, y;
x= p.x(); y= p.y();
if ((x1!=x)||(y1!=y) ) {
prepareGeometryChange();
x1=x; y1=y;
}
}
qreal graphicsLineItem::length()
{
QLineF lf = QLineF(x0,y0,x1,y1);
return lf.length();
}
void graphicsLineItem::setLength(qreal l)
{
QLineF lf = QLineF(x0,y0,x1,y1);
lf.setLength(l);
QPointF p = lf.p2();
if ((x1!=p.x())||(y1!=p.y())) {
prepareGeometryChange();
x1=p.x(); y1=p.y();
}
}
void graphicsLineItem::setAngle(qreal a)
{
QLineF lf = QLineF(x0,y0,x1,y1);
lf.setAngle(a);
QPointF p = lf.p2();
if ((x1!=p.x())||(y1!=p.y())) {
prepareGeometryChange();
x1=p.x(); y1=p.y();
}
}
void graphicsLineItem::setGlobalAngle(qreal a) // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> 0,0
{
QLineF lf, lf1;
lf = QLineF(0,0,x0,y0);
lf.setAngle(a);
QPointF p = lf.p2();
lf1 = QLineF(0,0,x1,y1);
lf1.setAngle(a);
QPointF p1 = lf1.p2();
if ((x0!=p.x())||(y0!=p.y())||(x1!=p1.x())||(y1!=p1.y())) {
prepareGeometryChange();
x0=p.x(); y0=p.y();
x1=p1.x(); y1=p1.y();
}
}
void graphicsLineItem::setWidth(qreal w)
{
if (wid!=w) {
prepareGeometryChange();
wid = w;
}
}
void graphicsLineItem::setText(QString s, int d)
{
if ((d==0)||(d==1))
if (str[d].txt!=s)
{
prepareGeometryChange();
str[d].txt = s;
}
}
void graphicsLineItem::setTextPos(QPointF p, int d)
{
if ((d==0)||(d==1))
if (str[d].pos!=p) {
prepareGeometryChange();
str[d].pos = p;
}
}
void graphicsLineItem::setTextColor(QColor c, int d)
{
if ((d==0)||(d==1))
if (str[d].col!=c) {
prepareGeometryChange();
str[d].col = c;
}
}
QString graphicsLineItem::getText(int d)
{
if ((d==0)||(d==1)) return str[d].txt;
else return 0;
}
QColor graphicsLineItem::getTextColor(int d)
{
if ((d==0)||(d==1)) return str[d].col;
else return QColor(Qt::black);
}
QPointF graphicsLineItem::getTextPos(int d)
{
if ((d==0)||(d==1)) return str[d].pos;
else return QPointF(0,0);
}
qreal graphicsLineItem::getTextSize(int d)
{
if ((d==0)||(d==1))
return str[d].size;
else return 0;
}
void graphicsLineItem::setTextSize(qreal s, int d)
{
if ((d==0)||(d==1))
if (str[d].size!=s) {
prepareGeometryChange();
str[d].size = s;
}
}