UBDesktopAnnotationController.cpp 32.9 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1
/*
Craig Watson's avatar
Craig Watson committed
2 3
 * Copyright (C) 2015-2016 Département de l'Instruction Publique (DIP-SEM)
 *
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

Claudio Valerio's avatar
Claudio Valerio committed
29

Claudio Valerio's avatar
Claudio Valerio committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include <QDesktopWidget>

#include "UBDesktopAnnotationController.h"

#include "frameworks/UBPlatformUtils.h"

#include "core/UBApplication.h"
#include "core/UBApplicationController.h"
#include "core/UBDisplayManager.h"
#include "core/UBSettings.h"

#include "web/UBWebController.h"

#include "gui/UBMainWindow.h"

#include "board/UBBoardView.h"
#include "board/UBDrawingController.h"
#include "board/UBBoardController.h"
shibakaneki's avatar
shibakaneki committed
48
#include "board/UBBoardPaletteManager.h"
Claudio Valerio's avatar
Claudio Valerio committed
49 50

#include "domain/UBGraphicsScene.h"
51
#include "domain/UBGraphicsPolygonItem.h"
Claudio Valerio's avatar
Claudio Valerio committed
52 53 54 55

#include "UBCustomCaptureWindow.h"
#include "UBWindowCapture.h"
#include "UBDesktopPalette.h"
56
#include "UBDesktopPropertyPalette.h"
Claudio Valerio's avatar
Claudio Valerio committed
57 58

#include "gui/UBKeyboardPalette.h"
59
#include "gui/UBResources.h"
Claudio Valerio's avatar
Claudio Valerio committed
60

61 62
#include "core/memcheck.h"

Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
63
UBDesktopAnnotationController::UBDesktopAnnotationController(QObject *parent, UBRightPalette* rightPalette)
Claudio Valerio's avatar
Claudio Valerio committed
64 65 66 67 68 69 70
        : QObject(parent)
        , mTransparentDrawingView(0)
        , mTransparentDrawingScene(0)
        , mDesktopPalette(NULL)
        , mDesktopPenPalette(NULL)
        , mDesktopMarkerPalette(NULL)
        , mDesktopEraserPalette(NULL)
71
        , mRightPalette(rightPalette)
72
        , mWindowPositionInitialized(false)
Claudio Valerio's avatar
Claudio Valerio committed
73 74 75 76 77 78
        , mIsFullyTransparent(false)
        , mDesktopToolsPalettePositioned(false)
        , mPendingPenButtonPressed(false)
        , mPendingMarkerButtonPressed(false)
        , mPendingEraserButtonPressed(false)
        , mbArrowClicked(false)
79 80
        , mBoardStylusTool(UBDrawingController::drawingController()->stylusTool())
        , mDesktopStylusTool(UBDrawingController::drawingController()->stylusTool())
Claudio Valerio's avatar
Claudio Valerio committed
81 82
{

83
    mTransparentDrawingView = new UBBoardView(UBApplication::boardController, static_cast<QWidget*>(0), false, true); // deleted in UBDesktopAnnotationController::destructor
Claudio Valerio's avatar
Claudio Valerio committed
84
    mTransparentDrawingView->setAttribute(Qt::WA_TranslucentBackground, true);
85
#ifdef Q_OS_OSX
86
    mTransparentDrawingView->setAttribute(Qt::WA_MacNoShadow, true);
87
#endif
88
    mTransparentDrawingView->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::Window | Qt::NoDropShadowWindowHint);
Claudio Valerio's avatar
Claudio Valerio committed
89 90 91 92 93
    mTransparentDrawingView->setCacheMode(QGraphicsView::CacheNone);
    mTransparentDrawingView->resize(UBApplication::desktop()->width(), UBApplication::desktop()->height());

    mTransparentDrawingView->setMouseTracking(true);

94
    mTransparentDrawingView->setAcceptDrops(true);
Claudio Valerio's avatar
Claudio Valerio committed
95 96 97 98

    QString backgroundStyle = "QWidget {background-color: rgba(127, 127, 127, 0)}";
    mTransparentDrawingView->setStyleSheet(backgroundStyle);

Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
99
    mTransparentDrawingScene = new UBGraphicsScene(0, false);
100 101
    updateColors();

Claudio Valerio's avatar
Claudio Valerio committed
102
    mTransparentDrawingView->setScene(mTransparentDrawingScene);
shibakaneki's avatar
shibakaneki committed
103
    mTransparentDrawingScene->setDrawingMode(true);
Claudio Valerio's avatar
Claudio Valerio committed
104

Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
105 106 107
    mDesktopPalette = new UBDesktopPalette(mTransparentDrawingView, rightPalette); 
    // This was not fix, parent reverted
    // FIX #633: The palette must be 'floating' in order to stay on top of the library palette
Claudio Valerio's avatar
Claudio Valerio committed
108

109 110
    if (UBPlatformUtils::hasVirtualKeyboard())
    {
111 112 113
        connect( UBApplication::boardController->paletteManager()->mKeyboardPalette, SIGNAL(keyboardActivated(bool)), 
                 mTransparentDrawingView, SLOT(virtualKeyboardActivated(bool)));

114
#ifdef Q_OS_LINUX
115
        connect(UBApplication::boardController->paletteManager()->mKeyboardPalette, SIGNAL(moved(QPoint)), this, SLOT(refreshMask()));
116
        connect(UBApplication::mainWindow->actionVirtualKeyboard, SIGNAL(triggered(bool)), this, SLOT(refreshMask()));
117
        connect(mDesktopPalette,SIGNAL(refreshMask()), this, SLOT(refreshMask()));
118
#endif
119 120
    }

Claudio Valerio's avatar
Claudio Valerio committed
121 122 123 124
    connect(mDesktopPalette, SIGNAL(uniboardClick()), this, SLOT(goToUniboard()));
    connect(mDesktopPalette, SIGNAL(customClick()), this, SLOT(customCapture()));
    connect(mDesktopPalette, SIGNAL(windowClick()), this, SLOT(windowCapture()));
    connect(mDesktopPalette, SIGNAL(screenClick()), this, SLOT(screenCapture()));
125 126
    connect(UBApplication::mainWindow->actionPointer, SIGNAL(triggered()), this, SLOT(onToolClicked()));
    connect(UBApplication::mainWindow->actionSelector, SIGNAL(triggered()), this, SLOT(onToolClicked()));
Claudio Valerio's avatar
Claudio Valerio committed
127 128
    connect(mDesktopPalette, SIGNAL(maximized()), this, SLOT(onDesktopPaletteMaximized()));
    connect(mDesktopPalette, SIGNAL(minimizeStart(eMinimizedLocation)), this, SLOT(onDesktopPaletteMinimize()));
129 130
    connect(mDesktopPalette, SIGNAL(mouseEntered()), mTransparentDrawingScene, SLOT(hideTool()));
    connect(mRightPalette, SIGNAL(mouseEntered()), mTransparentDrawingScene, SLOT(hideTool()));
131

Claudio Valerio's avatar
Claudio Valerio committed
132 133 134
    connect(mTransparentDrawingView, SIGNAL(resized(QResizeEvent*)), this, SLOT(onTransparentWidgetResized()));


135
    connect(UBDrawingController::drawingController(), SIGNAL(stylusToolChanged(int)), this, SLOT(stylusToolChanged(int)));
Claudio Valerio's avatar
Claudio Valerio committed
136 137

    // Add the desktop associated palettes
138
    mDesktopPenPalette = new UBDesktopPenPalette(mTransparentDrawingView, rightPalette); 
Claudio Valerio's avatar
Claudio Valerio committed
139 140 141 142

    connect(mDesktopPalette, SIGNAL(maximized()), mDesktopPenPalette, SLOT(onParentMaximized()));
    connect(mDesktopPalette, SIGNAL(minimizeStart(eMinimizedLocation)), mDesktopPenPalette, SLOT(onParentMinimized()));

143 144
    mDesktopMarkerPalette = new UBDesktopMarkerPalette(mTransparentDrawingView, rightPalette);
    mDesktopEraserPalette = new UBDesktopEraserPalette(mTransparentDrawingView, rightPalette);
Claudio Valerio's avatar
Claudio Valerio committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

    mDesktopPalette->setBackgroundBrush(UBSettings::settings()->opaquePaletteColor);
    mDesktopPenPalette->setBackgroundBrush(UBSettings::settings()->opaquePaletteColor);
    mDesktopMarkerPalette->setBackgroundBrush(UBSettings::settings()->opaquePaletteColor);
    mDesktopEraserPalette->setBackgroundBrush(UBSettings::settings()->opaquePaletteColor);


    // Hack : the size of the property palettes is computed the first time the palette is visible
    //        In order to prevent palette overlap on if the desktop palette is on the right of the
    //        screen, a setVisible(true) followed by a setVisible(false) is done.
    mDesktopPenPalette->setVisible(true);
    mDesktopMarkerPalette->setVisible(true);
    mDesktopEraserPalette->setVisible(true);
    mDesktopPenPalette->setVisible(false);
    mDesktopMarkerPalette->setVisible(false);
    mDesktopEraserPalette->setVisible(false);

    connect(UBApplication::mainWindow->actionEraseDesktopAnnotations, SIGNAL(triggered()), this, SLOT(eraseDesktopAnnotations()));
163 164
    connect(UBApplication::boardController, SIGNAL(backgroundChanged()), this, SLOT(updateColors()));
    connect(UBApplication::boardController, SIGNAL(activeSceneChanged()), this, SLOT(updateColors()));
Claudio Valerio's avatar
Claudio Valerio committed
165 166 167 168
    connect(&mHoldTimerPen, SIGNAL(timeout()), this, SLOT(penActionReleased()));
    connect(&mHoldTimerMarker, SIGNAL(timeout()), this, SLOT(markerActionReleased()));
    connect(&mHoldTimerEraser, SIGNAL(timeout()), this, SLOT(eraserActionReleased()));

169
#ifdef Q_OS_LINUX
170
    connect(mDesktopPalette, SIGNAL(moving()), this, SLOT(refreshMask()));
171
    connect(UBApplication::boardController->paletteManager()->rightPalette(), SIGNAL(resized()), this, SLOT(refreshMask()));
172
    connect(UBApplication::boardController->paletteManager()->addItemPalette(), SIGNAL(closed()), this, SLOT(refreshMask()));
173
#endif
Claudio Valerio's avatar
Claudio Valerio committed
174
    onDesktopPaletteMaximized();
shibakaneki's avatar
shibakaneki committed
175 176

    // FIX #633: Ensure that these palettes stay on top of the other elements
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
177 178 179
    //mDesktopEraserPalette->raise();
    //mDesktopMarkerPalette->raise();
    //mDesktopPenPalette->raise();
Claudio Valerio's avatar
Claudio Valerio committed
180 181 182 183 184 185 186 187
}

UBDesktopAnnotationController::~UBDesktopAnnotationController()
{
    delete mTransparentDrawingScene;
    delete mTransparentDrawingView;
}

188 189
void UBDesktopAnnotationController::updateColors(){
    if(UBApplication::boardController->activeScene()->isDarkBackground()){
Craig Watson's avatar
Craig Watson committed
190
        mTransparentDrawingScene->setBackground(true, UBPageBackground::plain);
191
    }else{
Craig Watson's avatar
Craig Watson committed
192
        mTransparentDrawingScene->setBackground(false, UBPageBackground::plain);
193 194
    }
}
Claudio Valerio's avatar
Claudio Valerio committed
195 196 197 198 199 200

UBDesktopPalette* UBDesktopAnnotationController::desktopPalette()
{
    return mDesktopPalette;
}

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
QPainterPath UBDesktopAnnotationController::desktopPalettePath() const
{
    QPainterPath result;
    if (mDesktopPalette && mDesktopPalette->isVisible()) {
        result.addRect(mDesktopPalette->geometry());
    }
    if (mDesktopPenPalette && mDesktopPenPalette->isVisible()) {
        result.addRect(mDesktopPenPalette->geometry());
    }
    if (mDesktopMarkerPalette && mDesktopMarkerPalette->isVisible()) {
        result.addRect(mDesktopMarkerPalette->geometry());
    }
    if (mDesktopEraserPalette && mDesktopEraserPalette->isVisible()) {
        result.addRect(mDesktopEraserPalette->geometry());
    }

    return result;
}

Claudio Valerio's avatar
Claudio Valerio committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
/**
 * \brief Toggle the visibility of the pen associated palette
 * @param checked as the visibility state
 */
