From acda6aee34b1aa7cec6f9aa04e73f4b0c3e1e5ee Mon Sep 17 00:00:00 2001 From: Flavio Date: Wed, 26 Jan 2011 00:34:30 +0100 Subject: [PATCH] Nicer selection style on Mac & Win --- src/Suggester.h | 18 ++++++++ src/{googlesuggest.cpp => autocomplete.cpp} | 0 src/{googlesuggest.h => autocomplete.h} | 0 src/channelsuggest.cpp | 46 +++++++++++++++++++++ src/channelsuggest.h | 24 +++++++++++ src/playlist/PrettyItemDelegate.cpp | 20 +++++++++ src/playlist/PrettyItemDelegate.h | 1 + src/suggester.h | 18 ++++++++ src/youtubesuggest.cpp | 42 +++++++++++++++++++ src/youtubesuggest.h | 24 +++++++++++ 10 files changed, 193 insertions(+) create mode 100644 src/Suggester.h rename src/{googlesuggest.cpp => autocomplete.cpp} (100%) rename src/{googlesuggest.h => autocomplete.h} (100%) create mode 100644 src/channelsuggest.cpp create mode 100644 src/channelsuggest.h create mode 100644 src/suggester.h create mode 100644 src/youtubesuggest.cpp create mode 100644 src/youtubesuggest.h diff --git a/src/Suggester.h b/src/Suggester.h new file mode 100644 index 0000000..dc30bd8 --- /dev/null +++ b/src/Suggester.h @@ -0,0 +1,18 @@ +#ifndef SUGGESTER_H +#define SUGGESTER_H + +#include + +class Suggester : public QObject { + + Q_OBJECT + +public: + virtual void suggest(QString query) = 0; + +signals: + void ready(QStringList); + +}; + +#endif // SUGGESTER_H diff --git a/src/googlesuggest.cpp b/src/autocomplete.cpp similarity index 100% rename from src/googlesuggest.cpp rename to src/autocomplete.cpp diff --git a/src/googlesuggest.h b/src/autocomplete.h similarity index 100% rename from src/googlesuggest.h rename to src/autocomplete.h diff --git a/src/channelsuggest.cpp b/src/channelsuggest.cpp new file mode 100644 index 0000000..c36afb7 --- /dev/null +++ b/src/channelsuggest.cpp @@ -0,0 +1,46 @@ +#include "channelsuggest.h" +#include "networkaccess.h" + +namespace The { + NetworkAccess* http(); +} + +ChannelSuggest::ChannelSuggest(QObject *parent) : Suggester() { + +} + +void ChannelSuggest::suggest(QString query) { + + /* // TODO how to localize results? + QString locale = QLocale::system().name().replace("_", "-"); + // case for system locales such as "C" + if (locale.length() < 2) { + locale = "en-US"; + }*/ + + QUrl url("http://www.youtube.com/results?search_type=search_users"); + url.addQueryItem("search_query", query); + // url.addQueryItem("hl", "it-IT"); + + QObject *reply = The::http()->get(url); + connect(reply, SIGNAL(data(QByteArray)), SLOT(handleNetworkData(QByteArray))); +} + +void ChannelSuggest::handleNetworkData(QByteArray data) { + QStringList choices; + QString html = QString::fromUtf8(data); + QRegExp re("/user/([a-zA-Z0-9]+)"); + + int pos = 0; + while ((pos = re.indexIn(html, pos)) != -1) { + // qDebug() << re.cap(0) << re.cap(1); + QString choice = re.cap(1); + if (!choices.contains(choice)) { + choices << choice; + if (choices.size() == 10) break; + } + pos += re.matchedLength(); + } + + emit ready(choices); +} diff --git a/src/channelsuggest.h b/src/channelsuggest.h new file mode 100644 index 0000000..85cbef9 --- /dev/null +++ b/src/channelsuggest.h @@ -0,0 +1,24 @@ +#ifndef CHANNELSUGGEST_H +#define CHANNELSUGGEST_H + +#include + +#include "suggester.h" + +class ChannelSuggest : public Suggester { + + Q_OBJECT + +public: + ChannelSuggest(QObject *parent = 0); + void suggest(QString query); + +signals: + void ready(QStringList); + +private slots: + void handleNetworkData(QByteArray response); + +}; + +#endif // CHANNELSUGGEST_H diff --git a/src/playlist/PrettyItemDelegate.cpp b/src/playlist/PrettyItemDelegate.cpp index 4f64c99..3454572 100644 --- a/src/playlist/PrettyItemDelegate.cpp +++ b/src/playlist/PrettyItemDelegate.cpp @@ -85,6 +85,12 @@ void PrettyItemDelegate::paintBody( QPainter* painter, paintActiveOverlay(painter, line.x(), line.y(), line.width(), line.height()); } +#if defined(APP_MAC) | defined(APP_WIN) + if (isSelected) { + paintSelectedOverlay(painter, line.x(), line.y(), line.width(), line.height()); + } +#endif + // get the video metadata const VideoPointer videoPointer = index.data( VideoRole ).value(); const Video *video = videoPointer.data(); @@ -211,6 +217,20 @@ void PrettyItemDelegate::paintActiveOverlay( QPainter *painter, qreal x, qreal y painter->restore(); } +void PrettyItemDelegate::paintSelectedOverlay( QPainter *painter, qreal x, qreal y, qreal w, qreal h ) const { + QColor color1 = QColor::fromRgb(0x69, 0xa6, 0xd9); + QColor color2 = QColor::fromRgb(0x14, 0x6b, 0xd4); + QRect rect((int) x, (int) y, (int) w, (int) h); + painter->save(); + painter->setPen(Qt::NoPen); + QLinearGradient linearGradient(0, 0, 0, rect.height()); + linearGradient.setColorAt(0.0, color1); + linearGradient.setColorAt(1.0, color2); + painter->setBrush(linearGradient); + painter->drawRect(rect); + painter->restore(); +} + void PrettyItemDelegate::paintPlayIcon(QPainter *painter) const { painter->save(); painter->setOpacity(.5); diff --git a/src/playlist/PrettyItemDelegate.h b/src/playlist/PrettyItemDelegate.h index 4260558..708256f 100644 --- a/src/playlist/PrettyItemDelegate.h +++ b/src/playlist/PrettyItemDelegate.h @@ -27,6 +27,7 @@ private: const QModelIndex& index ) const; // active track painting + void paintSelectedOverlay( QPainter *painter, qreal x, qreal y, qreal w, qreal h ) const; void paintActiveOverlay( QPainter *painter, qreal x, qreal y, qreal w, qreal h ) const; void paintPlayIcon(QPainter *painter) const; diff --git a/src/suggester.h b/src/suggester.h new file mode 100644 index 0000000..dc30bd8 --- /dev/null +++ b/src/suggester.h @@ -0,0 +1,18 @@ +#ifndef SUGGESTER_H +#define SUGGESTER_H + +#include + +class Suggester : public QObject { + + Q_OBJECT + +public: + virtual void suggest(QString query) = 0; + +signals: + void ready(QStringList); + +}; + +#endif // SUGGESTER_H diff --git a/src/youtubesuggest.cpp b/src/youtubesuggest.cpp new file mode 100644 index 0000000..c1965ca --- /dev/null +++ b/src/youtubesuggest.cpp @@ -0,0 +1,42 @@ +#include "youtubesuggest.h" +#include +#include "networkaccess.h" + +#define GSUGGEST_URL "http://suggestqueries.google.com/complete/search?ds=yt&output=toolbar&hl=%1&q=%2" + +namespace The { + NetworkAccess* http(); +} + +YouTubeSuggest::YouTubeSuggest(QObject *parent) : Suggester() { + +} + +void YouTubeSuggest::suggest(QString query) { + QString locale = QLocale::system().name().replace("_", "-"); + // case for system locales such as "C" + if (locale.length() < 2) { + locale = "en-US"; + } + + QString url = QString(GSUGGEST_URL).arg(locale, query); + + QObject *reply = The::http()->get(url); + connect(reply, SIGNAL(data(QByteArray)), SLOT(handleNetworkData(QByteArray))); +} + +void YouTubeSuggest::handleNetworkData(QByteArray response) { + QStringList choices; + + QXmlStreamReader xml(response); + while (!xml.atEnd()) { + xml.readNext(); + if (xml.tokenType() == QXmlStreamReader::StartElement) { + if (xml.name() == "suggestion") { + QStringRef str = xml.attributes().value("data"); + choices << str.toString(); + } + } + } + emit ready(choices); +} diff --git a/src/youtubesuggest.h b/src/youtubesuggest.h new file mode 100644 index 0000000..2d2221e --- /dev/null +++ b/src/youtubesuggest.h @@ -0,0 +1,24 @@ +#ifndef YOUTUBESUGGEST_H +#define YOUTUBESUGGEST_H + +#include + +#include "suggester.h" + +class YouTubeSuggest : public Suggester { + + Q_OBJECT + +public: + YouTubeSuggest(QObject *parent = 0); + void suggest(QString query); + +signals: + void ready(QStringList); + +private slots: + void handleNetworkData(QByteArray response); + +}; + +#endif // YOUTUBESUGGEST_H -- 2.39.5