X-Git-Url: https://vcs.maemo.org/git/?a=blobdiff_plain;f=src%2Fqtmadslabelad.cpp;fp=src%2Fqtmadslabelad.cpp;h=592918d83b7fcdac9a83d29d732c5b0d6fea58d4;hb=e494cd05f1809789fd03d888c5592753d04f2031;hp=0000000000000000000000000000000000000000;hpb=4c87a3c868d40870d0eed21fb6e01c014acd3062;p=qtmads diff --git a/src/qtmadslabelad.cpp b/src/qtmadslabelad.cpp new file mode 100644 index 0000000..592918d --- /dev/null +++ b/src/qtmadslabelad.cpp @@ -0,0 +1,145 @@ +/* + * Copyright (c) 2009 Eetu Lehmusvuo. + * + * This file is part of QtMAds. + * + * QtMAds is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * QtMAds is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with QtMAds. If not, see . + * + */ + +#include + +#include "qtmadslabelad.h" + +#include +#include +#include + +#include "qtmadsservice.h" + +QtmadsLabelAd::QtmadsLabelAd(QString serviceName, quint32 adGroupId, AdFit fitting, QWidget *parent) + : QtmadsAdWidget(serviceName, imageAd, adGroupId, fitting, parent) +{ + label = new QLabel(this); + QHBoxLayout *layout = new QHBoxLayout(this); + layout->addWidget(label); + this->setLayout(layout); +} + +QtmadsLabelAd::~QtmadsLabelAd() +{ + this->clearAd(); +} + +ParseState QtmadsLabelAd::parseFromParameters(QHash &ad) +{ + ParseState parseSuccess = parseReady; + bool textOrImageFound = false; + + QHash::iterator iter; + + for(iter = ad.begin(); iter != ad.end() && parseSuccess != parseFailed; iter++){ + //qDebug() << "key: " << iter.key() << " value: " << iter.value().toString(); + if(0 == iter.key().compare(TAG_TEXT)){ + label->setText(iter.value().toString()); + if(this->adFit == fitAdToWidget){ + //TODO: fit text to ad + /*QFontMetrics metrics (label->font()); + int pixelWidth = metrics.width(label->text()); + int pixelHeight = metrics.height(); + if() + label->font().setPixelSize();*/ + }/*TODO: else if(this->adFit == adToFit){ + }*/ + textOrImageFound = true; + + }else if(0 == iter.key().compare(TAG_CONTENT_URL)){ + connect(service, SIGNAL(imageRequestReady(const QImage *)),this, SLOT(imageRequestSucceeded(const QImage *))); + connect(service, SIGNAL(imageRequestFailed()),this, SLOT(imageRequestFailed())); + this->service->getRemoteImage(iter.value().toString()); + parseSuccess = parseWaiting; + textOrImageFound = true; + + }else if(0 == iter.key().compare(TAG_ADTYPE)){ + this->setAdType(this->service->getAdTypeFromString(iter.value().toString())); + + }else{ + qDebug() << "Unknown tag."; + } + } + + // either text or image should be found from ad + if(!textOrImageFound) + parseSuccess = parseFailed; + + return parseSuccess; +} + +void QtmadsLabelAd::imageRequestSucceeded(const QImage *image) +{ + disconnect(service, SIGNAL(imageRequestReady(const QImage *)),this, SLOT(imageRequestSucceeded(const QImage *))); + disconnect(service, SIGNAL(imageRequestFailed()),this, SLOT(imageRequestFailed())); + + if(image){ + QPixmap pic; + if(this->adFit == fitAdToWidget){ + pic = QPixmap::fromImage(*image).scaled(this->sizeHint()); + }/*TODO: else if(this->adFit == fitWidgetToAd){ + }*/else{ + pic = QPixmap::fromImage(*image); + } + label->setPixmap(pic); + + emit newAdReady(); + }else{ + emit newAdFailed(); + } +} + +void QtmadsLabelAd::imageRequestFailed() +{ + emit newAdFailed(); +} + +void QtmadsLabelAd::getAdAsHash(QHash &adParams) +{ + //adParams.insert("adsize", QVariant(this->sizeHint())); +} + +void QtmadsLabelAd::setAsTextAd(bool isOnlyTextAd) +{ + if(isOnlyTextAd){ + this->service->setDefaultAdType(txtAd); + }else{ + //TODO: HASH it + if(this->service->defaultAdType() != txtAd) + this->service->setDefaultAdType(anyAd); + } +} + +void QtmadsLabelAd::setAsImageAd(bool isOnlyImageAd) +{ + if(isOnlyImageAd){ + this->service->setDefaultAdType(imageAd); + }else{ + //TODO: HASH it + if(this->service->defaultAdType() != imageAd) + this->service->setDefaultAdType(anyAd); + } +} + +void QtmadsLabelAd::clearAd() +{ + this->label->clear(); +}