From 3ac7d9e4358a196625349f1361f913a709ea89e3 Mon Sep 17 00:00:00 2001 From: Andrey Kalmykov Date: Sun, 9 Feb 2025 21:21:21 +0100 Subject: [PATCH] Replace some Q3PtrList usages with QList --- qucs/mouseactions.cpp | 43 +++++++++++++------------ qucs/mouseactions.h | 6 ++-- qucs/qucs_actions.cpp | 28 +++++++++++++--- qucs/schematic.cpp | 2 +- qucs/schematic.h | 16 ++++----- qucs/schematic_element.cpp | 24 +++++++------- qucs/schematic_file.cpp | 66 +++++++++++++++++++++++++++++--------- 7 files changed, 121 insertions(+), 64 deletions(-) diff --git a/qucs/mouseactions.cpp b/qucs/mouseactions.cpp index f0ecea6b8..96272ab90 100644 --- a/qucs/mouseactions.cpp +++ b/qucs/mouseactions.cpp @@ -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; @@ -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; @@ -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 *movElements) { - Element *pe; - for (pe = movElements->first(); pe != nullptr; pe = movElements->next()) { + QList *movElements) { + for (auto* pe : *movElements) { // pe->isSelected = false; // deselect first (maybe afterwards pe == // NULL) switch (pe->Type) { // FIXME: use casts. @@ -245,11 +243,10 @@ void MouseActions::endElementMoving(Schematic *Doc, // ----------------------------------------------------------- // Moves elements in "movElements" by x/y -void MouseActions::moveElements(Q3PtrList *movElements, int x, int y) +void MouseActions::moveElements(QList *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 @@ -508,12 +505,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 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 @@ -575,7 +581,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 @@ -587,7 +592,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) @@ -1893,18 +1898,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; @@ -1919,10 +1922,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: @@ -1957,11 +1959,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: diff --git a/qucs/mouseactions.h b/qucs/mouseactions.h index 2ad775314..5e8e57b66 100644 --- a/qucs/mouseactions.h +++ b/qucs/mouseactions.h @@ -50,7 +50,7 @@ class MouseActions { QMouseEvent *focusMEvent; int MAx1, MAy1,MAx2, MAy2, MAx3, MAy3; // cache for mouse movements - Q3PtrList movingElements; + QList movingElements; int movingRotated; // menu appearing by right mouse button click on component @@ -123,8 +123,8 @@ class MouseActions { void paintElementsScheme(Schematic*); void rotateElements(Schematic*, int&, int&); void moveElements(Schematic*, int&, int&); - static void moveElements(Q3PtrList*, int, int); - void endElementMoving(Schematic*, Q3PtrList*); + static void moveElements(QList*, int, int); + void endElementMoving(Schematic*, QList*); void rightPressMenu(Schematic*, QMouseEvent*, float, float); }; diff --git a/qucs/qucs_actions.cpp b/qucs/qucs_actions.cpp index dda790ca6..3d03b2c62 100644 --- a/qucs/qucs_actions.cpp +++ b/qucs/qucs_actions.cpp @@ -1127,9 +1127,19 @@ void QucsApp::slotCursorLeft(bool left) } if(!editText->isHidden()) return; // for edit of component property ? - Q3PtrList movingElements; + QList 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 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 @@ -1193,9 +1203,19 @@ void QucsApp::slotCursorUp(bool up) return; } - Q3PtrList movingElements; + QList 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 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 diff --git a/qucs/schematic.cpp b/qucs/schematic.cpp index 7a604cfc2..b0a3f24dd 100644 --- a/qucs/schematic.cpp +++ b/qucs/schematic.cpp @@ -1658,7 +1658,7 @@ void Schematic::cut() // --------------------------------------------------- // Performs paste function from clipboard -bool Schematic::paste(QTextStream *stream, Q3PtrList *pe) +bool Schematic::paste(QTextStream *stream, QList *pe) { return pasteFromClipboard(stream, pe); } diff --git a/qucs/schematic.h b/qucs/schematic.h index 95daa0414..d82892e04 100644 --- a/qucs/schematic.h +++ b/qucs/schematic.h @@ -175,7 +175,7 @@ class Schematic : public Q3ScrollView, public QucsDoc { void cut(); void copy(); - bool paste(QTextStream*, Q3PtrList*); + bool paste(QTextStream*, QList*); bool load(); int save(); int saveSymbolCpp (void); @@ -445,8 +445,8 @@ protected slots: void deleteWire(Wire*); Marker* setMarker(int, int); - void markerLeftRight(bool, Q3PtrList*); - void markerUpDown(bool, Q3PtrList*); + void markerLeftRight(bool, QList*); + void markerUpDown(bool, QList*); Element* selectElement(float, float, bool, int *index=0); void deselectElements(Element*) const; @@ -522,15 +522,15 @@ protected slots: bool loadProperties(QTextStream*); void simpleInsertComponent(Component*); - bool loadComponents(QTextStream*, Q3PtrList *List=0); + bool loadComponents(QTextStream*, QList *List=0); void simpleInsertWire(Wire*); - bool loadWires(QTextStream*, Q3PtrList *List=0); - bool loadDiagrams(QTextStream*, Q3PtrList*); - bool loadPaintings(QTextStream*, Q3PtrList*); + bool loadWires(QTextStream*, QList *List=0); + bool loadDiagrams(QTextStream*, QList*); + bool loadPaintings(QTextStream*, QList*); bool loadIntoNothing(QTextStream*); QString createClipboardFile(); - bool pasteFromClipboard(QTextStream *, Q3PtrList*); + bool pasteFromClipboard(QTextStream *, QList*); QString createUndoString(char); bool rebuild(QString *); diff --git a/qucs/schematic_element.cpp b/qucs/schematic_element.cpp index e19925550..be941cb52 100644 --- a/qucs/schematic_element.cpp +++ b/qucs/schematic_element.cpp @@ -981,14 +981,14 @@ Marker* Schematic::setMarker(int x, int y) // --------------------------------------------------- // Moves the marker pointer left/right on the graph. -void Schematic::markerLeftRight(bool left, Q3PtrList *Elements) +void Schematic::markerLeftRight(bool left, QList *Elements) { bool acted = false; - for(auto i : *Elements) { - Marker* pm = prechecked_cast(i); - assert(pm); - if(pm->moveLeftRight(left)) - acted = true; + for (auto* e : *Elements) { + if (auto* pm = dynamic_cast(e)) { + if (pm->moveLeftRight(left)) + acted = true; + } } if(acted) setChanged(true, true, 'm'); @@ -996,14 +996,14 @@ void Schematic::markerLeftRight(bool left, Q3PtrList *Elements) // --------------------------------------------------- // Moves the marker pointer up/down on the more-dimensional graph. -void Schematic::markerUpDown(bool up, Q3PtrList *Elements) +void Schematic::markerUpDown(bool up, QList *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(e)) { + if (pm->moveUpDown(up)) + acted = true; + } } if(acted) setChanged(true, true, 'm'); diff --git a/qucs/schematic_file.cpp b/qucs/schematic_file.cpp index 188b8439d..f55856388 100644 --- a/qucs/schematic_file.cpp +++ b/qucs/schematic_file.cpp @@ -61,6 +61,22 @@ extern void osdi_log_skip(void *handle, char* msg, uint32_t lvl) (void)lvl; } +namespace shim { + // Historically Qucs-S has been using Q3PtrList a lot and it's not so easy + // to replace it. The goal of this shim is to help with transition to new + // container type. + // Wherever possible a "barrier" is made by replacing Q3PtrList with QList + // to stop its propagating. QList is passed instead of original Q3PtrList + // and then its contents are copied back to Q3PtrList as if it was passed + // in the first place +template +void copyToQ3PtrList(const QList& src, Q3PtrList& dst) { + for (auto* item : src) { + dst.append(item); + } +} +} // namespace shim + // ------------------------------------------------------------- // Creates a Qucs file format (without document properties) in the returning // string. This is used to copy the selected elements into the clipboard. @@ -131,7 +147,7 @@ bool Schematic::loadIntoNothing(QTextStream *stream) // ------------------------------------------------------------- // Paste from clipboard. -bool Schematic::pasteFromClipboard(QTextStream *stream, Q3PtrList *pe) +bool Schematic::pasteFromClipboard(QTextStream *stream, QList *pe) { QString Line; @@ -161,7 +177,7 @@ bool Schematic::pasteFromClipboard(QTextStream *stream, Q3PtrList *pe) if(!loadIntoNothing(stream)) return false; } else if(Line == "") { - if(!loadPaintings(stream, (Q3PtrList*)pe)) return false; } + if(!loadPaintings(stream, (QList*)pe)) return false; } else { QMessageBox::critical(0, QObject::tr("Error"), QObject::tr("Clipboard Format Error:\nUnknown field!")); @@ -176,16 +192,16 @@ bool Schematic::pasteFromClipboard(QTextStream *stream, Q3PtrList *pe) while(!stream->atEnd()) { Line = stream->readLine(); if(Line == "") { - if(!loadComponents(stream, (Q3PtrList*)pe)) return false; } + if(!loadComponents(stream, (QList*)pe)) return false; } else if(Line == "") { if(!loadWires(stream, pe)) return false; } else if(Line == "") { - if(!loadDiagrams(stream, (Q3PtrList*)pe)) return false; } + if(!loadDiagrams(stream, (QList*)pe)) return false; } else if(Line == "") { - if(!loadPaintings(stream, (Q3PtrList*)pe)) return false; } + if(!loadPaintings(stream, (QList*)pe)) return false; } else { QMessageBox::critical(0, QObject::tr("Error"), QObject::tr("Clipboard Format Error:\nUnknown field!")); @@ -869,7 +885,7 @@ void Schematic::simpleInsertComponent(Component *c) } // ------------------------------------------------------------- -bool Schematic::loadComponents(QTextStream *stream, Q3PtrList *List) +bool Schematic::loadComponents(QTextStream *stream, QList *List) { QString Line, cstr; Component *c; @@ -939,7 +955,7 @@ void Schematic::simpleInsertWire(Wire *pw) } // ------------------------------------------------------------- -bool Schematic::loadWires(QTextStream *stream, Q3PtrList *List) +bool Schematic::loadWires(QTextStream *stream, QList *List) { Wire *w; QString Line; @@ -976,7 +992,7 @@ bool Schematic::loadWires(QTextStream *stream, Q3PtrList *List) } // ------------------------------------------------------------- -bool Schematic::loadDiagrams(QTextStream *stream, Q3PtrList *List) +bool Schematic::loadDiagrams(QTextStream *stream, QList *List) { Diagram *d; QString Line, cstr; @@ -1019,7 +1035,7 @@ bool Schematic::loadDiagrams(QTextStream *stream, Q3PtrList *List) } // ------------------------------------------------------------- -bool Schematic::loadPaintings(QTextStream *stream, Q3PtrList *List) +bool Schematic::loadPaintings(QTextStream *stream, QList *List) { Painting *p=0; QString Line, cstr; @@ -1135,10 +1151,12 @@ bool Schematic::loadDocument() if(Line.isEmpty()) continue; if(Line == "") { - if(!loadPaintings(&stream, &a_SymbolPaints)) { + QList paintings; + if (!loadPaintings(&stream, &paintings)) { file.close(); return false; } + shim::copyToQ3PtrList(paintings, a_SymbolPaints); } else if(Line == "") { @@ -1151,10 +1169,16 @@ bool Schematic::loadDocument() if(!loadWires(&stream)) { file.close(); return false; } } else if(Line == "") { - if(!loadDiagrams(&stream, &a_DocDiags)) { file.close(); return false; } } + QList diagrams; + if (!loadDiagrams(&stream, &diagrams)) { file.close(); return false; } + shim::copyToQ3PtrList(diagrams, a_DocDiags); + } else if(Line == "") { - if(!loadPaintings(&stream, &a_DocPaints)) { file.close(); return false; } } + QList paintings; + if (!loadPaintings(&stream, &paintings)) { file.close(); return false; } + shim::copyToQ3PtrList(paintings, a_DocPaints); + } else { qDebug() << Line; QMessageBox::critical(0, QObject::tr("Error"), @@ -1241,8 +1265,16 @@ bool Schematic::rebuild(QString *s) // read content ************************* if(!loadComponents(&stream)) return false; if(!loadWires(&stream)) return false; - if(!loadDiagrams(&stream, &a_DocDiags)) return false; - if(!loadPaintings(&stream, &a_DocPaints)) return false; + { + QList diagrams; + if (!loadDiagrams(&stream, &diagrams)) return false; + shim::copyToQ3PtrList(diagrams, a_DocDiags); + } + { + QList paintings; + if (!loadPaintings(&stream, &paintings)) return false; + shim::copyToQ3PtrList(paintings, a_DocPaints); + } return true; } @@ -1261,8 +1293,12 @@ bool Schematic::rebuildSymbol(QString *s) Line = stream.readLine(); // skip components Line = stream.readLine(); // skip wires Line = stream.readLine(); // skip diagrams - if(!loadPaintings(&stream, &a_SymbolPaints)) return false; + { + QList paintings; + if (!loadPaintings(&stream, &paintings)) return false; + shim::copyToQ3PtrList(paintings, a_SymbolPaints); + } return true; }