Skip to content

Commit

Permalink
Fix compilation with Qt ≥ 6.8 (#307)
Browse files Browse the repository at this point in the history
* Fix compilation with Qt ≥ 6.8

Closes #306

* Updated `QDomDocument::setContent`
  • Loading branch information
tsujan authored Oct 12, 2024
1 parent d302e19 commit 91ed8fd
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/qtxdg/xdgmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ void XdgMenuPrivate::load(const QString& fileName)
qWarning() << QString::fromLatin1("%1 not loading: %2").arg(fileName, file.errorString());
return;
}
mXml.setContent(&file, true);
mXml.setContent(&file, QDomDocument::ParseOption::UseNamespaceProcessing);
}


Expand Down
13 changes: 5 additions & 8 deletions src/qtxdg/xdgmenureader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,13 @@ bool XdgMenuReader::load(const QString& fileName, const QString& baseDir)
//qDebug() << "Load file:" << mFileName;
mMenu->addWatchPath(mFileName);

QString errorStr;
int errorLine;
int errorColumn;

if (!mXml.setContent(&file, true, &errorStr, &errorLine, &errorColumn))
QDomDocument::ParseResult res = mXml.setContent(&file, QDomDocument::ParseOption::UseNamespaceProcessing);
if (!res)
{
mErrorStr = QString::fromLatin1("Parse error at line %1, column %2:\n%3")
.arg(errorLine)
.arg(errorColumn)
.arg(errorStr);
.arg(res.errorLine)
.arg(res.errorColumn)
.arg(res.errorMessage);
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions src/qtxdg/xdgmimetype.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ class QTXDG_API XdgMimeType : public QMimeType {
*/
bool operator==(const XdgMimeType &other) const
{
return QMimeType::operator==(other);
return name() == other.name();
}

inline bool operator!=(const XdgMimeType &other) const
{
return !QMimeType::operator==(other);
return !operator==(other);
}

void swap(XdgMimeType &other) noexcept;
Expand Down
92 changes: 85 additions & 7 deletions src/xdgiconloader/xdgiconloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,11 @@ QSize XdgIconLoaderEngine::actualSize(const QSize &size, QIcon::Mode mode,
}

// XXX: duplicated from qiconloader.cpp, because this symbol isn't exported :(
#if (QT_VERSION >= QT_VERSION_CHECK(6,8,0))
QPixmap PixmapEntry::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state, qreal scale)
#else
QPixmap PixmapEntry::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state)
#endif
{
Q_UNUSED(state);

Expand All @@ -766,18 +770,45 @@ QPixmap PixmapEntry::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State st
if (basePixmap.isNull())
basePixmap.load(filename);

// see QPixmapIconEngine::adjustSize
QSize actualSize = basePixmap.size();
// If the size of the best match we have (basePixmap) is larger than the
// requested size, we downscale it to match.
if (!actualSize.isNull() && (actualSize.width() > size.width() || actualSize.height() > size.height()))
actualSize.scale(size, Qt::KeepAspectRatio);

#if (QT_VERSION >= QT_VERSION_CHECK(6,8,0))
// see QIconPrivate::pixmapDevicePixelRatio
qreal calculatedDpr;
QSize targetSize = size * scale;
if ((actualSize.width() == targetSize.width() && actualSize.height() <= targetSize.height()) ||
(actualSize.width() <= targetSize.width() && actualSize.height() == targetSize.height()))
{
// Correctly scaled for dpr, just having different aspect ratio
calculatedDpr = scale;
}
else
{
qreal ratio = 0.5 * (qreal(actualSize.width()) / qreal(targetSize.width()) +
qreal(actualSize.height() / qreal(targetSize.height())));
calculatedDpr = qMax(qreal(1.0), scale * ratio);
}

QString key = QLatin1String("$qt_theme_")
% HexString<quint64>(basePixmap.cacheKey())
% HexString<quint8>(mode)
% HexString<quint64>(QGuiApplication::palette().cacheKey())
% HexString<uint>(actualSize.width())
% HexString<uint>(actualSize.height())
% HexString<quint16>(qRound(calculatedDpr * 1000));
#else
QString key = QLatin1String("$qt_theme_")
% HexString<qint64>(basePixmap.cacheKey())
% HexString<int>(mode)
% HexString<qint64>(QGuiApplication::palette().cacheKey())
% HexString<int>(actualSize.width())
% HexString<int>(actualSize.height());
#endif

QPixmap cachedPixmap;
if (QPixmapCache::find(key, &cachedPixmap)) {
Expand All @@ -789,28 +820,49 @@ QPixmap PixmapEntry::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State st
cachedPixmap = basePixmap;
if (QGuiApplication *guiApp = qobject_cast<QGuiApplication *>(qApp))
cachedPixmap = static_cast<QGuiApplicationPrivate*>(QObjectPrivate::get(guiApp))->applyQIconStyleHelper(mode, cachedPixmap);
#if (QT_VERSION >= QT_VERSION_CHECK(6,8,0))
cachedPixmap.setDevicePixelRatio(calculatedDpr);
#endif
QPixmapCache::insert(key, cachedPixmap);
}
return cachedPixmap;
}

