Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor drawing of elements: hide logic behind common interface #694

Closed
161 changes: 58 additions & 103 deletions qucs/components/component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,14 @@ void Component::paint(ViewPainter *p) {
int x, y, a, b, xb, yb;
QFont f = p->Painter->font(); // save current font
QFont newFont = f;
const bool correctSimulator = (Simulator & QucsSettings.DefaultSimulator) == QucsSettings.DefaultSimulator;
if (Model.at(0) == '.') { // is simulation component (dc, ac, ...)
newFont.setPointSizeF(p->Scale * Texts.first()->Size);
newFont.setWeight(QFont::DemiBold);
p->Painter->setFont(newFont);
p->map(cx, cy, x, y);

if ((Simulator & QucsSettings.DefaultSimulator) == QucsSettings.DefaultSimulator) {
if (correctSimulator) {
if ((Model == ".CUSTOMSIM") || (Model == ".DISTO")
|| (Model == ".NOISE") || (Model == ".PZ") ||
(Model == ".SENS") || (Model == ".SENS_AC") ||
Expand Down Expand Up @@ -238,75 +239,38 @@ void Component::paint(ViewPainter *p) {
p->Painter->drawLine(x + xb - 1, y, a + xb, b);
} else { // normal components go here

// paint all lines
for (qucs::Line *p1: Lines) {
if ((Simulator & QucsSettings.DefaultSimulator) == QucsSettings.DefaultSimulator) {
p->Painter->setPen(p1->style);
} else {
p->Painter->setPen(WrongSimulatorPen);
}
p->drawLine(cx + p1->x1, cy + p1->y1, cx + p1->x2, cy + p1->y2);
auto draw_primitive = [&](qucs::DrawingPrimitive* prim, ViewPainter* p) {
p->Painter->setPen(correctSimulator ? prim->penHint() : WrongSimulatorPen);
p->Painter->setBrush(prim->brushHint());
prim->draw(p, cx, cy);
};

for (qucs::DrawingPrimitive *line: Lines) {
draw_primitive(line, p);
}

// paint all arcs
for (qucs::Arc *p3: Arcs) {
if ((Simulator & QucsSettings.DefaultSimulator) == QucsSettings.DefaultSimulator) {
p->Painter->setPen(p3->style);
} else {
p->Painter->setPen(WrongSimulatorPen);
}
p->drawArc(cx + p3->x, cy + p3->y, p3->w, p3->h, p3->angle, p3->arclen);
for (qucs::DrawingPrimitive *arc: Arcs) {
draw_primitive(arc, p);
}

// paint all rectangles
for (qucs::Area *pa: Rects) {
if ((Simulator & QucsSettings.DefaultSimulator) == QucsSettings.DefaultSimulator) {
p->Painter->setPen(pa->Pen);
} else {
p->Painter->setPen(WrongSimulatorPen);
}
p->Painter->setBrush(pa->Brush);
p->drawRect(cx + pa->x, cy + pa->y, pa->w, pa->h);
for (qucs::DrawingPrimitive *rect: Rects) {
draw_primitive(rect, p);
}

// paint all ellipses
for (qucs::Area *pa: Ellips) {
if ((Simulator & QucsSettings.DefaultSimulator) == QucsSettings.DefaultSimulator) {
p->Painter->setPen(pa->Pen);
} else {
p->Painter->setPen(WrongSimulatorPen);
}
p->Painter->setBrush(pa->Brush);
p->drawEllipse(cx + pa->x, cy + pa->y, pa->w, pa->h);
for (qucs::DrawingPrimitive *ellips: Ellipses) {
draw_primitive(ellips, p);
}
p->Painter->setBrush(Qt::NoBrush);

newFont.setWeight(QFont::Light);

// keep track of painter state
p->Painter->save();

QTransform wm = p->Painter->worldTransform();
// write all text
for (Text *pt: Texts) {
p->Painter->setWorldTransform(
QTransform(pt->mCos, -pt->mSin, pt->mSin, pt->mCos,
p->DX + float(cx + pt->x) * p->Scale,
p->DY + float(cy + pt->y) * p->Scale));
newFont.setPointSizeF(p->Scale * pt->Size);
newFont.setOverline(pt->over);
newFont.setUnderline(pt->under);
p->Painter->setFont(newFont);
if ((Simulator & QucsSettings.DefaultSimulator) == QucsSettings.DefaultSimulator) {
p->Painter->setPen(pt->Color);
} else {
p->Painter->setPen(WrongSimulatorPen);
}
int w, h;
w = p->drawTextMapped(pt->s, 0, 0, &h);
Q_UNUSED(w)
Q_UNUSED(h)

for (qucs::DrawingPrimitive *text: Texts) {
draw_primitive(text, p);
}

p->Painter->setWorldTransform(wm);
p->Painter->setWorldMatrixEnabled(false);

Expand Down Expand Up @@ -373,50 +337,41 @@ void Component::paintIcon(QPixmap *pixmap)
p->drawEllipse(cx+pp->x-2,cy+pp->y-2,4,4);
}

for (qucs::Line *p1: Lines) {
p1->style.setWidth(3);
p->Painter->setPen(p1->style);
p->drawLine(cx + p1->x1, cy + p1->y1, cx + p1->x2, cy + p1->y2);
for (qucs::DrawingPrimitive *line: Lines) {
auto pen = line->penHint();
pen.setWidth(3);
p->Painter->setPen(pen);
line->draw(p, cx, cy);
}

// paint all arcs
for (qucs::Arc *p3: Arcs) {
p3->style.setWidth(3);
p->Painter->setPen(p3->style);
p->drawArc(cx + p3->x, cy + p3->y, p3->w, p3->h, p3->angle, p3->arclen);
for (qucs::DrawingPrimitive *arc: Arcs) {
auto pen = arc->penHint();
pen.setWidth(3);
p->Painter->setPen(pen);
arc->draw(p, cx, cy);
}

// paint all rectangles
for (qucs::Area *pa: Rects) {
pa->Pen.setWidth(3);
p->Painter->setPen(pa->Pen);
p->Painter->setBrush(pa->Brush);
p->drawRect(cx + pa->x, cy + pa->y, pa->w, pa->h);
for (qucs::DrawingPrimitive *rect: Rects) {
auto pen = rect->penHint();
pen.setWidth(3);
p->Painter->setPen(pen);
p->Painter->setBrush(rect->brushHint());
rect->draw(p, cx, cy);
}

// paint all ellipses
for (qucs::Area *pa: Ellips) {
p->Painter->setPen(pa->Pen);
p->Painter->setBrush(pa->Brush);
p->drawEllipse(cx + pa->x, cy + pa->y, pa->w, pa->h);
for (qucs::DrawingPrimitive *ellipse: Ellipses) {
p->Painter->setPen(ellipse->penHint());
p->Painter->setBrush(ellipse->brushHint());
ellipse->draw(p, cx, cy);
}
p->Painter->setBrush(Qt::NoBrush);

newFont.setWeight(QFont::Light);

for (Text *pt: Texts) {
p->Painter->setWorldTransform(
QTransform(pt->mCos, -pt->mSin, pt->mSin, pt->mCos,
p->DX + float(cx + pt->x) * p->Scale,
p->DY + float(cy + pt->y) * p->Scale));
newFont.setPointSizeF(p->Scale * pt->Size);
newFont.setOverline(pt->over);
newFont.setUnderline(pt->under);
p->Painter->setFont(newFont);
p->Painter->setPen(pt->Color);
w = p->drawTextMapped(pt->s, 0, 0, &h);
Q_UNUSED(w)
Q_UNUSED(h)
for (qucs::DrawingPrimitive *text: Texts) {
p->Painter->setPen(text->penHint());
p->Painter->setBrush(text->brushHint());
text->draw(p, cx, cy);
}
}
}
Expand Down Expand Up @@ -482,10 +437,10 @@ void Component::paintScheme(Schematic *p) {
p->PostPaintEvent(_Arc, cx + p3->x, cy + p3->y, p3->w, p3->h, p3->angle, p3->arclen);


for (qucs::Area *pa: Rects) // paint all rectangles
for (qucs::Rect *pa: Rects) // paint all rectangles
p->PostPaintEvent(_Rect, cx + pa->x, cy + pa->y, pa->w, pa->h);

for (qucs::Area *pa: Ellips) // paint all ellipses
for (qucs::Ellips *pa: Ellipses) // paint all ellipses
p->PostPaintEvent(_Ellipse, cx + pa->x, cy + pa->y, pa->w, pa->h);
}

Expand Down Expand Up @@ -540,7 +495,7 @@ void Component::rotate() {
}

// rotate all rectangles
for (qucs::Area *pa: Rects) {
for (qucs::Rect *pa: Rects) {
tmp = -pa->x;
pa->x = pa->y;
pa->y = tmp - pa->w;
Expand All @@ -550,7 +505,7 @@ void Component::rotate() {
}

// rotate all ellipses
for (qucs::Area *pa: Ellips) {
for (qucs::Ellips *pa: Ellipses) {
tmp = -pa->x;
pa->x = pa->y;
pa->y = tmp - pa->w;
Expand Down Expand Up @@ -638,11 +593,11 @@ void Component::mirrorX() {
}

// mirror all rectangles
for (qucs::Area *pa: Rects)
for (qucs::Rect *pa: Rects)
pa->y = -pa->y - pa->h;

// mirror all ellipses
for (qucs::Area *pa: Ellips)
for (qucs::Ellips *pa: Ellipses)
pa->y = -pa->y - pa->h;

QFont f = QucsSettings.font;
Expand Down Expand Up @@ -699,11 +654,11 @@ void Component::mirrorY() {
}

// mirror all rectangles
for (qucs::Area *pa: Rects)
for (qucs::Rect *pa: Rects)
pa->x = -pa->x - pa->w;

// mirror all ellipses
for (qucs::Area *pa: Ellips)
for (qucs::Ellips *pa: Ellipses)
pa->x = -pa->x - pa->w;

int tmp;
Expand Down Expand Up @@ -1278,7 +1233,7 @@ int Component::analyseLine(const QString &Row, int numProps) {
if (!getIntegers(Row, &i1, &i2, &i3, &i4)) return -1;
if (!getPen(Row, Pen, 5)) return -1;
if (!getBrush(Row, Brush, 8)) return -1;
Ellips.append(new qucs::Area(i1, i2, i3, i4, Pen, Brush));
Ellipses.append(new qucs::Ellips(i1, i2, i3, i4, Pen, Brush));

if (i1 < x1) x1 = i1; // keep track of component boundings
if (i1 > x2) x2 = i1;
Expand All @@ -1293,7 +1248,7 @@ int Component::analyseLine(const QString &Row, int numProps) {
if (!getIntegers(Row, &i1, &i2, &i3, &i4)) return -1;
if (!getPen(Row, Pen, 5)) return -1;
if (!getBrush(Row, Brush, 8)) return -1;
Rects.append(new qucs::Area(i1, i2, i3, i4, Pen, Brush));
Rects.append(new qucs::Rect(i1, i2, i3, i4, Pen, Brush));

if (i1 < x1) x1 = i1; // keep track of component boundings
if (i1 > x2) x2 = i1;
Expand Down Expand Up @@ -1461,7 +1416,7 @@ void Component::copyComponent(Component *pc) {
Lines = pc->Lines;
Arcs = pc->Arcs;
Rects = pc->Rects;
Ellips = pc->Ellips;
Ellipses = pc->Ellipses;
Texts = pc->Texts;
}

Expand All @@ -1477,7 +1432,7 @@ void MultiViewComponent::recreate(Schematic *Doc) {
Doc->deleteComp(this);
}

Ellips.clear();
Ellipses.clear();
Texts.clear();
Ports.clear();
Lines.clear();
Expand Down Expand Up @@ -1717,7 +1672,7 @@ void GateComponent::createSymbol() {
Texts.append(new Text(-10, 3 - y, "&", Qt::darkBlue, 15.0));
else if (Model.at(0) == 'X') {
if (Model.at(1) == 'N') {
Ellips.append(new qucs::Area(xr, -4, 8, 8,
Ellipses.append(new qucs::Ellips(xr, -4, 8, 8,
QPen(Qt::darkBlue, 0), QBrush(Qt::darkBlue)));
Texts.append(new Text(-11, 3 - y, "=1", Qt::darkBlue, 15.0));
} else
Expand Down Expand Up @@ -1747,7 +1702,7 @@ void GateComponent::createSymbol() {
}

if (Model.at(0) == 'N')
Ellips.append(new qucs::Area(xr, -4, 8, 8,
Ellipses.append(new qucs::Ellips(xr, -4, 8, 8,
QPen(Qt::darkBlue, 0), QBrush(Qt::darkBlue)));

Ports.append(new Port(30, 0));
Expand Down
4 changes: 2 additions & 2 deletions qucs/components/component.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ class Component : public Element {

QList<qucs::Line *> Lines;
QList<struct qucs::Arc *> Arcs;
QList<qucs::Area *> Rects;
QList<qucs::Area *> Ellips;
QList<qucs::Rect *> Rects;
QList<qucs::Ellips *> Ellipses;
QList<Port *> Ports;
QList<Text *> Texts;
Q3PtrList<Property> Props;
Expand Down
2 changes: 1 addition & 1 deletion qucs/components/logical_inv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ void Logical_Inv::createSymbol()
xr = 10;
}

Ellips.append(new qucs::Area(xr,-4, 8, 8,
Ellipses.append(new qucs::Ellips(xr,-4, 8, 8,
QPen(Qt::darkBlue,0), QBrush(Qt::darkBlue)));

Lines.append(new qucs::Line( xr, 0, 30, 0, QPen(Qt::darkBlue,2)));
Expand Down
2 changes: 1 addition & 1 deletion qucs/components/relais.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Relais::Relais()
Lines.append(new qucs::Line( 30, 15, 30, 30,QPen(Qt::darkBlue,2)));
Lines.append(new qucs::Line( 30, 15, 45,-15,QPen(Qt::darkBlue,2)));
Arcs.append(new qucs::Arc( 27,-18, 5, 5, 0, 16*360,QPen(Qt::darkBlue,2)));
Ellips.append(new qucs::Area( 27, 12, 6, 6, QPen(Qt::darkBlue,2),
Ellipses.append(new qucs::Ellips( 27, 12, 6, 6, QPen(Qt::darkBlue,2),
QBrush(Qt::darkBlue, Qt::SolidPattern)));

Ports.append(new Port(-30,-30));
Expand Down
2 changes: 1 addition & 1 deletion qucs/components/switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ void Switch::createSymbol()
Lines.append(new qucs::Line(-30, 0,-15, 0,QPen(Qt::darkBlue,2)));
Lines.append(new qucs::Line( 17, 0, 30, 0,QPen(Qt::darkBlue,2)));
Arcs.append(new qucs::Arc( 12, -3, 5, 5, 0, 16*360,QPen(Qt::darkBlue,2)));
Ellips.append(new qucs::Area(-18, -3, 6, 6, QPen(Qt::darkBlue,2),
Ellipses.append(new qucs::Ellips(-18, -3, 6, 6, QPen(Qt::darkBlue,2),
QBrush(Qt::darkBlue, Qt::SolidPattern)));

Ports.append(new Port(-30, 0));
Expand Down
4 changes: 2 additions & 2 deletions qucs/components/vacomponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ void vacomponent::createSymbol(QJsonObject json)
colorfill = getString(paint, "colorfill");
stylefill = getString(paint, "stylefill");

Rects.append (new qucs::Area (x, y, w, h,
Rects.append (new qucs::Rect (x, y, w, h,
QPen (QColor (color), thick, penMap.value(style)),
QBrush(QColor (colorfill), brushMap.value(stylefill))
));
Expand All @@ -236,7 +236,7 @@ void vacomponent::createSymbol(QJsonObject json)
colorfill = getString(paint, "colorfill");
stylefill = getString(paint, "stylefill");

Ellips.append (new qucs::Area (x, y, w, h,
Ellipses.append (new qucs::Ellips (x, y, w, h,
QPen (QColor (color), thick, penMap.value(style)),
QBrush(QColor (colorfill), brushMap.value(stylefill))
));
Expand Down
18 changes: 12 additions & 6 deletions qucs/diagrams/diagram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,22 @@ void Diagram::paint(ViewPainter *p) {
}

void Diagram::paintDiagram(ViewPainter *p) {
// Drawing primitives in Diagram have their coordinates
// as if they exist in space where Y-axis oriented upwards
// in contrast to downwards-oriented Y-axis of ViewPainter.
// We need to pass this flag to draw primitives correctly.
const bool y_directed_upwards = true;

// paint all lines
for (qucs::Line *pl: Lines) {
p->Painter->setPen(pl->style);
p->drawLine(cx + pl->x1, cy - pl->y1, cx + pl->x2, cy - pl->y2);
for (qucs::DrawingPrimitive* line : Lines) {
p->Painter->setPen(line->penHint());
line->draw(p, cx, cy, y_directed_upwards);
}

// paint all arcs (1 pixel larger to compensate for strange circle method)
for (qucs::Arc *pa: Arcs) {
p->Painter->setPen(pa->style);
p->drawArc(cx + pa->x, cy - pa->y, pa->w, pa->h, pa->angle, pa->arclen);
for (qucs::DrawingPrimitive* arc : Arcs) {
p->Painter->setPen(arc->penHint());
arc->draw(p, cx, cy, y_directed_upwards);
}

// draw all graphs
Expand Down
6 changes: 3 additions & 3 deletions qucs/diagrams/tabdiagram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ void TabDiagram::paint(ViewPainter *p)
void TabDiagram::paintDiagram(ViewPainter *p)
{
// paint all lines
for (qucs::Line *pl : Lines) {
p->Painter->setPen(pl->style);
p->drawLine(cx+pl->x1, cy-pl->y1, cx+pl->x2, cy-pl->y2);
for (qucs::DrawingPrimitive* line : Lines) {
p->Painter->setPen(line->penHint());
line->draw(p, cx, cy, true);
}

if(x1 > 0) { // paint scroll bar ?
Expand Down
Loading
Loading