blob: 88644ec91243e974689d8ad158ea8e75f184d552 [file] [log] [blame]
Edric Milaret029b95a2015-06-09 09:51:44 -04001/***************************************************************************
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 Milaret303bbf12015-06-22 14:24:13 -040030#include <QFileDialog>
31#include <QMimeData>
Edric Milaret029b95a2015-06-09 09:51:44 -040032
33#include <memory>
34
35#include "videooverlay.h"
36#include "selectareadialog.h"
37
38VideoView::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
67VideoView::~VideoView()
68{
69 delete ui;
70}
71
72void
73VideoView::resizeEvent(QResizeEvent *event)
74{
75 Q_UNUSED(event)
76 overlay_->resize(this->size());
77 overlay_->show();
78 overlay_->raise();
79}
80
81void
82VideoView::enterEvent(QEvent* event)
83{
84 Q_UNUSED(event)
85 fadeAnim_->stop();
86 fadeAnim_->targetObject()->setProperty(fadeAnim_->propertyName(), fadeAnim_->startValue());
87}
88
89void
90VideoView::leaveEvent(QEvent* event)
91{
92 Q_UNUSED(event)
93 fadeAnim_->start(QAbstractAnimation::KeepWhenStopped);
94}
95
96void
97VideoView::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
113void
114VideoView::updateTimer()
115{
116 overlay_->setTime(CallModel::instance()->selectedCall()->length());
117}
118
119void
120VideoView::mouseDoubleClickEvent(QMouseEvent* e) {
121 QWidget::mouseDoubleClickEvent(e);
122 toggleFullScreen();
123}
124
Edric Milaret303bbf12015-06-22 14:24:13 -0400125void VideoView::dragEnterEvent(QDragEnterEvent *event)
126{
127 if (event->mimeData()->hasUrls())
128 event->acceptProposedAction();
129}
130
131void VideoView::dropEvent(QDropEvent *event)
132{
133 auto urls = event->mimeData()->urls();
134 Video::SourceModel::instance()->setFile(urls.at(0));
135}
136
Edric Milaret029b95a2015-06-09 09:51:44 -0400137void
138VideoView::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
153void
154VideoView::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 Milaret303bbf12015-06-22 14:24:13 -0400188 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 Milaret029b95a2015-06-09 09:51:44 -0400199
200 menu.exec(globalPos);
201}
202