Added friend image support to UI
authorJanne Kiiski <janne.kiiski@ixonos.com>
Wed, 17 Nov 2010 10:14:54 +0000 (12:14 +0200)
committerJanne Kiiski <janne.kiiski@ixonos.com>
Wed, 17 Nov 2010 10:16:19 +0000 (12:16 +0200)
- FriendModel: resetModel and filter support added
- GeoMap: Friend geo pixmap handling added
- SituareEngine: FriendModel/GeoMap connections
- Map.qml: redirect click to map

src/engine/engine.cpp
src/qmlui/Map.qml
src/qmlui/geomap.cpp
src/qmlui/geomap.h
src/user/friendmodel.cpp
src/user/friendmodel.h

index 3b4b275..85866df 100644 (file)
@@ -195,13 +195,15 @@ SituareEngine::SituareEngine()
     d->ui->rootContext()->setContextProperty("friendModel", &d->friendModel);
     d->ui->rootContext()->setContextProperty("engine", this);
 
-    d->ui->rootContext()->setContextProperty("routingModel", &d->routeModel);
-    d->ui->rootContext()->setContextProperty("engine", this);
-
     d->ui->setSource(QUrl("qrc:/Main.qml"));
     d->ui->setResizeMode(QDeclarativeView::SizeRootObjectToView);
     d->ui->show();
 
+    GeoMap* geoMap = qobject_cast<GeoMap*>(d->ui->rootObject()->findChild<QObject*>("geoMap"));
+    Q_ASSERT(geoMap);
+    geoMap->setFriendModel(&d->friendModel);
+
+
     connect(d->ui->engine()->networkAccessManager(),
             SIGNAL(sslErrors(QNetworkReply*, QList<QSslError>)),
             m_facebookAuthenticator, SLOT(sslErrors(QNetworkReply*, QList<QSslError>)));
@@ -385,11 +387,13 @@ void SituareEngine::error(const int context, const int error)
 void SituareEngine::imageReady(User *user)
 {
     qDebug() << __PRETTY_FUNCTION__;
-
+    Q_D(SituareEngine);
     if(user->type())
         emit userLocationReady(user);
     else
         emit friendImageReady(user);
+
+    d->friendModel.resetFriends();
 }
 
 void SituareEngine::initializeGpsAndAutocentering()
index 85db118..e8510a1 100644 (file)
@@ -61,6 +61,9 @@ Item {
                     prevMouseY = mouse.y
                 }
             }
+            onClicked: {
+                map.onClicked(mouseX, mouseY)
+            }
         }
 
         Image {
index 2d1c2fe..27b8737 100644 (file)
@@ -1,11 +1,30 @@
 #include "geomap.h"
 #include <QDebug>
 #include <QGeoMapPixmapObject>
+#include "../user/friendmodel.h"
+#include "../coordinates/geocoordinate.h"
+#include <QGraphicsSceneMouseEvent>
 
 using namespace QtMobility;
 
+class GeoMapPrivate
+{
+public:
+    GeoMapPrivate()
+        : friendModel(0),
+          gpsLocationMapObject(0)
+    {}
+
+    FriendModel* friendModel;
+
+    QGeoMapPixmapObject* gpsLocationMapObject;
+
+    QList<QGeoMapPixmapObject*> friendMapObjects;
+};
+
 GeoMap::GeoMap(QGraphicsItem* parent)
     : QGraphicsGeoMap(parent),
+      d_ptr(new GeoMapPrivate),
       source(0),
       prevGpsLocation(0,0),
       initialized(false)
@@ -16,6 +35,8 @@ GeoMap::GeoMap(QGraphicsItem* parent)
     setCenter(prevGpsLocation);
 
     goToGpsLocation();
+
+    setObjectName("geoMap");
 }
 
 GeoMap::~GeoMap()
@@ -72,11 +93,51 @@ double GeoMap::gpsLocationLongitude() const
     return prevGpsLocation.longitude();
 }
 
