aboutsummaryrefslogtreecommitdiff
path: root/src/views/mtextmagnifier.cpp
blob: 5997ab867ca1dc5070afbbdf64bf0c9c5a14ddbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
63
64
65
66
67
68
69
70
71
72
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
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
149
150
151
152
153
154
155
156
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
183
184
185
186
/***************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (directui@nokia.com)
**
** This file is part of libmeegotouch.
**
** If you have questions regarding the use of this file, please contact
** Nokia at directui@nokia.com.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/

#include "mtextmagnifier.h"
#include "mdebug.h"

#include <QBitmap>
#include <QGraphicsItem>
#include <QGraphicsSceneResizeEvent>
#include <QPainter>
#include <QStyleOptionGraphicsItem>


MTextMagnifier::MTextMagnifier(const QGraphicsItem &sourceItem)
    : MStylableWidget(0),
      sourceItem(sourceItem)
{
    setParentItem(&overlay);
}

MTextMagnifier::~MTextMagnifier()
{
    // Before destroying parent, detach so it doesn't try to destroy us.
    setParentItem(0);
}

void MTextMagnifier::appear()
{
    // Appear in the scene of the source item.
    overlay.appear(sourceItem.scene());
    overlay.grabGesture(Qt::PanGesture);
}

void MTextMagnifier::disappear()
{
    overlay.ungrabGesture(Qt::PanGesture);
    overlay.disappear();
}

void MTextMagnifier::setMagnifiedPosition(const QPointF &sourceItemPos)
{
    setPos(sourceItem.mapToItem(parentItem(), sourceItemPos));
}

bool MTextMagnifier::isAppeared() const
{
    return overlay.isAppeared();
}

void MTextMagnifier::drawContents(QPainter *painter,
                                  const QStyleOptionGraphicsItem *option) const
{
    if (offscreenSurface.isNull()) {
        mWarning("MTextMagnifier") << "Offscreen surface not created!";
        return;
    }

    const qreal scaleFactor = 1.0 + style()->magnification();

    // Source rectangle in local item coordinates.
    const QSizeF sourceSize(size() / scaleFactor);
    const QRectF sourceRect(QPointF(-sourceSize.width() / 2.0,
                                    -sourceSize.height() / 2.0),
                            sourceSize);

    // Source rectangle in source item coordinates.
    const QRectF sourceItemRect(mapRectToItem(&sourceItem, sourceRect));

    // Paint sourceItem onto offscreen surface.
    offscreenSurface->fill(Qt::transparent);
    QPainter offscreenPainter(offscreenSurface.data());

    // SmoothPixmapTransform is to draw text edit borders smoothly.
    offscreenPainter.setRenderHint(QPainter::SmoothPixmapTransform, true);

    // Scale and then translate in scaled coordinates.
    offscreenPainter.scale(scaleFactor, scaleFactor);
    offscreenPainter.translate(-sourceItemRect.topLeft());

    QStyleOptionGraphicsItem sourceItemOption = *option;
    sourceItemOption.exposedRect = sourceItemRect;
    offscreenPainter.setCompositionMode(QPainter::CompositionMode_Source);
    const_cast<QGraphicsItem *>(&sourceItem)->paint(&offscreenPainter, &sourceItemOption);

    offscreenPainter.resetTransform();
    if (style()->magnifierMask()) {
        offscreenPainter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
        offscreenPainter.drawPixmap(offscreenSurface->rect(), *style()->magnifierMask());
    }
    if (style()->magnifierFrame()) {
        offscreenPainter.setCompositionMode(QPainter::CompositionMode_DestinationOver);
        offscreenPainter.drawPixmap(offscreenSurface->rect(), *style()->magnifierFrame());
    }
    offscreenPainter.end();

    // Paint the result to screen
    painter->save();

    painter->setClipRect(boundingRect(), Qt::IntersectClip);
    painter->drawPixmap(boundingRect().toRect(), *offscreenSurface);

    painter->restore();
}

void MTextMagnifier::applyStyle()
{
    if (!style()->magnifierFrame()) {
        return;
    }
    // Update bounding rectangle.
    prepareGeometryChange();
    QSize magnifierSize(style()->magnifierFrame()->size());
    if (!magnifierSize.isEmpty()) {
        resize(magnifierSize);
    }
}

void MTextMagnifier::resizeEvent(QGraphicsSceneResizeEvent *event)
{
    // The size should always be integers already,
    // therefore safe to round.
    prepareOffscreenSurface(event->newSize().toSize());
}

QRectF MTextMagnifier::boundingRect() const
{
    // Put origo to center and apply offset from style.
    return QRectF(-rect().center() + style()->visualOffset(), size());
}

void MTextMagnifier::prepareOffscreenSurface(const QSize &size)
{
    if (size.isEmpty()
        || (offscreenSurface && offscreenSurface->size() == size)) {
        return;
    }
    offscreenSurface.reset(new QPixmap(size));
}


// Magnifier overlay widget

MagnifierOverlay::MagnifierOverlay()
{
    setFlag(QGraphicsItem::ItemHasNoContents, true);

    // Occupy whole screen to be able to catch panning gestures. The size does not really matter
    // as long as it covers whole screen in portrait and landscape.
    setManagedManually(true);
    const QSizeF visibleSceneSize = sceneManager()->visibleSceneSize(M::Landscape);
    const qreal size = qMax<qreal>(visibleSceneSize.width(), visibleSceneSize.height());
    setPreferredSize(size, size);
}

bool MagnifierOverlay::isAppeared() const
{
    return sceneWindowState() != MSceneWindow::Disappeared;
}

void MagnifierOverlay::panGestureEvent(QGestureEvent *event, QPanGesture *panGesture)
{
    // Accept gesture if magnifier is appeared. This is done to prevent panning of
    // application page but will of course prevent other uses of pan gestures as well.
    if (panGesture->state() == Qt::GestureStarted
        && isAppeared()) {
        event->accept(panGesture);
    } else {
        event->ignore(panGesture);
    }
}