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

25

Claudio Valerio's avatar
Claudio Valerio committed
26

Claudio Valerio's avatar
Claudio Valerio committed
27

Claudio Valerio's avatar
Claudio Valerio committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#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
46
#include "board/UBBoardPaletteManager.h"
Claudio Valerio's avatar
Claudio Valerio committed
47 48

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

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

#include "gui/UBKeyboardPalette.h"
57
#include "gui/UBResources.h"
Claudio Valerio's avatar
Claudio Valerio committed
58

59 60
#include "core/memcheck.h"

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

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

    mTransparentDrawingView->setMouseTracking(true);

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

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

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

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

Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
103 104 105
    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
106

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

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

Claudio Valerio's avatar
Claudio Valerio committed
119 120 121 122
    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()));
123 124
    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
125 126
    connect(mDesktopPalette, SIGNAL(maximized()), this, SLOT(onDesktopPaletteMaximized()));
    connect(mDesktopPalette, SIGNAL(minimizeStart(eMinimizedLocation)), this, SLOT(onDesktopPaletteMinimize()));
127

Claudio Valerio's avatar
Claudio Valerio committed
128 129 130
    connect(mTransparentDrawingView, SIGNAL(resized(QResizeEvent*)), this, SLOT(onTransparentWidgetResized()));


131
    connect(UBDrawingController::drawingController(), SIGNAL(stylusToolChanged(int)), this, SLOT(stylusToolChanged(int)));
Claudio Valerio's avatar
Claudio Valerio committed
132 133

    // Add the desktop associated palettes
134
    mDesktopPenPalette = new UBDesktopPenPalette(mTransparentDrawingView, rightPalette); 
Claudio Valerio's avatar
Claudio Valerio committed
135 136 137 138

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

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

    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()));
159 160
    connect(UBApplication::boardController, SIGNAL(backgroundChanged()), this, SLOT(updateColors()));
    connect(UBApplication::boardController, SIGNAL(activeSceneChanged()), this, SLOT(updateColors()));
Claudio Valerio's avatar
Claudio Valerio committed
161 162 163 164
    connect(&mHoldTimerPen, SIGNAL(timeout()), this, SLOT(penActionReleased()));
    connect(&mHoldTimerMarker, SIGNAL(timeout()), this, SLOT(markerActionReleased()));
    connect(&mHoldTimerEraser, SIGNAL(timeout()), this, SLOT(eraserActionReleased()));

165
#ifdef Q_WS_X11
166
    connect(mDesktopPalette, SIGNAL(moving()), this, SLOT(refreshMask()));
167
    connect(UBApplication::boardController->paletteManager()->rightPalette(), SIGNAL(resized()), this, SLOT(refreshMask()));
168
    connect(UBApplication::boardController->paletteManager()->addItemPalette(), SIGNAL(closed()), this, SLOT(refreshMask()));
169
#endif
Claudio Valerio's avatar
Claudio Valerio committed
170
    onDesktopPaletteMaximized();
shibakaneki's avatar
shibakaneki committed
171 172

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

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

