6#pragma warning(disable : 4251)
7#pragma warning(disable : 4172)
8#pragma warning(disable : 4127)
9#pragma warning(disable : 4800)
11#include <QQuickPaintedItem>
18#include "../image.hpp"
43 class ImageLabelItem :
public QQuickItem
47 Q_PROPERTY(
double imageX MEMBER imageX_ READ
ImageX WRITE
SetImageX NOTIFY NotifyImageX);
48 Q_PROPERTY(
double imageY MEMBER imageY_ READ
ImageY WRITE
SetImageY NOTIFY NotifyImageY);
55 explicit ImageLabelItem(QQuickItem *parent =
nullptr);
69 static void Register(
const char *uri,
int versionMajor,
int versionMinor,
const char *qmlName)
71 qmlRegisterType<T>(uri, versionMajor, versionMinor, qmlName);
72 qmlRegisterUncreatableMetaObject(Cvb::UI::staticMetaObject, uri, versionMajor, versionMinor,
"LabelScale",
73 "CVB: cannot object for enumerations");
69 static void Register(
const char *uri,
int versionMajor,
int versionMinor,
const char *qmlName) {
…}
109 if (imageX_ == imageX)
137 if (imageY_ == imageY)
181 return static_cast<int>(labelScale_);
206 void OnImageXChanged();
208 void OnImageYChanged();
210 void OnImageViewChanged();
212 void OnLabelScaleChanged();
218 void NotifyImageView();
219 void NotifyLabelScale();
222 void SetupConnections();
224 double imageX_ = 0.0;
225 double imageY_ = 0.0;
43 class ImageLabelItem :
public QQuickItem {
…};
243 Q_PROPERTY(
int width READ
Width NOTIFY NotifyRefreshImage)
244 Q_PROPERTY(
int height READ
Height NOTIFY NotifyRefreshImage)
245 Q_PROPERTY(
int planesCount READ
PlanesCount NOTIFY NotifyRefreshImage)
269 UnregisterEventImageDataUpdated();
287 UnregisterEventImageDataUpdated();
291 eventCookieDataUpdated_ =
295 NotifyRefreshImage();
325 return image_->Width();
341 return image_->Height();
357 return image_->PlanesCount();
361 void UnregisterEventImageDataUpdated() noexcept
364 image_->UnregisterEventPixelContentChanged(eventCookieDataUpdated_);
376 void NotifyRefresh();
377 void NotifyRefreshImage();
389 class ImageViewItem :
public QQuickPaintedItem
397 Q_PROPERTY(QRectF imageRect READ
ImageRect NOTIFY NotifyImageRect)
398 Q_PROPERTY(QRectF viewRect READ
ViewRect NOTIFY NotifyViewRect)
399 Q_PROPERTY(QRectF sourceRect READ
SourceRect NOTIFY NotifySourceRect)
400 Q_PROPERTY(QRectF targetRect READ
TargetRect NOTIFY NotifyTargetRect)
402 Q_PROPERTY(QObject *image READ Image WRITE
SetImage NOTIFY NotifyImage)
410 Q_PROPERTY(QVector<double> hoverPixel READ
HoverPixel NOTIFY NotifyHover)
411 Q_PROPERTY(QPointF hoverPosition READ
HoverPosition NOTIFY NotifyHover)
423 auto result = dispatcher_->TryZoomIn([&]() { NotifyTargetRect(); }, [&] { NotifySourceRect(); });
441 auto result = dispatcher_->TryZoomOut([&]() { NotifyTargetRect(); }, [&] { NotifySourceRect(); });
461 auto result = dispatcher_->TryTranslate(
QtToCvb(translation), [&]() { NotifySourceRect(); });
488 return CvbToQt(dispatcher_->MapTargetToSource(
QtToCvb(targetPoint)));
499 return CvbToQt(dispatcher_->MapSourceToImage(
QtToCvb(sourcePoint)));
510 return CvbToQt(dispatcher_->MapTargetToView(
QtToCvb(targetPoint)));
521 return CvbToQt(dispatcher_->MapSourceToTarget(
QtToCvb(sourcePoint)));
532 return CvbToQt(dispatcher_->MapImageToSource(
QtToCvb(imagePoint)));
559 auto image = image_->Image();
563 dispatcher_->UploadImage(*image, [&]() { NotifyImageRect(); });
567#pragma endregion QML Interface
573 : QQuickPaintedItem(parent)
575 dispatcher_ = std::make_unique<Private::ImageViewDispatcher>([&]() { NotifyZoom(); });
578 setAcceptedMouseButtons(Qt::AllButtons);
579 setAcceptHoverEvents(
true);
581 connect(
this, &ImageViewItem::NotifyViewRect,
this, &ImageViewItem::OnViewRectChanged);
582 connect(
this, &ImageViewItem::NotifyImageRect,
this, &ImageViewItem::OnImageRectChanged);
583 connect(
this, &ImageViewItem::NotifyZoom,
this, &ImageViewItem::OnZoomIDChanged);
584 connect(
this, &ImageViewItem::NotifyZoom,
this, &ImageViewItem::OnZoomFactorChanged);
585 connect(
this, &ImageViewItem::NotifyViewAnchor,
this, &ImageViewItem::OnViewAnchorChnaged);
600 static void Register(
const char *uri,
int versionMajor,
int versionMinor,
const char *qmlName)
602 qmlRegisterType<T>(uri, versionMajor, versionMinor, qmlName);
603 qmlRegisterUncreatableMetaObject(Cvb::UI::staticMetaObject, uri, versionMajor, versionMinor,
"ZoomID",
604 "CVB: cannot object for enumerations");
605 qmlRegisterUncreatableMetaObject(Cvb::UI::staticMetaObject, uri, versionMajor, versionMinor,
"UploadMode",
606 "CVB: cannot object for enumerations");
600 static void Register(
const char *uri,
int versionMajor,
int versionMinor,
const char *qmlName) {
…}
621#pragma endregion CTor / DTor / Register
634 return CvbToQt(dispatcher_->ImageRect());
646 return CvbToQt(dispatcher_->ViewRect());
658 return CvbToQt(dispatcher_->TargetRect());
670 return CvbToQt(dispatcher_->SourceRect());
674 QObject *
Image()
const
690 return static_cast<int>(dispatcher_->ZoomID());
704 dispatcher_->UpdateZoomID(
static_cast<UI::ZoomID>(
id), [&]() { NotifyZoom(); });
716 return dispatcher_->ZoomFactor();
728 dispatcher_->UpdateZoomFactor(factor, [&]() { NotifyZoom(); });
763 return CvbToQt(dispatcher_->ViewAnchor());
775 dispatcher_->UpdateViewAnchor(
QtToCvb(viewAnchor), [&]() { NotifyViewAnchor(); });
787 return CvbToQt(dispatcher_->ImageAnchor());
799 dispatcher_->UpdateImageAnchor(
QtToCvb(imageAnchor), [&]() { NotifyImageAnchor(); });
814 return QVector<double>();
816 auto image = image_->Image();
817 if (!image || !dispatcher_->IsHoverPositionValid())
818 return QVector<double>();
820 auto hoverPosition = dispatcher_->HoverPosition();
822 static_cast<int>(
std::floor(hoverPosition.Y())));
825 if (hoverPositionInt.
X() == image->Width())
826 hoverPositionInt.
SetX(
static_cast<int>(hoverPosition.X() - 1.0));
827 if (hoverPositionInt.
Y() == image->Height())
828 hoverPositionInt.
SetY(
static_cast<int>(hoverPosition.Y() - 1.0));
830 return QVector<double>::fromStdVector(image->GetPixel(hoverPositionInt));
842 if (!dispatcher_->IsHoverPositionValid())
845 return CvbToQt(dispatcher_->HoverPosition());
859 return static_cast<int>(dispatcher_->UploadMode());
873 dispatcher_->UpdateUploadMode(
static_cast<UI::UploadMode>(uploadMode), [&]() { NotifyUploadMode(); });
876#pragma endregion Properties
882 void OnViewRectChanged()
884 UpdateTargetSource();
887 void OnImageRectChanged()
889 UpdateTargetSource();
892 void OnZoomFactorChanged()
894 UpdateTargetSource();
898 void OnZoomIDChanged()
900 UpdateTargetSource();
904 void OnViewAnchorChnaged()
906 auto targetPoint = dispatcher_->MapViewToTarget(dispatcher_->ViewAnchor());
907 targetPoint = dispatcher_->LimitPointToRect(targetPoint, dispatcher_->TargetRect());
909 auto sourcePoint = dispatcher_->MapTargetToSource(targetPoint);
910 auto imagePoint = dispatcher_->MapSourceToImage(sourcePoint);
912 dispatcher_->UpdateImageAnchor(imagePoint, [&]() { NotifyImageAnchor(); });
915#pragma endregion Slots
920 void UpdateTargetSource()
922 dispatcher_->UpdateTargetSource([&]() { NotifyTargetRect(); }, [&]() { NotifySourceRect(); });
925 QImage ScreenImage() const noexcept
927 auto data = dispatcher_->Buffer();
928 auto size = dispatcher_->BufferSize();
930 return QImage(data, size.Width(), size.Height(), QImage::Format_ARGB32_Premultiplied);
933#pragma endregion Helper
937 void paint(QPainter *painter)
override
940 Private::ImageViewDispatcherGuard guard(*dispatcher_);
941 if (!dispatcher_->IsBufferValid())
944 auto screenImage = ScreenImage();
946 if (dispatcher_->UploadMode() == UI::UploadMode::Image)
949 painter->drawImage(
CvbToQt(dispatcher_->TargetRect()), screenImage,
CvbToQt(dispatcher_->SourceRect()));
953 auto image = image_->Image();
957 painter->drawImage(
CvbToQt(dispatcher_->TargetRect()), screenImage,
CvbToQt(dispatcher_->SourceRectAdj()));
961 void geometryChanged(
const QRectF &newGeometry,
const QRectF &oldGeometry)
override
963 auto tmpNewGemotry =
QtToCvb(newGeometry);
964 if (tmpNewGemotry != dispatcher_->ViewRect())
965 dispatcher_->UpdateViewRect(tmpNewGemotry, [&]() { NotifyViewRect(); });
967 QQuickPaintedItem::geometryChanged(newGeometry, oldGeometry);
973 void wheelEvent(QWheelEvent *event)
override
976 auto numDegrees =
event->angleDelta().y();
978 if (numDegrees <= -15)
980 else if (numDegrees >= 15)
984 void mouseMoveEvent(QMouseEvent *event)
override
986 if (event->buttons() & Qt::LeftButton)
988 auto currentPos =
QtToCvb(mapFromGlobal(event->globalPos()));
989 if (!dispatcher_->IsLastMousePositionValid())
991 dispatcher_->InvalidateLastMousePosition(currentPos);
996 dispatcher_->InvalidateLastMousePosition(currentPos);
1000 void mouseReleaseEvent(QMouseEvent *)
override
1002 dispatcher_->InvalidateLastMousePosition();
1005 void mousePressEvent(QMouseEvent *)
override
1010 void hoverMoveEvent(QHoverEvent *event)
override
1012 auto hoverPosition = dispatcher_->MapViewToImage(
QtToCvb(event->posF()));
1013 if (!dispatcher_->ImageRect().Contains(hoverPosition))
1016 dispatcher_->UpdateHoverPosition(hoverPosition, [&]() { NotifyHover(); });
1019#pragma endregion Reimplemented from Qt
1023 std::unique_ptr<UI::Private::ImageViewDispatcher> dispatcher_;
1025 ImageController *image_ =
nullptr;
1027#pragma endregion Members
1033 void NotifyImageRect();
1034 void NotifyViewRect();
1035 void NotifySourceRect();
1036 void NotifyTargetRect();
1042 void NotifyViewAnchor();
1043 void NotifyImageAnchor();
1047 void NotifyUploadMode();
1049#pragma endregion Notify
389 class ImageViewItem :
public QQuickPaintedItem {
…};
1052 inline ImageLabelItem::ImageLabelItem(QQuickItem *parent)
1053 : QQuickItem(parent)
1055 setTransformOrigin(QQuickItem::TopLeft);
1056 connect(
this, &ImageLabelItem::NotifyImageView,
this, &ImageLabelItem::OnImageViewChanged);
1059 inline void ImageLabelItem::OnImageXChanged()
1064 auto sourceRect = view_->SourceRect();
1065 auto sourceX = imageX_ - sourceRect.x();
1066 auto targetX = sourceX * view_->ZoomFactor();
1067 auto targetRect = view_->TargetRect();
1068 auto viewX = targetX + targetRect.x();
1073 inline void ImageLabelItem::OnImageYChanged()
1078 auto sourceRect = view_->SourceRect();
1079 auto sourceY = imageY_ - sourceRect.y();
1080 auto targetY = sourceY * view_->ZoomFactor();
1081 auto targetRect = view_->TargetRect();
1082 auto viewY = targetY + targetRect.y();
1087 inline void ImageLabelItem::OnImageViewChanged()
1092 OnLabelScaleChanged();
1095 inline void ImageLabelItem::OnLabelScaleChanged()
1097 if (labelScale_ == UI::LabelScale::Off)
1106 setScale(view_->ZoomFactor());
1109 inline void ImageLabelItem::SetupConnections()
1114 connect(
this, &ImageLabelItem::NotifyImageX,
this, &ImageLabelItem::OnImageXChanged);
1115 connect(
this, &ImageLabelItem::NotifyImageY,
this, &ImageLabelItem::OnImageYChanged);
1116 connect(
this, &ImageLabelItem::NotifyLabelScale,
this, &ImageLabelItem::OnLabelScaleChanged);
1118 connect(view_, &ImageViewItem::NotifySourceRect,
this, &ImageLabelItem::OnImageXChanged);
1119 connect(view_, &ImageViewItem::NotifySourceRect,
this, &ImageLabelItem::OnImageYChanged);
1120 connect(view_, &ImageViewItem::NotifyTargetRect,
this, &ImageLabelItem::OnImageXChanged);
1121 connect(view_, &ImageViewItem::NotifyTargetRect,
this, &ImageLabelItem::OnImageYChanged);
1122 connect(view_, &ImageViewItem::NotifyZoom,
this, &ImageLabelItem::OnImageXChanged);
1123 connect(view_, &ImageViewItem::NotifyZoom,
this, &ImageLabelItem::OnImageYChanged);
1125 connect(view_, &ImageViewItem::NotifyZoom,
this, &ImageLabelItem::OnLabelScaleChanged);
Cookie used to unregister event handlers.
Definition global.hpp:591
Multi-purpose 2D vector class.
Definition point_2d.hpp:20
T X() const noexcept
Gets the x-component of the point.
Definition point_2d.hpp:84
T Y() const noexcept
Gets the y-component of the point.
Definition point_2d.hpp:104
void SetX(T x) noexcept
Sets the x-component of the point.
Definition point_2d.hpp:94
void SetY(T y)
Sets the y-component of the point.
Definition point_2d.hpp:114
Rectangle object.
Definition rect.hpp:24
View to display an image.
Definition decl_image_view.hpp:69
Controller object for the QML image view item.
Definition image_view_item.hpp:240
int Width() const noexcept
Width of the shared image in pixels.
Definition image_view_item.hpp:321
int Height() const noexcept
Height of the shared image in pixels.
Definition image_view_item.hpp:337
void Refresh(const ImagePtr &image, AutoRefresh autoRefresh=AutoRefresh::Off)
Share the image and refresh the view.
Definition image_view_item.hpp:280
int PlanesCount() const noexcept
Get the number of planes for this image.
Definition image_view_item.hpp:353
void Refresh()
Refresh the view.
Definition image_view_item.hpp:303
void ReleaseRefreshShare() noexcept
Releases the shared image.
Definition image_view_item.hpp:266
ImagePtr Image() const
Get the currently shared image.
Definition image_view_item.hpp:253
Image label item for QML.
Definition image_view_item.hpp:44
double ImageY() const noexcept
Get the Y position of this label on the image.
Definition image_view_item.hpp:123
void SetImageView(ImageViewItem *view)
Set the image view for this label.
Definition image_view_item.hpp:159
void SetImageY(double imageY)
Set the Y position of this label on the image.
Definition image_view_item.hpp:135
double ImageX() const noexcept
Get the X position of this label on the image.
Definition image_view_item.hpp:95
void SetImageX(double imageX)
Set the X position of this label on the image.
Definition image_view_item.hpp:107
static void Register()
Convenience method to register this type in QML.
Definition image_view_item.hpp:83
void SetLabelScale(int labelScale)
Set the label scale behaviour (as integer, see Cvb::UI::LabelScale).
Definition image_view_item.hpp:195
int LabelScale() const noexcept
Get the label scale behaviour (as integer, see Cvb::UI::LabelScale).
Definition image_view_item.hpp:179
static void Register(const char *uri, int versionMajor, int versionMinor, const char *qmlName)
Convenience method to register this type or a derived type in QML.
Definition image_view_item.hpp:69
Image view item for QML.
Definition image_view_item.hpp:391
Q_INVOKABLE QPointF MapViewToTarget(const QPointF &viewPoint) const
Maps a point from the view rectangle to the target rect.
Definition image_view_item.hpp:475
Q_INVOKABLE QPointF MapSourceToTarget(const QPointF &sourcePoint) const
Maps a point from the source rectangle to the target rectangle.
Definition image_view_item.hpp:519
Q_INVOKABLE QPointF MapTargetToSource(const QPointF &targetPoint) const
Maps a point from the target rectangle to the source rect.
Definition image_view_item.hpp:486
int ZoomID() const
Get the zoom identifier for special zoom steps (as integer, see Cvb::UI::ZoomID).
Definition image_view_item.hpp:688
void SetZoomID(int id)
Set the zoom identifier for special zoom steps (as integer, see Cvb::UI::ZoomID).
Definition image_view_item.hpp:702
QRectF SourceRect() const noexcept
Get the rectangle inside the image rectangle that is actually rendered.
Definition image_view_item.hpp:668
QPointF HoverPosition() const noexcept
Get the point the mouse is hovering over.
Definition image_view_item.hpp:840
QPointF ViewAnchor() const noexcept
Get the reference point for mouse operations in view coordinates.
Definition image_view_item.hpp:761
Q_INVOKABLE bool TryTranslate(const QPointF &translation)
Tries to translate the target rectangle to a give point.
Definition image_view_item.hpp:459
Q_INVOKABLE QPointF MapImageToSource(const QPointF &imagePoint) const
Maps a point from the image rectangle to the source rectangle.
Definition image_view_item.hpp:530
void SetImage(QObject *image)
Set the image controller for this view item.
Definition image_view_item.hpp:741
void SetViewAnchor(const QPointF &viewAnchor)
Set the reference point for mouse operations in view coordinates.
Definition image_view_item.hpp:773
QRectF ViewRect() const noexcept
Get the rectangle this items covers.
Definition image_view_item.hpp:644
QVector< double > HoverPixel() const
The image pixel value the mouse is currently hovered over.
Definition image_view_item.hpp:811
Q_INVOKABLE bool TryZoomOut()
Tries to zoom out.
Definition image_view_item.hpp:439
Q_INVOKABLE QPointF MapTargetToView(const QPointF &targetPoint) const
Maps a point from the target rectangle to the view rectangle.
Definition image_view_item.hpp:508
void SetImageAnchor(const QPointF &imageAnchor)
Set the reference point for mouse operations in image coordinates.
Definition image_view_item.hpp:797
QRectF ImageRect() const noexcept
Get the rectangle described by the image buffer associated with the view.
Definition image_view_item.hpp:632
int UploadMode() const noexcept
Get the current upload mode (as integer, see Cvb::UI::UploadMode).
Definition image_view_item.hpp:857
Q_INVOKABLE QPointF MapSourceToImage(const QPointF &sourcePoint) const
Maps a point from the source rectangle to the image rect.
Definition image_view_item.hpp:497
Q_INVOKABLE bool TryZoomIn()
Tries to zoom in.
Definition image_view_item.hpp:421
static void Register()
Convenience method to register this type in QML.
Definition image_view_item.hpp:616
QRectF TargetRect() const noexcept
Get the rectangle inside the view rectangle the image is actually rendered to.
Definition image_view_item.hpp:656
void SetZoomFactor(double factor)
Set the view's zoom factor.
Definition image_view_item.hpp:726
QPointF ImageAnchor() const noexcept
Get the reference point for mouse operations in image coordinates.
Definition image_view_item.hpp:785
void Refresh()
Refresh the currently shared image.
Definition image_view_item.hpp:554
Q_INVOKABLE QPointF MapViewToImage(const QPointF &viewPoint) const
Maps a point from the view rectangle to the image rectangle.
Definition image_view_item.hpp:541
void SetUploadMode(int uploadMode)
Set the current upload mode (as integer).
Definition image_view_item.hpp:871
double ZoomFactor() const
Get the view's zoom factor.
Definition image_view_item.hpp:714
static void Register(const char *uri, int versionMajor, int versionMinor, const char *qmlName)
Convenience method to register this type or a derived type in QML.
Definition image_view_item.hpp:600
Namespace for QML related classes.
Definition image_view_item.hpp:30
Namespace for user interface components.
Definition decl_image_scene.hpp:39
ZoomID
Identifier for a zoom factor.
Definition detail_ui.hpp:61
Cvb::String QtToCvb(const QString text) noexcept
Convenience converter for strings.
Definition ui.hpp:242
AutoRefresh
Allows to automatically refresh, if image content changes.
Definition detail_ui.hpp:114
@ On
Definition detail_ui.hpp:121
@ Off
Definition detail_ui.hpp:125
LabelScale
Switch defining if image view labels are sensitive to zoom operations.
Definition detail_ui.hpp:130
UploadMode
Defines the upload behavior.
Definition detail_ui.hpp:37
@ Viewport
Definition detail_ui.hpp:53
@ Image
Definition detail_ui.hpp:45
QString CvbToQt(const Cvb::String &text) noexcept
Convenience converter for strings.
Definition ui.hpp:257
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
std::shared_ptr< Image > ImagePtr
Convenience shared pointer for Image.
Definition global.hpp:86