]> git.sur5r.net Git - minitube/blob - src/videoareawidget.cpp
Update watch file to use releases instead of tags
[minitube] / src / videoareawidget.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 "videoareawidget.h"
22 #include "loadingwidget.h"
23 #include "mainwindow.h"
24 #include "playlistmodel.h"
25 #include "video.h"
26 #include "videomimedata.h"
27 #ifdef Q_OS_MAC
28 #include "macutils.h"
29 #endif
30 #include "snapshotpreview.h"
31 #include "fontutils.h"
32
33 namespace {
34
35 class MessageWidget : public QWidget {
36 public:
37     MessageWidget(QWidget *parent = nullptr) : QWidget(parent) {
38         QPalette p = palette();
39         p.setColor(QPalette::Window, Qt::black);
40         p.setColor(QPalette::WindowText, Qt::darkGray);
41         p.setColor(QPalette::Base, Qt::black);
42         p.setColor(QPalette::Text, Qt::darkGray);
43         setPalette(p);
44         setAutoFillBackground(true);
45
46         QBoxLayout *l = new QHBoxLayout(this);
47         l->setMargin(32);
48         l->setSpacing(32);
49         l->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
50
51         QLabel *arrowLabel = new QLabel("←");
52         arrowLabel->setFont(FontUtils::light(64));
53         arrowLabel->setPalette(p);
54         l->addWidget(arrowLabel);
55
56         QLabel *msgLabel = new QLabel(tr("Pick a video"));
57         msgLabel->setFont(FontUtils::light(32));
58         msgLabel->setPalette(p);
59         l->addWidget(msgLabel);
60     }
61 };
62 }
63
64 VideoAreaWidget::VideoAreaWidget(QWidget *parent)
65     : QWidget(parent), videoWidget(0), messageWidget(0) {
66     setAttribute(Qt::WA_OpaquePaintEvent);
67
68     QBoxLayout *layout = new QVBoxLayout(this);
69     layout->setMargin(0);
70     layout->setSpacing(0);
71
72     // hidden message widget
73     messageLabel = new QLabel(this);
74     messageLabel->setOpenExternalLinks(true);
75     messageLabel->setMargin(7);
76     messageLabel->setBackgroundRole(QPalette::ToolTipBase);
77     messageLabel->setForegroundRole(QPalette::ToolTipText);
78     messageLabel->setAutoFillBackground(true);
79     messageLabel->setWordWrap(true);
80     messageLabel->hide();
81     layout->addWidget(messageLabel);
82
83     stackedLayout = new QStackedLayout();
84     layout->addLayout(stackedLayout);
85
86 #ifdef APP_SNAPSHOT
87     snapshotPreview = new SnapshotPreview();
88     connect(stackedLayout, SIGNAL(currentChanged(int)), snapshotPreview, SLOT(hide()));
89 #endif
90
91     setAcceptDrops(true);
92     setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
93
94     setContextMenuPolicy(Qt::CustomContextMenu);
95     connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
96             SLOT(showContextMenu(const QPoint &)));
97 }
98
99 void VideoAreaWidget::setVideoWidget(QWidget *videoWidget) {
100     this->videoWidget = videoWidget;
101     stackedLayout->addWidget(videoWidget);
102 }
103
104 void VideoAreaWidget::setLoadingWidget(LoadingWidget *loadingWidget) {
105     this->loadingWidget = loadingWidget;
106     stackedLayout->addWidget(loadingWidget);
107     stackedLayout->setCurrentWidget(loadingWidget);
108 }
109
110 void VideoAreaWidget::showVideo() {
111     if (videoWidget) stackedLayout->setCurrentWidget(videoWidget);
112     loadingWidget->clear();
113 }
114
115 void VideoAreaWidget::showError(const QString &message) {
116     messageLabel->setText(message);
117     messageLabel->show();
118     stackedLayout->setCurrentWidget(loadingWidget);
119 }
120
121 void VideoAreaWidget::showPickMessage() {
122     if (!messageWidget) {
123         messageWidget = new MessageWidget();
124         stackedLayout->addWidget(messageWidget);
125     }
126     stackedLayout->setCurrentWidget(messageWidget);
127 }
128
129 void VideoAreaWidget::showLoading(Video *video) {
130     messageLabel->hide();
131     messageLabel->clear();
132     stackedLayout->setCurrentWidget(loadingWidget);
133     loadingWidget->setVideo(video);
134 }
135
136 #ifdef APP_SNAPSHOT
137 void VideoAreaWidget::showSnapshotPreview(const QPixmap &pixmap) {
138     bool soundOnly = false;
139 #ifdef APP_MAC
140     soundOnly = MainWindow::instance()->isReallyFullScreen();
141 #endif
142     snapshotPreview->start(videoWidget, pixmap, soundOnly);
143 }
144
145 void VideoAreaWidget::hideSnapshotPreview() {}
146 #endif
147
148 void VideoAreaWidget::clear() {
149     loadingWidget->clear();
150     messageLabel->hide();
151     messageLabel->clear();
152     stackedLayout->setCurrentWidget(loadingWidget);
153 }
154
155 void VideoAreaWidget::mouseDoubleClickEvent(QMouseEvent *event) {
156     if (event->button() == Qt::LeftButton) emit doubleClicked();
157 }
158
159 void VideoAreaWidget::dragEnterEvent(QDragEnterEvent *event) {
160     // qDebug() << event->mimeData()->formats();
161     if (event->mimeData()->hasFormat("application/x-minitube-video")) {
162         event->acceptProposedAction();
163     }
164 }
165
166 void VideoAreaWidget::dropEvent(QDropEvent *event) {
167     const VideoMimeData *videoMimeData = qobject_cast<const VideoMimeData *>(event->mimeData());
168     if (!videoMimeData) return;
169
170     QVector<Video *> droppedVideos = videoMimeData->getVideos();
171     if (droppedVideos.isEmpty()) return;
172     Video *video = droppedVideos.at(0);
173     int row = listModel->rowForVideo(video);
174     if (row != -1) listModel->setActiveRow(row);
175     event->acceptProposedAction();
176 }
177
178 void VideoAreaWidget::showContextMenu(const QPoint &point) {
179     QMenu *menu = MainWindow::instance()->getMenu("video");
180     menu->exec(mapToGlobal(point));
181 }