]> git.sur5r.net Git - minitube/blobdiff - src/searchview.cpp
Views refactoring, don't use RTTI
[minitube] / src / searchview.cpp
index e3cadcfd106dc713c5ff7cebf87a94e62b5e6987..2f79703f213392d3afa66cfef2717fc2a619d23e 100644 (file)
@@ -46,10 +46,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::small();
+SearchView::SearchView(QWidget *parent) : View(parent) {
 
 #if defined(APP_MAC) | defined(APP_WIN)
     // speedup painting since we'll paint the whole background
@@ -57,8 +54,6 @@ SearchView::SearchView(QWidget *parent) : QWidget(parent) {
     setAttribute(Qt::WA_OpaquePaintEvent);
 #endif
 
-    setAutoFillBackground(true);
-
     QBoxLayout *mainLayout = new QVBoxLayout(this);
     mainLayout->setMargin(PADDING);
     mainLayout->setSpacing(0);
@@ -109,6 +104,8 @@ 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
@@ -137,19 +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);
-#ifndef APP_MAC_SEARCHFIELD
-    queryEdit->setFont(biggerFont);
+    queryEdit->toWidget()->setFont(biggerFont);
 #endif
-    connect(queryEdit, SIGNAL(search(const QString&)), SLOT(watch(const QString&)));
-    connect(queryEdit, SIGNAL(textEdited(const QString &)), SLOT(textChanged(const QString &)));
-    connect(queryEdit, SIGNAL(suggestionAccepted(Suggestion*)), SLOT(suggestionAccepted(Suggestion*)));
+
+    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);
@@ -170,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);
@@ -183,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);
@@ -203,17 +202,14 @@ SearchView::SearchView(QWidget *parent) : QWidget(parent) {
 }
 
 void SearchView::appear() {
-    setUpdatesEnabled(false);
+    MainWindow::instance()->showActionInStatusBar(The::globalActions()->value("definition"), true);
+
     updateRecentKeywords();
     updateRecentChannels();
     queryEdit->selectAll();
     queryEdit->enableSuggest();
-    setUpdatesEnabled(true);
 
-    MainWindow::instance()->showActionInStatusBar(The::globalActions()->value("definition"), true);
-
-    queryEdit->setFocus();
-    QTimer::singleShot(100, queryEdit, SLOT(setFocus()));
+    if (!queryEdit->toWidget()->hasFocus()) queryEdit->toWidget()->setFocus();
 }
 
 void SearchView::disappear() {
@@ -221,6 +217,11 @@ void SearchView::disappear() {
 }
 
 void SearchView::updateRecentKeywords() {
+    // load
+    QSettings settings;
+    QStringList keywords = settings.value(recentKeywordsKey).toStringList();
+    if (keywords == recentKeywords) return;
+    recentKeywords = keywords;
 
     // cleanup
     QLayoutItem *item;
@@ -229,9 +230,6 @@ 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());
 
@@ -256,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);
@@ -269,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;
@@ -277,9 +280,6 @@ 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());
 
@@ -296,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);
@@ -321,7 +321,7 @@ void SearchView::watch(const QString &query) {
 
     // check for empty query
     if (q.length() == 0) {
-        queryEdit->setFocus(Qt::OtherFocusReason);
+        queryEdit->toWidget()->setFocus(Qt::OtherFocusReason);
         return;
     }
 
@@ -341,7 +341,7 @@ void SearchView::watch(const QString &query) {
 
 void SearchView::watchChannel(const QString &channelId) {
     if (channelId.length() == 0) {
-        queryEdit->setFocus(Qt::OtherFocusReason);
+        queryEdit->toWidget()->setFocus(Qt::OtherFocusReason);
         return;
     }
 
@@ -363,7 +363,7 @@ void SearchView::watchKeywords(const QString &query) {
 
     // check for empty query
     if (query.length() == 0) {
-        queryEdit->setFocus(Qt::OtherFocusReason);
+        queryEdit->toWidget()->setFocus(Qt::OtherFocusReason);
         return;
     }
 
@@ -384,7 +384,6 @@ 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();
@@ -399,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) {
@@ -409,7 +407,7 @@ void SearchView::searchTypeChanged(int index) {
         queryEdit->setSuggester(channelSuggest);
     }
     queryEdit->selectAll();
-    queryEdit->setFocus();
+    queryEdit->toWidget()->setFocus();
 }
 
 void SearchView::suggestionAccepted(Suggestion *suggestion) {