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&))); |
| 65 | } |
| 66 | |
| 67 | VideoView::~VideoView() |
| 68 | { |
| 69 | delete ui; |
| 70 | } |
| 71 | |
| 72 | void |
| 73 | VideoView::resizeEvent(QResizeEvent *event) |
| 74 | { |
| 75 | Q_UNUSED(event) |
| 76 | overlay_->resize(this->size()); |
| 77 | overlay_->show(); |
| 78 | overlay_->raise(); |
| 79 | } |
| 80 | |
| 81 | void |
| 82 | VideoView::enterEvent(QEvent* event) |
| 83 | { |
| 84 | Q_UNUSED(event) |
| 85 | fadeAnim_->stop(); |
| 86 | fadeAnim_->targetObject()->setProperty(fadeAnim_->propertyName(), fadeAnim_->startValue()); |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | VideoView::leaveEvent(QEvent* event) |
| 91 | { |
| 92 | Q_UNUSED(event) |
| 93 | fadeAnim_->start(QAbstractAnimation::KeepWhenStopped); |
| 94 | } |
| 95 | |
| 96 | void |
| 97 | VideoView::callStateChanged(Call* call, Call::State previousState) |
| 98 | { |
| 99 | Q_UNUSED(previousState) |
| 100 | if (call->state() == Call::State::CURRENT) { |
| 101 | ui->videoWidget->show(); |
| 102 | timerLength_->start(1000); |
| 103 | overlay_->setName(call->formattedName()); |
| 104 | } |
| 105 | else { |
| 106 | ui->videoWidget->hide(); |
| 107 | if (isFullScreen()) |
| 108 | toggleFullScreen(); |
| 109 | timerLength_->stop(); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void |
| 114 | VideoView::updateTimer() |
| 115 | { |
| 116 | overlay_->setTime(CallModel::instance()->selectedCall()->length()); |
| 117 | } |
| 118 | |
| 119 | void |
| 120 | VideoView::mouseDoubleClickEvent(QMouseEvent* e) { |
| 121 | QWidget::mouseDoubleClickEvent(e); |
| 122 | toggleFullScreen(); |
| 123 | } |
| 124 | |
Edric Milaret | 303bbf1 | 2015-06-22 14:24:13 -0400 | [diff] [blame] | 125 | void VideoView::dragEnterEvent(QDragEnterEvent *event) |
| 126 | { |
| 127 | if (event->mimeData()->hasUrls()) |
| 128 | event->acceptProposedAction(); |
| 129 | } |
| 130 | |
| 131 | void VideoView::dropEvent(QDropEvent *event) |
| 132 | { |
| 133 | auto urls = event->mimeData()->urls(); |
| 134 | Video::SourceModel::instance()->setFile(urls.at(0)); |
| 135 | } |
| 136 | |
Edric Milaret | 029b95a | 2015-06-09 09:51:44 -0400 | [diff] [blame] | 137 | void |
| 138 | VideoView::toggleFullScreen() |
| 139 | { |
| 140 | if(isFullScreen()) { |
| 141 | this->setParent(oldParent_); |
| 142 | this->showNormal(); |
| 143 | this->resize(oldSize_.width(), oldSize_.height()); |
| 144 | } else { |
| 145 | oldSize_ = this->size(); |
| 146 | oldParent_ = static_cast<QWidget*>(this->parent()); |
| 147 | this->setParent(0); |
| 148 | this->showFullScreen(); |
| 149 | this->show(); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | void |
| 154 | VideoView::showContextMenu(const QPoint& pos) |
| 155 | { |
| 156 | QPoint globalPos = this->mapToGlobal(pos); |
| 157 | |
| 158 | QMenu menu; |
| 159 | |
| 160 | for (auto device : Video::DeviceModel::instance()->devices()) { |
| 161 | std::unique_ptr<QAction> deviceAction(new QAction(device->name(), this)); |
| 162 | deviceAction->setCheckable(true); |
| 163 | if (device == Video::DeviceModel::instance()->activeDevice()) |
| 164 | deviceAction->setChecked(true); |
| 165 | auto ptr = deviceAction.release(); |
| 166 | menu.addAction(ptr); |
| 167 | connect(ptr, &QAction::toggled, [=](bool checked) { |
| 168 | if (checked == true) { |
| 169 | Video::SourceModel::instance()->switchTo(device); |
| 170 | Video::DeviceModel::instance()->setActive(device); |
| 171 | } |
| 172 | }); |
| 173 | } |
| 174 | |
| 175 | menu.addSeparator(); |
| 176 | |
| 177 | auto shareAction = new QAction("Share entire screen", this); |
| 178 | menu.addAction(shareAction); |
| 179 | connect(shareAction, &QAction::triggered, [=]() { |
| 180 | Video::SourceModel::instance()->setDisplay(0, QApplication::desktop()->rect()); |
| 181 | }); |
| 182 | auto shareAreaAction = new QAction("Share screen area", this); |
| 183 | menu.addAction(shareAreaAction); |
| 184 | connect(shareAreaAction, &QAction::triggered, [=]() { |
| 185 | SelectAreaDialog selec; |
| 186 | selec.exec(); |
| 187 | }); |
Edric Milaret | 303bbf1 | 2015-06-22 14:24:13 -0400 | [diff] [blame] | 188 | auto shareFileAction = new QAction("Share file", this); |
| 189 | menu.addAction(shareFileAction); |
| 190 | connect(shareFileAction, &QAction::triggered, [=]() { |
| 191 | QFileDialog dialog(this); |
| 192 | dialog.setFileMode(QFileDialog::AnyFile); |
| 193 | QStringList fileNames; |
| 194 | if (!dialog.exec()) |
| 195 | return; |
| 196 | fileNames = dialog.selectedFiles(); |
| 197 | Video::SourceModel::instance()->setFile(QUrl::fromLocalFile(fileNames.at(0))); |
| 198 | }); |
Edric Milaret | 029b95a | 2015-06-09 09:51:44 -0400 | [diff] [blame] | 199 | |
| 200 | menu.exec(globalPos); |
| 201 | } |
| 202 | |