void UBDesktopAnnotationController::desktopPenActionToggled(bool checked)
{
    setAssociatedPalettePosition(mDesktopPenPalette, "actionPen");
    mDesktopPenPalette->setVisible(checked);
    mDesktopMarkerPalette->setVisible(false);
    mDesktopEraserPalette->setVisible(false);
}

/**
 * \brief Toggle the visibility of the marker associated palette
 * @param checked as the visibility state
 */
void UBDesktopAnnotationController::desktopMarkerActionToggled(bool checked)
{
    setAssociatedPalettePosition(mDesktopMarkerPalette, "actionMarker");
    mDesktopMarkerPalette->setVisible(checked);
    mDesktopPenPalette->setVisible(false);
    mDesktopEraserPalette->setVisible(false);
}

/**
 * \brief Toggle the visibility of the eraser associated palette
 * @param checked as the visibility state
 */
void UBDesktopAnnotationController::desktopEraserActionToggled(bool checked)
{
    setAssociatedPalettePosition(mDesktopEraserPalette, "actionEraser");
    mDesktopEraserPalette->setVisible(checked);
    mDesktopPenPalette->setVisible(false);
    mDesktopMarkerPalette->setVisible(false);
}

/**
 * \brief Set the location of the properties palette
 * @param palette as the palette
 * @param actionName as the name of the related action
 */
