From e1368be6455f608a99bbb15cf9a15f4a7c4c5d63 Mon Sep 17 00:00:00 2001 From: Uwe Kindler Date: Sat, 5 Nov 2022 10:14:40 +0100 Subject: [PATCH] Added missing render widget to demo application --- demo/RenderWidget.cpp | 114 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 demo/RenderWidget.cpp diff --git a/demo/RenderWidget.cpp b/demo/RenderWidget.cpp new file mode 100644 index 0000000..41e3024 --- /dev/null +++ b/demo/RenderWidget.cpp @@ -0,0 +1,114 @@ +//============================================================================ +/// \file RenderWidget.cpp +/// \author Uwe Kindler +/// \date 04.11.2022 +/// \brief Implementation of CRenderWidget +//============================================================================ + + +//============================================================================ +// INCLUDES +//============================================================================ +#include "RenderWidget.h" + +#include +#include + + +//=========================================================================== +CRenderWidget::CRenderWidget(QWidget* Parent) : + QWidget(Parent), m_ScaleFactor(1) +{ + // + // OpaquePaintEvent indicates that we do not need an auto-filled + // background. It is used for widgets filling the whole paint area + // with its own opaque colors and need to draw its contents quickly. + // This applies for a capture widget. + // + this->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); + this->setCursor(Qt::OpenHandCursor); +} + +//=========================================================================== +CRenderWidget::~CRenderWidget() +{ + +} + +//=========================================================================== +void CRenderWidget::showImage(const QImage& Image) +{ + m_Image = QPixmap::fromImage(Image); + this->adjustWidgetSize(); + this->repaint(); +} + +//=========================================================================== +void CRenderWidget::paintEvent(QPaintEvent* Event) +{ + Q_UNUSED(Event); + QPainter Painter(this); + Painter.setRenderHint(QPainter::SmoothPixmapTransform, true); + Painter.setRenderHint(QPainter::Antialiasing, true); + Painter.scale(m_ScaleFactor, m_ScaleFactor); + Painter.drawPixmap(QPoint(0, 0), m_Image); +} + +//============================================================================ +void CRenderWidget::zoomIn() +{ + scaleImage(1.25); +} + +//============================================================================ +void CRenderWidget::zoomOut() +{ + scaleImage(0.8); +} + +//============================================================================ +void CRenderWidget::zoomByValue(double ZoomValue) +{ + scaleImage(ZoomValue); +} + +//============================================================================ +void CRenderWidget::normalSize() +{ + m_ScaleFactor = 1; + this->adjustWidgetSize(); +} + +//============================================================================ +void CRenderWidget::scaleImage(double ScaleFactor) +{ + m_ScaleFactor *= ScaleFactor; + this->adjustWidgetSize(); +} + +//============================================================================ +void CRenderWidget::adjustWidgetSize() +{ + QSize ScaledImageSize = m_Image.size() * m_ScaleFactor; + if (ScaledImageSize != this->size()) + { + this->setFixedSize(ScaledImageSize); + } +} + +//============================================================================ +void CRenderWidget::scaleToSize(const QSize& TargetSize) +{ + if (m_Image.isNull()) + { + return; + } + double ScaleFactorH = (double) TargetSize.width() / m_Image.size().width(); + double ScaleFactorV = (double) TargetSize.height() + / m_Image.size().height(); + m_ScaleFactor = (ScaleFactorH < ScaleFactorV) ? ScaleFactorH : ScaleFactorV; + this->adjustWidgetSize(); +} + +//--------------------------------------------------------------------------- +// EOF RenderWidget.cpp