UBDesktopAnnotationController.cpp 30.7 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1
/*
Claudio Valerio's avatar
Claudio Valerio committed
2 3 4 5
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
Claudio Valerio's avatar
Claudio Valerio committed
6
 *
Claudio Valerio's avatar
Claudio Valerio committed
7 8 9 10 11 12 13
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Claudio Valerio's avatar
Claudio Valerio committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 */

#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
34
#include "board/UBBoardPaletteManager.h"
Claudio Valerio's avatar
Claudio Valerio committed
35 36 37 38 39 40 41 42 43 44 45

#include "domain/UBGraphicsScene.h"

#include "UBCustomCaptureWindow.h"
#include "UBWindowCapture.h"
#include "UBDesktopPalette.h"
#include "UBDesktopPenPalette.h"
#include "UBDesktopMarkerPalette.h"
#include "UBDesktopEraserPalette.h"

#include "gui/UBKeyboardPalette.h"
46
#include "gui/UBResources.h"
Claudio Valerio's avatar
Claudio Valerio committed
47

48 49
#include "core/memcheck.h"

Claudio Valerio's avatar
Claudio Valerio committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
UBDesktopAnnotationController::UBDesktopAnnotationController(QObject *parent)
        : QObject(parent)
        , mTransparentDrawingView(0)
        , mTransparentDrawingScene(0)
        , mDesktopPalette(NULL)
        , mDesktopPenPalette(NULL)
        , mDesktopMarkerPalette(NULL)
        , mDesktopEraserPalette(NULL)
        , mWindowPositionInitialized(0)
        , mIsFullyTransparent(false)
        , mDesktopToolsPalettePositioned(false)
        , mPendingPenButtonPressed(false)
        , mPendingMarkerButtonPressed(false)
        , mPendingEraserButtonPressed(false)
        , mbArrowClicked(false)
65 66
        , mBoardStylusTool(UBDrawingController::drawingController()->stylusTool())
        , mDesktopStylusTool(UBDrawingController::drawingController()->stylusTool())
Claudio Valerio's avatar
Claudio Valerio committed
67 68 69 70 71
{

    mTransparentDrawingView = new UBBoardView(UBApplication::boardController, 0); // deleted in UBDesktopAnnotationController::destructor

    mTransparentDrawingView->setAttribute(Qt::WA_TranslucentBackground, true);
72
	// !!!! Should be included into Windows after QT recompilation
73
#ifdef Q_WS_MAC
74
    mTransparentDrawingView->setAttribute(Qt::WA_MacNoShadow, true);
75
#endif
Claudio Valerio's avatar
Claudio Valerio committed
76 77 78 79 80 81
    mTransparentDrawingView->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::Window);
    mTransparentDrawingView->setCacheMode(QGraphicsView::CacheNone);
    mTransparentDrawingView->resize(UBApplication::desktop()->width(), UBApplication::desktop()->height());

    mTransparentDrawingView->setMouseTracking(true);

82
    mTransparentDrawingView->setAcceptDrops(true);
Claudio Valerio's avatar
Claudio Valerio committed
83 84 85 86 87 88

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

    mTransparentDrawingScene = new UBGraphicsScene(0);
    mTransparentDrawingView->setScene(mTransparentDrawingScene);
shibakaneki's avatar
shibakaneki committed
89
    mTransparentDrawingScene->setDrawingMode(true);
Claudio Valerio's avatar
Claudio Valerio committed
90 91 92

    mDesktopPalette = new UBDesktopPalette(mTransparentDrawingView);

93 94
    if (UBPlatformUtils::hasVirtualKeyboard())
    {
95 96 97
        connect( UBApplication::boardController->paletteManager()->mKeyboardPalette, SIGNAL(keyboardActivated(bool)), 
                 mTransparentDrawingView, SLOT(virtualKeyboardActivated(bool)));

98
#ifdef Q_WS_X11
99
        connect(UBApplication::boardController->paletteManager()->mKeyboardPalette, SIGNAL(moved(QPoint)), this, SLOT(refreshMask()));
100
        connect(UBApplication::mainWindow->actionVirtualKeyboard, SIGNAL(triggered(bool)), this, SLOT(refreshMask()));
101
        connect(mDesktopPalette,SIGNAL(refreshMask()), this, SLOT(refreshMask()));
102
#endif
103 104
    }

