blob: ec620ef035b37475a4b9ad85c2ac3082fcbd476b [file] [log] [blame]
Edric Milaret029b95a2015-06-09 09:51:44 -04001/***************************************************************************
Edric Milaret5f316da2015-09-28 11:57:42 -04002 * Copyright (C) 2015 by Savoir-faire Linux *
Edric Milaret029b95a2015-06-09 09:51:44 -04003 * Author: Edric Ladent Milaret <edric.ladent-milaret@savoirfairelinux.com>*
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17 **************************************************************************/
18
19#include "videoview.h"
20#include "ui_videoview.h"
21
22#include "callmodel.h"
23#include "video/devicemodel.h"
24#include "video/sourcemodel.h"
25
26#include <QGraphicsOpacityEffect>
27#include <QPropertyAnimation>
28#include <QDesktopWidget>
29#include <QMenu>
Edric Milaret303bbf12015-06-22 14:24:13 -040030#include <QFileDialog>
31#include <QMimeData>
Edric Milaret029b95a2015-06-09 09:51:44 -040032
33#include <memory>
34
35#include "videooverlay.h"
36#include "selectareadialog.h"
37
38VideoView::VideoView(QWidget *parent) :
39 QWidget(parent),
40 ui(new Ui::VideoView)
41{
42 ui->setupUi(this);
43
44 connect(CallModel::instance(), SIGNAL(callStateChanged(Call*, Call::State)),
45 this, SLOT(callStateChanged(Call*, Call::State)));
46
47 overlay_ = new VideoOverlay(this);
48 auto effect = new QGraphicsOpacityEffect(overlay_);
49 effect->setOpacity(1.0);
50 overlay_->setGraphicsEffect(effect);
51 fadeAnim_ = new QPropertyAnimation(this);
52 fadeAnim_->setTargetObject(effect);
53 fadeAnim_->setPropertyName("opacity");
54 fadeAnim_->setDuration(fadeOverlayTime_);
55 fadeAnim_->setStartValue(effect->opacity());
56 fadeAnim_->setEndValue(0);
57 fadeAnim_->setEasingCurve(QEasingCurve::OutQuad);
58
59 timerLength_ = new QTimer(this);
60 connect(timerLength_, SIGNAL(timeout()), this, SLOT(updateTimer()));
61
62 this->setContextMenuPolicy(Qt::CustomContextMenu);
63 connect(this, SIGNAL(customContextMenuRequested(const QPoint&)),
64 this, SLOT(showContextMenu(const QPoint&)));
Edric Milaret3aca8e32015-06-12 10:01:40 -040065 connect(overlay_, &VideoOverlay::setChatVisibility, [=](bool visible) {
66 emit this->setChatVisibility(visible);
67 });
Edric Milaret029b95a2015-06-09 09:51:44 -040068}
69
70VideoView::~VideoView()
71{
72 delete ui;
Edric Milaret01f23842015-06-22 14:46:01 -040073 delete overlay_;
74 delete timerLength_;
75 delete fadeAnim_;
Edric Milaret029b95a2015-06-09 09:51:44 -040076}
77
78void
79VideoView::resizeEvent(QResizeEvent *event)
80{
81 Q_UNUSED(event)
82 overlay_->resize(this->size());
83 overlay_->show();
84 overlay_->raise();
85}
86
87void
88VideoView::enterEvent(QEvent* event)
89{
90 Q_UNUSED(event)
91 fadeAnim_->stop();
92 fadeAnim_->targetObject()->setProperty(fadeAnim_->propertyName(), fadeAnim_->startValue());
93}
94
95void
96VideoView::leaveEvent(QEvent* event)
97{
98 Q_UNUSED(event)
99 fadeAnim_->start(QAbstractAnimation::KeepWhenStopped);
100}
101
102void
103VideoView::callStateChanged(Call* call, Call::State previousState)
104{
105 Q_UNUSED(previousState)
106 if (call->state() == Call::State::CURRENT) {
107 ui->videoWidget->show();
108 timerLength_->start(1000);
109 overlay_->setName(call->formattedName());
110 }
111 else {
Edric Milaret3aca8e32015-06-12 10:01:40 -0400112 emit setChatVisibility(false);
Edric Milaret029b95a2015-06-09 09:51:44 -0400113 ui->videoWidget->hide();
114 if (isFullScreen())
115 toggleFullScreen();
116 timerLength_->stop();
117 }
118}
119
120void
121VideoView::updateTimer()
122{
123 overlay_->setTime(CallModel::instance()->selectedCall()->length());
124}
125
126void
127VideoView::mouseDoubleClickEvent(QMouseEvent* e) {
128 QWidget::mouseDoubleClickEvent(e);
129 toggleFullScreen();
130}
131
Edric Milaret303bbf12015-06-22 14:24:13 -0400132void VideoView::dragEnterEvent(QDragEnterEvent *event)
133{
134 if (event->mimeData()->hasUrls())
135 event->acceptProposedAction();
136}
137
138void VideoView::dropEvent(QDropEvent *event)
139{
140 auto urls = event->mimeData()->urls();
141 Video::SourceModel::instance()->setFile(urls.at(0));
142}
143
Edric Milaret029b95a2015-06-09 09:51:44 -0400144void
145VideoView::toggleFullScreen()
146{
147 if(isFullScreen()) {
148 this->setParent(oldParent_);
Edric Milaretb46f6492015-09-25 15:28:31 -0400149 oldParent_->layout()->addWidget(this);
Edric Milaret029b95a2015-06-09 09:51:44 -0400150 this->resize(oldSize_.width(), oldSize_.height());
Edric Milaretb46f6492015-09-25 15:28:31 -0400151 this->show();
Edric Milaret029b95a2015-06-09 09:51:44 -0400152 } else {
153 oldSize_ = this->size();
154 oldParent_ = static_cast<QWidget*>(this->parent());
155 this->setParent(0);
156 this->showFullScreen();
157 this->show();
158 }
159}
160
161void
162VideoView::showContextMenu(const QPoint& pos)
163{
164 QPoint globalPos = this->mapToGlobal(pos);
165
166 QMenu menu;
167
168 for (auto device : Video::DeviceModel::instance()->devices()) {
169 std::unique_ptr<QAction> deviceAction(new QAction(device->name(), this));
170 deviceAction->setCheckable(true);
171 if (device == Video::DeviceModel::instance()->activeDevice())
172 deviceAction->setChecked(true);
173 auto ptr = deviceAction.release();
174 menu.addAction(ptr);
175 connect(ptr, &QAction::toggled, [=](bool checked) {
176 if (checked == true) {
177 Video::SourceModel::instance()->switchTo(device);
178 Video::DeviceModel::instance()->setActive(device);
179 }
180 });
181 }
182
183 menu.addSeparator();
184
Edric Milaret53ac6e52015-09-14 13:37:06 -0400185 auto shareAction = new QAction(tr("Share entire screen"), this);
Edric Milaret029b95a2015-06-09 09:51:44 -0400186 menu.addAction(shareAction);
187 connect(shareAction, &QAction::triggered, [=]() {
188 Video::SourceModel::instance()->setDisplay(0, QApplication::desktop()->rect());
189 });
Edric Milaret53ac6e52015-09-14 13:37:06 -0400190 auto shareAreaAction = new QAction(tr("Share screen area"), this);
Edric Milaret029b95a2015-06-09 09:51:44 -0400191 menu.addAction(shareAreaAction);
192 connect(shareAreaAction, &QAction::triggered, [=]() {
193 SelectAreaDialog selec;
194 selec.exec();
195 });
Edric Milaret53ac6e52015-09-14 13:37:06 -0400196 auto shareFileAction = new QAction(tr("Share file"), this);
Edric Milaret303bbf12015-06-22 14:24:13 -0400197 menu.addAction(shareFileAction);
198 connect(shareFileAction, &QAction::triggered, [=]() {
199 QFileDialog dialog(this);
200 dialog.setFileMode(QFileDialog::AnyFile);
201 QStringList fileNames;
202 if (!dialog.exec())
203 return;
204 fileNames = dialog.selectedFiles();
205 Video::SourceModel::instance()->setFile(QUrl::fromLocalFile(fileNames.at(0)));
206 });
Edric Milaret029b95a2015-06-09 09:51:44 -0400207
208 menu.exec(globalPos);
209}
210