Skip to content

Commit

Permalink
change to allow compiling against older Qt versions
Browse files Browse the repository at this point in the history
  • Loading branch information
jkriege2 committed Mar 18, 2024
1 parent b2aad7c commit e25b494
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 42 deletions.
6 changes: 4 additions & 2 deletions lib/jkqtcommon/jkqtpcsstools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ JKQTPExpected<QGradient, JKQTPCSSParser::GeneralError> JKQTPCSSParser::parseGrad

if (get) getToken();

#if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0))
static QMap<QString,QGradient::Preset> s_GradientPresets = []() {
QMap<QString,QGradient::Preset> m;
for (int i=1; i<QMetaEnum::fromType<QGradient::Preset>().keyCount(); i++) {
Expand All @@ -220,7 +221,7 @@ JKQTPExpected<QGradient, JKQTPCSSParser::GeneralError> JKQTPCSSParser::parseGrad
}
return m;
}();

#endif
const QString func=CurrentToken.StringValue.trimmed().simplified().toLower();
if (CurrentToken.is(Token::TokenType::NAME) && func=="linear-gradient") {
QGradientStops colorStops;
Expand Down Expand Up @@ -321,10 +322,11 @@ JKQTPExpected<QGradient, JKQTPCSSParser::GeneralError> JKQTPCSSParser::parseGrad
lgrad.setStops(colorStops);
grad=lgrad;
}
#if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0))
} else if (CurrentToken.isNormStringAnyOf(s_GradientPresets.keys())) {
grad=QGradient(s_GradientPresets[CurrentToken.getNormString()]);
grad.setCoordinateMode(QGradient::ObjectBoundingMode);

#endif
} else {
return {JKQTPUnexpected, UnexpectedTermError("supported gradient-function [linear-gradient|] or predefined gradient name", CurrentToken, pos) };
}
Expand Down
13 changes: 8 additions & 5 deletions lib/jkqtcommon/jkqtpstringtools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ namespace {
m["diagcross"]=Qt::DiagCrossPattern;
return m;
}();

#if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0))
static QMap<QString,QGradient::Preset> s_GradientPresets = []() {
QMap<QString,QGradient::Preset> m;
for (int i=1; i<QMetaEnum::fromType<QGradient::Preset>().keyCount(); i++) {
Expand All @@ -399,6 +399,7 @@ namespace {
}
return m;
}();
#endif

}

