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 Schematic::sizeOfSelection() and its usages #1235

Merged
merged 3 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion qucs/imagewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ int ImageWriter::print(QWidget *doc)
int h = all.height();


QRect selected = sch->sizeOfSelection();
QRect selected = sch->currentSelection().bounds;
int wsel = selected.width();
int hsel = selected.height();

Expand Down
26 changes: 18 additions & 8 deletions qucs/schematic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ void Schematic::print(QPrinter*, QPainter* painter, bool printAll,
const QRectF pageSize{0, 0, static_cast<double>(painter->device()->width()),
static_cast<double>(painter->device()->height())};

QRect printedArea = printAll ? allBoundingRect() : sizeOfSelection();
QRect printedArea = printAll ? allBoundingRect() : currentSelection().bounds;

if (printAll && a_showFrame) {
int frame_width, frame_height;
Expand Down Expand Up @@ -977,7 +977,7 @@ void Schematic::zoomToSelection() {
return;
}

const QRect selectedBoundingRect{ sizeOfSelection() };
const QRect selectedBoundingRect{ currentSelection().bounds };

// Working with raw coordinates is clumsy, abstract them out
const QRect usedBoundingRect{a_UsedX1, a_UsedY1, a_UsedX2 - a_UsedX1, a_UsedY2 - a_UsedY1};
Expand Down Expand Up @@ -1280,7 +1280,7 @@ void Schematic::sizeOfAll(int &xmin, int &ymin, int &xmax, int &ymax)
}
}

QRect Schematic::sizeOfSelection() const {
Schematic::Selection Schematic::currentSelection() const {
int xmin = INT_MAX;
int ymin = INT_MAX;
int xmax = INT_MIN;
Expand All @@ -1290,16 +1290,19 @@ QRect Schematic::sizeOfSelection() const {

if (a_Components->isEmpty() && a_Wires->isEmpty() && a_Diagrams->isEmpty() &&
a_Paintings->isEmpty()) {
return QRect{};
return {};
}

Selection selection;

int x1, y1, x2, y2;
// find boundings of all components
for (auto* pc : *a_Components) {
if (!pc->isSelected) {
continue;
}
isAnySelected = true;
selection.components.push_back(pc);
pc->entireBounds(x1, y1, x2, y2);
xmin = std::min(x1, xmin);
xmax = std::max(x2, xmax);
Expand All @@ -1313,12 +1316,14 @@ QRect Schematic::sizeOfSelection() const {
continue;
}
isAnySelected = true;
selection.wires.push_back(pw);
xmin = std::min(pw->x1, xmin);
xmax = std::max(pw->x2, xmax);
ymin = std::min(pw->y1, ymin);
ymax = std::max(pw->y2, ymax);

if (auto* pl = pw->Label; pl) { // check position of wire label
selection.labels.push_back(pl);
pl->getLabelBounding(x1, y1, x2, y2);
xmin = std::min(x1, xmin);
xmax = std::max(x2, xmax);
Expand All @@ -1329,11 +1334,12 @@ QRect Schematic::sizeOfSelection() const {

// find boundings of all node labels
for (auto* pn : *a_Nodes) {
if (!pn->isSelected) {
continue;
if (std::all_of(pn->begin(), pn->end(), [](auto* e) { return e->isSelected; })) {
selection.nodes.push_back(pn);
}

if (auto* pl = pn->Label; pl) { // check position of node label
selection.labels.push_back(pl);
isAnySelected = true;
pl->getLabelBounding(x1, y1, x2, y2);
xmin = std::min(x1, xmin);
Expand All @@ -1348,6 +1354,7 @@ QRect Schematic::sizeOfSelection() const {
if (!pd->isSelected) {
continue;
}
selection.diagrams.push_back(pd);
isAnySelected = true;
pd->Bounding(x1, y1, x2, y2);
xmin = std::min(x1, xmin);
Expand All @@ -1358,6 +1365,7 @@ QRect Schematic::sizeOfSelection() const {
for (Graph* pg : pd->Graphs) {
// test all markers of diagram
for (Marker* pm : pg->Markers) {
selection.markers.push_back(pm);
pm->Bounding(x1, y1, x2, y2);
xmin = std::min(x1, xmin);
xmax = std::max(x2, xmax);
Expand All @@ -1372,6 +1380,7 @@ QRect Schematic::sizeOfSelection() const {
if (!pp->isSelected) {
continue;
}
selection.paintings.push_back(pp);
isAnySelected = true;
pp->Bounding(x1, y1, x2, y2);
xmin = std::min(x1, xmin);
Expand All @@ -1381,10 +1390,11 @@ QRect Schematic::sizeOfSelection() const {
}

if (!isAnySelected) {
return QRect{};
return {};
}

return QRect{xmin, ymin, xmax - xmin, ymax - ymin};
selection.bounds = QRect{xmin, ymin, xmax - xmin, ymax - ymin};
return selection;
}

// ---------------------------------------------------
Expand Down
14 changes: 13 additions & 1 deletion qucs/schematic.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,19 @@ class Schematic : public Q3ScrollView, public QucsDoc {
Returns the smallest rectangle enclosing all elements of schematic
*/
QRect allBoundingRect();
QRect sizeOfSelection() const;

struct Selection {
QRect bounds;
std::vector<Component*> components;
std::vector<Wire*> wires;
std::vector<Painting*> paintings;
std::vector<Diagram*> diagrams;
std::vector<WireLabel*> labels;
std::vector<Marker*> markers;
std::vector<Node*> nodes;
};

Selection currentSelection() const;
bool rotateElements();
bool mirrorXComponents();
bool mirrorYComponents();
Expand Down
Loading
Loading