]> git.sur5r.net Git - minitube/blobdiff - src/searchview.cpp
Fixed search widget
[minitube] / src / searchview.cpp
index f49abf472ca1b64f9fb2d851d4a323233b9ea8ca..5b9add75cdcdcc5d1e96f6f68fc3ba63a1761a2e 100644 (file)
@@ -1,21 +1,43 @@
+/* $BEGIN_LICENSE
+
+This file is part of Minitube.
+Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
+
+Minitube is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+Minitube 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 General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with Minitube.  If not, see <http://www.gnu.org/licenses/>.
+
+$END_LICENSE */
+
 #include "searchview.h"
 #include "constants.h"
 #include "fontutils.h"
 #include "searchparams.h"
 #include "ytsuggester.h"
 #include "channelsuggest.h"
-#ifdef APP_MAC
+#ifdef APP_MAC_SEARCHFIELD
 #include "searchlineedit_mac.h"
 #else
 #include "searchlineedit.h"
 #endif
-#ifndef Q_WS_X11
+#ifdef APP_EXTRA
 #include "extra.h"
 #endif
 #ifdef APP_ACTIVATION
 #include "activation.h"
 #endif
 #include "mainwindow.h"
+#include "painterutils.h"
+#include "iconutils.h"
 
 namespace The {
 QHash<QString, QAction*>* globalActions();
@@ -25,10 +47,7 @@ static const QString recentKeywordsKey = "recentKeywords";
 static const QString recentChannelsKey = "recentChannels";
 static const int PADDING = 30;
 
-SearchView::SearchView(QWidget *parent) : QWidget(parent) {
-
-    QFont biggerFont = FontUtils::big();
-    QFont smallerFont = FontUtils::smallBold();
+SearchView::SearchView(QWidget *parent) : View(parent) {
 
 #if defined(APP_MAC) | defined(APP_WIN)
     // speedup painting since we'll paint the whole background
@@ -36,7 +55,7 @@ SearchView::SearchView(QWidget *parent) : QWidget(parent) {
     setAttribute(Qt::WA_OpaquePaintEvent);
 #endif
 
-    QBoxLayout *mainLayout = new QVBoxLayout();
+    QBoxLayout *mainLayout = new QVBoxLayout(this);
     mainLayout->setMargin(PADDING);
     mainLayout->setSpacing(0);
 
@@ -46,14 +65,13 @@ SearchView::SearchView(QWidget *parent) : QWidget(parent) {
     mainLayout->addWidget(message);
 
     mainLayout->addStretch();
-    mainLayout->addSpacing(PADDING);
 
     QBoxLayout *hLayout = new QHBoxLayout();
     hLayout->setAlignment(Qt::AlignCenter);
     mainLayout->addLayout(hLayout);
 
     QLabel *logo = new QLabel(this);
-    logo->setPixmap(QPixmap(":/images/app.png"));
+    logo->setPixmap(IconUtils::pixmap(":/images/app.png"));
     hLayout->addWidget(logo, 0, Qt::AlignTop);
     hLayout->addSpacing(PADDING);
 
@@ -62,13 +80,23 @@ SearchView::SearchView(QWidget *parent) : QWidget(parent) {
     hLayout->addLayout(layout);
 
     QLabel *welcomeLabel =
-            new QLabel("<h1 style='font-weight:normal'>" +
+            new QLabel("<h1 style='font-weight:100'>" +
                        tr("Welcome to <a href='%1'>%2</a>,")
-                       // .replace("<a ", "<a style='color:palette(text)'")
-                       .replace("<a href", "<a style='text-decoration:none; color:palette(text); font-weight:bold' href")
+                       .replace("<a ", "<a style='text-decoration:none; color:palette(text);font-weight:normal' ")
                        .arg(Constants::WEBSITE, Constants::NAME)
                        + "</h1>", this);
     welcomeLabel->setOpenExternalLinks(true);
+    welcomeLabel->setProperty("heading", true);
+#ifdef APP_MAC
+    QFont f = welcomeLabel->font();
+    f.setFamily("Helvetica Neue");
+    f.setStyleName("Thin");
+    welcomeLabel->setFont(f);
+#elif APP_WIN
+    QFont f = welcomeLabel->font();
+    f.setFamily("Segoe UI Light");
+    welcomeLabel->setFont(f);
+#endif
     layout->addWidget(welcomeLabel);
 
     layout->addSpacing(PADDING / 2);
@@ -76,20 +104,30 @@ SearchView::SearchView(QWidget *parent) : QWidget(parent) {
     QBoxLayout *tipLayout = new QHBoxLayout();
     tipLayout->setSpacing(10);
 
+#ifndef APP_MAC
+    const QFont &biggerFont = FontUtils::big();
+#endif
+
     //: "Enter", as in "type". The whole phrase says: "Enter a keyword to start watching videos"
     QLabel *tipLabel = new QLabel(tr("Enter"), this);
+#ifndef APP_MAC
     tipLabel->setFont(biggerFont);
+#endif
     tipLayout->addWidget(tipLabel);
 
     typeCombo = new QComboBox(this);
     typeCombo->addItem(tr("a keyword"));
     typeCombo->addItem(tr("a channel"));
+#ifndef APP_MAC
     typeCombo->setFont(biggerFont);
+#endif
     connect(typeCombo, SIGNAL(currentIndexChanged(int)), SLOT(searchTypeChanged(int)));
     tipLayout->addWidget(typeCombo);
 
     tipLabel = new QLabel(tr("to start watching videos."), this);
+#ifndef APP_MAC
     tipLabel->setFont(biggerFont);
+#endif
     tipLayout->addWidget(tipLabel);
     layout->addLayout(tipLayout);
 
@@ -98,21 +136,30 @@ SearchView::SearchView(QWidget *parent) : QWidget(parent) {
     QHBoxLayout *searchLayout = new QHBoxLayout();
     searchLayout->setAlignment(Qt::AlignVCenter);
 
-    queryEdit = new SearchLineEdit(this);
-    queryEdit->setFont(biggerFont);
-    queryEdit->setMinimumWidth(queryEdit->fontInfo().pixelSize()*15);
-    connect(queryEdit, SIGNAL(search(const QString&)), SLOT(watch(const QString&)));
-    connect(queryEdit, SIGNAL(textChanged(const QString &)), SLOT(textChanged(const QString &)));
-    connect(queryEdit, SIGNAL(suggestionAccepted(const QString&)), SLOT(watch(const QString&)));
+#ifdef APP_MAC_SEARCHFIELD
+    queryEdit = new SearchLineEditMac(this);
+#else
+    SearchLineEdit *sle = new SearchLineEdit(this);
+    sle->setFont(biggerFont);
+    queryEdit = sle;
+#endif
+
+    qDebug() << "queryEdit->toWidget()" << (queryEdit->toWidget() == 0) << queryEdit->toWidget();
+    connect(queryEdit->toWidget(), SIGNAL(search(const QString&)), SLOT(watch(const QString&)));
+    connect(queryEdit->toWidget(), SIGNAL(textChanged(const QString &)), SLOT(textChanged(const QString &)));
+    connect(queryEdit->toWidget(), SIGNAL(suggestionAccepted(Suggestion*)), SLOT(suggestionAccepted(Suggestion*)));
 
     youtubeSuggest = new YTSuggester(this);
     channelSuggest = new ChannelSuggest(this);
     searchTypeChanged(0);
 
-    searchLayout->addWidget(queryEdit);
+    searchLayout->addWidget(queryEdit->toWidget());
     searchLayout->addSpacing(10);
 
     watchButton = new QPushButton(tr("Watch"), this);
+#ifndef APP_MAC
+    watchButton->setFont(biggerFont);
+#endif
     watchButton->setDefault(true);
     watchButton->setEnabled(false);
     watchButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
@@ -130,11 +177,10 @@ SearchView::SearchView(QWidget *parent) : QWidget(parent) {
     recentKeywordsLayout = new QVBoxLayout();
     recentKeywordsLayout->setSpacing(5);
     recentKeywordsLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
-    recentKeywordsLabel = new QLabel(tr("Recent keywords").toUpper(), this);
+    recentKeywordsLabel = new QLabel(tr("Recent keywords"), this);
     recentKeywordsLabel->setProperty("recentHeader", true);
     recentKeywordsLabel->setForegroundRole(QPalette::Dark);
     recentKeywordsLabel->hide();
-    recentKeywordsLabel->setFont(smallerFont);
     recentKeywordsLayout->addWidget(recentKeywordsLabel);
 
     otherLayout->addLayout(recentKeywordsLayout);
@@ -143,38 +189,45 @@ SearchView::SearchView(QWidget *parent) : QWidget(parent) {
     recentChannelsLayout = new QVBoxLayout();
     recentChannelsLayout->setSpacing(5);
     recentChannelsLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
-    recentChannelsLabel = new QLabel(tr("Recent channels").toUpper(), this);
+    recentChannelsLabel = new QLabel(tr("Recent channels"), this);
     recentChannelsLabel->setProperty("recentHeader", true);
     recentChannelsLabel->setForegroundRole(QPalette::Dark);
     recentChannelsLabel->hide();
-    recentChannelsLabel->setFont(smallerFont);
     recentChannelsLayout->addWidget(recentChannelsLabel);
 
     otherLayout->addLayout(recentChannelsLayout);
 
     layout->addLayout(otherLayout);
 
-    mainLayout->addSpacing(PADDING);
     mainLayout->addStretch();
 
 #ifdef APP_ACTIVATION
     if (!Activation::instance().isActivated())
         mainLayout->addWidget(Extra::buyButton(tr("Get the full version")), 0, Qt::AlignRight);
 #endif
-
-    setLayout(mainLayout);
-
 }
 
 void SearchView::appear() {
+    MainWindow::instance()->showActionInStatusBar(The::globalActions()->value("definition"), true);
+
     updateRecentKeywords();
     updateRecentChannels();
     queryEdit->selectAll();
     queryEdit->enableSuggest();
-    QTimer::singleShot(0, queryEdit, SLOT(setFocus()));
+
+    if (!queryEdit->toWidget()->hasFocus()) queryEdit->toWidget()->setFocus();
+}
+
+void SearchView::disappear() {
+    MainWindow::instance()->showActionInStatusBar(The::globalActions()->value("definition"), false);
 }
 
 void SearchView::updateRecentKeywords() {
+    // load
+    QSettings settings;
+    QStringList keywords = settings.value(recentKeywordsKey).toStringList();
+    if (keywords == recentKeywords) return;
+    recentKeywords = keywords;
 
     // cleanup
     QLayoutItem *item;
@@ -183,13 +236,10 @@ void SearchView::updateRecentKeywords() {
         delete item;
     }
 
-    // load
-    QSettings settings;
-    QStringList keywords = settings.value(recentKeywordsKey).toStringList();
     recentKeywordsLabel->setVisible(!keywords.isEmpty());
     The::globalActions()->value("clearRecentKeywords")->setEnabled(!keywords.isEmpty());
 
-    foreach (QString keyword, keywords) {
+    foreach (const QString &keyword, keywords) {
         QString link = keyword;
         QString display = keyword;
         if (keyword.startsWith("http://") || keyword.startsWith("https://")) {
@@ -210,8 +260,8 @@ void SearchView::updateRecentKeywords() {
                                        + display + "</a>", this);
         itemLabel->setAttribute(Qt::WA_DeleteOnClose);
         itemLabel->setProperty("recentItem", true);
-        itemLabel->setMaximumWidth(queryEdit->width() + watchButton->width());
-        // itemLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
+        itemLabel->setMaximumWidth(queryEdit->toWidget()->width() + watchButton->width());
+        itemLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
         // Make links navigable with the keyboard too
         itemLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse);
         if (needStatusTip)
@@ -223,6 +273,11 @@ void SearchView::updateRecentKeywords() {
 }
 
 void SearchView::updateRecentChannels() {
+    // load
+    QSettings settings;
+    QStringList keywords = settings.value(recentChannelsKey).toStringList();
+    if (keywords == recentChannels) return;
+    recentChannels = keywords;
 
     // cleanup
     QLayoutItem *item;
@@ -231,13 +286,10 @@ void SearchView::updateRecentChannels() {
         delete item;
     }
 
-    // load
-    QSettings settings;
-    QStringList keywords = settings.value(recentChannelsKey).toStringList();
     recentChannelsLabel->setVisible(!keywords.isEmpty());
     // TODO The::globalActions()->value("clearRecentKeywords")->setEnabled(!keywords.isEmpty());
 
-    foreach (QString keyword, keywords) {
+    foreach (const QString &keyword, keywords) {
         QString link = keyword;
         QString display = keyword;
         int separator = keyword.indexOf('|');
@@ -250,7 +302,7 @@ void SearchView::updateRecentChannels() {
                                        + display + "</a>", this);
         itemLabel->setAttribute(Qt::WA_DeleteOnClose);
         itemLabel->setProperty("recentItem", true);
-        itemLabel->setMaximumWidth(queryEdit->width() + watchButton->width());
+        itemLabel->setMaximumWidth(queryEdit->toWidget()->width() + watchButton->width());
         // itemLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
         // Make links navigable with the keyboard too
         itemLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse);
@@ -270,23 +322,22 @@ void SearchView::textChanged(const QString &text) {
     watchButton->setEnabled(!text.simplified().isEmpty());
 }
 
-void SearchView::watch(QString query) {
-
-    query = query.simplified();
+void SearchView::watch(const QString &query) {
+    QString q = query.simplified();
 
     // check for empty query
-    if (query.length() == 0) {
-        queryEdit->setFocus(Qt::OtherFocusReason);
+    if (q.length() == 0) {
+        queryEdit->toWidget()->setFocus(Qt::OtherFocusReason);
         return;
     }
 
     SearchParams *searchParams = new SearchParams();
     if (typeCombo->currentIndex() == 0)
-        searchParams->setKeywords(query);
+        searchParams->setKeywords(q);
     else {
         // remove spaces from channel name
-        query = query.replace(" ", "");
-        searchParams->setAuthor(query);
+        q.remove(' ');
+        searchParams->setChannelId(q);
         searchParams->setSortBy(SearchParams::SortByNewest);
     }
 
@@ -294,74 +345,65 @@ void SearchView::watch(QString query) {
     emit search(searchParams);
 }
 
-void SearchView::watchChannel(QString channel) {
-
-    channel = channel.simplified();
-
-    // check for empty query
-    if (channel.length() == 0) {
-        queryEdit->setFocus(Qt::OtherFocusReason);
+void SearchView::watchChannel(const QString &channelId) {
+    if (channelId.length() == 0) {
+        queryEdit->toWidget()->setFocus(Qt::OtherFocusReason);
         return;
     }
 
-    // remove spaces from channel name
-    channel = channel.remove(" ");
+    QString id = channelId;
+
+    // Fix old settings
+    if (!id.startsWith("UC")) id = "UC" + id;
 
     SearchParams *searchParams = new SearchParams();
-    searchParams->setAuthor(channel);
+    searchParams->setChannelId(id);
     searchParams->setSortBy(SearchParams::SortByNewest);
 
     // go!
     emit search(searchParams);
 }
 
-void SearchView::watchKeywords(QString query) {
-
-    query = query.simplified();
+void SearchView::watchKeywords(const QString &query) {
+    QString q = query.simplified();
 
     // check for empty query
     if (query.length() == 0) {
-        queryEdit->setFocus(Qt::OtherFocusReason);
+        queryEdit->toWidget()->setFocus(Qt::OtherFocusReason);
         return;
     }
 
     if (typeCombo->currentIndex() == 0) {
-        queryEdit->setText(query);
+        queryEdit->setText(q);
         watchButton->setEnabled(true);
     }
 
     SearchParams *searchParams = new SearchParams();
-    searchParams->setKeywords(query);
+    searchParams->setKeywords(q);
 
     // go!
     emit search(searchParams);
 }
 
-void SearchView::paintEvent(QPaintEvent * /*event*/) {
-    QPainter painter(this);
-
+void SearchView::paintEvent(QPaintEvent *event) {
+    QWidget::paintEvent(event);
 #if defined(APP_MAC) | defined(APP_WIN)
     QBrush brush;
     if (window()->isActiveWindow()) {
-        brush = QBrush(QColor(0xdd, 0xe4, 0xeb));
+        brush = Qt::white;
     } else {
         brush = palette().window();
     }
+    QPainter painter(this);
     painter.fillRect(0, 0, width(), height(), brush);
+    painter.end();
+#endif
+#ifdef APP_UBUNTU
+    QStyleOption o;
+    o.initFrom(this);
+    QPainter p(this);
+    style()->drawPrimitive(QStyle::PE_Widget, &o, &p, this);
 #endif
-
-    static QLinearGradient shadow;
-    static const int shadowHeight = 10;
-    if (shadow.stops().count() == 2) {
-        shadow.setFinalStop(0, shadowHeight);
-        const qreal initialOpacity = 96;
-        for (qreal i = 0; i <= 1; i += 1.0/shadowHeight) {
-            qreal opacity = qPow(initialOpacity, (1.0 - i)) - 1;
-            shadow.setColorAt(i, QColor(0x00, 0x00, 0x00, opacity));
-        }
-    }
-    QRect r = rect();
-    painter.fillRect(r.x(), r.y(), r.width(), shadowHeight, QBrush(shadow));
 }
 
 void SearchView::searchTypeChanged(int index) {
@@ -371,5 +413,11 @@ void SearchView::searchTypeChanged(int index) {
         queryEdit->setSuggester(channelSuggest);
     }
     queryEdit->selectAll();
-    queryEdit->setFocus();
+    queryEdit->toWidget()->setFocus();
+}
+
+void SearchView::suggestionAccepted(Suggestion *suggestion) {
+    if (suggestion->type == QLatin1String("channel")) {
+        watchChannel(suggestion->userData);
+    } else watch(suggestion->value);
 }