void UBDesktopAnnotationController::setAssociatedPalettePosition(UBActionPalette *palette, const QString &actionName)
{
    QPoint desktopPalettePos = mDesktopPalette->geometry().topLeft();
    QList<QAction*> actions = mDesktopPalette->actions();
    int yPen = 0;

    foreach(QAction* act, actions)
    {
        if(act->objectName() == actionName)
        {
            int iAction = actions.indexOf(act);
shibakaneki's avatar
shibakaneki committed
272
            yPen = iAction * (mDesktopPalette->buttonSize().height() + 2 * mDesktopPalette->border() +6); // This is the mysterious value (6)
Claudio Valerio's avatar
Claudio Valerio committed
273 274 275 276 277
            break;
        }
    }

    // First determine if the palette must be shown on the left or on the right
278
    if(desktopPalettePos.x() <= (mTransparentDrawingView->width() - (palette->width() + mDesktopPalette->width() + mRightPalette->width() + 20))) // we take a small margin of 20 pixels
Claudio Valerio's avatar
Claudio Valerio committed
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
    {
        // Display it on the right
        desktopPalettePos += QPoint(mDesktopPalette->width(), yPen);
    }
    else
    {
        // Display it on the left
        desktopPalettePos += QPoint(0 - palette->width(), yPen);
    }

    palette->setCustomPosition(true);
    palette->move(desktopPalettePos);
}

void UBDesktopAnnotationController::eraseDesktopAnnotations()
{
    if (mTransparentDrawingScene)
    {
Ilia Ryabokon's avatar
Ilia Ryabokon committed
297
        mTransparentDrawingScene->clearContent(UBGraphicsScene::clearAnnotations);
Claudio Valerio's avatar
Claudio Valerio committed
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
    }
}


UBBoardView* UBDesktopAnnotationController::drawingView()
{
    return mTransparentDrawingView;
}


