-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1483 from syyyr/qt-widgets-example
Add a demo map viewer for QtWidgets
- Loading branch information
Showing
3 changed files
with
127 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
#include <QApplication> | ||
#include <QWidget> | ||
|
||
#include <osmscoutclientqt/OSMScoutQt.h> | ||
|
||
const auto MAX_ZOOM = 20; | ||
const auto MIN_ZOOM = 0; | ||
const auto MAP_DPI = 96; | ||
|
||
namespace { | ||
template <typename EventType> | ||
auto pos(EventType* event) | ||
{ | ||
#if QT_VERSION < 0x051400 | ||
return event->pos(); | ||
#else | ||
return event->position().toPoint(); | ||
#endif | ||
} | ||
} | ||
|
||
class MapFrame : public QWidget { | ||
public: | ||
explicit MapFrame(const QString& maps_dir, const QString& stylesheet_file) | ||
{ | ||
m_currentProjection.Set({0, 0}, 0.0, osmscout::Magnification{osmscout::Magnification::magWorld}, MAP_DPI, width(), height()); | ||
|
||
QFileInfo stylesheetFile(stylesheet_file); | ||
if (!osmscout::OSMScoutQt::NewInstance() | ||
.WithMapLookupDirectories({maps_dir}) | ||
.WithStyleSheetDirectory(stylesheetFile.dir().path()) | ||
.WithStyleSheetFile(stylesheetFile.fileName()) | ||
.Init()) { | ||
throw std::runtime_error{"failed to init OSMScoutQt"}; | ||
} | ||
|
||
m_renderer = osmscout::OSMScoutQt::GetInstance().MakeMapRenderer(osmscout::RenderingType::TiledRendering); | ||
connect(m_renderer, &osmscout::MapRenderer::Redraw, this, [this] {update();}); | ||
} | ||
|
||
protected: | ||
void paintEvent(QPaintEvent*) override | ||
{ | ||
auto painter = QPainter(this); | ||
painter.setRenderHint(QPainter::Antialiasing, true); | ||
painter.setRenderHint(QPainter::TextAntialiasing, true); | ||
painter.setRenderHint(QPainter::SmoothPixmapTransform, true); | ||
|
||
m_renderer->RenderMap(painter, osmscout::MapViewStruct{ | ||
m_currentProjection.GetCenter(), | ||
osmscout::Bearing{}, | ||
m_currentProjection.GetMagnification(), | ||
static_cast<size_t>(width()), | ||
static_cast<size_t>(height()), | ||
MAP_DPI, | ||
}); | ||
} | ||
|
||
void mousePressEvent(QMouseEvent* ev) override | ||
{ | ||
m_lastMousePos = ::pos(ev); | ||
} | ||
|
||
void mouseMoveEvent(QMouseEvent* ev) override | ||
{ | ||
auto x_delta = ::pos(ev).x() - m_lastMousePos.x(); | ||
auto y_delta = ::pos(ev).y() - m_lastMousePos.y(); | ||
m_currentProjection.Move(-x_delta, y_delta); | ||
m_lastMousePos = ::pos(ev); | ||
update(); | ||
} | ||
|
||
void wheelEvent(QWheelEvent* ev) override | ||
{ | ||
auto magnification = m_currentProjection.GetMagnification().GetLevel(); | ||
if (ev->angleDelta().y() > 0) { | ||
if (magnification >= MAX_ZOOM) { | ||
return; | ||
} | ||
magnification++; | ||
auto x_delta = (width() / 2. - ::pos(ev).x()) * 0.75; | ||
auto y_delta = (height() / 2. - ::pos(ev).y()) * 0.75; | ||
m_currentProjection.Move(-x_delta, y_delta); | ||
} else { | ||
if (magnification <= MIN_ZOOM) { | ||
return; | ||
} | ||
magnification--; | ||
auto x_delta = (width() / 2. - ::pos(ev).x()) * 0.75; | ||
auto y_delta = (height() / 2. - ::pos(ev).y()) * 0.75; | ||
m_currentProjection.Move(x_delta, -y_delta); | ||
} | ||
m_currentProjection.Set(m_currentProjection.GetCenter(), osmscout::Magnification{osmscout::MagnificationLevel{magnification}}, width(), height()); | ||
update(); | ||
} | ||
|
||
private: | ||
osmscout::MapRenderer* m_renderer; | ||
osmscout::MercatorProjection m_currentProjection; | ||
QPoint m_lastMousePos; | ||
}; | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
if (argc != 3) { | ||
std::cerr << "Usage: " << argv[0] << ": <map_database_directory> <stylesheet_file>\n"; | ||
return 1; | ||
} | ||
osmscout::OSMScoutQt::RegisterQmlTypes(); | ||
QApplication app(argc, argv); | ||
MapFrame map(argv[1], argv[2]); | ||
map.show(); | ||
return QApplication::exec(); | ||
} |