#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();
}
#include <QtGui>
#include <cmath>
class GridScene : public QGraphicsScene
{
public:
GridScene(qreal x, qreal y, qreal w, qreal h)
: QGraphicsScene(x, y, w, h)
{ }
protected:
void drawBackground(QPainter *painter, const QRectF &rect)
{
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)
{
QPainterPath path;
path.addEllipse(elipseRect);
return path;
}
}
class Node : public QGraphicsItem
{
public:
Node(QPointF pos ,QGraphicsItem *par = 0, QGraphicsScene *sc = 0)
: QGraphicsItem(par,sc),
elipseRect(-3, -3, 2*3, 2*3),
elipseShape(constructNodeShape(elipseRect))
{
setPos(pos);
setFlags(0);
setAcceptedMouseButtons(0);
}
void paint(QPainter* p,const QStyleOptionGraphicsItem *, QWidget *)
{
p->setPen(Qt::darkRed);
p->setBrush(Qt::NoBrush);
p->setOpacity(1.0);
p->drawEllipse(elipseRect);
}
QPainterPath shape() const
{
return elipseShape;
}
QRectF boundingRect() const
{
return elipseRect;
}
protected:
const QRect elipseRect;
const QPainterPath elipseShape;
};
class Resistor : public QGraphicsItem
{
public:
Resistor(QGraphicsItem *par = 0, QGraphicsScene *scene = 0)
: QGraphicsItem(par,scene),
resistorBox(-18, -9, 36, 18),
connectors(new QLine[2])
{
setFlags(ItemIsMovable | ItemIsSelectable | ItemIsFocusable);
// comment the following 4 lines to see the performance difference
QGraphicsTextItem *t = new QGraphicsTextItem("R1 = 100k",this,scene);
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;
}
void paint(QPainter *p, const QStyleOptionGraphicsItem *o, QWidget *)
{
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);
}
QRectF boundingRect() const
{
return boundingBox;
}
private:
QRect resistorBox;
QLine* connectors;
QRectF boundingBox;
};
int main(int argc,char *argv[])
{
QApplication app(argc,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);
}
QGraphicsView *view = new QGraphicsView(&scene);
// Turn of antialiasing and the color problem is gone...
view->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
view->setDragMode(QGraphicsView::RubberBandDrag);
view->show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks