developed location update functionality tiensvi
authorVille Tiensuu <ville.tiensuu@ixonos.com>
Wed, 5 May 2010 12:00:39 +0000 (15:00 +0300)
committerVille Tiensuu <ville.tiensuu@ixonos.com>
Wed, 5 May 2010 12:00:39 +0000 (15:00 +0300)
src/map/mapengine.cpp
src/map/mapengine.h

index 3bf699b..fde09b9 100644 (file)
@@ -343,55 +343,74 @@ void MapEngine::receiveFriendLocations(QList<User *> &friendsList)
 
     if (receiveFriendsLocationsCounter == 0) {
         receiveFriendsLocationsCounter++;
-
-        for (int i=0; i<friendsList.count(); i++) {
-            FriendLocationItem *friendLocation
-                    = new FriendLocationItem(friendsList.at(i)->profileImage(),
-                                             friendsList.at(i)->coordinates(),0);
-
-            friendLocation->setUserId(friendsList.at(i)->userId());
-            m_friendItems.append(friendLocation);
-            m_mapScene->addItem(friendLocation);
-        }
-
+        updateFriendItemList(friendsList);
     }
 
     else {
         receiveFriendsLocationsCounter++;
+        updateFriendLocationsAndImages(friendsList);
+        cleanOldFriendData(friendsList);
+        updateFriendItemList(friendsList);
+    }    
+}
 
-        QList<int> currentFriendList;        
+void MapEngine::updateFriendItemList(const QList<User *> &friendsList)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    qDeleteAll(m_friendItems.begin(),m_friendItems.end());
+    m_friendItems.clear();
 
-        //finds users that have still location available.
-        //stores their index in to currentFriendlist       
-        //set new location if it is changed
-        //set new profile picture if it is chenged
-        for (int i=0; i<friendsList.count(); i++) {
 
-            for (int j=0; j<m_friendItems.count(); j++) {
+    for (int i=0; i<friendsList.count(); i++) {
+        FriendLocationItem *friendLocation
+                = new FriendLocationItem(friendsList.at(i)->profileImage(),
+                                         friendsList.at(i)->coordinates(),0);
+
+        friendLocation->setUserId(friendsList.at(i)->userId());
+        m_friendItems.append(friendLocation);
+        m_mapScene->addItem(friendLocation);
+    }
+}
 
-                if (m_friendItems.at(j)->userId() == friendsList.at(i)->userId()) {
-                    currentFriendList.append(j);                   
+void MapEngine::updateFriendLocationsAndImages(const QList<User *> &friendsList)
+{    
+    for (int i=0; i<friendsList.count(); i++) {
 
-                    if (m_friendItems.at(j)->position()
-                        != convertLatLonToSceneCoordinate(friendsList.at(i)->coordinates()))
-                        m_friendItems.at(j)->setPosition(friendsList.at(i)->coordinates());
+        for (int j=0; j<m_friendItems.count(); j++) {
 
-                    if (m_friendItems.at(j)->profileImageUrl()
-                        != friendsList.at(i)->profileImageUrl()) {
-                        m_friendItems.at(j)->setPixmap(friendsList.at(i)->profileImage());
-                    }
+            if (m_friendItems.at(j)->userId() == friendsList.at(i)->userId()) {                
+
+                if (m_friendItems.at(j)->position()
+                    != convertLatLonToSceneCoordinate(friendsList.at(i)->coordinates()))
+                    m_friendItems.at(j)->setPosition(friendsList.at(i)->coordinates());
+
+                if (m_friendItems.at(j)->profileImageUrl()
+                    != friendsList.at(i)->profileImageUrl()) {
+                    m_friendItems.at(j)->setPixmap(friendsList.at(i)->profileImage());
                 }
             }
         }
+    }
+}
 
-        // clean scene and m_friendItems list:
-        // (remove such user who don't has logged out from siture)
-        int m_friendItemsOriginalCount = m_friendItems.count();
-        for (int i=0; i<m_friendItemsOriginalCount; i++) {
-            if (currentFriendList.contains(i) == false) {
-                m_mapScene->removeItem(m_friendItems.at(i));
-                m_friendItems.removeAt(i);
-            }
+void MapEngine::cleanOldFriendData(const QList<User *> &friendsList)
+{
+    bool friendGone = true;
+
+    for (int i=0; i<m_friendItems.count(); i++) {
+
+        for (int j=0; j<friendsList.count(); j++) {
+
+            if (m_friendItems.at(i)->userId() == friendsList.at(j)->userId())
+                friendGone = false;
         }
+
+        if (friendGone) {
+            m_mapScene->removeItem(m_friendItems.at(i));
+            m_friendItems.removeAt(i);
+        }
+
+        friendGone = true;
     }
 }
index 3b7c598..60814d0 100644 (file)
@@ -5,6 +5,7 @@
        Sami Rämö - sami.ramo@ixonos.com
        Jussi Laitinen - jussi.laitinen@ixonos.com
        Pekka Nissinen - pekka.nissinen@ixonos.com
+       Ville Tiensuu - ville.tiensuu@ixonos.com
 
    Situare is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
@@ -43,6 +44,7 @@
 * @author Sami Rämö - sami.ramo (at) ixonos.com
 * @author Jussi Laitinen - jussi.laitinen (at) ixonos.com
 * @author Pekka Nissinen - pekka.nissinen (at) ixonos.com
+* @author Ville Tiensuu - ville.tiensuu (at) ixonos.com
 */
 class MapEngine : public QObject
 {
@@ -151,6 +153,7 @@ public slots:
     * @param user User info
     */
     void receiveOwnLocation(User *user);
+
     /**
     * @brief Slot to catch friends location data
     *
@@ -170,6 +173,13 @@ private:
     QRect calculateTileGrid(QPoint sceneCoordinate);
 
     /**
+    * @brief clean old friend data from m_mapScene and m_friendItems
+    *
+    * @param friendsList QList item of friend information
+    */
+    void cleanOldFriendData(const QList<User *> &friendsList);
+
+    /**
     * @brief Get new tiles.
     *
     * Calculates which tiles has to be fetched. Does emit fetchImage for tiles which
@@ -195,6 +205,20 @@ private:
     int tileMaxValue(int zoomLevel);
 
     /**
+    * @brief updates data member m_friendItems from given parameter
+    *
+    * @param friendsList QList item of friend information
+    */
+    void updateFriendItemList(const QList<User *> &friendsList);
+
+    /**
+    * @brief updates data member m_friendItems values that differs from given parameter
+    *
+    * @param friendsList QList item of friend information
+    */
+    void updateFriendLocationsAndImages(const QList<User *> &friendsList);
+
+    /**
     * @brief Updates the current view rect including margins
     *
     * Calculates tiles rect in scene based on m_viewTilesGrid and
@@ -274,7 +298,7 @@ private:
     QSize m_viewSize; ///< Current view size
     bool m_zoomedIn; ///< Flag for checking if zoomed in when zoom is finished
     int m_zoomLevel; ///< Current zoom level
-    QList<FriendLocationItem *> m_friendItems; ///< Location of friends
+    QList<FriendLocationItem *> m_friendItems; ///< List of friendLocationItems
 };
 
 #endif // MAPENGINE_H