summaryrefslogtreecommitdiffstats
path: root/InfoPanel.cpp
blob: 81cf31725b4b1b5a2b64aeaa9165fc4741d95a29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <QWidget>
#include <QFont>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QEventLoop>
#include <QPixmap>
#include <QImageReader>
#include "InfoPanel.h"
#include "traces.h"

#define BTN_STYLE "background-color: #333333; color: white;"

#define FONT_SIZE_BOLD      22
#define FONT_SIZE           16
#define STARS_IMG_OFFSET    1520
#define STARS_IMG_HEIGHT    69
#define STARS_IMG_WIDTH     324

#define LABEL_NAME_HEIGHT       37
#define LABEL_ADDR_HEIGHT       30
#define LABEL_PHONE_HEIGHT      30
#define LABEL_IMG_HEIGHT        220
#define LABEL_REVIEWS_HEIGHT    24

InfoPanel::InfoPanel(QWidget *parent, QRect rect):
            QWidget(parent),
            nameLabel(this, rect),
            imageLabel(this, rect),
            addressLabel(this, rect),
            phoneLabel(this, rect),
            imgRatingLabel(this, rect),
            nbReviewsLabel(this, rect),
            btnsBackground(this, rect),
            cancelButton("Cancel", this),
            goButton("Go !", this),
            networkManager(parent)
{
    setGeometry(rect);
    setVisible(false);
}

void InfoPanel::populateInfo(Business & business)
{
    int y = 0;
    QPixmap pixmap;
    bool isImageDownloaded = false;
    QFont font, fontBold;
    QRect rect = this->geometry();

    font = nameLabel.font();
    font.setPointSize(FONT_SIZE);

    fontBold = nameLabel.font();
    fontBold.setPointSize(FONT_SIZE_BOLD);
    fontBold.setBold(true);

    /* Preload image Url: */
    TRACE_INFO("Image URL: %s", qPrintable(business.ImageUrl));
    QEventLoop eventLoop;
    QObject::connect(&networkManager, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
    QNetworkRequest req(QUrl(business.ImageUrl));
    QNetworkReply* reply = networkManager.get(req);

    eventLoop.exec(); // wait for answer
    if (reply->error() == QNetworkReply::NoError)
    {
        QByteArray jpegData = reply->readAll();
        pixmap.loadFromData(jpegData);
        isImageDownloaded = true;
    }
    networkManager.disconnect();
    delete reply;

    /* Display Name: */
    nameLabel.Init(y, LABEL_NAME_HEIGHT, business.Name, &fontBold);
    y += LABEL_NAME_HEIGHT;

    /* Display Address: */
    addressLabel.Init(y, LABEL_ADDR_HEIGHT,
        business.Address+", "+business.City+", "+business.State+" "+business.ZipCode+", "+business.Country,
        &font);
    y += LABEL_ADDR_HEIGHT;

    /* Display phone number: */
    phoneLabel.Init(y, LABEL_PHONE_HEIGHT, business.Phone, &font);
    y += LABEL_PHONE_HEIGHT;

    /* Image Url: */
    imageLabel.Init(y, LABEL_IMG_HEIGHT, QString(""));
    y += LABEL_IMG_HEIGHT;
    if (isImageDownloaded)
    {
        imageLabel.setPixmap(pixmap.scaled(QSize(rect.width(), LABEL_IMG_HEIGHT-6), Qt::KeepAspectRatio));
    }

    /* Display number of reviews: */
    nbReviewsLabel.Init(y, LABEL_REVIEWS_HEIGHT, QString("Number of reviews : %1").arg(business.ReviewCount), &font);
    y += LABEL_REVIEWS_HEIGHT;

    /* Rating image: */
    QImageReader reader(QString(":/images/stars_map_www.png"));
    int RatingImgIndex = (int)((double)business.Rating*2)-1;
    if (RatingImgIndex < 0)
    {
        RatingImgIndex = 0;
    }
    reader.setClipRect(QRect(0,
        STARS_IMG_OFFSET + RatingImgIndex*STARS_IMG_HEIGHT, STARS_IMG_WIDTH, STARS_IMG_HEIGHT));
    const QImage image = reader.read();
    imgRatingLabel.Init(y, STARS_IMG_HEIGHT, QString(""));
    y += STARS_IMG_HEIGHT;
    imgRatingLabel.setPixmap(QPixmap::fromImage(image).scaled(QSize(rect.width() / 3, STARS_IMG_HEIGHT), Qt::KeepAspectRatio));

    /* Buttons: */
    btnsBackground.Init(y, 70, QString(""));
    y += 70;

    cancelButton.setStyleSheet(BTN_STYLE);
    cancelButton.setFont(font);
    cancelButton.setMinimumSize(QSize(rect.width()/4, 50));
    //cancelButton.setGeometry(QRect(rect.x()+rect.width()/8, rect.y()+y-60, rect.width()/4, 50));
    cancelButton.setGeometry(QRect(rect.width()/8, y-60, rect.width()/4, 50));

    goButton.setStyleSheet(BTN_STYLE);
    goButton.setFont(font);
    goButton.setMinimumSize(QSize(rect.width()/4, 50));
    goButton.setGeometry(QRect(rect.width()*5/8, y-60, rect.width()/4, 50));
}