5c5ea212118e6b076cbd548bd2a1da3d91b306fe
[qtmads] / src / qtmadsbannerad.cpp
1 /*
2  * Copyright (c) 2009 Eetu Lehmusvuo.
3  *
4  * This file is part of QtMAds.
5  *
6  * QtMAds is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * QtMAds is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with QtMAds.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include <QtGui>
22
23 #include "qtmadsbannerad.h"
24
25 #include <QApplication>
26 #include <QUrl>
27 #include <QDebug>
28 #include <QWebView>
29 #include <QWebFrame>
30
31 #include "qtmadsservice.h"
32
33 QtmadsBannerAd::QtmadsBannerAd(QString serviceName, quint32 adGroupId, AdFit fitting, QWidget *parent)
34     : QtmadsAdWidget(serviceName, imageBannerAd, adGroupId, fitting, parent)
35 {
36     this->webView = new QWebView(this);
37     QHBoxLayout *layout = new QHBoxLayout(this);
38     this->setLayout(layout);
39     this->layout()->addWidget(webView);
40
41     this->webView->hide();
42
43     QPalette palette = this->webView->palette();
44     palette.setColor(QPalette::Background, QColor(Qt::transparent));
45     this->webView->setPalette(palette);
46
47     QPalette palette1 = this->webView->palette();
48     palette1.setColor(QPalette::Background, QColor(Qt::transparent));
49     this->webView->page()->setPalette(palette1);
50 }
51
52 QtmadsBannerAd::~QtmadsBannerAd()
53 {
54     this->clearAd();
55 }
56
57 ParseState QtmadsBannerAd::parseFromParameters(QHash<QString, QVariant> &ad)
58 {
59     ParseState parseSuccess = parseFailed;
60
61     QHash<QString, QVariant>::iterator iter;
62
63     for(iter = ad.begin(); iter != ad.end(); iter++){
64         qDebug() << "key: " << iter.key() << " value: " << iter.value().toString();
65         /*if(0 == iter.key().compare(TAG_URL)){
66             adUrl.setUrl(iter.value().toString());
67
68         }else */
69         if(0 == iter.key().compare(TAG_HTML)){
70             this->webView->setHtml(iter.value().toString());
71             connect(webView->page(), SIGNAL(linkClicked(const QUrl &)),this, SLOT(adClicked(const QUrl &)));
72             webView->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
73             this->webView->show();
74
75             //TODO: correct margins; this is just hack to remove margins..
76             this->webView->page()->currentFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
77             this->webView->page()->currentFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
78             this->setContentsMargins(-17,-17,-17,-17);
79
80             parseSuccess = parseReady;
81
82             qDebug() << "##########font size: " << this->webView->palette().color(QPalette::Background).alpha();
83             qDebug() << "##########page color alpha: " << this->webView->page()->palette().color(QPalette::Background).alpha();
84
85         }else if(0 == iter.key().compare(TAG_ADTYPE)){
86             this->setAdType(this->service->getAdTypeFromString(iter.value().toString()));
87
88         }else{
89             qDebug() << "Unknown tag.";
90         }
91     }
92     qDebug() << "parseFromParameters(): " << parseSuccess;
93     return parseSuccess;
94 }
95
96 void QtmadsBannerAd::setAdFontSize(quint32 fontSize)
97 {
98     this->webView->page()->settings()->setFontSize(QWebSettings::MinimumFontSize, fontSize);
99 }
100
101 void QtmadsBannerAd::getAdAsHash(QHash<QString, QVariant> &adParams)
102 {
103     //adParams.insert("adsize", QVariant(this->sizeHint()));
104 }
105
106 void QtmadsBannerAd::setAsBannerAd(bool isOnlyBannerAd)
107 {
108     if(isOnlyBannerAd){
109         this->service->setDefaultAdType(imageBannerAd);
110     }else{
111         //TODO: HASH it
112         if(this->service->defaultAdType() != imageBannerAd)
113             this->service->setDefaultAdType(anyAd);
114     }
115 }
116
117
118 void QtmadsBannerAd::setAsTextBannerAd(bool isOnlyTextBannerAd)
119 {
120     if(isOnlyTextBannerAd){
121         this->service->setDefaultAdType(txtBannerAd);
122     }else{
123         //TODO: HASH it
124         if(this->service->defaultAdType() != txtBannerAd)
125             this->service->setDefaultAdType(anyAd);
126     }
127 }
128
129 void QtmadsBannerAd::clearAd()
130 {
131     disconnect(webView->page(), SIGNAL(linkClicked(const QUrl &)),this, SLOT(adClicked(const QUrl &)));
132     this->webView->hide();
133 }