Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019-2020 by Savoir-faire Linux |
| 3 | * Author: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com> |
| 4 | * Author: Mingrui Zhang <mingrui.zhang@savoirfairelinux.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "distantrenderer.h" |
| 21 | |
| 22 | #include "lrcinstance.h" |
| 23 | |
| 24 | DistantRenderer::DistantRenderer(QQuickItem *parent) |
| 25 | : QQuickPaintedItem(parent) |
| 26 | { |
| 27 | setAntialiasing(true); |
| 28 | setFillColor(Qt::black); |
| 29 | setRenderTarget(QQuickPaintedItem::FramebufferObject); |
| 30 | setPerformanceHint(QQuickPaintedItem::FastFBOResizing); |
| 31 | |
| 32 | connect(LRCInstance::renderer(), &RenderManager::distantFrameUpdated, [this](const QString &id) { |
| 33 | if (distantRenderId_ == id) |
| 34 | update(QRect(0, 0, width(), height())); |
| 35 | }); |
| 36 | |
| 37 | connect(LRCInstance::renderer(), |
| 38 | &RenderManager::distantRenderingStopped, |
| 39 | [this](const QString &id) { |
| 40 | if (distantRenderId_ == id) |
| 41 | update(QRect(0, 0, width(), height())); |
| 42 | }); |
| 43 | } |
| 44 | |
| 45 | DistantRenderer::~DistantRenderer() {} |
| 46 | |
| 47 | void |
| 48 | DistantRenderer::setRendererId(const QString &id) |
| 49 | { |
| 50 | distantRenderId_ = id; |
| 51 | update(QRect(0, 0, width(), height())); |
| 52 | } |
| 53 | |
Sébastien Blin | 6607e0e | 2020-07-24 15:15:47 -0400 | [diff] [blame^] | 54 | int |
| 55 | DistantRenderer::getXOffset() const |
| 56 | { |
| 57 | return xOffset_; |
| 58 | } |
| 59 | |
| 60 | int |
| 61 | DistantRenderer::getYOffset() const |
| 62 | { |
| 63 | return yOffset_; |
| 64 | } |
| 65 | |
| 66 | double |
| 67 | DistantRenderer::getScaledWidth() const |
| 68 | { |
| 69 | return scaledWidth_; |
| 70 | } |
| 71 | |
| 72 | double |
| 73 | DistantRenderer::getScaledHeight() const |
| 74 | { |
| 75 | return scaledHeight_; |
| 76 | } |
| 77 | |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 78 | void |
| 79 | DistantRenderer::paint(QPainter *painter) |
| 80 | { |
| 81 | auto distantImage = LRCInstance::renderer()->getFrame(distantRenderId_); |
| 82 | if (distantImage) { |
| 83 | auto scaledDistant = distantImage->scaled(size().toSize(), Qt::KeepAspectRatio); |
Sébastien Blin | 6607e0e | 2020-07-24 15:15:47 -0400 | [diff] [blame^] | 84 | auto tempScaledWidth = static_cast<int>(scaledWidth_*1000); |
| 85 | auto tempScaledHeight = static_cast<int>(scaledHeight_*1000); |
| 86 | auto tempXOffset = xOffset_; |
| 87 | auto tempYOffset = yOffset_; |
| 88 | scaledWidth_ = static_cast<double>(scaledDistant.width())/static_cast<double>(distantImage->width()); |
| 89 | scaledHeight_ = static_cast<double>(scaledDistant.height())/static_cast<double>(distantImage->height()); |
| 90 | xOffset_ = (width() - scaledDistant.width()) / 2; |
| 91 | yOffset_ = (height() - scaledDistant.height()) / 2; |
| 92 | if (tempXOffset != xOffset_ or tempYOffset != yOffset_ or static_cast<int>(scaledWidth_*1000) != tempScaledWidth or static_cast<int>(scaledHeight_*1000) != tempScaledHeight) { |
| 93 | emit offsetChanged(); |
| 94 | } |
| 95 | painter->drawImage(QRect(xOffset_, yOffset_, scaledDistant.width(), scaledDistant.height()), |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 96 | scaledDistant); |
| 97 | } |
| 98 | } |