Edric Milaret | 7153eed | 2015-06-03 15:29:03 -0400 | [diff] [blame] | 1 | /*************************************************************************** |
Anthony Léonard | 2fde81d | 2017-04-17 10:06:55 -0400 | [diff] [blame] | 2 | * Copyright (C) 2015-2017 by Savoir-faire Linux * |
Edric Milaret | 7153eed | 2015-06-03 15:29:03 -0400 | [diff] [blame] | 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 "selectareadialog.h" |
| 20 | |
Edric | b09982a | 2016-05-19 16:28:38 -0400 | [diff] [blame] | 21 | #ifdef Q_OS_WIN |
| 22 | #define WIN32_LEAN_AND_MEAN 1 |
Andreas Traczyk | e0a60b5 | 2018-07-10 18:16:15 -0400 | [diff] [blame] | 23 | #define NOMINMAX |
Edric | b09982a | 2016-05-19 16:28:38 -0400 | [diff] [blame] | 24 | #include <windows.h> |
| 25 | #include <winuser.h> |
| 26 | |
| 27 | #undef OUT |
| 28 | #undef IN |
| 29 | #undef ERROR |
| 30 | #endif |
| 31 | |
Edric Milaret | 7153eed | 2015-06-03 15:29:03 -0400 | [diff] [blame] | 32 | #include <QApplication> |
| 33 | #include <QScreen> |
| 34 | #include <QPainter> |
| 35 | |
| 36 | #include "video/sourcemodel.h" |
Edric Milaret | 0e1074d | 2015-12-08 12:08:52 -0500 | [diff] [blame] | 37 | #include "media/video.h" |
| 38 | #include "callmodel.h" |
Edric Milaret | 7153eed | 2015-06-03 15:29:03 -0400 | [diff] [blame] | 39 | |
| 40 | SelectAreaDialog::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(); |
Edric | b09982a | 2016-05-19 16:28:38 -0400 | [diff] [blame] | 51 | rubberBand_ = new QRubberBand(QRubberBand::Rectangle, this); |
Edric Milaret | 7153eed | 2015-06-03 15:29:03 -0400 | [diff] [blame] | 52 | QApplication::setOverrideCursor(Qt::CrossCursor); |
Edric | b09982a | 2016-05-19 16:28:38 -0400 | [diff] [blame] | 53 | QScreen* screen = QGuiApplication::primaryScreen(); |
| 54 | if (screen) { |
Edric Milaret | 7153eed | 2015-06-03 15:29:03 -0400 | [diff] [blame] | 55 | originalPixmap_ = screen->grabWindow(0); |
Edric | b09982a | 2016-05-19 16:28:38 -0400 | [diff] [blame] | 56 | originalPixmap_.setDevicePixelRatio(screen->devicePixelRatio()); |
| 57 | } |
Edric Milaret | 7153eed | 2015-06-03 15:29:03 -0400 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void |
| 61 | SelectAreaDialog::mousePressEvent(QMouseEvent* event) |
| 62 | { |
| 63 | if (rubberBand_) { |
| 64 | origin_ = event->globalPos(); |
| 65 | rubberBand_->setGeometry(QRect(origin_, QSize())); |
| 66 | rubberBand_->show(); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | void |
| 71 | SelectAreaDialog::mouseMoveEvent(QMouseEvent* event) |
| 72 | { |
Anthony Léonard | 3b4662f | 2016-11-08 10:55:24 -0500 | [diff] [blame] | 73 | 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 Milaret | 7153eed | 2015-06-03 15:29:03 -0400 | [diff] [blame] | 78 | if (rubberBand_) |
Anthony Léonard | 3b4662f | 2016-11-08 10:55:24 -0500 | [diff] [blame] | 79 | rubberBand_->setGeometry(QRect(QPoint(left,top), QPoint(right,bottom))); |
Edric Milaret | 7153eed | 2015-06-03 15:29:03 -0400 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void |
| 83 | SelectAreaDialog::mouseReleaseEvent(QMouseEvent* event) |
| 84 | { |
| 85 | Q_UNUSED(event) |
| 86 | |
| 87 | if(rubberBand_) { |
| 88 | QApplication::restoreOverrideCursor(); |
| 89 | releaseMouse(); |
Edric Milaret | 0e1074d | 2015-12-08 12:08:52 -0500 | [diff] [blame] | 90 | if (auto call = CallModel::instance().selectedCall()) { |
Andreas Traczyk | e86fb3c | 2018-08-02 17:38:34 -0400 | [diff] [blame] | 91 | if (auto outVideo = call->firstMedia<media::Video>(media::Media::Direction::OUT)) { |
Edric | b09982a | 2016-05-19 16:28:38 -0400 | [diff] [blame] | 92 | 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 Milaret | 9682605 | 2016-02-02 10:24:14 -0500 | [diff] [blame] | 109 | outVideo->sourceModel()->setDisplay(0, realRect); |
Edric Milaret | 0e1074d | 2015-12-08 12:08:52 -0500 | [diff] [blame] | 110 | } |
| 111 | } |
Edric Milaret | 7153eed | 2015-06-03 15:29:03 -0400 | [diff] [blame] | 112 | delete rubberBand_; |
| 113 | rubberBand_ = nullptr; |
| 114 | reject(); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | void |
Edric | b09982a | 2016-05-19 16:28:38 -0400 | [diff] [blame] | 119 | SelectAreaDialog::paintEvent(QPaintEvent* event) |
| 120 | { |
Edric Milaret | 7153eed | 2015-06-03 15:29:03 -0400 | [diff] [blame] | 121 | Q_UNUSED(event) |
| 122 | QPainter painter(this); |
| 123 | |
| 124 | painter.drawPixmap(QPoint(0, 0), originalPixmap_); |
| 125 | } |