UBDisplayManager.cpp 8.96 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 28 29 30 31 32
 */

#include "UBDisplayManager.h"

#include "frameworks/UBPlatformUtils.h"

#include "core/UBApplication.h"
#include "core/UBApplicationController.h"
Claudio Valerio's avatar
Claudio Valerio committed
33
#include "core/UBSettings.h"
Claudio Valerio's avatar
Claudio Valerio committed
34 35

#include "board/UBBoardView.h"
36
#include "board/UBBoardController.h"
Claudio Valerio's avatar
Claudio Valerio committed
37 38 39 40 41

#include "gui/UBBlackoutWidget.h"

#include "ui_blackoutWidget.h"

42
#include "core/memcheck.h"
Claudio Valerio's avatar
Claudio Valerio committed
43

44 45
#include "qdesktopwidget.h"

Claudio Valerio's avatar
Claudio Valerio committed
46 47 48 49 50 51
UBDisplayManager::UBDisplayManager(QObject *parent)
    : QObject(parent)
    , mControlScreenIndex(-1)
    , mDisplayScreenIndex(-1)
    , mControlWidget(0)
    , mDisplayWidget(0)
52
    , mDesktopWidget(0)
Claudio Valerio's avatar
Claudio Valerio committed
53 54 55
{
    mDesktop = qApp->desktop();

56
    mUseMultiScreen = UBSettings::settings()->appUseMultiscreen->get().toBool();
Claudio Valerio's avatar
Claudio Valerio committed
57 58 59

    initScreenIndexes();

60 61
    connect(mDesktop, &QDesktopWidget::resized, this, &UBDisplayManager::adjustScreens);
    connect(mDesktop, &QDesktopWidget::workAreaResized, this, &UBDisplayManager::adjustScreens);
Claudio Valerio's avatar
Claudio Valerio committed
62 63 64 65 66
}


void UBDisplayManager::initScreenIndexes()
{
67
    int screenCount = numScreens();
Claudio Valerio's avatar
Claudio Valerio committed
68 69 70 71 72 73

    mScreenIndexesRoles.clear();

    if (screenCount > 0)
    {
        mControlScreenIndex = mDesktop->primaryScreen();
74
        if (screenCount > 1 && UBSettings::settings()->swapControlAndDisplayScreens->get().toBool())
75
        {
Claudio Valerio's avatar
Claudio Valerio committed
76
            mControlScreenIndex = mControlScreenIndex^1;
77
        }
Claudio Valerio's avatar
Claudio Valerio committed
78 79 80 81 82 83 84 85

        mScreenIndexesRoles << Control;
    }
    else
    {
        mControlScreenIndex = -1;
    }

86
    if (screenCount > 1 && mUseMultiScreen)
Claudio Valerio's avatar
Claudio Valerio committed
87 88 89 90 91 92 93 94 95 96 97 98 99
    {
        mDisplayScreenIndex = mControlScreenIndex != 0 ? 0 : 1;
        mScreenIndexesRoles << Display;
    }
    else
    {
        mDisplayScreenIndex = -1;
    }

    mPreviousScreenIndexes.clear();

    if (screenCount > 2)
    {
Claudio Valerio's avatar
Claudio Valerio committed
100 101 102 103 104 105 106
        for(int i = 2; i < screenCount; i++)
        {
            if(mControlScreenIndex == i)
                mPreviousScreenIndexes.append(1);
            else
                mPreviousScreenIndexes.append(i);
        }
Claudio Valerio's avatar
Claudio Valerio committed
107 108 109
    }
}

110 111
void UBDisplayManager::swapDisplayScreens(bool swap)
{
112
    initScreenIndexes();
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

    if (swap)
    {
        // As it s a really specific ask and we don't have much time to handle it correctly
        // this code handles only the swap between the main display screen and the first previous one
        int displayScreenIndex = mDisplayScreenIndex;
        mDisplayScreenIndex = mPreviousScreenIndexes.at(0);
        mPreviousScreenIndexes.clear();
        mPreviousScreenIndexes.append(displayScreenIndex);
    }

    positionScreens();

    emit screenLayoutChanged();
    emit adjustDisplayViewsRequired();
}

Claudio Valerio's avatar
Claudio Valerio committed
130 131 132 133 134 135 136 137 138

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


