blob: 7154cac989ecf70599512f25998fdc3292774a84 [file] [log] [blame]
Edric Milaret7153eed2015-06-03 15:29:03 -04001/***************************************************************************
Anthony Léonard2fde81d2017-04-17 10:06:55 -04002 * Copyright (C) 2015-2017 by Savoir-faire Linux *
Edric Milaret7153eed2015-06-03 15:29:03 -04003 * 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 "selectareadialog.h"
20
Edricb09982a2016-05-19 16:28:38 -040021#ifdef Q_OS_WIN
22#define WIN32_LEAN_AND_MEAN 1
23#include <windows.h>
24#include <winuser.h>
25
26#undef OUT
27#undef IN
28#undef ERROR
29#endif
30
Edric Milaret7153eed2015-06-03 15:29:03 -040031#include <QApplication>
32#include <QScreen>
33#include <QPainter>
34
35#include "video/sourcemodel.h"
Edric Milaret0e1074d2015-12-08 12:08:52 -050036#include "media/video.h"
37#include "callmodel.h"
Edric Milaret7153eed2015-06-03 15:29:03 -040038
39SelectAreaDialog::SelectAreaDialog() :
40 rubberBand_(nullptr)
41{
42 setWindowFlags(Qt::Widget | Qt::FramelessWindowHint);
43 setWindowState(Qt::WindowFullScreen);
44 setParent(0);
45 setAutoFillBackground(false);
46 setAttribute(Qt::WA_NoSystemBackground, false);
47 setAttribute(Qt::WA_TranslucentBackground, true);
48 setAttribute(Qt::WA_PaintOnScreen);
49 grabMouse();
Edricb09982a2016-05-19 16:28:38 -040050 rubberBand_ = new QRubberBand(QRubberBand::Rectangle, this);
Edric Milaret7153eed2015-06-03 15:29:03 -040051 QApplication::setOverrideCursor(Qt::CrossCursor);
Edricb09982a2016-05-19 16:28:38 -040052 QScreen* screen = QGuiApplication::primaryScreen();
53 if (screen) {
Edric Milaret7153eed2015-06-03 15:29:03 -040054 originalPixmap_ = screen->grabWindow(0);
Edricb09982a2016-05-19 16:28:38 -040055 originalPixmap_.setDevicePixelRatio(screen->devicePixelRatio());
56 }
Edric Milaret7153eed2015-06-03 15:29:03 -040057}
58
59void
60SelectAreaDialog::mousePressEvent(QMouseEvent* event)
61{
62 if (rubberBand_) {
63 origin_ = event->globalPos();
64 rubberBand_->setGeometry(QRect(origin_, QSize()));
65 rubberBand_->show();
66 }
67}
68
69void
70SelectAreaDialog::mouseMoveEvent(QMouseEvent* event)
71{
Anthony Léonard3b4662f2016-11-08 10:55:24 -050072 int top = std::min(event->globalY(), origin_.y());
73 int left = std::min(event->globalX(), origin_.x());
74 int bottom = std::max(event->globalY(), origin_.y());
75 int right = std::max(event->globalX(), origin_.x());
76
Edric Milaret7153eed2015-06-03 15:29:03 -040077 if (rubberBand_)
Anthony Léonard3b4662f2016-11-08 10:55:24 -050078 rubberBand_->setGeometry(QRect(QPoint(left,top), QPoint(right,bottom)));
Edric Milaret7153eed2015-06-03 15:29:03 -040079}
80
81void
82SelectAreaDialog::mouseReleaseEvent(QMouseEvent* event)
83{
84 Q_UNUSED(event)
85
86 if(rubberBand_) {
87 QApplication::restoreOverrideCursor();
88 releaseMouse();
Edric Milaret0e1074d2015-12-08 12:08:52 -050089 if (auto call = CallModel::instance().selectedCall()) {
90 if (auto outVideo = call->firstMedia<Media::Video>(Media::Media::Direction::OUT)) {
Edricb09982a2016-05-19 16:28:38 -040091 QRect realRect = rubberBand_->geometry();
92#ifdef Q_OS_WIN
93 if (QGuiApplication::primaryScreen()->devicePixelRatio() > 1.0) {
94 auto scaledSize = QGuiApplication::primaryScreen()->geometry();
95 auto sourceHdc = GetDC(nullptr);
96 auto vertres = GetDeviceCaps(sourceHdc, VERTRES);
97 auto horzres = GetDeviceCaps(sourceHdc, HORZRES);
98 auto height = realRect.height() * QGuiApplication::primaryScreen()->devicePixelRatio();
99 auto width = realRect.width() * QGuiApplication::primaryScreen()->devicePixelRatio();
100 float xRatio = static_cast<float>(horzres) / static_cast<float>(scaledSize.width());
101 float yRatio = static_cast<float>(vertres) / static_cast<float>(scaledSize.height());
102 realRect.setX(static_cast<int>(realRect.x() * xRatio));
103 realRect.setY(static_cast<int>(realRect.y() * yRatio));
104 realRect.setWidth(static_cast<int>(width));
105 realRect.setHeight(static_cast<int>(height));
106 }
107#endif
Edric Milaret96826052016-02-02 10:24:14 -0500108 outVideo->sourceModel()->setDisplay(0, realRect);
Edric Milaret0e1074d2015-12-08 12:08:52 -0500109 }
110 }
Edric Milaret7153eed2015-06-03 15:29:03 -0400111 delete rubberBand_;
112 rubberBand_ = nullptr;
113 reject();
114 }
115}
116
117void
Edricb09982a2016-05-19 16:28:38 -0400118SelectAreaDialog::paintEvent(QPaintEvent* event)
119{
Edric Milaret7153eed2015-06-03 15:29:03 -0400120 Q_UNUSED(event)
121 QPainter painter(this);
122
123 painter.drawPixmap(QPoint(0, 0), originalPixmap_);
124}