void UBDesktopAnnotationController::showWindow()
{
    mDesktopPalette->setDisplaySelectButtonVisible(true);

    connect(UBApplication::applicationController, SIGNAL(desktopMode(bool))
            , mDesktopPalette, SLOT(setDisplaySelectButtonVisible(bool)));

    mDesktopPalette->show();

    bool showDisplay = UBSettings::settings()->webShowPageImmediatelyOnMirroredScreen->get().toBool();

    mDesktopPalette->showHideClick(showDisplay);
    mDesktopPalette->updateShowHideState(showDisplay);

    if (!mWindowPositionInitialized)
    {
        QRect desktopRect = QApplication::desktop()->screenGeometry(mDesktopPalette->pos());

        mDesktopPalette->move(5, desktopRect.top() + 150);

        mWindowPositionInitialized = true;
329
        mDesktopPalette->maximizeMe();
Claudio Valerio's avatar
Claudio Valerio committed
330 331 332 333 334 335 336 337
    }

    updateBackground();

    mBoardStylusTool = UBDrawingController::drawingController()->stylusTool();

    UBDrawingController::drawingController()->setStylusTool(mDesktopStylusTool);

338
#ifndef Q_OS_LINUX
339
    UBPlatformUtils::showFullScreen(mTransparentDrawingView);
340
#else
341 342
    // this is necessary to avoid hiding the panels on Unity and Cinnamon
    // if finer control is necessary, use qgetenv("XDG_CURRENT_DESKTOP")
343 344
    mTransparentDrawingView->show();
#endif
Claudio Valerio's avatar
Claudio Valerio committed
345 346 347
    UBPlatformUtils::setDesktopMode(true);

    mDesktopPalette->appear();
348

349
#ifdef Q_OS_LINUX
350 351
    updateMask(true);
#endif
Claudio Valerio's avatar
Claudio Valerio committed
352 353 354 355 356
}


void UBDesktopAnnotationController::close()
{
357 358 359 360
    if (mTransparentDrawingView)
        mTransparentDrawingView->hide();

    mDesktopPalette->hide();
Claudio Valerio's avatar
Claudio Valerio committed
361 362 363 364 365
}


void UBDesktopAnnotationController::stylusToolChanged(int tool)
{
366
    Q_UNUSED(tool);
367 368 369 370 371 372
//     UBStylusTool::Enum eTool = (UBStylusTool::Enum)tool;
//     if(eTool != UBStylusTool::Selector && eTool != UBStylusTool::Text)
//     {
//         if(mKeyboardPalette->m_isVisible)
//             UBApplication::mainWindow->actionVirtualKeyboard->activate(QAction::Trigger);
//     }
373

Claudio Valerio's avatar
Claudio Valerio committed
374 375 376 377 378 379 380 381 382 383 384 385
    updateBackground();
}


void UBDesktopAnnotationController::updateBackground()
{
    QBrush newBrush;

    if (mIsFullyTransparent
            || UBDrawingController::drawingController()->stylusTool() == UBStylusTool::Selector)
    {
        newBrush = QBrush(Qt::transparent);
386
#ifdef Q_OS_LINUX
387 388
        updateMask(true);
#endif
Claudio Valerio's avatar
Claudio Valerio committed
389 390 391
    }
    else
    {
392
#if defined(Q_OS_OSX)
Claudio Valerio's avatar
Claudio Valerio committed
393 394 395
        newBrush = QBrush(QColor(127, 127, 127, 15));
#else
        newBrush = QBrush(QColor(127, 127, 127, 1));
396
#endif
397
#ifdef Q_OS_LINUX
398
        updateMask(false);
Claudio Valerio's avatar
Claudio Valerio committed
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
#endif
    }

    if (mTransparentDrawingScene && mTransparentDrawingScene->backgroundBrush() != newBrush)
        mTransparentDrawingScene->setBackgroundBrush(newBrush);
}


void UBDesktopAnnotationController::hideWindow()
{
    if (mTransparentDrawingView)
        mTransparentDrawingView->hide();

    mDesktopPalette->hide();

    mDesktopStylusTool = UBDrawingController::drawingController()->stylusTool();
    UBDrawingController::drawingController()->setStylusTool(mBoardStylusTool);
}


void UBDesktopAnnotationController::goToUniboard()
{
421
    UBApplication::applicationController->showBoard();
Claudio Valerio's avatar
Claudio Valerio committed
422 423 424 425 426
}


void UBDesktopAnnotationController::customCapture()
{
Claudio Valerio's avatar
Claudio Valerio committed
427
    onToolClicked();
Claudio Valerio's avatar
Claudio Valerio committed
428 429 430 431 432 433
    mIsFullyTransparent = true;
    updateBackground();

    mDesktopPalette->disappearForCapture();
    UBCustomCaptureWindow customCaptureWindow(mDesktopPalette);
    // need to show the window before execute it to avoid some glitch on windows.
434

435
#ifndef Q_OS_WIN // Working only without this call on win32 desktop mode
436
    UBPlatformUtils::showFullScreen(&customCaptureWindow);
437
#endif
Claudio Valerio's avatar
Claudio Valerio committed
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453

    if (customCaptureWindow.execute(getScreenPixmap()) == QDialog::Accepted)
    {
        QPixmap selectedPixmap = customCaptureWindow.getSelectedPixmap();
        emit imageCaptured(selectedPixmap, false);
    }

    mDesktopPalette->appear();

    mIsFullyTransparent = false;
    updateBackground();
}


void UBDesktopAnnotationController::windowCapture()
{
Claudio Valerio's avatar
Claudio Valerio committed
454
    onToolClicked();
Claudio Valerio's avatar
Claudio Valerio committed
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
    mIsFullyTransparent = true;
    updateBackground();

    mDesktopPalette->disappearForCapture();

    UBWindowCapture util(this);

    if (util.execute() == QDialog::Accepted)
    {
        QPixmap windowPixmap = util.getCapturedWindow();

        // on Mac OS X we can only know that user cancel the operatiion by checking is the image is null
        // because the screencapture utility always return code 0 event if user cancel the application
        if (!windowPixmap.isNull())
        {
            emit imageCaptured(windowPixmap, false);
        }
    }

    mDesktopPalette->appear();

    mIsFullyTransparent = false;

    updateBackground();
}


