Skip to content

Commit

Permalink
Merge pull request #1237 from wawuwo/refactor-out-some-q3ptrlist-usages
Browse files Browse the repository at this point in the history
Replace some Q3PtrList usages with QList
  • Loading branch information
ra3xdh authored Feb 15, 2025
2 parents f114398 + 3ac7d9e commit eff2da6
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 64 deletions.
43 changes: 22 additions & 21 deletions qucs/mouseactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,11 @@ bool MouseActions::pasteElements(Schematic *Doc)
if (!Doc->paste(&stream, &movingElements))
return false;

Element *pe;
int xmax, xmin, ymax, ymin;
xmin = ymin = INT_MAX;
xmax = ymax = INT_MIN;
// First, get the max and min coordinates of all selected elements.
for (pe = movingElements.first(); pe != 0; pe = movingElements.next()) {
for (auto* pe : movingElements) {
if (pe->Type == isWire) {
if (pe->x1 < xmin)
xmin = pe->x1;
Expand All @@ -137,7 +136,7 @@ bool MouseActions::pasteElements(Schematic *Doc)
Doc->setOnGrid(xmin, ymin);

// moving with mouse cursor in the midpoint
for (pe = movingElements.first(); pe != 0; pe = movingElements.next())
for (auto* pe : movingElements)
if (pe->Type & isLabel) {
pe->cx += xmin;
pe->x1 += xmin;
Expand Down Expand Up @@ -187,9 +186,8 @@ void MouseActions::editLabel(Schematic *Doc, WireLabel *pl)
// -----------------------------------------------------------
// Reinserts all elements (moved by the user) back into the schematic.
void MouseActions::endElementMoving(Schematic *Doc,
Q3PtrList<Element> *movElements) {
Element *pe;
for (pe = movElements->first(); pe != nullptr; pe = movElements->next()) {
QList<Element*> *movElements) {
for (auto* pe : *movElements) {
// pe->isSelected = false; // deselect first (maybe afterwards pe ==
// NULL)
switch (pe->Type) { // FIXME: use casts.
Expand Down Expand Up @@ -245,11 +243,10 @@ void MouseActions::endElementMoving(Schematic *Doc,

// -----------------------------------------------------------
// Moves elements in "movElements" by x/y
void MouseActions::moveElements(Q3PtrList<Element> *movElements, int x, int y)
void MouseActions::moveElements(QList<Element*> *movElements, int x, int y)
{
Wire *pw;
Element *pe;
for (pe = movElements->first(); pe != 0; pe = movElements->next()) {
for (auto* pe : *movElements) {
if (pe->Type == isWire) {
pw = (Wire *) pe; // connected wires are not moved completely

Expand Down Expand Up @@ -499,12 +496,21 @@ void MouseActions::MMoveMoving(Schematic *Doc, QMouseEvent *Event)
MAy3 = MAy1 = MAy2 - MAy1;

movingElements.clear();
Doc->copySelectedElements(&movingElements);
{
// Q3PtrList is long time deprecated and has to be replaced with another
// container type, which is not always easy. Here it's simpler to use it
// once and go back to QList, because copySelectedElements() uses API
// unique to Q3PtrList and to refactor it is a piece of work
Q3PtrList<Element> temp_buffer;
temp_buffer.setAutoDelete(false);
Doc->copySelectedElements(&temp_buffer);
for (auto* e : temp_buffer) { movingElements.append(e); }
}
Doc->viewport()->repaint();

Wire *pw;
// Changes the position of all moving elements by dx/dy
for (Element *pe = movingElements.first(); pe != 0; pe = movingElements.next()) {
for (Element *pe : movingElements) {
if (pe->Type == isWire) {
pw = (Wire *) pe; // connecting wires are not moved completely

Expand Down Expand Up @@ -566,7 +572,6 @@ void MouseActions::MMoveMoving2(Schematic *Doc, QMouseEvent *Event)
MAx2 = inModel.x();
MAy2 = inModel.y();

Element *pe;

if ((Event->modifiers().testFlag(Qt::ControlModifier)) == 0)
Doc->setOnGrid(MAx2, MAy2); // use grid only if CTRL key not pressed
Expand All @@ -578,7 +583,7 @@ void MouseActions::MMoveMoving2(Schematic *Doc, QMouseEvent *Event)
moveElements(&movingElements, MAx1, MAy1); // moves elements by MAx1/MAy1

// paint afterwards to avoid conflict between wire and label painting
for (pe = movingElements.first(); pe != 0; pe = movingElements.next())
for (auto* pe : movingElements)
pe->paintScheme(Doc);
// if(pe->Type == isWire) if(((Wire*)pe)->Label)
// if(!((Wire*)pe)->Label->isSelected)
Expand Down Expand Up @@ -1860,18 +1865,16 @@ void MouseActions::MReleaseSetLimits(Schematic *Doc, QMouseEvent *Event)
// -----------------------------------------------------------
void MouseActions::paintElementsScheme(Schematic *p)
{
Element *pe;
for (pe = movingElements.first(); pe != nullptr; pe = movingElements.next())
for (auto* pe : movingElements)
pe->paintScheme(p);
}

// -----------------------------------------------------------
void MouseActions::moveElements(Schematic *Doc, int &x1, int &y1)
{
Element *pe;
Doc->setOnGrid(x1, y1);

for (pe = movingElements.first(); pe != 0; pe = movingElements.next()) {
for (auto* pe : movingElements) {
if (pe->Type & isLabel) {
pe->cx += x1;
pe->x1 += x1;
Expand All @@ -1886,10 +1889,9 @@ void MouseActions::moveElements(Schematic *Doc, int &x1, int &y1)
void MouseActions::rotateElements(Schematic *Doc, int &x1, int &y1)
{
int x2;
Element *pe;
Doc->setOnGrid(x1, y1);

for (pe = movingElements.first(); pe != 0; pe = movingElements.next()) {
for (auto* pe : movingElements) {
switch (pe->Type) {
case isComponent:
case isAnalogComponent:
Expand Down Expand Up @@ -1924,11 +1926,10 @@ void MouseActions::MReleasePaste(Schematic *Doc, QMouseEvent *Event)
QFileInfo Info(Doc->getDocName());
//QPainter painter(Doc->viewport());

Element *pe;
switch (Event->button()) {
case Qt::LeftButton:
// insert all moved elements into document
for (pe = movingElements.first(); pe != 0; pe = movingElements.next()) {
for (auto* pe : movingElements) {
pe->isSelected = false;
switch (pe->Type) {
case isWire:
Expand Down
6 changes: 3 additions & 3 deletions qucs/mouseactions.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class MouseActions {
QMouseEvent *focusMEvent;

int MAx1, MAy1,MAx2, MAy2, MAx3, MAy3; // cache for mouse movements
Q3PtrList<Element> movingElements;
QList<Element*> movingElements;
int movingRotated;

// menu appearing by right mouse button click on component
Expand Down Expand Up @@ -123,8 +123,8 @@ class MouseActions {
void paintElementsScheme(Schematic*);
void rotateElements(Schematic*, int&, int&);
void moveElements(Schematic*, int&, int&);
static void moveElements(Q3PtrList<Element>*, int, int);
void endElementMoving(Schematic*, Q3PtrList<Element>*);
static void moveElements(QList<Element*>*, int, int);
void endElementMoving(Schematic*, QList<Element*>*);
void rightPressMenu(Schematic*, QMouseEvent*, float, float);
};

Expand Down
28 changes: 24 additions & 4 deletions qucs/qucs_actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1127,9 +1127,19 @@ void QucsApp::slotCursorLeft(bool left)
}
if(!editText->isHidden()) return; // for edit of component property ?

Q3PtrList<Element> movingElements;
QList<Element*> movingElements;
Schematic *Doc = (Schematic*)DocumentTab->currentWidget();
int markerCount = Doc->copySelectedElements(&movingElements);
int markerCount = 0;
{
// Q3PtrList is long time deprecated and has to be replaced with another
// container type, which is not always easy. Here it's simpler to use it
// once and go back to QList, because copySelectedElements() uses API
// unique to Q3PtrList and to refactor it is a piece of work
Q3PtrList<Element> temp_buffer;
temp_buffer.setAutoDelete(false);
markerCount = Doc->copySelectedElements(&temp_buffer);
for (auto* e : temp_buffer) { movingElements.append(e); }
}

if((movingElements.count() - markerCount) < 1) {
if(markerCount > 0) { // only move marker if nothing else selected
Expand Down Expand Up @@ -1193,9 +1203,19 @@ void QucsApp::slotCursorUp(bool up)
return;
}

Q3PtrList<Element> movingElements;
QList<Element*> movingElements;
Schematic *Doc = (Schematic*)DocumentTab->currentWidget();
int markerCount = Doc->copySelectedElements(&movingElements);
int markerCount = 0;
{
// Q3PtrList is long time deprecated and has to be replaced with another
// container type, which is not always easy. Here it's simpler to use it
// once and go back to QList, because copySelectedElements() uses API
// unique to Q3PtrList and to refactor it is a piece of work
Q3PtrList<Element> temp_buffer;
temp_buffer.setAutoDelete(false);
markerCount = Doc->copySelectedElements(&temp_buffer);
for (auto* e : temp_buffer) { movingElements.append(e); }
}

if((movingElements.count() - markerCount) < 1) { // all selections are markers
if(markerCount > 0) { // only move marker if nothing else selected
Expand Down
2 changes: 1 addition & 1 deletion qucs/schematic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1668,7 +1668,7 @@ void Schematic::cut()

// ---------------------------------------------------
// Performs paste function from clipboard
bool Schematic::paste(QTextStream *stream, Q3PtrList<Element> *pe)
bool Schematic::paste(QTextStream *stream, QList<Element*> *pe)
{
return pasteFromClipboard(stream, pe);
}
Expand Down
16 changes: 8 additions & 8 deletions qucs/schematic.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class Schematic : public Q3ScrollView, public QucsDoc {

void cut();
void copy();
bool paste(QTextStream*, Q3PtrList<Element>*);
bool paste(QTextStream*, QList<Element*>*);
bool load();
int save();
int saveSymbolCpp (void);
Expand Down Expand Up @@ -462,8 +462,8 @@ protected slots:
void deleteWire(Wire*);

Marker* setMarker(int, int);
void markerLeftRight(bool, Q3PtrList<Element>*);
void markerUpDown(bool, Q3PtrList<Element>*);
void markerLeftRight(bool, QList<Element*>*);
void markerUpDown(bool, QList<Element*>*);

Element* selectElement(float, float, bool, int *index=0);
void deselectElements(Element*) const;
Expand Down Expand Up @@ -539,15 +539,15 @@ protected slots:

bool loadProperties(QTextStream*);
void simpleInsertComponent(Component*);
bool loadComponents(QTextStream*, Q3PtrList<Component> *List=0);
bool loadComponents(QTextStream*, QList<Component*> *List=0);
void simpleInsertWire(Wire*);
bool loadWires(QTextStream*, Q3PtrList<Element> *List=0);
bool loadDiagrams(QTextStream*, Q3PtrList<Diagram>*);
bool loadPaintings(QTextStream*, Q3PtrList<Painting>*);
bool loadWires(QTextStream*, QList<Element*> *List=0);
bool loadDiagrams(QTextStream*, QList<Diagram*>*);
bool loadPaintings(QTextStream*, QList<Painting*>*);
bool loadIntoNothing(QTextStream*);

QString createClipboardFile();
bool pasteFromClipboard(QTextStream *, Q3PtrList<Element>*);
bool pasteFromClipboard(QTextStream *, QList<Element*>*);

QString createUndoString(char);
bool rebuild(QString *);
Expand Down
24 changes: 12 additions & 12 deletions qucs/schematic_element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -982,29 +982,29 @@ Marker* Schematic::setMarker(int x, int y)

// ---------------------------------------------------
// Moves the marker pointer left/right on the graph.
void Schematic::markerLeftRight(bool left, Q3PtrList<Element> *Elements)
void Schematic::markerLeftRight(bool left, QList<Element*> *Elements)
{
bool acted = false;
for(auto i : *Elements) {
Marker* pm = prechecked_cast<Marker*>(i);
assert(pm);
if(pm->moveLeftRight(left))
acted = true;
for (auto* e : *Elements) {
if (auto* pm = dynamic_cast<Marker*>(e)) {
if (pm->moveLeftRight(left))
acted = true;
}
}

if(acted) setChanged(true, true, 'm');
}

// ---------------------------------------------------
// Moves the marker pointer up/down on the more-dimensional graph.
void Schematic::markerUpDown(bool up, Q3PtrList<Element> *Elements)
void Schematic::markerUpDown(bool up, QList<Element*> *Elements)
{
Marker *pm;
bool acted = false;
for(pm = (Marker*)Elements->first(); pm!=0; pm = (Marker*)Elements->next())
{
if(pm->moveUpDown(up))
acted = true;
for (auto* e : *Elements) {
if (auto* pm = dynamic_cast<Marker*>(e)) {
if (pm->moveUpDown(up))
acted = true;
}
}

if(acted) setChanged(true, true, 'm');
Expand Down
Loading

0 comments on commit eff2da6

Please sign in to comment.