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> |
| 30 | |
| 31 | #include <memory> |
| 32 | |
| 33 | #include "videooverlay.h" |
| 34 | #include "selectareadialog.h" |
| 35 | |
| 36 | VideoView::VideoView(QWidget *parent) : |
| 37 | QWidget(parent), |
| 38 | ui(new Ui::VideoView) |
| 39 | { |
| 40 | ui->setupUi(this); |
| 41 | |
| 42 | connect(CallModel::instance(), SIGNAL(callStateChanged(Call*, Call::State)), |
| 43 | this, SLOT(callStateChanged(Call*, Call::State))); |
| 44 | |
| 45 | overlay_ = new VideoOverlay(this); |
| 46 | auto effect = new QGraphicsOpacityEffect(overlay_); |
| 47 | effect->setOpacity(1.0); |
| 48 | overlay_->setGraphicsEffect(effect); |
| 49 | fadeAnim_ = new QPropertyAnimation(this); |
| 50 | fadeAnim_->setTargetObject(effect); |
| 51 | fadeAnim_->setPropertyName("opacity"); |
| 52 | fadeAnim_->setDuration(fadeOverlayTime_); |
| 53 | fadeAnim_->setStartValue(effect->opacity()); |
| 54 | fadeAnim_->setEndValue(0); |
| 55 | fadeAnim_->setEasingCurve(QEasingCurve::OutQuad); |
| 56 | |
| 57 | timerLength_ = new QTimer(this); |
| 58 | connect(timerLength_, SIGNAL(timeout()), this, SLOT(updateTimer())); |
| 59 | |
| 60 | this->setContextMenuPolicy(Qt::CustomContextMenu); |
| 61 | connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), |
| 62 | this, SLOT(showContextMenu(const QPoint&))); |
| 63 | } |
| 64 | |
| 65 | VideoView::~VideoView() |
| 66 | { |
| 67 | delete ui; |
| 68 | } |
| 69 | |
| 70 | void |
| 71 | VideoView::resizeEvent(QResizeEvent *event) |
| 72 | { |
| 73 | Q_UNUSED(event) |
| 74 | overlay_->resize(this->size()); |
| 75 | overlay_->show(); |
| 76 | overlay_->raise(); |
| 77 | } |
| 78 | |
| 79 | void |
| 80 | VideoView::enterEvent(QEvent* event) |
| 81 | { |
| 82 | Q_UNUSED(event) |
| 83 | fadeAnim_->stop(); |
| 84 | fadeAnim_->targetObject()->setProperty(fadeAnim_->propertyName(), fadeAnim_->startValue()); |
| 85 | } |
| 86 | |
| 87 | void |
| 88 | VideoView::leaveEvent(QEvent* event) |
| 89 | { |
| 90 | Q_UNUSED(event) |
| 91 | fadeAnim_->start(QAbstractAnimation::KeepWhenStopped); |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | VideoView::callStateChanged(Call* call, Call::State previousState) |
| 96 | { |
| 97 | Q_UNUSED(previousState) |
| 98 | if (call->state() == Call::State::CURRENT) { |
| 99 | ui->videoWidget->show(); |
| 100 | timerLength_->start(1000); |
| 101 | overlay_->setName(call->formattedName()); |
| 102 | } |
| 103 | else { |
| 104 | ui->videoWidget->hide(); |
| 105 | if (isFullScreen()) |
| 106 | toggleFullScreen(); |
| 107 | timerLength_->stop(); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | void |
| 112 | VideoView::updateTimer() |
| 113 | { |
| 114 | overlay_->setTime(CallModel::instance()->selectedCall()->length()); |
| 115 | } |
| 116 | |
| 117 | void |
| 118 | VideoView::mouseDoubleClickEvent(QMouseEvent* e) { |
| 119 | QWidget::mouseDoubleClickEvent(e); |
| 120 | toggleFullScreen(); |
| 121 | } |
| 122 | |
| 123 | void |
| 124 | VideoView::toggleFullScreen() |
| 125 | { |
| 126 | if(isFullScreen()) { |
| 127 | this->setParent(oldParent_); |
| 128 | this->showNormal(); |
| 129 | this->resize(oldSize_.width(), oldSize_.height()); |
| 130 | } else { |
| 131 | oldSize_ = this->size(); |
| 132 | oldParent_ = static_cast<QWidget*>(this->parent()); |
| 133 | this->setParent(0); |
| 134 | this->showFullScreen(); |
| 135 | this->show(); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | void |
| 140 | VideoView::showContextMenu(const QPoint& pos) |
| 141 | { |
| 142 | QPoint globalPos = this->mapToGlobal(pos); |
| 143 | |
| 144 | QMenu menu; |
| 145 | |
| 146 | for (auto device : Video::DeviceModel::instance()->devices()) { |
| 147 | std::unique_ptr<QAction> deviceAction(new QAction(device->name(), this)); |
| 148 | deviceAction->setCheckable(true); |
| 149 | if (device == Video::DeviceModel::instance()->activeDevice()) |
| 150 | deviceAction->setChecked(true); |
| 151 | auto ptr = deviceAction.release(); |
| 152 | menu.addAction(ptr); |
| 153 | connect(ptr, &QAction::toggled, [=](bool checked) { |
| 154 | if (checked == true) { |
| 155 | Video::SourceModel::instance()->switchTo(device); |
| 156 | Video::DeviceModel::instance()->setActive(device); |
| 157 | } |
| 158 | }); |
| 159 | } |
| 160 | |
| 161 | menu.addSeparator(); |
| 162 | |
| 163 | auto shareAction = new QAction("Share entire screen", this); |
| 164 | menu.addAction(shareAction); |
| 165 | connect(shareAction, &QAction::triggered, [=]() { |
| 166 | Video::SourceModel::instance()->setDisplay(0, QApplication::desktop()->rect()); |
| 167 | }); |
| 168 | auto shareAreaAction = new QAction("Share screen area", this); |
| 169 | menu.addAction(shareAreaAction); |
| 170 | connect(shareAreaAction, &QAction::triggered, [=]() { |
| 171 | SelectAreaDialog selec; |
| 172 | selec.exec(); |
| 173 | }); |
| 174 | |
| 175 | menu.exec(globalPos); |
| 176 | } |
| 177 | |