+void GeoMap::setFriendModel(FriendModel* friendModel)
+{
+    Q_D(GeoMap);
+    if (friendModel) {
+        d->friendModel = friendModel;
+        connect(d->friendModel, SIGNAL(modelReset()), this, SLOT(onFriendModelReset()));
+    }
+}
+
 void GeoMap::panned()
 {
     emit positionChanged();
 }
 
+void GeoMap::onFriendModelReset()
+{
+    Q_D(GeoMap);
+    Q_ASSERT(d->friendModel);
+
+    foreach(QGeoMapPixmapObject* mapObject, d->friendMapObjects)
+        removeMapObject(mapObject);
+
+    for (int i=0; i < d->friendModel->rowCount(); ++i) {
+        const QModelIndex index = d->friendModel->index(i);
+        QPixmap pixmap = d->friendModel->data(index, FriendModel::ImageRole).value<QPixmap>();
+        GeoCoordinate coordinate = d->friendModel->data(index, FriendModel::CoordinateRole).value<GeoCoordinate>();
+
+        if (pixmap.isNull())
+            pixmap = QPixmap(":/res/images/user_info.png");
+
+        Q_ASSERT(!pixmap.isNull());
+
+        if (!pixmap.isNull() && coordinate.isValid()) {
+            QPoint offset(-pixmap.width()/2, -pixmap.height()/2);
+            QGeoMapPixmapObject* object = new QGeoMapPixmapObject(QGeoCoordinate(coordinate.latitude(), coordinate.longitude()),
+                                                                  offset,
+                                                                  pixmap,
+                                                                  0);
+            object->setProperty("user", d->friendModel->data(index, FriendModel::UserRole));
+            addMapObject(object);
+            d->friendMapObjects << object;
+        }
+    }
+}
+
 void GeoMap::goToGpsLocation()
 {
     qDebug() << "QmlMap::goToGpsLocation";
@@ -91,23 +152,37 @@ void GeoMap::goToGpsLocation()
     }
 }
 
