/*
magnifyingglas.cpp
*/
#include "magnifyingglas.h"
#include <QGraphicsItem>
{
pressed = false;
snapped = false;
zoomed = false;
image.load("/home/kuehnel/Development/Unbenannt_2.png");
largePixmap.load("/home/kuehnel/Development/Unbenannt_2.png");
smallPixmap = largePixmap.scaled(largePixmap.size()/2);
this->setScene(scene);
scene->addPixmap(largePixmap);
}
void MagnifyingGlas
::pan(const QPoint &delta
) {
offset += delta;
update();
}
void MagnifyingGlas::activateZoom()
{
zoomed = true;
tapTimer.stop();
update();
}
{
if (!zoomed) activateZoom();
update();
}
void MagnifyingGlas
::mousePressEvent(QMouseEvent *event
) {
if (event->buttons()!= Qt::LeftButton) return;
pressed = snapped = true;
pressPos = dragPos = event->pos();
tapTimer.start(HOLD_TIME, viewport());
}
void MagnifyingGlas
::mouseMoveEvent(QMouseEvent *event
) {
if (!event->buttons()) return;
if (!zoomed)
{
if (!pressed || !snapped)
{
QPoint delta
= event
->pos
() - pressPos;
pressPos = event->pos();
pan(delta);
return;
}
else
{
const int threshold = 10;
QPoint delta
= event
->pos
() - pressPos;
if (snapped)
{
snapped &= delta.x() < threshold;
snapped &= delta.y() < threshold;
snapped &= delta.x() > -threshold;
snapped &= delta.y() >-threshold;
}
if (!snapped) tapTimer.stop();
}
}
else
{
dragPos = event->pos();
update();
}
}
{
zoomed = false;
update();
}
{
zoomed = false;
update();
}
{
p.drawPixmap(offset,smallPixmap);
p.end();
if (zoomed)
{
int dim = qMin(width(), height());
int magnifierSize = dim * 5/6;
int radius = magnifierSize / 2;
int ring = radius - 15;
if(maskPixmap.size() != box)
{
maskPixmap.fill(Qt::transparent);
g.setCenter(radius, radius);
g.setFocalPoint(radius, radius);
g.setRadius(radius);
g.
setColorAt(1.0,
QColor(64,
64,
64,
0));
g.
setColorAt(0.5,
QColor(0,
0,
0,
255));
mask.
setRenderHint(QPainter::Antialiasing);
mask.
setCompositionMode(QPainter::CompositionMode_Source);
mask.setBrush(g);
mask.setPen(Qt::NoPen);
mask.drawRect(maskPixmap.rect());
mask.
setBrush(QColor(Qt
::transparent));
mask.drawEllipse(g.center(), ring, ring);
mask.end();
}
center
= center
+ QPoint(0, radius
/2);
if (zoomPixmap.
size() != box
) zoomPixmap
= QPixmap(box
);
if (true)
{
zoomPixmap.fill(Qt::lightGray);
p.translate(-xy);
p.drawPixmap(offset * 2, largePixmap);
p.end();
}
clipPath.addEllipse(center, ring, ring);
p.
setRenderHint(QPainter::Antialiasing);
p.setClipPath(clipPath);
p.drawPixmap(corner, zoomPixmap);
p.setClipping(false);
p.drawPixmap(corner, maskPixmap);
p.setPen(Qt::gray);
p.drawPath(clipPath);
update();
}
}
void MagnifyingGlas
::keyPressEvent(QKeyEvent *event
) {
if (!zoomed)
{
if (event
->key
() == Qt
::Key_Left) offset
+= QPoint(20 ,
0);
if (event
->key
() == Qt
::Key_Right) offset
+= QPoint(-20,
0);
if (event
->key
() == Qt
::Key_Up) offset
+= QPoint(0 ,
20);
if (event
->key
() == Qt
::Key_Down) offset
+= QPoint(0 ,
-20);
if (event->key() == Qt::Key_Z || event->key() == Qt::Key_Select)
{
dragPos
= QPoint(width
()/2, height
()/2);
activateZoom();
}
update();
}
else
{
if (event->key() == Qt::Key_Z || event->key() == Qt::Key_Select)
{
zoomed = false;
update();
}
if (event
->key
() == Qt
::Key_Left) delta
+= QPoint(-15,
0);
if (event
->key
() == Qt
::Key_Right) delta
+= QPoint(15 ,
0);
if (event
->key
() == Qt
::Key_Up) delta
+= QPoint(0 ,
-15);
if (event
->key
() == Qt
::Key_Down) delta
+= QPoint(0 ,
15);
{
dragPos += delta;
update();
}
}
}
/*
magnifyingglas.cpp
*/
#include "magnifyingglas.h"
#include <QGraphicsItem>
MagnifyingGlas::MagnifyingGlas(QWidget *parent): QGraphicsView(new QGraphicsScene(), parent)
{
pressed = false;
snapped = false;
zoomed = false;
image.load("/home/kuehnel/Development/Unbenannt_2.png");
largePixmap.load("/home/kuehnel/Development/Unbenannt_2.png");
smallPixmap = largePixmap.scaled(largePixmap.size()/2);
offset = QPoint(0,0);
scene = new QGraphicsScene;
this->setScene(scene);
scene->addPixmap(largePixmap);
}
void MagnifyingGlas::pan(const QPoint &delta)
{
offset += delta;
update();
}
void MagnifyingGlas::activateZoom()
{
zoomed = true;
tapTimer.stop();
update();
}
void MagnifyingGlas::timerEvent(QTimerEvent *)
{
if (!zoomed) activateZoom();
update();
}
void MagnifyingGlas::mousePressEvent(QMouseEvent *event)
{
if (event->buttons()!= Qt::LeftButton) return;
pressed = snapped = true;
pressPos = dragPos = event->pos();
tapTimer.start(HOLD_TIME, viewport());
}
void MagnifyingGlas::mouseMoveEvent(QMouseEvent *event)
{
if (!event->buttons()) return;
if (!zoomed)
{
if (!pressed || !snapped)
{
QPoint delta = event->pos() - pressPos;
pressPos = event->pos();
pan(delta);
return;
}
else
{
const int threshold = 10;
QPoint delta = event->pos() - pressPos;
if (snapped)
{
snapped &= delta.x() < threshold;
snapped &= delta.y() < threshold;
snapped &= delta.x() > -threshold;
snapped &= delta.y() >-threshold;
}
if (!snapped) tapTimer.stop();
}
}
else
{
dragPos = event->pos();
update();
}
}
void MagnifyingGlas::mouseReleaseEvent(QMouseEvent *)
{
zoomed = false;
update();
}
void MagnifyingGlas::resizeEvent(QResizeEvent *)
{
zoomed = false;
update();
}
void MagnifyingGlas::paintEvent(QPaintEvent *e)
{
QPainter p(viewport());
p.drawPixmap(offset,smallPixmap);
p.end();
if (zoomed)
{
int dim = qMin(width(), height());
int magnifierSize = dim * 5/6;
int radius = magnifierSize / 2;
int ring = radius - 15;
QSize box = QSize(magnifierSize, magnifierSize);
if(maskPixmap.size() != box)
{
maskPixmap = QPixmap(box);
maskPixmap.fill(Qt::transparent);
QRadialGradient g;
g.setCenter(radius, radius);
g.setFocalPoint(radius, radius);
g.setRadius(radius);
g.setColorAt(1.0, QColor(64, 64, 64, 0));
g.setColorAt(0.5, QColor(0, 0, 0, 255));
QPainter mask(&maskPixmap);
mask.setRenderHint(QPainter::Antialiasing);
mask.setCompositionMode(QPainter::CompositionMode_Source);
mask.setBrush(g);
mask.setPen(Qt::NoPen);
mask.drawRect(maskPixmap.rect());
mask.setBrush(QColor(Qt::transparent));
mask.drawEllipse(g.center(), ring, ring);
mask.end();
}
QPoint center = dragPos - QPoint(0, radius);
center = center + QPoint(0, radius/2);
QPoint corner = center - QPoint(radius, radius);
QPoint xy = center * 2 - QPoint(radius, radius);
if (zoomPixmap.size() != box) zoomPixmap = QPixmap(box);
if (true)
{
zoomPixmap.fill(Qt::lightGray);
QPainter p(&zoomPixmap);
p.translate(-xy);
p.drawPixmap(offset * 2, largePixmap);
p.end();
}
QPainterPath clipPath;
clipPath.addEllipse(center, ring, ring);
QPainter p(viewport());
p.setRenderHint(QPainter::Antialiasing);
p.setClipPath(clipPath);
p.drawPixmap(corner, zoomPixmap);
p.setClipping(false);
p.drawPixmap(corner, maskPixmap);
p.setPen(Qt::gray);
p.drawPath(clipPath);
update();
}
}
void MagnifyingGlas::keyPressEvent(QKeyEvent *event)
{
if (!zoomed)
{
if (event->key() == Qt::Key_Left) offset += QPoint(20 , 0);
if (event->key() == Qt::Key_Right) offset += QPoint(-20, 0);
if (event->key() == Qt::Key_Up) offset += QPoint(0 , 20);
if (event->key() == Qt::Key_Down) offset += QPoint(0 , -20);
if (event->key() == Qt::Key_Z || event->key() == Qt::Key_Select)
{
dragPos = QPoint(width()/2, height()/2);
activateZoom();
}
update();
}
else
{
if (event->key() == Qt::Key_Z || event->key() == Qt::Key_Select)
{
zoomed = false;
update();
}
QPoint delta(0,0);
if (event->key() == Qt::Key_Left) delta += QPoint(-15, 0);
if (event->key() == Qt::Key_Right) delta += QPoint(15 , 0);
if (event->key() == Qt::Key_Up) delta += QPoint(0 , -15);
if (event->key() == Qt::Key_Down) delta += QPoint(0 , 15);
if (delta != QPoint(0, 0))
{
dragPos += delta;
update();
}
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks