Skip to content

Commit

Permalink
Merge pull request #1496 from Karry/qt-version-check
Browse files Browse the repository at this point in the history
use proper check for Qt version
  • Loading branch information
Framstag committed Sep 6, 2023
2 parents f9cdeb7 + 5d1726d commit f75f49a
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 22 deletions.
22 changes: 17 additions & 5 deletions Demos/src/QtWidgetsDemoApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,22 @@ const auto MIN_ZOOM = 0;
const auto MAP_DPI = 96;

namespace {
// used with QWheelEvent
template <typename EventType>
auto pos(EventType* event)
{
#if QT_VERSION < 0x051400
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
return event->pos();
#else
return event->position().toPoint();
#endif
}

// used with QMouseEvent
template <typename EventType>
auto pos2(EventType* event)
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
return event->pos();
#else
return event->position().toPoint();
Expand Down Expand Up @@ -58,15 +70,15 @@ class MapFrame : public QWidget {

void mousePressEvent(QMouseEvent* ev) override
{
m_lastMousePos = ::pos(ev);
m_lastMousePos = ::pos2(ev);
}

void mouseMoveEvent(QMouseEvent* ev) override
{
auto x_delta = ::pos(ev).x() - m_lastMousePos.x();
auto y_delta = ::pos(ev).y() - m_lastMousePos.y();
auto x_delta = ::pos2(ev).x() - m_lastMousePos.x();
auto y_delta = ::pos2(ev).y() - m_lastMousePos.y();
m_currentProjection.Move(-x_delta, y_delta);
m_lastMousePos = ::pos(ev);
m_lastMousePos = ::pos2(ev);
update();
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/src/DrawTextQt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void DrawWindow::drawText1(QPainter *painter, QString string, const osmscout::La

painter->drawText(point.GetX(), point.GetY(), QString(string[i]));

#if QT_VERSION < 0x051100
#if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
offset+=metrics.width(string[i]);
#else
offset+=metrics.horizontalAdvance(string[i]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include <QAbstractListModel>
#include <QList>
#include <QMediaPlayer>
#if QT_VERSION < 0x060000
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include <QMediaPlaylist>
#endif

Expand Down Expand Up @@ -89,7 +89,7 @@ public slots:
SettingsRef settings;

// we setup QObject parents, objects are cleaned after Module destruction
#if QT_VERSION < 0x060000
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QMediaPlaylist *currentPlaylist{nullptr};
#endif
QMediaPlayer *mediaPlayer{nullptr};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@
#include <QTimer>
#include <QMediaPlayer>

#if QT_VERSION < 0x060000
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include <QMediaPlaylist>
#endif

#include <optional>

namespace osmscout {

#if QT_VERSION < 0x060000
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
using QtMediaPlayerState = QMediaPlayer::State;
#else
using QtMediaPlayerState = QMediaPlayer::PlaybackState;
Expand Down Expand Up @@ -142,7 +142,7 @@ public slots:
QString voiceDir;
// player and playlist should be created in module thread, not in UI thread (constructor)
// we setup QObject parents, objects are cleaned after Module destruction
#if QT_VERSION < 0x060000
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QMediaPlaylist *currentPlaylist{nullptr};
#endif
QMediaPlayer *mediaPlayer{nullptr};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ void InstalledVoicesModel::playSample(const QModelIndex &index, const QStringLis
return;
}

#if QT_VERSION < 0x060000
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
if (mediaPlayer==nullptr){
assert(currentPlaylist==nullptr);
mediaPlayer = new QMediaPlayer(this);
Expand Down
20 changes: 10 additions & 10 deletions libosmscout-client-qt/src/osmscoutclientqt/NavigationModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,19 @@ void NavigationModule::InitPlayer()
}

if (mediaPlayer==nullptr){
#if QT_VERSION < 0x060000
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
assert(currentPlaylist==nullptr);
#endif
mediaPlayer = new QMediaPlayer(this);
#if QT_VERSION < 0x060000
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
currentPlaylist = new QMediaPlaylist(mediaPlayer);
#endif
#if QT_VERSION < 0x060000
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
connect(mediaPlayer, &QMediaPlayer::stateChanged, this, &NavigationModule::playerStateChanged);
#else
connect(mediaPlayer, &QMediaPlayer::playbackStateChanged, this, &NavigationModule::playerStateChanged);
#endif
#if QT_VERSION < 0x060000
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
mediaPlayer->setPlaylist(currentPlaylist);
#endif
}
Expand Down Expand Up @@ -138,7 +138,7 @@ void NavigationModule::ProcessMessages(const std::list<osmscout::NavigationMessa
nextMessage = voiceInstructionMessage->message;
InitPlayer();
assert(mediaPlayer);
#if QT_VERSION < 0x060000
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
playerStateChanged(mediaPlayer->state());
#else
playerStateChanged(mediaPlayer->playbackState());
Expand Down Expand Up @@ -329,7 +329,7 @@ QString NavigationModule::sampleFile(osmscout::VoiceInstructionMessage::VoiceSam
}
}

#if QT_VERSION < 0x060000
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
void NavigationModule::playerStateChanged(QMediaPlayer::State state)
#else
void NavigationModule::playerStateChanged(QMediaPlayer::PlaybackState state)
Expand All @@ -339,26 +339,26 @@ QString NavigationModule::sampleFile(osmscout::VoiceInstructionMessage::VoiceSam
qWarning() << "Player state changed from incorrect thread;" << thread << "!=" << QThread::currentThread();
}

#if QT_VERSION < 0x060000
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
qDebug() << "Voice player state:" << mediaPlayer->state() << "(" << currentPlaylist->currentIndex() << "/" << currentPlaylist->mediaCount() << ")";
#endif
if (!voiceDir.isEmpty() &&
!nextMessage.empty() &&
state == QMediaPlayer::StoppedState) {

#if QT_VERSION < 0x060000
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
currentPlaylist->clear();
#endif

for (const auto& sample : nextMessage){
auto sampleUrl = QUrl::fromLocalFile(voiceDir + QDir::separator() + sampleFile(sample));
#if QT_VERSION < 0x060000
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
qDebug() << "Adding to playlist:" << sampleUrl;
currentPlaylist->addMedia(sampleUrl);
#endif
}
nextMessage.clear();
#if QT_VERSION < 0x060000
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
currentPlaylist->setCurrentIndex(0);
#endif
mediaPlayer->play();
Expand Down

0 comments on commit f75f49a

Please sign in to comment.