UBScreenMirror.cpp 3.85 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1
/*
2
 * Copyright (C) 2015-2018 Département de l'Instruction Publique (DIP-SEM)
Craig Watson's avatar
Craig Watson committed
3
 *
Claudio Valerio's avatar
Claudio Valerio committed
4
 * Copyright (C) 2013 Open Education Foundation
Claudio Valerio's avatar
Claudio Valerio committed
5
 *
Claudio Valerio's avatar
Claudio Valerio committed
6 7
 * Copyright (C) 2010-2013 Groupement d'Intérêt Public pour
 * l'Education Numérique en Afrique (GIP ENA)
8
 *
Claudio Valerio's avatar
Claudio Valerio committed
9 10 11
 * This file is part of OpenBoard.
 *
 * OpenBoard is free software: you can redistribute it and/or modify
Claudio Valerio's avatar
Claudio Valerio committed
12 13
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License,
14 15 16 17
 * with a specific linking exception for the OpenSSL project's
 * "OpenSSL" library (or with modified versions of it that use the
 * same license as the "OpenSSL" library).
 *
Claudio Valerio's avatar
Claudio Valerio committed
18
 * OpenBoard is distributed in the hope that it will be useful,
Claudio Valerio's avatar
Claudio Valerio committed
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Claudio Valerio's avatar
Claudio Valerio committed
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Claudio Valerio's avatar
Claudio Valerio committed
21
 * GNU General Public License for more details.
Claudio Valerio's avatar
Claudio Valerio committed
22
 *
Claudio Valerio's avatar
Claudio Valerio committed
23
 * You should have received a copy of the GNU General Public License
Claudio Valerio's avatar
Claudio Valerio committed
24
 * along with OpenBoard. If not, see <http://www.gnu.org/licenses/>.
Claudio Valerio's avatar
Claudio Valerio committed
25 26
 */

27

Claudio Valerio's avatar
Claudio Valerio committed
28

29
#include <QDesktopWidget>
Claudio Valerio's avatar
Claudio Valerio committed
30

Claudio Valerio's avatar
Claudio Valerio committed
31 32 33 34 35
#include "UBScreenMirror.h"

#include "core/UBSettings.h"
#include "core/UBSetting.h"
#include "core/UBApplication.h"
Didier Clerc's avatar
Didier Clerc committed
36
#include "board/UBBoardController.h"
37

38
#if defined(Q_OS_OSX)
39 40 41
#include <ApplicationServices/ApplicationServices.h>
#endif

42 43
#include "core/memcheck.h"

Didier Clerc's avatar
Didier Clerc committed
44

Claudio Valerio's avatar
Claudio Valerio committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
UBScreenMirror::UBScreenMirror(QWidget* parent)
    : QWidget(parent)
    , mScreenIndex(0)
    , mSourceWidget(0)
    , mTimerID(0)
{
    // NOOP
}


UBScreenMirror::~UBScreenMirror()
{
    // NOOP
}


void UBScreenMirror::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QPainter painter(this);

    painter.fillRect(0, 0, width(), height(), QBrush(Qt::black));

    if (!mLastPixmap.isNull())
    {
        int x = (width() - mLastPixmap.width()) / 2;
        int y = (height() - mLastPixmap.height()) / 2;

        painter.drawPixmap(x, y, mLastPixmap);
    }
}


void UBScreenMirror::timerEvent(QTimerEvent *event)
{
    Q_UNUSED(event);

    grabPixmap();

    update();
}

void UBScreenMirror::grabPixmap()
{
Claudio Valerio's avatar
Claudio Valerio committed
90 91 92 93 94 95 96
    if (mSourceWidget)
    {
        QPoint topLeft = mSourceWidget->mapToGlobal(mSourceWidget->geometry().topLeft());
        QPoint bottomRight = mSourceWidget->mapToGlobal(mSourceWidget->geometry().bottomRight());

        mRect.setTopLeft(topLeft);
        mRect.setBottomRight(bottomRight);
Craig Watson's avatar
Craig Watson committed
97
        mLastPixmap = mSourceWidget->grab();
Claudio Valerio's avatar
Claudio Valerio committed
98 99 100 101 102
    }
    else{
        // WHY HERE?
        // this is the case we are showing the desktop but the is no widget and we use the last widget rectagle to know
        // what we have to grab. Not very good way of doing
103
        QDesktopWidget * desktop = QApplication::desktop();
104
        QScreen * screen = UBApplication::controlScreen();
105
        mLastPixmap = screen->grabWindow(desktop->effectiveWinId(), mRect.x(), mRect.y(), mRect.width(), mRect.height());
Claudio Valerio's avatar
Claudio Valerio committed
106 107
    }

Craig Watson's avatar
Craig Watson committed
108 109
    if (!mLastPixmap.isNull())
        mLastPixmap = mLastPixmap.scaled(width(), height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
Claudio Valerio's avatar
Claudio Valerio committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
}


void UBScreenMirror::setSourceWidget(QWidget *sourceWidget)
{
    mSourceWidget = sourceWidget;

    mScreenIndex = qApp->desktop()->screenNumber(sourceWidget);

    grabPixmap();

    update();
}


void UBScreenMirror::start()
{
Didier Clerc's avatar
Didier Clerc committed
127 128
    qDebug() << "mirroring START";
    UBApplication::boardController->freezeW3CWidgets(true);
Claudio Valerio's avatar
Claudio Valerio committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    if (mTimerID == 0)
    {
        int ms = 125;

        bool success;
        int fps = UBSettings::settings()->mirroringRefreshRateInFps->get().toInt(&success);

        if (success && fps > 0)
        {
            ms = 1000 / fps;
        }

        mTimerID = startTimer(ms);
    }
    else
    {
145
        qDebug() << "UBScreenMirror::start() : Timer already running ...";
Claudio Valerio's avatar
Claudio Valerio committed
146 147 148 149 150 151
    }
}


void UBScreenMirror::stop()
{
Didier Clerc's avatar
Didier Clerc committed
152 153
    qDebug() << "mirroring STOP";
    UBApplication::boardController->freezeW3CWidgets(false);
Claudio Valerio's avatar
Claudio Valerio committed
154 155 156 157 158 159
    if (mTimerID != 0)
    {
        killTimer(mTimerID);
        mTimerID = 0;
    }
}