#include <QtGui>
#include <cmath>
const int NodeRadius = 3;
inline qreal distance(const QPointF& p1, const QPointF& p2)
{
qreal dx = p1.x() - p2.x();
qreal dy = p1.y() - p2.y();
return std::sqrt((dx*dx)+(dy*dy));
}
{
public:
enum { Type = UserType + 1 };
{
setAcceptedMouseButtons(0);
setFlags(0);
}
{
return QRectF(-NodeRadius,
-NodeRadius,
2*NodeRadius,
2*NodeRadius
);
}
{
Q_UNUSED(o);
Q_UNUSED(w);
p
->setRenderHints
(QPainter::Antialiasing);
p->setPen(Qt::darkRed);
p->drawEllipse(boundingRect());
}
bool contains(const QPointF& pt) const
{
qreal dist
= distance
(QPointF(0,
0),pt
);
return (((dist * dist) - (NodeRadius*NodeRadius)) <= 0);
}
{
Node *port = qgraphicsitem_cast<Node*>(other);
if(!port)
qreal dist = distance(pos(),port->pos());
return (dist <= (2 * NodeRadius));
}
{
path.addEllipse(boundingRect());
return path;
}
int type() const
{
return Type;
}
};
namespace SeparateNodes
{
{
public:
{
setFlags(ItemIsMovable | ItemIsSelectable | ItemIsFocusable);
Node *n = new Node(0,scene);
n->setPos(-27.0,0.0);
nodes << n;
n = new Node(0,scene);
n->setPos(27.0,0.0);
nodes << n;
}
{
}
{
Q_UNUSED(o);
Q_UNUSED(w);
p->setPen(Qt::darkMagenta);
p->drawLine(-18, -9, 18, -9);
p->drawLine( 18, -9, 18, 9);
p->drawLine( 18, 9,-18, 9);
p->drawLine(-18, 9,-18, -9);
p->drawLine(-27, 0,-18, 0);
p->drawLine( 18, 0, 27, 0);
if( o
->state
& QStyle::State_Selected) {
p->setPen(Qt::darkGray);
p->drawRect(boundingRect());
}
}
{
if (change == ItemPositionChange && scene()) {
foreach(Node *n, nodes)
n->moveBy(delta.x(), delta.y());
}
}
QList<Node*> nodes;
};
}
namespace NodesAsChild
{
{
public:
{
setFlags(ItemIsMovable | ItemIsSelectable | ItemIsFocusable);
Node *n = new Node(this,scene);
n->setPos(-27.0,0.0);
n = new Node(this,scene);
n->setPos(27.0,0.0);
}
{
}
{
Q_UNUSED(o);
Q_UNUSED(w);
p->setPen(Qt::darkBlue);
p->drawLine(-18, -9, 18, -9);
p->drawLine( 18, -9, 18, 9);
p->drawLine( 18, 9,-18, 9);
p->drawLine(-18, 9,-18, -9);
p->drawLine(-27, 0,-18, 0);
p->drawLine( 18, 0, 27, 0);
if( o
->state
& QStyle::State_Selected) {
p->setPen(Qt::darkGray);
p->drawRect(boundingRect());
}
}
};
}
namespace NodesPartOfResistor
{
{
public:
{
setFlags(ItemIsMovable | ItemIsSelectable | ItemIsFocusable);
}
{
}
{
Q_UNUSED(o);
Q_UNUSED(w);
p->setPen(Qt::darkGreen);
p->drawLine(-18, -9, 18, -9);
p->drawLine( 18, -9, 18, 9);
p->drawLine( 18, 9,-18, 9);
p->drawLine(-18, 9,-18, -9);
p->drawLine(-27, 0,-18, 0);
p->drawLine( 18, 0, 27, 0);
p->setPen(Qt::darkRed);
p
->setRenderHints
(QPainter::Antialiasing);
p->drawEllipse(-27-NodeRadius,-NodeRadius,2*NodeRadius,2*NodeRadius);
p->drawEllipse(27-NodeRadius,-NodeRadius,2*NodeRadius,2*NodeRadius);
if( o
->state
& QStyle::State_Selected) {
p->setPen(Qt::darkGray);
p->drawRect(boundingRect());
}
}
};
}
int main(int argc, char *argv[])
{
view->setAcceptDrops(true);
//view->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
const int factor = 68;
for(int i = 1; i < 11; i++)
for(int j=1;j<3;j++) {
SeparateNodes::Resistor *r = new SeparateNodes::Resistor(0,scene);
r->setPos(100+i*factor,j*100);
}
for(int i = 1; i < 11; i++)
for(int j=1;j<3;j++) {
NodesAsChild::Resistor *r = new NodesAsChild::Resistor(0,scene);
r->setPos(100+i*factor,200+j*100);
}
for(int i = 1; i < 11; i++)
for(int j=1;j<3;j++) {
NodesPartOfResistor::Resistor *r = new NodesPartOfResistor::Resistor(0,scene);
r->setPos(100+i*factor,400+j*100);
}
view->show();
return app.exec();
}
#include <QtGui>
#include <cmath>
const int NodeRadius = 3;
inline qreal distance(const QPointF& p1, const QPointF& p2)
{
qreal dx = p1.x() - p2.x();
qreal dy = p1.y() - p2.y();
return std::sqrt((dx*dx)+(dy*dy));
}
class Node : public QGraphicsItem
{
public:
enum { Type = UserType + 1 };
Node(QGraphicsItem *parent=0, QGraphicsScene *scene=0) : QGraphicsItem(parent,scene)
{
setAcceptedMouseButtons(0);
setFlags(0);
}
QRectF boundingRect() const
{
return QRectF(-NodeRadius, -NodeRadius, 2*NodeRadius, 2*NodeRadius);
}
void paint(QPainter *p, const QStyleOptionGraphicsItem *o, QWidget *w = 0)
{
Q_UNUSED(o);
Q_UNUSED(w);
p->setRenderHints(QPainter::Antialiasing);
p->setPen(Qt::darkRed);
p->drawEllipse(boundingRect());
}
bool contains(const QPointF& pt) const
{
qreal dist = distance(QPointF(0,0),pt);
return (((dist * dist) - (NodeRadius*NodeRadius)) <= 0);
}
bool collidesWithItem(QGraphicsItem *other) const
{
Node *port = qgraphicsitem_cast<Node*>(other);
if(!port)
return QGraphicsItem::collidesWithItem(other);
qreal dist = distance(pos(),port->pos());
return (dist <= (2 * NodeRadius));
}
QPainterPath shape() const
{
QPainterPath path;
path.addEllipse(boundingRect());
return path;
}
int type() const
{
return Type;
}
};
namespace SeparateNodes
{
class Resistor : public QGraphicsItem
{
public:
Resistor(QGraphicsItem *parent = 0, QGraphicsScene *scene=0) : QGraphicsItem(parent,scene)
{
setFlags(ItemIsMovable | ItemIsSelectable | ItemIsFocusable);
Node *n = new Node(0,scene);
n->setPos(-27.0,0.0);
nodes << n;
n = new Node(0,scene);
n->setPos(27.0,0.0);
nodes << n;
}
QRectF boundingRect() const
{
return QRectF(-27,-9,54,18);
}
void paint(QPainter *p, const QStyleOptionGraphicsItem *o, QWidget *w = 0)
{
Q_UNUSED(o);
Q_UNUSED(w);
p->setPen(Qt::darkMagenta);
p->drawLine(-18, -9, 18, -9);
p->drawLine( 18, -9, 18, 9);
p->drawLine( 18, 9,-18, 9);
p->drawLine(-18, 9,-18, -9);
p->drawLine(-27, 0,-18, 0);
p->drawLine( 18, 0, 27, 0);
if( o->state & QStyle::State_Selected)
{
p->setPen(Qt::darkGray);
p->drawRect(boundingRect());
}
}
QVariant itemChange(GraphicsItemChange change, const QVariant &value)
{
if (change == ItemPositionChange && scene()) {
QPointF newPos = value.toPointF();
QPointF delta = newPos - pos();
foreach(Node *n, nodes)
n->moveBy(delta.x(), delta.y());
}
return QGraphicsItem::itemChange(change, value);
}
QList<Node*> nodes;
};
}
namespace NodesAsChild
{
class Resistor : public QGraphicsItem
{
public:
Resistor(QGraphicsItem *parent = 0, QGraphicsScene *scene=0) : QGraphicsItem(parent,scene)
{
setFlags(ItemIsMovable | ItemIsSelectable | ItemIsFocusable);
Node *n = new Node(this,scene);
n->setPos(-27.0,0.0);
n = new Node(this,scene);
n->setPos(27.0,0.0);
}
QRectF boundingRect() const
{
return QRectF(-27,-9,54,18);
}
void paint(QPainter *p, const QStyleOptionGraphicsItem *o, QWidget *w = 0)
{
Q_UNUSED(o);
Q_UNUSED(w);
p->setPen(Qt::darkBlue);
p->drawLine(-18, -9, 18, -9);
p->drawLine( 18, -9, 18, 9);
p->drawLine( 18, 9,-18, 9);
p->drawLine(-18, 9,-18, -9);
p->drawLine(-27, 0,-18, 0);
p->drawLine( 18, 0, 27, 0);
if( o->state & QStyle::State_Selected)
{
p->setPen(Qt::darkGray);
p->drawRect(boundingRect());
}
}
};
}
namespace NodesPartOfResistor
{
class Resistor : public QGraphicsItem
{
public:
Resistor(QGraphicsItem *parent = 0, QGraphicsScene *scene=0) : QGraphicsItem(parent,scene)
{
setFlags(ItemIsMovable | ItemIsSelectable | ItemIsFocusable);
}
QRectF boundingRect() const
{
return QRectF(-30,-12,60,24);
}
void paint(QPainter *p, const QStyleOptionGraphicsItem *o, QWidget *w = 0)
{
Q_UNUSED(o);
Q_UNUSED(w);
p->setPen(Qt::darkGreen);
p->drawLine(-18, -9, 18, -9);
p->drawLine( 18, -9, 18, 9);
p->drawLine( 18, 9,-18, 9);
p->drawLine(-18, 9,-18, -9);
p->drawLine(-27, 0,-18, 0);
p->drawLine( 18, 0, 27, 0);
p->setPen(Qt::darkRed);
p->setRenderHints(QPainter::Antialiasing);
p->drawEllipse(-27-NodeRadius,-NodeRadius,2*NodeRadius,2*NodeRadius);
p->drawEllipse(27-NodeRadius,-NodeRadius,2*NodeRadius,2*NodeRadius);
if( o->state & QStyle::State_Selected)
{
p->setPen(Qt::darkGray);
p->drawRect(boundingRect());
}
}
};
}
int main(int argc, char *argv[])
{
QApplication app(argc,argv);
QGraphicsScene *scene = new QGraphicsScene(0.,0.,1024,768);
QGraphicsView *view = new QGraphicsView(scene);
view->setDragMode(QGraphicsView::RubberBandDrag);
view->setAcceptDrops(true);
//view->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
const int factor = 68;
for(int i = 1; i < 11; i++)
for(int j=1;j<3;j++) {
SeparateNodes::Resistor *r = new SeparateNodes::Resistor(0,scene);
r->setPos(100+i*factor,j*100);
}
for(int i = 1; i < 11; i++)
for(int j=1;j<3;j++) {
NodesAsChild::Resistor *r = new NodesAsChild::Resistor(0,scene);
r->setPos(100+i*factor,200+j*100);
}
for(int i = 1; i < 11; i++)
for(int j=1;j<3;j++) {
NodesPartOfResistor::Resistor *r = new NodesPartOfResistor::Resistor(0,scene);
r->setPos(100+i*factor,400+j*100);
}
view->show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks