videoview.cpp 13.2 KB
Newer Older
1
/***************************************************************************
2
 * Copyright (C) 2015-2017 by Savoir-faire Linux                           *
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * Author: Edric Ladent Milaret <edric.ladent-milaret@savoirfairelinux.com>*
 *                                                                         *
 * This program is free software; you can redistribute it and/or modify    *
 * it under the terms of the GNU General Public License as published by    *
 * the Free Software Foundation; either version 3 of the License, or       *
 * (at your option) any later version.                                     *
 *                                                                         *
 * This program is distributed in the hope that it will be useful,         *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 * GNU General Public License for more details.                            *
 *                                                                         *
 * You should have received a copy of the GNU General Public License       *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.   *
 **************************************************************************/

#include "videoview.h"
#include "ui_videoview.h"

22 23 24
#include "utils.h"
#include "lrcinstance.h"

25 26 27 28
#include <QGraphicsOpacityEffect>
#include <QPropertyAnimation>
#include <QDesktopWidget>
#include <QMenu>
29 30
#include <QFileDialog>
#include <QMimeData>
31
#include <QSplitter>
Edric's avatar
Edric committed
32
#include <QScreen>
33 34 35 36 37 38

#include <memory>

#include "videooverlay.h"
#include "selectareadialog.h"

39
VideoView::VideoView(QWidget* parent) :
40 41 42 43 44 45 46
    QWidget(parent),
    ui(new Ui::VideoView)
{
    ui->setupUi(this);

    overlay_ = new VideoOverlay(this);
    auto effect = new QGraphicsOpacityEffect(overlay_);
47
    effect->setOpacity(maxOverlayOpacity_);
48 49 50 51 52 53 54 55
    overlay_->setGraphicsEffect(effect);
    fadeAnim_ = new QPropertyAnimation(this);
    fadeAnim_->setTargetObject(effect);
    fadeAnim_->setPropertyName("opacity");
    fadeAnim_->setDuration(fadeOverlayTime_);
    fadeAnim_->setStartValue(effect->opacity());
    fadeAnim_->setEndValue(0);
    fadeAnim_->setEasingCurve(QEasingCurve::OutQuad);
56

57 58 59 60 61
    // Setup the timer to start the fade when the mouse stops moving
    this->setMouseTracking(true);
    overlay_->setMouseTracking(true);
    fadeTimer_.setSingleShot(true);
    connect(&fadeTimer_, SIGNAL(timeout()), this, SLOT(fadeOverlayOut()));
62 63 64 65

    this->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(this, SIGNAL(customContextMenuRequested(const QPoint&)),
        this, SLOT(showContextMenu(const QPoint&)));
66 67 68
    connect(overlay_, &VideoOverlay::setChatVisibility, [=](bool visible) {
        emit this->setChatVisibility(visible);
    });
69

70 71 72 73 74
}

VideoView::~VideoView()
{
    delete ui;
Edric Milaret's avatar
Edric Milaret committed
75 76
    delete overlay_;
    delete fadeAnim_;
77 78 79
}

void
80
VideoView::resizeEvent(QResizeEvent* event)
81
{
82 83 84 85 86
    QRect& previewRect = ui->videoWidget->getPreviewRect();
    int deltaW = event->size().width() - event->oldSize().width();
    int deltaH = event->size().height() - event->oldSize().height();

    QPoint previewCenter = ui->videoWidget->getPreviewRect().center();
Edric's avatar
Edric committed
87 88
    int cx = (event->oldSize().width()) / 2;
    int cy = (event->oldSize().height()) / 2;
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    QPoint center = QPoint(cx, cy);

    // first we check if we want to displace the preview
    if (previewRect.x() + deltaW > 0 && previewRect.y() + deltaH > 0) {
        // then we check which way
        if (center.x() - previewCenter.x() < 0 && center.y() - previewCenter.y() < 0)
            ui->videoWidget->getPreviewRect().translate(deltaW, deltaH);
        else if (center.x() - previewCenter.x() > 0 && center.y() - previewCenter.y() < 0)
            ui->videoWidget->getPreviewRect().translate(0, deltaH);
        else if (center.x() - previewCenter.x() < 0 && center.y() - previewCenter.y() > 0)
            ui->videoWidget->getPreviewRect().translate(deltaW, 0);
    }

    if (previewRect.left() <= 0)
        previewRect.moveLeft(1);

    if (previewRect.right() >= width())
        previewRect.moveRight(width() - 1);

    if (previewRect.top() <= 0)
        previewRect.moveTop(1);

    if (previewRect.bottom() >= height())
        previewRect.moveBottom(height() - 1);

114 115 116 117 118 119 120 121 122
    overlay_->resize(this->size());
    overlay_->show();
    overlay_->raise();
}

