UBMagnifer.cpp 11.8 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * 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.
 *
 * 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/>.
 */
15 16

#include <QtGui>
17
#include "UBMagnifer.h"
18 19 20 21 22

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

23 24
#include "core/memcheck.h"

25 26 27 28 29

UBMagnifier::UBMagnifier(QWidget *parent, bool isInteractive) 
    : QWidget(parent, parent ? Qt::Widget : Qt::Tool | (Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint))
    , mShouldMoveWidget(false)
    , mShouldResizeWidget(false)
Claudio Valerio's avatar
Claudio Valerio committed
30 31 32 33
    , inTimer(false)
    , borderPen(Qt::darkGray)
    , gView(0)
    , mView(0)
34
{
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
    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");

    if (parent)
    {
        setAttribute(Qt::WA_NoMousePropagation);
    }
    else
    {
        // standalone window
        // !!!! Should be included into Windows after QT recompilation
#ifndef Q_WS_WIN
Ivan Ilin's avatar
Ivan Ilin committed
63
//        setAttribute(Qt::WA_TranslucentBackground);
64 65 66 67 68 69 70 71 72
        setAttribute(Qt::WA_MacAlwaysShowToolWindow);
#endif
#ifdef Q_WS_MAC
        setAttribute(Qt::WA_MacAlwaysShowToolWindow);
        setAttribute(Qt::WA_MacNonActivatingToolWindow);
        setAttribute(Qt::WA_MacNoShadow);
#endif
    }

73 74
}

75
UBMagnifier::~UBMagnifier()
76
{
Claudio Valerio's avatar
Claudio Valerio committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    if (timerUpdate != 0)
    {
        killTimer(timerUpdate);
        timerUpdate = 0;
    }

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

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

    if(sDecreasePixmap)
    {
        delete sDecreasePixmap;
        sDecreasePixmap = NULL;
    }
100 101
}

102
void UBMagnifier::setSize(qreal percentFromScene) 
103
{
104
    if(gView == NULL || mView == NULL) return;
105 106

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

111 112 113 114 115 116 117 118
    QRect currGeom = geometry();
    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);
119 120

    // prepare transparent bit mask
Claudio Valerio's avatar
Claudio Valerio committed
121 122 123
    QImage mask_img(width(), height(), QImage::Format_Mono);
    mask_img.fill(0xff);
    QPainter mask_ptr(&mask_img);
124
    mask_ptr.setBrush( QBrush( QColor(0, 0, 0) ) );
125
    mask_ptr.drawEllipse(QPointF(size/2, size/2), size / 2 - sClosePixmap->width(), size / 2 - sClosePixmap->width());
126 127 128 129 130 131 132 133
    bmpMask = QBitmap::fromImage(mask_img);

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

134 135 136 137 138 139 140
void UBMagnifier::setZoom(qreal zoom) 
{
    params.zoom = zoom;
}

void UBMagnifier::paintEvent(QPaintEvent * event)
{
Claudio Valerio's avatar
Claudio Valerio committed
141
    Q_UNUSED(event);
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    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);

        painter.setBrush(QColor(190, 190, 190, 255));
        painter.drawEllipse(QPoint( size().width() / 2, size().height() / 2), ( size().width() - sClosePixmap->width() ) / 2, ( size().height() -  sClosePixmap->height() ) / 2);

        painter.drawPixmap(size().width() - sClosePixmap->width(), size().height() / 2 + sClosePixmap->height() * 1, *sClosePixmap);
        painter.drawPixmap(size().width() - sIncreasePixmap->width(), size().height() / 2 + sIncreasePixmap->height() * 2.5, *sIncreasePixmap);
        painter.drawPixmap(size().width() - sDecreasePixmap->width(), size().height() / 2 + sDecreasePixmap->height() * 3.6, *sDecreasePixmap);

        painter.drawPixmap(size().width() - mResizeItem->width() - 20, size().height() - mResizeItem->height() - 20, *mResizeItem);
    }
    else
    {
        painter.setBrush(QColor(127, 127, 127, 127));
        painter.drawEllipse(QPoint( size().width() / 2, size().height() / 2), ( size().width() - sClosePixmap->width() ) / 2, ( size().height() -  sClosePixmap->height() ) / 2);
    }

    painter.drawPixmap(0, 0, pMap);
}

