Added method to group friends by distance.
authorJussi Laitinen <jupe@l3l7588.ixonos.local>
Wed, 10 Nov 2010 14:08:56 +0000 (16:08 +0200)
committerJussi Laitinen <jupe@l3l7588.ixonos.local>
Wed, 10 Nov 2010 14:08:56 +0000 (16:08 +0200)
src/ui/meetpeoplepanel.cpp
src/ui/meetpeoplepanel.h

index 1e3271b..967b9fc 100644 (file)
@@ -100,6 +100,46 @@ bool MeetPeoplePanel::friendDistanceLessThan(const User &user1, const User &user
     return user1Value < user2Value;
 }
 
+QList<QList<User> > MeetPeoplePanel::groupFriendsByDistance(const QList<User> &friends)
+{
+    const int AEROPLANE_DISTANCE = 5000;///< Aeroplane distance limit
+    const int CAR_DISTANCE = 500;       ///< Car distance limit
+    const int WALK_DISTANCE = 5;        ///< Walk distance limit
+
+    QList<QList<User> > groupedFriends;
+    QList<User> walkingDistanceUsers;
+    QList<User> drivingDistanceUsers;
+    QList<User> flyingDistanceUsers;
+    QList<User> rocketDistanceUsers;
+
+    foreach (const User user, friends) {
+        double value;
+        QString unit;
+        user.distance(value, unit);
+
+        if ((unit == "m") || (value < WALK_DISTANCE))
+            walkingDistanceUsers.append(user);
+        else if (value < CAR_DISTANCE)
+            drivingDistanceUsers.append(user);
+        else if (value < AEROPLANE_DISTANCE)
+            flyingDistanceUsers.append(user);
+        else
+            rocketDistanceUsers.append(user);
+    }
+
+    qSort(walkingDistanceUsers.begin(), walkingDistanceUsers.end(), friendDistanceLessThan);
+    qSort(drivingDistanceUsers.begin(), drivingDistanceUsers.end(), friendDistanceLessThan);
+    qSort(flyingDistanceUsers.begin(), flyingDistanceUsers.end(), friendDistanceLessThan);
+    qSort(rocketDistanceUsers.begin(), rocketDistanceUsers.end(), friendDistanceLessThan);
+
+    groupedFriends.append(walkingDistanceUsers);
+    groupedFriends.append(drivingDistanceUsers);
+    groupedFriends.append(flyingDistanceUsers);
+    groupedFriends.append(rocketDistanceUsers);
+
+    return groupedFriends;
+}
+
 void MeetPeoplePanel::hideEvent(QHideEvent *event)
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -181,12 +221,26 @@ void MeetPeoplePanel::populateInterestingPeopleListView(QList<User> &friends, QL
     initItemDelegates();
 
     if (friends.count() > 0) {
-
-        qSort(friends.begin(), friends.end(), friendDistanceLessThan);
-        foreach (User user, friends) {
-            PersonListItem *item = new PersonListItem();
-            item->setPersonData(user, true);
-            m_personListView->addListItem(user.userId(), item);
+        QStringList headers;
+        headers.append(tr("Walking distance:"));
+        headers.append(tr("Driving distance:"));
+        headers.append(tr("Flying distance:"));
+        headers.append(tr("Rocket distance:"));
+
+        QList<QList<User> > groupedFriends = groupFriendsByDistance(friends);
+
+        foreach (QList<User> group, groupedFriends) {
+            qWarning() << group.count();
+            QString header = headers.takeFirst();
+            if (group.count() > 0) {
+                addHeaderItem(header, header);
+
+                foreach (User user, group) {
+                    PersonListItem *item = new PersonListItem();
+                    item->setPersonData(user, true);
+                    m_personListView->addListItem(user.userId(), item);
+                }
+            }
         }
     }
 
index 43c9d2a..8cd6157 100644 (file)
@@ -122,12 +122,16 @@ private:
     */
     void addHeaderItem(const QString &key, const QString &title);
 
+    static bool friendDistanceLessThan(const User &user1, const User &user2);
+
+    QList<QList<User> > groupFriendsByDistance(const QList<User> &friends);
+
     /**
     * @brief Inits item delegates for message list view.
     */
     void initItemDelegates();
 
-    static bool friendDistanceLessThan(const User &user1, const User &user2);
+
 
 /*******************************************************************************
  * SIGNALS