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");
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;
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)));
565 return CvbToQt(dispatcher_->MapTargetToImage(
QtToCvb(targetPoint)));
576 return CvbToQt(dispatcher_->MapImageToTarget(
QtToCvb(imagePoint)));
592 auto image = image_->Image();
596 dispatcher_->UploadImage(*image, [&]() { NotifyImageRect(); });
600#pragma endregion QML Interface
606 : QQuickPaintedItem(parent)
608 dispatcher_ = std::make_unique<Private::ImageViewDispatcher>([&]() { NotifyZoom(); });
611 setAcceptedMouseButtons(Qt::AllButtons);
612 setAcceptHoverEvents(
true);
614 connect(
this, &ImageViewItem::NotifyViewRect,
this, &ImageViewItem::OnViewRectChanged);
615 connect(
this, &ImageViewItem::NotifyImageRect,
this, &ImageViewItem::OnImageRectChanged);
616 connect(
this, &ImageViewItem::NotifyZoom,
this, &ImageViewItem::OnZoomIDChanged);
617 connect(
this, &ImageViewItem::NotifyZoom,
this, &ImageViewItem::OnZoomFactorChanged);
618 connect(
this, &ImageViewItem::NotifyViewAnchor,
this, &ImageViewItem::OnViewAnchorChnaged);
633 static void Register(
const char *uri,
int versionMajor,
int versionMinor,
const char *qmlName)
635 qmlRegisterType<T>(uri, versionMajor, versionMinor, qmlName);
636 qmlRegisterUncreatableMetaObject(Cvb::UI::staticMetaObject, uri, versionMajor, versionMinor,
"ZoomID",
637 "CVB: cannot object for enumerations");
638 qmlRegisterUncreatableMetaObject(Cvb::UI::staticMetaObject, uri, versionMajor, versionMinor,
"UploadMode",
639 "CVB: cannot object for enumerations");
654#pragma endregion CTor / DTor / Register
667 return CvbToQt(dispatcher_->ImageRect());
679 return CvbToQt(dispatcher_->ViewRect());
691 return CvbToQt(dispatcher_->TargetRect());
703 return CvbToQt(dispatcher_->SourceRect());
707 QObject *
Image()
const
723 return static_cast<int>(dispatcher_->ZoomID());
737 dispatcher_->UpdateZoomID(
static_cast<UI::ZoomID>(
id), [&]() { NotifyZoom(); });
749 return dispatcher_->ZoomFactor();
761 dispatcher_->UpdateZoomFactor(factor, [&]() { NotifyZoom(); });
796 return CvbToQt(dispatcher_->ViewAnchor());
808 dispatcher_->UpdateViewAnchor(
QtToCvb(viewAnchor), [&]() { NotifyViewAnchor(); });
820 return CvbToQt(dispatcher_->ImageAnchor());
832 dispatcher_->UpdateImageAnchor(
QtToCvb(imageAnchor), [&]() { NotifyImageAnchor(); });
847 return QVector<double>();
849 auto image = image_->Image();
850 if (!image || !dispatcher_->IsHoverPositionValid())
851 return QVector<double>();
853 auto hoverPosition = dispatcher_->HoverPosition();
855 static_cast<int>(
std::floor(hoverPosition.Y())));
858 if (hoverPositionInt.
X() == image->Width())
859 hoverPositionInt.
SetX(
static_cast<int>(hoverPosition.X() - 1.0));
860 if (hoverPositionInt.
Y() == image->Height())
861 hoverPositionInt.
SetY(
static_cast<int>(hoverPosition.Y() - 1.0));
863 return QVector<double>::fromStdVector(image->GetPixel(hoverPositionInt));
875 if (!dispatcher_->IsHoverPositionValid())
878 return CvbToQt(dispatcher_->HoverPosition());
892 return static_cast<int>(dispatcher_->UploadMode());
906 dispatcher_->UpdateUploadMode(
static_cast<UI::UploadMode>(uploadMode), [&]() { NotifyUploadMode(); });
909#pragma endregion Properties
915 void OnViewRectChanged()
917 UpdateTargetSource();
920 void OnImageRectChanged()
922 UpdateTargetSource();
925 void OnZoomFactorChanged()
927 UpdateTargetSource();
931 void OnZoomIDChanged()
933 UpdateTargetSource();
937 void OnViewAnchorChnaged()
939 auto targetPoint = dispatcher_->MapViewToTarget(dispatcher_->ViewAnchor());
940 targetPoint = dispatcher_->LimitPointToRect(targetPoint, dispatcher_->TargetRect());
942 auto sourcePoint = dispatcher_->MapTargetToSource(targetPoint);
943 auto imagePoint = dispatcher_->MapSourceToImage(sourcePoint);
945 dispatcher_->UpdateImageAnchor(imagePoint, [&]() { NotifyImageAnchor(); });
948#pragma endregion Slots
953 void UpdateTargetSource()
955 dispatcher_->UpdateTargetSource([&]() { NotifyTargetRect(); }, [&]() { NotifySourceRect(); });
958 QImage ScreenImage() const noexcept
960 auto data = dispatcher_->Buffer();
961 auto size = dispatcher_->BufferSize();
963 return QImage(data, size.Width(), size.Height(), QImage::Format_ARGB32_Premultiplied);
966#pragma endregion Helper
970 void paint(QPainter *painter)
override
973 Private::ImageViewDispatcherGuard guard(*dispatcher_);
974 if (!dispatcher_->IsBufferValid())
977 auto screenImage = ScreenImage();
979 if (dispatcher_->UploadMode() == UI::UploadMode::Image)
982 painter->drawImage(
CvbToQt(dispatcher_->TargetRect()), screenImage,
CvbToQt(dispatcher_->SourceRect()));
986 auto image = image_->Image();
990 painter->drawImage(
CvbToQt(dispatcher_->TargetRect()), screenImage,
CvbToQt(dispatcher_->SourceRectAdj()));
994 void geometryChanged(
const QRectF &newGeometry,
const QRectF &oldGeometry)
override
996 auto tmpNewGemotry =
QtToCvb(newGeometry);
997 if (tmpNewGemotry != dispatcher_->ViewRect())
998 dispatcher_->UpdateViewRect(tmpNewGemotry, [&]() { NotifyViewRect(); });
1000 QQuickPaintedItem::geometryChanged(newGeometry, oldGeometry);
1006 void wheelEvent(QWheelEvent *event)
override
1009 auto numDegrees =
event->angleDelta().y();
1011 if (numDegrees <= -15)
1013 else if (numDegrees >= 15)
1017 void mouseMoveEvent(QMouseEvent *event)
override
1019 if (event->buttons() & Qt::LeftButton)
1021 auto currentPos =
QtToCvb(event->pos());
1022 if (!dispatcher_->IsLastMousePositionValid())
1024 dispatcher_->InvalidateLastMousePosition(currentPos);
1029 dispatcher_->InvalidateLastMousePosition(currentPos);
1033 void mouseReleaseEvent(QMouseEvent *)
override
1035 dispatcher_->InvalidateLastMousePosition();
1038 void mousePressEvent(QMouseEvent *)
override
1043 void hoverMoveEvent(QHoverEvent *event)
override
1045 auto hoverPosition = dispatcher_->MapViewToImage(
QtToCvb(event->posF()));
1046 if (!dispatcher_->ImageRect().Contains(hoverPosition))
1049 dispatcher_->UpdateHoverPosition(hoverPosition, [&]() { NotifyHover(); });
1052#pragma endregion Reimplemented from Qt
1056 std::unique_ptr<UI::Private::ImageViewDispatcher> dispatcher_;
1058 ImageController *image_ =
nullptr;
1060#pragma endregion Members
1066 void NotifyImageRect();
1067 void NotifyViewRect();
1068 void NotifySourceRect();
1069 void NotifyTargetRect();
1075 void NotifyViewAnchor();
1076 void NotifyImageAnchor();
1080 void NotifyUploadMode();
1082#pragma endregion Notify
1085 inline ImageLabelItem::ImageLabelItem(QQuickItem *parent)
1086 : QQuickItem(parent)
1088 setTransformOrigin(QQuickItem::TopLeft);
1089 connect(
this, &ImageLabelItem::NotifyImageView,
this, &ImageLabelItem::OnImageViewChanged);
1092 inline void ImageLabelItem::OnImageXChanged()
1097 auto sourceRect = view_->SourceRect();
1098 auto sourceX = imageX_ - sourceRect.x();
1099 auto targetX = sourceX * view_->ZoomFactor();
1100 auto targetRect = view_->TargetRect();
1101 auto viewX = targetX + targetRect.x();
1106 inline void ImageLabelItem::OnImageYChanged()
1111 auto sourceRect = view_->SourceRect();
1112 auto sourceY = imageY_ - sourceRect.y();
1113 auto targetY = sourceY * view_->ZoomFactor();
1114 auto targetRect = view_->TargetRect();
1115 auto viewY = targetY + targetRect.y();
1120 inline void ImageLabelItem::OnImageViewChanged()
1125 OnLabelScaleChanged();
1128 inline void ImageLabelItem::OnLabelScaleChanged()
1130 if (labelScale_ == UI::LabelScale::Off)
1139 setScale(view_->ZoomFactor());
1142 inline void ImageLabelItem::SetupConnections()
1147 connect(
this, &ImageLabelItem::NotifyImageX,
this, &ImageLabelItem::OnImageXChanged);
1148 connect(
this, &ImageLabelItem::NotifyImageY,
this, &ImageLabelItem::OnImageYChanged);
1149 connect(
this, &ImageLabelItem::NotifyLabelScale,
this, &ImageLabelItem::OnLabelScaleChanged);
1151 connect(view_, &ImageViewItem::NotifySourceRect,
this, &ImageLabelItem::OnImageXChanged);
1152 connect(view_, &ImageViewItem::NotifySourceRect,
this, &ImageLabelItem::OnImageYChanged);
1153 connect(view_, &ImageViewItem::NotifyTargetRect,
this, &ImageLabelItem::OnImageXChanged);
1154 connect(view_, &ImageViewItem::NotifyTargetRect,
this, &ImageLabelItem::OnImageYChanged);
1155 connect(view_, &ImageViewItem::NotifyZoom,
this, &ImageLabelItem::OnImageXChanged);
1156 connect(view_, &ImageViewItem::NotifyZoom,
this, &ImageLabelItem::OnImageYChanged);
1158 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
Q_INVOKABLE QPointF MapImageToTarget(const QPointF &imagePoint) const
Maps a point from the image rectangle to the target rectangle.
Definition image_view_item.hpp:574
int ZoomID() const
Get the zoom identifier for special zoom steps (as integer, see Cvb::UI::ZoomID).
Definition image_view_item.hpp:721
void SetZoomID(int id)
Set the zoom identifier for special zoom steps (as integer, see Cvb::UI::ZoomID).
Definition image_view_item.hpp:735
QRectF SourceRect() const noexcept
Get the rectangle inside the image rectangle that is actually rendered.
Definition image_view_item.hpp:701
QPointF HoverPosition() const noexcept
Get the point the mouse is hovering over.
Definition image_view_item.hpp:873
QPointF ViewAnchor() const noexcept
Get the reference point for mouse operations in view coordinates.
Definition image_view_item.hpp:794
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:774
void SetViewAnchor(const QPointF &viewAnchor)
Set the reference point for mouse operations in view coordinates.
Definition image_view_item.hpp:806
QRectF ViewRect() const noexcept
Get the rectangle this items covers.
Definition image_view_item.hpp:677
QVector< double > HoverPixel() const
The image pixel value the mouse is currently hovered over.
Definition image_view_item.hpp:844
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:830
QRectF ImageRect() const noexcept
Get the rectangle described by the image buffer associated with the view.
Definition image_view_item.hpp:665
int UploadMode() const noexcept
Get the current upload mode (as integer, see Cvb::UI::UploadMode).
Definition image_view_item.hpp:890
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:649
QRectF TargetRect() const noexcept
Get the rectangle inside the view rectangle the image is actually rendered to.
Definition image_view_item.hpp:689
void SetZoomFactor(double factor)
Set the view's zoom factor.
Definition image_view_item.hpp:759
QPointF ImageAnchor() const noexcept
Get the reference point for mouse operations in image coordinates.
Definition image_view_item.hpp:818
void Refresh()
Refresh the currently shared image.
Definition image_view_item.hpp:587
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
Q_INVOKABLE QPointF MapImageToView(const QPointF &imagePoint) const
Maps a point from the image rectangle to the view rectangle.
Definition image_view_item.hpp:552
void SetUploadMode(int uploadMode)
Set the current upload mode (as integer).
Definition image_view_item.hpp:904
Q_INVOKABLE QPointF MapTargetToImage(const QPointF &targetPoint) const
Maps a point from the target rectangle to the image rectangle.
Definition image_view_item.hpp:563
double ZoomFactor() const
Get the view's zoom factor.
Definition image_view_item.hpp:747
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:633
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 version.hpp:11
std::shared_ptr< Image > ImagePtr
Convenience shared pointer for Image.
Definition global.hpp:86