Added routing feature to friend and location list.
[situare] / src / ui / routingpanel.cpp
1 #include "coordinates/geocoordinate.h"
2 #include "locationlistitem.h"
3 #include "locationlistview.h"
4 #include "extendedlistitemdelegate.h"
5 #include "routing/location.h"
6 #include "panelcommon.h"
7
8 #include "routingpanel.h"
9
10 RoutingPanel::RoutingPanel(QWidget *parent)
11     : QWidget(parent)
12 {
13     qDebug() << __PRETTY_FUNCTION__;
14
15     QVBoxLayout *routingLayout = new QVBoxLayout;
16     routingLayout->setMargin(0);
17     routingLayout->setSpacing(0);
18     setLayout(routingLayout);
19
20     QHBoxLayout *headerLayout = new QHBoxLayout();
21     headerLayout->setContentsMargins(FRIENDPANEL_FILTER_MARGIN_LEFT, 0,
22                                      FRIENDPANEL_FILTER_MARGIN_RIGHT, 0);
23
24     QVBoxLayout *listViewLayout = new QVBoxLayout;
25     listViewLayout->setContentsMargins(PANEL_MARGIN_LEFT, 0, PANEL_MARGIN_RIGHT, 0);
26
27     m_routeButton = new QPushButton("Route to location");
28
29     m_locationListHeaderWidget = new QWidget();
30     m_locationListHeaderWidget->setLayout(headerLayout);
31     m_locationListHeaderWidget->setAutoFillBackground(true);
32     QPalette labelPalette = m_locationListHeaderWidget->palette();
33     labelPalette.setColor(QPalette::Background, Qt::black);
34     m_locationListHeaderWidget->setPalette(labelPalette);
35     m_locationListHeaderWidget->hide();
36
37     m_locationListLabel = new QLabel(this);
38
39     m_locationListView = new LocationListView(this);
40     m_locationListView->setItemDelegate(new ExtendedListItemDelegate(this));
41
42     headerLayout->addWidget(m_locationListLabel, 0, Qt::AlignCenter);
43
44     listViewLayout->addWidget(m_locationListView);
45
46     routingLayout->addWidget(m_routeButton);
47     routingLayout->addWidget(m_locationListHeaderWidget);
48     routingLayout->addLayout(listViewLayout);
49
50     connect(m_locationListView,
51             SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)),
52             this,
53             SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)));
54
55     connect(m_routeButton, SIGNAL(clicked()),
56             this, SLOT(routeToSelectedLocation()));
57 }
58
59 void RoutingPanel::populateLocationListView(const QList<Location> &locations)
60 {
61     qDebug() << __PRETTY_FUNCTION__;
62
63     m_locationListHeaderWidget->show();
64     m_locationListLabel->setText(tr("Search results: %1").arg(locations.count()));
65
66     m_locationListView->clearList();
67
68     for (int i = 0; i < locations.size(); ++i) {
69         LocationListItem *item = new LocationListItem();
70         item->setLocationData(locations.at(i));
71         m_locationListView->addListItem(QString::number(i), item);
72     }
73
74     //openPanel();
75
76     const int FIRST_LOCATION_ITEM_INDEX = 0;
77     const int ONE_LOCATION_ITEM = 1;
78
79     if (locations.size() == ONE_LOCATION_ITEM) {
80         ListItem *item = m_locationListView->listItemAt(FIRST_LOCATION_ITEM_INDEX);
81
82         if (item)
83             m_locationListView->setSelectedItem(item);
84     }
85 }
86
87 void RoutingPanel::routeToSelectedLocation()
88 {
89     qDebug() << __PRETTY_FUNCTION__;
90
91     LocationListItem *item = dynamic_cast<LocationListItem *>
92                              (m_locationListView->selectedItem());
93
94     if (item)
95         emit routeToLocation(item->coordinates());
96 }