void
VideoView::enterEvent(QEvent* event)
{
    Q_UNUSED(event)
123
    showOverlay();
124 125 126 127 128 129
}

void
VideoView::leaveEvent(QEvent* event)
{
    Q_UNUSED(event)
130 131 132 133 134 135 136 137 138 139 140 141 142
    fadeOverlayOut();
}

void
VideoView::showOverlay()
{
    fadeAnim_->stop();
    fadeAnim_->targetObject()->setProperty(fadeAnim_->propertyName(), fadeAnim_->startValue());
}

void
VideoView::fadeOverlayOut()
{
143
    if (!overlay_->isDialogVisible() && !overlay_->shouldShowOverlay()) {
144
        fadeAnim_->start(QAbstractAnimation::KeepWhenStopped);
145
    }
146 147 148
}

void
149
VideoView::slotCallStatusChanged(const std::string& callId)
150
{
151 152 153 154 155
    using namespace lrc::api::call;
    auto call = LRCInstance::getCurrentCallModel()->getCall(callId);
    switch (call.status) {
    case Status::IN_PROGRESS:
    {
156
        ui->videoWidget->show();
157 158 159 160 161
        auto convInfo = Utils::getConversationFromCallId(call.id);
        if (!convInfo.uid.empty()) {
            auto contactInfo = LRCInstance::getCurrentAccountInfo().contactModel->getContact(convInfo.participants[0]);
            auto contactName = Utils::bestNameForContact(contactInfo);
            overlay_->setName(QString::fromStdString(contactName));
162
        }
163
        return;
164
    }
165 166 167 168 169
    default:
        emit closing(call.id);
        break;
    }
    QObject::disconnect(timerConnection_);
170 171
}

172 173 174 175 176 177
void
VideoView::showChatviewIfToggled()
{
    emit setChatVisibility(overlay_->getShowChatView());
}

178 179 180
void
VideoView::simulateShowChatview(bool checked)
{
181
    Q_UNUSED(checked);
182 183 184
    overlay_->simulateShowChatview(true);
}

185 186 187 188 189 190
void
VideoView::mouseDoubleClickEvent(QMouseEvent* e) {
    QWidget::mouseDoubleClickEvent(e);
    toggleFullScreen();
}

191 192
void
VideoView::dragEnterEvent(QDragEnterEvent* event)
193 194 195 196 197
{
    if (event->mimeData()->hasUrls())
        event->acceptProposedAction();
}

198 199
void
VideoView::dropEvent(QDropEvent* event)
200 201
{
    auto urls = event->mimeData()->urls();
202 203 204 205 206 207 208 209 210 211 212 213 214
    auto selectedConvUid = LRCInstance::getSelectedConvUid();
    auto convModel = LRCInstance::getCurrentConversationModel();
    auto conversation = Utils::getConversationFromUid(selectedConvUid, *convModel);
    auto callList = CallModel::instance().getActiveCalls();
    Call* thisCall = nullptr;
    for (auto call : callList) {
        if (call->historyId() == QString::fromStdString(conversation->callId)) {
            thisCall = call;
            break;
        }
    }
    if (thisCall) {
        if (auto outVideo = thisCall->firstMedia<media::Video>(media::Media::Direction::OUT)) {
215 216 217
            outVideo->sourceModel()->setFile(urls.at(0));
        }
    }
218 219
}

