UBMagnifer.cpp 15.5 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)
Claudio Valerio's avatar
Claudio Valerio committed
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 12 13 14 15
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License,
 * 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 20 21
 * GNU General Public License for more details.
 *
 * 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
 */

Claudio Valerio's avatar
Claudio Valerio committed
25 26 27 28 29 30 31 32 33 34 35 36 37


#include <QtGui>
#include "UBMagnifer.h"

#include "core/UBApplication.h"
#include "board/UBBoardController.h"
#include "domain/UBGraphicsScene.h"
#include "board/UBBoardView.h"

#include "core/memcheck.h"


38
UBMagnifier::UBMagnifier(QWidget *parent, bool isInteractive)
Claudio Valerio's avatar
Claudio Valerio committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    : QWidget(parent, parent ? Qt::Widget : Qt::Tool | (Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint))
    , mShouldMoveWidget(false)
    , mShouldResizeWidget(false)
    , borderPen(Qt::darkGray)
    , gView(0)
    , mView(0)
{
    isCusrsorAlreadyStored = false;
    setMouseTracking(true);

    //--------------------------------------------------//

    QPixmap pix(":/images/cursors/resize.png");
    QTransform tr;
    tr.rotate(45);
    mResizeCursor  = QCursor(pix.transformed(tr, Qt::SmoothTransformation), pix.width() / 2,  pix.height() / 2);

    //--------------------------------------------------//

    params.sizePercentFromScene = 20;
    m_isInteractive = isInteractive;
    sClosePixmap = new QPixmap(":/images/close.svg");
    sIncreasePixmap = new QPixmap(":/images/increase.svg");
    sDecreasePixmap = new QPixmap(":/images/decrease.svg");
    mResizeItem = new QPixmap(":/images/resize.svg");
64 65 66 67 68 69 70 71 72
    sChangeModePixmap = new QPixmap();

    qDebug() << "sClosePixmap" << sClosePixmap->size() << endl
             << "sIncreasePixmap" << sIncreasePixmap->size() << endl
             << "sDecreasePixmap" << sDecreasePixmap->size() << endl
             << "mResizeItem" << mResizeItem->size() << endl;


    setDrawingMode(UBSettings::settings()->magnifierDrawingMode->get().toInt());
Claudio Valerio's avatar
Claudio Valerio committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

    if (parent)
    {
        setAttribute(Qt::WA_NoMousePropagation);
    }
    else
    {
        // standalone window
        // !!!! Should be included into Windows after QT recompilation
#ifndef Q_WS_WIN
//        setAttribute(Qt::WA_TranslucentBackground);
        setAttribute(Qt::WA_MacAlwaysShowToolWindow);
#endif
#ifdef Q_WS_MAC
        setAttribute(Qt::WA_MacAlwaysShowToolWindow);
        setAttribute(Qt::WA_MacNonActivatingToolWindow);
        setAttribute(Qt::WA_MacNoShadow);
#endif
    }

    connect(&mRefreshTimer, SIGNAL(timeout()), this, SLOT(slot_refresh()));
}

UBMagnifier::~UBMagnifier()
{
    if(sClosePixmap)
    {
        delete sClosePixmap;
        sClosePixmap = NULL;
    }

    if(sIncreasePixmap)
    {
        delete sIncreasePixmap;
        sIncreasePixmap = NULL;
    }

    if(sDecreasePixmap)
    {
        delete sDecreasePixmap;
        sDecreasePixmap = NULL;
    }
115 116 117 118 119 120

    if (sChangeModePixmap)
    {
        delete sChangeModePixmap;
        sChangeModePixmap = NULL;
    }
Claudio Valerio's avatar
Claudio Valerio committed
121 122
}

