From 7e3c7e0f883de02ef954a10f73bb53010af7a534 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Thu, 16 Jan 2020 19:09:23 +0200 Subject: [PATCH] Refactoring. Hide compatibility layer behind a function. toSet(), toList(), swapItemsAt(). --HG-- branch : develop --- src/app/tape/tmainwindow.cpp | 16 ++------- src/app/valentina/mainwindowsnogui.cpp | 17 +++------ src/app/valentina/xml/vpattern.cpp | 15 ++------ src/libs/ifc/xml/vabstractpattern.cpp | 24 +++---------- src/libs/vlayout/vlayoutgenerator.cpp | 7 ++-- src/libs/vlayout/vlayoutpiece.cpp | 13 ++----- src/libs/vmisc/compatibility.h | 44 +++++++++++++++++++++++ src/libs/vwidgets/vabstractmainwindow.cpp | 3 +- src/test/ValentinaTest/tst_dxf.cpp | 7 ++-- 9 files changed, 68 insertions(+), 78 deletions(-) diff --git a/src/app/tape/tmainwindow.cpp b/src/app/tape/tmainwindow.cpp index eda521b7a..a46641118 100644 --- a/src/app/tape/tmainwindow.cpp +++ b/src/app/tape/tmainwindow.cpp @@ -43,6 +43,7 @@ #include "../vmisc/vsysexits.h" #include "../vmisc/qxtcsvmodel.h" #include "../vmisc/dialogs/dialogexporttocsv.h" +#include "../vmisc/compatibility.h" #include "vlitepattern.h" #include "../qmuparser/qmudef.h" #include "../vtools/dialogs/support/dialogeditwrongformula.h" @@ -2795,13 +2796,7 @@ void TMainWindow::WriteSettings() //--------------------------------------------------------------------------------------------------------------------- QStringList TMainWindow::FilterMeasurements(const QStringList &mNew, const QStringList &mFilter) { -#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) - const auto import = QSet(mNew.begin(), mNew.end()).subtract(QSet(mFilter.begin(), mFilter.end())); - return QStringList(import.begin(), import.end()); -#else - const QSet import = mNew.toSet().subtract(mFilter.toSet()); - return QStringList(import.toList()); -#endif + return ConvertToList(ConvertToSet(mNew).subtract(ConvertToSet(mFilter))); } //--------------------------------------------------------------------------------------------------------------------- @@ -3070,12 +3065,7 @@ QString TMainWindow::CheckMName(const QString &name, const QSet &import } else { -#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) - const QStringList allGroupNames = AllGroupNames(); - if (not QSet(allGroupNames.begin(), allGroupNames.end()).contains(name)) -#else - if (not AllGroupNames().toSet().contains(name)) -#endif + if (not ConvertToSet(AllGroupNames()).contains(name)) { throw VException(tr("Measurement '%1' is not one of known measurements.").arg(name)); } diff --git a/src/app/valentina/mainwindowsnogui.cpp b/src/app/valentina/mainwindowsnogui.cpp index 93c16beda..e0959071f 100644 --- a/src/app/valentina/mainwindowsnogui.cpp +++ b/src/app/valentina/mainwindowsnogui.cpp @@ -35,6 +35,7 @@ #include "../vwidgets/vmaingraphicsscene.h" #include "../vmisc/dialogs/dialogexporttocsv.h" #include "../vmisc/qxtcsvmodel.h" +#include "../vmisc/compatibility.h" #include "../vformat/vmeasurements.h" #include "../vformat/vwatermark.h" #include "../vlayout/vlayoutgenerator.h" @@ -2116,22 +2117,12 @@ QSharedPointer MainWindowsNoGUI::OpenMeasurementFile(const QStrin //--------------------------------------------------------------------------------------------------------------------- void MainWindowsNoGUI::CheckRequiredMeasurements(const VMeasurements *m) const { -#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) - QStringList patternMeasurments = doc->ListMeasurements(); - QStringList bodyMeasurments = m->ListAll(); - const auto match = QSet(patternMeasurments.begin(), patternMeasurments.end()) - .subtract(QSet(bodyMeasurments.begin(), bodyMeasurments.end())); -#else - const QSet match = doc->ListMeasurements().toSet().subtract(m->ListAll().toSet()); -#endif + const QSet match = ConvertToSet(doc->ListMeasurements()) + .subtract(ConvertToSet(m->ListAll())); if (not match.isEmpty()) { -#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) - QList list = QList(match.begin(), match.end()); -#else - QList list = match.toList(); -#endif + QList list = ConvertToList(match); for (int i = 0; i < list.size(); ++i) { list[i] = qApp->TrVars()->MToUser(list.at(i)); diff --git a/src/app/valentina/xml/vpattern.cpp b/src/app/valentina/xml/vpattern.cpp index 52c48abb6..5847a531f 100644 --- a/src/app/valentina/xml/vpattern.cpp +++ b/src/app/valentina/xml/vpattern.cpp @@ -43,6 +43,7 @@ #include "../vmisc/vsettings.h" #include "../vmisc/vmath.h" #include "../vmisc/projectversion.h" +#include "../vmisc/compatibility.h" #include "../qmuparser/qmuparsererror.h" #include "../qmuparser/qmutokenparser.h" #include "../vgeometry/varc.h" @@ -98,12 +99,7 @@ void GatherCount(int &count, const int nodes) QString DefLabelLanguage() { QString def = qApp->ValentinaSettings()->GetLabelLanguage(); -#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) - QStringList languages = VApplication::LabelLanguages(); - if (not QSet(languages.begin(), languages.end()).contains(def)) -#else - if (not VApplication::LabelLanguages().toSet().contains(def)) -#endif + if (not ConvertToSet(VApplication::LabelLanguages()).contains(def)) { def = QStringLiteral("en"); } @@ -4387,12 +4383,7 @@ void VPattern::SetLabelPrefix(const QString &prefix) if (not pattern.isNull()) { -#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) - QStringList languages = VApplication::LabelLanguages(); - if (QSet(languages.begin(), languages.end()).contains(prefix)) -#else - if (VApplication::LabelLanguages().toSet().contains(prefix)) -#endif + if (ConvertToSet(VApplication::LabelLanguages()).contains(prefix)) { SetAttribute(pattern, AttrLabelPrefix, prefix); modified = true; diff --git a/src/libs/ifc/xml/vabstractpattern.cpp b/src/libs/ifc/xml/vabstractpattern.cpp index 34a3c9e2e..84f905025 100644 --- a/src/libs/ifc/xml/vabstractpattern.cpp +++ b/src/libs/ifc/xml/vabstractpattern.cpp @@ -55,6 +55,7 @@ #include "vdomdocument.h" #include "vtoolrecord.h" #include "../vmisc/vabstractapplication.h" +#include "../vmisc/compatibility.h" #include "../vlayout/vtextmanager.h" class QDomElement; @@ -256,11 +257,7 @@ QList GetTokens(const VFormulaField &formula) //--------------------------------------------------------------------------------------------------------------------- void GatherTokens(QSet &tokens, const QList &tokenList) { -#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) - tokens = tokens.unite(QSet(tokenList.begin(), tokenList.end())); -#else - tokens = tokens.unite(tokenList.toSet()); -#endif + tokens = tokens.unite(ConvertToSet(tokenList)); } //--------------------------------------------------------------------------------------------------------------------- @@ -316,22 +313,11 @@ bool VAbstractPattern::RequiresMeasurements() const QStringList VAbstractPattern::ListMeasurements() const { const QFuture futureIncrements = QtConcurrent::run(this, &VAbstractPattern::ListIncrements); - -#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) - QSet results = QtConcurrent::blockingMappedReduced(ListExpressions(), GetTokens, GatherTokens); - const auto tokens = QList(results.begin(), results.end()); -#else - const QList tokens = QtConcurrent::blockingMappedReduced(ListExpressions(), GetTokens, - GatherTokens).toList(); -#endif + const QList tokens = ConvertToList(QtConcurrent::blockingMappedReduced(ListExpressions(), GetTokens, + GatherTokens)); QSet measurements; -#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) - QStringList result = futureIncrements.result(); - QSet others = QSet(result.begin(), result.end()); -#else - QSet others = futureIncrements.result().toSet(); -#endif + QSet others = ConvertToSet(futureIncrements.result()); for (const auto &token : tokens) { diff --git a/src/libs/vlayout/vlayoutgenerator.cpp b/src/libs/vlayout/vlayoutgenerator.cpp index c08a9b9a7..35b645933 100644 --- a/src/libs/vlayout/vlayoutgenerator.cpp +++ b/src/libs/vlayout/vlayoutgenerator.cpp @@ -35,6 +35,7 @@ #include "../vmisc/def.h" #include "../vmisc/vmath.h" +#include "../vmisc/compatibility.h" #include "vlayoutpiece.h" #include "vlayoutpaper.h" #include "../ifc/exception/vexceptionterminatedposition.h" @@ -642,11 +643,7 @@ QList VLayoutGenerator::MoveDetails(qreal length, const QVector= QT_VERSION_CHECK(5, 14, 0) - return QList(details.begin(), details.end()); -#else - return details.toList(); -#endif + return ConvertToList(details); } QList newDetails; diff --git a/src/libs/vlayout/vlayoutpiece.cpp b/src/libs/vlayout/vlayoutpiece.cpp index 62dcae9f0..0a200d00e 100644 --- a/src/libs/vlayout/vlayoutpiece.cpp +++ b/src/libs/vlayout/vlayoutpiece.cpp @@ -513,18 +513,11 @@ QVector VLayoutPiece::Map(QVector points) const if (d->mirror) { -#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) - auto list = QList(points.begin(), points.end()); -#else - QList list = points.toList(); -#endif + QList list = ConvertToList(points); + for (int k=0, s=list.size(), max=(s/2); k= QT_VERSION_CHECK(5, 13, 0) - list.swapItemsAt(k, s-(1+k)); -#else - list.swap(k, s-(1+k)); -#endif + SwapItemsAt(list, k, s-(1+k)); } points = list.toVector(); } diff --git a/src/libs/vmisc/compatibility.h b/src/libs/vmisc/compatibility.h index b99e2ce69..09a3d6cd2 100644 --- a/src/libs/vmisc/compatibility.h +++ b/src/libs/vmisc/compatibility.h @@ -69,4 +69,48 @@ inline typename T::IntersectType Intersects(const T &l1, const T &l2, QPointF *i #endif } +//--------------------------------------------------------------------------------------------------------------------- +template class C> +inline QList ConvertToList(const C &container) +{ +#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) + return T(container.begin(), container.end()); +#else + return container.toList(); +#endif +} + +//--------------------------------------------------------------------------------------------------------------------- +template class C> +inline QSet ConvertToSet(const C &container) +{ +#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) + return T(container.begin(), container.end()); +#else + return container.toSet(); +#endif +} + +//--------------------------------------------------------------------------------------------------------------------- +template +inline QSet ConvertToSet(const C &container) +{ +#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) + return T(container.begin(), container.end()); +#else + return container.toSet(); +#endif +} + +//--------------------------------------------------------------------------------------------------------------------- +template +inline void SwapItemsAt(T &container, int i, int j) +{ +#if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0) + container.swapItemsAt(i, j); +#else + container.swap(i, j); +#endif +} + #endif // COMPATIBILITY_H diff --git a/src/libs/vwidgets/vabstractmainwindow.cpp b/src/libs/vwidgets/vabstractmainwindow.cpp index 4521f7050..1c84ab114 100644 --- a/src/libs/vwidgets/vabstractmainwindow.cpp +++ b/src/libs/vwidgets/vabstractmainwindow.cpp @@ -29,6 +29,7 @@ #include "vabstractmainwindow.h" #include "../vpropertyexplorer/checkablemessagebox.h" #include "../vmisc/vabstractapplication.h" +#include "../vmisc/compatibility.h" #include "dialogs/dialogexporttocsv.h" #include @@ -79,7 +80,7 @@ QStringList ReverseList(const QStringList &list) reversedList.replace(j, list.at(i)); ++j; } - return reversedList.toList(); + return ConvertToList(reversedList); } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/test/ValentinaTest/tst_dxf.cpp b/src/test/ValentinaTest/tst_dxf.cpp index 88d95d8e4..5920ab2d2 100644 --- a/src/test/ValentinaTest/tst_dxf.cpp +++ b/src/test/ValentinaTest/tst_dxf.cpp @@ -30,6 +30,7 @@ #include #include "../vmisc/def.h" +#include "../vmisc/compatibility.h" #include "../vdxf/dxfdef.h" #include "../vdxf/libdxfrw/intern/drw_textcodec.h" @@ -45,11 +46,7 @@ QStringList AvailableCodecs() uniqueNames.insert(codec); } -#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) - return QStringList(uniqueNames.begin(), uniqueNames.end()); -#else - return QStringList(uniqueNames.toList()); -#endif + return ConvertToList(uniqueNames); } }