void UBDesktopAnnotationController::screenCapture()
{
484
    onToolClicked();
Claudio Valerio's avatar
Claudio Valerio committed
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
    mIsFullyTransparent = true;
    updateBackground();

    mDesktopPalette->disappearForCapture();

    QPixmap originalPixmap = getScreenPixmap();

    mDesktopPalette->appear();

    emit imageCaptured(originalPixmap, false);

    mIsFullyTransparent = false;

    updateBackground();
}


QPixmap UBDesktopAnnotationController::getScreenPixmap()
{
    QDesktopWidget *desktop = QApplication::desktop();
505
    QScreen * screen = UBApplication::controlScreen();
506

507 508 509 510
    QRect rect = desktop->screenGeometry(QCursor::pos());

    return screen->grabWindow(desktop->effectiveWinId(),
                              rect.x(), rect.y(), rect.width(), rect.height());
Claudio Valerio's avatar
Claudio Valerio committed
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
}


void UBDesktopAnnotationController::updateShowHideState(bool pEnabled)
{
    mDesktopPalette->updateShowHideState(pEnabled);
}


void UBDesktopAnnotationController::screenLayoutChanged()
{
    if (UBApplication::applicationController &&
            UBApplication::applicationController->displayManager() &&
            UBApplication::applicationController->displayManager()->hasDisplay())
    {
        mDesktopPalette->setShowHideButtonVisible(true);
    }
    else
    {
        mDesktopPalette->setShowHideButtonVisible(false);
    }
}

/**
 * \brief Handles the pen action pressed event
 */
void UBDesktopAnnotationController::penActionPressed()
{
    mbArrowClicked = false;
    mDesktopMarkerPalette->hide();
    mDesktopEraserPalette->hide();
    UBDrawingController::drawingController()->setStylusTool(UBStylusTool::Pen);
    mPenHoldTimer = QTime::currentTime();
    mPendingPenButtonPressed = true;

    // Check if the mouse cursor is on the little arrow
    QPoint cursorPos = QCursor::pos();
    QPoint palettePos = mDesktopPalette->pos();
    QPoint buttonPos = mDesktopPalette->buttonPos(UBApplication::mainWindow->actionPen);

    int iX = cursorPos.x() - (palettePos.x() + buttonPos.x());    // x position of the cursor in the palette
    int iY = cursorPos.y() - (palettePos.y() + buttonPos.y());    // y position of the cursor in the palette

    if(iX >= 37 && iX <= 44 && iY >= 37 && iY <= 44)
    {
        mbArrowClicked = true;
        penActionReleased();
    }
    else
    {
        mHoldTimerPen.start(PROPERTY_PALETTE_TIMER);
    }
}

/**
 * \brief Handles the pen action released event
 */
void UBDesktopAnnotationController::penActionReleased()
{
    mHoldTimerPen.stop();
    if(mPendingPenButtonPressed)
    {
        if(mbArrowClicked || mPenHoldTimer.msecsTo(QTime::currentTime()) > PROPERTY_PALETTE_TIMER - 100)
        {
            togglePropertyPalette(mDesktopPenPalette);
        }
        else
        {
            UBApplication::mainWindow->actionPen->trigger();
        }
        mPendingPenButtonPressed = false;
    }
    UBApplication::mainWindow->actionPen->setChecked(true);
584 585

    switchCursor(UBStylusTool::Pen);
Claudio Valerio's avatar
Claudio Valerio committed
586 587 588
}

/**
589
 * \brief Handles the eraser action pressed event
Claudio Valerio's avatar
Claudio Valerio committed
590
 */
591
void UBDesktopAnnotationController::eraserActionPressed()
Claudio Valerio's avatar
Claudio Valerio committed
592 593 594
{
    mbArrowClicked = false;
    mDesktopPenPalette->hide();
595 596 597 598
    mDesktopMarkerPalette->hide();
    UBDrawingController::drawingController()->setStylusTool(UBStylusTool::Eraser);
    mEraserHoldTimer = QTime::currentTime();
    mPendingEraserButtonPressed = true;
Claudio Valerio's avatar
Claudio Valerio committed
599 600 601 602

    // Check if the mouse cursor is on the little arrow
    QPoint cursorPos = QCursor::pos();
    QPoint palettePos = mDesktopPalette->pos();
603
    QPoint buttonPos = mDesktopPalette->buttonPos(UBApplication::mainWindow->actionEraser);
Claudio Valerio's avatar
Claudio Valerio committed
604 605 606 607 608 609 610

    int iX = cursorPos.x() - (palettePos.x() + buttonPos.x());    // x position of the cursor in the palette
    int iY = cursorPos.y() - (palettePos.y() + buttonPos.y());    // y position of the cursor in the palette

    if(iX >= 37 && iX <= 44 && iY >= 37 && iY <= 44)
    {
        mbArrowClicked = true;
611
        eraserActionReleased();
Claudio Valerio's avatar
Claudio Valerio committed
612 613 614
    }
    else
    {
615
        mHoldTimerEraser.start(PROPERTY_PALETTE_TIMER);
Claudio Valerio's avatar
Claudio Valerio committed
616 617 618 619
    }
}

/**
620
 * \brief Handles the eraser action released event
Claudio Valerio's avatar
Claudio Valerio committed
621
 */
622
void UBDesktopAnnotationController::eraserActionReleased()
Claudio Valerio's avatar
Claudio Valerio committed
623
{
624 625
    mHoldTimerEraser.stop();
    if(mPendingEraserButtonPressed)
Claudio Valerio's avatar
Claudio Valerio committed
626
    {
627
        if(mbArrowClicked || mEraserHoldTimer.msecsTo(QTime::currentTime()) > PROPERTY_PALETTE_TIMER - 100)
Claudio Valerio's avatar
Claudio Valerio committed
628
        {
629
            togglePropertyPalette(mDesktopEraserPalette);
Claudio Valerio's avatar
Claudio Valerio committed
630 631 632
        }
        else
        {
633
            UBApplication::mainWindow->actionEraser->trigger();
Claudio Valerio's avatar
Claudio Valerio committed
634
        }
635
        mPendingEraserButtonPressed = false;
Claudio Valerio's avatar
Claudio Valerio committed
636
    }
637 638 639
    UBApplication::mainWindow->actionEraser->setChecked(true);

    switchCursor(UBStylusTool::Eraser);
Claudio Valerio's avatar
Claudio Valerio committed
640 641
}

642

Claudio Valerio's avatar
Claudio Valerio committed
643
/**
644
 * \brief Handles the marker action pressed event
Claudio Valerio's avatar
Claudio Valerio committed
645
 */
646
void UBDesktopAnnotationController::markerActionPressed()
Claudio Valerio's avatar
Claudio Valerio committed
647 648 649
{
    mbArrowClicked = false;
    mDesktopPenPalette->hide();
650 651 652 653
    mDesktopEraserPalette->hide();
    UBDrawingController::drawingController()->setStylusTool(UBStylusTool::Marker);
    mMarkerHoldTimer = QTime::currentTime();
    mPendingMarkerButtonPressed = true;
Claudio Valerio's avatar
Claudio Valerio committed
654 655 656 657

    // Check if the mouse cursor is on the little arrow
    QPoint cursorPos = QCursor::pos();
    QPoint palettePos = mDesktopPalette->pos();
658
    QPoint buttonPos = mDesktopPalette->buttonPos(UBApplication::mainWindow->actionMarker);
Claudio Valerio's avatar
Claudio Valerio committed
659 660 661 662 663 664 665

    int iX = cursorPos.x() - (palettePos.x() + buttonPos.x());    // x position of the cursor in the palette
    int iY = cursorPos.y() - (palettePos.y() + buttonPos.y());    // y position of the cursor in the palette

    if(iX >= 37 && iX <= 44 && iY >= 37 && iY <= 44)
    {
        mbArrowClicked = true;
666
        markerActionReleased();
Claudio Valerio's avatar
Claudio Valerio committed
667 668 669
    }
    else
    {
670
        mHoldTimerMarker.start(PROPERTY_PALETTE_TIMER);
Claudio Valerio's avatar
Claudio Valerio committed
671 672 673
    }
}

674

Claudio Valerio's avatar
Claudio Valerio committed
675
/**
676
 * \brief Handles the marker action released event
Claudio Valerio's avatar
Claudio Valerio committed
677
 */
678
void UBDesktopAnnotationController::markerActionReleased()
Claudio Valerio's avatar
Claudio Valerio committed
679
{
680 681
    mHoldTimerMarker.stop();
    if(mPendingMarkerButtonPressed)
Claudio Valerio's avatar
Claudio Valerio committed
682
    {
683
        if(mbArrowClicked || mMarkerHoldTimer.msecsTo(QTime::currentTime()) > PROPERTY_PALETTE_TIMER - 100)
Claudio Valerio's avatar
Claudio Valerio committed
684
        {
685
            togglePropertyPalette(mDesktopMarkerPalette);
Claudio Valerio's avatar
Claudio Valerio committed
686 687 688
        }
        else
        {
689
            UBApplication::mainWindow->actionMarker->trigger();
Claudio Valerio's avatar
Claudio Valerio committed
690
        }
691
        mPendingMarkerButtonPressed = false;
Claudio Valerio's avatar
Claudio Valerio committed
692
    }
693 694 695
    UBApplication::mainWindow->actionMarker->setChecked(true);

    switchCursor(UBStylusTool::Marker);
Claudio Valerio's avatar
Claudio Valerio committed
696 697
}

698 699 700 701 702 703 704
void UBDesktopAnnotationController::selectorActionPressed()
{

}

void UBDesktopAnnotationController::selectorActionReleased()
{
705
    UBApplication::mainWindow->actionSelector->setChecked(true);
706 707 708 709 710 711 712 713 714 715 716
    switchCursor(UBStylusTool::Selector);
}


void UBDesktopAnnotationController::pointerActionPressed()
{

}

void UBDesktopAnnotationController::pointerActionReleased()
{
717
    UBApplication::mainWindow->actionPointer->setChecked(true);
718 719 720 721
    switchCursor(UBStylusTool::Pointer);
}


Claudio Valerio's avatar
Claudio Valerio committed
722
/**
723
 * \brief Toggle the given palette visibility
Claudio Valerio's avatar
Claudio Valerio committed
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
 * @param palette as the given palette
 */
void UBDesktopAnnotationController::togglePropertyPalette(UBActionPalette *palette)
{
    if(NULL != palette)
    {
        bool bShow = !palette->isVisible();
        if(mDesktopPenPalette == palette)
        {
            desktopPenActionToggled(bShow);
        }
        else if(mDesktopMarkerPalette == palette)
        {
            desktopMarkerActionToggled(bShow);
        }
        else if(mDesktopEraserPalette == palette)
        {
            desktopEraserActionToggled(bShow);
        }
    }
}

746 747 748 749 750 751 752

