Skip to content

Instantly share code, notes, and snippets.

@neel
Created September 9, 2017 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neel/55b3acebbbff6d883e5fa55f581c5d47 to your computer and use it in GitHub Desktop.
Save neel/55b3acebbbff6d883e5fa55f581c5d47 to your computer and use it in GitHub Desktop.
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
import QtQuick 2.0
import QtQuick.Window 2.0
import QtLocation 5.5
import QtPositioning 5.5
Rectangle {
width: 2048
height: 2048
visible: true
Item {
id: settings
property int marker_size: 16
property var marker_colors: {"observation": "green", "important": "red", "redundant": "blue", "deleted": "white"}
}
Plugin {
id: mapPlugin
name: "osm"
}
Map {
id: map
anchors.fill: parent
plugin: mapPlugin
center: QtPositioning.coordinate(22.5726, 88.3639)
zoomLevel: 14
MapItemView {
model: markerModel
delegate: markerDelegate
}
Component {
id: markerDelegate
MapQuickItem{
anchorPoint: Qt.point(2.5, 2.5)
coordinate: QtPositioning.coordinate(position.x, position.y)
zoomLevel: 0
sourceItem: Rectangle{
width: settings.marker_size;
height: settings.marker_size;
radius: settings.marker_size/2;
color: settings.marker_colors[status]
border.color: "white"
border.width: 1
}
}
}
Component{
id: polyline
MapPolyline {
line.color: black
line.width: 2
path: []
}
}
}
// function add_point(lat, lng){
// var item = Qt.createmlObject('import QtQuick 2.0; import QtLocation 5.3; MapQuickItem{}', map, "dynamic");
// item.anchorPoint.x = Qt.point(2.5,2.5)
// item.coordinate = QtPositioning.coordinate(lat, lng);
// item.zoomLevel = 0
// var circle = Qt.createQmlObject('import QtQuick 2.0; Rectangle{ width: 16; height: 16; radius: 8}', map);
// item.sourceItem = circle
// map.addMapItem(item);
// data.points.push(item);
// return true;
// }
}
#include <QQuickView>
#include <QFileDialog>
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow), _map(new MapView){
ui->setupUi(this);
ui->mapLayout->addWidget(_map);
// _map->draw_point(QPointF(22.5868, 88.4149));
connect(ui->actionAdd, SIGNAL(triggered(bool)), _map, SLOT(addObservation_slot()));
}
MainWindow::~MainWindow(){
delete ui;
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "mapview.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
MapView* _map;
};
#endif // MAINWINDOW_H
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="tab">
<attribute name="title">
<string>Map</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<layout class="QVBoxLayout" name="mapLayout">
<property name="spacing">
<number>0</number>
</property>
</layout>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_2">
<attribute name="title">
<string>Data</string>
</attribute>
</widget>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>29</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
<addaction name="actionAdd"/>
</widget>
<widget class="QStatusBar" name="statusBar"/>
<action name="actionAdd">
<property name="text">
<string>Add Observation</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
#include "mapview.h"
#include <QVBoxLayout>
#include <QQmlEngine>
#include <QQmlContext>
#include <QFileDialog>
#include <QStringList>
MapView::MapView(QWidget *parent) : QWidget(parent), _view(new QQuickView()), _model(new MarkerModel){
_model->addMarker(new MarkerItem(QPointF(22.5868f, 88.4149f), MarkerItem::marker_observation, QDateTime::currentDateTime(), "1"));
qRegisterMetaType<MarkerModel*>("MarkerModel");
QWidget* container = QWidget::createWindowContainer(_view, this);
container->setFocusPolicy(Qt::TabFocus);
_view->engine()->rootContext()->setContextProperty("markerModel", _model);
_view->setSource(QUrl("qrc:///main.qml"));
QVBoxLayout* layout = new QVBoxLayout;
setLayout(layout);
layout->addWidget(container);
_root = _view->rootObject();
_model->addMarker(new MarkerItem(QPointF(22.5391f, 88.3958f), MarkerItem::marker_observation, QDateTime::currentDateTime(), "2"));
}
void MapView::draw_point(const QPointF& position){
QVariant latitude(position.x());
QVariant longitude(position.y());
QVariant ret;
// QMetaObject::invokeMethod((QObject*)_root, "add_point", Q_RETURN_ARG(QVariant, ret), Q_ARG(QVariant, latitude), Q_ARG(QVariant, longitude));
}
void MapView::addObservation_slot(){
QString file_name = QFileDialog::getOpenFileName(this, tr("Open File"), ".", tr("Spreadsheets (*.csv)"));
QFile f(file_name);
f.open(QFile::ReadOnly | QFile::Text);
QTextStream in(&f);
QString text = in.readAll();
QStringList lines = text.split("\n", QString::SkipEmptyParts);
unsigned int counter = 0;
foreach(QString line, lines){
QStringList values_str = line.split(",", QString::SkipEmptyParts);
qWarning() << values_str;
QString label = QString::number(counter);
QPointF position = QPointF(values_str[0].toDouble(), values_str[1].toDouble());
QDateTime when = QDateTime::fromString(values_str[2].split("+")[0],"yyyy/MM/dd HH:mm:ss");
MarkerItem* marker = new MarkerItem(position, MarkerItem::marker_observation, when, label);
_model->addMarker(marker);
qWarning() << marker;
counter++;
}
}
#ifndef MAPVIEW_H
#define MAPVIEW_H
#include <QWidget>
#include <QQuickView>
#include <QQuickItem>
#include "markermodel.h"
class MapView : public QWidget{
Q_OBJECT
QQuickView* _view;
QQuickItem* _root;
MarkerModel* _model;
public:
explicit MapView(QWidget *parent = 0);
public:
void draw_point(const QPointF& position);
public slots:
void addObservation_slot();
};
#endif // MAPVIEW_H
#include <QtQml>
#include "markermodel.h"
#include <QMetaType>
MarkerItem::MarkerItem(const QPointF &pos, MarkerItem::marker_state state, const QDateTime &when, const QString &label): _position(pos), _state(state), _label(label), _when(when){
// qmlRegisterType<QGeoRoute*>();
}
const QPointF &MarkerItem::position() const{
return _position;
}
QGeoCoordinate MarkerItem::coordinate() const{
return QGeoCoordinate(_position.x(), _position.y());
}
const QString &MarkerItem::label() const{
return _label;
}
MarkerItem::marker_state MarkerItem::state() const{
return _state;
}
void MarkerItem::change_state(MarkerItem::marker_state state){
_state = state;
}
MarkerModel::MarkerModel(QObject *parent): QAbstractListModel(parent){
}
int MarkerModel::rowCount(const QModelIndex &parent) const{
if (parent.isValid())
return 0;
return _markers.count();
}
QVariant MarkerModel::data(const QModelIndex &index, int role) const{
if (!index.isValid())
return QVariant();
if(role == int(PositionRole)){
return _markers.at(index.row())->position();
}else if(role == int(StateRole)){
switch(_markers.at(index.row())->state()){
case MarkerItem::marker_important:
return "important";
case MarkerItem::marker_observation:
return "observation";
case MarkerItem::marker_redundant:
return "redundant";
case MarkerItem::marker_deleted:
return "deleted";
}
}else if(role == int(LabelRole)){
return _markers.at(index.row())->label();
}
return QVariant();
}
QHash<int, QByteArray> MarkerModel::roleNames() const{
QHash<int, QByteArray> roles;
roles[PositionRole] = "position";
roles[StateRole] = "status";
roles[LabelRole] = "label";
return roles;
}
void MarkerModel::addMarker(MarkerItem *marker){
beginInsertRows(QModelIndex(), rowCount(), rowCount());
_markers.push_back(marker);
qWarning() << rowCount();
endInsertRows();
}
QGeoRoute *MarkerModel::route() const{
QGeoRoute* geo_route = new QGeoRoute;
QList<QGeoCoordinate> coordinates;
foreach(MarkerItem* marker, _markers){
coordinates.push_back(marker->coordinate());
}
geo_route->setPath(coordinates);
return geo_route;
}
#ifndef MARKERMODEL_H
#define MARKERMODEL_H
#include <QList>
#include <QPointF>
#include <QMetaType>
#include <QDateTime>
#include <QGeoRoute>
#include <QGeoCoordinate>
#include <QAbstractListModel>
struct MarkerItem{
enum marker_state{
marker_observation = 0,
marker_important,
marker_redundant,
marker_deleted
};
MarkerItem(const QPointF& pos, marker_state state, const QDateTime& when, const QString& label);
const QPointF& position() const;
QGeoCoordinate coordinate() const;
const QString& label() const;
marker_state state() const;
void change_state(marker_state state);
private:
QPointF _position;
marker_state _state;
QString _label;
QDateTime _when;
};
class MarkerModel : public QAbstractListModel{
Q_OBJECT
Q_PROPERTY(QGeoRoute* route READ route NOTIFY routeChanged)
public:
enum MarkerRoles {
PositionRole = Qt::UserRole + 1,
StateRole,
LabelRole
};
explicit MarkerModel(QObject *parent = 0);
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
public:
QHash<int, QByteArray> roleNames() const;
private:
QList<MarkerItem*> _markers;
public:
void addMarker(MarkerItem* marker);
public:
QGeoRoute* route() const;
signals:
void routeChanged();
};
#endif // MARKERMODEL_H
#-------------------------------------------------
#
# Project created by QtCreator 2017-08-25T22:40:16
#
#-------------------------------------------------
CONFIG += c++11
QT += core gui quick qml location
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = PrediGeo
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
mapview.cpp \
markermodel.cpp
HEADERS += mainwindow.h \
mapview.h \
markermodel.h
FORMS += mainwindow.ui
RESOURCES += \
resources.qrc
DISTFILES += \
main.qml
<RCC>
<qresource prefix="/">
<file>main.qml</file>
</qresource>
</RCC>
@neel
Copy link
Author

neel commented Sep 9, 2017

Example CSV File contents to test

88.43666488,22.64998183,2017/06/18 15:26:06+00,0
88.43770597,22.65195643,2017/06/18 15:26:26+00,0
88.43726054,22.65322171,2017/06/18 15:26:46+00,0
88.43482114,22.65365592,2017/06/18 15:27:06+00,0
88.43277268,22.65325555,2017/06/18 15:27:26+00,0
88.43032002,22.65264038,2017/06/18 15:27:46+00,0
88.42716476,22.65248626,2017/06/18 15:28:06+00,0
88.42449427,22.65353276,2017/06/18 15:28:26+00,0
88.4220495,22.6548282,2017/06/18 15:28:46+00,0
88.41894316,22.65611474,2017/06/18 15:29:06+00,0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment