2 * Copyright (c) 2009 Eetu Lehmusvuo.
4 * This file is part of QtMAds.
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.
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.
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/>.
23 #include "qtmadslabelad.h"
29 #include "qtmadsservice.h"
31 QtmadsLabelAd::QtmadsLabelAd(QString serviceName, quint32 adGroupId, AdFit fitting, QWidget *parent)
32 : QtmadsAdWidget(serviceName, imageAd, adGroupId, fitting, parent)
34 label = new QLabel(this);
35 QHBoxLayout *layout = new QHBoxLayout(this);
36 layout->addWidget(label);
37 this->setLayout(layout);
40 QtmadsLabelAd::~QtmadsLabelAd()
45 ParseState QtmadsLabelAd::parseFromParameters(QHash<QString, QVariant> &ad)
47 ParseState parseSuccess = parseReady;
48 bool textOrImageFound = false;
50 QHash<QString, QVariant>::iterator iter;
52 for(iter = ad.begin(); iter != ad.end() && parseSuccess != parseFailed; iter++){
53 //qDebug() << "key: " << iter.key() << " value: " << iter.value().toString();
54 if(0 == iter.key().compare(TAG_TEXT)){
55 label->setText(iter.value().toString());
56 if(this->adFit == fitAdToWidget){
57 //TODO: fit text to ad
58 /*QFontMetrics metrics (label->font());
59 int pixelWidth = metrics.width(label->text());
60 int pixelHeight = metrics.height();
62 label->font().setPixelSize();*/
63 }/*TODO: else if(this->adFit == adToFit){
65 textOrImageFound = true;
67 }else if(0 == iter.key().compare(TAG_CONTENT_URL)){
68 connect(service, SIGNAL(imageRequestReady(const QImage *)),this, SLOT(imageRequestSucceeded(const QImage *)));
69 connect(service, SIGNAL(imageRequestFailed()),this, SLOT(imageRequestFailed()));
70 this->service->getRemoteImage(iter.value().toString());
71 parseSuccess = parseWaiting;
72 textOrImageFound = true;
74 }else if(0 == iter.key().compare(TAG_ADTYPE)){
75 this->setAdType(this->service->getAdTypeFromString(iter.value().toString()));
78 qDebug() << "Unknown tag.";
82 // either text or image should be found from ad
84 parseSuccess = parseFailed;
89 void QtmadsLabelAd::imageRequestSucceeded(const QImage *image)
91 disconnect(service, SIGNAL(imageRequestReady(const QImage *)),this, SLOT(imageRequestSucceeded(const QImage *)));
92 disconnect(service, SIGNAL(imageRequestFailed()),this, SLOT(imageRequestFailed()));
96 if(this->adFit == fitAdToWidget){
97 pic = QPixmap::fromImage(*image).scaled(this->sizeHint());
98 }/*TODO: else if(this->adFit == fitWidgetToAd){
100 pic = QPixmap::fromImage(*image);
102 label->setPixmap(pic);
110 void QtmadsLabelAd::imageRequestFailed()
115 void QtmadsLabelAd::getAdAsHash(QHash<QString, QVariant> &adParams)
117 //adParams.insert("adsize", QVariant(this->sizeHint()));
120 void QtmadsLabelAd::setAsTextAd(bool isOnlyTextAd)
123 this->service->setDefaultAdType(txtAd);
126 if(this->service->defaultAdType() != txtAd)
127 this->service->setDefaultAdType(anyAd);
131 void QtmadsLabelAd::setAsImageAd(bool isOnlyImageAd)
134 this->service->setDefaultAdType(imageAd);
137 if(this->service->defaultAdType() != imageAd)
138 this->service->setDefaultAdType(anyAd);
142 void QtmadsLabelAd::clearAd()
144 this->label->clear();