123
void UBMagnifier::setSize(qreal percentFromScene)
Claudio Valerio's avatar
Claudio Valerio committed
124 125 126 127 128 129 130 131 132
{
    if(gView == NULL || mView == NULL) return;

    // calculate object size
    params.sizePercentFromScene = percentFromScene;
    QSize sceneSize = mView->size();
    qreal size = params.sizePercentFromScene * sceneSize.width() / 100;

    QRect currGeom = geometry();
133 134 135 136 137 138 139 140 141 142 143
    if (circular == mDrawingMode)
    {
        if(currGeom.width() == currGeom.height())
        {
            QPoint newPos = mView->mapFromGlobal(updPointMove);
            setGeometry(newPos.x() - size / 2, newPos.y() - size / 2, size, size);
        }
        else
            setGeometry(0, 0, size, size);
    }
    else if (rectangular == mDrawingMode)
Claudio Valerio's avatar
Claudio Valerio committed
144 145
    {
        QPoint newPos = mView->mapFromGlobal(updPointMove);
146
        setGeometry(newPos.x() - size / 2, newPos.y() - size / 2 / 3, size, size/3);
Claudio Valerio's avatar
Claudio Valerio committed
147 148
    }

149 150 151 152 153 154 155 156 157 158 159 160 161
    calculateButtonsPositions();
    createMask();

}

void UBMagnifier::createMask()
{
    if(gView == NULL || mView == NULL) return;

    // calculate object size
    QSize sceneSize = mView->size();
    qreal isize = params.sizePercentFromScene * sceneSize.width() / 100;

Claudio Valerio's avatar
Claudio Valerio committed
162 163 164 165
    QImage mask_img(width(), height(), QImage::Format_Mono);
    mask_img.fill(0xff);
    QPainter mask_ptr(&mask_img);
    mask_ptr.setBrush( QBrush( QColor(0, 0, 0) ) );
166 167 168 169 170 171

    if (circular == mDrawingMode)
        mask_ptr.drawEllipse(QPointF(isize/2, isize/2), isize / 2 - sClosePixmap->width(), isize / 2 - sClosePixmap->width());
    else if (rectangular == mDrawingMode)
        mask_ptr.drawRoundedRect(QRect(sClosePixmap->width(), sClosePixmap->width(), size().width() - 2*sClosePixmap->width(), size().height() - 2*sClosePixmap->width()), sClosePixmap->width()/2, sClosePixmap->width()/2);

Claudio Valerio's avatar
Claudio Valerio committed
172 173 174 175 176 177 178
    bmpMask = QBitmap::fromImage(mask_img);

    pMap = QPixmap(width(), height());
    pMap.fill(Qt::transparent);
    pMap.setMask(bmpMask);
}

179
void UBMagnifier::setZoom(qreal zoom)
Claudio Valerio's avatar
Claudio Valerio committed
180 181 182 183
{
    params.zoom = zoom;
}

184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

void UBMagnifier::calculateButtonsPositions()
{
    qDebug() << "current widget size is " << size();

    m_iButtonInterval = 5;
    mResizeItemButtonRect = QRect(size().width() - 1.5*mResizeItem->width() - m_iButtonInterval, size().height() - 1.5*mResizeItem->height() - m_iButtonInterval, mResizeItem->width(), mResizeItem->height());
    sClosePixmapButtonRect = QRect(mResizeItemButtonRect.x() - sChangeModePixmap->width() - 3*m_iButtonInterval, size().height() - sChangeModePixmap->height(), sChangeModePixmap->width(), sChangeModePixmap->height());
    sChangeModePixmapButtonRect = QRect(sClosePixmapButtonRect.x() - sChangeModePixmap->width() - m_iButtonInterval, size().height() - sChangeModePixmap->height(), sChangeModePixmap->width(), sChangeModePixmap->height());
    sDecreasePixmapButtonRect = QRect(sChangeModePixmapButtonRect.x() - sChangeModePixmap->width() - m_iButtonInterval, size().height() - sDecreasePixmap->height(), sDecreasePixmap->width(), sDecreasePixmap->height());
    sIncreasePixmapButtonRect = QRect(sDecreasePixmapButtonRect.x() - sChangeModePixmap->width() - m_iButtonInterval, size().height() - sIncreasePixmap->height(), sIncreasePixmap->width(), sIncreasePixmap->height());

    qDebug() << "mResizeItemButtonRect" << mResizeItemButtonRect << endl
             << "sClosePixmapButtonRect" << sClosePixmapButtonRect << endl
             << "sChangeModePixmapButtonRect" << sChangeModePixmapButtonRect << endl
             << "sDecreasePixmapButtonRect" << sDecreasePixmapButtonRect << endl
             << "sIncreasePixmapButtonRect" << sIncreasePixmapButtonRect << endl;
}

