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