]> git.sur5r.net Git - minitube/commitdiff
Search spell suggestions
authorFlavio <flavio@odisseo.local>
Wed, 25 Jul 2012 14:25:12 +0000 (16:25 +0200)
committerFlavio <flavio@odisseo.local>
Wed, 25 Jul 2012 14:25:12 +0000 (16:25 +0200)
src/ListModel.cpp
src/ListModel.h
src/MainWindow.h
src/MediaView.cpp
src/sidebarwidget.cpp
src/sidebarwidget.h
src/youtubesearch.cpp
src/youtubesearch.h
src/youtubestreamreader.cpp
src/youtubestreamreader.h

index 468435f8908060b7cabf170de1825183d0792f23..851c15293d36f2cfdf5d9e619dbab0d4656e3038 100644 (file)
@@ -197,6 +197,10 @@ void ListModel::searchFinished(int total) {
 
     // update the message item
     emit dataChanged( createIndex( MAX_ITEMS, 0 ), createIndex( MAX_ITEMS, columnCount() - 1 ) );
+
+    if (!youtubeSearch->getSuggestions().isEmpty()) {
+        emit haveSuggestions(youtubeSearch->getSuggestions());
+    }
 }
 
 void ListModel::searchError(QString message) {
index 2752aab196229dd4b1c4e20d8d2e4adc84322d67..f9d424bec16aa4627420cdda9d9a0a8581fe9da1 100644 (file)
@@ -84,6 +84,7 @@ public slots:
 signals:
     void activeRowChanged(int);
     void needSelectionFor(QList<Video*>);
+    void haveSuggestions(const QStringList &suggestions);
 
 private:
     void searchMore(int max);
index 6710e2cd05ac7b80c0b45740a4a6b97d6d18996a..713422d43d56b69071607110e2d26de6700cba60 100644 (file)
@@ -32,6 +32,7 @@ public slots:
     void restore();
     void messageReceived(const QString &message);
     void quit();
+    void startToolbarSearch(QString query);
 
 protected:
     void changeEvent(QEvent *);
@@ -78,7 +79,6 @@ private slots:
     void downloadsFinished();
     void toggleDownloads(bool show);
 
-    void startToolbarSearch(QString query);
     void floatOnTop(bool);
     void showActionInStatusBar(QAction*, bool show);
     void showStopAfterThisInStatusBar(bool show);
index f4ef7275375b97d3f2e1a00d5b7127ecb464620c..8ad65ba547cea808a1ad2d915195d2f1cd3b5da9 100644 (file)
@@ -71,6 +71,10 @@ MediaView::MediaView(QWidget *parent) : QWidget(parent) {
     sidebar->setPlaylist(listView);
     connect(sidebar->getRefineSearchWidget(), SIGNAL(searchRefined()),
             SLOT(searchAgain()));
+    connect(listModel, SIGNAL(haveSuggestions(const QStringList &)),
+            sidebar, SLOT(showSuggestions(const QStringList &)));
+    connect(sidebar, SIGNAL(suggestionAccepted(QString)),
+            MainWindow::instance(), SLOT(startToolbarSearch(QString)));
     splitter->addWidget(sidebar);
 
     videoAreaWidget = new VideoAreaWidget(this);
@@ -172,6 +176,7 @@ void MediaView::search(SearchParams *searchParams) {
     }
 
     sidebar->getRefineSearchWidget()->setSearchParams(searchParams);
+    sidebar->hideSuggestions();
 }
 
 void MediaView::searchAgain() {
index 6717e3030122508954c9c9d5c75c22de9f59c9dc..ceae1503a2c06b0cdaf571fac3aa048129c4201e 100644 (file)
@@ -14,8 +14,25 @@ SidebarWidget::SidebarWidget(QWidget *parent) :
     playlist = 0;
 
     QBoxLayout *layout = new QVBoxLayout(this);
+    layout->setSpacing(1);
     layout->setMargin(0);
 
+    // hidden message widget
+    messageLabel = new QLabel(this);
+    messageLabel->setMargin(10);
+    messageLabel->setBackgroundRole(QPalette::ToolTipBase);
+    messageLabel->setForegroundRole(QPalette::ToolTipText);
+    messageLabel->setAutoFillBackground(true);
+    messageLabel->setWordWrap(true);
+    messageLabel->setTextFormat(Qt::RichText);
+    messageLabel->setTextInteractionFlags(
+                Qt::LinksAccessibleByKeyboard |
+                Qt::LinksAccessibleByMouse);
+    connect(messageLabel, SIGNAL(linkActivated(QString)),
+            SIGNAL(suggestionAccepted(QString)));
+    messageLabel->hide();
+    layout->addWidget(messageLabel);
+
     stackedWidget = new QStackedWidget(this);
     layout->addWidget(stackedWidget);
 
@@ -119,3 +136,29 @@ void SidebarWidget::showRefineSearchButton() {
                 height() - refineSearchButton->minimumHeight());
     refineSearchButton->show();
 }
+
+void SidebarWidget::showSuggestions(const QStringList &suggestions) {
+    QString message = tr("Did you mean: %1");
+
+    QString suggestionLinks;
+    foreach (QString suggestion, suggestions) {
+        suggestionLinks += "<a href='" + suggestion + "'>" + suggestion + "</a> ";
+    }
+    message = message.arg(suggestionLinks);
+
+    QString html =
+            "<html>"
+            "<style>"
+            "a { color: palette(text); text-decoration: none; font-weight: bold }"
+            "</style>"
+            "<body>%1</body>"
+            "</html>";
+    html = html.arg(message);
+    messageLabel->setText(html);
+    messageLabel->show();
+}
+
+void SidebarWidget::hideSuggestions() {
+    messageLabel->hide();
+    messageLabel->clear();
+}
index e8612998e1cc428506ac51b23c2f059f48068d7c..5edb56f81f5bd4911404c7f39388d11b2b07bde9 100644 (file)
@@ -15,11 +15,16 @@ public:
     void setPlaylist(QListView *playlist);
     void showPlaylist();
     RefineSearchWidget* getRefineSearchWidget() { return refineSearchWidget; }
+    void hideSuggestions();
 
 public slots:
     void showRefineSearchWidget();
     void hideRefineSearchWidget();
     void toggleRefineSearch(bool show = false);
+    void showSuggestions(const QStringList &suggestions);
+
+signals:
+    void suggestionAccepted(QString);
 
 protected:
     void resizeEvent(QResizeEvent *);
@@ -38,6 +43,7 @@ private:
     QListView *playlist;
     RefineSearchWidget *refineSearchWidget;
     QTimer *mouseTimer;
+    QLabel *messageLabel;
     
 };
 
