blob: 834f6b39946ecea5af896f1328675af7dcb414e9 [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();});
Edric Milaret029b95a2015-06-09 09:51:44 -040073}
74
75VideoView::~VideoView()
76{
77 delete ui;
Edric Milaret01f23842015-06-22 14:46:01 -040078 delete overlay_;
Edric Milaret01f23842015-06-22 14:46:01 -040079 delete fadeAnim_;
Edric Milaret029b95a2015-06-09 09:51:44 -040080}
81
82void
Edric Milaret80e0b212015-10-16 10:07:43 -040083VideoView::resizeEvent(QResizeEvent* event)
Edric Milaret029b95a2015-06-09 09:51:44 -040084{
Nicolas Jagerc7b8e4b2016-02-29 09:20:40 -050085 QRect& previewRect = ui->videoWidget->getPreviewRect();
86 int deltaW = event->size().width() - event->oldSize().width();
87 int deltaH = event->size().height() - event->oldSize().height();
88
89 QPoint previewCenter = ui->videoWidget->getPreviewRect().center();
Edricb09982a2016-05-19 16:28:38 -040090 int cx = (event->oldSize().width()) / 2;
91 int cy = (event->oldSize().height()) / 2;
Nicolas Jagerc7b8e4b2016-02-29 09:20:40 -050092 QPoint center = QPoint(cx, cy);
93
94 // first we check if we want to displace the preview
95 if (previewRect.x() + deltaW > 0 && previewRect.y() + deltaH > 0) {
96 // then we check which way
97 if (center.x() - previewCenter.x() < 0 && center.y() - previewCenter.y() < 0)
98 ui->videoWidget->getPreviewRect().translate(deltaW, deltaH);
99 else if (center.x() - previewCenter.x() > 0 && center.y() - previewCenter.y() < 0)
100 ui->videoWidget->getPreviewRect().translate(0, deltaH);
101 else if (center.x() - previewCenter.x() < 0 && center.y() - previewCenter.y() > 0)
102 ui->videoWidget->getPreviewRect().translate(deltaW, 0);
103 }
104
105 if (previewRect.left() <= 0)
106 previewRect.moveLeft(1);
107
108 if (previewRect.right() >= width())
109 previewRect.moveRight(width() - 1);
110
111 if (previewRect.top() <= 0)
112 previewRect.moveTop(1);
113
114 if (previewRect.bottom() >= height())
115 previewRect.moveBottom(height() - 1);
116
Edric Milaret029b95a2015-06-09 09:51:44 -0400117 overlay_->resize(this->size());
118 overlay_->show();
119 overlay_->raise();
120}
121
122void
123VideoView::enterEvent(QEvent* event)
124{
125 Q_UNUSED(event)
Andreas Traczykca320d82018-08-09 18:02:07 -0400126 showOverlay();
Edric Milaret029b95a2015-06-09 09:51:44 -0400127}
128
129void
130VideoView::leaveEvent(QEvent* event)
131{
132 Q_UNUSED(event)
Andreas Traczykca320d82018-08-09 18:02:07 -0400133 fadeOverlayOut();
134}
135
136void
137VideoView::showOverlay()
138{
139 fadeAnim_->stop();
140 fadeAnim_->targetObject()->setProperty(fadeAnim_->propertyName(), fadeAnim_->startValue());
141}
142
143void
144VideoView::fadeOverlayOut()
145{
Andreas Traczykb8b13ba2018-08-21 16:30:16 -0400146 if (!overlay_->isDialogVisible() && !overlay_->shouldShowOverlay()) {
Nicolas Jager0a9fc602016-03-11 18:35:42 -0500147 fadeAnim_->start(QAbstractAnimation::KeepWhenStopped);
Andreas Traczykb8b13ba2018-08-21 16:30:16 -0400148 }
Edric Milaret029b95a2015-06-09 09:51:44 -0400149}
150
151void
152VideoView::callStateChanged(Call* call, Call::State previousState)
153{
154 Q_UNUSED(previousState)
Edric Milaret80e0b212015-10-16 10:07:43 -0400155
Edric Milaret029b95a2015-06-09 09:51:44 -0400156 if (call->state() == Call::State::CURRENT) {
157 ui->videoWidget->show();
Edric Milareted0b2802015-10-01 15:10:02 -0400158 timerConnection_ = connect(call, SIGNAL(changed()), this, SLOT(updateCall()));
SĂ©bastien Blin93bd2062018-12-17 15:57:16 -0500159 } else {
Edric Milaret80e0b212015-10-16 10:07:43 -0400160 QObject::disconnect(timerConnection_);
Edric Milaret3aca8e32015-06-12 10:01:40 -0400161 emit setChatVisibility(false);
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());
190 overlay_->setTime(call->length());
191 }
Edric Milaret029b95a2015-06-09 09:51:44 -0400192}
193
194void
195VideoView::mouseDoubleClickEvent(QMouseEvent* e) {
196 QWidget::mouseDoubleClickEvent(e);
197 toggleFullScreen();
198}
199
Edric Milaret80e0b212015-10-16 10:07:43 -0400200void
201VideoView::dragEnterEvent(QDragEnterEvent* event)
Edric Milaret303bbf12015-06-22 14:24:13 -0400202{
203 if (event->mimeData()->hasUrls())
204 event->acceptProposedAction();
205}
206
Edric Milaret80e0b212015-10-16 10:07:43 -0400207void
208VideoView::dropEvent(QDropEvent* event)
Edric Milaret303bbf12015-06-22 14:24:13 -0400209{
210 auto urls = event->mimeData()->urls();
Andreas Traczykb8b13ba2018-08-21 16:30:16 -0400211 auto selectedConvUid = LRCInstance::getSelectedConvUid();
212 auto convModel = LRCInstance::getCurrentConversationModel();
213 auto conversation = Utils::getConversationFromUid(selectedConvUid, *convModel);
214 auto callList = CallModel::instance().getActiveCalls();
215 Call* thisCall = nullptr;
216 for (auto call : callList) {
217 if (call->historyId() == QString::fromStdString(conversation->callId)) {
218 thisCall = call;
219 break;
220 }
221 }
222 if (thisCall) {
223 if (auto outVideo = thisCall->firstMedia<media::Video>(media::Media::Direction::OUT)) {
Edric Milaret0e1074d2015-12-08 12:08:52 -0500224 outVideo->sourceModel()->setFile(urls.at(0));
225 }
226 }
Edric Milaret303bbf12015-06-22 14:24:13 -0400227}
228
Edric Milaret029b95a2015-06-09 09:51:44 -0400229void
230VideoView::toggleFullScreen()
231{
SĂ©bastien Blin93bd2062018-12-17 15:57:16 -0500232 emit toggleFullScreenClicked();
Edric Milaret029b95a2015-06-09 09:51:44 -0400233}
234
235void
236VideoView::showContextMenu(const QPoint& pos)
237{
238 QPoint globalPos = this->mapToGlobal(pos);
239
240 QMenu menu;
Andreas Traczyke86fb3c2018-08-02 17:38:34 -0400241 media::Video* outVideo = nullptr;
Edric Milaretc4b1a102016-02-02 11:48:30 -0500242 int activeIndex = -1;
243
Andreas Traczykb8b13ba2018-08-21 16:30:16 -0400244 auto selectedConvUid = LRCInstance::getSelectedConvUid();
245 auto convModel = LRCInstance::getCurrentConversationModel();
246 auto conversation = Utils::getConversationFromUid(selectedConvUid, *convModel);
247 auto callList = CallModel::instance().getActiveCalls();
248 Call* thisCall = nullptr;
249 for (auto call : callList) {
250 if (call->historyId() == QString::fromStdString(conversation->callId)) {
251 thisCall = call;
252 break;
253 }
254 }
255 if (thisCall) {
256 outVideo = thisCall->firstMedia<media::Video>(media::Media::Direction::OUT);
Edric Milaretc4b1a102016-02-02 11:48:30 -0500257 if (outVideo)
258 activeIndex = outVideo->sourceModel()->activeIndex();
259 }
Edric Milaret029b95a2015-06-09 09:51:44 -0400260
Edric Milareta3e47282015-10-23 15:20:30 -0400261 for (auto device : Video::DeviceModel::instance().devices()) {
Edric Milaret029b95a2015-06-09 09:51:44 -0400262 std::unique_ptr<QAction> deviceAction(new QAction(device->name(), this));
263 deviceAction->setCheckable(true);
Edric Milaretc4b1a102016-02-02 11:48:30 -0500264 if (outVideo)
265 if (outVideo->sourceModel()->getDeviceIndex(device) == activeIndex)
266 deviceAction->setChecked(true);
Edric Milaret029b95a2015-06-09 09:51:44 -0400267 auto ptr = deviceAction.release();
268 menu.addAction(ptr);
269 connect(ptr, &QAction::toggled, [=](bool checked) {
270 if (checked == true) {
Edric Milaretc4b1a102016-02-02 11:48:30 -0500271 if (outVideo)
272 outVideo->sourceModel()->switchTo(device);
Edric Milareta3e47282015-10-23 15:20:30 -0400273 Video::DeviceModel::instance().setActive(device);
Edric Milaret029b95a2015-06-09 09:51:44 -0400274 }
275 });
276 }
277
278 menu.addSeparator();
279
Edric Milaret53ac6e52015-09-14 13:37:06 -0400280 auto shareAction = new QAction(tr("Share entire screen"), this);
Edric Milaret029b95a2015-06-09 09:51:44 -0400281 menu.addAction(shareAction);
Edric Milaretc4b1a102016-02-02 11:48:30 -0500282 shareAction->setCheckable(true);
Edric Milaret53ac6e52015-09-14 13:37:06 -0400283 auto shareAreaAction = new QAction(tr("Share screen area"), this);
Edric Milaret029b95a2015-06-09 09:51:44 -0400284 menu.addAction(shareAreaAction);
Edric Milaretc4b1a102016-02-02 11:48:30 -0500285 shareAreaAction->setCheckable(true);
Edric Milaret029b95a2015-06-09 09:51:44 -0400286 connect(shareAreaAction, &QAction::triggered, [=]() {
287 SelectAreaDialog selec;
288 selec.exec();
289 });
Edric Milaret53ac6e52015-09-14 13:37:06 -0400290 auto shareFileAction = new QAction(tr("Share file"), this);
Edric Milaret303bbf12015-06-22 14:24:13 -0400291 menu.addAction(shareFileAction);
Edric Milaretc4b1a102016-02-02 11:48:30 -0500292 shareFileAction->setCheckable(true);
293
294 switch(activeIndex) {
295
296 case Video::SourceModel::ExtendedDeviceList::SCREEN:
297 shareAction->setChecked(true);
298 break;
299 case Video::SourceModel::ExtendedDeviceList::FILE:
300 shareFileAction->setChecked(true);
301 break;
302
303 }
304
305 connect(shareAction, &QAction::triggered, [=]() {
Edricb09982a2016-05-19 16:28:38 -0400306 if (outVideo) {
307 auto realRect = QApplication::desktop()->geometry();
308 realRect.setWidth(static_cast<int>(realRect.width() * QApplication::primaryScreen()->devicePixelRatio()));
309 realRect.setHeight(static_cast<int>(realRect.height() * QApplication::primaryScreen()->devicePixelRatio()));
310 outVideo->sourceModel()->setDisplay(0, realRect);
311 }
Edric Milaretc4b1a102016-02-02 11:48:30 -0500312 });
Edric Milaret303bbf12015-06-22 14:24:13 -0400313 connect(shareFileAction, &QAction::triggered, [=]() {
314 QFileDialog dialog(this);
315 dialog.setFileMode(QFileDialog::AnyFile);
316 QStringList fileNames;
317 if (!dialog.exec())
318 return;
319 fileNames = dialog.selectedFiles();
Edric Milaretc4b1a102016-02-02 11:48:30 -0500320 if (outVideo)
321 outVideo->sourceModel()->setFile(QUrl::fromLocalFile(fileNames.at(0)));
Edric Milaret303bbf12015-06-22 14:24:13 -0400322 });
Edric Milaret029b95a2015-06-09 09:51:44 -0400323
324 menu.exec(globalPos);
325}
326
Edric Milaret80e0b212015-10-16 10:07:43 -0400327void
Andreas Traczykb8b13ba2018-08-21 16:30:16 -0400328VideoView::pushRenderer(const std::string& callUid) {
329 auto callModel = LRCInstance::getCurrentCallModel();
330
331 QObject::disconnect(videoStartedConnection_);
332 if (!callModel->hasCall(callUid)) {
333 return;
334 }
335
336 auto call = callModel->getCall(callUid);
337
338 videoStartedConnection_ = QObject::connect(callModel, &lrc::api::NewCallModel::remotePreviewStarted,
339 [this](const std::string& callId, Video::Renderer* renderer) {
340 Q_UNUSED(callId);
341 slotVideoStarted(renderer);
Andreas Traczyk7548e732018-12-12 10:47:21 -0500342 this->overlay_->setVideoMuteVisibility(!LRCInstance::getCurrentCallModel()->getCall(callId).isAudioOnly);
Andreas Traczykb8b13ba2018-08-21 16:30:16 -0400343 });
344 ui->videoWidget->setPreviewDisplay(call.type != lrc::api::call::Type::CONFERENCE);
345}
346
347void
Edric Milaret80e0b212015-10-16 10:07:43 -0400348VideoView::slotVideoStarted(Video::Renderer* renderer) {
349 ui->videoWidget->show();
350 ui->videoWidget->setDistantRenderer(renderer);
351}
Nicolas Jagerc7b8e4b2016-02-29 09:20:40 -0500352
353void
354VideoView::mousePressEvent(QMouseEvent* event)
355{
356 QPoint clickPosition = event->pos();
357 if (ui->videoWidget->getPreviewRect().contains(clickPosition)) {
358 QLine distance = QLine(clickPosition, ui->videoWidget->getPreviewRect().bottomRight());
359 if (distance.dy() < resizeGrip_ and distance.dx() < resizeGrip_) {
360 QApplication::setOverrideCursor(Qt::SizeFDiagCursor);
361 resizingPreview_ = true;
362 } else {
363 originMouseDisplacement_ = event->pos() - ui->videoWidget->getPreviewRect().topLeft();
364 QApplication::setOverrideCursor(Qt::SizeAllCursor);
365 draggingPreview_ = true;
366 }
367 }
368}
369
370void
371VideoView::mouseReleaseEvent(QMouseEvent* event)
372{
Edric Milaret6a785af2016-03-07 15:39:30 -0500373 Q_UNUSED(event)
374
Nicolas Jagerc7b8e4b2016-02-29 09:20:40 -0500375 draggingPreview_ = false;
376 resizingPreview_ = false;
377 QApplication::setOverrideCursor(Qt::ArrowCursor);
378}
379
380void
381VideoView::mouseMoveEvent(QMouseEvent* event)
382{
Andreas Traczykca320d82018-08-09 18:02:07 -0400383 // start/restart the timer after which the overlay will fade
384 if (fadeTimer_.isActive()) {
385 showOverlay();
386 } else {
387 fadeTimer_.start(startfadeOverlayTime_);
388 }
Andreas Traczykb8b13ba2018-08-21 16:30:16 -0400389
Nicolas Jagerc7b8e4b2016-02-29 09:20:40 -0500390 QRect& previewRect = ui->videoWidget->getPreviewRect();
391 if (draggingPreview_) {
392 if (previewRect.left() > 0
393 && previewRect.top() > 0
394 && previewRect.right() < width()
395 && previewRect.bottom() < height()) {
396
397 previewRect.moveTo(event->pos() - originMouseDisplacement_);
398 if (previewRect.left() <= 0)
399 previewRect.moveLeft(1);
400
401 if (previewRect.right() >= width())
402 previewRect.moveRight(width() - 1);
403
404 if (previewRect.top() <= 0)
405 previewRect.moveTop(1);
406
407 if (previewRect.bottom() >= height())
408 previewRect.moveBottom(height() - 1);
409 }
410 }
411
412 QLine distance = QLine(previewRect.topLeft(), event->pos());
413
414 if (resizingPreview_
415 and distance.dx() > minimalSize_
416 and distance.dy() > minimalSize_
417 and geometry().contains(event->pos()))
418 previewRect.setBottomRight(event->pos());
419}