Claudio Valerio's avatar
Claudio Valerio committed
203 204 205 206 207 208 209 210 211 212 213 214
void UBMagnifier::paintEvent(QPaintEvent * event)
{
    Q_UNUSED(event);
    QPainter painter(this);

    painter.setRenderHint(QPainter::Antialiasing);
    painter.setPen(Qt::NoPen);

    if (m_isInteractive)
    {
        painter.setBrush(QColor(127, 127, 127, 127));
        painter.drawRoundedRect(QRectF(size().width() / 2, size().height() / 2, ( size().width() - sClosePixmap->width() ) / 2, ( size().height() - sClosePixmap->width() ) / 2), 15, 15);
215
    }
Claudio Valerio's avatar
Claudio Valerio committed
216

217 218 219 220
    painter.setBrush(QColor(190, 190, 190, 255));
    if (circular == mDrawingMode)
    {
        painter.drawEllipse(QPoint(size().width() / 2, size().height() / 2), ( size().width() - sClosePixmap->width() ) / 2, ( size().height() -  sClosePixmap->height() ) / 2);
Claudio Valerio's avatar
Claudio Valerio committed
221
    }
222
    else if (rectangular == mDrawingMode)
Claudio Valerio's avatar
Claudio Valerio committed
223
    {
224 225
        QRect r = QRect(sClosePixmap->width()/2, sClosePixmap->width()/2, size().width()- sClosePixmap->width(), size().height() - sClosePixmap->width());
        painter.drawRoundedRect(r, sClosePixmap->width()/2, sClosePixmap->width()/2);
Claudio Valerio's avatar
Claudio Valerio committed
226 227 228
    }

    painter.drawPixmap(0, 0, pMap);
229 230 231 232 233 234 235 236 237 238

    if (m_isInteractive)
    {
        painter.setBrush(QColor(190, 190, 190, 255));
        painter.drawPixmap(sClosePixmapButtonRect.topLeft(), *sClosePixmap);
        painter.drawPixmap(sIncreasePixmapButtonRect.topLeft(), *sIncreasePixmap);
        painter.drawPixmap(sDecreasePixmapButtonRect.topLeft(), *sDecreasePixmap);
        painter.drawPixmap(sChangeModePixmapButtonRect.topLeft(), *sChangeModePixmap);
        painter.drawPixmap(mResizeItemButtonRect.topLeft(), *mResizeItem);
    }
Claudio Valerio's avatar
Claudio Valerio committed
239 240 241 242 243 244 245 246 247
}

void UBMagnifier::mousePressEvent ( QMouseEvent * event )
{
    if(m_isInteractive)
    {

        QWidget::mousePressEvent(event);

248 249 250 251
        if (event->pos().x() >= size().width() - mResizeItem->width() - 14 &&
            event->pos().x() < size().width() - 14 &&
            event->pos().y() >= size().height() - mResizeItem->height() - 14 &&
            event->pos().y() < size().height() - - 14)
Claudio Valerio's avatar
Claudio Valerio committed
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
        {
            mShouldResizeWidget = true;
        }
        else
        {
            mShouldMoveWidget = !event->isAccepted() && (event->buttons() & Qt::LeftButton);
        }

        mMousePressPos = event->pos();
        mMousePressDelta = (qreal)updPointGrab.x() + (qreal)size().width() / 2 - (qreal)event->globalPos().x();

        event->accept();

        update();
    }
    else
        event->ignore();
}