Expand All @@ -422,13 +423,15 @@ Qt::BrushStyle jkqtp_String2QBrushStyleExt(const QString &style, QGradient *grad
qWarning()<<"error converting '"<<style<<"' into a QGradient: "<<E.what();
return Qt::SolidPattern;
}
#if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0))
} else if (s_GradientPresets.contains(s)) {
QGradient g(s_GradientPresets[s]);
g.setCoordinateMode(QGradient::ObjectBoundingMode);
if (gradient) *gradient=g;
if (g.type()==QGradient::Type::RadialGradient) return Qt::RadialGradientPattern;
else if (g.type()==QGradient::Type::ConicalGradient) return Qt::ConicalGradientPattern;
else return Qt::LinearGradientPattern;
#endif
} else if (jkqtp_rxExactlyMatches(s, "\\s*image\\s*\\(\\s*[\\\"\\\']?(.*)[\\\"\\\']?\\s*\\)\\s*", &caps)) {
if (image) *image=QPixmap(caps[1]);
return Qt::TexturePattern;
Expand Down Expand Up @@ -1097,7 +1100,7 @@ namespace JKQTCommon_private {
#endif
}

bool jkqtp_rxContains(const QString& text, const QString &regex, qsizetype offset, QStringList* caps)
bool jkqtp_rxContains(const QString& text, const QString &regex, size_t offset, QStringList* caps)
{
#if (QT_VERSION>=QT_VERSION_CHECK(6, 0, 0))
if (!JKQTCommon_private::rxCache.contains(regex)) JKQTCommon_private::rxCache.insert(regex, new QRegularExpression(regex));
Expand Down Expand Up @@ -1125,7 +1128,7 @@ bool jkqtp_rxContains(const QString& text, const QString &regex, qsizetype offse
}


qsizetype jkqtp_rxIndexIn(const QString& text, const QString &regex, qsizetype offset, QStringList* caps)
size_t jkqtp_rxIndexIn(const QString& text, const QString &regex, size_t offset, QStringList* caps)
{
#if (QT_VERSION>=QT_VERSION_CHECK(6, 0, 0))
if (!JKQTCommon_private::rxCache.contains(regex)) JKQTCommon_private::rxCache.insert(regex, new QRegularExpression(regex));
Expand All @@ -1147,14 +1150,14 @@ qsizetype jkqtp_rxIndexIn(const QString& text, const QString &regex, qsizetype o
tempRX.reset(new QRegExp(regex));
rx=tempRX.data();
}
const qsizetype idx = rx->indexIn(text, offset);
const size_t idx = rx->indexIn(text, offset);
if (caps) *caps=rx->capturedTexts();
return idx;
#endif
}


bool jkqtp_rxPartiallyMatchesAt(const QString& text, const QString &regex, qsizetype offset, QStringList* caps)
bool jkqtp_rxPartiallyMatchesAt(const QString& text, const QString &regex, size_t offset, QStringList* caps)
{
#if (QT_VERSION>=QT_VERSION_CHECK(6, 0, 0))
if (!JKQTCommon_private::rxCache.contains(regex)) JKQTCommon_private::rxCache.insert(regex, new QRegularExpression(regex));
Expand Down
6 changes: 3 additions & 3 deletions lib/jkqtcommon/jkqtpstringtools.h
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ JKQTCOMMON_LIB_EXPORT Qt::MouseButton jkqtp_String2MouseButton(const QString &bu
*
* \see jkqtp_rxExactlyMatches(), jkqtp_rxIndexIn(), jkqtp_rxContains(), jkqtp_rxPartiallyMatchesAt()
*/
JKQTCOMMON_LIB_EXPORT bool jkqtp_rxContains(const QString& text, const QString &regex, qsizetype offset=0, QStringList* caps=nullptr);
JKQTCOMMON_LIB_EXPORT bool jkqtp_rxContains(const QString& text, const QString &regex, size_t offset=0, QStringList* caps=nullptr);

/** \brief returns the next match (i.e. its index) of the given regular expression \a regex within \a text,
* starts from \a offset and optionally returns the match in \a caps \c =[fullmatch, cap1,cap2,...]
Expand All @@ -371,7 +371,7 @@ JKQTCOMMON_LIB_EXPORT bool jkqtp_rxContains(const QString& text, const QString &
*
* \see jkqtp_rxExactlyMatches(), jkqtp_rxIndexIn(), jkqtp_rxContains(), jkqtp_rxPartiallyMatchesAt()
*/
JKQTCOMMON_LIB_EXPORT qsizetype jkqtp_rxIndexIn(const QString& text, const QString &regex, qsizetype offset=0, QStringList* caps=nullptr);
JKQTCOMMON_LIB_EXPORT size_t jkqtp_rxIndexIn(const QString& text, const QString &regex, size_t offset=0, QStringList* caps=nullptr);

/** \brief returns \c true, if \a text exactly matches the given regular expression \a regex,
* starts from \a offset and optionally returns the match in \a caps \c =[fullmatch, cap1,cap2,...]
Expand All @@ -395,7 +395,7 @@ JKQTCOMMON_LIB_EXPORT bool jkqtp_rxExactlyMatches(const QString& text, const QSt
*
* \see jkqtp_rxExactlyMatches(), jkqtp_rxIndexIn(), jkqtp_rxContains(), jkqtp_rxPartiallyMatchesAt()
*/
JKQTCOMMON_LIB_EXPORT bool jkqtp_rxPartiallyMatchesAt(const QString& text, const QString &regex, qsizetype offset=0, QStringList* caps=nullptr);
JKQTCOMMON_LIB_EXPORT bool jkqtp_rxPartiallyMatchesAt(const QString& text, const QString &regex, size_t offset=0, QStringList* caps=nullptr);


#endif // JKQTPSTRINGTOOLS_H_INCLUDED
45 changes: 23 additions & 22 deletions lib/jkqtmathtext/jkqtmathtexttools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -997,35 +997,36 @@ namespace {

}

template<>
struct std::hash<JKQTMathTextCacheKeyBase>
{
std::size_t operator()(const JKQTMathTextCacheKeyBase& data) const noexcept
namespace std {
template<>
struct hash<JKQTMathTextCacheKeyBase>
{
return qHash(data.f)+std::hash<int>()(data.ldpiX)+std::hash<int>()(data.ldpiY)+std::hash<int>()(data.pdpiX)+std::hash<int>()(data.pdpiY);
}
};
size_t operator()(const JKQTMathTextCacheKeyBase& data) const noexcept
{
return qHash(data.f)+hash<int>()(data.ldpiX)+hash<int>()(data.ldpiY)+hash<int>()(data.pdpiX)+hash<int>()(data.pdpiY);
}
};



template<>
struct std::hash<JKQTMathTextTBRDataH<QString>>
{
std::size_t operator()(const JKQTMathTextTBRDataH<QString>& data) const noexcept
template<>
struct hash<JKQTMathTextTBRDataH<QString>>
{
return qHash(data.f)+qHash(data.text)+std::hash<int>()(data.ldpiX)+std::hash<int>()(data.ldpiY)+std::hash<int>()(data.pdpiX)+std::hash<int>()(data.pdpiY);
}
};
size_t operator()(const JKQTMathTextTBRDataH<QString>& data) const noexcept
{
return qHash(data.f)+qHash(data.text)+hash<int>()(data.ldpiX)+hash<int>()(data.ldpiY)+hash<int>()(data.pdpiX)+hash<int>()(data.pdpiY);
}
};

template<>
struct std::hash<JKQTMathTextTBRDataH<QChar>>
{
std::size_t operator()(const JKQTMathTextTBRDataH<QChar>& data) const noexcept
template<>
struct hash<JKQTMathTextTBRDataH<QChar>>
{
return qHash(data.f)+qHash(data.text)+std::hash<int>()(data.ldpiX)+std::hash<int>()(data.ldpiY)+std::hash<int>()(data.pdpiX)+std::hash<int>()(data.pdpiY);
}
};

size_t operator()(const JKQTMathTextTBRDataH<QChar>& data) const noexcept
{
return qHash(data.f)+qHash(data.text)+hash<int>()(data.ldpiX)+hash<int>()(data.ldpiY)+hash<int>()(data.pdpiX)+hash<int>()(data.pdpiY);
}
};
}


QRectF JKQTMathTextGetTightBoundingRect(const QFont &f, const QString &text, QPaintDevice *pd)
Expand Down
8 changes: 4 additions & 4 deletions lib/jkqtmathtext/parsers/jkqtmathtextlatexparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,7 @@ JKQTMathTextNode* JKQTMathTextLatexParser::parseLatexString(bool get, JKQTMathTe
bool first=true;
bool firstLine=true;
QVector<JKQTMathTextNode*> line;
qsizetype colCount=0;
size_t colCount=0;
//qDebug()<<"start "<<envname;
while (first || currentToken==MTTampersand || currentToken==MTTinstructionNewline) {
//qDebug()<<" - START: "<<tokenType2String(currentToken)<<" first="<<first;
Expand Down Expand Up @@ -930,12 +930,12 @@ JKQTMathTextNode* JKQTMathTextLatexParser::parseLatexString(bool get, JKQTMathTe
line.append(it);

if (currentToken==MTTinstructionNewline || (currentToken==MTTinstructionEnd && currentTokenName==envname) || line.size()>0) {
colCount=qMax(colCount, static_cast<qsizetype>(line.size()));
colCount=qMax(colCount, static_cast<size_t>(line.size()));
//qDebug()<<" - colCount="<<colCount;
if (line.size()==0 || (line.size()>=1 && static_cast<qsizetype>(line.size())==colCount)) {
if (line.size()==0 || (line.size()>=1 && static_cast<size_t>(line.size())==colCount)) {
items.append(line);
//qDebug()<<" - appending line with "<<line.size()<<" items. items.size now "<<items.size();
} else if (line.size()>=1 && static_cast<qsizetype>(line.size())!=colCount) {
} else if (line.size()>=1 && static_cast<size_t>(line.size())!=colCount) {
addToErrorList(tr("error @ ch. %1: wrong number of entries widthin '\\begin{%2}...\\end{%2}'").arg(currentTokenID).arg(envname));
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/jkqtplotter/jkqtpbaseplotter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3741,7 +3741,7 @@ bool JKQTBasePlotter::saveImage(const QString& filename, bool displayPreview) {
QSharedPointer<QPaintDevice> paintDevice=QSharedPointer<QPaintDevice>(jkqtpPaintDeviceAdapters.get()[adapterID]->createPaintdevice(fn, jkqtp_roundTo<int>(gridPrintingSize.width()), jkqtp_roundTo<int>(gridPrintingSize.height())));

#ifndef JKQTPLOTTER_COMPILE_WITHOUT_PRINTSUPPORT
if (!printpreviewNew(paintDevice.get(), jkqtpPaintDeviceAdapters.get()[adapterID]->getSetAbsolutePaperSize(), jkqtpPaintDeviceAdapters.get()[adapterID]->getPrintSizeXInMM(), jkqtpPaintDeviceAdapters.get()[adapterID]->getPrintSizeYInMM(), displayPreview)) {
if (!printpreviewNew(paintDevice.data(), jkqtpPaintDeviceAdapters.get()[adapterID]->getSetAbsolutePaperSize(), jkqtpPaintDeviceAdapters.get()[adapterID]->getPrintSizeXInMM(), jkqtpPaintDeviceAdapters.get()[adapterID]->getPrintSizeYInMM(), displayPreview)) {
if (QFile::exists(tempFM)) {
QFile::copy(tempFM, fn);
QFile::remove(tempFM);
Expand All @@ -3751,7 +3751,7 @@ bool JKQTBasePlotter::saveImage(const QString& filename, bool displayPreview) {
#endif
{
paintDevice.reset(jkqtpPaintDeviceAdapters.get()[adapterID]->createPaintdeviceMM(fn,printSizeX_Millimeter,printSizeY_Millimeter));
printpreviewPaintRequestedNewPaintDevice(paintDevice.get());
printpreviewPaintRequestedNewPaintDevice(paintDevice.data());
return true;
}

Expand Down
4 changes: 4 additions & 0 deletions lib/jkqtplotter/jkqtpgraphsbasestyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,11 @@ QBrush JKQTFillStyleSummmary::brush(const QColor &color) const
if (brushStyle==Qt::LinearGradientPattern || brushStyle==Qt::RadialGradientPattern || brushStyle==Qt::ConicalGradientPattern) {
QGradient g=gradient;
JKQTPReplaceCurrentColor(g, color);
#if QT_VERSION >= QT_VERSION_CHECK(5,12,0)
g.setCoordinateMode(QGradient::ObjectMode);
#else
g.setCoordinateMode(QGradient::ObjectBoundingMode);
#endif
b=QBrush(g);
} else {
b.setStyle(brushStyle);
Expand Down
5 changes: 4 additions & 1 deletion tests/jkqtcommmon/JKQTPCSSParser_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,12 @@ private slots:

QVERIFY_THROWS_EXCEPTION(std::exception, n=JKQTPCSSParser::readGradient("wa__flame"));

#if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0))
QVERIFY_THROWS_NO_EXCEPTION(n=JKQTPCSSParser::readGradient("warmflame"));
g = QGradient(QGradient::WarmFlame);
g.setCoordinateMode(QGradient::ObjectBoundingMode);
QCOMPARE_EQ(n, g);

#endif

QVERIFY_THROWS_NO_EXCEPTION(n=JKQTPCSSParser::readGradient("linear-gradient(to left, red, blue)"));
lg = QLinearGradient(1,0.5,0,0.5);
Expand Down Expand Up @@ -171,11 +172,13 @@ private slots:
QLinearGradient lg;


#if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0))
QVERIFY_THROWS_NO_EXCEPTION(bs=jkqtp_String2QBrushStyleExt("warmflame", &n, nullptr));
g = QGradient(QGradient::WarmFlame);
g.setCoordinateMode(QGradient::ObjectBoundingMode);
QCOMPARE_EQ(n, g);
QCOMPARE_EQ(bs, Qt::LinearGradientPattern);
#endif

QVERIFY_THROWS_NO_EXCEPTION(bs=jkqtp_String2QBrushStyleExt("d1", &n, nullptr));
QCOMPARE_EQ(bs, Qt::Dense1Pattern);
Expand Down
2 changes: 2 additions & 0 deletions tests/jkqtcommmon/JKQTPStringTools_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ private slots:
QLinearGradient lg;


#if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0))
QVERIFY_THROWS_NO_EXCEPTION(bs=jkqtp_String2QBrushStyleExt("warmflame", &n, nullptr));
g = QGradient(QGradient::WarmFlame);
g.setCoordinateMode(QGradient::ObjectBoundingMode);
QCOMPARE_EQ(n, g);
QCOMPARE_EQ(bs, Qt::LinearGradientPattern);
#endif

QVERIFY_THROWS_NO_EXCEPTION(bs=jkqtp_String2QBrushStyleExt("d1", &n, nullptr));
QCOMPARE_EQ(bs, Qt::Dense1Pattern);
Expand Down
9 changes: 6 additions & 3 deletions tools/jkqtmathtext_render/jkqtmathtext_render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,17 @@ int main(int argc, char* argv[])
fileList<<" <table>\n";
fileList<<" <tr>\n";
i=1;

auto myIsLower=[](const QString& s) { for (size_t i=0; i<s.size(); i++) if (!s[i].isLower()) return false; return true; };
auto myIsUpper=[](const QString& s) { for (size_t i=0; i<s.size(); i++) if (!s[i].isUpper()) return false; return true; };
#if (QT_VERSION>=QT_VERSION_CHECK(6, 0, 0))
std::sort
#else
qSort
#endif
(symbolsAll.begin(), symbolsAll.end(), [](const QString& a, const QString& b) { if (a.contains("harpoon") && !b.contains("harpoon")) return false;
else if (a.isLower() && b.isUpper()) return true;
else if (a.isUpper() && b.isLower()) return false;
(symbolsAll.begin(), symbolsAll.end(), [myIsLower,myIsUpper](const QString& a, const QString& b) { if (a.contains("harpoon") && !b.contains("harpoon")) return false;
else if (myIsLower(a) && myIsUpper(b)) return true;
else if (myIsUpper(a) && myIsLower(b)) return false;
else return a<b;
});
for (const QString& arrow: arrowNames) {
Expand Down

0 comments on commit e25b494

Please sign in to comment.