blob: 969abcf0c88e6704772bb0223e1b129f4e127092 [file] [log] [blame]
Edric Milaret029b95a2015-06-09 09:51:44 -04001/***************************************************************************
Anthony LĂ©onard2fde81d2017-04-17 10:06:55 -04002 * Copyright (C) 2015-2017 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
Andreas Traczykb8b13ba2018-08-21 16:30:16 -040022#include "utils.h"
23#include "lrcinstance.h"
24
Edric Milaret029b95a2015-06-09 09:51:44 -040025#include <QGraphicsOpacityEffect>
26#include <QPropertyAnimation>
27#include <QDesktopWidget>
28#include <QMenu>
Edric Milaret303bbf12015-06-22 14:24:13 -040029#include <QFileDialog>
30#include <QMimeData>
Nicolas Jagere0854512016-02-29 12:08:10 -050031#include <QSplitter>
Edricb09982a2016-05-19 16:28:38 -040032#include <QScreen>
Edric Milaret029b95a2015-06-09 09:51:44 -040033
34#include <memory>
35
36#include "videooverlay.h"
37#include "selectareadialog.h"
38
Edric Milaret80e0b212015-10-16 10:07:43 -040039VideoView::VideoView(QWidget* parent) :
Edric Milaret029b95a2015-06-09 09:51:44 -040040 QWidget(parent),
41 ui(new Ui::VideoView)
42{
43 ui->setupUi(this);
44
Edric Milareta3e47282015-10-23 15:20:30 -040045 connect(&CallModel::instance(), SIGNAL(callStateChanged(Call*, Call::State)),
Edric Milaret029b95a2015-06-09 09:51:44 -040046 this, SLOT(callStateChanged(Call*, Call::State)));
Andreas Traczykb8b13ba2018-08-21 16:30:16 -040047
Edric Milaret029b95a2015-06-09 09:51:44 -040048 overlay_ = new VideoOverlay(this);
49 auto effect = new QGraphicsOpacityEffect(overlay_);
Andreas Traczyk8d5e5492018-08-09 15:41:52 -040050 effect->setOpacity(maxOverlayOpacity_);
Edric Milaret029b95a2015-06-09 09:51:44 -040051 overlay_->setGraphicsEffect(effect);
52 fadeAnim_ = new QPropertyAnimation(this);
53 fadeAnim_->setTargetObject(effect);
54 fadeAnim_->setPropertyName("opacity");
55 fadeAnim_->setDuration(fadeOverlayTime_);
56 fadeAnim_->setStartValue(effect->opacity());
57 fadeAnim_->setEndValue(0);
58 fadeAnim_->setEasingCurve(QEasingCurve::OutQuad);
Andreas Traczykb8b13ba2018-08-21 16:30:16 -040059
Andreas Traczykca320d82018-08-09 18:02:07 -040060 // Setup the timer to start the fade when the mouse stops moving
61 this->setMouseTracking(true);
62 overlay_->setMouseTracking(true);
63 fadeTimer_.setSingleShot(true);
64 connect(&fadeTimer_, SIGNAL(timeout()), this, SLOT(fadeOverlayOut()));
Edric Milaret029b95a2015-06-09 09:51:44 -040065
Edric Milaret029b95a2015-06-09 09:51:44 -040066 this->setContextMenuPolicy(Qt::CustomContextMenu);
67 connect(this, SIGNAL(customContextMenuRequested(const QPoint&)),
68 this, SLOT(showContextMenu(const QPoint&)));
Edric Milaret3aca8e32015-06-12 10:01:40 -040069 connect(overlay_, &VideoOverlay::setChatVisibility, [=](bool visible) {
70 emit this->setChatVisibility(visible);
71 });
Olivier SOLDANO2bcdfd72017-05-02 14:37:19 -040072 connect(overlay_, &VideoOverlay::videoCfgBtnClicked, [=](){emit videoSettingsClicked();});
Isa Nanicca027ab2018-12-19 16:54:05 -050073
Edric Milaret029b95a2015-06-09 09:51:44 -040074}
75
76VideoView::~VideoView()
77{
78 delete ui;
Edric Milaret01f23842015-06-22 14:46:01 -040079 delete overlay_;
Edric Milaret01f23842015-06-22 14:46:01 -040080 delete fadeAnim_;
Edric Milaret029b95a2015-06-09 09:51:44 -040081}
82
83void
Edric Milaret80e0b212015-10-16 10:07:43 -040084VideoView::resizeEvent(QResizeEvent* event)
Edric Milaret029b95a2015-06-09 09:51:44 -040085{
Nicolas Jagerc7b8e4b2016-02-29 09:20:40 -050086 QRect& previewRect = ui->videoWidget->getPreviewRect();
87 int deltaW = event->size().width() - event->oldSize().width();
88 int deltaH = event->size().height() - event->oldSize().height();
89
90 QPoint previewCenter = ui->videoWidget->getPreviewRect().center();
Edricb09982a2016-05-19 16:28:38 -040091 int cx = (event->oldSize().width()) / 2;
92 int cy = (event->oldSize().height()) / 2;
Nicolas Jagerc7b8e4b2016-02-29 09:20:40 -050093 QPoint center = QPoint(cx, cy);
94
95 // first we check if we want to displace the preview
96 if (previewRect.x() + deltaW > 0 && previewRect.y() + deltaH > 0) {
97 // then we check which way
98 if (center.x() - previewCenter.x() < 0 && center.y() - previewCenter.y() < 0)
99 ui->videoWidget->getPreviewRect().translate(deltaW, deltaH);
100 else if (center.x() - previewCenter.x() > 0 && center.y() - previewCenter.y() < 0)
101 ui->videoWidget->getPreviewRect().translate(0, deltaH);
102 else if (center.x() - previewCenter.x() < 0 && center.y() - previewCenter.y() > 0)
103 ui->videoWidget->getPreviewRect().translate(deltaW, 0);
104 }
105
106 if (previewRect.left() <= 0)
107 previewRect.moveLeft(1);
108
109 if (previewRect.right() >= width())
110 previewRect.moveRight(width() - 1);
111
112 if (previewRect.top() <= 0)
113 previewRect.moveTop(1);
114
115 if (previewRect.bottom() >= height())
116 previewRect.moveBottom(height() - 1);
117
Edric Milaret029b95a2015-06-09 09:51:44 -0400118 overlay_->resize(this->size());
119 overlay_->show();
120 overlay_->raise();
121}
122
123void
124VideoView::enterEvent(QEvent* event)
125{
126 Q_UNUSED(event)
Andreas Traczykca320d82018-08-09 18:02:07 -0400127 showOverlay();
Edric Milaret029b95a2015-06-09 09:51:44 -0400128}
129
130void
131VideoView::leaveEvent(QEvent* event)
132{
133 Q_UNUSED(event)
Andreas Traczykca320d82018-08-09 18:02:07 -0400134 fadeOverlayOut();
135}
136
137void
138VideoView::showOverlay()
139{
140 fadeAnim_->stop();
141 fadeAnim_->targetObject()->setProperty(fadeAnim_->propertyName(), fadeAnim_->startValue());
142}
143
144void
145VideoView::fadeOverlayOut()
146{
Andreas Traczykb8b13ba2018-08-21 16:30:16 -0400147 if (!overlay_->isDialogVisible() && !overlay_->shouldShowOverlay()) {
Nicolas Jager0a9fc602016-03-11 18:35:42 -0500148 fadeAnim_->start(QAbstractAnimation::KeepWhenStopped);
Andreas Traczykb8b13ba2018-08-21 16:30:16 -0400149 }
Edric Milaret029b95a2015-06-09 09:51:44 -0400150}
151
152void
153VideoView::callStateChanged(Call* call, Call::State previousState)
154{
155 Q_UNUSED(previousState)
Edric Milaret80e0b212015-10-16 10:07:43 -0400156
Edric Milaret029b95a2015-06-09 09:51:44 -0400157 if (call->state() == Call::State::CURRENT) {
158 ui->videoWidget->show();
Edric Milareted0b2802015-10-01 15:10:02 -0400159 timerConnection_ = connect(call, SIGNAL(changed()), this, SLOT(updateCall()));
SĂ©bastien Blin93bd2062018-12-17 15:57:16 -0500160 } else {
Edric Milaret80e0b212015-10-16 10:07:43 -0400161 QObject::disconnect(timerConnection_);
SĂ©bastien Blin93bd2062018-12-17 15:57:16 -0500162 try {
163 if (call) {
164 emit closing(call->historyId().toStdString());
165 }
166 } catch (...) {
167 qWarning() << "VideoView::callStateChanged except";
168 }
Edric Milaret029b95a2015-06-09 09:51:44 -0400169 }
170}
171
172void
SĂ©bastien Blin93bd2062018-12-17 15:57:16 -0500173VideoView::showChatviewIfToggled()
174{
175 emit setChatVisibility(overlay_->getShowChatView());
176}
177
178void
Andreas Traczyk4b4bfde2018-12-19 09:53:22 -0500179VideoView::simulateShowChatview(bool checked)
180{
Andreas Traczyk14c3e862018-12-26 14:02:30 -0500181 Q_UNUSED(checked);
Andreas Traczyk4b4bfde2018-12-19 09:53:22 -0500182 overlay_->simulateShowChatview(true);
183}
184
185void
Edric Milareted0b2802015-10-01 15:10:02 -0400186VideoView::updateCall()
Edric Milaret029b95a2015-06-09 09:51:44 -0400187{
Edric Milaret80e0b212015-10-16 10:07:43 -0400188 if (auto call = CallModel::instance().selectedCall()) {
189 overlay_->setName(call->formattedName());
Edric Milaret80e0b212015-10-16 10:07:43 -0400190 }
Edric Milaret029b95a2015-06-09 09:51:44 -0400191}
192
193void
194VideoView::mouseDoubleClickEvent(QMouseEvent* e) {
195 QWidget::mouseDoubleClickEvent(e);
196 toggleFullScreen();
197}
198
Edric Milaret80e0b212015-10-16 10:07:43 -0400199void
200VideoView::dragEnterEvent(QDragEnterEvent* event)
Edric Milaret303bbf12015-06-22 14:24:13 -0400201{
202 if (event->mimeData()->hasUrls())
203 event->acceptProposedAction();
204}
205
Edric Milaret80e0b212015-10-16 10:07:43 -0400206void
207VideoView::dropEvent(QDropEvent* event)
Edric Milaret303bbf12015-06-22 14:24:13 -0400208{
209 auto urls = event->mimeData()->urls();
Andreas Traczykb8b13ba2018-08-21 16:30:16 -0400210 auto selectedConvUid = LRCInstance::getSelectedConvUid();
211 auto convModel = LRCInstance::getCurrentConversationModel();
212 auto conversation = Utils::getConversationFromUid(selectedConvUid, *convModel);
213 auto callList = CallModel::instance().getActiveCalls();
214 Call* thisCall = nullptr;
215 for (auto call : callList) {
216 if (call->historyId() == QString::fromStdString(conversation->callId)) {
217 thisCall = call;
218 break;
219 }
220 }
221 if (thisCall) {
222 if (auto outVideo = thisCall->firstMedia<media::Video>(media::Media::Direction::OUT)) {
Edric Milaret0e1074d2015-12-08 12:08:52 -0500223 outVideo->sourceModel()->setFile(urls.at(0));
224 }
225 }
Edric Milaret303bbf12015-06-22 14:24:13 -0400226}
227
Edric Milaret029b95a2015-06-09 09:51:44 -0400228void
229VideoView::toggleFullScreen()
230{
SĂ©bastien Blin93bd2062018-12-17 15:57:16 -0500231 emit toggleFullScreenClicked();
Edric Milaret029b95a2015-06-09 09:51:44 -0400232}
233
234void
235VideoView::showContextMenu(const QPoint& pos)
236{
237 QPoint globalPos = this->mapToGlobal(pos);
238
239 QMenu menu;
Andreas Traczyke86fb3c2018-08-02 17:38:34 -0400240 media::Video* outVideo = nullptr;
Edric Milaretc4b1a102016-02-02 11:48:30 -0500241 int activeIndex = -1;
242
Andreas Traczykb8b13ba2018-08-21 16:30:16 -0400243 auto selectedConvUid = LRCInstance::getSelectedConvUid();
244 auto convModel = LRCInstance::getCurrentConversationModel();
245 auto conversation = Utils::getConversationFromUid(selectedConvUid, *convModel);
246 auto callList = CallModel::instance().getActiveCalls();
247 Call* thisCall = nullptr;
248 for (auto call : callList) {
249 if (call->historyId() == QString::fromStdString(conversation->callId)) {
250 thisCall = call;
251 break;
252 }
253 }
254 if (thisCall) {
255 outVideo = thisCall->firstMedia<media::Video>(media::Media::Direction::OUT);
Edric Milaretc4b1a102016-02-02 11:48:30 -0500256 if (outVideo)
257 activeIndex = outVideo->sourceModel()->activeIndex();
258 }
Edric Milaret029b95a2015-06-09 09:51:44 -0400259
Edric Milareta3e47282015-10-23 15:20:30 -0400260 for (auto device : Video::DeviceModel::instance().devices()) {
Edric Milaret029b95a2015-06-09 09:51:44 -0400261 std::unique_ptr<QAction> deviceAction(new QAction(device->name(), this));
262 deviceAction->setCheckable(true);
Edric Milaretc4b1a102016-02-02 11:48:30 -0500263 if (outVideo)
264 if (outVideo->sourceModel()->getDeviceIndex(device) == activeIndex)
265 deviceAction->setChecked(true);
Edric Milaret029b95a2015-06-09 09:51:44 -0400266 auto ptr = deviceAction.release();
267 menu.addAction(ptr);
268 connect(ptr, &QAction::toggled, [=](bool checked) {
269 if (checked == true) {
Edric Milaretc4b1a102016-02-02 11:48:30 -0500270 if (outVideo)
271 outVideo->sourceModel()->switchTo(device);
Edric Milareta3e47282015-10-23 15:20:30 -0400272 Video::DeviceModel::instance().setActive(device);
Edric Milaret029b95a2015-06-09 09:51:44 -0400273 }
274 });
275 }
276
277 menu.addSeparator();
278
Edric Milaret53ac6e52015-09-14 13:37:06 -0400279 auto shareAction = new QAction(tr("Share entire screen"), this);
Edric Milaret029b95a2015-06-09 09:51:44 -0400280 menu.addAction(shareAction);
Edric Milaretc4b1a102016-02-02 11:48:30 -0500281 shareAction->setCheckable(true);
Edric Milaret53ac6e52015-09-14 13:37:06 -0400282 auto shareAreaAction = new QAction(tr("Share screen area"), this);
Edric Milaret029b95a2015-06-09 09:51:44 -0400283 menu.addAction(shareAreaAction);
Edric Milaretc4b1a102016-02-02 11:48:30 -0500284 shareAreaAction->setCheckable(true);
Edric Milaret029b95a2015-06-09 09:51:44 -0400285 connect(shareAreaAction, &QAction::triggered, [=]() {
286 SelectAreaDialog selec;
287 selec.exec();
288 });
Edric Milaret53ac6e52015-09-14 13:37:06 -0400289 auto shareFileAction = new QAction(tr("Share file"), this);
Edric Milaret303bbf12015-06-22 14:24:13 -0400290 menu.addAction(shareFileAction);
Edric Milaretc4b1a102016-02-02 11:48:30 -0500291 shareFileAction->setCheckable(true);
292
293 switch(activeIndex) {
294
295 case Video::SourceModel::ExtendedDeviceList::SCREEN:
296 shareAction->setChecked(true);
297 break;
298 case Video::SourceModel::ExtendedDeviceList::FILE:
299 shareFileAction->setChecked(true);
300 break;
301
302 }
303
304 connect(shareAction, &QAction::triggered, [=]() {
Edricb09982a2016-05-19 16:28:38 -0400305 if (outVideo) {
306 auto realRect = QApplication::desktop()->geometry();
307 realRect.setWidth(static_cast<int>(realRect.width() * QApplication::primaryScreen()->devicePixelRatio()));
308 realRect.setHeight(static_cast<int>(realRect.height() * QApplication::primaryScreen()->devicePixelRatio()));
309 outVideo->sourceModel()->setDisplay(0, realRect);
310 }
Edric Milaretc4b1a102016-02-02 11:48:30 -0500311 });
Edric Milaret303bbf12015-06-22 14:24:13 -0400312 connect(shareFileAction, &QAction::triggered, [=]() {
313 QFileDialog dialog(this);
314 dialog.setFileMode(QFileDialog::AnyFile);
315 QStringList fileNames;
316 if (!dialog.exec())
317 return;
318 fileNames = dialog.selectedFiles();
Edric Milaretc4b1a102016-02-02 11:48:30 -0500319 if (outVideo)
320 outVideo->sourceModel()->setFile(QUrl::fromLocalFile(fileNames.at(0)));
Edric Milaret303bbf12015-06-22 14:24:13 -0400321 });
Edric Milaret029b95a2015-06-09 09:51:44 -0400322
323 menu.exec(globalPos);
324}
325
Edric Milaret80e0b212015-10-16 10:07:43 -0400326void
Andreas Traczyka493d5b2019-01-09 20:14:29 -0500327VideoView::pushRenderer(const std::string& callId) {
Andreas Traczykb8b13ba2018-08-21 16:30:16 -0400328 auto callModel = LRCInstance::getCurrentCallModel();
329
330 QObject::disconnect(videoStartedConnection_);
Andreas Traczyka493d5b2019-01-09 20:14:29 -0500331 if (!callModel->hasCall(callId)) {
Andreas Traczykb8b13ba2018-08-21 16:30:16 -0400332 return;
333 }
334
Andreas Traczyka493d5b2019-01-09 20:14:29 -0500335 auto call = callModel->getCall(callId);
336
337 this->overlay_->callStarted(callId);
338 this->overlay_->setVideoMuteVisibility(!LRCInstance::getCurrentCallModel()->getCall(callId).isAudioOnly);
Andreas Traczykb8b13ba2018-08-21 16:30:16 -0400339
340 videoStartedConnection_ = QObject::connect(callModel, &lrc::api::NewCallModel::remotePreviewStarted,
341 [this](const std::string& callId, Video::Renderer* renderer) {
342 Q_UNUSED(callId);
343 slotVideoStarted(renderer);
Andreas Traczykb8b13ba2018-08-21 16:30:16 -0400344 });
345 ui->videoWidget->setPreviewDisplay(call.type != lrc::api::call::Type::CONFERENCE);
346}
347
348void
Edric Milaret80e0b212015-10-16 10:07:43 -0400349VideoView::slotVideoStarted(Video::Renderer* renderer) {
350 ui->videoWidget->show();
351 ui->videoWidget->setDistantRenderer(renderer);
352}
Nicolas Jagerc7b8e4b2016-02-29 09:20:40 -0500353
354void
355VideoView::mousePressEvent(QMouseEvent* event)
356{
357 QPoint clickPosition = event->pos();
358 if (ui->videoWidget->getPreviewRect().contains(clickPosition)) {
359 QLine distance = QLine(clickPosition, ui->videoWidget->getPreviewRect().bottomRight());
360 if (distance.dy() < resizeGrip_ and distance.dx() < resizeGrip_) {
361 QApplication::setOverrideCursor(Qt::SizeFDiagCursor);
362 resizingPreview_ = true;
363 } else {
364 originMouseDisplacement_ = event->pos() - ui->videoWidget->getPreviewRect().topLeft();
365 QApplication::setOverrideCursor(Qt::SizeAllCursor);
366 draggingPreview_ = true;
367 }
368 }
369}
370
371void
372VideoView::mouseReleaseEvent(QMouseEvent* event)
373{
Edric Milaret6a785af2016-03-07 15:39:30 -0500374 Q_UNUSED(event)
375
Nicolas Jagerc7b8e4b2016-02-29 09:20:40 -0500376 draggingPreview_ = false;
377 resizingPreview_ = false;
378 QApplication::setOverrideCursor(Qt::ArrowCursor);
379}
380
381void
382VideoView::mouseMoveEvent(QMouseEvent* event)
383{
Andreas Traczykca320d82018-08-09 18:02:07 -0400384 // start/restart the timer after which the overlay will fade
385 if (fadeTimer_.isActive()) {
386 showOverlay();
387 } else {
388 fadeTimer_.start(startfadeOverlayTime_);
389 }
Andreas Traczykb8b13ba2018-08-21 16:30:16 -0400390
Nicolas Jagerc7b8e4b2016-02-29 09:20:40 -0500391 QRect& previewRect = ui->videoWidget->getPreviewRect();
392 if (draggingPreview_) {
393 if (previewRect.left() > 0
394 && previewRect.top() > 0
395 && previewRect.right() < width()
396 && previewRect.bottom() < height()) {
397
398 previewRect.moveTo(event->pos() - originMouseDisplacement_);
399 if (previewRect.left() <= 0)
400 previewRect.moveLeft(1);
401
402 if (previewRect.right() >= width())
403 previewRect.moveRight(width() - 1);
404
405 if (previewRect.top() <= 0)
406 previewRect.moveTop(1);
407
408 if (previewRect.bottom() >= height())
409 previewRect.moveBottom(height() - 1);
410 }
411 }
412
413 QLine distance = QLine(previewRect.topLeft(), event->pos());
414
415 if (resizingPreview_
416 and distance.dx() > minimalSize_
417 and distance.dy() > minimalSize_
418 and geometry().contains(event->pos()))
419 previewRect.setBottomRight(event->pos());
420}