void UBMagnifier::mousePressEvent ( QMouseEvent * event )
171
{
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
    if(m_isInteractive)
    {

        QWidget::mousePressEvent(event);

        if (event->pos().x() >= size().width() - mResizeItem->width() - 20 && 
            event->pos().x() < size().width() - 20 && 
            event->pos().y() >= size().height() - mResizeItem->height() - 20 && 
            event->pos().y() < size().height() - - 20)
        {
            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();
193

194 195 196 197
        update();
    }
    else
        event->ignore();
198 199
}

200
void UBMagnifier::mouseMoveEvent ( QMouseEvent * event )
201
{
202 203 204 205 206 207 208 209
    if(m_isInteractive)
    {
        if(mShouldMoveWidget && (event->buttons() & Qt::LeftButton))
        {
            move(pos() - mMousePressPos + event->pos());
            event->accept();

            QWidget::mouseMoveEvent(event);
Claudio Valerio's avatar
Claudio Valerio committed
210 211
            emit magnifierMoved_Signal(QPoint(this->pos().x() + size().width() / 2, this->pos().y() + size().height() / 2 ));
            return;
212 213 214 215 216 217 218 219 220 221 222
        }
    
        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;

Claudio Valerio's avatar
Claudio Valerio committed
223
            emit magnifierResized_Signal(newPercentSize);
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240

            event->ignore();
            return;
        }

        if (event->pos().x() >= size().width() - mResizeItem->width() - 20 && 
            event->pos().x() < size().width() - 20 && 
            event->pos().y() >= size().height() - mResizeItem->height() - 20 && 
            event->pos().y() < size().height() - - 20 &&
            isCusrsorAlreadyStored == false
           )
        {
            mOldCursor = cursor();
            isCusrsorAlreadyStored = true;
            setCursor(mResizeCursor);
        }

Claudio Valerio's avatar
Claudio Valerio committed
241 242
    }
    else
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
        event->ignore();
}


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

        if (event->pos().x() >= size().width() - sClosePixmap->width() && 
            event->pos().x() < size().width()&& 
            event->pos().y() >= size().height() / 2 + sClosePixmap->height() * 1 && 
            event->pos().y() < size().height() / 2 + sClosePixmap->height() * 2)
        {
            event->accept();
Claudio Valerio's avatar
Claudio Valerio committed
260
            emit magnifierClose_Signal();
261 262 263 264 265 266 267 268
        }
        else
        if (event->pos().x() >= size().width() - sIncreasePixmap->width() && 
            event->pos().x() < size().width()&& 
            event->pos().y() >= size().height() / 2 + sIncreasePixmap->height() * 2.5 && 
            event->pos().y() < size().height() / 2 + sIncreasePixmap->height() * 3.5)
        {
            event->accept();
Claudio Valerio's avatar
Claudio Valerio committed
269
            emit magnifierZoomIn_Signal();
270 271 272 273 274 275 276 277
        }
        else
        if (event->pos().x() >= size().width() - sDecreasePixmap->width() && 
            event->pos().x() < size().width()&& 
            event->pos().y() >= size().height() / 2 + sDecreasePixmap->height() * 3.6 && 
            event->pos().y() < size().height() / 2 + sDecreasePixmap->height() * 4.6)
        {
            event->accept();
Claudio Valerio's avatar
Claudio Valerio committed
278
            emit magnifierZoomOut_Signal();
279 280 281 282 283 284 285
        }
        else
            QWidget::mouseReleaseEvent(event); // don't propgate to parent, the widget is deleted in UBApplication::boardController->removeTool
    }
    else
        event->ignore();

286 287
}