Claudio Valerio's avatar
Claudio Valerio committed
105 106 107 108 109 110
    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()));
    connect(mDesktopPalette, SIGNAL(maximized()), this, SLOT(onDesktopPaletteMaximized()));
    connect(mDesktopPalette, SIGNAL(minimizeStart(eMinimizedLocation)), this, SLOT(onDesktopPaletteMinimize()));
111

Claudio Valerio's avatar
Claudio Valerio committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    connect(mTransparentDrawingView, SIGNAL(resized(QResizeEvent*)), this, SLOT(onTransparentWidgetResized()));


    connect(UBDrawingController::drawingController(), SIGNAL(stylusToolChanged(int))
            , this, SLOT(stylusToolChanged(int)));

    // Add the desktop associated palettes
    mDesktopPenPalette = new UBDesktopPenPalette(mTransparentDrawingView);

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

    mDesktopMarkerPalette = new UBDesktopMarkerPalette(mTransparentDrawingView);
    mDesktopEraserPalette = new UBDesktopEraserPalette(mTransparentDrawingView);

    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()));

    connect(&mHoldTimerPen, SIGNAL(timeout()), this, SLOT(penActionReleased()));
    connect(&mHoldTimerMarker, SIGNAL(timeout()), this, SLOT(markerActionReleased()));
    connect(&mHoldTimerEraser, SIGNAL(timeout()), this, SLOT(eraserActionReleased()));

149
#ifdef Q_WS_X11
150
    connect(mDesktopPalette, SIGNAL(moving()), this, SLOT(refreshMask()));
151 152
//    connect(mRightPalette, SIGNAL(resized()), this, SLOT(refreshMask()));
    connect(UBApplication::boardController->paletteManager()->rightPalette(), SIGNAL(resized()), this, SLOT(refreshMask()));
153
#endif
Claudio Valerio's avatar
Claudio Valerio committed
154 155 156
    onDesktopPaletteMaximized();
}

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
// void UBDesktopAnnotationController::showKeyboard(bool show)
// {
//     #ifdef Q_WS_X11
//         if (!mTransparentDrawingView->isVisible())
//             return;
//     #endif
// 
//     if(mKeyboardPalette)
//     {
//         if(show)
//             UBDrawingController::drawingController()->setStylusTool(UBStylusTool::Selector);
//         mKeyboardPalette->setVisible(show);
// 
//         #ifdef Q_WS_X11
//             updateMask(true);
//         #endif
//         
//     }
// 
// }

// void UBDesktopAnnotationController::showKeyboard()
// {
//     if (UBApplication::mainWindow->actionVirtualKeyboard->isChecked())
//         mKeyboardPalette->show();
// }
183

Claudio Valerio's avatar
Claudio Valerio committed
184 185 186 187 188
UBDesktopAnnotationController::~UBDesktopAnnotationController()
{
    delete mTransparentDrawingScene;
    delete mTransparentDrawingView;

189
	/*
Claudio Valerio's avatar
Claudio Valerio committed
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
    if(NULL != mDesktopPenPalette)
    {
        delete mDesktopPalette;
        mDesktopPenPalette = NULL;
    }
    if(NULL != mDesktopMarkerPalette)
    {
        delete mDesktopMarkerPalette;
        mDesktopMarkerPalette = NULL;
    }
    if(NULL != mDesktopEraserPalette)
    {
        delete mDesktopEraserPalette;
        mDesktopEraserPalette = NULL;
    }
205
    if(NULL != mRightPalette)
Claudio Valerio's avatar
Claudio Valerio committed
206
    {
207 208
        delete mRightPalette;
        mRightPalette = NULL;
Claudio Valerio's avatar
Claudio Valerio committed
209
    }
210
	*/
Claudio Valerio's avatar
Claudio Valerio committed
211 212 213 214 215 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 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 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
}


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

