Files
qt-migration-legatus/qt6-after/qt6-migrated-legatus/Lts/Common/LtsWidgets/LtsImageCropper/lts_image_cropper.cpp
T
torstenandCopilot d6b6ffce7c Apply clang-format to Lts/Common and DTC/enum headers
- Formatted all CodeGen module files (9 files)
- Formatted LtsAppDescriptor module files
- Formatted LtsAppManager/AppChooser files
- Applied clang-format to all DTC and enum headers across modules
- Removed coordTransTests from CommonUnitTestsPlaceholder (moved to tests/)

Cosmetic changes only, no functional modifications.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-08-19 14:46:29 +02:00

123 lines
2.9 KiB
C++

#include "lts_image_cropper.h"
#include <QApplication>
#include <QGraphicsItem>
#include <QMouseEvent>
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;
m_pixmapItem = new QGraphicsPixmapItem(pixmap);
m_scene->addItem(m_pixmapItem);
setTransform(QTransform());
m_currentZoom = 1.0;
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);
double ratioWidth = 1.0 * size().width() / pixmap.width();
double ratioHeight = 1.0 * size().height() / pixmap.height();
m_minScale = qMax(ratioHeight, ratioWidth);
calculateScaleStep();
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;
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;
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);
}
} // namespace LtsWidgets