Claudio Valerio's avatar
Claudio Valerio committed
288 289 290 291 292 293 294 295 296 297 298 299 300
void UBMagnifier::timerEvent(QTimerEvent *e)
{
    if(inTimer) return;
    if (e->timerId() == timerUpdate)
    {
        inTimer = true;
        if(!(updPointGrab.isNull())) 
            grabPoint(updPointGrab);

        if(isCusrsorAlreadyStored)
        {
            QPoint globalCursorPos = QCursor::pos();
            QPoint cursorPos = mapFromGlobal(globalCursorPos);
301 302 303 304 305 306 307 308 309
            if (cursorPos.x() < size().width() - mResizeItem->width() - 20 || 
                cursorPos.x() > size().width() - 20 ||
                cursorPos.y() < size().height() - mResizeItem->height() - 20 ||
                cursorPos.y() > size().height() - - 20
                )
            {
                isCusrsorAlreadyStored = false;
                setCursor(mOldCursor);
            }
Claudio Valerio's avatar
Claudio Valerio committed
310 311 312 313 314 315

        }

        inTimer = false;
    }
}
316

317
void UBMagnifier::grabPoint()
318
{
319 320 321 322 323 324
    QPointF itemPos = gView->mapFromGlobal(updPointGrab);

    qreal zWidth = size().width() / params.zoom;
    qreal zWidthHalf = zWidth / 2;
    qreal zHeight = size().height() / params.zoom;
    qreal zHeightHalf = zHeight / 2;
325 326 327 328 329 330

    int x = itemPos.x() - zWidthHalf;
    int y = itemPos.y() - zHeightHalf;

    QPixmap newPixMap(QSize(zWidth,zHeight));
    ((QWidget*)gView)->render(&newPixMap, QPoint(0, 0), QRegion(x, y, zWidth, zHeight));
Claudio Valerio's avatar
Claudio Valerio committed
331 332
    UBApplication::boardController->activeScene()->update();

333 334 335 336 337 338 339
    pMap.fill(Qt::transparent);
    pMap = newPixMap.scaled(QSize(width(), height()));
    pMap.setMask(bmpMask);

    update();
}

340
void UBMagnifier::grabPoint(const QPoint &pGrab)
341
{
342 343 344 345 346 347 348
    updPointGrab = pGrab;
    QPointF itemPos = gView->mapFromGlobal(pGrab);

    qreal zWidth = size().width() / params.zoom;
    qreal zWidthHalf = zWidth / 2;
    qreal zHeight = size().height() / params.zoom;
    qreal zHeightHalf = zHeight / 2;
349 350 351 352 353 354

    int x = itemPos.x() - zWidthHalf;
    int y = itemPos.y() - zHeightHalf;

    QPixmap newPixMap(QSize(zWidth,zHeight));
    ((QWidget*)gView)->render(&newPixMap, QPoint(0, 0), QRegion(x, y, zWidth, zHeight));
Claudio Valerio's avatar
Claudio Valerio committed
355 356
    UBApplication::boardController->activeScene()->update();

357 358 359 360 361 362 363 364
    pMap.fill(Qt::transparent);
    pMap = newPixMap.scaled(QSize(width(), height()));
    pMap.setMask(bmpMask);

    update();
}

// from global
365
void UBMagnifier::grabNMove(const QPoint &pGrab, const QPoint &pMove, bool needGrab, bool needMove)
366
{
Claudio Valerio's avatar
Claudio Valerio committed
367 368
    updPointGrab = pGrab;
    updPointMove = pMove;
369 370

    if(needGrab)
371
        grabPoint(pGrab);
372

373 374 375 376 377 378
    if(needMove)
    {
        QPoint movePos = mView->mapFromGlobal(pMove);
        move(movePos.x() - width()/2, movePos.y() - height()/2);
        //    move(itemPos.x(), itemPos.y());
    }
379 380
}

Claudio Valerio's avatar
Claudio Valerio committed
381 382 383 384 385 386 387
void UBMagnifier::setGrabView(QWidget *view)
{
    if (timerUpdate != 0)
        killTimer(timerUpdate);
    gView = view;
    timerUpdate = startTimer(200);
}
388