index 67eee080de0c4747ad6d82dba11644d52ed2c5a2..3263525c020f7dc0bd494b310d1568d1a26b6922 100644 (file)
@@ -87,6 +87,7 @@ void YouTubeSearch::parseResults(QByteArray data) {
         qDebug() << "Error parsing XML";
     }
     videos = reader.getVideos();
+    suggestions = reader.getSuggestions();
 
     foreach (Video *video, videos) {
         // send it to the model
@@ -106,6 +107,10 @@ QList<Video*> YouTubeSearch::getResults() {
     return videos;
 }
 
+const QStringList & YouTubeSearch::getSuggestions() const {
+    return suggestions;
+}
+
 void YouTubeSearch::abort() {
     this->abortFlag = true;
 }
index 4a094e0f3d49e7a031882c40d4bdde0f79f6ede2..254ea02cb59df62cb26a7ddb1d955577a5a6fb26 100644 (file)
@@ -13,6 +13,7 @@ public:
     void search(SearchParams *searchParams, int max, int skip);
     void abort();
     QList<Video*> getResults();
+    const QStringList & getSuggestions() const;
     static QString videoIdFromUrl(QString url);
 
 signals:
@@ -27,6 +28,7 @@ private slots:
 private:
 
     QList<Video*> videos;
+    QStringList suggestions;
 
     bool abortFlag;
 
index c7e54efa414f030fe30b8b901ecd3e7d9fb96c59..9869da758632c0e7e640fb03acc68edc87900a71 100644 (file)
@@ -17,6 +17,10 @@ bool YouTubeStreamReader::read(QByteArray data) {
                     readNext();
                     if (isStartElement() && name() == "entry") {
                         readEntry();
+                    } else if (name() == "link"
+                        && attributes().value("rel").toString()
+                           == "http://schemas.google.com/g/2006#spellcorrection") {
+                        suggestions << attributes().value("title").toString();
                     }
                 }
             }
@@ -55,6 +59,7 @@ void YouTubeStreamReader::readEntry() {
                 webpage.remove("&feature=youtube_gdata");
                 // qDebug() << "Webpage: " << webpage;
                 video->setWebpage(QUrl(webpage));
+
             } else if (name() == "author") {
                 readNext();
                 if (name() == "name") {
@@ -110,3 +115,7 @@ void YouTubeStreamReader::readEntry() {
 QList<Video*> YouTubeStreamReader::getVideos() {
     return videos;
 }
+
+const QStringList & YouTubeStreamReader::getSuggestions() const {
+    return suggestions;
+}
index 4cd3894c7c7026d23315da7bac14bf0e56b6e97c..8c8c617dc988ea2051e086d6115b1fd5c666331c 100644 (file)
@@ -11,11 +11,13 @@ public:
     YouTubeStreamReader();
     bool read(QByteArray data);
     QList<Video*> getVideos();
+    const QStringList & getSuggestions() const;
 
 private:
     void readMediaGroup();
     void readEntry();
     QList<Video*> videos;
+    QStringList suggestions;
 };
 
 #endif // YOUTUBESTREAMREADER_H