From a23daae698fdca06ee55ddf0e1aaaeb1b9921a08 Mon Sep 17 00:00:00 2001 From: Jean Porcherot Date: Thu, 3 Feb 2022 15:48:51 +0100 Subject: [PATCH 1/2] Fix compilation for Qt6 --- Libs/Core/ctkUtils.cpp | 29 ++++++++++++++++++++++++++++- Libs/Core/ctkUtils.h | 4 ++++ Libs/Widgets/ctkBasePopupWidget.cpp | 2 ++ Libs/Widgets/ctkDoubleSpinBox.cpp | 27 +++++++++++++++++++++------ Libs/Widgets/ctkPopupWidget.cpp | 6 ++++++ Libs/Widgets/ctkPopupWidget.h | 4 ++++ Libs/Widgets/ctkRangeSlider.cpp | 4 ++-- 7 files changed, 67 insertions(+), 9 deletions(-) diff --git a/Libs/Core/ctkUtils.cpp b/Libs/Core/ctkUtils.cpp index d22201f019..b59872c8c0 100644 --- a/Libs/Core/ctkUtils.cpp +++ b/Libs/Core/ctkUtils.cpp @@ -21,7 +21,11 @@ // Qt includes #include #include +#if QT_VERSION >= 0x060000 +#include +#else #include +#endif #include #include @@ -109,17 +113,30 @@ const char *ctkValidWildCard = QStringList ctk::nameFilterToExtensions(const QString& nameFilter) { QRegExp regexp(QString::fromLatin1(ctkNameFilterRegExp)); +#if QT_VERSION >= 0x060000 + int i = nameFilter.indexOf(regexp); +#else int i = regexp.indexIn(nameFilter); +#endif if (i < 0) { QRegExp isWildCard(QString::fromLatin1(ctkValidWildCard)); +#if QT_VERSION >= 0x060000 + if (nameFilter.indexOf(isWildCard) >= 0) +#else if (isWildCard.indexIn(nameFilter) >= 0) +#endif { return QStringList(nameFilter); } return QStringList(); } +#if QT_VERSION >= 0x060000 + QStringList captured = regexp.namedCaptureGroups(); + QString f = captured[2]; +#else QString f = regexp.cap(2); +#endif #if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) return f.split(QLatin1Char(' '), Qt::SkipEmptyParts); #else @@ -143,12 +160,22 @@ QString ctk::extensionToRegExp(const QString& extension) { // typically *.jpg QRegExp extensionExtractor("\\*\\.(\\w+)"); +#if QT_VERSION >= 0x060000 + int pos = extension.indexOf(extensionExtractor); +#else int pos = extensionExtractor.indexIn(extension); +#endif if (pos < 0) { return QString(); } - return ".*\\." + extensionExtractor.cap(1) + "?$"; +#if QT_VERSION >= 0x060000 + QStringList captured = extensionExtractor.namedCaptureGroups(); + QString cap = captured[1]; +#else + QString cap = extensionExtractor.cap(1); +#endif + return ".*\\." + cap + "?$"; } //----------------------------------------------------------------------------- diff --git a/Libs/Core/ctkUtils.h b/Libs/Core/ctkUtils.h index 5ddd7c774f..40e633d14a 100644 --- a/Libs/Core/ctkUtils.h +++ b/Libs/Core/ctkUtils.h @@ -28,6 +28,10 @@ #include #include +#if QT_VERSION >= 0x060000 +#define QRegExp QRegularExpression +#endif + // STD includes #include diff --git a/Libs/Widgets/ctkBasePopupWidget.cpp b/Libs/Widgets/ctkBasePopupWidget.cpp index 8348161c7e..5ec90d4f64 100644 --- a/Libs/Widgets/ctkBasePopupWidget.cpp +++ b/Libs/Widgets/ctkBasePopupWidget.cpp @@ -21,7 +21,9 @@ // Qt includes #include #include +#if QT_VERSION < 0x060000 #include +#endif #include #include #include diff --git a/Libs/Widgets/ctkDoubleSpinBox.cpp b/Libs/Widgets/ctkDoubleSpinBox.cpp index c263823199..1147d23946 100644 --- a/Libs/Widgets/ctkDoubleSpinBox.cpp +++ b/Libs/Widgets/ctkDoubleSpinBox.cpp @@ -398,13 +398,22 @@ ::validateAndInterpret(QString &input, int &pos, // could be because of group separators: if (!ok && state == QValidator::Acceptable) { - if (q->locale().groupSeparator().isPrint()) +#if QT_VERSION >= 0x060000 + QChar groupSeparator; + if ( q->locale().groupSeparator().size() == 1 ) + groupSeparator = q->locale().groupSeparator()[0]; + else + assert( false ); +#else + QChar groupSeparator = q->locale().groupSeparator(); +#endif + if (groupSeparator.isPrint()) { int start = (dec == -1 ? text.size() : dec)- 1; int lastGroupSeparator = start; for (int digit = start; digit >= 0; --digit) { - if (text.at(digit) == q->locale().groupSeparator()) + if (text.at(digit) == groupSeparator) { if (digit != lastGroupSeparator - 3) { @@ -1101,8 +1110,11 @@ QSize ctkDoubleSpinBox::sizeHint() const opt.rect = this->rect(); d->CachedSizeHint = this->style()->sizeFromContents( - QStyle::CT_SpinBox, &opt, newSizeHint, this) - .expandedTo(QApplication::globalStrut()); + QStyle::CT_SpinBox, &opt, newSizeHint, this +#if QT_VERSION < 0x060000 + ).expandedTo(QApplication::globalStrut() +#endif + ); return d->CachedSizeHint; } @@ -1157,8 +1169,11 @@ QSize ctkDoubleSpinBox::minimumSizeHint() const opt.rect = this->rect(); d->CachedMinimumSizeHint = this->style()->sizeFromContents( - QStyle::CT_SpinBox, &opt, newSizeHint, this) - .expandedTo(QApplication::globalStrut()); + QStyle::CT_SpinBox, &opt, newSizeHint, this +#if QT_VERSION < 0x060000 + ).expandedTo(QApplication::globalStrut() +#endif + ); return d->CachedMinimumSizeHint; } diff --git a/Libs/Widgets/ctkPopupWidget.cpp b/Libs/Widgets/ctkPopupWidget.cpp index 1015547f12..4710fe4656 100644 --- a/Libs/Widgets/ctkPopupWidget.cpp +++ b/Libs/Widgets/ctkPopupWidget.cpp @@ -20,7 +20,9 @@ // Qt includes #include +#if QT_VERSION < 0x060000 #include +#endif #include #include #include @@ -416,7 +418,11 @@ void ctkPopupWidget::leaveEvent(QEvent* event) } // -------------------------------------------------------------------------- +#if QT_VERSION >= 0x060000 +void ctkPopupWidget::enterEvent(QEnterEvent* event) +#else void ctkPopupWidget::enterEvent(QEvent* event) +#endif { Q_D(ctkPopupWidget); QTimer::singleShot(d->ShowDelay, this, SLOT(updatePopup())); diff --git a/Libs/Widgets/ctkPopupWidget.h b/Libs/Widgets/ctkPopupWidget.h index 99675c544e..85438de253 100644 --- a/Libs/Widgets/ctkPopupWidget.h +++ b/Libs/Widgets/ctkPopupWidget.h @@ -113,7 +113,11 @@ public Q_SLOTS: protected: virtual void leaveEvent(QEvent* event); +#if QT_VERSION >= 0x060000 + virtual void enterEvent(QEnterEvent* event); +#else virtual void enterEvent(QEvent* event); +#endif virtual bool eventFilter(QObject* obj, QEvent* event); /// Widget the popup is attached to. It opens right under \a baseWidget diff --git a/Libs/Widgets/ctkRangeSlider.cpp b/Libs/Widgets/ctkRangeSlider.cpp index a20a99b242..6b06fd1460 100644 --- a/Libs/Widgets/ctkRangeSlider.cpp +++ b/Libs/Widgets/ctkRangeSlider.cpp @@ -111,7 +111,7 @@ ctkRangeSliderPrivate::ctkRangeSliderPrivate(ctkRangeSlider& object) this->m_SubclassClickOffset = 0; this->m_SubclassPosition = 0; this->m_SubclassWidth = 0.0; - this->m_SelectedHandles = 0; + this->m_SelectedHandles = ctkRangeSliderPrivate::Handles(); // QFlags inits to 0 this->m_SymmetricMoves = false; } @@ -773,7 +773,7 @@ void ctkRangeSlider::mouseReleaseEvent(QMouseEvent* mouseEvent) this->QSlider::mouseReleaseEvent(mouseEvent); setSliderDown(false); - d->m_SelectedHandles = 0; + d->m_SelectedHandles = ctkRangeSliderPrivate::Handles(); // sets value to 0 this->update(); } From 2b366f04176593d02c72ea843e07d1caa0a66e75 Mon Sep 17 00:00:00 2001 From: jporcher Date: Thu, 5 May 2022 08:56:40 +0200 Subject: [PATCH 2/2] Fix according to lassoan comments --- Libs/Core/ctkUtils.cpp | 16 ++++++++-------- Libs/Core/ctkUtils.h | 2 +- Libs/Widgets/ctkBasePopupWidget.cpp | 2 +- Libs/Widgets/ctkDoubleSpinBox.cpp | 12 +++++++----- Libs/Widgets/ctkPopupWidget.cpp | 4 ++-- Libs/Widgets/ctkPopupWidget.h | 2 +- Libs/Widgets/ctkRangeSlider.cpp | 4 ++-- 7 files changed, 22 insertions(+), 20 deletions(-) diff --git a/Libs/Core/ctkUtils.cpp b/Libs/Core/ctkUtils.cpp index b59872c8c0..e055ff7ade 100644 --- a/Libs/Core/ctkUtils.cpp +++ b/Libs/Core/ctkUtils.cpp @@ -21,7 +21,7 @@ // Qt includes #include #include -#if QT_VERSION >= 0x060000 +#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) #include #else #include @@ -113,7 +113,7 @@ const char *ctkValidWildCard = QStringList ctk::nameFilterToExtensions(const QString& nameFilter) { QRegExp regexp(QString::fromLatin1(ctkNameFilterRegExp)); -#if QT_VERSION >= 0x060000 +#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) int i = nameFilter.indexOf(regexp); #else int i = regexp.indexIn(nameFilter); @@ -121,7 +121,7 @@ QStringList ctk::nameFilterToExtensions(const QString& nameFilter) if (i < 0) { QRegExp isWildCard(QString::fromLatin1(ctkValidWildCard)); -#if QT_VERSION >= 0x060000 +#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) if (nameFilter.indexOf(isWildCard) >= 0) #else if (isWildCard.indexIn(nameFilter) >= 0) @@ -131,9 +131,9 @@ QStringList ctk::nameFilterToExtensions(const QString& nameFilter) } return QStringList(); } -#if QT_VERSION >= 0x060000 +#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) QStringList captured = regexp.namedCaptureGroups(); - QString f = captured[2]; + QString f = captured.size() >= 3 ? captured[2] : ""; #else QString f = regexp.cap(2); #endif @@ -160,7 +160,7 @@ QString ctk::extensionToRegExp(const QString& extension) { // typically *.jpg QRegExp extensionExtractor("\\*\\.(\\w+)"); -#if QT_VERSION >= 0x060000 +#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) int pos = extension.indexOf(extensionExtractor); #else int pos = extensionExtractor.indexIn(extension); @@ -169,9 +169,9 @@ QString ctk::extensionToRegExp(const QString& extension) { return QString(); } -#if QT_VERSION >= 0x060000 +#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) QStringList captured = extensionExtractor.namedCaptureGroups(); - QString cap = captured[1]; + QString cap = captured.size() >= 2 ? captured[1] : ""; #else QString cap = extensionExtractor.cap(1); #endif diff --git a/Libs/Core/ctkUtils.h b/Libs/Core/ctkUtils.h index 40e633d14a..605f9e3e1d 100644 --- a/Libs/Core/ctkUtils.h +++ b/Libs/Core/ctkUtils.h @@ -28,7 +28,7 @@ #include #include -#if QT_VERSION >= 0x060000 +#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) #define QRegExp QRegularExpression #endif diff --git a/Libs/Widgets/ctkBasePopupWidget.cpp b/Libs/Widgets/ctkBasePopupWidget.cpp index 5ec90d4f64..0962eea117 100644 --- a/Libs/Widgets/ctkBasePopupWidget.cpp +++ b/Libs/Widgets/ctkBasePopupWidget.cpp @@ -21,7 +21,7 @@ // Qt includes #include #include -#if QT_VERSION < 0x060000 +#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) #include #endif #include diff --git a/Libs/Widgets/ctkDoubleSpinBox.cpp b/Libs/Widgets/ctkDoubleSpinBox.cpp index 1147d23946..96fdb72518 100644 --- a/Libs/Widgets/ctkDoubleSpinBox.cpp +++ b/Libs/Widgets/ctkDoubleSpinBox.cpp @@ -398,12 +398,14 @@ ::validateAndInterpret(QString &input, int &pos, // could be because of group separators: if (!ok && state == QValidator::Acceptable) { -#if QT_VERSION >= 0x060000 +#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) QChar groupSeparator; if ( q->locale().groupSeparator().size() == 1 ) + { groupSeparator = q->locale().groupSeparator()[0]; - else - assert( false ); + } + // else: group separator does not necessarily fit into a QChar (https://bugreports.qt.io/browse/QTBUG-69324) + // but CTK only support group separators if they fit into a QChar #else QChar groupSeparator = q->locale().groupSeparator(); #endif @@ -1111,7 +1113,7 @@ QSize ctkDoubleSpinBox::sizeHint() const opt.rect = this->rect(); d->CachedSizeHint = this->style()->sizeFromContents( QStyle::CT_SpinBox, &opt, newSizeHint, this -#if QT_VERSION < 0x060000 +#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) ).expandedTo(QApplication::globalStrut() #endif ); @@ -1170,7 +1172,7 @@ QSize ctkDoubleSpinBox::minimumSizeHint() const opt.rect = this->rect(); d->CachedMinimumSizeHint = this->style()->sizeFromContents( QStyle::CT_SpinBox, &opt, newSizeHint, this -#if QT_VERSION < 0x060000 +#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) ).expandedTo(QApplication::globalStrut() #endif ); diff --git a/Libs/Widgets/ctkPopupWidget.cpp b/Libs/Widgets/ctkPopupWidget.cpp index 4710fe4656..b01662549f 100644 --- a/Libs/Widgets/ctkPopupWidget.cpp +++ b/Libs/Widgets/ctkPopupWidget.cpp @@ -20,7 +20,7 @@ // Qt includes #include -#if QT_VERSION < 0x060000 +#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) #include #endif #include @@ -418,7 +418,7 @@ void ctkPopupWidget::leaveEvent(QEvent* event) } // -------------------------------------------------------------------------- -#if QT_VERSION >= 0x060000 +#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) void ctkPopupWidget::enterEvent(QEnterEvent* event) #else void ctkPopupWidget::enterEvent(QEvent* event) diff --git a/Libs/Widgets/ctkPopupWidget.h b/Libs/Widgets/ctkPopupWidget.h index 85438de253..7ab8f5b7d9 100644 --- a/Libs/Widgets/ctkPopupWidget.h +++ b/Libs/Widgets/ctkPopupWidget.h @@ -113,7 +113,7 @@ public Q_SLOTS: protected: virtual void leaveEvent(QEvent* event); -#if QT_VERSION >= 0x060000 +#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) virtual void enterEvent(QEnterEvent* event); #else virtual void enterEvent(QEvent* event); diff --git a/Libs/Widgets/ctkRangeSlider.cpp b/Libs/Widgets/ctkRangeSlider.cpp index 6b06fd1460..48fd0b9038 100644 --- a/Libs/Widgets/ctkRangeSlider.cpp +++ b/Libs/Widgets/ctkRangeSlider.cpp @@ -111,7 +111,7 @@ ctkRangeSliderPrivate::ctkRangeSliderPrivate(ctkRangeSlider& object) this->m_SubclassClickOffset = 0; this->m_SubclassPosition = 0; this->m_SubclassWidth = 0.0; - this->m_SelectedHandles = ctkRangeSliderPrivate::Handles(); // QFlags inits to 0 + this->m_SelectedHandles = ctkRangeSliderPrivate::Handles(); // initialize QFlags with no flags this->m_SymmetricMoves = false; } @@ -773,7 +773,7 @@ void ctkRangeSlider::mouseReleaseEvent(QMouseEvent* mouseEvent) this->QSlider::mouseReleaseEvent(mouseEvent); setSliderDown(false); - d->m_SelectedHandles = ctkRangeSliderPrivate::Handles(); // sets value to 0 + d->m_SelectedHandles = ctkRangeSliderPrivate::Handles(); // initialize QFlags with no flags set this->update(); }