184 185 186 187 188 189 190
void UBDesktopAnnotationController::updateColors(){
    if(UBApplication::boardController->activeScene()->isDarkBackground()){
        mTransparentDrawingScene->setBackground(true, false);
    }else{
        mTransparentDrawingScene->setBackground(false, false);
    }
}
Claudio Valerio's avatar
Claudio Valerio committed
191 192 193 194 195 196

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

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
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
216 217 218 219 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
/**
 * \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
268
            yPen = iAction * (mDesktopPalette->buttonSize().height() + 2 * mDesktopPalette->border() +6); // This is the mysterious value (6)
Claudio Valerio's avatar
Claudio Valerio committed
269 270 271 272 273
            break;
        }
    }

    // First determine if the palette must be shown on the left or on the right
274
    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
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
    {
        // 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
293
        mTransparentDrawingScene->clearContent(UBGraphicsScene::clearAnnotations);
Claudio Valerio's avatar
Claudio Valerio committed
294 295 296 297 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
    }
}


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;
325
        mDesktopPalette->maximizeMe();
Claudio Valerio's avatar
Claudio Valerio committed
326 327 328 329 330 331 332 333
    }

    updateBackground();

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

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

334
#ifndef Q_WS_X11
Claudio Valerio's avatar
Claudio Valerio committed
335
    mTransparentDrawingView->showFullScreen();
336 337 338 339
#else
    // this is necessary to avoid unity to hide the panels
    mTransparentDrawingView->show();
#endif
Claudio Valerio's avatar
Claudio Valerio committed
340 341 342
    UBPlatformUtils::setDesktopMode(true);

    mDesktopPalette->appear();
343 344 345 346

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


void UBDesktopAnnotationController::close()
{
    // NOOP
}


void UBDesktopAnnotationController::stylusToolChanged(int tool)
{
358
    Q_UNUSED(tool);
359 360 361 362 363 364
//     UBStylusTool::Enum eTool = (UBStylusTool::Enum)tool;
//     if(eTool != UBStylusTool::Selector && eTool != UBStylusTool::Text)
//     {
//         if(mKeyboardPalette->m_isVisible)
//             UBApplication::mainWindow->actionVirtualKeyboard->activate(QAction::Trigger);
//     }
365

Claudio Valerio's avatar
Claudio Valerio committed
366 367 368 369 370 371 372 373 374 375 376 377
    updateBackground();
}


void UBDesktopAnnotationController::updateBackground()
{
    QBrush newBrush;

    if (mIsFullyTransparent
            || UBDrawingController::drawingController()->stylusTool() == UBStylusTool::Selector)
    {
        newBrush = QBrush(Qt::transparent);
378 379 380
#ifdef Q_WS_X11
        updateMask(true);
#endif
Claudio Valerio's avatar
Claudio Valerio committed
381 382 383 384 385 386 387
    }
    else
    {
#if defined(Q_WS_MAC)
        newBrush = QBrush(QColor(127, 127, 127, 15));
#else
        newBrush = QBrush(QColor(127, 127, 127, 1));
388 389 390
#endif
#ifdef Q_WS_X11
        updateMask(false);
Claudio Valerio's avatar
Claudio Valerio committed
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
#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()
{
Claudio Valerio's avatar
Claudio Valerio committed
413
    onToolClicked();
Claudio Valerio's avatar
Claudio Valerio committed
414 415 416
    hideWindow();

    UBPlatformUtils::setDesktopMode(false);
shibakaneki's avatar
shibakaneki committed
417
    UBDrawingController::drawingController()->setInDestopMode(false);
shibakaneki's avatar
shibakaneki committed
418

Claudio Valerio's avatar
Claudio Valerio committed
419 420 421 422 423 424
    emit restoreUniboard();
}


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

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

#ifndef Q_WS_WIN // Working only without this call on win32 desktop mode
Claudio Valerio's avatar
Claudio Valerio committed
434
    customCaptureWindow.show();
435
#endif
Claudio Valerio's avatar
Claudio Valerio committed
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451

    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
452
    onToolClicked();
Claudio Valerio's avatar
Claudio Valerio committed
453 454 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
    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()
{
482
    onToolClicked();
Claudio Valerio's avatar
Claudio Valerio committed
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 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
    mIsFullyTransparent = true;
    updateBackground();

    mDesktopPalette->disappearForCapture();

    QPixmap originalPixmap = getScreenPixmap();

    mDesktopPalette->appear();

    emit imageCaptured(originalPixmap, false);

    mIsFullyTransparent = false;

    updateBackground();
}


QPixmap UBDesktopAnnotationController::getScreenPixmap()
{
    QDesktopWidget *desktop = QApplication::desktop();

    // we capture the screen in which the mouse is.
    const QRect primaryScreenRect = desktop->screenGeometry(QCursor::pos());
    QCoreApplication::flush();

    return QPixmap::grabWindow(desktop->winId(), primaryScreenRect.x()
                               , primaryScreenRect.y(), primaryScreenRect.width(), primaryScreenRect.height());
}


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);
583 584

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

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

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

    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;
610
        eraserActionReleased();
Claudio Valerio's avatar
Claudio Valerio committed
611 612 613
    }
    else
    {
614
        mHoldTimerEraser.start(PROPERTY_PALETTE_TIMER);
Claudio Valerio's avatar
Claudio Valerio committed
615 616 617 618
    }
}

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

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

641

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

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

    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;
665
        markerActionReleased();
Claudio Valerio's avatar
Claudio Valerio committed
666 667 668
    }
    else
    {
669
        mHoldTimerMarker.start(PROPERTY_PALETTE_TIMER);
Claudio Valerio's avatar
Claudio Valerio committed
670 671 672
    }
}

673

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

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

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

}

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


void UBDesktopAnnotationController::pointerActionPressed()
{

}

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


Claudio Valerio's avatar
Claudio Valerio committed
721
/**
722
 * \brief Toggle the given palette visibility
Claudio Valerio's avatar
Claudio Valerio committed
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
 * @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);
        }
    }
}

745 746 747 748 749 750 751

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

Claudio Valerio's avatar
Claudio Valerio committed
752 753 754 755 756 757 758 759 760 761 762 763 764
/**
 * \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()));
    }

765 766 767 768 769 770 771 772
    // 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
773 774 775 776 777 778 779 780
    // 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()));
    }

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

    // 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
796 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
}

/**
 * \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()));
    }
}

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

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

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

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

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

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

        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());
        }
872
        if(UBApplication::boardController->paletteManager()->mKeyboardPalette->isVisible())
873
        {
874 875
            p.drawRect(UBApplication::boardController->paletteManager()->mKeyboardPalette->geometry().x(), UBApplication::boardController->paletteManager()->mKeyboardPalette->geometry().y(),
                       UBApplication::boardController->paletteManager()->mKeyboardPalette->width(), UBApplication::boardController->paletteManager()->mKeyboardPalette->height());
876
        }
877

878 879 880 881 882 883 884 885 886 887 888 889 890
        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);
        }

891
        if(UBApplication::boardController->paletteManager()->rightPalette()->isVisible())
892
        {
Ivan Ilin's avatar
Ivan Ilin committed
893 894 895 896 897 898 899 900 901
            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);
902 903
        }

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

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

#endif

914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
        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);

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

                annotationPainter.drawRect(rect);
            }
        }

        annotationPainter.end();

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

void UBDesktopAnnotationController::refreshMask()
{
958 959 960 961 962 963 964 965 966 967 968
    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);
        }
969
    }
970
}
shibakaneki's avatar
shibakaneki committed
971

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