+void GeoMap::onClicked(qreal mouseX, qreal mouseY)
+{
+    Q_D(GeoMap);
+    QList<QGeoMapObject*> mapObjects = mapObjectsAtScreenPosition(QPointF(mouseX, mouseY));
+    QList<User*> filter;
+    foreach(QGeoMapObject* mapObject, mapObjects) {
+        User* user = static_cast<User*>(mapObject->property("user").value<void*>());
+        Q_ASSERT(user);
+        filter << user;
+    }
+    // TODO: reset filter only if it contains at least one entry
+    //if (!filter.isEmpty())
+        d->friendModel->filter(filter);
+}
+
 void GeoMap::positionUpdated(const QGeoPositionInfo& positionInfo)
 {
     qDebug() << "QmlMap::positionUpdated: " << positionInfo.coordinate();
     prevGpsLocation = positionInfo.coordinate();
+    Q_D(GeoMap);
 
-    if (!initialized) {
-        initialized = true;
+    if (!d->gpsLocationMapObject) {
         QPixmap image(":/res/gps_position.png");
         Q_ASSERT(!image.isNull());
-        QGeoMapPixmapObject* pixmap = new QGeoMapPixmapObject(prevGpsLocation, QPoint(-image.width()/2, -image.height()/2), image);
-        addMapObject(pixmap);
+        d->gpsLocationMapObject = new QGeoMapPixmapObject(prevGpsLocation, QPoint(-image.width()/2, -image.height()/2), image);
+        addMapObject(d->gpsLocationMapObject);
         setCenter(prevGpsLocation);
         emit positionChanged();
     }
     else {
-        qobject_cast<QGeoMapPixmapObject*>(mapObjects().at(0))->setCoordinate(prevGpsLocation);
-
+        d->gpsLocationMapObject->setCoordinate(prevGpsLocation);
     }
     emit gpsLocationChanged();
 }
index 37a1eb3..19dc5d2 100644 (file)
@@ -5,9 +5,11 @@
 #include <QGeoPositionInfo>
 #include <QGeoPositionInfoSource>
 #include <QGeoCoordinate>
-#include <QVector3D>
 
 using namespace QtMobility;
+class FriendModel;
+
+class GeoMapPrivate;
 
 class GeoMap : public QGraphicsGeoMap
 {
@@ -34,6 +36,8 @@ public:
     double gpsLocationLatitude() const;
     double gpsLocationLongitude() const;
 
+    void setFriendModel(FriendModel* friendModel);
+
 signals:
     void positionChanged();
     void screenCenterChanged();
@@ -41,12 +45,18 @@ signals:
 
 public slots:
     void goToGpsLocation();
+    void onClicked(qreal mouseX, qreal mouseY);
 
 private slots:
     void positionUpdated(const QGeoPositionInfo& positionInfo);
     void panned();
+    void onFriendModelReset();
 
 private:
+    Q_DISABLE_COPY(GeoMap);
+    Q_DECLARE_PRIVATE(GeoMap);
+    QScopedPointer<GeoMapPrivate> d_ptr;
+
     QGeoPositionInfoSource *source;
     QGeoCoordinate prevGpsLocation;
     bool initialized;
index ebb9748..be9edcd 100644 (file)
@@ -48,11 +48,47 @@ void FriendModel::addFriend(const User &singleFriend)
     endInsertRows();
 }
 
-//void RouteModel::setSegments(const QList<RouteSegment>& segments)
+namespace {
+    QList<User *> filtered(const QList<User *>& all_friends, const QList<User *>& filter)
+    {
+        QList<User *> result = all_friends;
+        if (!filter.isEmpty()) {
+            foreach(User* user, all_friends) {
+                if (!filter.contains(user))
+                    result.removeAll(user);
+            }
+        }
+        return result;
+    }
+}
+
 void FriendModel::setFriends(const QList<User *>& friends)
 {
     beginResetModel();
-    m_friends = friends;
+    m_all_friends = friends;
+    m_friends = filtered(m_all_friends, m_filter);
+    endResetModel();
+}
+
+void FriendModel::resetFriends()
+{
+    beginResetModel();
+    endResetModel();
+}
+
+void FriendModel::filter(const QList<User*>& friends)
+{
+    beginResetModel();
+    m_filter = friends;
+    m_friends = filtered(m_all_friends, m_filter);
+    endResetModel();
+}
+
+void FriendModel::resetFilter()
+{
+    beginResetModel();
+    m_filter.clear();
+    m_friends = m_all_friends;
     endResetModel();
 }
 
@@ -68,7 +104,7 @@ QVariant FriendModel::data(const QModelIndex & index, int role) const {
     const User *user = m_friends[index.row()];
     switch (role) {
         case CoordinateRole:
-//            return user->coordinates();
+            return QVariant::fromValue(user->coordinates());
         case AddressRole:
             return user->address();
         case NameRole:
@@ -97,6 +133,8 @@ QVariant FriendModel::data(const QModelIndex & index, int role) const {
             }
         case UserIdRole:
             return user->userId();
+        case UserRole:
+            return QVariant::fromValue((void*)user);
         default:
             return QVariant();
     }
index b4e6a62..edd117f 100644 (file)
@@ -42,16 +42,24 @@ public:
         TimeStampRole,
         DistanceRole,
         DistanceUnitRole,
-        UserIdRole
+        UserIdRole,
+        UserRole
     };
 
     explicit FriendModel(QObject *parent = 0);
     void addFriend(const User &singleFriend);
     void setFriends(const QList<User *>& friends);
+    void resetFriends();
+
+    void filter(const QList<User*>& friends);
+    Q_INVOKABLE void resetFilter();
+
     int rowCount(const QModelIndex & parent = QModelIndex()) const;
     QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
 
 private:
+    QList<User *> m_all_friends;
+    QList<User *> m_filter;
     QList<User *> m_friends;
 };