// NOTE: For SVG, QSvgRenderer is used to prevent our icon handling from
// being broken by icon engines that register themselves for SVG.
#if (QT_VERSION >= QT_VERSION_CHECK(6,8,0))
QPixmap ScalableEntry::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state, qreal scale)
#else
QPixmap ScalableEntry::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state)
#endif
{
QPixmap pm;
if (size.isEmpty())
return pm;

#if (QT_VERSION >= QT_VERSION_CHECK(6,8,0))
QString key = QLatin1String("lxqt_")
% filename
% HexString<quint8>(mode)
% HexString<int>(state)
% HexString<uint>(size.width())
% HexString<uint>(size.height())
% HexString<quint16>(qRound(scale * 1000));
#else
QString key = QLatin1String("lxqt_")
% filename
% HexString<int>(mode)
% HexString<int>(state)
% HexString<int>(size.width())
% HexString<int>(size.height());
#endif
if (!QPixmapCache::find(key, &pm))
{
#if (QT_VERSION >= QT_VERSION_CHECK(6,8,0))
int icnSize = qMin(size.width(), size.height()) * scale;
#else
int icnSize = qMin(size.width(), size.height());
#endif
pm = QPixmap(icnSize, icnSize);
pm.fill(Qt::transparent);

Expand All @@ -824,8 +876,12 @@ QPixmap ScalableEntry::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State
}

svgIcon = QIcon(pm);
#if (QT_VERSION >= QT_VERSION_CHECK(6,8,0))
pm = svgIcon.pixmap(size, scale, mode, state);
#else
if (QIconEngine *engine = svgIcon.data_ptr() ? svgIcon.data_ptr()->engine : nullptr)
pm = engine->pixmap(size, mode, state);
#endif
QPixmapCache::insert(key, pm);
}

Expand All @@ -838,7 +894,11 @@ static const QString STYLE = QStringLiteral("\n.ColorScheme-Text, .ColorScheme-N
// NOTE: Qt palette does not have any colors for positive/negative text
// .ColorScheme-PositiveText,ColorScheme-NegativeText {color:%4;}

#if (QT_VERSION >= QT_VERSION_CHECK(6,8,0))
QPixmap ScalableFollowsColorEntry::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state, qreal scale)
#else
QPixmap ScalableFollowsColorEntry::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state)
#endif
{
QPixmap pm;
if (size.isEmpty())
Expand Down Expand Up @@ -866,16 +926,31 @@ QPixmap ScalableFollowsColorEntry::pixmap(const QSize &size, QIcon::Mode mode, Q
}
hCol = pal.highlight().color().name();
}
#if (QT_VERSION >= QT_VERSION_CHECK(6,8,0))
QString key = QLatin1String("lxqt_")
% filename
% HexString<quint8>(mode)
% HexString<int>(state)
% HexString<uint>(size.width())
% HexString<uint>(size.height())
% HexString<quint16>(qRound(scale * 1000))
% txtCol % bgCol % hCol;
#else
QString key = QLatin1String("lxqt_")
% filename
% HexString<int>(mode)
% HexString<int>(state)
% HexString<int>(size.width())
% HexString<int>(size.height())
% txtCol % bgCol % hCol;
#endif
if (!QPixmapCache::find(key, &pm))
{
#if (QT_VERSION >= QT_VERSION_CHECK(6,8,0))
int icnSize = qMin(size.width(), size.height()) * scale;
#else
int icnSize = qMin(size.width(), size.height());
#endif
pm = QPixmap(icnSize, icnSize);
pm.fill(Qt::transparent);

Expand Down Expand Up @@ -926,8 +1001,12 @@ QPixmap ScalableFollowsColorEntry::pixmap(const QSize &size, QIcon::Mode mode, Q
// for QIcon::pixmap() to handle states and modes,
// especially the disabled mode.
svgIcon = QIcon(pm);
#if (QT_VERSION >= QT_VERSION_CHECK(6,8,0))
pm = svgIcon.pixmap(size, scale, mode, state);
#else
if (QIconEngine *engine = svgIcon.data_ptr() ? svgIcon.data_ptr()->engine : nullptr)
pm = engine->pixmap(size, mode, state);
#endif
QPixmapCache::insert(key, pm);
}

Expand All @@ -937,13 +1016,7 @@ QPixmap ScalableFollowsColorEntry::pixmap(const QSize &size, QIcon::Mode mode, Q
QPixmap XdgIconLoaderEngine::pixmap(const QSize &size, QIcon::Mode mode,
QIcon::State state)
{
ensureLoaded();

QIconLoaderEngineEntry *entry = entryForSize(m_info, size);
if (entry)
return entry->pixmap(size, mode, state);

return QPixmap();
return scaledPixmap(size, mode, state, 1.0);
}

QString XdgIconLoaderEngine::key() const
Expand All @@ -967,8 +1040,13 @@ QPixmap XdgIconLoaderEngine::scaledPixmap(const QSize &size, QIcon::Mode mode, Q
{
ensureLoaded();
const int integerScale = qCeil(scale);
#if (QT_VERSION >= QT_VERSION_CHECK(6,8,0))
QIconLoaderEngineEntry *entry = entryForSize(m_info, size, integerScale);
return entry ? entry->pixmap(size, mode, state, scale) : QPixmap();
#else
QIconLoaderEngineEntry *entry = entryForSize(m_info, size / integerScale, integerScale);
return entry ? entry->pixmap(size, mode, state) : QPixmap();
#endif
}

QList<QSize> XdgIconLoaderEngine::availableSizes(QIcon::Mode mode, QIcon::State state)
Expand Down
4 changes: 4 additions & 0 deletions src/xdgiconloader/xdgiconloader_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ class XdgIconLoader;

struct ScalableFollowsColorEntry : public ScalableEntry
{
#if (QT_VERSION >= QT_VERSION_CHECK(6,8,0))
QPixmap pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state, qreal scale) override;
#else
QPixmap pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state) override;
#endif
};

//class QIconLoaderEngine : public QIconEngine
Expand Down

0 comments on commit 91ed8fd

Please sign in to comment.