]> git.sur5r.net Git - minitube/blob - src/homeview.cpp
a8f061b7f40e4dd575e500e5b5a9f167d8f688f8
[minitube] / src / homeview.cpp
1 /* $BEGIN_LICENSE
2
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Minitube.  If not, see <http://www.gnu.org/licenses/>.
18
19 $END_LICENSE */
20
21 #include "homeview.h"
22 #include "segmentedcontrol.h"
23 #include "searchview.h"
24 #include "standardfeedsview.h"
25 #include "channelview.h"
26 #include "mainwindow.h"
27 #include "mediaview.h"
28 #include "ytstandardfeed.h"
29 #include "iconutils.h"
30 #include "channelaggregator.h"
31 #ifdef APP_MAC
32 #include "macutils.h"
33 #endif
34
35 HomeView::HomeView(QWidget *parent) : View(parent),
36     standardFeedsView(0),
37     channelsView(0) {
38
39     QBoxLayout *layout = new QVBoxLayout(this);
40     layout->setMargin(0);
41     layout->setSpacing(0);
42
43     setupBar();
44     layout->addWidget(bar);
45
46     stackedWidget = new QStackedWidget();
47     layout->addWidget(stackedWidget);
48
49     searchView = new SearchView(this);
50     connect(searchView, SIGNAL(search(SearchParams*)),
51             MainWindow::instance(), SLOT(showMedia(SearchParams*)));
52     stackedWidget->addWidget(searchView);
53 }
54
55 void HomeView::setupBar() {
56     bar = new SegmentedControl();
57
58     QAction *action = new QAction(tr("Search"), this);
59     action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_1));
60     action->setStatusTip(tr("Find videos and channels by keyword"));
61     connect(action, SIGNAL(triggered()), SLOT(showSearch()));
62     bar->addAction(action);
63     bar->setCheckedAction(action);
64
65     action = new QAction(tr("Browse"), this);
66     action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_2));
67     action->setStatusTip(tr("Browse videos by category"));
68     connect(action, SIGNAL(triggered()), SLOT(showStandardFeeds()));
69     bar->addAction(action);
70
71     subscriptionsAction = new QAction(tr("Subscriptions"), this);
72     subscriptionsAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_3));
73     subscriptionsAction->setStatusTip(tr("Channel subscriptions"));
74     connect(subscriptionsAction, SIGNAL(triggered()), SLOT(showChannels()));
75     bar->addAction(subscriptionsAction);
76     connect(ChannelAggregator::instance(), SIGNAL(unwatchedCountChanged(int)),
77             SLOT(unwatchedCountChanged(int)));
78
79     const auto a = bar->actions();
80     for (QAction* action : a) {
81         addAction(action);
82         IconUtils::setupAction(action);
83     }
84 }
85
86 void HomeView::showWidget(QWidget *widget) {
87     QWidget* currentWidget = stackedWidget->currentWidget();
88     if (currentWidget == widget) return;
89     QMetaObject::invokeMethod(currentWidget, "disappear");
90     currentWidget->setEnabled(false);
91     stackedWidget->setCurrentWidget(widget);
92     widget->setEnabled(true);
93     QMetaObject::invokeMethod(widget, "appear");
94     QTimer::singleShot(0, widget, SLOT(setFocus()));
95
96 #ifdef APP_MAC
97     // Workaround cursor bug on macOS
98     window()->unsetCursor();
99 #endif
100 }
101
102 void HomeView::appear() {
103     QMetaObject::invokeMethod(stackedWidget->currentWidget(), "appear", Qt::QueuedConnection);
104 }
105
106 void HomeView::disappear() {
107     QMetaObject::invokeMethod(stackedWidget->currentWidget(), "disappear");
108 }
109
110 void HomeView::showSearch() {
111     showWidget(searchView);
112     bar->setCheckedAction(0);
113 }
114
115 void HomeView::showStandardFeeds() {
116     if (!standardFeedsView) {
117         standardFeedsView = new StandardFeedsView();
118         connect(standardFeedsView, SIGNAL(activated(VideoSource*)),
119                 MainWindow::instance(),
120                 SLOT(showMedia(VideoSource*)));
121         stackedWidget->addWidget(standardFeedsView);
122     }
123     showWidget(standardFeedsView);
124     bar->setCheckedAction(1);
125 }
126
127 void HomeView::showChannels() {
128     if (!channelsView) {
129         channelsView = new ChannelView();
130         connect(channelsView, SIGNAL(activated(VideoSource*)),
131                 MainWindow::instance(),
132                 SLOT(showMedia(VideoSource*)));
133         stackedWidget->addWidget(channelsView);
134     }
135     showWidget(channelsView);
136     bar->setCheckedAction(2);
137 }
138
139 void HomeView::unwatchedCountChanged(int count) {
140     QVariant v;
141     QString s;
142     if (count > 0) {
143         s = QString::number(count);
144         v = s;
145     }
146     subscriptionsAction->setProperty("notifyCount", v);
147     bar->update();
148 #ifdef APP_MAC
149     mac::dockBadge(s);
150 #endif
151 }