void UBMagnifier::mouseMoveEvent ( QMouseEvent * event )
{
    if(m_isInteractive)
    {
        if(mShouldMoveWidget && (event->buttons() & Qt::LeftButton))
        {
            move(pos() - mMousePressPos + event->pos());
            event->accept();

            QWidget::mouseMoveEvent(event);
            emit magnifierMoved_Signal(QPoint(this->pos().x() + size().width() / 2, this->pos().y() + size().height() / 2 ));
            return;
        }
284

Claudio Valerio's avatar
Claudio Valerio committed
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
        if(mShouldResizeWidget && (event->buttons() & Qt::LeftButton))
        {

            QPoint currGlobalPos = event->globalPos();
            qreal cvW = mView->width();

            qreal newXSize = ( currGlobalPos.x() + mMousePressDelta - updPointGrab.x() ) * 2;
            qreal newPercentSize = newXSize * 100 / cvW;

            emit magnifierResized_Signal(newPercentSize);

            event->ignore();
            return;
        }

300
        if (mResizeItemButtonRect.contains(event->pos())&&
Claudio Valerio's avatar
Claudio Valerio committed
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
            isCusrsorAlreadyStored == false
           )
        {
            mOldCursor = cursor();
            isCusrsorAlreadyStored = true;
            setCursor(mResizeCursor);
        }

    }
    else
        event->ignore();
}


void UBMagnifier::mouseReleaseEvent(QMouseEvent * event)
{
    if(m_isInteractive)
    {
        mShouldMoveWidget = false;
        mShouldResizeWidget = false;

322
        if (sClosePixmapButtonRect.contains(event->pos()))
Claudio Valerio's avatar
Claudio Valerio committed
323 324 325 326 327
        {
            event->accept();
            emit magnifierClose_Signal();
        }
        else
328
        if (sIncreasePixmapButtonRect.contains(event->pos()))
Claudio Valerio's avatar
Claudio Valerio committed
329 330 331 332 333
        {
            event->accept();
            emit magnifierZoomIn_Signal();
        }
        else
334
        if (sDecreasePixmapButtonRect.contains(event->pos()))
Claudio Valerio's avatar
Claudio Valerio committed
335 336 337 338
        {
            event->accept();
            emit magnifierZoomOut_Signal();
        }
339 340 341 342 343 344
        else
        if (sChangeModePixmapButtonRect.contains(event->pos()))
        {
            event->accept();
            emit magnifierDrawingModeChange_Signal(static_cast<int>(mDrawingMode+1)%modesCount);
        }
Claudio Valerio's avatar
Claudio Valerio committed
345 346 347 348 349 350 351 352 353 354
        else
            QWidget::mouseReleaseEvent(event); // don't propgate to parent, the widget is deleted in UBApplication::boardController->removeTool
    }
    else
        event->ignore();

}

void UBMagnifier::slot_refresh()
{
355
    if(!(updPointGrab.isNull()))
Claudio Valerio's avatar
Claudio Valerio committed
356 357 358 359 360 361
        grabPoint(updPointGrab);

    if(isCusrsorAlreadyStored)
    {
        QPoint globalCursorPos = QCursor::pos();
        QPoint cursorPos = mapFromGlobal(globalCursorPos);
362 363 364 365
        if (cursorPos.x() < size().width() - mResizeItem->width() - 14 ||
            cursorPos.x() > size().width() - 14 ||
            cursorPos.y() < size().height() - mResizeItem->height() - 14 ||
            cursorPos.y() > size().height() - - 14
Claudio Valerio's avatar
Claudio Valerio committed
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
            )
        {
            isCusrsorAlreadyStored = false;
            setCursor(mOldCursor);
        }
    }
}

void UBMagnifier::grabPoint()
{
    QMatrix transM = UBApplication::boardController->controlView()->matrix();
    QPointF itemPos = gView->mapFromGlobal(updPointGrab);

    qreal zWidth = width() / (params.zoom * transM.m11());
    qreal zWidthHalf = zWidth / 2;
    qreal zHeight = height() / (params.zoom * transM.m22());
    qreal zHeightHalf = zHeight / 2;
383

Claudio Valerio's avatar
Claudio Valerio committed
384 385

    QPointF pfScLtF(UBApplication::boardController->controlView()->mapToScene(QPoint(itemPos.x(), itemPos.y())));
386

Claudio Valerio's avatar
Claudio Valerio committed
387 388 389 390
    float x = pfScLtF.x() - zWidthHalf;
    float y = pfScLtF.y() - zHeightHalf;

    QPointF leftTop(x,y);
391
    QPointF rightBottom(x + zWidth, y + zHeight);
Claudio Valerio's avatar
Claudio Valerio committed
392 393 394 395 396 397 398
    QRectF srcRect(leftTop, rightBottom);

    QPixmap newPixMap(QSize(width(), height()));
    QPainter painter(&newPixMap);

    UBApplication::boardController->activeScene()->render(&painter, QRectF(0,0,width(),height()), srcRect);
    painter.end();
399

Claudio Valerio's avatar
Claudio Valerio committed
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
    pMap.fill(Qt::transparent);
    pMap = newPixMap.scaled(QSize(width(), height()));
    pMap.setMask(bmpMask);

    update();
}

