blob: c983bde4f618cedd48233d8d2bb034735ba77581 [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
Andreas Traczyke0a60b52018-07-10 18:16:15 -040023#define NOMINMAX
Edricb09982a2016-05-19 16:28:38 -040024#include <windows.h>
25#include <winuser.h>
26
27#undef OUT
28#undef IN
29#undef ERROR
30#endif
31
Edric Milaret7153eed2015-06-03 15:29:03 -040032#include <QApplication>
33#include <QScreen>
34#include <QPainter>
35
36#include "video/sourcemodel.h"
Edric Milaret0e1074d2015-12-08 12:08:52 -050037#include "media/video.h"
38#include "callmodel.h"
Edric Milaret7153eed2015-06-03 15:29:03 -040039
40SelectAreaDialog::SelectAreaDialog() :
41 rubberBand_(nullptr)
42{
43 setWindowFlags(Qt::Widget | Qt::FramelessWindowHint);
44 setWindowState(Qt::WindowFullScreen);
45 setParent(0);
46 setAutoFillBackground(false);
47 setAttribute(Qt::WA_NoSystemBackground, false);
48 setAttribute(Qt::WA_TranslucentBackground, true);
49 setAttribute(Qt::WA_PaintOnScreen);
50 grabMouse();
Edricb09982a2016-05-19 16:28:38 -040051 rubberBand_ = new QRubberBand(QRubberBand::Rectangle, this);
Edric Milaret7153eed2015-06-03 15:29:03 -040052 QApplication::setOverrideCursor(Qt::CrossCursor);
Edricb09982a2016-05-19 16:28:38 -040053 QScreen* screen = QGuiApplication::primaryScreen();
54 if (screen) {
Edric Milaret7153eed2015-06-03 15:29:03 -040055 originalPixmap_ = screen->grabWindow(0);
Edricb09982a2016-05-19 16:28:38 -040056 originalPixmap_.setDevicePixelRatio(screen->devicePixelRatio());
57 }
Edric Milaret7153eed2015-06-03 15:29:03 -040058}
59
60void
61SelectAreaDialog::mousePressEvent(QMouseEvent* event)
62{
63 if (rubberBand_) {
64 origin_ = event->globalPos();
65 rubberBand_->setGeometry(QRect(origin_, QSize()));
66 rubberBand_->show();
67 }
68}
69
70void
71SelectAreaDialog::mouseMoveEvent(QMouseEvent* event)
72{
Anthony Léonard3b4662f2016-11-08 10:55:24 -050073 int top = std::min(event->globalY(), origin_.y());
74 int left = std::min(event->globalX(), origin_.x());
75 int bottom = std::max(event->globalY(), origin_.y());
76 int right = std::max(event->globalX(), origin_.x());
77
Edric Milaret7153eed2015-06-03 15:29:03 -040078 if (rubberBand_)
Anthony Léonard3b4662f2016-11-08 10:55:24 -050079 rubberBand_->setGeometry(QRect(QPoint(left,top), QPoint(right,bottom)));
Edric Milaret7153eed2015-06-03 15:29:03 -040080}
81
82void
83SelectAreaDialog::mouseReleaseEvent(QMouseEvent* event)
84{
85 Q_UNUSED(event)
86
87 if(rubberBand_) {
88 QApplication::restoreOverrideCursor();
89 releaseMouse();
Edric Milaret0e1074d2015-12-08 12:08:52 -050090 if (auto call = CallModel::instance().selectedCall()) {
Andreas Traczyke86fb3c2018-08-02 17:38:34 -040091 if (auto outVideo = call->firstMedia<media::Video>(media::Media::Direction::OUT)) {
Edricb09982a2016-05-19 16:28:38 -040092 QRect realRect = rubberBand_->geometry();
93#ifdef Q_OS_WIN
94 if (QGuiApplication::primaryScreen()->devicePixelRatio() > 1.0) {
95 auto scaledSize = QGuiApplication::primaryScreen()->geometry();
96 auto sourceHdc = GetDC(nullptr);
97 auto vertres = GetDeviceCaps(sourceHdc, VERTRES);
98 auto horzres = GetDeviceCaps(sourceHdc, HORZRES);
99 auto height = realRect.height() * QGuiApplication::primaryScreen()->devicePixelRatio();
100 auto width = realRect.width() * QGuiApplication::primaryScreen()->devicePixelRatio();
101 float xRatio = static_cast<float>(horzres) / static_cast<float>(scaledSize.width());
102 float yRatio = static_cast<float>(vertres) / static_cast<float>(scaledSize.height());
103 realRect.setX(static_cast<int>(realRect.x() * xRatio));
104 realRect.setY(static_cast<int>(realRect.y() * yRatio));
105 realRect.setWidth(static_cast<int>(width));
106 realRect.setHeight(static_cast<int>(height));
107 }
108#endif
Edric Milaret96826052016-02-02 10:24:14 -0500109 outVideo->sourceModel()->setDisplay(0, realRect);
Edric Milaret0e1074d2015-12-08 12:08:52 -0500110 }
111 }
Edric Milaret7153eed2015-06-03 15:29:03 -0400112 delete rubberBand_;
113 rubberBand_ = nullptr;
114 reject();
115 }
116}
117
118void
Edricb09982a2016-05-19 16:28:38 -0400119SelectAreaDialog::paintEvent(QPaintEvent* event)
120{
Edric Milaret7153eed2015-06-03 15:29:03 -0400121 Q_UNUSED(event)
122 QPainter painter(this);
123
124 painter.drawPixmap(QPoint(0, 0), originalPixmap_);
125}