int UBDisplayManager::numScreens()
{
139 140 141 142
    int screenCount = mDesktop->screenCount();
    // Some window managers report two screens when the two monitors are in "cloned" mode; this hack ensures
    // that we consider this as just one screen. On most desktops, at least one of the following conditions is
    // a good indicator of the displays being in cloned or extended mode.
143
#ifdef Q_OS_LINUX
144 145 146
    if (screenCount > 1
        && (mDesktop->screenNumber(mDesktop->screen(0)) == mDesktop->screenNumber(mDesktop->screen(1))
            || mDesktop->screenGeometry(0) == mDesktop->screenGeometry(1)))
Claudio Valerio's avatar
Claudio Valerio committed
147
        return 1;
148 149
#endif
    return screenCount;
Claudio Valerio's avatar
Claudio Valerio committed
150 151 152 153 154 155 156 157 158
}


int UBDisplayManager::numPreviousViews()
{
    return mPreviousScreenIndexes.size();
}


shibakaneki's avatar
shibakaneki committed
159
void UBDisplayManager::setControlWidget(QWidget* pControlWidget)
Claudio Valerio's avatar
Claudio Valerio committed
160 161 162 163 164
{
    if(hasControl() && pControlWidget && (pControlWidget != mControlWidget))
        mControlWidget = pControlWidget;
}

shibakaneki's avatar
shibakaneki committed
165
void UBDisplayManager::setDesktopWidget(QWidget* pControlWidget )
166
{
167
    if(pControlWidget && (pControlWidget != mControlWidget))
168 169
        mDesktopWidget = pControlWidget;
}
Claudio Valerio's avatar
Claudio Valerio committed
170

shibakaneki's avatar
shibakaneki committed
171
void UBDisplayManager::setDisplayWidget(QWidget* pDisplayWidget)
Claudio Valerio's avatar
Claudio Valerio committed
172
{
173
    if(pDisplayWidget && (pDisplayWidget != mDisplayWidget))
Claudio Valerio's avatar
Claudio Valerio committed
174
    {
Aleksei Kanash's avatar
Aleksei Kanash committed
175 176
        if (mDisplayWidget)
        {
177
            mDisplayWidget->hide();
Aleksei Kanash's avatar
Aleksei Kanash committed
178 179 180
            pDisplayWidget->setGeometry(mDisplayWidget->geometry());
            pDisplayWidget->setWindowFlags(mDisplayWidget->windowFlags());
        }
Claudio Valerio's avatar
Claudio Valerio committed
181
        mDisplayWidget = pDisplayWidget;
182
        mDisplayWidget->setGeometry(mDesktop->screenGeometry(mDisplayScreenIndex));
183
        if (UBSettings::settings()->appUseMultiscreen->get().toBool())
184
            UBPlatformUtils::showFullScreen(mDisplayWidget);
Claudio Valerio's avatar
Claudio Valerio committed
185 186 187 188
    }
}


shibakaneki's avatar
shibakaneki committed
189
void UBDisplayManager::setPreviousDisplaysWidgets(QList<UBBoardView*> pPreviousViews)
Claudio Valerio's avatar
Claudio Valerio committed
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
{
    mPreviousDisplayWidgets = pPreviousViews;
}


QRect UBDisplayManager::controlGeometry()
{
    return mDesktop->screenGeometry(mControlScreenIndex);
}

QRect UBDisplayManager::displayGeometry()
{
    return mDesktop->screenGeometry(mDisplayScreenIndex);
}

shibakaneki's avatar
shibakaneki committed
205
void UBDisplayManager::reinitScreens(bool swap)
206
{
207
    Q_UNUSED(swap);
208 209 210
    adjustScreens(-1);
}

Claudio Valerio's avatar
Claudio Valerio committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224
void UBDisplayManager::adjustScreens(int screen)
{
    Q_UNUSED(screen);

    initScreenIndexes();

    positionScreens();

    emit screenLayoutChanged();
}