/**
 * \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);
            yPen = iAction * mDesktopPalette->buttonSize().height();

            // Add the borders
            yPen += (iAction) * (mDesktopPalette->border() + 4); // 4 has been set after some experiment. We must determine why this value is good

            break;
        }
    }

    // First determine if the palette must be shown on the left or on the right
    if(desktopPalettePos.x() <= (mTransparentDrawingView->width() - (palette->width() + mDesktopPalette->width() + 20))) // we take a small margin of 20 pixels
    {
        // 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)
    {
        mTransparentDrawingScene->clearAnnotations();
    }
}


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;
    }

    updateBackground();

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

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

    mTransparentDrawingView->showFullScreen();

    UBPlatformUtils::setDesktopMode(true);

    mDesktopPalette->appear();
345 346 347 348

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


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


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

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


void UBDesktopAnnotationController::updateBackground()
{
    QBrush newBrush;

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

    UBPlatformUtils::setDesktopMode(false);

    emit restoreUniboard();
}


void UBDesktopAnnotationController::customCapture()
{
    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 452 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 482 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

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

    mDesktopPalette->appear();

    mIsFullyTransparent = false;
    updateBackground();
}


void UBDesktopAnnotationController::windowCapture()
{
    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()
{
    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()
{
    qDebug() << "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);
582 583

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

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

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

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

/**
618
 * \brief Handles the eraser action released event
Claudio Valerio's avatar
Claudio Valerio committed
619
 */
620
void UBDesktopAnnotationController::eraserActionReleased()
Claudio Valerio's avatar
Claudio Valerio committed
621
{
622 623 624
    qDebug() << "eraserActionReleased()";
    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 681
    qDebug() << "markerActionReleased()";
    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 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
void UBDesktopAnnotationController::selectorActionPressed()
{

}

void UBDesktopAnnotationController::selectorActionReleased()
{
    switchCursor(UBStylusTool::Selector);
}


void UBDesktopAnnotationController::pointerActionPressed()
{

}

void UBDesktopAnnotationController::pointerActionReleased()
{
    switchCursor(UBStylusTool::Pointer);
}


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

744 745 746 747 748 749 750

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

Claudio Valerio's avatar
Claudio Valerio committed
751 752 753 754 755
/**
 * \brief Reconnect the pressed & released signals of the property palettes
 */
void UBDesktopAnnotationController::onDesktopPaletteMaximized()
{
756

Claudio Valerio's avatar
Claudio Valerio committed
757 758 759 760 761 762 763 764
    // 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 858 859 860 861 862 863 864 865 866 867 868 869 870
        // consuming but for the moment this is the only solution that I found.
        mMask = QPixmap(mTransparentDrawingView->width(), mTransparentDrawingView->height());

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

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

890
        if(UBApplication::boardController->paletteManager()->rightPalette()->isVisible())
891
        {
Ivan Ilin's avatar
Ivan Ilin committed
892 893 894 895 896 897 898 899 900
            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);
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
        }

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

            if(pCrntItem->isVisible())
            {
                QPainterPath crntPath = pCrntItem->shape();
                QRectF rect = crntPath.boundingRect();

                annotationPainter.drawRect(rect);
            }
        }

        annotationPainter.end();

        mTransparentDrawingView->setMask(mMask.createMaskFromColor(Qt::black));
    }
    else
    {
        // Remove the mask
        QPixmap noMask(mTransparentDrawingView->width(), mTransparentDrawingView->height());
        mTransparentDrawingView->setMask(noMask.mask());
    }
}

void UBDesktopAnnotationController::refreshMask()
{
    if(mIsFullyTransparent
948
            || UBDrawingController::drawingController()->stylusTool() == UBStylusTool::Selector
949
            //Needed to work correctly when another actions on stylus are checked
950 951 952 953
            || UBDrawingController::drawingController()->stylusTool() == UBStylusTool::Eraser
            || UBDrawingController::drawingController()->stylusTool() == UBStylusTool::Pointer
            || UBDrawingController::drawingController()->stylusTool() == UBStylusTool::Pen
            || UBDrawingController::drawingController()->stylusTool() == UBStylusTool::Marker)
954 955 956
    {
        updateMask(true);
    }
957
}