Refactoring. Hide compatibility layer behind a function.

toSet(), toList(), swapItemsAt().

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2020-01-16 19:09:23 +02:00
parent 98a88ed529
commit 7e3c7e0f88
9 changed files with 68 additions and 78 deletions

View File

@ -43,6 +43,7 @@
#include "../vmisc/vsysexits.h" #include "../vmisc/vsysexits.h"
#include "../vmisc/qxtcsvmodel.h" #include "../vmisc/qxtcsvmodel.h"
#include "../vmisc/dialogs/dialogexporttocsv.h" #include "../vmisc/dialogs/dialogexporttocsv.h"
#include "../vmisc/compatibility.h"
#include "vlitepattern.h" #include "vlitepattern.h"
#include "../qmuparser/qmudef.h" #include "../qmuparser/qmudef.h"
#include "../vtools/dialogs/support/dialogeditwrongformula.h" #include "../vtools/dialogs/support/dialogeditwrongformula.h"
@ -2795,13 +2796,7 @@ void TMainWindow::WriteSettings()
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QStringList TMainWindow::FilterMeasurements(const QStringList &mNew, const QStringList &mFilter) QStringList TMainWindow::FilterMeasurements(const QStringList &mNew, const QStringList &mFilter)
{ {
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) return ConvertToList(ConvertToSet<QString>(mNew).subtract(ConvertToSet<QString>(mFilter)));
const auto import = QSet<QString>(mNew.begin(), mNew.end()).subtract(QSet<QString>(mFilter.begin(), mFilter.end()));
return QStringList(import.begin(), import.end());
#else
const QSet<QString> import = mNew.toSet().subtract(mFilter.toSet());
return QStringList(import.toList());
#endif
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -3070,12 +3065,7 @@ QString TMainWindow::CheckMName(const QString &name, const QSet<QString> &import
} }
else else
{ {
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) if (not ConvertToSet<QString>(AllGroupNames()).contains(name))
const QStringList allGroupNames = AllGroupNames();
if (not QSet<QString>(allGroupNames.begin(), allGroupNames.end()).contains(name))
#else
if (not AllGroupNames().toSet().contains(name))
#endif
{ {
throw VException(tr("Measurement '%1' is not one of known measurements.").arg(name)); throw VException(tr("Measurement '%1' is not one of known measurements.").arg(name));
} }

View File

@ -35,6 +35,7 @@
#include "../vwidgets/vmaingraphicsscene.h" #include "../vwidgets/vmaingraphicsscene.h"
#include "../vmisc/dialogs/dialogexporttocsv.h" #include "../vmisc/dialogs/dialogexporttocsv.h"
#include "../vmisc/qxtcsvmodel.h" #include "../vmisc/qxtcsvmodel.h"
#include "../vmisc/compatibility.h"
#include "../vformat/vmeasurements.h" #include "../vformat/vmeasurements.h"
#include "../vformat/vwatermark.h" #include "../vformat/vwatermark.h"
#include "../vlayout/vlayoutgenerator.h" #include "../vlayout/vlayoutgenerator.h"
@ -2116,22 +2117,12 @@ QSharedPointer<VMeasurements> MainWindowsNoGUI::OpenMeasurementFile(const QStrin
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void MainWindowsNoGUI::CheckRequiredMeasurements(const VMeasurements *m) const void MainWindowsNoGUI::CheckRequiredMeasurements(const VMeasurements *m) const
{ {
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) const QSet<QString> match = ConvertToSet<QString>(doc->ListMeasurements())
QStringList patternMeasurments = doc->ListMeasurements(); .subtract(ConvertToSet<QString>(m->ListAll()));
QStringList bodyMeasurments = m->ListAll();
const auto match = QSet<QString>(patternMeasurments.begin(), patternMeasurments.end())
.subtract(QSet<QString>(bodyMeasurments.begin(), bodyMeasurments.end()));
#else
const QSet<QString> match = doc->ListMeasurements().toSet().subtract(m->ListAll().toSet());
#endif
if (not match.isEmpty()) if (not match.isEmpty())
{ {
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) QList<QString> list = ConvertToList(match);
QList<QString> list = QList<QString>(match.begin(), match.end());
#else
QList<QString> list = match.toList();
#endif
for (int i = 0; i < list.size(); ++i) for (int i = 0; i < list.size(); ++i)
{ {
list[i] = qApp->TrVars()->MToUser(list.at(i)); list[i] = qApp->TrVars()->MToUser(list.at(i));

View File

@ -43,6 +43,7 @@
#include "../vmisc/vsettings.h" #include "../vmisc/vsettings.h"
#include "../vmisc/vmath.h" #include "../vmisc/vmath.h"
#include "../vmisc/projectversion.h" #include "../vmisc/projectversion.h"
#include "../vmisc/compatibility.h"
#include "../qmuparser/qmuparsererror.h" #include "../qmuparser/qmuparsererror.h"
#include "../qmuparser/qmutokenparser.h" #include "../qmuparser/qmutokenparser.h"
#include "../vgeometry/varc.h" #include "../vgeometry/varc.h"
@ -98,12 +99,7 @@ void GatherCount(int &count, const int nodes)
QString DefLabelLanguage() QString DefLabelLanguage()
{ {
QString def = qApp->ValentinaSettings()->GetLabelLanguage(); QString def = qApp->ValentinaSettings()->GetLabelLanguage();
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) if (not ConvertToSet<QString>(VApplication::LabelLanguages()).contains(def))
QStringList languages = VApplication::LabelLanguages();
if (not QSet<QString>(languages.begin(), languages.end()).contains(def))
#else
if (not VApplication::LabelLanguages().toSet().contains(def))
#endif
{ {
def = QStringLiteral("en"); def = QStringLiteral("en");
} }
@ -4387,12 +4383,7 @@ void VPattern::SetLabelPrefix(const QString &prefix)
if (not pattern.isNull()) if (not pattern.isNull())
{ {
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) if (ConvertToSet<QString>(VApplication::LabelLanguages()).contains(prefix))
QStringList languages = VApplication::LabelLanguages();
if (QSet<QString>(languages.begin(), languages.end()).contains(prefix))
#else
if (VApplication::LabelLanguages().toSet().contains(prefix))
#endif
{ {
SetAttribute(pattern, AttrLabelPrefix, prefix); SetAttribute(pattern, AttrLabelPrefix, prefix);
modified = true; modified = true;

View File

@ -55,6 +55,7 @@
#include "vdomdocument.h" #include "vdomdocument.h"
#include "vtoolrecord.h" #include "vtoolrecord.h"
#include "../vmisc/vabstractapplication.h" #include "../vmisc/vabstractapplication.h"
#include "../vmisc/compatibility.h"
#include "../vlayout/vtextmanager.h" #include "../vlayout/vtextmanager.h"
class QDomElement; class QDomElement;
@ -256,11 +257,7 @@ QList<QString> GetTokens(const VFormulaField &formula)
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void GatherTokens(QSet<QString> &tokens, const QList<QString> &tokenList) void GatherTokens(QSet<QString> &tokens, const QList<QString> &tokenList)
{ {
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) tokens = tokens.unite(ConvertToSet(tokenList));
tokens = tokens.unite(QSet<QString>(tokenList.begin(), tokenList.end()));
#else
tokens = tokens.unite(tokenList.toSet());
#endif
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -316,22 +313,11 @@ bool VAbstractPattern::RequiresMeasurements() const
QStringList VAbstractPattern::ListMeasurements() const QStringList VAbstractPattern::ListMeasurements() const
{ {
const QFuture<QStringList> futureIncrements = QtConcurrent::run(this, &VAbstractPattern::ListIncrements); const QFuture<QStringList> futureIncrements = QtConcurrent::run(this, &VAbstractPattern::ListIncrements);
const QList<QString> tokens = ConvertToList(QtConcurrent::blockingMappedReduced(ListExpressions(), GetTokens,
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) GatherTokens));
QSet<QString> results = QtConcurrent::blockingMappedReduced(ListExpressions(), GetTokens, GatherTokens);
const auto tokens = QList<QString>(results.begin(), results.end());
#else
const QList<QString> tokens = QtConcurrent::blockingMappedReduced(ListExpressions(), GetTokens,
GatherTokens).toList();
#endif
QSet<QString> measurements; QSet<QString> measurements;
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) QSet<QString> others = ConvertToSet<QString>(futureIncrements.result());
QStringList result = futureIncrements.result();
QSet<QString> others = QSet<QString>(result.begin(), result.end());
#else
QSet<QString> others = futureIncrements.result().toSet();
#endif
for (const auto &token : tokens) for (const auto &token : tokens)
{ {

View File

@ -35,6 +35,7 @@
#include "../vmisc/def.h" #include "../vmisc/def.h"
#include "../vmisc/vmath.h" #include "../vmisc/vmath.h"
#include "../vmisc/compatibility.h"
#include "vlayoutpiece.h" #include "vlayoutpiece.h"
#include "vlayoutpaper.h" #include "vlayoutpaper.h"
#include "../ifc/exception/vexceptionterminatedposition.h" #include "../ifc/exception/vexceptionterminatedposition.h"
@ -642,11 +643,7 @@ QList<VLayoutPiece> VLayoutGenerator::MoveDetails(qreal length, const QVector<VL
{ {
if (qFuzzyIsNull(length)) if (qFuzzyIsNull(length))
{ {
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) return ConvertToList(details);
return QList<VLayoutPiece>(details.begin(), details.end());
#else
return details.toList();
#endif
} }
QList<VLayoutPiece> newDetails; QList<VLayoutPiece> newDetails;

View File

@ -513,18 +513,11 @@ QVector<T> VLayoutPiece::Map(QVector<T> points) const
if (d->mirror) if (d->mirror)
{ {
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) QList<T> list = ConvertToList(points);
auto list = QList<T>(points.begin(), points.end());
#else
QList<T> list = points.toList();
#endif
for (int k=0, s=list.size(), max=(s/2); k<max; k++) for (int k=0, s=list.size(), max=(s/2); k<max; k++)
{ {
#if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0) SwapItemsAt(list, k, s-(1+k));
list.swapItemsAt(k, s-(1+k));
#else
list.swap(k, s-(1+k));
#endif
} }
points = list.toVector(); points = list.toVector();
} }

View File

@ -69,4 +69,48 @@ inline typename T::IntersectType Intersects(const T &l1, const T &l2, QPointF *i
#endif #endif
} }
//---------------------------------------------------------------------------------------------------------------------
template <typename T, template <typename> class C>
inline QList<T> ConvertToList(const C<T> &container)
{
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
return T(container.begin(), container.end());
#else
return container.toList();
#endif
}
//---------------------------------------------------------------------------------------------------------------------
template <typename T, template <typename> class C>
inline QSet<T> ConvertToSet(const C<T> &container)
{
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
return T(container.begin(), container.end());
#else
return container.toSet();
#endif
}
//---------------------------------------------------------------------------------------------------------------------
template <typename T, typename C>
inline QSet<T> 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 <typename T>
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 #endif // COMPATIBILITY_H

View File

@ -29,6 +29,7 @@
#include "vabstractmainwindow.h" #include "vabstractmainwindow.h"
#include "../vpropertyexplorer/checkablemessagebox.h" #include "../vpropertyexplorer/checkablemessagebox.h"
#include "../vmisc/vabstractapplication.h" #include "../vmisc/vabstractapplication.h"
#include "../vmisc/compatibility.h"
#include "dialogs/dialogexporttocsv.h" #include "dialogs/dialogexporttocsv.h"
#include <QStyle> #include <QStyle>
@ -79,7 +80,7 @@ QStringList ReverseList(const QStringList &list)
reversedList.replace(j, list.at(i)); reversedList.replace(j, list.at(i));
++j; ++j;
} }
return reversedList.toList(); return ConvertToList(reversedList);
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------

View File

@ -30,6 +30,7 @@
#include <QtTest> #include <QtTest>
#include "../vmisc/def.h" #include "../vmisc/def.h"
#include "../vmisc/compatibility.h"
#include "../vdxf/dxfdef.h" #include "../vdxf/dxfdef.h"
#include "../vdxf/libdxfrw/intern/drw_textcodec.h" #include "../vdxf/libdxfrw/intern/drw_textcodec.h"
@ -45,11 +46,7 @@ QStringList AvailableCodecs()
uniqueNames.insert(codec); uniqueNames.insert(codec);
} }
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) return ConvertToList(uniqueNames);
return QStringList(uniqueNames.begin(), uniqueNames.end());
#else
return QStringList(uniqueNames.toList());
#endif
} }
} }