void UBDisplayManager::positionScreens()
{
225 226
    if(mDesktopWidget && mControlScreenIndex > -1)
    {
227
        mDesktopWidget->hide();
228
        mDesktopWidget->setGeometry(mDesktop->availableGeometry(mDesktopWidget));
229
    }
Claudio Valerio's avatar
Claudio Valerio committed
230 231
    if (mControlWidget && mControlScreenIndex > -1)
    {
232
        mControlWidget->hide();
233
        mControlWidget->setGeometry(mDesktop->availableGeometry(mControlWidget));
234
        UBPlatformUtils::showFullScreen(mControlWidget);
Claudio Valerio's avatar
Claudio Valerio committed
235 236 237 238
    }

    if (mDisplayWidget && mDisplayScreenIndex > -1)
    {
239
        mDisplayWidget->hide();
Claudio Valerio's avatar
Claudio Valerio committed
240
        mDisplayWidget->setGeometry(mDesktop->screenGeometry(mDisplayScreenIndex));
241
        UBPlatformUtils::showFullScreen(mDisplayWidget);
Claudio Valerio's avatar
Claudio Valerio committed
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
    }
    else if(mDisplayWidget)
    {
        mDisplayWidget->hide();
    }

    for (int wi = mPreviousScreenIndexes.size(); wi < mPreviousDisplayWidgets.size(); wi++)
    {
        mPreviousDisplayWidgets.at(wi)->hide();
    }

    for (int psi = 0; psi < mPreviousScreenIndexes.size(); psi++)
    {
        if (mPreviousDisplayWidgets.size() > psi)
        {
            QWidget* previous = mPreviousDisplayWidgets.at(psi);
            previous->setGeometry(mDesktop->screenGeometry(mPreviousScreenIndexes.at(psi)));
259
            UBPlatformUtils::showFullScreen(previous);
Claudio Valerio's avatar
Claudio Valerio committed
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
        }
    }

    if (mControlWidget && mControlScreenIndex > -1)
        mControlWidget->activateWindow();

}


void UBDisplayManager::blackout()
{
    QList<int> screenIndexes;

    if (mControlScreenIndex > -1)
        screenIndexes << mControlScreenIndex;

    if (mDisplayScreenIndex > -1)
        screenIndexes << mDisplayScreenIndex;

    screenIndexes << mPreviousScreenIndexes;

    for (int i = 0; i < screenIndexes.size(); i++)
    {
        int screenIndex = screenIndexes.at(i);

        UBBlackoutWidget *blackoutWidget = new UBBlackoutWidget(); //deleted in UBDisplayManager::unBlackout
        Ui::BlackoutWidget *blackoutUi = new Ui::BlackoutWidget();
        blackoutUi->setupUi(blackoutWidget);

        connect(blackoutUi->iconButton, SIGNAL(pressed()), blackoutWidget, SLOT(doActivity()));
        connect(blackoutWidget, SIGNAL(activity()), this, SLOT(unBlackout()));

        // display Uniboard logo on main screen
        blackoutUi->iconButton->setVisible(screenIndex == mControlScreenIndex);
        blackoutUi->labelClickToReturn->setVisible(screenIndex == mControlScreenIndex);

        blackoutWidget->setGeometry(mDesktop->screenGeometry(screenIndex));

        mBlackoutWidgets << blackoutWidget;
    }

    UBPlatformUtils::fadeDisplayOut();

    foreach(UBBlackoutWidget *blackoutWidget, mBlackoutWidgets)
    {
305
        UBPlatformUtils::showFullScreen(blackoutWidget);
Claudio Valerio's avatar
Claudio Valerio committed
306 307 308 309 310 311 312 313 314 315 316 317
    }
}

void UBDisplayManager::unBlackout()
{
    while (!mBlackoutWidgets.isEmpty())
    {
        // the widget is also destroyed thanks to its Qt::WA_DeleteOnClose attribute
        mBlackoutWidgets.takeFirst()->close();
    }

    UBPlatformUtils::fadeDisplayIn();
318 319 320

    UBApplication::boardController->freezeW3CWidgets(false);

Claudio Valerio's avatar
Claudio Valerio committed
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
}


void UBDisplayManager::setRoleToScreen(DisplayRole role, int screenIndex)
{
    Q_UNUSED(role);
    Q_UNUSED(screenIndex);
}


void UBDisplayManager::setUseMultiScreen(bool pUse)
{
    mUseMultiScreen = pUse;
}