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/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<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
return ConvertToList(ConvertToSet<QString>(mNew).subtract(ConvertToSet<QString>(mFilter)));
}
//---------------------------------------------------------------------------------------------------------------------
@ -3070,12 +3065,7 @@ QString TMainWindow::CheckMName(const QString &name, const QSet<QString> &import
}
else
{
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
const QStringList allGroupNames = AllGroupNames();
if (not QSet<QString>(allGroupNames.begin(), allGroupNames.end()).contains(name))
#else
if (not AllGroupNames().toSet().contains(name))
#endif
if (not ConvertToSet<QString>(AllGroupNames()).contains(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 "../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<VMeasurements> 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<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
const QSet<QString> match = ConvertToSet<QString>(doc->ListMeasurements())
.subtract(ConvertToSet<QString>(m->ListAll()));
if (not match.isEmpty())
{
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
QList<QString> list = QList<QString>(match.begin(), match.end());
#else
QList<QString> list = match.toList();
#endif
QList<QString> list = ConvertToList(match);
for (int i = 0; i < list.size(); ++i)
{
list[i] = qApp->TrVars()->MToUser(list.at(i));

View File

@ -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<QString>(languages.begin(), languages.end()).contains(def))
#else
if (not VApplication::LabelLanguages().toSet().contains(def))
#endif
if (not ConvertToSet<QString>(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<QString>(languages.begin(), languages.end()).contains(prefix))
#else
if (VApplication::LabelLanguages().toSet().contains(prefix))
#endif
if (ConvertToSet<QString>(VApplication::LabelLanguages()).contains(prefix))
{
SetAttribute(pattern, AttrLabelPrefix, prefix);
modified = true;

View File

@ -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<QString> GetTokens(const VFormulaField &formula)
//---------------------------------------------------------------------------------------------------------------------
void GatherTokens(QSet<QString> &tokens, const QList<QString> &tokenList)
{
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
tokens = tokens.unite(QSet<QString>(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<QStringList> futureIncrements = QtConcurrent::run(this, &VAbstractPattern::ListIncrements);
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
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
const QList<QString> tokens = ConvertToList(QtConcurrent::blockingMappedReduced(ListExpressions(), GetTokens,
GatherTokens));
QSet<QString> measurements;
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
QStringList result = futureIncrements.result();
QSet<QString> others = QSet<QString>(result.begin(), result.end());
#else
QSet<QString> others = futureIncrements.result().toSet();
#endif
QSet<QString> others = ConvertToSet<QString>(futureIncrements.result());
for (const auto &token : tokens)
{

View File

@ -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<VLayoutPiece> VLayoutGenerator::MoveDetails(qreal length, const QVector<VL
{
if (qFuzzyIsNull(length))
{
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
return QList<VLayoutPiece>(details.begin(), details.end());
#else
return details.toList();
#endif
return ConvertToList(details);
}
QList<VLayoutPiece> newDetails;

View File

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

View File

@ -69,4 +69,48 @@ inline typename T::IntersectType Intersects(const T &l1, const T &l2, QPointF *i
#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

View File

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

View File

@ -30,6 +30,7 @@
#include <QtTest>
#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);
}
}