Edric Milaret | 029b95a | 2015-06-09 09:51:44 -0400 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2015 by Savoir-Faire Linux * |
| 3 | * 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 Milaret | 303bbf1 | 2015-06-22 14:24:13 -0400 | [diff] [blame] | 30 | #include <QFileDialog> |
| 31 | #include <QMimeData> |
Edric Milaret | 029b95a | 2015-06-09 09:51:44 -0400 | [diff] [blame] | 32 | |
| 33 | #include <memory> |
| 34 | |
| 35 | #include "videooverlay.h" |
| 36 | #include "selectareadialog.h" |
| 37 | |
| 38 | VideoView::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 Milaret | 3aca8e3 | 2015-06-12 10:01:40 -0400 | [diff] [blame] | 65 | connect(overlay_, &VideoOverlay::setChatVisibility, [=](bool visible) { |
| 66 | emit this->setChatVisibility(visible); |
| 67 | }); |
Edric Milaret | 029b95a | 2015-06-09 09:51:44 -0400 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | VideoView::~VideoView() |
| 71 | { |
| 72 | delete ui; |
Edric Milaret | 01f2384 | 2015-06-22 14:46:01 -0400 | [diff] [blame] | 73 | delete overlay_; |
| 74 | delete timerLength_; |
| 75 | delete fadeAnim_; |
Edric Milaret | 029b95a | 2015-06-09 09:51:44 -0400 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void |
| 79 | VideoView::resizeEvent(QResizeEvent *event) |
| 80 | { |
| 81 | Q_UNUSED(event) |
| 82 | overlay_->resize(this->size()); |
| 83 | overlay_->show(); |
| 84 | overlay_->raise(); |
| 85 | } |
| 86 | |
| 87 | void |
| 88 | VideoView::enterEvent(QEvent* event) |
| 89 | { |
| 90 | Q_UNUSED(event) |
| 91 | fadeAnim_->stop(); |
| 92 | fadeAnim_->targetObject()->setProperty(fadeAnim_->propertyName(), fadeAnim_->startValue()); |
| 93 | } |
| 94 | |
| 95 | void |
| 96 | VideoView::leaveEvent(QEvent* event) |
| 97 | { |
| 98 | Q_UNUSED(event) |
| 99 | fadeAnim_->start(QAbstractAnimation::KeepWhenStopped); |
| 100 | } |
| 101 | |
| 102 | void |
| 103 | VideoView::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 Milaret | 3aca8e3 | 2015-06-12 10:01:40 -0400 | [diff] [blame] | 112 | emit setChatVisibility(false); |
Edric Milaret | 029b95a | 2015-06-09 09:51:44 -0400 | [diff] [blame] | 113 | ui->videoWidget->hide(); |
| 114 | if (isFullScreen()) |
| 115 | toggleFullScreen(); |
| 116 | timerLength_->stop(); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | void |
| 121 | VideoView::updateTimer() |
| 122 | { |
| 123 | overlay_->setTime(CallModel::instance()->selectedCall()->length()); |
| 124 | } |
| 125 | |
| 126 | void |
| 127 | VideoView::mouseDoubleClickEvent(QMouseEvent* e) { |
| 128 | QWidget::mouseDoubleClickEvent(e); |
| 129 | toggleFullScreen(); |
| 130 | } |
| 131 | |
Edric Milaret | 303bbf1 | 2015-06-22 14:24:13 -0400 | [diff] [blame] | 132 | void VideoView::dragEnterEvent(QDragEnterEvent *event) |
| 133 | { |
| 134 | if (event->mimeData()->hasUrls()) |
| 135 | event->acceptProposedAction(); |
| 136 | } |
| 137 | |
| 138 | void VideoView::dropEvent(QDropEvent *event) |
| 139 | { |
| 140 | auto urls = event->mimeData()->urls(); |
| 141 | Video::SourceModel::instance()->setFile(urls.at(0)); |
| 142 | } |
| 143 | |
Edric Milaret | 029b95a | 2015-06-09 09:51:44 -0400 | [diff] [blame] | 144 | void |
| 145 | VideoView::toggleFullScreen() |
| 146 | { |
| 147 | if(isFullScreen()) { |
| 148 | this->setParent(oldParent_); |
Edric Milaret | b46f649 | 2015-09-25 15:28:31 -0400 | [diff] [blame] | 149 | oldParent_->layout()->addWidget(this); |
Edric Milaret | 029b95a | 2015-06-09 09:51:44 -0400 | [diff] [blame] | 150 | this->resize(oldSize_.width(), oldSize_.height()); |
Edric Milaret | b46f649 | 2015-09-25 15:28:31 -0400 | [diff] [blame] | 151 | this->show(); |
Edric Milaret | 029b95a | 2015-06-09 09:51:44 -0400 | [diff] [blame] | 152 | } 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 | |
| 161 | void |
| 162 | VideoView::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 Milaret | 53ac6e5 | 2015-09-14 13:37:06 -0400 | [diff] [blame] | 185 | auto shareAction = new QAction(tr("Share entire screen"), this); |
Edric Milaret | 029b95a | 2015-06-09 09:51:44 -0400 | [diff] [blame] | 186 | menu.addAction(shareAction); |
| 187 | connect(shareAction, &QAction::triggered, [=]() { |
| 188 | Video::SourceModel::instance()->setDisplay(0, QApplication::desktop()->rect()); |
| 189 | }); |
Edric Milaret | 53ac6e5 | 2015-09-14 13:37:06 -0400 | [diff] [blame] | 190 | auto shareAreaAction = new QAction(tr("Share screen area"), this); |
Edric Milaret | 029b95a | 2015-06-09 09:51:44 -0400 | [diff] [blame] | 191 | menu.addAction(shareAreaAction); |
| 192 | connect(shareAreaAction, &QAction::triggered, [=]() { |
| 193 | SelectAreaDialog selec; |
| 194 | selec.exec(); |
| 195 | }); |
Edric Milaret | 53ac6e5 | 2015-09-14 13:37:06 -0400 | [diff] [blame] | 196 | auto shareFileAction = new QAction(tr("Share file"), this); |
Edric Milaret | 303bbf1 | 2015-06-22 14:24:13 -0400 | [diff] [blame] | 197 | 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 Milaret | 029b95a | 2015-06-09 09:51:44 -0400 | [diff] [blame] | 207 | |
| 208 | menu.exec(globalPos); |
| 209 | } |
| 210 | |