Initial commit
This commit is contained in:
commit
b7d90b9afe
16245 files changed
+3396666
No files matched your search
+137
@@ -0,0 +1,137 @@
|
||||
/*
|
||||
* @RDE_HEADER@
|
||||
* graphicsview widget to resize qpixmaps/qimages with mouse drag and mwheel
|
||||
* $Id$
|
||||
* $HeadURL$
|
||||
* $Author$
|
||||
* Created by Jens Heilmann, SLTE, 2020-10-14
|
||||
*/
|
||||
|
||||
#include "lts_image_cropper.h"
|
||||
#include <QGraphicsItem>
|
||||
#include <QMouseEvent>
|
||||
#include <QApplication>
|
||||
|
||||
namespace LtsWidgets
|
||||
{
|
||||
|
||||
LtsImageCropper::LtsImageCropper(QWidget *parent, const QSize &size, const QPixmap &pixmap)
|
||||
: QGraphicsView(parent)
|
||||
, m_scene(new QGraphicsScene(this))
|
||||
, m_maxScale(2.0f)
|
||||
, m_multiplier(1.0f)
|
||||
{
|
||||
if(!size.isNull())
|
||||
setFixedSize(size);
|
||||
|
||||
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||||
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||||
setFrameStyle(QFrame::NoFrame);
|
||||
setDragMode(QGraphicsView::ScrollHandDrag);
|
||||
setTransformationAnchor(QGraphicsView::AnchorViewCenter);
|
||||
|
||||
m_scene->setBackgroundBrush(Qt::Dense6Pattern);
|
||||
setScene(m_scene);
|
||||
|
||||
setPixmap(pixmap);
|
||||
}
|
||||
|
||||
LtsImageCropper::~LtsImageCropper()
|
||||
{
|
||||
delete m_pixmapItem;
|
||||
}
|
||||
|
||||
void LtsImageCropper::setPixmap(const QPixmap &pixmap)
|
||||
{
|
||||
if(pixmap.isNull())
|
||||
return;
|
||||
|
||||
delete m_pixmapItem; // delete previous pixmapitem
|
||||
|
||||
m_pixmapItem = new QGraphicsPixmapItem(pixmap);
|
||||
m_scene->addItem(m_pixmapItem);
|
||||
|
||||
setTransform(QTransform()); // set a fresh transformation matrix
|
||||
m_currentZoom = 1.0; // reset current zoom
|
||||
|
||||
//check if image size is min view, if not scale it
|
||||
double sx = static_cast<double>(size().height()) / pixmap.height();
|
||||
double sy = static_cast<double>(size().width()) / pixmap.width();
|
||||
if(sx > 1 )
|
||||
scale(sx,sx);
|
||||
if(sy > 1)
|
||||
scale(sy,sy);
|
||||
|
||||
// calculate min scale from qMin(width,height);
|
||||
double ratioWidth = 1.0 * size().width() / pixmap.width();
|
||||
double ratioHeight = 1.0 * size().height() / pixmap.height();
|
||||
m_minScale = qMax(ratioHeight,ratioWidth);
|
||||
|
||||
calculateScaleStep();
|
||||
|
||||
// change scene boundaries to image rectangle and center scene on image center
|
||||
m_scene->setSceneRect(pixmap.rect());
|
||||
centerOn(m_scene->sceneRect().center());
|
||||
}
|
||||
|
||||
void LtsImageCropper::setImage(const QImage &image)
|
||||
{
|
||||
setPixmap(QPixmap::fromImage(image));
|
||||
}
|
||||
|
||||
void LtsImageCropper::setMultiplier(double multiplier)
|
||||
{
|
||||
m_multiplier = multiplier;
|
||||
calculateScaleStep();
|
||||
}
|
||||
|
||||
void LtsImageCropper::calculateScaleStep()
|
||||
{
|
||||
m_stepScale = (m_maxScale - m_minScale) / 100.0;
|
||||
}
|
||||
|
||||
QImage LtsImageCropper::image()
|
||||
{
|
||||
return grab().toImage();
|
||||
}
|
||||
|
||||
void LtsImageCropper::wheelEvent(QWheelEvent *event)
|
||||
{
|
||||
if(!m_pixmapItem)
|
||||
return;
|
||||
|
||||
auto value = event->pixelDelta();
|
||||
if(value.isNull())
|
||||
value = event->angleDelta() / 15.0;
|
||||
|
||||
// calculate scrollZoom value to be added to the current zoom
|
||||
double scrollZoom = m_stepScale * value.y() * m_multiplier / 8.0;
|
||||
|
||||
m_currentZoom += scrollZoom;
|
||||
|
||||
if(m_currentZoom > m_maxScale)
|
||||
m_currentZoom = m_maxScale;
|
||||
else if (m_currentZoom < m_minScale)
|
||||
m_currentZoom = m_minScale;
|
||||
|
||||
// scale the view
|
||||
setTransform(QTransform(m_currentZoom, transform().m12(), transform().m13(),
|
||||
transform().m21(), m_currentZoom, transform().m23(),
|
||||
transform().m31(), transform().m32(), 1));
|
||||
}
|
||||
|
||||
void LtsImageCropper::mousePressEvent(QMouseEvent *event)
|
||||
{
|
||||
QApplication::setOverrideCursor(Qt::DragMoveCursor);
|
||||
update();
|
||||
QGraphicsView::mousePressEvent(event);
|
||||
}
|
||||
|
||||
void LtsImageCropper::mouseReleaseEvent(QMouseEvent *event)
|
||||
{
|
||||
QApplication::restoreOverrideCursor();
|
||||
update();
|
||||
QGraphicsView::mouseReleaseEvent(event);
|
||||
}
|
||||
|
||||
}
|
||||
+108
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* @RDE_HEADER@
|
||||
* graphicsview widget to resize qpixmaps/qimages with mouse drag and mwheel
|
||||
* $Id$
|
||||
* $HeadURL$
|
||||
* $Author$
|
||||
* Created by Jens Heilmann, SLTE, 2020-10-14
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "lts_widgets_export.h"
|
||||
#include <QGraphicsView>
|
||||
|
||||
namespace LtsWidgets
|
||||
{
|
||||
|
||||
/**
|
||||
* @brief The ImageCropper class provides a graphicsview widget to crop images with mouse drag and mwheel scroll.
|
||||
* avatar() returns a pixmap of the current view (the cropped image).
|
||||
*/
|
||||
class LTSWIDGETS_EXPORT LtsImageCropper : public QGraphicsView
|
||||
{
|
||||
|
||||
Q_OBJECT
|
||||
|
||||
Q_PROPERTY(QPixmap pixMap READ pixmap WRITE setPixmap)
|
||||
Q_PROPERTY(double multiplier READ multiplier WRITE setMultiplier)
|
||||
Q_PROPERTY(double maxZoom READ maxZoom WRITE setMaxZoom)
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief ImageCropper
|
||||
* @param parent
|
||||
* @param size specifies the size of the graphicsview widget, aswell as the size of the cropped image.
|
||||
* @param image to crop
|
||||
*/
|
||||
LtsImageCropper(QWidget *parent = nullptr, const QSize &size = {} , const QPixmap &pixmap = {});
|
||||
~LtsImageCropper();
|
||||
|
||||
/**
|
||||
* @brief setter for image
|
||||
* @param image
|
||||
*/
|
||||
void setPixmap(const QPixmap &pixmap);
|
||||
|
||||
/**
|
||||
* @brief setter for image
|
||||
* @param image
|
||||
*/
|
||||
void setImage(const QImage &image);
|
||||
|
||||
/**
|
||||
* @brief set the zoom mutliplier for mwheel scaling e.g. 1.03.
|
||||
* @param multiplier
|
||||
*/
|
||||
void setMultiplier(double multiplier);
|
||||
|
||||
/**
|
||||
* @brief set the maximum zoom in scale e.g. 2.0 = 200% zoom in.
|
||||
* @param maxZoom
|
||||
*/
|
||||
void setMaxZoom(double maxZoom) { m_maxScale = maxZoom; }
|
||||
|
||||
/**
|
||||
* @brief returns the current graphicsview view as a pixmap.
|
||||
* @return qpixmap
|
||||
*/
|
||||
QPixmap pixmap() { return grab(); }
|
||||
|
||||
/**
|
||||
* @brief returns the current graphicsview view as qimage.
|
||||
* @return qimage
|
||||
*/
|
||||
QImage image();
|
||||
|
||||
/**
|
||||
* @brief returns the current multiplier
|
||||
* @return double
|
||||
*/
|
||||
double multiplier() const { return m_multiplier; }
|
||||
|
||||
/**
|
||||
* @brief returns the current max zoom value
|
||||
* @return double
|
||||
*/
|
||||
double maxZoom() const { return m_maxScale; }
|
||||
|
||||
private:
|
||||
QGraphicsScene *m_scene = nullptr;
|
||||
QGraphicsPixmapItem *m_pixmapItem = nullptr;
|
||||
|
||||
double m_currentZoom;
|
||||
double m_minScale;
|
||||
double m_maxScale;
|
||||
double m_stepScale;
|
||||
double m_multiplier;
|
||||
|
||||
void calculateScaleStep();
|
||||
|
||||
protected:
|
||||
void wheelEvent(QWheelEvent *event) override;
|
||||
void mousePressEvent(QMouseEvent *event) override;
|
||||
void mouseReleaseEvent(QMouseEvent *event) override;
|
||||
|
||||
};
|
||||
|
||||
} // namespace LtsWidgets
|
||||
Reference in new issue
Block a user