220 221 222
void
VideoView::toggleFullScreen()
{
223
    emit toggleFullScreenClicked();
224 225 226 227 228 229 230 231
}

void
VideoView::showContextMenu(const QPoint& pos)
{
    QPoint globalPos = this->mapToGlobal(pos);

    QMenu menu;
232
    media::Video* outVideo = nullptr;
233 234
    int activeIndex = -1;

235 236 237 238 239 240 241 242 243 244 245 246 247
    auto selectedConvUid = LRCInstance::getSelectedConvUid();
    auto convModel = LRCInstance::getCurrentConversationModel();
    auto conversation = Utils::getConversationFromUid(selectedConvUid, *convModel);
    auto callList = CallModel::instance().getActiveCalls();
    Call* thisCall = nullptr;
    for (auto call : callList) {
        if (call->historyId() == QString::fromStdString(conversation->callId)) {
            thisCall = call;
            break;
        }
    }
    if (thisCall) {
        outVideo = thisCall->firstMedia<media::Video>(media::Media::Direction::OUT);
248 249 250
        if (outVideo)
            activeIndex = outVideo->sourceModel()->activeIndex();
    }
251

Edric Milaret's avatar
Edric Milaret committed
252
    for (auto device : Video::DeviceModel::instance().devices()) {
253 254
        std::unique_ptr<QAction> deviceAction(new QAction(device->name(), this));
        deviceAction->setCheckable(true);
255 256 257
        if (outVideo)
            if (outVideo->sourceModel()->getDeviceIndex(device) == activeIndex)
                deviceAction->setChecked(true);
258 259 260 261
        auto ptr = deviceAction.release();
        menu.addAction(ptr);
        connect(ptr, &QAction::toggled, [=](bool checked) {
            if (checked == true) {
262 263
                if (outVideo)
                    outVideo->sourceModel()->switchTo(device);
Edric Milaret's avatar
Edric Milaret committed
264
                Video::DeviceModel::instance().setActive(device);
265 266 267 268 269 270
            }
        });
    }

    menu.addSeparator();

271
    auto shareAction = new QAction(tr("Share entire screen"), this);
272
    menu.addAction(shareAction);
273
    shareAction->setCheckable(true);
274
    auto shareAreaAction = new QAction(tr("Share screen area"), this);
275
    menu.addAction(shareAreaAction);
276
    shareAreaAction->setCheckable(true);
277 278 279 280
    connect(shareAreaAction, &QAction::triggered, [=]() {
        SelectAreaDialog selec;
        selec.exec();
    });
281
    auto shareFileAction = new QAction(tr("Share file"), this);
282
    menu.addAction(shareFileAction);
283 284 285 286 287 288 289 290 291 292 293 294 295 296
    shareFileAction->setCheckable(true);

    switch(activeIndex) {

    case Video::SourceModel::ExtendedDeviceList::SCREEN:
        shareAction->setChecked(true);
        break;
    case Video::SourceModel::ExtendedDeviceList::FILE:
        shareFileAction->setChecked(true);
        break;

    }

    connect(shareAction, &QAction::triggered, [=]() {
Edric's avatar
Edric committed
297 298 299 300 301 302
        if (outVideo) {
            auto realRect = QApplication::desktop()->geometry();
            realRect.setWidth(static_cast<int>(realRect.width() * QApplication::primaryScreen()->devicePixelRatio()));
            realRect.setHeight(static_cast<int>(realRect.height() * QApplication::primaryScreen()->devicePixelRatio()));
            outVideo->sourceModel()->setDisplay(0, realRect);
        }
303
    });
304 305 306 307 308 309 310
    connect(shareFileAction, &QAction::triggered, [=]() {
        QFileDialog dialog(this);
        dialog.setFileMode(QFileDialog::AnyFile);
        QStringList fileNames;
        if (!dialog.exec())
            return;
        fileNames = dialog.selectedFiles();
311 312
        if (outVideo)
            outVideo->sourceModel()->setFile(QUrl::fromLocalFile(fileNames.at(0)));
313
    });
314 315 316 317

    menu.exec(globalPos);
}

