Last active
September 26, 2017 18:01
-
-
Save eyllanesc/3f60a08691b4e8f57e3ef8badf567c6c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
from random import random, randint | |
from PyQt5.QtCore import QAbstractListModel, Qt, QModelIndex, QVariant, QCoreApplication, QPointF, QUrl, QByteArray, \ | |
QTimer | |
from PyQt5.QtGui import QColor, QGuiApplication | |
from PyQt5.QtQml import QQmlApplicationEngine | |
class MarkerItem(object): | |
def __init__(self, position, color=QColor("red")): | |
self._position = position | |
self._color = color | |
def position(self): | |
return self._position | |
def setPosition(self, value): | |
self._position = value | |
def color(self): | |
return self._color | |
def setColor(self, value): | |
self._color = value | |
class MarkerModel(QAbstractListModel): | |
PositionRole = Qt.UserRole + 1 | |
ColorRole = Qt.UserRole + 2 | |
_roles = {PositionRole: QByteArray(b"markerPosition"), ColorRole: QByteArray(b"markerColor")} | |
def __init__(self, parent=None): | |
QAbstractListModel.__init__(self, parent) | |
self._markers = [] | |
def rowCount(self, index=QModelIndex()): | |
return len(self._markers) | |
def roleNames(self): | |
return self._roles | |
def data(self, index, role=Qt.DisplayRole): | |
if index.row() >= self.rowCount(): | |
return QVariant() | |
marker = self._markers[index.row()] | |
if role == MarkerModel.PositionRole: | |
return marker.position() | |
elif role == MarkerModel.ColorRole: | |
return marker.color() | |
return QVariant() | |
def setData(self, index, value, role=Qt.EditRole): | |
if index.isValid(): | |
marker = self._markers[index.row()] | |
if role == MarkerModel.PositionRole: | |
marker.setPosition(value) | |
if role == MarkerModel.ColorRole: | |
marker.setColor(value) | |
self.dataChanged.emit(index, index) | |
return True | |
return QAbstractListModel.setData(self, index, value, role) | |
def addMarker(self, marker): | |
self.beginInsertRows(QModelIndex(), self.rowCount(), self.rowCount()) | |
self._markers.append(marker) | |
self.endInsertRows() | |
def flags(self, index): | |
if not index.isValid(): | |
return Qt.ItemIsEnabled | |
return QAbstractListModel.flags(index)|Qt.ItemIsEditable | |
def moveRandom(self, ix): | |
ind = self.index(ix, 0) | |
current_pos = self.data(ind, MarkerModel.PositionRole) | |
next_pos = current_pos + 0.002*QPointF(random() - 0.5, random() - 0.5) | |
self.setData(ind, next_pos, MarkerModel.PositionRole) | |
self.setData(ind, QColor(randint(0, 255), randint(0, 255), randint(0, 255)), MarkerModel.ColorRole) | |
if __name__ == "__main__": | |
import sys | |
QCoreApplication.setAttribute(Qt.AA_EnableHighDpiScaling) | |
app = QGuiApplication(sys.argv) | |
engine = QQmlApplicationEngine() | |
model = MarkerModel() | |
model.addMarker(MarkerItem(QPointF(-12.0464, -77.0428), QColor("black"))) | |
model.addMarker(MarkerItem(QPointF(-12.0340, -77.0428), QColor("red"))) | |
model.addMarker(MarkerItem(QPointF(-12.0440, -77.0280), QColor("#9f5522"))) | |
context = engine.rootContext() | |
context.setContextProperty('markerModel', model) | |
engine.load(QUrl.fromLocalFile("main.qml")) | |
if len(engine.rootObjects()) == 0: | |
sys.exit(-1) | |
engine.quit.connect(app.quit) | |
timer = QTimer(engine) | |
timer.timeout.connect(lambda: model.moveRandom(0)) | |
timer.start(100) | |
sys.exit(app.exec_()) |
This file contains hidden or 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
import QtQuick 2.0 | |
import QtQuick.Window 2.0 | |
import QtLocation 5.5 | |
import QtPositioning 5.5 | |
Window { | |
visible: true | |
title: "Python OSM" | |
width: 640 | |
height: 480 | |
property int marker_size: 16 | |
Map { | |
id: map | |
anchors.fill: parent | |
plugin: Plugin { | |
name: "osm" | |
} | |
center: QtPositioning.coordinate(-12.0464, -77.0428) | |
zoomLevel: 14 | |
MapItemView { | |
model: markerModel | |
delegate: MapQuickItem{ | |
anchorPoint: Qt.point(2.5, 2.5) | |
coordinate: QtPositioning.coordinate(markerPosition.x, markerPosition.y) | |
zoomLevel: 0 | |
sourceItem: Rectangle{ | |
width: marker_size | |
height: marker_size | |
radius: marker_size/2 | |
border.color: "white" | |
color: markerColor | |
border.width: 1 | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment