Rework to raise navigation app and improve behavior
[apps/poi-yelp.git] / InfoPanel.cpp
1 #include <QWidget>
2 #include <QFont>
3 #include <QNetworkRequest>
4 #include <QNetworkReply>
5 #include <QEventLoop>
6 #include <QPixmap>
7 #include <QImageReader>
8 #include "InfoPanel.h"
9 #include "traces.h"
10
11 #define BTN_STYLE "background-color: #333333; color: white;"
12
13 #define FONT_SIZE_BOLD      22
14 #define FONT_SIZE           16
15 #define STARS_IMG_OFFSET    1520
16 #define STARS_IMG_HEIGHT    69
17 #define STARS_IMG_WIDTH     324
18
19 #define LABEL_NAME_HEIGHT       37
20 #define LABEL_ADDR_HEIGHT       30
21 #define LABEL_PHONE_HEIGHT      30
22 #define LABEL_IMG_HEIGHT        220
23 #define LABEL_REVIEWS_HEIGHT    24
24
25 InfoPanel::InfoPanel(QWidget *parent, QRect rect):
26             QWidget(parent),
27             nameLabel(this, rect),
28             imageLabel(this, rect),
29             addressLabel(this, rect),
30             phoneLabel(this, rect),
31             imgRatingLabel(this, rect),
32             nbReviewsLabel(this, rect),
33             btnsBackground(this, rect),
34             cancelButton("Cancel", this),
35             goButton("Go !", this),
36             networkManager(parent)
37 {
38     setGeometry(rect);
39     setVisible(false);
40 }
41
42 void InfoPanel::populateInfo(Business & business)
43 {
44     int y = 0;
45     QPixmap pixmap;
46     bool isImageDownloaded = false;
47     QFont font, fontBold;
48     QRect rect = this->geometry();
49
50     font = nameLabel.font();
51     font.setPointSize(FONT_SIZE);
52
53     fontBold = nameLabel.font();
54     fontBold.setPointSize(FONT_SIZE_BOLD);
55     fontBold.setBold(true);
56
57     /* Preload image Url: */
58     TRACE_INFO("Image URL: %s", qPrintable(business.ImageUrl));
59     QEventLoop eventLoop;
60     QObject::connect(&networkManager, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
61     QNetworkRequest req(QUrl(business.ImageUrl));
62     QNetworkReply* reply = networkManager.get(req);
63
64     eventLoop.exec(); // wait for answer
65     if (reply->error() == QNetworkReply::NoError)
66     {
67         QByteArray jpegData = reply->readAll();
68         pixmap.loadFromData(jpegData);
69         isImageDownloaded = true;
70     }
71     networkManager.disconnect();
72     delete reply;
73
74     /* Display Name: */
75     nameLabel.Init(y, LABEL_NAME_HEIGHT, business.Name, &fontBold);
76     y += LABEL_NAME_HEIGHT;
77
78     /* Display Address: */
79     addressLabel.Init(y, LABEL_ADDR_HEIGHT,
80         business.Address+", "+business.City+", "+business.State+" "+business.ZipCode+", "+business.Country,
81         &font);
82     y += LABEL_ADDR_HEIGHT;
83
84     /* Display phone number: */
85     phoneLabel.Init(y, LABEL_PHONE_HEIGHT, business.Phone, &font);
86     y += LABEL_PHONE_HEIGHT;
87
88     /* Image Url: */
89     imageLabel.Init(y, LABEL_IMG_HEIGHT, QString(""));
90     y += LABEL_IMG_HEIGHT;
91     if (isImageDownloaded)
92     {
93         imageLabel.setPixmap(pixmap.scaled(QSize(rect.width(), LABEL_IMG_HEIGHT-6), Qt::KeepAspectRatio));
94     }
95
96     /* Display number of reviews: */
97     nbReviewsLabel.Init(y, LABEL_REVIEWS_HEIGHT, QString("Number of reviews : %1").arg(business.ReviewCount), &font);
98     y += LABEL_REVIEWS_HEIGHT;
99
100     /* Rating image: */
101     QImageReader reader(QString(":/images/stars_map_www.png"));
102     int RatingImgIndex = (int)((double)business.Rating*2)-1;
103     if (RatingImgIndex < 0)
104     {
105         RatingImgIndex = 0;
106     }
107     reader.setClipRect(QRect(0,
108         STARS_IMG_OFFSET + RatingImgIndex*STARS_IMG_HEIGHT, STARS_IMG_WIDTH, STARS_IMG_HEIGHT));
109     const QImage image = reader.read();
110     imgRatingLabel.Init(y, STARS_IMG_HEIGHT, QString(""));
111     y += STARS_IMG_HEIGHT;
112     imgRatingLabel.setPixmap(QPixmap::fromImage(image).scaled(QSize(rect.width() / 3, STARS_IMG_HEIGHT), Qt::KeepAspectRatio));
113
114     /* Buttons: */
115     btnsBackground.Init(y, 70, QString(""));
116     y += 70;
117
118     cancelButton.setStyleSheet(BTN_STYLE);
119     cancelButton.setFont(font);
120     cancelButton.setMinimumSize(QSize(rect.width()/4, 50));
121     //cancelButton.setGeometry(QRect(rect.x()+rect.width()/8, rect.y()+y-60, rect.width()/4, 50));
122     cancelButton.setGeometry(QRect(rect.width()/8, y-60, rect.width()/4, 50));
123
124     goButton.setStyleSheet(BTN_STYLE);
125     goButton.setFont(font);
126     goButton.setMinimumSize(QSize(rect.width()/4, 50));
127     goButton.setGeometry(QRect(rect.width()*5/8, y-60, rect.width()/4, 50));
128 }