318
void
319
VideoView::pushRenderer(const std::string& callId) {
320 321 322
    auto callModel = LRCInstance::getCurrentCallModel();

    QObject::disconnect(videoStartedConnection_);
323 324
    QObject::disconnect(callStatusChangedConnection_);

325
    if (!callModel->hasCall(callId)) {
326 327 328
        return;
    }

329 330 331 332
    auto call = callModel->getCall(callId);

    this->overlay_->callStarted(callId);
    this->overlay_->setVideoMuteVisibility(!LRCInstance::getCurrentCallModel()->getCall(callId).isAudioOnly);
333

334 335 336
    callStatusChangedConnection_ = QObject::connect(callModel, &lrc::api::NewCallModel::callStatusChanged,
        this, &VideoView::slotCallStatusChanged);

337 338 339 340 341 342 343 344
    videoStartedConnection_ = QObject::connect(callModel, &lrc::api::NewCallModel::remotePreviewStarted,
        [this](const std::string& callId, Video::Renderer* renderer) {
            Q_UNUSED(callId);
            slotVideoStarted(renderer);
        });
    ui->videoWidget->setPreviewDisplay(call.type != lrc::api::call::Type::CONFERENCE);
}

345 346 347 348 349
void
VideoView::slotVideoStarted(Video::Renderer* renderer) {
    ui->videoWidget->show();
    ui->videoWidget->setDistantRenderer(renderer);
}
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370

void
VideoView::mousePressEvent(QMouseEvent* event)
{
    QPoint clickPosition = event->pos();
    if (ui->videoWidget->getPreviewRect().contains(clickPosition)) {
        QLine distance = QLine(clickPosition, ui->videoWidget->getPreviewRect().bottomRight());
        if (distance.dy() < resizeGrip_ and distance.dx() < resizeGrip_) {
            QApplication::setOverrideCursor(Qt::SizeFDiagCursor);
            resizingPreview_ = true;
        } else {
            originMouseDisplacement_ = event->pos() - ui->videoWidget->getPreviewRect().topLeft();
            QApplication::setOverrideCursor(Qt::SizeAllCursor);
            draggingPreview_ = true;
        }
    }
}

void
VideoView::mouseReleaseEvent(QMouseEvent* event)
{
371 372
    Q_UNUSED(event)

373 374 375 376 377 378 379 380
    draggingPreview_ = false;
    resizingPreview_ = false;
    QApplication::setOverrideCursor(Qt::ArrowCursor);
}

void
VideoView::mouseMoveEvent(QMouseEvent* event)
{
381 382 383 384 385 386
    // start/restart the timer after which the overlay will fade
    if (fadeTimer_.isActive()) {
        showOverlay();
    } else {
        fadeTimer_.start(startfadeOverlayTime_);
    }
387

388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
    QRect& previewRect =  ui->videoWidget->getPreviewRect();
    if (draggingPreview_) {
        if (previewRect.left() > 0
                && previewRect.top() > 0
                && previewRect.right() < width()
                && previewRect.bottom() < height()) {

            previewRect.moveTo(event->pos() - originMouseDisplacement_);
            if (previewRect.left() <= 0)
                previewRect.moveLeft(1);

            if (previewRect.right() >= width())
                previewRect.moveRight(width() - 1);

            if (previewRect.top() <= 0)
                previewRect.moveTop(1);

            if (previewRect.bottom() >= height())
                previewRect.moveBottom(height() - 1);
        }
    }

    QLine distance = QLine(previewRect.topLeft(), event->pos());

    if (resizingPreview_
            and distance.dx() > minimalSize_
            and distance.dy() > minimalSize_
            and geometry().contains(event->pos()))
        previewRect.setBottomRight(event->pos());
}