#include <QtGui>
#include <cmath>
{
public:
GridScene(qreal x, qreal y, qreal w, qreal h)
{ }
protected:
{
const int gridSize = 25;
const int realLeft = static_cast<int>(std::floor(rect.left()));
const int realRight = static_cast<int>(std::ceil(rect.right()));
const int realTop = static_cast<int>(std::floor(rect.top()));
const int realBottom = static_cast<int>(std::ceil(rect.bottom()));
const int firstLeftGridLine = realLeft - (realLeft % gridSize);
const int firstTopGridLine = realTop - (realTop % gridSize);
QVarLengthArray<QLine, 100> lines;
for (int x = firstLeftGridLine; x <= realRight; x += gridSize)
lines.
append(QLine(x, realTop, x, realBottom
));
for (int y = firstTopGridLine; y <= realBottom; y += gridSize)
lines.
append(QLine(realLeft, y, realRight, y
));
//If you uncomment this, you will find the color problem lessened...
//why? well your guess is as good as mine ;-)
// painter->fillRect(QRect(QPoint(realLeft, realTop), QPoint(realRight, realBottom)), QBrush(Qt::white));
painter->setOpacity(1.0);
painter
->setPen
(QPen(Qt
::darkGreen,
0));
painter->drawLines(lines.data(), lines.size());
}
};
namespace {
static inline QPainterPath constructNodeShape
(const QRect
& elipseRect
) {
path.addEllipse(elipseRect);
return path;
}
}
{
public:
elipseRect(-3, -3, 2*3, 2*3),
elipseShape(constructNodeShape(elipseRect))
{
setPos(pos);
setFlags(0);
setAcceptedMouseButtons(0);
}
{
p->setPen(Qt::darkRed);
p->setBrush(Qt::NoBrush);
p->setOpacity(1.0);
p->drawEllipse(elipseRect);
}
{
return elipseShape;
}
{
return elipseRect;
}
protected:
};
{
public:
resistorBox(-18, -9, 36, 18),
{
setFlags(ItemIsMovable | ItemIsSelectable | ItemIsFocusable);
// comment the following 4 lines to see the performance difference
t->setPos(0,-35);
new Node
(QPointF(-30,
0),
this,scene
);
new Node
(QPointF(30,
0),
this,scene
);
connectors
[0] = QLine(-27,
0,
-18,
0);
connectors
[1] = QLine(18,
0,
27,
0);
boundingBox |= resistorBox;
boundingBox |
= QRectF(connectors
[0].
x1(), connectors
[0].
y1(), connectors
[0].
x2(), connectors
[0].
y2() );
boundingBox |
= QRectF(connectors
[1].
x1(), connectors
[1].
y1(), connectors
[1].
x2(), connectors
[1].
y2() );
boundingBox.adjust(-0.5, -0.5, 0.5, 0.5);
}
~Resistor()
{
delete[] connectors;
}
{
if(!(o
->state
& QStyle::State_Open)) p->setPen(Qt::darkBlue);
if(o
->state
& QStyle::State_Selected) p
->setPen
(QPen(Qt
::darkGray,
1));
p->setBrush(Qt::NoBrush);
p->drawRect(resistorBox);
p->drawLines(connectors, 2);
}
{
return boundingBox;
}
private:
};
int main(int argc,char *argv[])
{
GridScene scene(0,0,1024,768);
for(int j = 2; j < 4; ++j)
for(int i = 1; i <11; ++i)
{
Resistor *r = new Resistor(0,&scene);
r->setPos(j*100, i * 50);
}
// Turn of antialiasing and the color problem is gone...
view->show();
return app.exec();
}