void UBDesktopAnnotationController::switchCursor(const int tool)
{
    mTransparentDrawingScene->setToolCursor(tool);
    mTransparentDrawingView->setToolCursor(tool);
}

Claudio Valerio's avatar
Claudio Valerio committed
753 754 755 756 757 758 759 760 761 762 763 764 765
/**
 * \brief Reconnect the pressed & released signals of the property palettes
 */
void UBDesktopAnnotationController::onDesktopPaletteMaximized()
{
    // Pen
    UBActionPaletteButton* pPenButton = mDesktopPalette->getButtonFromAction(UBApplication::mainWindow->actionPen);
    if(NULL != pPenButton)
    {
        connect(pPenButton, SIGNAL(pressed()), this, SLOT(penActionPressed()));
        connect(pPenButton, SIGNAL(released()), this, SLOT(penActionReleased()));
    }

766 767 768 769 770 771 772 773
    // Eraser
    UBActionPaletteButton* pEraserButton = mDesktopPalette->getButtonFromAction(UBApplication::mainWindow->actionEraser);
    if(NULL != pEraserButton)
    {
        connect(pEraserButton, SIGNAL(pressed()), this, SLOT(eraserActionPressed()));
        connect(pEraserButton, SIGNAL(released()), this, SLOT(eraserActionReleased()));
    }

Claudio Valerio's avatar
Claudio Valerio committed
774 775 776 777 778 779 780 781
    // Marker
    UBActionPaletteButton* pMarkerButton = mDesktopPalette->getButtonFromAction(UBApplication::mainWindow->actionMarker);
    if(NULL != pMarkerButton)
    {
        connect(pMarkerButton, SIGNAL(pressed()), this, SLOT(markerActionPressed()));
        connect(pMarkerButton, SIGNAL(released()), this, SLOT(markerActionReleased()));
    }

782 783 784
    // Pointer
    UBActionPaletteButton* pSelectorButton = mDesktopPalette->getButtonFromAction(UBApplication::mainWindow->actionSelector);
    if(NULL != pSelectorButton)
Claudio Valerio's avatar
Claudio Valerio committed
785
    {
786 787
        connect(pSelectorButton, SIGNAL(pressed()), this, SLOT(selectorActionPressed()));
        connect(pSelectorButton, SIGNAL(released()), this, SLOT(selectorActionReleased()));
Claudio Valerio's avatar
Claudio Valerio committed
788
    }
789 790 791 792 793 794 795 796

    // Pointer
    UBActionPaletteButton* pPointerButton = mDesktopPalette->getButtonFromAction(UBApplication::mainWindow->actionPointer);
    if(NULL != pPointerButton)
    {
        connect(pPointerButton, SIGNAL(pressed()), this, SLOT(pointerActionPressed()));
        connect(pPointerButton, SIGNAL(released()), this, SLOT(pointerActionReleased()));
    }
Claudio Valerio's avatar
Claudio Valerio committed
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
}

/**
 * \brief Disconnect the pressed & release signals of the property palettes
 * This is done to prevent memory leaks
 */
void UBDesktopAnnotationController::onDesktopPaletteMinimize()
{
    // Pen
    UBActionPaletteButton* pPenButton = mDesktopPalette->getButtonFromAction(UBApplication::mainWindow->actionPen);
    if(NULL != pPenButton)
    {
        disconnect(pPenButton, SIGNAL(pressed()), this, SLOT(penActionPressed()));
        disconnect(pPenButton, SIGNAL(released()), this, SLOT(penActionReleased()));
    }

    // Marker
    UBActionPaletteButton* pMarkerButton = mDesktopPalette->getButtonFromAction(UBApplication::mainWindow->actionMarker);
    if(NULL != pMarkerButton)
    {
        disconnect(pMarkerButton, SIGNAL(pressed()), this, SLOT(markerActionPressed()));
        disconnect(pMarkerButton, SIGNAL(released()), this, SLOT(markerActionReleased()));
    }

    // Eraser
    UBActionPaletteButton* pEraserButton = mDesktopPalette->getButtonFromAction(UBApplication::mainWindow->actionEraser);
    if(NULL != pEraserButton)
    {
        disconnect(pEraserButton, SIGNAL(pressed()), this, SLOT(eraserActionPressed()));
        disconnect(pEraserButton, SIGNAL(released()), this, SLOT(eraserActionReleased()));
    }
}

830 831 832 833 834
void UBDesktopAnnotationController::TransparentWidgetResized()
{
    onTransparentWidgetResized();
}

Claudio Valerio's avatar
Claudio Valerio committed
835 836 837 838 839
/**
 * \brief Resize the library palette.
 */
void UBDesktopAnnotationController::onTransparentWidgetResized()
{
840
    int rW = UBApplication::boardController->paletteManager()->rightPalette()->width();
841 842
    int lW = UBApplication::boardController->paletteManager()->leftPalette()->width();

843 844 845 846 847
    int rH = mTransparentDrawingView->height();

    UBApplication::boardController->paletteManager()->rightPalette()->resize(rW+1, rH);
    UBApplication::boardController->paletteManager()->rightPalette()->resize(rW, rH);

848 849
    UBApplication::boardController->paletteManager()->leftPalette()->resize(lW+1, rH);
    UBApplication::boardController->paletteManager()->leftPalette()->resize(lW, rH);
Claudio Valerio's avatar
Claudio Valerio committed
850
}
851 852 853 854 855