void UBMagnifier::grabPoint(const QPoint &pGrab)
{
    QMatrix transM = UBApplication::boardController->controlView()->matrix();
    updPointGrab = pGrab;
    QPointF itemPos = gView->mapFromGlobal(pGrab);

    qreal zWidth = width() / (params.zoom * transM.m11());
    qreal zWidthHalf = zWidth / 2;
    qreal zHeight = height() / (params.zoom * transM.m22());
    qreal zHeightHalf = zHeight / 2;
417

Claudio Valerio's avatar
Claudio Valerio committed
418 419

    QPointF pfScLtF(UBApplication::boardController->controlView()->mapToScene(QPoint(itemPos.x(), itemPos.y())));
420

Claudio Valerio's avatar
Claudio Valerio committed
421 422 423 424
    float x = pfScLtF.x() - zWidthHalf;
    float y = pfScLtF.y() - zHeightHalf;

    QPointF leftTop(x,y);
425
    QPointF rightBottom(x + zWidth, y + zHeight);
Claudio Valerio's avatar
Claudio Valerio committed
426 427 428 429 430
    QRectF srcRect(leftTop, rightBottom);

    QPixmap newPixMap(QSize(width(), height()));
    QPainter painter(&newPixMap);

431
    UBApplication::boardController->activeScene()->render(&painter, QRectF(0,0,width(),height()), srcRect);
Claudio Valerio's avatar
Claudio Valerio committed
432
    painter.end();
433

Claudio Valerio's avatar
Claudio Valerio committed
434 435 436 437 438 439 440 441 442 443 444 445
   // pMap.fill(Qt::transparent);
    pMap = newPixMap;
    pMap.setMask(bmpMask);

    update();
}



// from global
void UBMagnifier::grabNMove(const QPoint &pGrab, const QPoint &pMove, bool needGrab, bool needMove)
{
446 447
    QPoint pointToGrab = pGrab;
    updPointGrab = pointToGrab;
Claudio Valerio's avatar
Claudio Valerio committed
448 449 450
    updPointMove = pMove;

    if(needGrab)
451
        grabPoint(pointToGrab);
Claudio Valerio's avatar
Claudio Valerio committed
452 453 454 455 456 457 458 459 460 461

    if(needMove)
    {
        QPoint movePos = mView->mapFromGlobal(pMove);
        move(movePos.x() - width()/2, movePos.y() - height()/2);
        //    move(itemPos.x(), itemPos.y());
    }
}

void UBMagnifier::setGrabView(QWidget *view)
462
{
Claudio Valerio's avatar
Claudio Valerio committed
463 464 465 466 467
    gView = view;
    mRefreshTimer.setInterval(40);
    mRefreshTimer.start();
}

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
void UBMagnifier::setDrawingMode(int mode)
{
    mDrawingMode = static_cast<DrawingMode>(mode);

    QString sMode;

    if (circular == mDrawingMode)
    {
        sMode = "roundeRrectangle";
        resize(width(), width());
    }

    if (rectangular == mDrawingMode)
    {
        sMode = "circle";
        resize(width(), height()/3);

        if (mView)
        {
            qreal newPercentSize = size().width()/3 * 100 / mView->width();
            emit magnifierResized_Signal(newPercentSize);
        }
    }

    sChangeModePixmap->load(":/images/"+sMode+".svg");

    calculateButtonsPositions();
    if (mView && gView)
        UBApplication::boardController->controlView()->scene()->moveMagnifier();

    createMask();

    UBSettings::settings()->magnifierDrawingMode->set(mode);
}