diff --git a/sources/qetgraphicsitem/dynamicelementtextitem.cpp b/sources/qetgraphicsitem/dynamicelementtextitem.cpp index 015b1cda8..9512ea0e0 100644 --- a/sources/qetgraphicsitem/dynamicelementtextitem.cpp +++ b/sources/qetgraphicsitem/dynamicelementtextitem.cpp @@ -1810,12 +1810,36 @@ void DynamicElementTextItem::setPlainText(const QString &text) bool update_alignment = true; + bool parsing_only = false; if (diagram() && (diagram()->project()->state() == QETProject::ProjectParsingRunning)) + { update_alignment = false; + parsing_only = true; + } if (m_parent_element.data()->state() == QET::GIBuildingFromXml || m_parent_element.data()->state() == QET::GILoadingFromXml) + { update_alignment = false; - + parsing_only = false; + } + + //A text emptied with its alignment kept (the project not being + //opened) is filled again the same way, even if the project is + //being opened by then: lazy folios build the folios left late with + //the project in that state, and the text would stay where emptying + //moved it (F119) + if (text.isEmpty()) + m_emptied_aligned = update_alignment; + else if (!toPlainText().isEmpty()) + m_emptied_aligned = false; + else if (parsing_only && m_emptied_aligned) + { + update_alignment = true; + m_emptied_aligned = false; + } + else + m_emptied_aligned = false; + if (update_alignment) { prepareAlignment(); } diff --git a/sources/qetgraphicsitem/dynamicelementtextitem.h b/sources/qetgraphicsitem/dynamicelementtextitem.h index 5ac043a27..524dd91ad 100644 --- a/sources/qetgraphicsitem/dynamicelementtextitem.h +++ b/sources/qetgraphicsitem/dynamicelementtextitem.h @@ -196,6 +196,8 @@ class DynamicElementTextItem : public DiagramTextItem CrossRefItem *m_Xref_item = nullptr; QGraphicsTextItem *m_slave_Xref_item = nullptr; qreal m_text_width = -1; + /// The text was emptied with its alignment kept (setPlainText()) + bool m_emptied_aligned = false; QPointF m_initial_position; bool m_keep_visual_rotation = true; bool m_rotation_point_center = false; diff --git a/tests/qttest/CMakeLists.txt b/tests/qttest/CMakeLists.txt index df7bc5015..6dab05931 100644 --- a/tests/qttest/CMakeLists.txt +++ b/tests/qttest/CMakeLists.txt @@ -1349,3 +1349,18 @@ target_compile_definitions(tst_qetcontainer PRIVATE "QET_EXAMPLES_DIR=\"${QET_DIR}/examples\"" "QET_FIXTURES_DIR=\"${CMAKE_CURRENT_SOURCE_DIR}/fixtures\"" "QET_FORMAT_SPEC=\"${QET_DIR}/sources/container/QETZ-FORMAT.md\"") + +# A right-aligned report text emptied and set again while lazy folios are +# built is saved where it was (F119): the real binary's --run on +# fixtures/report_text_late_folio.qet, lazily and with every folio built. +if(QET_HAS_SCRIPTING) + add_executable( + tst_reporttextlatefolio + tst_reporttextlatefolio.cpp) + add_test(NAME tst_reporttextlatefolio COMMAND tst_reporttextlatefolio) + add_dependencies(tst_reporttextlatefolio qelectrotech) + target_link_libraries(tst_reporttextlatefolio PRIVATE Qt::Test) + target_compile_definitions(tst_reporttextlatefolio PRIVATE + "QET_TEST_BINARY_PATH=\"$\"" + "QET_FIXTURES_DIR=\"${CMAKE_CURRENT_SOURCE_DIR}/fixtures\"") +endif() diff --git a/tests/qttest/fixtures/report_text_late_folio.qet b/tests/qttest/fixtures/report_text_late_folio.qet new file mode 100644 index 000000000..b55af5aba --- /dev/null +++ b/tests/qttest/fixtures/report_text_late_folio.qet @@ -0,0 +1,278 @@ + + + + + + + + + + + + + + + + + + + + + + T + label + + + Motor_X + function + + + + + + + + + + + + + + + + K1 + + + + T + + + T + + + T + description + + + T + label + + + T + comment + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + T + label + + + Motor_X + function + + + + + + + + + + + + + + + + + + + K1 + + + + T + + + T + + + T + description + + + T + label + + + T + comment + + + + + + + + + + + + + + + + + + + + + + + + + Следующая страница + الصفحة التالية + Nächste Folie + Επόμενη σελίδα + Going arrow + Foglio successivo + Folio suivant + Wyjście adresowe + Folio siguiente + Další list + Referentie volgend + + Author: The QElectroTech team +License: see http://qelectrotech.org/wiki/doc/elements_license + + + + + + + + + + + + + Предыдущая страница + الصفحة السابقة + Vorherige Folie + Προηγούμενη σελίδα + Coming arrow + Foglio precedente + Folio précédent + Wejście adresowe + folio anterior + Předchozí list + Referentie Vorig + + Author: The QElectroTech team +License: see http://qelectrotech.org/wiki/doc/elements_license + + + + + + + + + + + + + + + Coil + Bobina + Πηνίο + Bobine + Cívka + Bobina + Cewka + ملف + Spoel + Spule + Relais spoel (algemeen) + Обмотка + + + coil + + + + + tag + + + + Nome + + + comentário + + + Author: The QElectroTech team +License: see http://qelectrotech.org/wiki/doc/elements_license + + + A2 + + + A1 + + + + + Nome + description + + + tag + label + + + comentário + comment + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/qttest/tst_reporttextlatefolio.cpp b/tests/qttest/tst_reporttextlatefolio.cpp new file mode 100644 index 000000000..b73e91b8e --- /dev/null +++ b/tests/qttest/tst_reporttextlatefolio.cpp @@ -0,0 +1,90 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +#include + +#include +#include +#include +#include +#include + +// A report symbol on folio B shows the function of the wire its partner has +// on folio A, right-aligned. Deleting the coil on folio B empties that text +// while folio A, not built yet (lazy folios), is not known to the report, and +// builds it again when folio A is built. The text keeps its right edge when +// emptied outside the opening of the project, and was not moved back when set +// again while the late folios are built (the project is then in the state of +// being opened): it was saved at another x than with every folio built (F119). +// The real binary's --run on fixtures/report_text_late_folio.qet, lazily and +// with QET_EAGER_FOLIOS=1. +class tst_reporttextlatefolio : public QObject +{ + Q_OBJECT + + QTemporaryDir m_dir; + + QString savedX(bool eager) + { + const QString tag = eager ? QStringLiteral("eager") : QStringLiteral("lazy"); + const QString out = m_dir.filePath(tag + QStringLiteral(".qet")); + const QString script = m_dir.filePath(tag + QStringLiteral(".js")); + const QString home = m_dir.filePath(tag + QStringLiteral("-home")); + QDir().mkpath(home); + QFile f(script); + if (!f.open(QIODevice::WriteOnly)) + return QString(); + f.write("qet.deleteElement(1, '{794d8944-035d-4bcd-8ee6-c653d384fd4f}');\n" + "qet.save('" + out.toUtf8() + "');\n"); + f.close(); + + QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); + env.insert(QStringLiteral("QT_QPA_PLATFORM"), QStringLiteral("offscreen")); + env.insert(QStringLiteral("QET_ENABLE_SCRIPTING"), QStringLiteral("1")); + env.insert(QStringLiteral("HOME"), home); + env.insert(QStringLiteral("XDG_CONFIG_HOME"), home + QStringLiteral("/config")); + env.insert(QStringLiteral("XDG_DATA_HOME"), home + QStringLiteral("/data")); + env.insert(QStringLiteral("TMPDIR"), m_dir.path()); + if (eager) + env.insert(QStringLiteral("QET_EAGER_FOLIOS"), QStringLiteral("1")); + else + env.remove(QStringLiteral("QET_EAGER_FOLIOS")); + QProcess proc; + proc.setProcessEnvironment(env); + proc.start(QStringLiteral(QET_TEST_BINARY_PATH), + {QStringLiteral("--run"), script, + QStringLiteral(QET_FIXTURES_DIR "/report_text_late_folio.qet")}); + if (!proc.waitForFinished(120000)) + return QString(); + QFile saved(out); + if (!saved.open(QIODevice::ReadOnly)) + return QString(); + const QString xml = QString::fromUtf8(saved.readAll()); + const QRegularExpression re(QStringLiteral( + "uuid=\"\\{cbaf051e-0ce0-40f1-ada9-b8f637a79f92\\}\" x=\"([^\"]*)\"")); + const auto m = re.match(xml); + return m.hasMatch() ? m.captured(1) : QString(); + } + +private slots: + void initTestCase() + { + QVERIFY(m_dir.isValid()); + QVERIFY(QFile::exists(QStringLiteral(QET_TEST_BINARY_PATH))); + } + + void rightAlignedTextKeepsItsPlace_data() + { + QTest::addColumn("eager"); + QTest::newRow("lazy folios") << false; + QTest::newRow("every folio built") << true; + } + + void rightAlignedTextKeepsItsPlace() + { + QFETCH(bool, eager); + QCOMPARE(savedX(eager), QStringLiteral("60")); + } +}; + +QTEST_APPLESS_MAIN(tst_reporttextlatefolio) + +#include "tst_reporttextlatefolio.moc"