X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2FSearchView.cpp;h=11f86d07fa848babebfd9aab1ee8f1f8a2567936;hb=9f91bb99d2296bff925f80e38ef2795fdabb86af;hp=76f7da4abe9980b65789db1f3b7c1b6bda2e717f;hpb=033b0ff93a80717dc60ee0f597ee7d6a18c6fb49;p=minitube diff --git a/src/SearchView.cpp b/src/SearchView.cpp index 76f7da4..11f86d0 100644 --- a/src/SearchView.cpp +++ b/src/SearchView.cpp @@ -1,12 +1,16 @@ #include "SearchView.h" #include "constants.h" #include "fontutils.h" +#include "searchparams.h" +#include "youtubesuggest.h" +#include "channelsuggest.h" namespace The { QMap* globalActions(); } static const QString recentKeywordsKey = "recentKeywords"; +static const QString recentChannelsKey = "recentChannels"; static const int PADDING = 30; SearchView::SearchView(QWidget *parent) : QWidget(parent) { @@ -14,7 +18,7 @@ SearchView::SearchView(QWidget *parent) : QWidget(parent) { QFont biggerFont = FontUtils::big(); QFont smallerFont = FontUtils::smallBold(); -#ifdef APP_MAC +#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); @@ -29,6 +33,26 @@ SearchView::SearchView(QWidget *parent) : QWidget(parent) { message->hide(); mainLayout->addWidget(message); +#ifdef APP_DEMO + QLabel *buy = new QLabel(this); + buy->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred); + buy->setText(QString("%2").arg( + QString(Constants::WEBSITE) + "#download", + tr("Get the full version").toUpper() + )); + buy->setOpenExternalLinks(true); + buy->setMargin(7); + buy->setAlignment(Qt::AlignRight); + buy->setStyleSheet("QLabel {" + "background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #CCD6E0, stop: 1 #ADBCCC);" + "border-bottom-left-radius: 8px;" + "border-bottom-right-radius: 8px;" + "font-size: 10px;" + "margin-right: 50px;" + "}"); + mainLayout->addWidget(buy, 0, Qt::AlignRight); +#endif + mainLayout->addStretch(); mainLayout->addSpacing(PADDING); @@ -57,9 +81,25 @@ SearchView::SearchView(QWidget *parent) : QWidget(parent) { layout->addSpacing(PADDING / 2); - QLabel *tipLabel = new QLabel(tr("Enter a keyword to start watching videos."), this); + QBoxLayout *tipLayout = new QHBoxLayout(); + tipLayout->setSpacing(10); + + //: "Enter", as in "type". The whole frase says: "Enter a keyword to start watching videos" + QLabel *tipLabel = new QLabel(tr("Enter"), this); + tipLabel->setFont(biggerFont); + tipLayout->addWidget(tipLabel); + + typeCombo = new QComboBox(this); + typeCombo->addItem(tr("a keyword")); + typeCombo->addItem(tr("a channel")); + typeCombo->setFont(biggerFont); + connect(typeCombo, SIGNAL(currentIndexChanged(int)), SLOT(searchTypeChanged(int))); + tipLayout->addWidget(typeCombo); + + tipLabel = new QLabel(tr("to start watching videos."), this); tipLabel->setFont(biggerFont); - layout->addWidget(tipLabel); + tipLayout->addWidget(tipLabel); + layout->addLayout(tipLayout); layout->addSpacing(PADDING / 2); @@ -73,8 +113,12 @@ SearchView::SearchView(QWidget *parent) : QWidget(parent) { queryEdit->setFocus(Qt::OtherFocusReason); connect(queryEdit, SIGNAL(search(const QString&)), this, SLOT(watch(const QString&))); connect(queryEdit, SIGNAL(textChanged(const QString &)), this, SLOT(textChanged(const QString &))); - searchLayout->addWidget(queryEdit); + youtubeSuggest = new YouTubeSuggest(this); + channelSuggest = new ChannelSuggest(this); + searchTypeChanged(0); + + searchLayout->addWidget(queryEdit); searchLayout->addSpacing(10); watchButton = new QPushButton(tr("Watch"), this); @@ -90,12 +134,13 @@ SearchView::SearchView(QWidget *parent) : QWidget(parent) { QHBoxLayout *otherLayout = new QHBoxLayout(); otherLayout->setMargin(0); + otherLayout->setSpacing(10); recentKeywordsLayout = new QVBoxLayout(); recentKeywordsLayout->setSpacing(5); - recentKeywordsLayout->setAlignment(Qt::AlignVCenter | Qt::AlignLeft); + recentKeywordsLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft); recentKeywordsLabel = new QLabel(tr("Recent keywords").toUpper(), this); -#ifdef APP_MAC +#if defined(APP_MAC) | defined(APP_WIN) QPalette palette = recentKeywordsLabel->palette(); palette.setColor(QPalette::WindowText, QColor(0x65, 0x71, 0x80)); recentKeywordsLabel->setPalette(palette); @@ -108,6 +153,24 @@ SearchView::SearchView(QWidget *parent) : QWidget(parent) { otherLayout->addLayout(recentKeywordsLayout); + // recent channels + recentChannelsLayout = new QVBoxLayout(); + recentChannelsLayout->setSpacing(5); + recentChannelsLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft); + recentChannelsLabel = new QLabel(tr("Recent channels").toUpper(), this); +#if defined(APP_MAC) | defined(APP_WIN) + palette = recentChannelsLabel->palette(); + palette.setColor(QPalette::WindowText, QColor(0x65, 0x71, 0x80)); + recentChannelsLabel->setPalette(palette); +#else + recentChannelsLabel->setForegroundRole(QPalette::Dark); +#endif + recentChannelsLabel->hide(); + recentChannelsLabel->setFont(smallerFont); + recentChannelsLayout->addWidget(recentChannelsLabel); + + otherLayout->addLayout(recentChannelsLayout); + layout->addLayout(otherLayout); mainLayout->addSpacing(PADDING); @@ -116,7 +179,11 @@ SearchView::SearchView(QWidget *parent) : QWidget(parent) { setLayout(mainLayout); updateChecker = 0; + +#ifndef APP_MAC_STORE checkForUpdate(); +#endif + } void SearchView::updateRecentKeywords() { @@ -137,7 +204,7 @@ void SearchView::updateRecentKeywords() { foreach (QString keyword, keywords) { QString link = keyword; QString display = keyword; - if (keyword.startsWith("http://")) { + if (keyword.startsWith("http://") || keyword.startsWith("https://")) { int separator = keyword.indexOf("|"); if (separator > 0 && separator + 1 < keyword.length()) { link = keyword.left(separator); @@ -153,16 +220,54 @@ void SearchView::updateRecentKeywords() { // Make links navigable with the keyboard too itemLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse); - connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watch(QString))); + connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watchKeywords(QString))); recentKeywordsLayout->addWidget(itemLabel); } } +void SearchView::updateRecentChannels() { + + // cleanup + QLayoutItem *item; + while ((item = recentChannelsLayout->takeAt(1)) != 0) { + item->widget()->close(); + 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) { + QString link = keyword; + QString display = keyword; + if (keyword.startsWith("http://") || keyword.startsWith("https://")) { + int separator = keyword.indexOf("|"); + if (separator > 0 && separator + 1 < keyword.length()) { + link = keyword.left(separator); + display = keyword.mid(separator+1); + } + } + QLabel *itemLabel = new QLabel("" + + display + "", this); + + itemLabel->setMaximumWidth(queryEdit->width() + watchButton->width()); + // itemLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); + // Make links navigable with the keyboard too + itemLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse); + + connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watchChannel(QString))); + recentChannelsLayout->addWidget(itemLabel); + } +} void SearchView::watch() { - QString query = queryEdit->text().simplified(); + QString query = queryEdit->text(); watch(query); } @@ -172,14 +277,64 @@ void SearchView::textChanged(const QString &text) { void SearchView::watch(QString query) { + query = query.simplified(); + // check for empty query if (query.length() == 0) { queryEdit->setFocus(Qt::OtherFocusReason); return; } + SearchParams *searchParams = new SearchParams(); + if (typeCombo->currentIndex() == 0) + searchParams->setKeywords(query); + else { + // remove spaces from channel name + query = query.replace(" ", ""); + searchParams->setAuthor(query); + searchParams->setSortBy(SearchParams::SortByNewest); + } + // go! - emit search(query); + emit search(searchParams); +} + +void SearchView::watchChannel(QString channel) { + + channel = channel.simplified(); + + // check for empty query + if (channel.length() == 0) { + queryEdit->setFocus(Qt::OtherFocusReason); + return; + } + + // remove spaces from channel name + channel = channel.replace(" ", ""); + + SearchParams *searchParams = new SearchParams(); + searchParams->setAuthor(channel); + searchParams->setSortBy(SearchParams::SortByNewest); + + // go! + emit search(searchParams); +} + +void SearchView::watchKeywords(QString query) { + + query = query.simplified(); + + // check for empty query + if (query.length() == 0) { + queryEdit->setFocus(Qt::OtherFocusReason); + return; + } + + SearchParams *searchParams = new SearchParams(); + searchParams->setKeywords(query); + + // go! + emit search(searchParams); } void SearchView::checkForUpdate() { @@ -224,7 +379,7 @@ void SearchView::gotNewVersion(QString version) { } void SearchView::paintEvent(QPaintEvent * /*event*/) { -#ifdef APP_MAC +#if defined(APP_MAC) | defined(APP_WIN) QBrush brush; if (window()->isActiveWindow()) { brush = QBrush(QColor(0xdd, 0xe4, 0xeb)); @@ -235,3 +390,12 @@ void SearchView::paintEvent(QPaintEvent * /*event*/) { painter.fillRect(0, 0, width(), height(), brush); #endif } + +void SearchView::searchTypeChanged(int index) { + if (index == 0) { + queryEdit->setSuggester(youtubeSuggest); + } else { + queryEdit->setSuggester(channelSuggest); + } + queryEdit->setFocus(); +}