poi-yelp: update LICENSE with text from COPYING
[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, Business & business, QRect rect):
26             nameLabel(parent, rect),
27             imageLabel(parent, rect),
28             addressLabel(parent, rect),
29             phoneLabel(parent, rect),
30             imgRatingLabel(parent, rect),
31             nbReviewsLabel(parent, rect),
32             btnsBackground(parent, rect),
33             cancelButton("Cancel", parent),
34             goButton("Go !", parent),
35             networkManager(parent)
36 {
37     int y = 0;
38     QPixmap pixmap;
39     bool isImageDownloaded = false;
40     QFont font, fontBold;
41
42     font = nameLabel.font();
43     font.setPointSize(FONT_SIZE);
44
45     fontBold = nameLabel.font();
46     fontBold.setPointSize(FONT_SIZE_BOLD);
47     fontBold.setBold(true);
48
49     /* Preload image Url: */
50     TRACE_INFO("Image URL: %s", qPrintable(business.ImageUrl));
51     QEventLoop eventLoop;
52     QObject::connect(&networkManager, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
53     QNetworkRequest req(QUrl(business.ImageUrl));
54     QNetworkReply* reply = networkManager.get(req);
55
56     eventLoop.exec(); // wait for answer
57     if (reply->error() == QNetworkReply::NoError)
58     {
59         QByteArray jpegData = reply->readAll();
60         pixmap.loadFromData(jpegData);
61         isImageDownloaded = true;
62     }
63     networkManager.disconnect();
64     delete reply;
65
66     /* Display Name: */
67     nameLabel.Init(y, LABEL_NAME_HEIGHT, business.Name, &fontBold);
68     y += LABEL_NAME_HEIGHT;
69
70     /* Display Address: */
71     addressLabel.Init(y, LABEL_ADDR_HEIGHT,
72         business.Address+", "+business.City+", "+business.State+" "+business.ZipCode+", "+business.Country,
73         &font);
74     y += LABEL_ADDR_HEIGHT;
75
76     /* Display phone number: */
77     phoneLabel.Init(y, LABEL_PHONE_HEIGHT, business.Phone, &font);
78     y += LABEL_PHONE_HEIGHT;
79
80     /* Image Url: */
81     imageLabel.Init(y, LABEL_IMG_HEIGHT, QString(""));
82     y += LABEL_IMG_HEIGHT;
83     if (isImageDownloaded)
84     {
85         imageLabel.setPixmap(pixmap.scaled(QSize(rect.width(), LABEL_IMG_HEIGHT-6), Qt::KeepAspectRatio));
86     }
87
88     /* Display number of reviews: */
89     nbReviewsLabel.Init(y, LABEL_REVIEWS_HEIGHT, QString("Number of reviews : %1").arg(business.ReviewCount), &font);
90     y += LABEL_REVIEWS_HEIGHT;
91
92     /* Rating image: */
93     QImageReader reader(QString(":/images/stars_map_www.png"));
94     int RatingImgIndex = (int)((double)business.Rating*2)-1;
95     if (RatingImgIndex < 0)
96     {
97         RatingImgIndex = 0;
98     }
99     reader.setClipRect(QRect(0,
100         STARS_IMG_OFFSET + RatingImgIndex*STARS_IMG_HEIGHT, STARS_IMG_WIDTH, STARS_IMG_HEIGHT));
101     const QImage image = reader.read();
102     imgRatingLabel.Init(y, STARS_IMG_HEIGHT, QString(""));
103     y += STARS_IMG_HEIGHT;
104     imgRatingLabel.setPixmap(QPixmap::fromImage(image).scaled(QSize(rect.width() / 3, STARS_IMG_HEIGHT), Qt::KeepAspectRatio));
105
106     /* Buttons: */
107     btnsBackground.Init(y, 70, QString(""));
108     y += 70;
109
110     cancelButton.setStyleSheet(BTN_STYLE);
111     cancelButton.setFont(font);
112     cancelButton.setMinimumSize(QSize(rect.width()/4, 50));
113     cancelButton.setGeometry(QRect(rect.x()+rect.width()/8, rect.y()+y-60, rect.width()/4, 50));
114     cancelButton.setVisible(true);
115
116     goButton.setStyleSheet(BTN_STYLE);
117     goButton.setFont(font);
118     goButton.setMinimumSize(QSize(rect.width()/4, 50));
119     goButton.setGeometry(QRect(rect.x()+rect.width()*5/8, rect.y()+y-60, rect.width()/4, 50));
120     goButton.setVisible(true);
121 }