--- /dev/null
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <QtGui>
+
+#include "qtmadslabelad.h"
+
+#include <QPixmap>
+#include <QUrl>
+#include <QDebug>
+
+#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<QString, QVariant> &ad)
+{
+ ParseState parseSuccess = parseReady;
+ bool textOrImageFound = false;
+
+ QHash<QString, QVariant>::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<QString, QVariant> &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();
+}