]> git.sur5r.net Git - minitube/blobdiff - src/searchview.cpp
SearchLineEdit refactoring
[minitube] / src / searchview.cpp
index 77aa0dfc0c9d83d9b1714090ddbe598039d33a8e..ec5829511df93dad736b665f057f2358bf6a0f98 100644 (file)
@@ -24,7 +24,7 @@ $END_LICENSE */
 #include "searchparams.h"
 #include "ytsuggester.h"
 #include "channelsuggest.h"
-#ifdef APP_MAC
+#ifdef APP_MAC_SEARCHFIELD
 #include "searchlineedit_mac.h"
 #else
 #include "searchlineedit.h"
@@ -48,17 +48,12 @@ static const int PADDING = 30;
 
 SearchView::SearchView(QWidget *parent) : QWidget(parent) {
 
-    QFont biggerFont = FontUtils::big();
-    QFont smallerFont = FontUtils::smallBold();
-
 #if defined(APP_MAC) | defined(APP_WIN)
     // speedup painting since we'll paint the whole background
     // by ourselves anyway in paintEvent()
     setAttribute(Qt::WA_OpaquePaintEvent);
 #endif
 
-    setAutoFillBackground(true);
-
     QBoxLayout *mainLayout = new QVBoxLayout(this);
     mainLayout->setMargin(PADDING);
     mainLayout->setSpacing(0);
@@ -87,19 +82,18 @@ SearchView::SearchView(QWidget *parent) : QWidget(parent) {
             new QLabel("<h1 style='font-weight:100'>" +
                        tr("Welcome to <a href='%1'>%2</a>,")
                        // .replace("<a ", "<a style='color:palette(text)'")
-                       .replace("<a ", "<a style='text-decoration:none; color:palette(text);font-weight:"
-                            #if defined(APP_UBUNTU) || defined(APP_WIN)
-                                "normal"
-                            #else
-                                "bold"
-                            #endif
-                                "' ")
+                       .replace("<a ", "<a style='text-decoration:none; color:palette(text);font-weight:normal' ")
                        .arg(Constants::WEBSITE, Constants::NAME)
                        + "</h1>", this);
     welcomeLabel->setOpenExternalLinks(true);
-#ifdef APP_WIN
+    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.setHintingPreference(QFont::PreferNoHinting);
     f.setFamily("Segoe UI Light");
     welcomeLabel->setFont(f);
 #endif
@@ -110,20 +104,28 @@ SearchView::SearchView(QWidget *parent) : QWidget(parent) {
     QBoxLayout *tipLayout = new QHBoxLayout();
     tipLayout->setSpacing(10);
 
+    const QFont &biggerFont = FontUtils::big();
+
     //: "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);
 
@@ -132,18 +134,23 @@ SearchView::SearchView(QWidget *parent) : QWidget(parent) {
     QHBoxLayout *searchLayout = new QHBoxLayout();
     searchLayout->setAlignment(Qt::AlignVCenter);
 
+#ifdef APP_MAC_SEARCHFIELD
+    queryEdit = new SearchLineEditMac(this);
+#else
     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&)));
+    queryEdit->toWidget()->setFont(biggerFont);
+#endif
+
+    qDebug() << "queryEdit->toWidget()" << (queryEdit->toWidget() == 0) << queryEdit->toWidget();
+    connect(queryEdit->toWidget(), SIGNAL(search(const QString&)), SLOT(watch(const QString&)));
+    connect(queryEdit->toWidget(), SIGNAL(textEdited(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);
@@ -164,11 +171,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);
@@ -177,11 +183,10 @@ 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);
@@ -197,14 +202,26 @@ SearchView::SearchView(QWidget *parent) : QWidget(parent) {
 }
 
 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;
@@ -213,13 +230,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://")) {
@@ -240,7 +254,7 @@ void SearchView::updateRecentKeywords() {
                                        + 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);
@@ -253,6 +267,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;
@@ -261,13 +280,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('|');
@@ -280,7 +296,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);
@@ -300,23 +316,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.simplified();
-        searchParams->setAuthor(query);
+        q.remove(' ');
+        searchParams->setChannelId(q);
         searchParams->setSortBy(SearchParams::SortByNewest);
     }
 
@@ -324,44 +339,41 @@ 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);
@@ -372,7 +384,7 @@ void SearchView::paintEvent(QPaintEvent *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();
     }
@@ -386,7 +398,6 @@ void SearchView::paintEvent(QPaintEvent *event) {
     QPainter p(this);
     style()->drawPrimitive(QStyle::PE_Widget, &o, &p, this);
 #endif
-    PainterUtils::topShadow(this);
 }
 
 void SearchView::searchTypeChanged(int index) {
@@ -396,5 +407,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);
 }