void UBDesktopAnnotationController::updateMask(bool bTransparent)
{
    if(bTransparent)
    {
Ivan Ilin's avatar
Ivan Ilin committed
856
        // Here we have to generate a new mask This method is certainly resource
857 858
        // consuming but for the moment this is the only solution that I found.
        mMask = QPixmap(mTransparentDrawingView->width(), mTransparentDrawingView->height());
859
        mMask.fill(Qt::transparent);
860 861 862 863 864 865 866 867 868 869 870 871 872

        QPainter p;

        p.begin(&mMask);

        p.setPen(Qt::red);
        p.setBrush(QBrush(Qt::red));

        // Here we draw the widget mask
        if(mDesktopPalette->isVisible())
        {
            p.drawRect(mDesktopPalette->geometry().x(), mDesktopPalette->geometry().y(), mDesktopPalette->width(), mDesktopPalette->height());
        }
873
        if(UBApplication::boardController->paletteManager()->mKeyboardPalette->isVisible())
874
        {
875 876
            p.drawRect(UBApplication::boardController->paletteManager()->mKeyboardPalette->geometry().x(), UBApplication::boardController->paletteManager()->mKeyboardPalette->geometry().y(),
                       UBApplication::boardController->paletteManager()->mKeyboardPalette->width(), UBApplication::boardController->paletteManager()->mKeyboardPalette->height());
877
        }
878

879 880 881 882 883 884 885 886 887 888 889 890 891
        if(UBApplication::boardController->paletteManager()->leftPalette()->isVisible())
        {
            QRect leftPalette(UBApplication::boardController->paletteManager()->leftPalette()->geometry().x(),
                        UBApplication::boardController->paletteManager()->leftPalette()->geometry().y(),
                        UBApplication::boardController->paletteManager()->leftPalette()->width(),
                        UBApplication::boardController->paletteManager()->leftPalette()->height());

            QRect tabsPalette(UBApplication::boardController->paletteManager()->leftPalette()->getTabPaletteRect());

            p.drawRect(leftPalette);
            p.drawRect(tabsPalette);
        }

892
        if(UBApplication::boardController->paletteManager()->rightPalette()->isVisible())
893
        {
Ivan Ilin's avatar
Ivan Ilin committed
894 895 896 897 898 899 900 901 902
            QRect rightPalette(UBApplication::boardController->paletteManager()->rightPalette()->geometry().x(),
                        UBApplication::boardController->paletteManager()->rightPalette()->geometry().y(),
                        UBApplication::boardController->paletteManager()->rightPalette()->width(),
                        UBApplication::boardController->paletteManager()->rightPalette()->height());

            QRect tabsPalette(UBApplication::boardController->paletteManager()->rightPalette()->getTabPaletteRect());

            p.drawRect(rightPalette);
            p.drawRect(tabsPalette);
903 904
        }

905
#ifdef Q_OS_LINUX
906 907 908 909 910 911 912 913 914
        //Rquiered only for compiz wm
        //TODO. Window manager detection screen

        if (UBApplication::boardController->paletteManager()->addItemPalette()->isVisible()) {
            p.drawRect(UBApplication::boardController->paletteManager()->addItemPalette()->geometry());
        }

#endif

915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
        p.end();

        // Then we add the annotations. We create another painter because we need to
        // apply transformations on it for coordinates matching
        QPainter annotationPainter;

        QTransform trans;
        trans.translate(mTransparentDrawingView->width()/2, mTransparentDrawingView->height()/2);

        annotationPainter.begin(&mMask);
        annotationPainter.setPen(Qt::red);
        annotationPainter.setBrush(Qt::red);

        annotationPainter.setTransform(trans);

        QList<QGraphicsItem*> allItems = mTransparentDrawingScene->items();

        for(int i = 0; i < allItems.size(); i++)
        {
            QGraphicsItem* pCrntItem = allItems.at(i);

936
            if(pCrntItem->isVisible() && pCrntItem->type() == UBGraphicsPolygonItem::Type)
937 938 939 940 941 942 943 944 945 946
            {
                QPainterPath crntPath = pCrntItem->shape();
                QRectF rect = crntPath.boundingRect();

                annotationPainter.drawRect(rect);
            }
        }

        annotationPainter.end();

947
        mTransparentDrawingView->setMask(mMask.mask());
948 949 950 951 952 953 954 955 956 957 958
    }
    else
    {
        // Remove the mask
        QPixmap noMask(mTransparentDrawingView->width(), mTransparentDrawingView->height());
        mTransparentDrawingView->setMask(noMask.mask());
    }
}

void UBDesktopAnnotationController::refreshMask()
{
959 960 961 962 963 964 965 966 967 968 969
    if (mTransparentDrawingScene && mTransparentDrawingView->isVisible()) {
        if(mIsFullyTransparent
                || UBDrawingController::drawingController()->stylusTool() == UBStylusTool::Selector
                //Needed to work correctly when another actions on stylus are checked
                || UBDrawingController::drawingController()->stylusTool() == UBStylusTool::Eraser
                || UBDrawingController::drawingController()->stylusTool() == UBStylusTool::Pointer
                || UBDrawingController::drawingController()->stylusTool() == UBStylusTool::Pen
                || UBDrawingController::drawingController()->stylusTool() == UBStylusTool::Marker)
        {
            updateMask(true);
        }
970
    }
971
}
shibakaneki's avatar
shibakaneki committed
972

973 974
void UBDesktopAnnotationController::onToolClicked()
{
Claudio Valerio's avatar
Claudio Valerio committed
975 976 977
    mDesktopEraserPalette->hide();
    mDesktopMarkerPalette->hide();
    mDesktopPenPalette->hide();
shibakaneki's avatar
shibakaneki committed
978
}