+++ /dev/null
-/* $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 */
-
-#ifndef SUGGESTER_H
-#define SUGGESTER_H
-
-#include <QtCore>
-
-class Suggestion {
-
-public:
- Suggestion(QString value = QString(),
- QString type = QString()) :
- value(value), type(type) { }
- QString value;
- QString type;
-
- bool operator==(const Suggestion &other) const {
- return (value == other.value) && (type == other.type);
- }
-
- bool operator!=(const Suggestion &other) const {
- return !(*this == other);
- }
-
- bool operator==(Suggestion *other) const {
- qDebug() << "Comparing" << this << other;
- return (value == other->value) && (type == other->type);
- }
-
- bool operator!=(Suggestion *other) const {
- return !(this == other);
- }
-
-};
-
-class Suggester : public QObject {
-
- Q_OBJECT
-
-public:
- Suggester(QObject *parent) : QObject(parent) { }
- virtual void suggest(const QString &query) = 0;
-
-signals:
- void ready(const QList<Suggestion*> &suggestions);
-
-};
-
-#endif // SUGGESTER_H
-
#include "suggester.h"
#ifdef APP_MAC
#include "searchlineedit_mac.h"
+#include "macutils.h"
#else
#include "searchlineedit.h"
#endif
AutoComplete::AutoComplete(SearchLineEdit *buddy, QLineEdit *lineEdit):
- QObject(buddy), buddy(buddy), lineEdit(lineEdit), suggester(0) {
-
- enabled = true;
+ QObject(buddy), buddy(buddy), lineEdit(lineEdit), enabled(true), suggester(0) {
popup = new QListWidget();
popup->setMouseTracking(true);
popup->setFocusPolicy(Qt::NoFocus);
popup->setFocusProxy(buddy);
popup->installEventFilter(this);
+ buddy->window()->installEventFilter(this);
+ // style
popup->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
popup->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
popup->setWindowOpacity(.9);
popup->setProperty("suggest", true);
- popup->setFrameShape(QFrame::NoFrame);
- popup->setAttribute(Qt::WA_TranslucentBackground);
- popup->viewport()->setStyleSheet("border:0; border-radius:5px; background:palette(base)");
connect(popup, SIGNAL(itemClicked(QListWidgetItem*)), SLOT(acceptSuggestion()));
connect(popup, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)),
- SLOT(currentItemChanged(QListWidgetItem*)));
+ SLOT(currentItemChanged(QListWidgetItem*)));
connect(popup, SIGNAL(itemEntered(QListWidgetItem*)), SLOT(itemEntered(QListWidgetItem *)));
timer = new QTimer(this);
}
bool AutoComplete::eventFilter(QObject *obj, QEvent *ev) {
- if (obj != popup) return false;
+ if (obj != popup) {
+ switch (ev->type()) {
+ case QEvent::Move:
+ case QEvent::Resize:
+ adjustPosition();
+ break;
+ default:
+ break;
+ }
+ return false;
+ }
if (ev->type() == QEvent::Leave) {
popup->setCurrentItem(0);
return true;
}
- if (ev->type() == QEvent::FocusOut) {
- popup->hide();
- buddy->setText(originalText);
- buddy->setFocus();
+ if (ev->type() == QEvent::FocusOut || ev->type() == QEvent::MouseButtonPress) {
+ hideSuggestions();
return true;
}
acceptSuggestion();
consumed = true;
} else {
- buddy->setFocus();
lineEdit->event(ev);
- popup->hide();
+ hideSuggestions();
}
break;
case Qt::Key_Escape:
- popup->hide();
- popup->clear();
- buddy->setText(originalText);
- buddy->setFocus();
+ hideSuggestions();
consumed = true;
break;
void AutoComplete::showCompletion(const QList<Suggestion *> &suggestions) {
if (suggestions.isEmpty()) {
- popup->clear();
- popup->hide();
+ hideSuggestions();
return;
}
popup->setUpdatesEnabled(false);
popup->clear();
for (int i = 0; i < suggestions.count(); ++i) {
- QListWidgetItem * item;
- item = new QListWidgetItem(popup);
+ QListWidgetItem *item = new QListWidgetItem(popup);
Suggestion *s = suggestions[i];
item->setText(s->value);
if (!s->type.isEmpty())
for (int i = 0; i < suggestions.count(); ++i)
h += popup->sizeHintForRow(i);
popup->resize(buddy->width(), h);
- popup->move(buddy->mapToGlobal(QPoint(0, buddy->height())));
- popup->setFocus();
-
- if (popup->isHidden()) popup->show();
+ adjustPosition();
popup->setUpdatesEnabled(true);
+
+ if (popup->isHidden()) {
+ popup->show();
+ popup->setFocus();
+ }
}
void AutoComplete::acceptSuggestion() {
- timer->stop();
- originalText.clear();
- popup->hide();
- buddy->setFocus();
int index = popup->currentIndex().row();
if (index >= 0 && index < suggestions.size()) {
Suggestion* suggestion = suggestions.at(index);
buddy->setText(suggestion->value);
emit suggestionAccepted(suggestion);
emit suggestionAccepted(suggestion->value);
- popup->clear();
+ originalText.clear();
+ hideSuggestions();
} else qWarning() << "No suggestion for index" << index;
}
timer->stop();
enabled = false;
popup->hide();
- popup->setFrameShape(QFrame::NoFrame);
}
void AutoComplete::enableSuggest() {
void AutoComplete::suggest() {
if (!enabled) return;
- if (!buddy->hasFocus()) return;
popup->setCurrentItem(0);
popup->clearSelection();
originalText = buddy->text();
if (originalText.isEmpty()) {
- popup->hide();
- buddy->setFocus();
+ hideSuggestions();
return;
}
showCompletion(suggestions);
}
+void AutoComplete::adjustPosition() {
+ popup->move(buddy->mapToGlobal(QPoint(0, buddy->height())));
+}
+
+void AutoComplete::hideSuggestions() {
+#ifdef APP_MAC
+ mac::fadeOutWindow(popup);
+#else
+ popup->hide();
+ popup->clear();
+#endif
+ if (!originalText.isEmpty()) {
+ buddy->setText(originalText);
+ originalText.clear();
+ }
+ buddy->setFocus();
+ timer->stop();
+}
+
void AutoComplete::itemEntered(QListWidgetItem *item) {
if (!item) return;
item->setSelected(true);
void showCompletion(const QList<Suggestion*> &suggestions);
void setSuggester(Suggester* suggester);
QListWidget* getPopup() { return popup; }
-
-public slots:
- void acceptSuggestion();
void preventSuggest();
void enableSuggest();
- void suggest();
- void itemEntered(QListWidgetItem *item);
- void currentItemChanged(QListWidgetItem *item);
- void suggestionsReady(const QList<Suggestion*> &suggestions);
signals:
void suggestionAccepted(Suggestion *suggestion);
void suggestionAccepted(const QString &value);
+private slots:
+ void acceptSuggestion();
+ void suggest();
+ void itemEntered(QListWidgetItem *item);
+ void currentItemChanged(QListWidgetItem *item);
+ void suggestionsReady(const QList<Suggestion*> &suggestions);
+ void adjustPosition();
+
private:
+ void hideSuggestions();
+
SearchLineEdit *buddy;
QLineEdit *lineEdit;
QString originalText;
#ifdef APP_PHONON_SEEK
seekSlider = new Phonon::SeekSlider(this);
+ seekSlider->setTracking(true);
seekSlider->setIconVisible(false);
seekSlider->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
mainToolBar->addWidget(seekSlider);
#endif
toolbarSearch->setMinimumWidth(toolbarSearch->fontInfo().pixelSize()*15);
toolbarSearch->setSuggester(new YTSuggester(this));
- connect(toolbarSearch, SIGNAL(search(const QString&)), this, SLOT(startToolbarSearch(const QString&)));
- connect(toolbarSearch, SIGNAL(suggestionAccepted(const QString&)), SLOT(startToolbarSearch(const QString&)));
+ connect(toolbarSearch, SIGNAL(search(const QString&)), SLOT(search(const QString&)));
+ connect(toolbarSearch, SIGNAL(suggestionAccepted(Suggestion*)), SLOT(suggestionAccepted(Suggestion*)));
toolbarSearch->setStatusTip(searchFocusAct->statusTip());
#ifdef APP_MAC
mainToolBar->addWidget(searchWrapper);
else goBack();
}
-void MainWindow::startToolbarSearch(QString query) {
- query = query.trimmed();
-
- // check for empty query
- if (query.length() == 0) {
- return;
- }
+void MainWindow::suggestionAccepted(Suggestion *suggestion) {
+ search(suggestion->value);
+}
+void MainWindow::search(const QString &query) {
+ QString q = query.trimmed();
+ if (q.length() == 0) return;
SearchParams *searchParams = new SearchParams();
- searchParams->setKeywords(query);
-
- // go!
+ searchParams->setKeywords(q);
showMedia(searchParams);
}
void MainWindow::dragEnterEvent(QDragEnterEvent *event) {
if (event->mimeData()->hasFormat("text/uri-list")) {
QList<QUrl> urls = event->mimeData()->urls();
- if (urls.isEmpty())
- return;
+ if (urls.isEmpty()) return;
QUrl url = urls.first();
QString videoId = YTSearch::videoIdFromUrl(url.toString());
- if (!videoId.isNull())
+ if (!videoId.isEmpty())
event->acceptProposedAction();
}
}
return;
QUrl url = urls.first();
QString videoId = YTSearch::videoIdFromUrl(url.toString());
- if (!videoId.isNull()) {
+ if (!videoId.isEmpty()) {
setWindowTitle(url.toString());
SearchParams *searchParams = new SearchParams();
searchParams->setKeywords(videoId);
#include <phonon/mediaobject.h>
#include <phonon/seekslider.h>
#endif
-#include "view.h"
class HomeView;
class MediaView;
class UpdateChecker;
class SearchParams;
class VideoSource;
+class Suggestion;
class MainWindow : public QMainWindow {
void restore();
void messageReceived(const QString &message);
void quit();
- void startToolbarSearch(QString query);
+ void suggestionAccepted(Suggestion *suggestion);
+ void search(const QString &query);
void goBack();
void showMessage(QString message);
#ifdef APP_ACTIVATION
connect(playlistModel, SIGNAL(haveSuggestions(const QStringList &)),
sidebar, SLOT(showSuggestions(const QStringList &)));
connect(sidebar, SIGNAL(suggestionAccepted(QString)),
- MainWindow::instance(), SLOT(startToolbarSearch(QString)));
+ MainWindow::instance(), SLOT(search(QString)));
splitter->addWidget(sidebar);
videoAreaWidget = new VideoAreaWidget(this);
}
-void MediaView::handleError(QString /* message */) {
+void MediaView::handleError(QString message) {
+ qWarning() << __PRETTY_FUNCTION__ << message;
#ifdef APP_PHONON_SEEK
mediaObject->play();
#else
void textChanged(const QString &text);
void textEdited(const QString &text);
void search(const QString &text);
- void suggestionAccepted(const QString &suggestion);
+ void suggestionAccepted(Suggestion *suggestion);
public:
SearchLineEdit(QWidget *parent = 0);
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&)));
+ connect(queryEdit, SIGNAL(textEdited(const QString &)), SLOT(textChanged(const QString &)));
+ connect(queryEdit, SIGNAL(suggestionAccepted(Suggestion*)), SLOT(suggestionAccepted(Suggestion*)));
youtubeSuggest = new YTSuggester(this);
channelSuggest = new ChannelSuggest(this);
queryEdit->selectAll();
queryEdit->setFocus();
}
+
+void SearchView::suggestionAccepted(Suggestion *suggestion) {
+ watch(suggestion->value);
+}
class SearchParams;
class YTSuggester;
class ChannelSuggest;
+class Suggestion;
class SearchView : public QWidget, public View {
void watch();
void textChanged(const QString &text);
void searchTypeChanged(int index);
+ void suggestionAccepted(Suggestion *suggestion);
private:
YTSuggester *youtubeSuggest;