From 7160eeb650855a207b271d8399e6f7ede96a5011 Mon Sep 17 00:00:00 2001 From: Lionel Chauvin Date: Sun, 10 Oct 2010 11:47:14 +0200 Subject: Images in visual suggestion are now displayed !! --- src/opensearch/suggestionparser.cpp | 38 ++++++++++++++++++------------------- src/urlbar/listitem.cpp | 36 +++++++++++++++++++++++++++++++++-- src/urlbar/listitem.h | 20 +++++++++++++++++++ src/urlbar/urlresolver.cpp | 4 ++++ 4 files changed, 77 insertions(+), 21 deletions(-) diff --git a/src/opensearch/suggestionparser.cpp b/src/opensearch/suggestionparser.cpp index 01581bf9..02f99644 100644 --- a/src/opensearch/suggestionparser.cpp +++ b/src/opensearch/suggestionparser.cpp @@ -53,38 +53,38 @@ ResponseList XMLParser::parse(const QByteArray &resp) m_reader.clear(); m_reader.addData(resp); - - while (!m_reader.isStartElement() && !m_reader.atEnd()) - { - m_reader.readNext(); - } - while (!(m_reader.isEndElement() && m_reader.name() == QL1S("SearchSuggestion")) && !m_reader.atEnd()) + while (!m_reader.atEnd() && !m_reader.hasError()) { - m_reader.readNext(); - - if (!m_reader.isStartElement()) - continue; + QXmlStreamReader::TokenType token = m_reader.readNext(); + + if (m_reader.isStartDocument()) continue; - if (m_reader.name() == QL1S("Item")) + if (m_reader.isStartElement() && m_reader.name() == QL1S("Item")) { QString title; QString url; QString image; QString description; - while (!m_reader.isEndElement() && !m_reader.atEnd()) + m_reader.readNext(); + + while(!(m_reader.isEndElement() && m_reader.name() == "Item")) { - m_reader.readNext(); + if(m_reader.isStartElement()) + { + if (m_reader.name() == QL1S("Text")) title = m_reader.readElementText(); + if (m_reader.name() == QL1S("Url")) url = m_reader.readElementText(); + if (m_reader.name() == QL1S("Image")) image = m_reader.attributes().value("source").toString(); + if (m_reader.name() == QL1S("Description")) description = m_reader.readElementText(); + } - if (m_reader.name() == QL1S("Text")) title = m_reader.readElementText(); - if (m_reader.name() == QL1S("Url")) url = m_reader.readElementText(); - if (m_reader.name() == QL1S("Image")) image = m_reader.readElementText(); - if (m_reader.name() == QL1S("Description")) description = m_reader.readElementText(); + m_reader.readNext(); } - rlist << Response(url, title, image, description); } + + m_reader.readNext(); } return rlist; @@ -124,7 +124,7 @@ ResponseList JSONParser::parse(const QByteArray &resp) ResponseList rlist; QStringList responsePartsList; qScriptValueToSequence(responseParts.property(1), responsePartsList); - + foreach(QString s, responsePartsList) { rlist << Response(QString(), s, QString()); diff --git a/src/urlbar/listitem.cpp b/src/urlbar/listitem.cpp index 374d9cca..219c7072 100644 --- a/src/urlbar/listitem.cpp +++ b/src/urlbar/listitem.cpp @@ -42,6 +42,8 @@ // KDE Includes #include #include +#include +#include // Qt Includes #include @@ -329,6 +331,36 @@ PreviewLabel::PreviewLabel(const QString &url, int width, int height, QWidget *p } } +// --------------------------------------------------------------- + + +ImageLabel::ImageLabel(const QString &url, int width, int height, QWidget *parent) + : QLabel(parent), + m_width(width), + m_height(height) +{ + setFixedSize(width, height); + KIO::TransferJob *job = KIO::get(KUrl(url), KIO::NoReload, KIO::HideProgressInfo); + connect(job, SIGNAL(data(KIO::Job *, const QByteArray &)), + this, SLOT(slotData(KIO::Job*, const QByteArray&))); + connect(job, SIGNAL(result(KJob *)), + this, SLOT(slotResult(KJob *))); +} + + +void ImageLabel::slotData(KIO::Job *job, const QByteArray &data) +{ + Q_UNUSED(job); + m_data.append(data); +} + +void ImageLabel::slotResult(KJob *job) +{ + QPixmap pix; + if (!pix.loadFromData(m_data)) + kDebug() << "error while loading image: "; + setPixmap(pix.scaled(m_width, m_height, Qt::KeepAspectRatio, Qt::SmoothTransformation)); +} // --------------------------------------------------------------- @@ -491,7 +523,7 @@ VisualSuggestionListItem::VisualSuggestionListItem(const UrlSearchItem &item, co QLabel *previewLabelIcon = new QLabel(this); previewLabelIcon->setFixedSize(45, 33); - new PreviewLabel(item.image, 38, 29, previewLabelIcon); + new ImageLabel(item.image, 38, 29, previewLabelIcon); IconLabel* icon = new IconLabel(item.url, previewLabelIcon); icon->move(27, 16); hLayout->addWidget(previewLabelIcon); @@ -501,7 +533,7 @@ VisualSuggestionListItem::VisualSuggestionListItem(const UrlSearchItem &item, co QString query = SearchEngine::extractQuery(text); vLayout->addWidget(new TextLabel(item.title, query, this)); - vLayout->addWidget(new TextLabel("aaa aa", query, this)); + vLayout->addWidget(new TextLabel(""+item.description+"", query, this)); hLayout->addLayout(vLayout); diff --git a/src/urlbar/listitem.h b/src/urlbar/listitem.h index c662bc24..ab12917a 100644 --- a/src/urlbar/listitem.h +++ b/src/urlbar/listitem.h @@ -232,6 +232,26 @@ public: }; +// ------------------------------------------------------------------------- + +class ImageLabel : public QLabel +{ + Q_OBJECT + +public: + ImageLabel(const QString &url, int width, int height, QWidget *parent); + +private: + int m_width; + int m_height; + QByteArray m_data; + +private slots: + void slotData(KIO::Job* job, const QByteArray& data); + void slotResult(KJob* job); +}; + + // ------------------------------------------------------------------------- diff --git a/src/urlbar/urlresolver.cpp b/src/urlbar/urlresolver.cpp index a7c63969..7429adce 100644 --- a/src/urlbar/urlresolver.cpp +++ b/src/urlbar/urlresolver.cpp @@ -408,11 +408,15 @@ void UrlResolver::suggestionsReceived(const QString &text, const ResponseList &s Q_FOREACH(const Response &i, suggestions) { +<<<<<<< HEAD <<<<<<< HEAD UrlSearchItem gItem(UrlSearchItem::Suggestion, SearchEngine::buildQuery(UrlResolver::searchEngine(), s), s); ======= UrlSearchItem gItem(UrlSearchItem::Suggestion, i.title, i.title); >>>>>>> add an xml parser to the opensearch engine. +======= + UrlSearchItem gItem(UrlSearchItem::Suggestion, i.url, i.title, i.image, i.description); +>>>>>>> Images in visual suggestion are now displayed !! sugList << gItem; } emit suggestionsReady(sugList, _typedString); -- cgit v1.2.1